package lingtao.net.controller;

import java.io.ByteArrayInputStream;
import java.io.File;
import java.io.IOException;
import java.util.Date;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

import javax.management.RuntimeErrorException;
import javax.servlet.http.HttpSession;

import org.apache.commons.io.FileUtils;
import org.apache.commons.io.FilenameUtils;
import org.apache.http.client.utils.DateUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.RestController;
import org.springframework.web.multipart.MultipartFile;

import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;

import lingtao.net.bean.CustomerTrainContent;
import lingtao.net.bean.CustomerTrainKindLabel;
import lingtao.net.bean.CustomerTrainProType;
import lingtao.net.bean.Msg;
import lingtao.net.service.CustomerTrainService;

@RestController
public class CustomerTrainController {

	@Autowired
	private CustomerTrainService customerTrainService;

	private String localPrefix = "abc\\train";

	private String localPath = "C:\\lingtao\\upload";

	private String localDomain = "http://47.114.150.226:80/erp";

	// 图片上传及新增
	@RequestMapping("/imgUpload")
	public Object upload(@RequestParam("file") MultipartFile file) throws Exception {
		if (file.isEmpty()) {
			return Msg.fail("文件不能为空");
		}
		// 获取文件名后缀
		String extension = FilenameUtils.getExtension(file.getOriginalFilename());
		// 获取path
		String path = getPath(extension, FilenameUtils.getBaseName(file.getOriginalFilename()));
		// 保存文件信息
		File newFile = new File(localPath + File.separator + path);
		try {
			FileUtils.copyInputStreamToFile(new ByteArrayInputStream(file.getBytes()), newFile);
		} catch (IOException e) {
			throw new RuntimeErrorException(null, "");
		}
		String serverPath = localDomain + "/" + path;
		Map<String, Object> map = new HashMap<String, Object>();
		Map<String, Object> map2 = new HashMap<String, Object>();
		map.put("code", 0); // 0表示上传成功
		map.put("msg", "上传成功"); // 提示消息
		// src返回图片上传成功后的下载路径，这里直接给绝对路径
		map2.put("src", serverPath);
		map.put("data", map2);
		return map;
	}

	/**
	 *
	 * @param prefixSelf 根据上传的接口存入自己的文件夹
	 * @param suffix     文件的后缀
	 * @param fileName   文件名
	 * @return
	 */
	public String getPath(String suffix, String fileName) {

		// 生成uuid
		// String uuid = UUID.randomUUID().toString().replaceAll("-", "");
		String path = null;

		// 文件路径
		path = DateUtils.formatDate(new Date(), "yyyyMMdd") + File.separator + fileName;

		path = localPrefix + File.separator + File.separator + path;

		return path + "." + suffix;
	}

	/* =============产品知识内容================ */
	/**
	 * 查询
	 * 
	 * @param page
	 * @param limit
	 * @param customerTrainContent
	 * @return
	 */
	@RequestMapping("/getCustomerTrainContents")
	public PageInfo<CustomerTrainContent> getCustomerTrainContents(
			@RequestParam(value = "page", defaultValue = "1") Integer page,
			@RequestParam(value = "limit", defaultValue = "10") Integer limit,
			CustomerTrainContent customerTrainContent) {
		PageHelper.startPage(page, limit);
		List<CustomerTrainContent> customerTrainContentList = customerTrainService
				.getCustomerTrainContents(customerTrainContent);
		PageInfo<CustomerTrainContent> pageInfo = new PageInfo<CustomerTrainContent>(customerTrainContentList);
		return pageInfo;
	}

	/**
	 * 添加
	 * 
	 */
	@RequestMapping("/addCustomerTrainContent")
	public Msg addCustomerTrainContent(CustomerTrainContent customerTrainContent, HttpSession session) {

		return customerTrainService.addCustomerTrainContent(customerTrainContent, session);
	}

	/**
	 * 修改
	 */
	@RequestMapping("/updateCustomerTrainContent")
	public Msg updateCustomerTrainContent(CustomerTrainContent customerTrainContent, HttpSession session) {
		return customerTrainService.updateCustomerTrainContentById(customerTrainContent, session);
	}

	/**
	 * 修改
	 */
	@RequestMapping("/updateCustomerTrainContentSort")
	public Msg updateById(@RequestParam(value = "id") int id, @RequestParam(value = "value") String value) {
		return customerTrainService.updateCustomerTrainContentSort(id, value);
	}

	/**
	 * 删除
	 */
	@RequestMapping("/deleteCustomerTrainContent")
	public Msg deleteCustomerTrainContent(@RequestParam("id") Integer id) {
		return customerTrainService.deleteCustomerTrainContentById(id);
	}

	/* =============产品种类================ */
	/**
	 * 查询
	 * 
	 * @param page
	 * @param limit
	 * @param customerTrainProType
	 * @return
	 */
	@RequestMapping("/getCustomerTrainProTypes")
	public PageInfo<CustomerTrainProType> getCustomerTrainProTypes(
			@RequestParam(value = "page", defaultValue = "1") Integer page,
			@RequestParam(value = "limit", defaultValue = "10") Integer limit,
			CustomerTrainProType customerTrainProType) {
		PageHelper.startPage(page, limit);
		List<CustomerTrainProType> customerTrainProTypeList = customerTrainService
				.getCustomerTrainProTypes(customerTrainProType);
		PageInfo<CustomerTrainProType> pageInfo = new PageInfo<CustomerTrainProType>(customerTrainProTypeList);
		return pageInfo;
	}

	/**
	 * 添加
	 * 
	 */
	@RequestMapping("/addCustomerTrainProType")
	public Msg addCustomerTrainProType(CustomerTrainProType customerTrainProType, HttpSession session) {

		return customerTrainService.addCustomerTrainProType(customerTrainProType, session);
	}

	/**
	 * 修改
	 */
	@RequestMapping("/updateCustomerTrainProType")
	public Msg updateCustomerTrainProType(CustomerTrainProType customerTrainProType, HttpSession session) {
		return customerTrainService.updateCustomerTrainProTypeById(customerTrainProType, session);
	}

	/**
	 * 删除
	 */
	@RequestMapping("/deleteCustomerTrainProType")
	public Msg deleteCustomerTrainProType(@RequestParam("id") Integer id) {
		return customerTrainService.deleteCustomerTrainProTypeById(id);
	}

	/* =============产品类型================ */
	/**
	 * 查询
	 * 
	 * @param page
	 * @param limit
	 * @param customerTrainKindLabel
	 * @return
	 */
	@RequestMapping("/getCustomerTrainKindLabelsByProType")
	public PageInfo<CustomerTrainKindLabel> getCustomerTrainKindLabelsByProType(
			@RequestParam(value = "page", defaultValue = "1") Integer page,
			@RequestParam(value = "limit", defaultValue = "10") Integer limit,
			@RequestParam(value = "needPage") String needPage, CustomerTrainKindLabel customerTrainKindLabel) {
		// 0:不要分页 1:要分页
		if ("1".equals(needPage))
			PageHelper.startPage(page, limit);
		List<CustomerTrainKindLabel> customerTrainKindLabelList = customerTrainService
				.getCustomerTrainKindLabelsByProType(customerTrainKindLabel);
		PageInfo<CustomerTrainKindLabel> pageInfo = new PageInfo<CustomerTrainKindLabel>(customerTrainKindLabelList);
		return pageInfo;
	}

	/**
	 * 添加
	 * 
	 */
	@RequestMapping("/addCustomerTrainKindLabel")
	public Msg addCustomerTrainKindLabel(CustomerTrainKindLabel customerTrainKindLabel, HttpSession session) {

		return customerTrainService.addCustomerTrainKindLabel(customerTrainKindLabel, session);
	}

	/**
	 * 修改
	 */
	@RequestMapping("/updateCustomerTrainKindLabel")
	public Msg updateCustomerTrainKindLabel(CustomerTrainKindLabel customerTrainKindLabel, HttpSession session) {
		return customerTrainService.updateCustomerTrainKindLabelById(customerTrainKindLabel, session);
	}

	/**
	 * 删除
	 */
	@RequestMapping("/deleteCustomerTrainKindLabel")
	public Msg deleteCustomerTrainKindLabel(@RequestParam("id") Integer id) {
		return customerTrainService.deleteCustomerTrainKindLabelById(id);
	}
}
