package cn.com.countrygarden.bi.main.controller.sharppathevaluation;

import cn.com.countrygarden.bi.main.controller.BaseController;
import cn.com.countrygarden.bi.main.core.Response;
import cn.com.countrygarden.bi.main.core.Result;
import cn.com.countrygarden.bi.main.dto.PersonInfoVO;
import cn.com.countrygarden.bi.main.dto.personnel.QueryCriteriaDTO;
import cn.com.countrygarden.bi.main.exception.ServiceException;
import cn.com.countrygarden.bi.main.model.sharppathevaluation.PersonnelSharppathevaluation;
import cn.com.countrygarden.bi.main.service.personnel.PersonnelCommenService;
import cn.com.countrygarden.bi.main.service.sharppathevaluation.PersonnelSharppathevaluationService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;
import org.springframework.web.multipart.MultipartFile;
import org.springframework.web.multipart.commons.CommonsMultipartFile;

import java.io.IOException;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

/**
 * 锐途测评-业务逻辑层实现
 *
 * @author 汤婷
 * @email www.justintoforest@gamail.com
 * @url
 * @date 2018-09-25
 */
@RestController("PersonnelSharppathevaluationController")
@RequestMapping("/api/personnelSharppathevaluation")
public class PersonnelSharppathevaluationController extends BaseController {
	@Autowired
	private PersonnelSharppathevaluationService personnelSharppathevaluationService;

	@Autowired
	private PersonnelCommenService personnelInfoRoleService;

	@GetMapping(value = "/list", produces = "application/json;charset=utf-8")
	public Result list(PersonInfoVO personInfoVO,
			@RequestParam(value = "size", required = false, defaultValue = "10") int size,
			@RequestParam(value = "page", required = false, defaultValue = "1") int page, HttpServletRequest request) {

		return personnelSharppathevaluationService.queryPageList(personInfoVO, page, size, getAccessToken(request));
	}

	@GetMapping(value = "/one", produces = "application/json;charset=utf-8")
	public Result queryById(PersonnelSharppathevaluation personnelSharppathevaluation, HttpServletRequest request) {
		if (personnelSharppathevaluation.getPersonneltagId() == null
				&& personnelSharppathevaluation.getPersonnelId() == null) {
			throw new ServiceException("传递参数有误");
		}
		return personnelSharppathevaluationService.queryById(personnelSharppathevaluation);
	}

	@PostMapping(value = "/editOrAdd")
	public Result editOrAdd(PersonnelSharppathevaluation personnelSharppathevaluation, HttpServletRequest request) {
		if (personnelSharppathevaluation.getPersonnelId() != null
				&& personnelSharppathevaluation.getPersonneltagId() != null) {

		} else {
			throw new ServiceException("传递的参数错误");
		}
		return personnelSharppathevaluationService.editOrAdd(personnelSharppathevaluation, getAccessToken(request));
	}

	/**
	 * 导入文件
	 * 
	 * @param personnelSharppathevaluation
	 * @param request
	 * @return
	 */
	@ResponseBody
	@RequestMapping(value = "/export", produces = "application/json;charset=utf-8", method = RequestMethod.POST)
	public Result upload(@RequestParam(value = "name", defaultValue = "1", required = true) String name,
			@RequestParam(value = "radio", defaultValue = "0", required = true) Integer radio,
			@RequestParam(value = "uploadfiles", defaultValue = "", required = true) MultipartFile multipartFile,
			HttpServletRequest request)
			throws ServiceException {

		if (multipartFile == null) {
			throw new ServiceException("请选择需要上传的文件");
		} else if (name == null) {
			throw new ServiceException("请输入文件名");
		}
		CommonsMultipartFile commonsMultipartFile = (CommonsMultipartFile) multipartFile;
		if (commonsMultipartFile.isEmpty()) {
			return Response.responseFail("上传文件为空文件");
		}
		String fileName = commonsMultipartFile.getOriginalFilename();
		if (fileName.indexOf(".xlsx") <= 0 || fileName.indexOf(".xls") <= 0) {
			return Response.responseFail("上传文件应为excel文件格式为：xlsx 或 xls");
		}

		return Response.response(personnelSharppathevaluationService.exportExcel(fileName, multipartFile, radio,
				getAccessToken(request)));

	}

	/**
	 * 导出excel
	 * 
	 * @param isAll(1:导出全部，0：导出最新)
	 * @return
	 */
	@GetMapping(value = "/import", produces = "application/json;charset=utf-8")
	public Result importExcel(QueryCriteriaDTO queryCriteriaDTO, HttpServletRequest request,
			HttpServletResponse response) {
		if (queryCriteriaDTO.getIs_all() == null || queryCriteriaDTO.getPersonnelIds() == null) {
			return Response.response("传递参数有误");
		}

		try {
			return personnelSharppathevaluationService.importExcel(queryCriteriaDTO, getAccessToken(request), response);
		} catch (IOException e) {

			e.printStackTrace();
		}
		return ERROR_MSG;

	}

	@DeleteMapping(value = "/del")
	public Result del(@RequestParam(value = "id", required = true) Integer id) {

		return personnelSharppathevaluationService.del(id);
	}
	
	@DeleteMapping(value = "/delpiliang")
	public Result delpiliang(@RequestParam(value = "id", required = true) String id) {

		return personnelSharppathevaluationService.delpiliang(id);
	}

}