package com.goafanti.personnel.controller;


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

import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;

import com.goafanti.common.bo.Result;
import com.goafanti.common.constant.AFTConstants;
import com.goafanti.common.constant.ErrorConstants;
import com.goafanti.common.controller.CertifyApiController;
import com.goafanti.common.utils.DateUtils;
import com.goafanti.common.utils.StringUtils;
import com.goafanti.personnel.bo.InputPersonnelChooseBo;
import com.goafanti.personnel.bo.InputPersonnelBo;
import com.goafanti.personnel.bo.InputPersonnelContactBo;
import com.goafanti.personnel.bo.InputPersonnelContractBo;
import com.goafanti.personnel.bo.InputPersonnelEntryBo;
import com.goafanti.personnel.bo.InputPersonnelListBo;
import com.goafanti.personnel.bo.InputPersonnelLvlBo;
import com.goafanti.personnel.bo.InputPersonnelPromotionBo;
import com.goafanti.personnel.bo.InputPersonnelQuitBo;
import com.goafanti.personnel.bo.IntpersonnelWelfareBo;
import com.goafanti.personnel.service.PersonnelService;


@Controller
@RequestMapping(value = "/api/admin/personnel")
public class AdminPersonnelApiController extends CertifyApiController {

	@Resource
	private PersonnelService personnelService;
 
	
	/**
	 * 新增人事档案
	 */
	@RequestMapping(value = "/add", method = RequestMethod.POST)
	public Result addPersonnel(InputPersonnelBo i){
		Result res = new Result();
		if (StringUtils.isBlank(i.getName())) {
			res.getError().add(buildError(ErrorConstants.PARAM_ERROR, "姓名", "姓名"));
			return res;
		}
		if (i.getDoorId()==null) {
			res.getError().add(buildError(ErrorConstants.PARAM_ERROR, "门禁", "门禁"));
			return res;
		}
		if (i.getEmployeeId()==null) {
			res.getError().add(buildError(ErrorConstants.PARAM_ERROR, "员工编号", "员工编号"));
			return res;
		}
		int x=personnelService.checkDoorId(i.getDoorId(),null);
		if(x>0) {
			res.getError().add(buildError(ErrorConstants.PARAM_BEING_ERROR, "门禁", "门禁"));
			return res;
		}
		int y=personnelService.checkEmployeeId(i.getEmployeeId(),null);
		if (y>0) {
			res.getError().add(buildError(ErrorConstants.PARAM_BEING_ERROR, "员工编号", "员工编号"));
			return res;
		}
		if ( !StringUtils.isBlank(i.getBirthdays())) {
				i.setBirthday(DateUtils.StringToDate(i.getBirthdays(), AFTConstants.YYYYMMDD));
			
		}
		return res.data(personnelService.addPersonnel(i));
	}
	
	/**
	 * 门禁检测
	 */
	@RequestMapping(value ="/checkDoorId", method =RequestMethod.GET)
	public Result checkDoorId(Integer doorId,Integer id) {
		Result res = new Result();
		if (null==doorId) {
			res.getError().add(buildError(ErrorConstants.PARAM_ERROR, "门禁", "门禁"));
			return res;
		}
		int i=personnelService.checkDoorId(doorId,id);
		if (i>0) {
			res.getError().add(buildError(ErrorConstants.PARAM_BEING_ERROR, "门禁", "门禁"));
			return res;
		}
		return res.data(i);
	}
	
	/**
	 * 员工编号检测
	 */
	@RequestMapping(value ="/checkEmployeeId", method =RequestMethod.GET)
	public Result checkEmployeeId(Integer employeeId,Integer id) {
		Result res = new Result();
		if (null==employeeId) {
			res.getError().add(buildError(ErrorConstants.PARAM_ERROR, "员工编号", "员工编号"));
			return res;
		}
		int i=personnelService.checkEmployeeId(employeeId,id);
		if (i>0) {
			res.getError().add(buildError(ErrorConstants.PARAM_BEING_ERROR, "员工编号", "员工编号"));
			return res;
		}
		return res.data(i);
	}
	
	/**
	 * 修改人事档案
	 */
	@RequestMapping(value = "/update", method = RequestMethod.POST)
	public Result updatePersonnel(InputPersonnelBo i){
		Result res = new Result();
		if (i.getDoorId()!=null) {
			int x=personnelService.checkDoorId(i.getDoorId(),i.getId());
			if(x>0) {
				res.getError().add(buildError(ErrorConstants.PARAM_BEING_ERROR, "门禁", "门禁"));
				return res;
			}
		}
		if (i.getEmployeeId()!=null) {
			int y=personnelService.checkEmployeeId(i.getEmployeeId(),i.getId());
			if (y>0) {
				res.getError().add(buildError(ErrorConstants.PARAM_BEING_ERROR, "员工编号", "员工编号"));
				return res;
			}
		}
		if ( !StringUtils.isBlank(i.getBirthdays())) {
				i.setBirthday(DateUtils.StringToDate(i.getBirthdays(), AFTConstants.YYYYMMDD));
			
		}
		if ( !StringUtils.isBlank(i.getBirthdays()))i.setBirthday(DateUtils.StringToDate(i.getBirthdays(), AFTConstants.YYYYMMDD));
		if ( !StringUtils.isBlank(i.getEntryTimes()))i.setEntryTime(DateUtils.StringToDate(i.getEntryTimes(), AFTConstants.YYYYMMDD));
		if ( !StringUtils.isBlank(i.getPromotionTimes()))i.setPromotionTime(DateUtils.StringToDate(i.getPromotionTimes(), AFTConstants.YYYYMMDD));
		return res.data(personnelService.updatePersonnel(i));
	}
	
	/**
	 * 修改人事联系人
	 */
	@RequestMapping(value = "/updateContact", method = RequestMethod.POST)
	public Result updateContact(InputPersonnelContactBo i){
		Result res = new Result();
		return res.data(personnelService.updateContact(i));
	}
	
	/**
	 * 修改人事联系人
	 */
	@RequestMapping(value = "/updateEntry", method = RequestMethod.POST)
	public Result updateEntry(InputPersonnelEntryBo i){
		Result res = new Result();
		return res.data(personnelService.updateEntry(i));
	}
	
	/**
	 * 修改人事晋升
	 */
	@RequestMapping(value = "/updatePromotion", method = RequestMethod.POST)
	public Result updatePromotion(InputPersonnelPromotionBo i){
		Result res = new Result();
		if (null==i.getPdId()) {
			res.getError().add(buildError(ErrorConstants.PARAM_ERROR, "编号", "编号"));
			return res;
		}
		if ( !StringUtils.isBlank(i.getPromotionTimes()))i.setPromotionTime(DateUtils.StringToDate(i.getPromotionTimes(), AFTConstants.YYYYMMDD));
		return res.data(personnelService.updatePromotion(i));
	}
	
	/**
	 * 修改人事合同
	 */
	@RequestMapping(value = "/updateContract", method = RequestMethod.POST)
	public Result updateContract(InputPersonnelContractBo i){
		Result res = new Result();
		if (null==i.getPdId()) {
			res.getError().add(buildError(ErrorConstants.PARAM_ERROR, "编号", "编号"));
			return res;
		}
		if ( !StringUtils.isBlank(i.getStartTimes()))i.setStartTime(DateUtils.StringToDate(i.getStartTimes(), AFTConstants.YYYYMMDD));
		if ( !StringUtils.isBlank(i.getEndTimes()))i.setEndTime(DateUtils.StringToDate(i.getEndTimes(), AFTConstants.YYYYMMDD));
		return res.data(personnelService.updateContract(i));
	}
	
	/**
	 * 修改人事奖惩
	 */
	@RequestMapping(value = "/updateChoose", method = RequestMethod.POST)
	public Result updateChoose(InputPersonnelChooseBo i){
		Result res = new Result();
		if (null==i.getPdId()) {
			res.getError().add(buildError(ErrorConstants.PARAM_ERROR, "编号", "编号"));
			return res;
		}
		if ( !StringUtils.isBlank(i.getEffectTimes()))i.setEffectTime(DateUtils.StringToDate(i.getEffectTimes(), AFTConstants.YYYYMMDD));
		return res.data(personnelService.updateChoose(i));
	}
	
	/**
	 * 修改人事离职
	 */
	@RequestMapping(value = "/updateQuit", method = RequestMethod.POST)
	public Result updateQuit(InputPersonnelQuitBo i){
		Result res = new Result();
		if (null==i.getPdId()) {
			res.getError().add(buildError(ErrorConstants.PARAM_ERROR, "编号", "编号"));
			return res;
		}
		if ( !StringUtils.isBlank(i.getEffectTimes()))i.setEffectTime(DateUtils.StringToDate(i.getEffectTimes(), AFTConstants.YYYYMMDD));
		return res.data(personnelService.updateQuit(i));
	}
	
	/**
	 * 人事列表
	 */
	@RequestMapping(value = "/selectList", method = RequestMethod.GET)
	public Result selectList(InputPersonnelListBo i){
		Result res = new Result();
		return res.data(personnelService.selectList(i));
	}
	
	
	/**
	 * 人事等级列表
	 */
	@RequestMapping(value = "/selectLvlList", method = RequestMethod.GET)
	public Result selectLvlList(){
		Result res = new Result();
		return res.data(personnelService.selectLvlList());
	}
	
	/**
	 * 人事福利修改
	 */
	@RequestMapping(value = "/updateWelfare", method = RequestMethod.POST)
	public Result updateWelfare(IntpersonnelWelfareBo i){
		Result res = new Result();
		if (null==i.getPdId()) {
			res.getError().add(buildError(ErrorConstants.PARAM_ERROR, "编号", "编号"));
			return res;
		}
		if ( !StringUtils.isBlank(i.getStartTimes())) {
			i.setStartTime(DateUtils.StringToDate(i.getStartTimes(), AFTConstants.YYYYMMDD));
		}else {
			i.setStartTime(null);
		}
		if ( !StringUtils.isBlank(i.getEndTimes())) {
			i.setEndTime(DateUtils.StringToDate(i.getEndTimes(), AFTConstants.YYYYMMDD));
		}else {
			i.setEndTime(null);
		}
		return res.data(personnelService.updateWelfare(i));
	}
	
	
	/**
	 * 人事等级修改
	 */
	@RequestMapping(value = "/updateLvl", method = RequestMethod.POST)
	public Result updateLvl(InputPersonnelLvlBo i){
		Result res = new Result();
		return res.data(personnelService.updateLvl(i));
	}
	/**
	 * 人事详情
	 */
	@RequestMapping(value = "/selectBypdId", method = RequestMethod.GET)
	public Result selectBypdId(Integer id){
		Result res = new Result();
		if (null==id) {
			res.getError().add(buildError(ErrorConstants.PARAM_ERROR, "编号", "编号"));
			return res;
		}
		return res.data(personnelService.selectBypdId(id));
	}
	
	/**
	 * 	导出变更列表
     * 
     * @param response
     * @return
     */
    @RequestMapping(value = "/exportPersonnelList" , method = RequestMethod.GET)
    public Result exportPersonnelList(HttpServletResponse response,InputPersonnelListBo i)  {
    	Result res=new Result();
			try {
				personnelService.exportPersonnelList( response, i);
			} catch (Exception e) {
				res.getError().add(buildError("格式不正确"));
				e.printStackTrace();
				return res;
			}
         res.data(1); 
    	return res;
    }
	
	/** 证件上传 **/
	@RequestMapping(value = "/uploadFile", method = RequestMethod.POST)
	public Result uploadRefundOrderFile(HttpServletRequest req,String sign){
		Result res = new Result();
		//order_refund_file
		res.setData(handleFile(res, "/personnel/", false, req, sign));
		return res;
	}
	
}
