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.dao.TalentGroupDao;
import com.unis.base.server.entity.PersonnelDO;
import com.unis.base.server.entity.TalentGroupDO;
import com.unis.common.utils.CurrentUtil;
import com.unis.common.utils.LsBeanUtils;
import com.unis.common.utils.UUIDUtils;
import org.apache.commons.lang3.StringUtils;
import org.apache.ibatis.annotations.Mapper;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Component;

import java.util.ArrayList;
import java.util.List;

@Component
public class TalentGroupDomain {
    @Autowired
    private TalentGroupDao talentGroupDao;
    @Autowired
    private PersonnelMxDao personnelDao;

    public void addOrganization(GroupVO groupVO) {
        TalentGroupVO talentGroupVO = new TalentGroupVO();
        String currentUser = CurrentUtil.getUserName();
        talentGroupVO.setCreator(currentUser);
        talentGroupVO.setGroupId(UUIDUtils.uuid());
        talentGroupVO.setGroupName(groupVO.getGroupName());
        talentGroupVO.setCreationTime(groupVO.getCreationTime());
        String[] names = talentGroupDao.selectname(groupVO.getGroupName());
        if (names.length > 0) {
            throw new RuntimeException("人才组名称重复!");
        }
        talentGroupDao.insertGroup(talentGroupVO);

    }

    public PageInfo<GroupPersonSVO> seeOrganization(String groupId, Integer pageNum, Integer pageSize) {
        PageHelper.startPage(pageNum, pageSize);
        List<GroupPersonSVO> groupPersonVOList = talentGroupDao.selectGroups(groupId);
        PageInfo<GroupPersonSVO> pageInfoVo = new PageInfo<>(groupPersonVOList);
        return pageInfoVo;
    }

    public PersonnelMxVO seePersonOrganization(String rybh) {
        PersonnelMxVO personnelMxVO = talentGroupDao.selectPerson(rybh);
        return personnelMxVO;
    }

    public void insertInfo(GroupMainVo viewObj) {
        String groupId = viewObj.getGroupId();
        List<SelectPersonVo> personVoList = viewObj.getPersonVoList();
        List<QiyeMcVO> qiyeMcVOList = viewObj.getQiyeMcVOList();
        TalentGroupDO talentGroupDO = talentGroupDao.selectGroupInfo(groupId);
        for (SelectPersonVo selectPersonVo : personVoList) {
            String userId = selectPersonVo.getUserId();
            PersonnelMxVO personnelMxVO = personnelDao.selectPer(userId);
            String groupId1 = personnelMxVO.getGroupId();
            // 判断groupId1是否为空或者等于0，是的话直接groupId直接为groupId；
            if (StringUtils.isBlank(groupId1) || "0".equals(groupId1)) {
                // 如果association是无或者空的话，association传talentGroupDO的groupName
                if (StringUtils.isBlank(personnelMxVO.getAssociation()) || "无".equals(personnelMxVO.getAssociation())) {
                    talentGroupDao.updatePerson(userId, groupId, talentGroupDO.getGroupName());
                } else {
                    // 如果association不是无或者空的话，association拼接talentGroupDO的groupName
                    String association = personnelMxVO.getAssociation() + "," + talentGroupDO.getGroupName();
                    talentGroupDao.updatePerson(userId, groupId, association);
                }
            } else {
                // 不是的话拼接groupId1和groupId
                groupId1 = groupId1 + "," + groupId;
                // 如果association是无或者空的话，association传talentGroupDO的groupName
                if (StringUtils.isBlank(personnelMxVO.getAssociation()) || "无".equals(personnelMxVO.getAssociation())) {
                    talentGroupDao.updatePerson(userId, groupId1, talentGroupDO.getGroupName());
                } else {
                    // 如果association不是无或者空的话，association拼接talentGroupDO的groupName
                    String association = personnelMxVO.getAssociation() + "," + talentGroupDO.getGroupName();
                    talentGroupDao.updatePerson(userId, groupId1, association);
                }
            }
        }
        for (QiyeMcVO qiyeMcVO : qiyeMcVOList) {
            String deptId = qiyeMcVO.getDeptId();
            List<PersonnelMxVO> personnelMxVOList = personnelDao.selectByDeptId(deptId);
            if (personnelMxVOList.size() > 0) {
                for (PersonnelMxVO personnelMxVO : personnelMxVOList) {
                    String userId = personnelMxVO.getRybh();
                    String groupId1 = personnelMxVO.getGroupId();
                    // 判断groupId1是否为空或者等于0，是的话直接groupId直接为groupId；
                    if (StringUtils.isBlank(groupId1) || "0".equals(groupId1)) {
                        // 如果association是无或者空的话，association传talentGroupDO的groupName
                        if (StringUtils.isBlank(personnelMxVO.getAssociation()) || "无".equals(personnelMxVO.getAssociation())) {
                            talentGroupDao.updatePerson(userId, groupId, talentGroupDO.getGroupName());
                        } else {
                            // 如果association不是无或者空的话，association拼接talentGroupDO的groupName
                            String association = personnelMxVO.getAssociation() + "," + talentGroupDO.getGroupName();
                            talentGroupDao.updatePerson(userId, groupId, association);
                        }
                    } else {
                        // 不是的话拼接groupId1和groupId
                        groupId1 = groupId1 + "," + groupId;
                        // 如果association是无或者空的话，association传talentGroupDO的groupName
                        if (StringUtils.isBlank(personnelMxVO.getAssociation()) || "无".equals(personnelMxVO.getAssociation())) {
                            talentGroupDao.updatePerson(userId, groupId1, talentGroupDO.getGroupName());
                        } else {
                            // 如果association不是无或者空的话，association拼接talentGroupDO的groupName
                            String association = personnelMxVO.getAssociation() + "," + talentGroupDO.getGroupName();
                            talentGroupDao.updatePerson(userId, groupId1, association);
                        }
                    }
                }
            }
        }
        //talentGroupDao.insertQiYe(qiyeMcVOList);
        //talentGroupDao.setGroupID(personVoList);
    }

    public void deleteInfo(String groupId) {
        List<PersonnelMxVO> personnelMxVOList = personnelDao.selectByGroupId(groupId);
        if (personnelMxVOList.size() > 0) {
            for (PersonnelMxVO personnelMxVO : personnelMxVOList) {
                String userId = personnelMxVO.getRybh();
                updatePerson(userId, groupId);
            }
        }
        talentGroupDao.delectMore(groupId);
    }

    public PageInfo<GroupVO> organization(String groupName, Integer pageNum, Integer pageSize) {
        PageHelper.startPage(pageNum, pageSize);
        List<GroupVO> groupPersonVOList = talentGroupDao.selectG(groupName);
        PageInfo<GroupVO> pageInfoVo = new PageInfo<>(groupPersonVOList);
        return pageInfoVo;
    }

    public void deletePersonInfo(String userId, String groupId) {
        //String groupId = talentGroupDao.selectGroupID(userId);
        updatePerson(userId, groupId);
    }

    // 将人员从人员组剔除，主要是更新人员的groupId和association
    public void updatePerson(String userId, String groupId) {
        PersonnelMxVO personnelMxVO = personnelDao.selectPer(userId);
        String groupId1 = personnelMxVO.getGroupId();
        String association1 = personnelMxVO.getAssociation();
        TalentGroupDO talentGroupDO = talentGroupDao.selectGroupInfo(groupId);
        // 将人员的groupId和association更新，逻辑是将groupId和association中的groupId和talentGroupDO的groupName去掉
        if (StringUtils.isNotBlank(groupId1)) {
            String[] groupId1s = groupId1.split(",");
            String[] association1s = association1.split(",");
            List<String> groupId1List = new ArrayList<>();
            List<String> association1List = new ArrayList<>();
            for (String groupId1s1 : groupId1s) {
                if (!groupId1s1.equals(groupId)) {
                    groupId1List.add(groupId1s1);
                }
            }
            for (String association1s1 : association1s) {
                if (!association1s1.equals(talentGroupDO.getGroupName())) {
                    association1List.add(association1s1);
                }
            }
            String groupId2 = StringUtils.join(groupId1List, ",");
            String association2 = StringUtils.join(association1List, ",");
            talentGroupDao.updatePerson(userId, groupId2, association2);
        } else {
            talentGroupDao.updatePerson(userId, groupId1, association1);
        }
    }

    public PersonnelMxVO catchPersonnelHDetails(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 (rybh != null) {
            educationVO = talentGroupDao.selectEducation(rybh);
            reportVO = talentGroupDao.selectReport(rybh);
            skillVO = talentGroupDao.selectSkill(rybh);
            rotaterotateVO = talentGroupDao.selectRotaterotateVO(rybh);
            feiEducationVO = talentGroupDao.selectFeiEducationVO(rybh);
            otherCertificatesVO = talentGroupDao.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 List<GroupPersonVO> seeAndPersonOrganization(GroupMainVo viewObj) {
        List<GroupPersonVO> groupPersonVOList = new ArrayList<>();
        List<QiyeMcVO> qiyeMcVOList = viewObj.getQiyeMcVOList();
        List<SelectPersonVo> personVoList = viewObj.getPersonVoList();
        for (QiyeMcVO qiyeMcVO : qiyeMcVOList) {
            String deptId = qiyeMcVO.getDeptId();
            List<SelectPersonVo> personVoList1 = talentGroupDao.selectpersonId(deptId);
            personVoList.addAll(personVoList1);
        }
        for (SelectPersonVo selectPersonVo : personVoList) {
            String userId = selectPersonVo.getUserId();
            GroupPersonVO groupPersonVO = talentGroupDao.selectPP(userId);
            groupPersonVOList.add(groupPersonVO);
        }
        return groupPersonVOList;


    }

    public List<GetPagePersonnelVO> pageSDetails() {
        List<GetPagePersonnelVO> getPagePersonnelVOS = talentGroupDao.selectGg();
        for (GetPagePersonnelVO getPagePersonnelVO : getPagePersonnelVOS) {
            if (getPagePersonnelVO != null) {
                String qymc = getPagePersonnelVO.getQymc();
                List<BmRecordVO> bmVoList1 = talentGroupDao.selectBm(qymc);
                getPagePersonnelVO.setBmVoList(bmVoList1);
            }

        }
        return getPagePersonnelVOS;
    }

    public PageInfo<PersonnelMxVO> getPpersonDetails(UserQueryVo userQueryVo) {
        List<PersonnelMxVO> userInfoVoList = new ArrayList<>();
        if (userQueryVo.getDeptCodes() != null && userQueryVo.getDeptCodes().length > 0) {
            List<PersonnelMxVO> userInfoVoList3 = talentGroupDao.listInfo(userQueryVo);
            userInfoVoList.addAll(userInfoVoList3);
        }
        if (userQueryVo.getQymc() != null && userQueryVo.getQymc().length > 0) {
            List<PersonnelMxVO> userInfoVoList1 = talentGroupDao.selectAll(userQueryVo);
            userInfoVoList.addAll(userInfoVoList1);
        }
        if (userQueryVo.getRybh() != null && userQueryVo.getRybh().length > 0) {
            List<PersonnelMxVO> userInfoVoList2 = talentGroupDao.selectPerAll(userQueryVo);
            userInfoVoList.addAll(userInfoVoList2);
        }


        return new PageInfo<>(userInfoVoList);
        /*GetPagePersonnelVO getPagePersonnelVO1 = new GetPagePersonnelVO();
        List<BmRecordVO> bmVoList = getPagePersonnelVO.getBmVoList();
        if (bmVoList.size() > 0) {
            for (BmRecordVO bmRecordVO : bmVoList) {
                String bmmc = bmRecordVO.getBmmc();
                bmRecordVO.setBrVoList(talentGroupDao.seleRy(bmmc));
            }
            getPagePersonnelVO1.setBmVoList(bmVoList);
        }
        String tyshxydm = getPagePersonnelVO.getTyshxydm();
        getPagePersonnelVO1.setBrVoList(talentGroupDao.selectRtt(tyshxydm));
        return getPagePersonnelVO1;*/
    }

    public List<GroupPersonSVO> personexport(String groupId) {
//        List<PersonnelMxVO> personnelMxVOList = new ArrayList<>();
//        List<String> rybhList = talentGroupDao.selectPersons(groupId);
//        for (String rybh : rybhList) {
//            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 (rybh != null) {
//                educationVO = talentGroupDao.selectEducation(rybh);
//                reportVO = talentGroupDao.selectReport(rybh);
//                skillVO = talentGroupDao.selectSkill(rybh);
//                rotaterotateVO = talentGroupDao.selectRotaterotateVO(rybh);
//                feiEducationVO = talentGroupDao.selectFeiEducationVO(rybh);
//                otherCertificatesVO = talentGroupDao.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);
//            }
//            personnelMxVOList.add(personnelMxVO);
//        }
        List<GroupPersonSVO> personnelMxVOList = talentGroupDao.selectGroups(groupId);
        return personnelMxVOList;
    }
}

