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

import com.alibaba.fastjson.JSONObject;
import com.bonus.hn.bearing.capacity.backstage.manager.utils.ImportNoPhotoExcelHelper;
import com.bonus.hn.bearing.capacity.backstage.manager.utils.ListHelper;
import com.bonus.hn.bearing.capacity.backstage.manager.utils.StringHelper;
import com.bonus.hn.bearing.capacity.backstage.manager.utils.UserUtil;
import com.bonus.hn.bearing.capacity.backstage.personnelPool.dao.PersonnelPoolDao;
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.real.entity.SmzPersonnelRosterBean;
import com.bonus.hn.bearing.capacity.backstage.utils.Aes;
import org.apache.commons.lang3.StringUtils;
import org.springframework.stereotype.Service;
import org.springframework.web.multipart.MultipartFile;

import javax.annotation.Resource;
import javax.servlet.http.HttpServletRequest;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

/**
 * 包名称：com.bonus.hn.bearing.capacity.backstage.companypro.service
 * 类名称：ProjectManagerServiceImpl
 * 类描述：
 * 创建人：@author xxxxxx
 * 创建时间：2023年11月06日 16:27
 */
@Service("PersonnelPoolService")
public class PersonnelPoolServiceImpl implements PersonnelPoolService {

    @Resource
    private PersonnelPoolDao dao;

    @Override
    public List<SubsidiaryCompanyBean> getSubsidiaryCompanyList(SubsidiaryCompanyBean bean) {
        return dao.getSubsidiaryCompanyList(bean);
    }

    @Override
    public int updateSwitchListen(SubsidiaryCompanyBean bean) {
        return dao.updateSwitchListen(bean);
    }

    @Override
    public int delById(String id) {
        return dao.delById(id);
    }

    @Override
    public int addSubsidiaryCompany(SubsidiaryCompanyBean bean) {
        int num = dao.getIdCard(bean);
        if (num > 0){
            num = 3;
        }else {
            if (bean.getSex().equals("0")){
                bean.setSex("男");
            }else {
                bean.setSex("女");
            }
            num = dao.addSubsidiaryCompany(bean);
        }
        return num;
    }

    @Override
    public int updateSubsidiaryCompany(SubsidiaryCompanyBean bean) {
            if (bean.getSex().equals("0")){
                bean.setSex("男");
            }else {
                bean.setSex("女");
            }
        return dao.updateSubsidiaryCompany(bean);
    }

    @Override
    public List<SubsidiaryCompanyBean> getWorkRecordList(SubsidiaryCompanyBean bean) {
        return dao.getWorkRecordList(bean);
    }

    @Override
    public String importExcel(HttpServletRequest request, MultipartFile file, String orgId) {
        int successCount = 0;
        int errorCount = 0;
        String fileName = file.getOriginalFilename();
        String res = "";
        try {
            SubsidiaryCompanyBean entity = new SubsidiaryCompanyBean();
            entity.setNation(request.getParameter("nation"));
            entity.setPersonName(request.getParameter("personName"));
            entity.setSex(request.getParameter("sex"));
            entity.setId(request.getParameter("id"));
            entity.setCertificate(request.getParameter("certificate"));
            entity.setLevel(request.getParameter("level"));

            List<JSONObject> lstObj = (List<JSONObject>) ImportNoPhotoExcelHelper.readExcel(file, SubsidiaryCompanyBean.class);
            List<SubsidiaryCompanyBean> list = new ArrayList<>();
            int i = 0;
            boolean active = false;
            String nation = "";
            String personName = "";
            String sex = "";
            String id = "";
            String certificate = "";
            String level = "";
            if (lstObj != null && lstObj.size() > 0) {
                for (JSONObject obj : lstObj) {
                    i++;
                    SubsidiaryCompanyBean bean = new SubsidiaryCompanyBean();
//                    orgName = String.valueOf(obj.getString("orgName"));
//                    String orgId = dao.OrgName(orgName.trim());
//                    bean.setOrgName(orgId);
//                    if (StringHelper.isEmpty(orgName) ||  "0.0".equals(orgName.trim())) {
//                        res += "第" + i + "行分公司为空;";
//                        active = true;
//                    }
//                    if (StringHelper.isEmpty(orgId) ||  "0.0".equals(orgId.trim())) {
//                        res += "第" + i + "行分公司不存在;";
//                        active = true;
//                    }
                    personName = String.valueOf(obj.getString("personName"));
                    bean.setPersonName(personName.trim());
                    if (StringHelper.isEmpty(personName) ||  "0.0".equals(personName.trim())) {
                        res += "第" + i + "行姓名为空;";
                        active = true;
                    }
                    sex = String.valueOf(obj.getString("sex"));
                    bean.setSex(sex.trim());
                    if (StringHelper.isEmpty(sex) ||  "0.0".equals(sex.trim())) {
                        res += "第" + i + "行姓别为空;";
                        active = true;
                    }
                    nation = String.valueOf(obj.getString("nation"));
                    bean.setNation(nation.trim());
                    if (StringHelper.isEmpty(nation) ||  "0.0".equals(nation.trim())) {
                        res += "第" + i + "行民族为空;";
                        active = true;
                    }
                    id = String.valueOf(obj.getString("id"));
                    bean.setId(id.trim());
                    if (StringHelper.isEmpty(id) ||  "0.0".equals(id.trim())) {
                        res += "第" + i + "行身份证为空;";
                        active = true;
                    } else if (id.length() != 18) {
                        res += "第" + i + "行身份证格式错误;";
                        active = true;
                    }
                    certificate = String.valueOf(obj.getString("certificate"));
                    bean.setCertificate(certificate.trim());
                    if (StringHelper.isEmpty(certificate) ||  "0.0".equals(certificate.trim())) {
                        res += "第" + i + "行执照情况为空;";
                        active = true;
                    }
                    level = String.valueOf(obj.getString("level"));
                    bean.setLevel(level.trim());
                    if (StringHelper.isEmpty(level) ||  "0.0".equals(level.trim())) {
                        res += "第" + i + "行职称/技能等级为空;";
                        active = true;
                    }
                    list.add(bean);
                }
            }else {
                return "数据为空";
            }
            if (active) {
                String[] arr = res.split(";");
                String code = "<pre>" +
                        "导入数据错误提示：<br/>";
                for (int j = 0; j < arr.length; j++) {
                    if (j % 2 == 0){
                        if (arr[j].indexOf("检查所见异常结果") != -1){
                            code += arr[j] + ";\t";
                        }else if(arr[j].indexOf("分公司") != -1 || arr[j].indexOf("姓名") != -1 || arr[j].indexOf("姓别") != -1 || arr[j].indexOf("身份证") != -1
                                || arr[j].indexOf("执照情况") != -1 || arr[j].indexOf("职称/技能等级") != -1){
                            code += arr[j] + ";\t\t\t";
                        }else {
                            code += arr[j] + ";\t\t";
                        }

                    }else {
                        code += arr[j] + "<br/>";
                    }
                }
                return code+ "<pre>";
            }
            if (ListHelper.isNotEmpty(list)) {
                for (SubsidiaryCompanyBean vo : list) {
                    System.out.println("vo="+vo.toString());
                    vo.setOrgId(orgId);
                    int num = dao.getRepeat(vo);
                    if (num > 0){
                        dao.updataImportExcel(vo);
                    }else {
                        dao.importExcel(vo);
                    }
                    successCount++;
                }
            }
            res = "导入成功";
        } catch (Exception e) {
            if (e.getMessage().indexOf("模板错误，请重新选择模板！") != -1){
                res = "模板错误，请重新选择模板！";
            }else {
                res = "导入失败";
            }
            e.printStackTrace();
        }
        SubsidiaryCompanyBean bean = new SubsidiaryCompanyBean();
        bean.setFileName(fileName);
        bean.setDataVolume(String.valueOf(successCount));
        bean.setIdName(UserUtil.getLoginUser().getId().toString());
        dao.addImportRecord(bean);
        Map<String, Integer> obj = new HashMap<>(100);
        obj.put("成功导入数量", successCount);
        obj.put("导入异常记录数量", errorCount);
        return res;
    }




    /*******************************************************************分包商人员库*********************************************************************************************/
    @Override
    public List<SubcontractorBean> getSubcontractorList(SubcontractorBean data) {
        List<SubcontractorBean> list= dao.getSubcontractorList(data);
        for (SubcontractorBean bean : list) {
            if(StringUtils.isNotBlank(bean.getId())){
                bean.setIdCard(Aes.aesEncrypt(bean.getId()));
            }
        }
        return list;
    }

    @Override
    public int updateSubcontractorSwitchListen(SubcontractorBean bean) {
        return dao.updateSubcontractorSwitchListen(bean);
    }

    @Override
    public int delSubcontractorById(String id) {
        return dao.delSubcontractorById(id);
    }

    @Override
    public int addSubcontractor(SubcontractorBean bean) {
        int num = dao.getIdCards(bean);
        if (num > 0){
            num = 3;
        }else {
            if (bean.getSex().equals("0")){
                bean.setSex("男");
            }else {
                bean.setSex("女");
            }
            bean.setIsEmploymentAgreement(bean.getIsEmploymentAgreement().equals("0") ? "是" : "否");
            bean.setIsSocialSecurity(bean.getIsSocialSecurity().equals("0") ? "是" : "否");
            bean.setIsSalaryPaymentCertificate(bean.getIsSalaryPaymentCertificate().equals("0") ? "是" : "否");
            num = dao.addSubcontractor(bean);
        }
        return num;
    }

    @Override
    public int updateSubcontractor(SubcontractorBean bean) {
        if (bean.getSex().equals("0")){
            bean.setSex("男");
        }else {
            bean.setSex("女");
        }
        bean.setIsEmploymentAgreement(bean.getIsEmploymentAgreement().equals("0") ? "是" : "否");
        bean.setIsSocialSecurity(bean.getIsSocialSecurity().equals("0") ? "是" : "否");
        bean.setIsSalaryPaymentCertificate(bean.getIsSalaryPaymentCertificate().equals("0") ? "是" : "否");
        return dao.updateSubcontractor(bean);
    }

    @Override
    public List<SubsidiaryCompanyBean> getWorkRecordLists(SubsidiaryCompanyBean bean) {
        return dao.getWorkRecordLists(bean);
    }

    @Override
    public List<SelfOwnedBean> getSelfOwnedWorkRecordList(SelfOwnedBean bean) {
        return dao.getSelfOwnedWorkRecordList(bean);
    }
    @Override
    public String subcontractorImport(HttpServletRequest request, MultipartFile file, String subId) {
        int successCount = 0;
        int errorCount = 0;
        String fileName = file.getOriginalFilename();
        String res = "";
        try {
            SubcontractorBean entity = new SubcontractorBean();
            entity.setPersonName(request.getParameter("personName"));
            entity.setSex(request.getParameter("sex"));
            entity.setNation(request.getParameter("nation"));
            entity.setId(request.getParameter("id"));
            entity.setCertificate(request.getParameter("certificate"));
            entity.setLevel(request.getParameter("level"));
//            entity.setSubId(request.getParameter("subId"));
            List<JSONObject> lstObj = (List<JSONObject>) ImportNoPhotoExcelHelper.readExcel(file, SubcontractorBean.class);
            List<SubcontractorBean> list = new ArrayList<>();
            int i = 0;
            boolean active = false;
            String personName = "";
            String sex = "";
            String nation = "";
            String id = "";
            String certificate = "";
            String level = "";
            if (lstObj != null && lstObj.size() > 0) {
                for (JSONObject obj : lstObj) {
                    i++;
                    SubcontractorBean bean = new SubcontractorBean();
                    personName = String.valueOf(obj.getString("personName"));
                    bean.setPersonName(personName.trim());
                    if (StringHelper.isEmpty(personName) ||  "0.0".equals(personName.trim())) {
                        res += "第" + i + "行姓名为空;";
                        active = true;
                    }
                    sex = String.valueOf(obj.getString("sex"));
                    bean.setSex(sex.trim());
                    if (StringHelper.isEmpty(sex) ||  "0.0".equals(sex.trim())) {
                        res += "第" + i + "行姓别为空;";
                        active = true;
                    }
                    nation = String.valueOf(obj.getString("nation"));
                    bean.setNation(nation.trim());
                    if (StringHelper.isEmpty(nation) ||  "0.0".equals(nation.trim())) {
                        res += "第" + i + "行民族为空;";
                        active = true;
                    }
                    id = String.valueOf(obj.getString("id"));
                    bean.setId(id.trim());
                    if (StringHelper.isEmpty(id) ||  "0.0".equals(id.trim())) {
                        res += "第" + i + "行身份证为空;";
                        active = true;
                    } else if (id.length() != 18) {
                        res += "第" + i + "行身份证格式错误;";
                        active = true;
                    }
                    certificate = String.valueOf(obj.getString("certificate"));
                    bean.setCertificate(certificate.trim());
                    if (StringHelper.isEmpty(certificate) ||  "0.0".equals(certificate.trim())) {
                        res += "第" + i + "行执照情况为空;";
                        active = true;
                    }
                    level = String.valueOf(obj.getString("level"));
                    bean.setLevel(level.trim());
                    if (StringHelper.isEmpty(level) ||  "0.0".equals(level.trim())) {
                        res += "第" + i + "行职称/技能等级为空;";
                        active = true;
                    }
                    list.add(bean);
                }
            }else {
                return "数据为空";
            }
            if (active) {
                String[] arr = res.split(";");
                String code = "<pre>" +
                        "导入数据错误提示：<br/>";
                for (int j = 0; j < arr.length; j++) {
                    if (j % 2 == 0){
                        if (arr[j].indexOf("检查所见异常结果") != -1){
                            code += arr[j] + ";\t";
                        }else if(arr[j].indexOf("分公司") != -1 || arr[j].indexOf("姓名") != -1 || arr[j].indexOf("姓别") != -1 || arr[j].indexOf("身份证") != -1
                                || arr[j].indexOf("执照情况") != -1 || arr[j].indexOf("职称/技能等级") != -1){
                            code += arr[j] + ";\t\t\t";
                        }else {
                            code += arr[j] + ";\t\t";
                        }

                    }else {
                        code += arr[j] + "<br/>";
                    }
                }
                return code+ "<pre>";
            }
            if (ListHelper.isNotEmpty(list)) {
                for (SubcontractorBean vo : list) {
                    System.out.println("vo="+vo.toString());
                    vo.setSubId(subId);
                    int num = dao.getSubcontractIdCard(vo);
                    if (num > 0){
                        dao.updataSubcontractImportExcel(vo);
                    }else {
                        dao.addSubcontractimportExcel(vo);
                    }
                    successCount++;
                }
            }
            res = "导入成功";
        } catch (Exception e) {
            if (e.getMessage().indexOf("模板错误，请重新选择模板！") != -1){
                res = "模板错误，请重新选择模板！";
            }else {
                res = "导入失败";
            }
            e.printStackTrace();
        }
        SubsidiaryCompanyBean bean = new SubsidiaryCompanyBean();
        bean.setFileName(fileName);
        bean.setDataVolume(String.valueOf(successCount));
        bean.setIdName(UserUtil.getLoginUser().getId().toString());
        dao.addImportRecord(bean);
        Map<String, Integer> obj = new HashMap<>(100);
        obj.put("成功导入数量", successCount);
        obj.put("导入异常记录数量", errorCount);
        return res;
    }

    @Override
    public String selfOwnedImport(HttpServletRequest request, MultipartFile file) {
        int successCount = 0;
        int errorCount = 0;
        String fileName = file.getOriginalFilename();
        String res = "";
        try {
            SelfOwnedBean entity = new SelfOwnedBean();
            entity.setPersonName(request.getParameter("personName"));
            entity.setSex(request.getParameter("sex"));
            entity.setNation(request.getParameter("nation"));
            entity.setId(request.getParameter("id"));
            entity.setCertificate(request.getParameter("certificate"));
            entity.setLevel(request.getParameter("level"));

            List<JSONObject> lstObj = (List<JSONObject>) ImportNoPhotoExcelHelper.readExcel(file, SelfOwnedBean.class);
            List<SelfOwnedBean> list = new ArrayList<>();
            int i = 0;
            boolean active = false;
            String personName = "";
            String sex = "";
            String id = "";
            String nation = "";
            String certificate = "";
            String level = "";
            if (lstObj != null && lstObj.size() > 0) {
                for (JSONObject obj : lstObj) {
                    i++;
                    SelfOwnedBean bean = new SelfOwnedBean();
                    personName = String.valueOf(obj.getString("personName"));
                    bean.setPersonName(personName.trim());
                    if (StringHelper.isEmpty(personName) ||  "0.0".equals(personName.trim())) {
                        res += "第" + i + "行姓名为空;";
                        active = true;
                    }
                    sex = String.valueOf(obj.getString("sex"));
                    bean.setSex(sex.trim());
                    if (StringHelper.isEmpty(sex) ||  "0.0".equals(sex.trim())) {
                        res += "第" + i + "行姓别为空;";
                        active = true;
                    }
                    nation = String.valueOf(obj.getString("nation"));
                    bean.setNation(nation.trim());
                    if (StringHelper.isEmpty(nation) ||  "0.0".equals(nation.trim())) {
                        res += "第" + i + "行民族为空;";
                        active = true;
                    }
                    id = String.valueOf(obj.getString("id"));
                    bean.setId(id.trim());
                    if (StringHelper.isEmpty(id) ||  "0.0".equals(id.trim())) {
                        res += "第" + i + "行身份证为空;";
                        active = true;
                    } else if (id.length() != 18) {
                        res += "第" + i + "行身份证格式错误;";
                        active = true;
                    }
                    certificate = String.valueOf(obj.getString("certificate"));
                    bean.setCertificate(certificate.trim());
                    if (StringHelper.isEmpty(certificate) ||  "0.0".equals(certificate.trim())) {
                        res += "第" + i + "行执照情况为空;";
                        active = true;
                    }
                    level = String.valueOf(obj.getString("level"));
                    bean.setLevel(level.trim());
                    if (StringHelper.isEmpty(level) ||  "0.0".equals(level.trim())) {
                        res += "第" + i + "行职称/技能等级为空;";
                        active = true;
                    }
                    list.add(bean);
                }
            }else {
                return "数据为空";
            }
            if (active) {
                String[] arr = res.split(";");
                String code = "<pre>" +
                        "导入数据错误提示：<br/>";
                for (int j = 0; j < arr.length; j++) {
                    if (j % 2 == 0){
                        if (arr[j].indexOf("检查所见异常结果") != -1){
                            code += arr[j] + ";\t";
                        }else if(arr[j].indexOf("分公司") != -1 || arr[j].indexOf("姓名") != -1 || arr[j].indexOf("姓别") != -1 || arr[j].indexOf("身份证") != -1
                                || arr[j].indexOf("执照情况") != -1 || arr[j].indexOf("职称/技能等级") != -1){
                            code += arr[j] + ";\t\t\t";
                        }else {
                            code += arr[j] + ";\t\t";
                        }

                    }else {
                        code += arr[j] + "<br/>";
                    }
                }
                return code+ "<pre>";
            }
            if (ListHelper.isNotEmpty(list)) {
                for (SelfOwnedBean vo : list) {
                    System.out.println("vo="+vo.toString());
                    int num = dao.getSelfOwnedRepeat(vo);
                    if (num > 0){
                        dao.updataSelfOwnedImportExcel(vo);
                    }else {
                        dao.addSelfOwnedimportExcel(vo);
                    }
                    successCount++;
                }
            }
            res = "导入成功";
        } catch (Exception e) {
            if (e.getMessage().indexOf("模板错误，请重新选择模板！") != -1){
                res = "模板错误，请重新选择模板！";
            }else {
                res = "导入失败";
            }
            e.printStackTrace();
        }
        SubsidiaryCompanyBean bean = new SubsidiaryCompanyBean();
        bean.setFileName(fileName);
        bean.setDataVolume(String.valueOf(successCount));
        bean.setIdName(UserUtil.getLoginUser().getId().toString());
        dao.addImportRecord(bean);
        Map<String, Integer> obj = new HashMap<>(100);
        obj.put("成功导入数量", successCount);
        obj.put("导入异常记录数量", errorCount);
        return res;
    }

    @Override
    public List<SelfOwnedBean> getDerivedRecordList(SelfOwnedBean bean) {
        bean.setId(UserUtil.getLoginUser().getId().toString());
        return dao.getDerivedRecordList(bean);
    }


    /*******************************************************************自有人员库*********************************************************************************************/
    @Override
    public List<SelfOwnedBean> getSelfOwnedList(SelfOwnedBean bean) {
        return dao.getSelfOwnedList(bean);
    }

    @Override
    public int SelfOwnedSwitchListen(SelfOwnedBean bean) {
        return dao.SelfOwnedSwitchListen(bean);
    }

    @Override
    public int delSelfOwnedById(String id) {
        return dao.delSelfOwnedById(id);
    }

    @Override
    public int addSelfOwned(SelfOwnedBean bean) {
        int num = dao.getSelfOwnedIdCard(bean);
        if (num > 0){
            num = 3;
        }else {
            if (bean.getSex().equals("0")){
                bean.setSex("男");
            }else {
                bean.setSex("女");
            }
            num = dao.addSelfOwned(bean);
        }
        return num;
    }

    @Override
    public int updateSelfOwned(SelfOwnedBean bean) {
        if (bean.getSex().equals("0")){
            bean.setSex("男");
        }else {
            bean.setSex("女");
        }
        return dao.updateSelfOwned(bean);
    }


}
