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

import cn.estsh.i3plus.ext.mes.api.busi.bu.IMesSkillKanbanProcessInformationService;
import cn.estsh.i3plus.ext.mes.pojo.bean.MesSkillKanbanProcessInformation;
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.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.enumutil.MesEnumUtil;
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.StringUtils;

import java.util.List;

@Service
public class MesSkillKanbanProcessInformationService implements IMesSkillKanbanProcessInformationService {

    @Autowired
    private MesSkillKanbanProcessInformationRepository skillKanbanProcessInformationRepository;

    /**
     * 技能看板工序信息分页查询
     * @param skillKanbanProcessInformation
     * @param pager
     * @param organizeCode
     * @return
     */
    @Override
    public ListPager getSkillKanbanProcessInformationPage(MesSkillKanbanProcessInformation skillKanbanProcessInformation, Pager pager, String organizeCode) {
        if (null == skillKanbanProcessInformation) {
            pager = PagerHelper.getPager(pager, skillKanbanProcessInformationRepository.listCount());
            return new ListPager(skillKanbanProcessInformationRepository.listPager(pager), pager);
        } else {
            DdlPackBean packBean = MesExtHqlPack.getSkillKanbanProcessInformationSelectQuery(skillKanbanProcessInformation, organizeCode);

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

            return new ListPager(skillKanbanProcessInformationList, pager);
        }
    }

    /**
     * 新增技能看板工序信息
     * @param skillKanbanProcessInformation
     * @param organizeCode
     * @param userName
     */
    @Override
    public void insertSkillKanbanProcessInformation(MesSkillKanbanProcessInformation skillKanbanProcessInformation, String organizeCode, String userName) {

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

    /**
     * 更新技能看板工序信息
     * @param skillKanbanProcessInformation
     * @param organizeCode
     * @param userName
     */
    @Override
    public void updateSkillKanbanProcessInformation(MesSkillKanbanProcessInformation skillKanbanProcessInformation, String organizeCode, String userName) {
//        checkRepeat(skillKanbanProcessInformation, organizeCode);
        skillKanbanProcessInformation.setOrganizeCode(organizeCode);
        ConvertBean.serviceModelUpdate(skillKanbanProcessInformation, userName);
        skillKanbanProcessInformationRepository.update(skillKanbanProcessInformation);
    }

    private void checkRepeat(MesSkillKanbanProcessInformation skillKanbanProcessInformation, String organizeCode) {
        DdlPackBean packBean = DdlPackBean.getDdlPackBean(organizeCode);
        DdlPreparedPack.getNumEqualPack(skillKanbanProcessInformation.getSeq(),"seq",packBean);
        DdlPreparedPack.getStringEqualPack(skillKanbanProcessInformation.getWorkCenterCode(),"workCenterCode",packBean);
        DdlPreparedPack.getStringEqualPack(skillKanbanProcessInformation.getSkillKanbanProcessCode(),"skillKanbanProcessCode",packBean);
        MesSkillKanbanProcessInformation skillKanbanProcessInformationCheck = skillKanbanProcessInformationRepository.getByProperty(packBean);

        if (null != skillKanbanProcessInformationCheck) {
            throw ImppExceptionBuilder.newInstance()
                    .setSystemID(CommonEnumUtil.SOFT_TYPE.MES.getCode())
                    .setErrorCode(ImppExceptionEnum.VARIFY_EXCEPTION_DATA_NOT_EXIT.getCode())
                    .setErrorDetail("已存在相同技能看板工序信息")
                    .build();
        }

        DdlPackBean packBeanSeq = DdlPackBean.getDdlPackBean(organizeCode);
        DdlPreparedPack.getStringEqualPack(skillKanbanProcessInformation.getWorkCenterCode(),"workCenterCode",packBeanSeq);
        DdlPreparedPack.getNumEqualPack(skillKanbanProcessInformation.getSeq(),MesExtConstWords.SEQ,packBeanSeq);
        MesSkillKanbanProcessInformation skillKanbanProcessInformationSeq = skillKanbanProcessInformationRepository.getByProperty(packBeanSeq);

        if (null != skillKanbanProcessInformationSeq) {
            throw ImppExceptionBuilder.newInstance()
                    .setSystemID(CommonEnumUtil.SOFT_TYPE.MES.getCode())
                    .setErrorCode(ImppExceptionEnum.VARIFY_EXCEPTION_DATA_NOT_EXIT.getCode())
                    .setErrorDetail("已存在序号【%s】",skillKanbanProcessInformation.getSeq())
                    .build();
        }

        skillKanbanProcessInformation.setOrganizeCode(organizeCode);
    }

    private Integer selectBigManage(String organizeCode) {
        DdlPackBean packBean = DdlPackBean.getDdlPackBean(organizeCode);
        DdlPreparedPack.getOrderByPack(new Object[]{MesEnumUtil.ASC_OR_DESC.DESC.getValue()}, new String[]{MesExtConstWords.SEQ}, packBean);
        MesSkillKanbanProcessInformation byProperty = skillKanbanProcessInformationRepository.getByProperty(packBean);
        return null == byProperty ? 0 : byProperty.getSeq();
    }

}
