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

import cn.estsh.i3plus.ext.mes.api.busi.bu.IMesEmployeeAndProcessBindService;
import cn.estsh.i3plus.ext.mes.pojo.bean.MesEmployeeAndProcessBind;
import cn.estsh.i3plus.ext.mes.pojo.bean.MesEmployeeSkillsManagement;
import cn.estsh.i3plus.ext.mes.pojo.bean.MesSkillKanbanProcessInformation;
import cn.estsh.i3plus.ext.mes.pojo.model.bu.EmployeeAndProcessBindModel;
import cn.estsh.i3plus.ext.mes.pojo.repository.MesEmployeeAndProcessBindRepository;
import cn.estsh.i3plus.ext.mes.pojo.repository.MesEmployeeSkillsManagementRepository;
import cn.estsh.i3plus.ext.mes.pojo.repository.MesSkillKanbanProcessInformationRepository;
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.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

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

@Service
public class
MesEmployeeAndProcessBindService implements IMesEmployeeAndProcessBindService {

    @Autowired
    private MesSkillKanbanProcessInformationRepository skillKanbanProcessInformationRepository;

    @Autowired
    private MesEmployeeSkillsManagementRepository employeeSkillsManagementRepository;

    @Autowired
    private MesEmployeeAndProcessBindRepository employeeAndProcessBindRepository;

    /**
     * 人员与工序关系分页查询
     * @param employeeAndProcessBind
     * @param pager
     * @param organizeCode
     * @return
     */
    @Override
    public ListPager getemployeeAndProcessBindPage(MesEmployeeAndProcessBind employeeAndProcessBind, Pager pager, String organizeCode) {
        if (null == employeeAndProcessBind) {
            pager = PagerHelper.getPager(pager, employeeAndProcessBindRepository.listCount());
            return new ListPager(employeeAndProcessBindRepository.listPager(pager), pager);
        } else {
            DdlPackBean packBean = MesExtHqlPack.getEmployeeAndProcessBindSelectQuery(employeeAndProcessBind, organizeCode);

            int count = employeeAndProcessBindRepository.findByHqlWhereCount(packBean);
            pager = PagerHelper.getPager(pager, count);
            DdlPreparedPack.getOrderByPack(new Object[]{"2"}, new String[]{MesExtConstWords.MODIFY_DATE_TIME}, packBean);
            List<MesEmployeeAndProcessBind> employeeAndProcessBindList = employeeAndProcessBindRepository.findByHqlWherePage(packBean, pager);

            DdlPackBean packBeanEmployee = DdlPackBean.getDdlPackBean(organizeCode);
            List<MesEmployeeSkillsManagement> employeeSkillsManagementList = employeeSkillsManagementRepository.findByHqlWhere(packBeanEmployee);
            Map<String, MesEmployeeSkillsManagement> employeeSkillsManagementMap = employeeSkillsManagementList.stream().collect(Collectors.toMap(MesEmployeeSkillsManagement::getEmployeeCode, t -> t));

            DdlPackBean packBeanProcess = DdlPackBean.getDdlPackBean(organizeCode);
            List<MesSkillKanbanProcessInformation> kanbanProcessInformationList = skillKanbanProcessInformationRepository.findByHqlWhere(packBeanProcess);
            Map<String, MesSkillKanbanProcessInformation> skillKanbanProcessInformationMap = kanbanProcessInformationList.stream().collect(Collectors.toMap(MesSkillKanbanProcessInformation::getSkillKanbanProcessCode, t -> t));

            List<EmployeeAndProcessBindModel> employeeAndProcessBindModelList = new ArrayList<>();

            employeeAndProcessBindList.forEach(e -> {
                EmployeeAndProcessBindModel employeeAndProcessBindModel = new EmployeeAndProcessBindModel();
                BeanUtils.copyProperties(e,employeeAndProcessBindModel);
                employeeAndProcessBindModel.setEmployeeName(employeeSkillsManagementMap.get(employeeAndProcessBindModel.getEmployeeCode()).getEmployeeName());
                employeeAndProcessBindModel.setSkillKanbanProcessName(skillKanbanProcessInformationMap.get(employeeAndProcessBindModel.getSkillKanbanProcessCode()).getSkillKanbanProcessName());
                employeeAndProcessBindModelList.add(employeeAndProcessBindModel);
            });

            return new ListPager(employeeAndProcessBindModelList, pager);
        }
    }

    /**
     * 新增人员与工序关系
     * @param employeeAndProcessBind
     * @param organizeCode
     * @param userName
     */
    @Override
    public void insertEmployeeAndProcessBind(MesEmployeeAndProcessBind employeeAndProcessBind, String organizeCode, String userName) {

        checkRepeat(employeeAndProcessBind, organizeCode);
        ConvertBean.serviceModelInitialize(employeeAndProcessBind, userName);
        employeeAndProcessBindRepository.insert(employeeAndProcessBind);
    }

    /**
     * 更新人员与工序关系
     * @param employeeAndProcessBind
     * @param organizeCode
     * @param userName
     */
    @Override
    public void updateEmployeeAndProcessBind(MesEmployeeAndProcessBind employeeAndProcessBind, String organizeCode, String userName) {

        employeeAndProcessBind.setOrganizeCode(organizeCode);
        checkRepeatUpdate(employeeAndProcessBind, organizeCode);
        ConvertBean.serviceModelUpdate(employeeAndProcessBind, userName);
        employeeAndProcessBindRepository.update(employeeAndProcessBind);
    }

    /**
     * 更新人员与工序关系信息状态
     * @param ids id集合
     * @param status 状态
     * @param userName 用户名
     */
    @Override
    public int updateEmployeeAndProcessBindValidStatusByIds(Long[] ids, int status, String userName) {
        return employeeAndProcessBindRepository.updateValidStatusByIds(ids, status, userName);
    }

    private void checkRepeat(MesEmployeeAndProcessBind employeeAndProcessBind, String organizeCode) {
        DdlPackBean packBean = MesExtHqlPack.getEmployeeAndProcessBindSelectQuery(employeeAndProcessBind, organizeCode);
        MesEmployeeAndProcessBind employeeAndProcessBindCheck = employeeAndProcessBindRepository.getByProperty(packBean);

        if (null != employeeAndProcessBindCheck) {
            throw ImppExceptionBuilder.newInstance()
                    .setSystemID(CommonEnumUtil.SOFT_TYPE.MES.getCode())
                    .setErrorCode(ImppExceptionEnum.VARIFY_EXCEPTION_DATA_NOT_EXIT.getCode())
                    .setErrorDetail("已存在相同人员与工序关系")
                    .build();
        }

        employeeAndProcessBind.setOrganizeCode(organizeCode);
    }

    private void checkRepeatUpdate(MesEmployeeAndProcessBind employeeAndProcessBind, String organizeCode) {

        if (null != employeeAndProcessBind.getIsValid() && employeeAndProcessBind.getIsValid().equals(MesExtEnumUtil.IS_VAILD.INVAILD.getValue())){
            return;
        }

        DdlPackBean packBean = DdlPackBean.getDdlPackBean(organizeCode);
        DdlPreparedPack.getStringEqualPack(employeeAndProcessBind.getWorkCenterCode(),"workCenterCode",packBean);
        DdlPreparedPack.getStringEqualPack(employeeAndProcessBind.getSkillKanbanProcessCode(),"skillKanbanProcessCode",packBean);
        DdlPreparedPack.getStringEqualPack(employeeAndProcessBind.getEmployeeCode(),"employeeCode",packBean);
        MesEmployeeAndProcessBind employeeAndProcessBindCheck = employeeAndProcessBindRepository.getByProperty(packBean);

        if (null != employeeAndProcessBindCheck) {
            throw ImppExceptionBuilder.newInstance()
                    .setSystemID(CommonEnumUtil.SOFT_TYPE.MES.getCode())
                    .setErrorCode(ImppExceptionEnum.VARIFY_EXCEPTION_DATA_NOT_EXIT.getCode())
                    .setErrorDetail("已存在相同人员与工序关系")
                    .build();
        }

        employeeAndProcessBind.setOrganizeCode(organizeCode);
    }



}
