package com.bonus.hn.bearing.capacity.backstage.personnelPool.controller;

import com.bonus.hn.bearing.capacity.backstage.companypro.entity.ProjectManagerBean;
import com.bonus.hn.bearing.capacity.backstage.corporateOrg.entity.CorporateBean;
import com.bonus.hn.bearing.capacity.backstage.manager.annotation.LogAnnotation;
import com.bonus.hn.bearing.capacity.backstage.manager.table.PageTableRequest;
import com.bonus.hn.bearing.capacity.backstage.manager.utils.AjaxRes;
import com.bonus.hn.bearing.capacity.backstage.manager.utils.ExcelUtils;
import com.bonus.hn.bearing.capacity.backstage.manager.utils.GlobalConst;
import com.bonus.hn.bearing.capacity.backstage.manager.utils.R;
import com.bonus.hn.bearing.capacity.backstage.personnelPool.entity.SelfOwnedBean;
import com.bonus.hn.bearing.capacity.backstage.personnelPool.entity.SubcontractorBean;
import com.bonus.hn.bearing.capacity.backstage.personnelPool.entity.SubsidiaryCompanyBean;
import com.bonus.hn.bearing.capacity.backstage.personnelPool.service.PersonnelPoolService;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import org.apache.commons.lang3.StringUtils;
import org.springframework.web.bind.annotation.*;
import org.springframework.web.multipart.MultipartFile;

import javax.annotation.Resource;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.io.IOException;
import java.text.ParseException;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

/**
 * @author jjLV
 * @date 2023/12/6 15:29 人员库管理
 */
@Api(tags = "人员库管理")
@RestController
@RequestMapping("/personnel")
public class PersonnelPoolController {

    @Resource
    private PersonnelPoolService service;

    /*******************************************************************分公司人员库*********************************************************************************************/
    @LogAnnotation
    @PostMapping("getSubsidiaryCompanyList")
    @ApiOperation(value = "分公司人员库-列表查询")
    public R getProjectManagementTeamsList(SubsidiaryCompanyBean bean){
        List<SubsidiaryCompanyBean> list = service.getSubsidiaryCompanyList(bean);
        return list.size() > 0 ? R.okTable(list, list.size()):R.failTable("暂无数据");
    }

    @LogAnnotation
    @PostMapping("switchListen")
    @ApiOperation(value = "分公司人员库-权限锁定")
    public AjaxRes switchListen(SubsidiaryCompanyBean bean) {
        AjaxRes ar = new AjaxRes();
        int result  = service.updateSwitchListen(bean);
        if(result==1){
            ar.setSucceedMsg(GlobalConst.DATA_SUCCEED);
        }else {
            ar.setFailMsg(GlobalConst.DATA_FAIL);
        }
        return ar;
    }

    @RequestMapping(value = "delById", method = RequestMethod.POST)
    @ResponseBody
    @ApiOperation(value = "分公司人员库-删除")
    public AjaxRes delById(String id) throws Exception {
        AjaxRes ar = new AjaxRes();
        int cb = service.delById(id);
        ar.setFailMsg(GlobalConst.DATA_FAIL);
        if(cb>0){
            ar.setFailMsg(GlobalConst.INIT_SUCCEED);
        }
        return ar;
    }

    @RequestMapping(value = "addSubsidiaryCompany", method = RequestMethod.POST)
    @ResponseBody
    @ApiOperation(value = "分公司人员库-新增")
    public AjaxRes addSubsidiaryCompany(@RequestBody SubsidiaryCompanyBean bean){
        AjaxRes ar = new AjaxRes();
        int cb = service.addSubsidiaryCompany(bean);
        ar.setFailMsg(GlobalConst.DATA_FAIL);
        if(cb>0){
            ar.setFailMsg(GlobalConst.INIT_SUCCEED);
            if (cb == 3){
                ar.setFailMsg("身份证号已存在");
            }
        }
        return  ar;
    }

    @RequestMapping(value = "updateSubsidiaryCompany", method = RequestMethod.POST)
    @ResponseBody
    @ApiOperation(value = "分公司人员库-修改")
    public AjaxRes updateSubsidiaryCompany(@RequestBody SubsidiaryCompanyBean bean){
        AjaxRes ar = new AjaxRes();
        int cb = service.updateSubsidiaryCompany(bean);
        ar.setFailMsg(GlobalConst.DATA_FAIL);
        if(cb>0){
            ar.setFailMsg(GlobalConst.INIT_SUCCEED);
            if (cb == 3){
                ar.setFailMsg("身份证号已存在");
            }
        }
        return  ar;
    }

    @LogAnnotation
    @PostMapping("getWorkRecordList")
    @ApiOperation(value = "分公司人员库-工作记录")
    public R getWorkRecordList(SubsidiaryCompanyBean bean){
        List<SubsidiaryCompanyBean> list = service.getWorkRecordList(bean);
        return list.size() > 0 ? R.okTable(list, list.size()):R.failTable("暂无数据");
    }

    /**
     * 文件上传请求
     */
    @LogAnnotation
    @PostMapping("/import")
    @ApiOperation(value = "分公司人员库-文件导入")
    public AjaxRes uploadVoucher(@RequestParam("file") MultipartFile file, @RequestParam("orgId") String orgId, HttpServletRequest request, HttpServletResponse response) throws IOException {
        AjaxRes ar = new AjaxRes();
        try {
            String code = service.importExcel(request,file,orgId);
            if ("导入成功".equals(code)){
                ar.setSucceedMsg("导入成功");
            }else{
                ar.setFailMsg(code);
            }
        } catch (Exception e) {
            e.printStackTrace();
        }
        return ar;
    }

    @RequestMapping(value = "/exportPhysicalCheckResult", method = RequestMethod.GET)
    @ApiOperation(value = "分公司人员库-导出")
    public void exportPayroll(HttpServletRequest request, HttpServletResponse response, PageTableRequest page) throws ParseException {
        SubsidiaryCompanyBean b = new SubsidiaryCompanyBean();
        b.setKeyWord(request.getParameter("keyword"));
        b.setOrgId(request.getParameter("orgId"));
        b.setVoltageLevelId(request.getParameter("voltageLevelId"));
        List<SubsidiaryCompanyBean> list = service.getSubsidiaryCompanyList(b);
        List<Map<String, Object>> mapList = new ArrayList<Map<String, Object>>();
        String[] excelHeader = {"序号","分公司","姓名","性别","身份证","执证情况"
                ,"职称/技能等级","所在项目","项目管理团队名称","电压等级","岗位"};
        int[] ds_format = {2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2};
        String fileName = "分公司项目管理资源-人员信息";
        if (list != null) {
            int i = 0;
            for (SubsidiaryCompanyBean bean : list){
                i++;
                Map<String, Object> map = new HashMap<>(100);
                map.put("序号",i);
                map.put("分公司",bean.getOrgName());
                map.put("姓名",bean.getPersonName());
                map.put("性别",bean.getSex());
                map.put("身份证",bean.getId());
                map.put("执证情况",bean.getCertificate());
                map.put("职称/技能等级",bean.getLevel());
                map.put("所在项目",bean.getProName());
                map.put("项目管理团队名称",bean.getProTeamName());
                map.put("电压等级",bean.getDictKey());
                map.put("岗位",bean.getManagePersonName());
                mapList.add(map);
            }
        }
        try {
            ExcelUtils.export(fileName, fileName, excelHeader, excelHeader, ds_format, null, mapList, request,
                    response);
        } catch (IOException e) {
            e.printStackTrace();
        }
    }

    /*******************************************************************分包商人员库*********************************************************************************************/
    @LogAnnotation
    @PostMapping("getSubcontractorList")
    @ApiOperation(value = "分包商人员库-列表查询")
    public R getSubcontractorList(SubcontractorBean bean){
        List<SubcontractorBean> list = service.getSubcontractorList(bean);
        return list.size() > 0 ? R.okTable(list, list.size()):R.failTable("暂无数据");
    }

    @LogAnnotation
    @PostMapping("subcontractorSwitchListen")
    @ApiOperation(value = "分包商人员库-权限锁定")
    public AjaxRes subcontractorSwitchListen(SubcontractorBean bean) {
        AjaxRes ar = new AjaxRes();
        int result  = service.updateSubcontractorSwitchListen(bean);
        if(result==1){
            ar.setSucceedMsg(GlobalConst.DATA_SUCCEED);
        }else {
            ar.setFailMsg(GlobalConst.DATA_FAIL);
        }
        return ar;
    }

    @RequestMapping(value = "delSubcontractorById", method = RequestMethod.POST)
    @ResponseBody
    @ApiOperation(value = "分包商人员库-删除")
    public AjaxRes delSubcontractorById(String id) throws Exception {
        AjaxRes ar = new AjaxRes();
        int cb = service.delSubcontractorById(id);
        ar.setFailMsg(GlobalConst.DATA_FAIL);
        if(cb>0){
            ar.setFailMsg(GlobalConst.INIT_SUCCEED);
        }
        return ar;
    }

    @RequestMapping(value = "addSubcontractor", method = RequestMethod.POST)
    @ResponseBody
    @ApiOperation(value = "分包商人员库-新增")
    public AjaxRes addSubcontractor(@RequestBody SubcontractorBean bean){
        AjaxRes ar = new AjaxRes();
        int cb = service.addSubcontractor(bean);
        ar.setFailMsg(GlobalConst.DATA_FAIL);
        if(cb>0){
            ar.setFailMsg(GlobalConst.INIT_SUCCEED);
            if (cb == 3){
                ar.setFailMsg("身份证号已存在");
            }
        }
        return  ar;
    }

    @RequestMapping(value = "updateSubcontractor", method = RequestMethod.POST)
    @ResponseBody
    @ApiOperation(value = "分包商人员库-修改")
    public AjaxRes updateSubcontractor(@RequestBody SubcontractorBean bean){
        AjaxRes ar = new AjaxRes();
        int cb = service.updateSubcontractor(bean);
        ar.setFailMsg(GlobalConst.DATA_FAIL);
        if(cb>0){
            ar.setFailMsg(GlobalConst.INIT_SUCCEED);
            if (cb == 3){
                ar.setFailMsg("身份证号已存在");
            }
        }
        return  ar;
    }

    @LogAnnotation
    @PostMapping("getWorkRecordLists")
    @ApiOperation(value = "分包商人员库-工作记录")
    public R getWorkRecordLists(SubsidiaryCompanyBean bean){
        List<SubsidiaryCompanyBean> list = service.getWorkRecordLists(bean);
        return list.size() > 0 ? R.okTable(list, list.size()):R.failTable("暂无数据");
    }

    /**
     * 文件上传请求
     */
    @LogAnnotation
    @PostMapping("/subcontractorImport")
    @ApiOperation(value = "分公司人员库-文件导入")
    public AjaxRes subcontractorImport(@RequestParam("file") MultipartFile file, @RequestParam("subId") String subId, HttpServletRequest request, HttpServletResponse response) throws IOException {
        AjaxRes ar = new AjaxRes();
        try {
            String code = service.subcontractorImport(request,file,subId);
            if ("导入成功".equals(code)){
                ar.setSucceedMsg("导入成功");
            }else{
                ar.setFailMsg(code);
            }
        } catch (Exception e) {
            e.printStackTrace();
        }
        return ar;
    }

    @RequestMapping(value = "/exportSubcontractorResult", method = RequestMethod.GET)
    @ApiOperation(value = "分公司人员库-导出")
    public void exportSubcontractorResult(HttpServletRequest request, HttpServletResponse response, PageTableRequest page) throws ParseException {
        SubcontractorBean b = new SubcontractorBean();
        b.setKeyWord(request.getParameter("keyword"));
        b.setDictKey(request.getParameter("dictKey"));
        b.setSubId(request.getParameter("subId"));
        List<SubcontractorBean> list = service.getSubcontractorList(b);
        List<Map<String, Object>> mapList = new ArrayList<Map<String, Object>>();
        String[] excelHeader = {"序号","分包单位名称","姓名","性别","民族","身份证"
                ,"是否有社保","是否有用工协议","工资发放证明","工序","班组","岗位"};
        int[] ds_format = {2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2,2};
        String fileName = "分包商骨干人员信息";
        if (list != null) {
            int i = 0;
            for (SubcontractorBean bean : list){
                i++;
                Map<String, Object> map = new HashMap<>(100);
                map.put("序号",i);
                map.put("分包单位名称",bean.getSubName());
                map.put("姓名",bean.getPersonName());
                map.put("性别",bean.getSex());
                map.put("民族",bean.getNation());
                map.put("身份证",bean.getId());
                map.put("是否有社保",bean.getIsSocialSecurity());
                map.put("是否有用工协议",bean.getIsEmploymentAgreement());
                map.put("工资发放证明",bean.getIsSalaryPaymentCertificate());
                map.put("工序",bean.getSubTeamType());
                map.put("班组",bean.getSubTeamName());
                map.put("岗位",bean.getManagePersonName());
                mapList.add(map);
            }
        }
        try {
            ExcelUtils.export(fileName, fileName, excelHeader, excelHeader, ds_format, null, mapList, request,
                    response);
        } catch (IOException e) {
            e.printStackTrace();
        }
    }

    /*******************************************************************自有人员库*********************************************************************************************/
    @LogAnnotation
    @PostMapping("getSelfOwnedList")
    @ApiOperation(value = "自有人员库-列表查询")
    public R getSelfOwnedList(SelfOwnedBean bean){
        List<SelfOwnedBean> list = service.getSelfOwnedList(bean);
        return list.size() > 0 ? R.okTable(list, list.size()):R.failTable("暂无数据");
    }

    @LogAnnotation
    @PostMapping("SelfOwnedSwitchListen")
    @ApiOperation(value = "自有人员库-权限锁定")
    public AjaxRes SelfOwnedSwitchListen(SelfOwnedBean bean) {
        AjaxRes ar = new AjaxRes();
        int result  = service.SelfOwnedSwitchListen(bean);
        if(result==1){
            ar.setSucceedMsg(GlobalConst.DATA_SUCCEED);
        }else {
            ar.setFailMsg(GlobalConst.DATA_FAIL);
        }
        return ar;
    }

    @RequestMapping(value = "delSelfOwnedById", method = RequestMethod.POST)
    @ResponseBody
    @ApiOperation(value = "自有人员库-删除")
    public AjaxRes delSelfOwnedById(String id) throws Exception {
        AjaxRes ar = new AjaxRes();
        int cb = service.delSelfOwnedById(id);
        ar.setFailMsg(GlobalConst.DATA_FAIL);
        if(cb>0){
            ar.setFailMsg(GlobalConst.INIT_SUCCEED);
        }
        return ar;
    }

    @RequestMapping(value = "addSelfOwned", method = RequestMethod.POST)
    @ResponseBody
    @ApiOperation(value = "自有人员库-新增")
    public AjaxRes addSelfOwned(@RequestBody SelfOwnedBean bean){
        AjaxRes ar = new AjaxRes();
        int cb = service.addSelfOwned(bean);
        ar.setFailMsg(GlobalConst.DATA_FAIL);
        if(cb>0){
            ar.setFailMsg(GlobalConst.INIT_SUCCEED);
            if (cb == 3){
                ar.setFailMsg("身份证号已存在");
            }
        }
        return  ar;
    }

    @RequestMapping(value = "updateSelfOwned", method = RequestMethod.POST)
    @ResponseBody
    @ApiOperation(value = "自有人员库-修改")
    public AjaxRes updateSelfOwned(@RequestBody SelfOwnedBean bean){
        AjaxRes ar = new AjaxRes();
        int cb = service.updateSelfOwned(bean);
        ar.setFailMsg(GlobalConst.DATA_FAIL);
        if(cb>0){
            ar.setFailMsg(GlobalConst.INIT_SUCCEED);
            if (cb == 3){
                ar.setFailMsg("身份证号已存在");
            }
        }
        return  ar;
    }
    @LogAnnotation
    @PostMapping("getSelfOwnedWorkRecordList")
    @ApiOperation(value = "自有人员库-工作记录")
    public R getSelfOwnedWorkRecordList(SelfOwnedBean bean){
        List<SelfOwnedBean> list = service.getSelfOwnedWorkRecordList(bean);
        return list.size() > 0 ? R.okTable(list, list.size()):R.failTable("暂无数据");
    }
    /**
     * 文件上传请求
     */
    @LogAnnotation
    @PostMapping("/selfOwnedImport")
    @ApiOperation(value = "自有人员库-文件导入")
    public AjaxRes selfOwnedImport(@RequestParam("file") MultipartFile file, HttpServletRequest request, HttpServletResponse response) throws IOException {
        AjaxRes ar = new AjaxRes();
        try {
            String code = service.selfOwnedImport(request,file);
            if ("导入成功".equals(code)){
                ar.setSucceedMsg("导入成功");
            }else{
                ar.setFailMsg(code);
            }
        } catch (Exception e) {
            e.printStackTrace();
        }
        return ar;
    }

    @RequestMapping(value = "/exportSelfOwnedResult", method = RequestMethod.GET)
    @ApiOperation(value = "自有人员库-导出")
    public void exportSelfOwnedResult(HttpServletRequest request, HttpServletResponse response, PageTableRequest page) throws ParseException {
        SelfOwnedBean b = new SelfOwnedBean();
        b.setKeyWord(request.getParameter("keyword"));
        b.setOrgId(request.getParameter("orgId"));
        List<SelfOwnedBean> list = service.getSelfOwnedList(b);
        List<Map<String, Object>> mapList = new ArrayList<Map<String, Object>>();
        String[] excelHeader = {"序号","姓名","性别","身份证","执证情况"
                ,"职称/技能等级","所在项目","工序","班组","岗位"};
        int[] ds_format = {2, 2, 2, 2, 2, 2, 2, 2, 2, 2};
        String fileName = "自由班组人员信息";
        if (list != null) {
            int i = 0;
            for (SelfOwnedBean bean : list){
                i++;
                Map<String, Object> map = new HashMap<>(100);
                map.put("序号",i);
                map.put("姓名",bean.getPersonName());
                map.put("性别",bean.getSex());
                map.put("身份证",bean.getId());
                map.put("执证情况",bean.getCertificate());
                map.put("职称/技能等级",bean.getLevel());
                map.put("所在项目",bean.getProName());
                map.put("工序",bean.getSubTeamType());
                map.put("班组",bean.getResourceName());
                map.put("岗位",bean.getManagePersonName());
                mapList.add(map);
            }
        }
        try {
            ExcelUtils.export(fileName, fileName, excelHeader, excelHeader, ds_format, null, mapList, request,
                    response);
        } catch (IOException e) {
            e.printStackTrace();
        }
    }

    @LogAnnotation
    @PostMapping("getDerivedRecordList")
    @ApiOperation(value = "人员库-导出记录")
    public R getDerivedRecordList(SelfOwnedBean bean){
        List<SelfOwnedBean> list = service.getDerivedRecordList(bean);
        return list.size() > 0 ? R.okTable(list, list.size()):R.failTable("暂无数据");
    }
}
