package com.unis.base.server.domain;

import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import com.unis.base.common.vo.*;
import com.unis.base.server.dao.PersonnelMxDao;
import com.unis.base.server.entity.PersonnelDO;
import com.unis.base.server.entity.QyiDo;
import com.unis.common.utils.LsBeanUtils;
import com.unis.common.utils.UUIDUtils;
import jdk.nashorn.internal.ir.IfNode;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.cloud.commons.util.IdUtils;
import org.springframework.stereotype.Component;
import org.springframework.util.StringUtils;

import java.math.BigDecimal;
import java.util.List;
import java.util.Objects;
import java.util.UUID;

@Component
public class PersonnelMxDomain {
    @Autowired
    private PersonnelMxDao personnelDao;

   /* public List<PersonnelMxVO> getPersonnelDetails() {
//        List<PersonnelMxVO> personnelMxVOS = personnelDao.selectDetails();
//        personnelDao.deleteAll();

        List<PersonnelMxVO> personnelMxVOList = personnelDao.selectBatch();
        return personnelMxVOList;
    }*/

    public PageInfo<PagePersonnelVO> pagePersonnel(PageSDetailsVO pageSDetailsVO) {
        String deptId = pageSDetailsVO.getDeptId();
        PageInfo<PagePersonnelVO> pageInfoVo = new PageInfo<>();
        PageHelper.startPage(pageSDetailsVO.getPageNum(), pageSDetailsVO.getPageSize());
        if (pageSDetailsVO.getDeptId().equals("DP001")) {
            List<QyiDo> pagePersonnelVOS = personnelDao.selectduty1(pageSDetailsVO);
//            for (QyiDo pagePersonnelVO : pagePersonnelVOS) {
//                if (pagePersonnelVO != null) {
//                    String qymc = pagePersonnelVO.getQymc();
//                    List<String> ty = personnelDao.selectTy(qymc);
//                    pagePersonnelVO.setTyshxydm(ty.get(0));
//                }
//
//            }
//            List<PageCountVO> pageCountVOS = personnelDao.selectPerson1(pageSDetailsVO);
//            for (QyiDo pagePersonnelVO : pagePersonnelVOS) {
//                for (PageCountVO pageCountVO : pageCountVOS) {
//                    if (pagePersonnelVO != null && Objects.equals(pagePersonnelVO.getQymc(), pageCountVO.getQymc())) {
//                        pagePersonnelVO.setPersonnumber(pageCountVO.getPersonnumber());
//                    }
//                }
//            }
            PageInfo<QyiDo> pageInfoDo = new PageInfo<>(pagePersonnelVOS);
            LsBeanUtils.copyAllProperties(pageInfoDo, pageInfoVo);

        } else {
            List<QyiDo> pagePersonnelVOS = personnelDao.selectduty(pageSDetailsVO);
            for (QyiDo pagePersonnelVO : pagePersonnelVOS) {
                if (pagePersonnelVO != null) {
                    String qymc = pagePersonnelVO.getQymc();
                    List<String> ty = personnelDao.selectTy(qymc);
                    pagePersonnelVO.setTyshxydm(ty.get(0));
                }

            }
            pageSDetailsVO.setDeptId(null);

            for (QyiDo pagePersonnelVO : pagePersonnelVOS) {
                pageSDetailsVO.setQymc(pagePersonnelVO.getQymc());
                List<PageCountVO> pageCountVOS = personnelDao.selectPerson(pageSDetailsVO);
                for (PageCountVO pageCountVO : pageCountVOS) {
                    if (pagePersonnelVO != null && Objects.equals(pagePersonnelVO.getQymc(), pageCountVO.getQymc())) {
                        pagePersonnelVO.setPersonnumber(pageCountVO.getPersonnumber());
                    }
                }
            }

            PageInfo<QyiDo> pageInfoDo = new PageInfo<>(pagePersonnelVOS);
            LsBeanUtils.copyAllProperties(pageInfoDo, pageInfoVo);
        }

//        personnelDao.deleteOld();
//        personnelDao.insertQiyi(pagePersonnelVOS);
        return pageInfoVo;
    }

    public List<PersonnelVO> pagePersonnelDetails(String ryxm) {
        List<PersonnelVO> personnelVOS = personnelDao.selectPersonDetails(ryxm);
        return personnelVOS;
    }

    public void insertPersonnelDetails(PersonnelMxVO personnelMxVO) {
        String rybh = UUID.randomUUID().toString().replaceAll("-", "");
        String lxdh = personnelMxVO.getLxdh();
        personnelMxVO.setRybh(rybh);
        personnelMxVO.setStatus(new BigDecimal(1));
        personnelMxVO.setPersonStatus(1);
        personnelDao.insertBatch(personnelMxVO);
        // String rybh = personnelDao.selectrybh(personnelMxVO.getRyxm());
        List<EducationVO> educationVOList = personnelMxVO.getEducationVOList();
        for (EducationVO educationVO : educationVOList) {
            educationVO.setRybh(rybh);
            educationVO.setTelphone(lxdh);
        }
        personnelDao.insertEducation(educationVOList);
        List<FeiEducationVO> feiEducationVOList = personnelMxVO.getFeiEducationVOList();
        for (FeiEducationVO feiEducationVO : feiEducationVOList) {

            feiEducationVO.setRybh(rybh);
        }
        personnelDao.insertFeieducation(feiEducationVOList);
        List<ReportVO> reportVOList = personnelMxVO.getReportVOList();
        for (ReportVO reportVO : reportVOList) {
            reportVO.setRybh(rybh);
            reportVO.setTelphone(lxdh);
        }
        personnelDao.insertReport(reportVOList);
        List<SkillVO> skillVOList = personnelMxVO.getSkillVOList();
        for (SkillVO skillVO : skillVOList) {
            skillVO.setRybh(rybh);
        }
        personnelDao.insertSkill(skillVOList);
        List<RotaterotateVO> rotaterotateVOList = personnelMxVO.getRotaterotateVOList();
        for (RotaterotateVO rotaterotateVO : rotaterotateVOList) {
            rotaterotateVO.setRybh(rybh);
        }
        personnelDao.insertRotater(rotaterotateVOList);
        List<OtherCertificatesVO> otherCertificatesVOList = personnelMxVO.getOtherCertificatesVOList();
        for (OtherCertificatesVO otherCertificatesVO : otherCertificatesVOList) {
            otherCertificatesVO.setRybh(rybh);
        }
        personnelDao.insertCertificate(otherCertificatesVOList);
    }

    public List<PagePersonnelVO> pageSDetails(PageSDetailsVO pageSDetailsVO) {
        List<PagePersonnelVO> pagePersonnelVOS = personnelDao.selectdutys(pageSDetailsVO);
        List<PageCountVO> pageCountVOS = personnelDao.selectPersons(pageSDetailsVO);
        for (PagePersonnelVO pagePersonnelVO : pagePersonnelVOS) {
            for (PageCountVO pageCountVO : pageCountVOS) {
                if (pagePersonnelVO.getTyshxydm().equals(pageCountVO.getTyshxydm())) {
                    pagePersonnelVO.setPersonnumber(pageCountVO.getPersonnumber());
                }
            }
        }
        return pagePersonnelVOS;
    }

    public PageInfo<PersonnelVO> pageDetails(Integer pageNum, Integer pageSize, String qymc, String ryxm, String personStatus) {
        PageInfo<PersonnelVO> pageInfoVo = new PageInfo<>();
        PageHelper.startPage(pageNum, pageSize);
        List<PersonnelDO> personnelVOS = personnelDao.selectDetails(qymc, ryxm, personStatus);
        PageInfo<PersonnelDO> pageInfoDo = new PageInfo<>(personnelVOS);
        LsBeanUtils.copyAllProperties(pageInfoDo, pageInfoVo);
        return pageInfoVo;
    }

    public PersonnelDO updatePersonnel(Integer personStatus, String rybh) {
        if (personStatus != null) {
            if (personStatus == 1) {
                personnelDao.updateN(rybh);
            } else if (personStatus == 2) {
                personnelDao.updateY(rybh);
            }
        }
        PersonnelDO personnelDO = personnelDao.selectDetailzs(rybh);
        return personnelDO;
    }


    public String determine(String ryxm) {
        String groupId = personnelDao.selectGroup(ryxm);
        if (StringUtils.isEmpty(groupId)) {
            return null;
        }
        String groupName = personnelDao.selectGroupName(groupId);
        return groupName;

    }

    public void upPersonnelDetails(PersonnelMxVO personnelMxVO) {
        String rybh = personnelMxVO.getRybh();
        personnelDao.ubdateRy(personnelMxVO);
        personnelDao.deleteS(rybh);
        for (SkillVO skillVO : personnelMxVO.getSkillVOList()) {
            skillVO.setRybh(rybh);
            personnelDao.updateS(skillVO);
        }

        personnelDao.deleteC(rybh);
        for (OtherCertificatesVO otherCertificatesVO : personnelMxVO.getOtherCertificatesVOList()) {
            otherCertificatesVO.setRybh(rybh);
            personnelDao.updateC(otherCertificatesVO);
        }
        personnelDao.deleteR(rybh);
        for (RotaterotateVO rotaterotateVO : personnelMxVO.getRotaterotateVOList()) {
            rotaterotateVO.setRybh(rybh);
            personnelDao.updateR(rotaterotateVO);
        }
        personnelDao.deleteRe(rybh);
        for (ReportVO reportVO : personnelMxVO.getReportVOList()) {
            reportVO.setRybh(rybh);
            personnelDao.updateRe(reportVO);
        }
        personnelDao.deleteFe(rybh);
        for (FeiEducationVO feiEducationVO : personnelMxVO.getFeiEducationVOList()) {
            feiEducationVO.setRybh(rybh);
            personnelDao.updateFe(feiEducationVO);
        }
        personnelDao.deleteEd(rybh);
        for (EducationVO educationVO : personnelMxVO.getEducationVOList()) {
            educationVO.setRybh(rybh);
            personnelDao.updateEd(educationVO);
        }


    }

    public PersonnelMxVO catchPersonnelDetails(String rybh) {
        PersonnelMxVO personnelMxVO = personnelDao.selectPer(rybh);
//        String rybh = personnelMxVO.getRybh();
        List<EducationVO> educationVO = personnelDao.selectEducation(rybh);
        List<ReportVO> reportVO = personnelDao.selectReport(rybh);
        List<SkillVO> skillVO = personnelDao.selectSkill(rybh);
        List<RotaterotateVO> rotaterotateVO = personnelDao.selectRotaterotateVO(rybh);
        List<FeiEducationVO> feiEducationVO = personnelDao.selectFeiEducationVO(rybh);
        OtherCertificatesVO otherCertificatesVO = personnelDao.selectOtherCertificatesVO(rybh);

        if (educationVO == null) {
            EducationVO educationVO1 = new EducationVO();
            educationVO1.setQuanEducationalForm(null);
            educationVO1.setQuanAcademic(null);
            educationVO1.setQuanHighestEducation(null);
            educationVO1.setQuanFoodstuff(null);
            educationVO1.setQuanInstitutions(null);
            educationVO1.setQuanSpeciality(null);
            personnelMxVO.getEducationVOList().add(educationVO1);
        } else {
            personnelMxVO.getEducationVOList().addAll(educationVO);
        }

        if (feiEducationVO == null) {
            FeiEducationVO feiEducationVO1 = new FeiEducationVO();
            feiEducationVO1.setFeiHighestEducation(null);
            feiEducationVO1.setFeiEducationalForm(null);
            feiEducationVO1.setFeiAcademic(null);
            feiEducationVO1.setFeiFoodstuff(null);
            feiEducationVO1.setFeiInstitutions(null);
            feiEducationVO1.setFeiSpeciality(null);
            personnelMxVO.getFeiEducationVOList().add(feiEducationVO1);
        } else {
            personnelMxVO.getFeiEducationVOList().addAll(feiEducationVO);
        }

        if (reportVO == null) {
            ReportVO reportVO1 = new ReportVO();
            reportVO1.setReportName(null);
            reportVO1.setAuthoritys(null);
            reportVO1.setReportNumber(null);
            reportVO1.setHonoraryName(null);
            reportVO1.setHonoraryTitle(null);
            reportVO1.setReleaseTime(null);
            reportVO1.setTitle(null);
            personnelMxVO.getReportVOList().add(reportVO1);

        } else {
            personnelMxVO.getReportVOList().addAll(reportVO);
        }

        if (skillVO == null) {
            SkillVO skillVO1 = new SkillVO();
            skillVO1.setSkillDate(null);
            skillVO1.setSkillLevel(null);
            skillVO1.setSkillName(null);
            personnelMxVO.getSkillVOList().add(skillVO1);

        } else {
            personnelMxVO.getSkillVOList().addAll(skillVO);
        }

        if (rotaterotateVO == null) {
            RotaterotateVO rotaterotateVO1 = new RotaterotateVO();
            rotaterotateVO1.setSerialNumber(null);
            rotaterotateVO1.setBusinessTime(null);
            rotaterotateVO1.setBusinessType(null);
            personnelMxVO.getRotaterotateVOList().add(rotaterotateVO1);

        } else {
            personnelMxVO.getRotaterotateVOList().addAll(rotaterotateVO);
        }

        if (otherCertificatesVO == null) {
            OtherCertificatesVO otherCertificatesVO1 = new OtherCertificatesVO();
            otherCertificatesVO1.setSkillLevel(null);
            otherCertificatesVO1.setAcquisitionTime(null);
            otherCertificatesVO1.setSpeciality(null);
            personnelMxVO.getOtherCertificatesVOList().add(otherCertificatesVO1);

        } else {
            personnelMxVO.getOtherCertificatesVOList().add(otherCertificatesVO);
        }
        return personnelMxVO;
    }

    public PersonnelMxVO catchPersonnelHDetails(String rybh) {
        PersonnelMxVO personnelMxVO = new PersonnelMxVO();
        List<EducationVO> educationVO = personnelDao.selectEducation(rybh);
        List<ReportVO> reportVO = personnelDao.selectReport(rybh);
        List<SkillVO> skillVO = personnelDao.selectSkill(rybh);
        List<RotaterotateVO> rotaterotateVO = personnelDao.selectRotaterotateVO(rybh);
        List<FeiEducationVO> feiEducationVO = personnelDao.selectFeiEducationVO(rybh);
        OtherCertificatesVO otherCertificatesVO = personnelDao.selectOtherCertificatesVO(rybh);

        if (educationVO == null) {
            EducationVO educationVO1 = new EducationVO();
            educationVO1.setQuanEducationalForm(null);
            educationVO1.setQuanAcademic(null);
            educationVO1.setQuanHighestEducation(null);
            educationVO1.setQuanFoodstuff(null);
            educationVO1.setQuanInstitutions(null);
            educationVO1.setQuanSpeciality(null);
            personnelMxVO.getEducationVOList().add(educationVO1);
        } else {
            personnelMxVO.getEducationVOList().addAll(educationVO);
        }

        if (feiEducationVO == null) {
            FeiEducationVO feiEducationVO1 = new FeiEducationVO();
            feiEducationVO1.setFeiHighestEducation(null);
            feiEducationVO1.setFeiEducationalForm(null);
            feiEducationVO1.setFeiAcademic(null);
            feiEducationVO1.setFeiFoodstuff(null);
            feiEducationVO1.setFeiInstitutions(null);
            feiEducationVO1.setFeiSpeciality(null);
            personnelMxVO.getFeiEducationVOList().add(feiEducationVO1);
        } else {
            personnelMxVO.getFeiEducationVOList().addAll(feiEducationVO);
        }

        if (reportVO == null) {
            ReportVO reportVO1 = new ReportVO();
            reportVO1.setReportName(null);
            reportVO1.setAuthoritys(null);
            reportVO1.setReportNumber(null);
            reportVO1.setHonoraryName(null);
            reportVO1.setHonoraryTitle(null);
            reportVO1.setReleaseTime(null);
            reportVO1.setTitle(null);
            personnelMxVO.getReportVOList().add(reportVO1);

        } else {
            personnelMxVO.getReportVOList().addAll(reportVO);
        }

        if (skillVO == null) {
            SkillVO skillVO1 = new SkillVO();
            skillVO1.setSkillDate(null);
            skillVO1.setSkillLevel(null);
            skillVO1.setSkillName(null);
            personnelMxVO.getSkillVOList().add(skillVO1);

        } else {
            personnelMxVO.getSkillVOList().addAll(skillVO);
        }

        if (rotaterotateVO == null) {
            RotaterotateVO rotaterotateVO1 = new RotaterotateVO();
            rotaterotateVO1.setSerialNumber(null);
            rotaterotateVO1.setBusinessTime(null);
            rotaterotateVO1.setBusinessType(null);
            personnelMxVO.getRotaterotateVOList().add(rotaterotateVO1);

        } else {
            personnelMxVO.getRotaterotateVOList().addAll(rotaterotateVO);
        }

        if (otherCertificatesVO == null) {
            OtherCertificatesVO otherCertificatesVO1 = new OtherCertificatesVO();
            otherCertificatesVO1.setSkillLevel(null);
            otherCertificatesVO1.setAcquisitionTime(null);
            otherCertificatesVO1.setSpeciality(null);
            personnelMxVO.getOtherCertificatesVOList().add(otherCertificatesVO1);

        } else {
            personnelMxVO.getOtherCertificatesVOList().add(otherCertificatesVO);
        }
        return personnelMxVO;
    }
}
