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

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 org.apache.commons.lang3.StringUtils;
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 com.alibaba.fastjson.JSON;
import com.alibaba.fastjson.TypeReference;

import java.io.IOException;
import java.util.ArrayList;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import cn.com.countrygarden.bi.main.dto.PersonInfoVO;
import cn.com.countrygarden.bi.main.dto.PersonnelWrittenexaminationVO;
import cn.com.countrygarden.bi.main.dto.personnel.QueryCriteriaDTO;
import cn.com.countrygarden.bi.main.exception.ServiceException;
import cn.com.countrygarden.bi.main.model.writtenExamination.PersonnelSelectionscore;
import cn.com.countrygarden.bi.main.service.writtenExamination.PersonnelSelectionscoreService;

/**
 * 笔试-业务逻辑层实现
 *
 * @author 汤婷
 * @email www.justintoforest@gamail.com
 * @url
 * @date 2018-10-17
 */
@RestController("PersonnelSelectionscoreController")
@RequestMapping("/api/personnelWrittenexamination")
public class PersonnelWrittenexaminationController extends BaseController {
	@Autowired
	private PersonnelSelectionscoreService personnelSelectionscoreService;

	@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 personnelSelectionscoreService.queryPageList(personInfoVO, page, size,
				getAccessToken(request));

	}

	@GetMapping(value = "/one", produces = "application/json;charset=utf-8")
	public Result queryById(PersonnelWrittenexaminationVO personnelWrittenexaminationVO, HttpServletRequest request) {
		if (personnelWrittenexaminationVO.getPersonnelId() != null
				&& personnelWrittenexaminationVO.getPersonneltagId() != null
				&& StringUtils.isNoneBlank(personnelWrittenexaminationVO.getSelectType())) {

		} else {
			throw new ServiceException("传递参数有误");
		}
		return personnelSelectionscoreService.queryById(personnelWrittenexaminationVO);
	}

	@PostMapping(value = "/add")
	public Result add(PersonnelWrittenexaminationVO personnelWrittenexaminationVO,
			@RequestParam(value = "List", required = false) String list, HttpServletRequest request) {

		if (personnelWrittenexaminationVO.getPersonnelId() != null
				&& personnelWrittenexaminationVO.getPersonneltagId() != null
				&& StringUtils.isNoneBlank(personnelWrittenexaminationVO.getSelectType())) {
		} else {
			throw new ServiceException("传递参数有误");
		}
		ArrayList<PersonnelSelectionscore> writtenexaminationVOList = JSON.parseObject(list,
				new TypeReference<ArrayList<PersonnelSelectionscore>>() {
				});
		return personnelSelectionscoreService.add(personnelWrittenexaminationVO, writtenexaminationVOList,
				getAccessToken(request));
	}

	@PutMapping(value = "/edit")
	public Result edit(PersonnelWrittenexaminationVO personnelWrittenexaminationVO,
			@RequestParam(value = "List", required = true) String list, HttpServletRequest request) {
		if (personnelWrittenexaminationVO.getPersonnelId() != null
				&& personnelWrittenexaminationVO.getPersonneltagId() != null
				&& StringUtils.isNoneBlank(personnelWrittenexaminationVO.getSelectType())) {
		} else {
			throw new ServiceException("传递参数有误");
		}
		ArrayList<PersonnelSelectionscore> writtenexaminationVOList = JSON.parseObject(list,
				new TypeReference<ArrayList<PersonnelSelectionscore>>() {
				});

		return personnelSelectionscoreService.edit(personnelWrittenexaminationVO, writtenexaminationVOList,
				getAccessToken(request));
	}

	@DeleteMapping(value = "/del")
	public Result del(PersonnelWrittenexaminationVO personnelWrittenexaminationVO) {
		if (personnelWrittenexaminationVO.getPersonnelId() != null
				&& personnelWrittenexaminationVO.getPersonneltagId() != null) {
		} else {
			throw new ServiceException("传递参数错误");
		}

		return personnelSelectionscoreService.del(personnelWrittenexaminationVO.getPersonnelId(),
				personnelWrittenexaminationVO.getPersonneltagId());
	}
	
	@DeleteMapping(value = "/delpiliang")
	public Result delpiliang(@RequestParam(value = "personnelId", defaultValue = "", required = true) String personnelId,
			@RequestParam(value = "personneltagId", defaultValue = "", required = true) String personneltagId,
			@RequestParam(value = "personnelSelectionTypeId", defaultValue = "", required = true) String personnelSelectionTypeId) {
		
		return personnelSelectionscoreService.delpiliang(personnelId,personneltagId,personnelSelectionTypeId);
		
	}

	@ResponseBody
	@RequestMapping(value = "/fileUpload", produces = "application/json;charset=utf-8", method = RequestMethod.POST)
	public Result upload(PersonnelWrittenexaminationVO personnelWrittenexaminationVO, HttpServletRequest request,
			@RequestParam(value = "name", defaultValue = "", required = true) String name,
			@RequestParam(value = "file", defaultValue = "", required = true) MultipartFile multipartFile)
			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("上传文件为空文件");
		}

		return Response.response(
				personnelSelectionscoreService.FileUpload(multipartFile, personnelWrittenexaminationVO, request));

	}

	/**
	 * 导出excel
	 * 
	 * @param isAll(1:导出全部，0：导出最新)
	 *            
	 * @return
	 */
	@GetMapping(value = "/excelImport", produces = "application/json;charset=utf-8")
	public Result excelImport(QueryCriteriaDTO queryCriteriaDTO, HttpServletRequest request,
			HttpServletResponse response) {
		if (queryCriteriaDTO.getIs_all() == null) {
			return Response.response("请选择导出数据的类型");
		}
		if (queryCriteriaDTO.getPersonnelIds() == null || queryCriteriaDTO.getPersonnelIds().equals("")) {
			return Response.response("请选择需要导出的数据");
		}
		try {
			return personnelSelectionscoreService.excelImport(queryCriteriaDTO, getAccessToken(request), response);
		} catch (IOException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}
		return Response.response();
	}

	/**
	 * 导入文件
	 * 
	 * @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 = "createName", defaultValue = "0", required = true) String createName,
			@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(personnelSelectionscoreService.exportExcel(fileName, createName, multipartFile, radio,
				getAccessToken(request)));

	}

}