package cn.estsh.i3plus.ext.mes.apiservice.serviceimpl.base.bu;

import cn.estsh.i3plus.ext.mes.api.busi.bu.IMesEmployeeSkillsManagementService;
import cn.estsh.i3plus.ext.mes.pojo.bean.MesEmployeeNature;
import cn.estsh.i3plus.ext.mes.pojo.bean.MesEmployeeSkillsManagement;
import cn.estsh.i3plus.ext.mes.pojo.repository.MesEmployeeNatureRepository;
import cn.estsh.i3plus.ext.mes.pojo.repository.MesEmployeeSkillsManagementRepository;
import cn.estsh.i3plus.ext.mes.pojo.sqlpack.MesExtHqlPack;
import cn.estsh.i3plus.ext.mes.pojo.util.MesExtConstWords;
import cn.estsh.i3plus.ext.mes.pojo.util.MesExtEnumUtil;
import cn.estsh.i3plus.platform.common.convert.ConvertBean;
import cn.estsh.i3plus.platform.common.exception.ImppExceptionEnum;
import cn.estsh.i3plus.pojo.base.bean.DdlPackBean;
import cn.estsh.i3plus.pojo.base.bean.ListPager;
import cn.estsh.i3plus.pojo.base.common.Pager;
import cn.estsh.i3plus.pojo.base.common.PagerHelper;
import cn.estsh.i3plus.pojo.base.enumutil.CommonEnumUtil;
import cn.estsh.i3plus.pojo.base.tool.DdlPreparedPack;
import cn.estsh.impp.framework.boot.exception.ImppExceptionBuilder;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.util.CollectionUtils;
import org.springframework.util.StringUtils;

import java.util.List;
import java.util.Map;
import java.util.stream.Collectors;

@Service
public class MesEmployeeSkillsManagementService implements IMesEmployeeSkillsManagementService {

    @Autowired
    private MesEmployeeNatureRepository employeeNatureRepository;

    @Autowired
    private MesEmployeeSkillsManagementRepository employeeSkillsManagementRepository;

    /**
     * 员工技能管理分页查询
     * @param employeeSkillsManagement
     * @param pager
     * @param organizeCode
     * @return
     */
    @Override
    public ListPager getEmployeeSkillsManagementPage(MesEmployeeSkillsManagement employeeSkillsManagement, Pager pager, String organizeCode) {
        if (null == employeeSkillsManagement) {
            pager = PagerHelper.getPager(pager, employeeSkillsManagementRepository.listCount());
            return new ListPager(employeeSkillsManagementRepository.listPager(pager), pager);
        } else {
            DdlPackBean packBean = MesExtHqlPack.getEmployeeSkillsManagementSelectQuery(employeeSkillsManagement, organizeCode);

            int count = employeeSkillsManagementRepository.findByHqlWhereCount(packBean);
            pager = PagerHelper.getPager(pager, count);
            DdlPreparedPack.getOrderByPack(new Object[]{CommonEnumUtil.ASC_OR_DESC.DESC.getValue()}, new String[]{MesExtConstWords.SEQ}, packBean);
            List<MesEmployeeSkillsManagement> employeeSkillsManagementList = employeeSkillsManagementRepository.findByHqlWherePage(packBean, pager);

            DdlPackBean packBeanEmployeeNature = DdlPackBean.getDdlPackBean(organizeCode);
            List<MesEmployeeNature> employeeNatureList = employeeNatureRepository.findByHqlWhere(packBeanEmployeeNature);
            Map<String, MesEmployeeNature> employeeNatureMap = employeeNatureList.stream().collect(Collectors.toMap(MesEmployeeNature::getEmployeeNatureCode, t -> t));

            for (MesEmployeeSkillsManagement mesEmployeeSkillsManagement : employeeSkillsManagementList) {
                mesEmployeeSkillsManagement.setSkillLevelName(StringUtils.isEmpty(mesEmployeeSkillsManagement.getSkillLevel()) ? null : MesExtEnumUtil.SKILLS_MANAGEMENT_BENCHMARK_SKTLLS_LEVEL.valueOfDescription(mesEmployeeSkillsManagement.getSkillLevel()));
                mesEmployeeSkillsManagement.setEmployeeNatureName(CollectionUtils.isEmpty(employeeNatureMap) || !employeeNatureMap.containsKey(mesEmployeeSkillsManagement.getEmployeeNatureCode()) ? "" : employeeNatureMap.get(mesEmployeeSkillsManagement.getEmployeeNatureCode()).getEmployeeNatureName());
            }

//            employeeSkillsManagementList.forEach(s -> {
//                s.setSkillLevelName(StringUtils.isEmpty(s.getSkillLevel()) ? null : MesExtEnumUtil.SKILLS_MANAGEMENT_BENCHMARK_SKTLLS_LEVEL.valueOfDescription(s.getSkillLevel()));
//                s.setEmployeeNatureName(CollectionUtils.isEmpty(employeeNatureMap) && !employeeNatureMap.containsKey(s.getEmployeeNatureCode()) ? "" : employeeNatureMap.get(s.getEmployeeNatureCode()).getEmployeeNatureName());
//            });

            return new ListPager(employeeSkillsManagementList, pager);
        }
    }

    /**
     * 新增员工技能管理
     * @param employeeSkillsManagement
     * @param organizeCode
     * @param userName
     */
    @Override
    public void insertEmployeeSkillsManagement(MesEmployeeSkillsManagement employeeSkillsManagement, String organizeCode, String userName) {

        checkRepeat(employeeSkillsManagement, organizeCode, MesExtConstWords.INSERT);
        employeeSkillsManagement.setOrganizeCode(organizeCode);
        /*//增加序号
        Integer bigNum = selectBigManage(organizeCode);
        if (null == bigNum){
            employeeSkillsManagement.setSeq(1);
        }else {
            employeeSkillsManagement.setSeq(bigNum + 1);
        }*/
        ConvertBean.serviceModelInitialize(employeeSkillsManagement, userName);
        employeeSkillsManagementRepository.insert(employeeSkillsManagement);
    }

    /**
     * 更新员工技能管理
     * @param employeeSkillsManagement
     * @param organizeCode
     * @param userName
     */
    @Override
    public void updateEmployeeSkillsManagement(MesEmployeeSkillsManagement employeeSkillsManagement, String organizeCode, String userName) {

        checkRepeat(employeeSkillsManagement, organizeCode, MesExtConstWords.UPDATE);
        employeeSkillsManagement.setOrganizeCode(organizeCode);
        ConvertBean.serviceModelUpdate(employeeSkillsManagement, userName);
        employeeSkillsManagementRepository.update(employeeSkillsManagement);
    }

    private void checkRepeat(MesEmployeeSkillsManagement employeeSkillsManagement, String organizeCode, String operationType) {

        if(operationType.equals(MesExtConstWords.INSERT)){
            MesEmployeeSkillsManagement mesEmployeeSkillsManagementDb = employeeSkillsManagementRepository.getByProperty(new String[]{MesExtConstWords.ORGANIZE_CODE, MesExtConstWords.IS_DELETED, MesExtConstWords.IS_VALID, MesExtConstWords.SEQ,  MesExtConstWords.WORK_CENTER_CODE},
                    new Object[]{employeeSkillsManagement.getOrganizeCode(), CommonEnumUtil.TRUE_OR_FALSE.FALSE.getValue(), CommonEnumUtil.TRUE_OR_FALSE.TRUE.getValue(), employeeSkillsManagement.getSeq(), employeeSkillsManagement.getWorkCenterCode()});

            if (null != mesEmployeeSkillsManagementDb) {
                throw ImppExceptionBuilder.newInstance()
                        .setSystemID(CommonEnumUtil.SOFT_TYPE.QMS.getCode())
                        .setErrorCode(ImppExceptionEnum.VARIFY_EXCEPTION_DATA_EXIT.getCode())
                        .setErrorDetail("根据[序号,产线代码]查询已经存在此条数据,修改失败")
                        .build();
            }

            DdlPackBean packBean = DdlPackBean.getDdlPackBean(organizeCode);
            DdlPreparedPack.getStringEqualPack(employeeSkillsManagement.getEmployeeCode(), MesExtConstWords.EMPLOYEE_CODE,packBean);
            MesEmployeeSkillsManagement employeeSkillsManagementCheck = employeeSkillsManagementRepository.getByProperty(packBean);

            if ((null != employeeSkillsManagementCheck) && (!employeeSkillsManagement.getWorkCenterCode().equals(employeeSkillsManagementCheck.getWorkCenterCode()) || employeeSkillsManagementCheck.getSeq() != employeeSkillsManagement.getSeq()) && !StringUtils.isEmpty(employeeSkillsManagementCheck.getWorkCenterCode())) {
                throw ImppExceptionBuilder.newInstance()
                        .setSystemID(CommonEnumUtil.SOFT_TYPE.MES.getCode())
                        .setErrorCode(ImppExceptionEnum.VARIFY_EXCEPTION_DATA_NOT_EXIT.getCode())
                        .setErrorDetail("已存在相同员工")
                        .build();
            }
        }else{
            MesEmployeeSkillsManagement skillsManagementMeta = employeeSkillsManagementRepository.getById(employeeSkillsManagement.getId());

            if(skillsManagementMeta == null){
                throw ImppExceptionBuilder.newInstance()
                        .setSystemID(CommonEnumUtil.SOFT_TYPE.MES.getCode())
                        .setErrorCode(ImppExceptionEnum.VARIFY_EXCEPTION_DATA_NOT_EXIT.getCode())
                        .setErrorDetail("数据不存在")
                        .build();
            }

            MesEmployeeSkillsManagement mesEmployeeSkillsManagementDb = employeeSkillsManagementRepository.getByProperty(new String[]{MesExtConstWords.ORGANIZE_CODE, MesExtConstWords.IS_DELETED, MesExtConstWords.IS_VALID, MesExtConstWords.SEQ,  MesExtConstWords.WORK_CENTER_CODE},
                    new Object[]{employeeSkillsManagement.getOrganizeCode(), CommonEnumUtil.TRUE_OR_FALSE.FALSE.getValue(), CommonEnumUtil.TRUE_OR_FALSE.TRUE.getValue(), employeeSkillsManagement.getSeq(), employeeSkillsManagement.getWorkCenterCode()});

            if (null != mesEmployeeSkillsManagementDb && mesEmployeeSkillsManagementDb.getId().compareTo(employeeSkillsManagement.getId()) != 0) {
                throw ImppExceptionBuilder.newInstance()
                        .setSystemID(CommonEnumUtil.SOFT_TYPE.QMS.getCode())
                        .setErrorCode(ImppExceptionEnum.VARIFY_EXCEPTION_DATA_EXIT.getCode())
                        .setErrorDetail("根据[序号,产线代码]查询已经存在此条数据,修改失败")
                        .build();
            }
        }
//
//        DdlPackBean packBeanSeq = DdlPackBean.getDdlPackBean(organizeCode);
//        DdlPreparedPack.getNumEqualPack(employeeSkillsManagement.getSeq(),MesExtConstWords.SEQ,packBeanSeq);
//        MesEmployeeSkillsManagement employeeSkillsManagementSeq = employeeSkillsManagementRepository.getByProperty(packBeanSeq);
//
//        if (null != employeeSkillsManagementSeq) {
//            throw ImppExceptionBuilder.newInstance()
//                    .setSystemID(CommonEnumUtil.SOFT_TYPE.MES.getCode())
//                    .setErrorCode(ImppExceptionEnum.VARIFY_EXCEPTION_DATA_NOT_EXIT.getCode())
//                    .setErrorDetail("已存在相同序号【%s】",employeeSkillsManagement.getSeq())
//                    .build();
//        }

        employeeSkillsManagement.setOrganizeCode(organizeCode);
    }

}
