package com.unis.base.server.controller;

import java.util.List;

import org.apache.commons.lang3.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.http.HttpStatus;
import org.springframework.web.bind.annotation.DeleteMapping;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.PutMapping;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import com.github.pagehelper.PageInfo;
import com.unis.common.utils.ResponseWrapper;
import com.unis.base.common.vo.DRyxxsjyVO;
import com.unis.base.server.entity.DRyxxsjyDO;
import com.unis.base.server.service.DRyxxsjyService;

import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import lombok.extern.slf4j.Slf4j;

/**
 * @author huangxing
 * @version 1.0
 * @Title DRyxxsjyController
 * @ProjectName 人员信息 Controller
 * @Description
 * @date 2019/07/22 17点26分
 */
@RestController
@Api(value = "人员信息", tags = "人员信息维护")
@Slf4j
public class PersonnelController {

	@Autowired
	private DRyxxsjyService dRyxxsjyService;

	/**
	 * @author huangxing
	 * @Description 获取人员列表
	 * @param 人员编号，人员名称，库点编号，库点名称，岗位代码
	 * @return 人员信息列表
	 */
	@ApiOperation(value = "获取人员信息列表", notes = "参数：人员编号，人员名称，库点编号，库点名称，岗位代码")
	@PostMapping(value = "/personnelList")
    public ResponseWrapper getDRyxxsjyList(@RequestBody DRyxxsjyVO dRyxxsjyVO){
        try {
        	List<DRyxxsjyVO> dRyxxsjyDOs = dRyxxsjyService.getDRyxxsjyList(dRyxxsjyVO);
            return ResponseWrapper.success(HttpStatus.OK.value(), HttpStatus.OK.getReasonPhrase(), dRyxxsjyDOs);
        } catch (Exception e) {
            e.printStackTrace();
            log.error(e.getMessage());
        }
        return ResponseWrapper.error(HttpStatus.INTERNAL_SERVER_ERROR.value(), HttpStatus.INTERNAL_SERVER_ERROR.getReasonPhrase(),"服务错误，请联系管理员");
    }

	/**
	 * @author huangxing
	 * @Description 获取人员信息列表
	 * @param 人员编号，人员名称，库点编号，库点名称，岗位代码
	 * @return 人员信息列表
	 */
	@ApiOperation(value = "分页获取人员信息列表", notes = "参数：人员编号，人员名称，库点编号，库点名称，岗位代码")
	@PostMapping(value = "/personnelPage/{pageNum}/{pageSize}")
	public ResponseWrapper getDRyxxsjyPage(@RequestBody DRyxxsjyVO dRyxxsjyVO,@PathVariable("pageNum")Integer pageNum,@PathVariable("pageSize") Integer pageSize) {
		try {
			PageInfo<DRyxxsjyVO> pageInfos = new PageInfo<DRyxxsjyVO>(dRyxxsjyService.getDRyxxsjyPage(dRyxxsjyVO,pageNum,pageSize)) ;
            return ResponseWrapper.success(HttpStatus.OK.value(), HttpStatus.OK.getReasonPhrase(), pageInfos);
        } catch (Exception e) {
            e.printStackTrace();
            log.error(e.getMessage());
        }
		return ResponseWrapper.error(HttpStatus.INTERNAL_SERVER_ERROR.value(), HttpStatus.INTERNAL_SERVER_ERROR.getReasonPhrase(),"服务错误，请联系管理员");
	}


	/**
	 * @author huangxing
	 * @Description 根据人员编码获取人员信息
	 * @return 库点信息
	 */
	@ApiOperation(value = "根据人员编码获取人员信息", notes = "参数：库点编号")
	@GetMapping(value = "/personnel/{rybh}")
	public ResponseWrapper getDRyxxsjyInfoByrybh(@PathVariable("rybh")String rybh) {
		try {
			DRyxxsjyDO dKdxxsjyDO = dRyxxsjyService.getDRyxxsjyInfoByrybh(rybh);
            return ResponseWrapper.success(HttpStatus.OK.value(), HttpStatus.OK.getReasonPhrase(), dKdxxsjyDO);
        } catch (Exception e) {
            e.printStackTrace();
            log.error(e.getMessage());
        }
        return ResponseWrapper.error(HttpStatus.INTERNAL_SERVER_ERROR.value(), HttpStatus.INTERNAL_SERVER_ERROR.getReasonPhrase(),"服务错误，请联系管理员");
	}

	/**
	 * @author huangxing
	 * @Description 根据库点编码获取库点信息
	 * @return 库点信息
	 */
	@ApiOperation(value = "更新人员信息", notes = "参数：人员姓名，所属库点代码")
	@PutMapping(value = "/personnel")
	public ResponseWrapper update(@RequestBody DRyxxsjyVO dRyxxsjyVO) {
		try{
			if(StringUtils.isEmpty(dRyxxsjyVO.getRybh())) {
				return ResponseWrapper.success(HttpStatus.BAD_REQUEST.value(), "有必传参数为空", null);
			}
			int x = dRyxxsjyService.update(dRyxxsjyVO);
			if(x>0) {
	            return ResponseWrapper.success(HttpStatus.OK.value(), HttpStatus.OK.getReasonPhrase(),null);
			} else if (x==0) {
	            return ResponseWrapper.success(HttpStatus.BAD_REQUEST.value(), "该人员不存在", null);
			}
        } catch (Exception e) {
            e.printStackTrace();
            log.error(e.getMessage());
        }
        return ResponseWrapper.error(HttpStatus.INTERNAL_SERVER_ERROR.value(), HttpStatus.INTERNAL_SERVER_ERROR.getReasonPhrase(),"服务错误，请联系管理员");
	}

	/**
	 * @author WangZL
	 * @Description 根据库点编码获取库点信息
	 * @return 库点信息
	 */
	@ApiOperation(value = "人员信息-带状态更新", notes = "参数：人员姓名，所属库点代码")
	@PutMapping(value = "/personnelUpdate")
	public ResponseWrapper personnelUpdate(@RequestBody DRyxxsjyVO dRyxxsjyVO) {
		try{
			if(StringUtils.isEmpty(dRyxxsjyVO.getRybh())) {
				return ResponseWrapper.success(HttpStatus.BAD_REQUEST.value(), "有必传参数为空", null);
			}
			int x = dRyxxsjyService.personnelUpdate(dRyxxsjyVO);
			if(x>0) {
				return ResponseWrapper.success(HttpStatus.OK.value(), HttpStatus.OK.getReasonPhrase(),null);
			} else if (x==0) {
				return ResponseWrapper.success(HttpStatus.BAD_REQUEST.value(), "该人员不存在", null);
			}
		} catch (Exception e) {
			e.printStackTrace();
			log.error(e.getMessage());
		}
		return ResponseWrapper.error(HttpStatus.INTERNAL_SERVER_ERROR.value(), HttpStatus.INTERNAL_SERVER_ERROR.getReasonPhrase(),"服务错误，请联系管理员");
	}

	/**
	 * @author huangxing
	 * @Description 删除企业信息
	 * @param 参数：社会行政代码
	 * @return 状态
	 */
	@DeleteMapping(value="/personnel/{rybh}")
	@ApiOperation(value = "根据人员编号删除人员信息", notes = "参数：人员编号")
	public ResponseWrapper delete(@PathVariable("rybh")String rybh) {
		try{
			int x = dRyxxsjyService.delete(rybh);
			if(x>0) {
	            return ResponseWrapper.success(HttpStatus.OK.value(), HttpStatus.OK.getReasonPhrase(),null);
			} else if (x==0) {
	            return ResponseWrapper.success(HttpStatus.BAD_REQUEST.value(), "删除失败", null);
			} else {
				 return ResponseWrapper.success(HttpStatus.BAD_REQUEST.value(), "删除失败,请人员编号是否正确", null);
			}
        } catch (Exception e) {
            e.printStackTrace();
            log.error(e.getMessage());
        }
        return ResponseWrapper.error(HttpStatus.INTERNAL_SERVER_ERROR.value(), HttpStatus.INTERNAL_SERVER_ERROR.getReasonPhrase(),"服务错误，请联系管理员");
    }

	/**
	 * @author huangxing
	 * @Description 新增人员信息
	 * @param 人员信息实体 models
	 * @return 新增状态
	 */
	@PostMapping(value = "/personnel")
	@ApiOperation(value = "新增人员信息", notes = "参数：人员编号")
    public ResponseWrapper insert(@RequestBody DRyxxsjyVO dRyxxsjyVO) {
		try{
			/*if(*//*StringUtils.isEmpty(dRyxxsjyVO.getRyxm())||*//*StringUtils.isEmpty(dRyxxsjyVO.getKddm())) {
				return ResponseWrapper.success(HttpStatus.BAD_REQUEST.value(), "有必传参数为空", null);
			}*/
			int x = dRyxxsjyService.insert(dRyxxsjyVO);
			if(x>0) {
	            return ResponseWrapper.success(HttpStatus.OK.value(), HttpStatus.OK.getReasonPhrase(),null);
			} else if (x==0) {
	            return ResponseWrapper.success(HttpStatus.BAD_REQUEST.value(), "添加人员失败,该人员已存在", null);
			} else if (x==-1) {
	            return ResponseWrapper.success(HttpStatus.BAD_REQUEST.value(), "添加人员失败,该库点不存在", null);
			}
        } catch (Exception e) {
            e.printStackTrace();
            log.error(e.getMessage());
        }
        return ResponseWrapper.error(HttpStatus.INTERNAL_SERVER_ERROR.value(), HttpStatus.INTERNAL_SERVER_ERROR.getReasonPhrase(),"服务错误，请联系管理员");
    }

	/**
	 * @author WangZL
	 * @Description 根据库点代码删除人员列表
	 * @param 库点代码
	 * @return 状态
	 */
	@DeleteMapping(value="/personnelList/{kddm}")
	@ApiOperation(value = "人员信息-根据库点代码删除人员列表", notes = "参数：库点代码")
	public ResponseWrapper deleteList(@PathVariable("kddm")String kddm) {
		try{
			int x = dRyxxsjyService.deleteList(kddm);
			if(x>0) {
				return ResponseWrapper.success(HttpStatus.OK.value(), HttpStatus.OK.getReasonPhrase(),null);
			} else if (x==0) {
				return ResponseWrapper.success(HttpStatus.OK.value(), HttpStatus.OK.getReasonPhrase(), null);
			} else {
				return ResponseWrapper.success(HttpStatus.BAD_REQUEST.value(), "删除失败,请库点代码是否正确", null);
			}
		} catch (Exception e) {
			e.printStackTrace();
			log.error(e.getMessage());
		}
		return ResponseWrapper.error(HttpStatus.INTERNAL_SERVER_ERROR.value(), HttpStatus.INTERNAL_SERVER_ERROR.getReasonPhrase(),"服务错误，请联系管理员");
	}

	/**
	 * @author WangZL
	 * @Description 根据库点代码获取人员列表
	 * @param 库点代码
	 * @return 人员信息列表
	 */
	@ApiOperation(value = "人员信息-根据库点代码获取人员列表", notes = "参数：库点代码")
	@PostMapping(value = "/personnelListByKddm/{kddm}")
	public ResponseWrapper getPersonnelListByKddm(@PathVariable("kddm")String kddm){
		try {
			if (kddm == null) {
				return ResponseWrapper.success(HttpStatus.BAD_REQUEST.value(), "有必传参数为空", null);
			}
			List<DRyxxsjyVO> dRyxxsjyVOList = dRyxxsjyService.getPersonnelListByKddm(kddm);
			return ResponseWrapper.success(HttpStatus.OK.value(), HttpStatus.OK.getReasonPhrase(), dRyxxsjyVOList);
		} catch (Exception e) {
			e.printStackTrace();
			log.error(e.getMessage());
		}
		return ResponseWrapper.error(HttpStatus.INTERNAL_SERVER_ERROR.value(), HttpStatus.INTERNAL_SERVER_ERROR.getReasonPhrase(),"服务错误，请联系管理员");
	}

	/**
	 * @author WangZL
	 * @Description 根据库点代码获取人员列表
	 * @param 库点代码
	 * @return 人员信息列表
	 */
	@ApiOperation(value = "人员信息-根据库点代码获取质检，化验人员列表", notes = "参数：库点代码")
	@PostMapping(value = "/personnelZHListByKddm/{kddm}")
	public ResponseWrapper personnelZHListByKddm(@PathVariable("kddm")String kddm){
		try {
			if (kddm == null) {
				return ResponseWrapper.success(HttpStatus.BAD_REQUEST.value(), "有必传参数为空", null);
			}
			List<DRyxxsjyVO> dRyxxsjyVOList = dRyxxsjyService.personnelZHListByKddm(kddm);
			return ResponseWrapper.success(HttpStatus.OK.value(), HttpStatus.OK.getReasonPhrase(), dRyxxsjyVOList);
		} catch (Exception e) {
			e.printStackTrace();
			log.error(e.getMessage());
		}
		return ResponseWrapper.error(HttpStatus.INTERNAL_SERVER_ERROR.value(), HttpStatus.INTERNAL_SERVER_ERROR.getReasonPhrase(),"服务错误，请联系管理员");
	}

	/**
	 * 人员信息-备案主表状态
	 * @param dRyxxsjyVO
	 * @return
	 */
	@PostMapping(value = "/personnelFiling")
	@ApiOperation(value = "人员信息-备案主表状态", notes = "参数：人员编号，是否备案")
	public ResponseWrapper updateFiling(@RequestBody DRyxxsjyVO dRyxxsjyVO) {
		try {
			if (StringUtils.isEmpty(dRyxxsjyVO.getRybh()) || dRyxxsjyVO.getSfba() == null) {
				return ResponseWrapper.success(HttpStatus.BAD_REQUEST.value(), "有必传参数为空", null);
			}
			int i = dRyxxsjyService.updateFiling(dRyxxsjyVO);
			if (i > 0) {
				return ResponseWrapper.success(HttpStatus.OK.value(), HttpStatus.OK.getReasonPhrase(), "操作成功");
			} else {
				return ResponseWrapper.success(HttpStatus.BAD_REQUEST.value(), HttpStatus.BAD_REQUEST.getReasonPhrase(), "操作失败，请联系管理员");
			}
		} catch (Exception e) {
			log.error(e.getMessage());
			return ResponseWrapper.error(HttpStatus.INTERNAL_SERVER_ERROR.value(), HttpStatus.INTERNAL_SERVER_ERROR.getReasonPhrase(), "服务错误，请联系管理员");
		}
	}

	/**
	 * 人员信息-更改启用状态
	 * @param dRyxxsjyVO
	 * @return
	 */
	@PostMapping(value = "/personnelEnabled")
	@ApiOperation(value = "人员信息-更改启用状态", notes = "参数：人员编号，启用状态")
	public ResponseWrapper updateEnabled(@RequestBody DRyxxsjyVO dRyxxsjyVO) {
		try {
			if (StringUtils.isEmpty(dRyxxsjyVO.getRybh()) || dRyxxsjyVO.getQyzt() == null) {
				return ResponseWrapper.success(HttpStatus.BAD_REQUEST.value(), "有必传参数为空", null);
			}
			int i = dRyxxsjyService.updateEnabled(dRyxxsjyVO);
			if (i > 0) {
				return ResponseWrapper.success(HttpStatus.OK.value(), HttpStatus.OK.getReasonPhrase(), "操作成功");
			} else {
				return ResponseWrapper.success(HttpStatus.BAD_REQUEST.value(), HttpStatus.BAD_REQUEST.getReasonPhrase(), "操作失败，请联系管理员");
			}
		} catch (Exception e) {
			log.error(e.getMessage());
			return ResponseWrapper.error(HttpStatus.INTERNAL_SERVER_ERROR.value(), HttpStatus.INTERNAL_SERVER_ERROR.getReasonPhrase(), "服务错误，请联系管理员");
		}
	}
}
