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

import cn.estsh.i3plus.ext.mes.api.busi.bu.IMesProcessLabelMarkingService;
import cn.estsh.i3plus.ext.mes.pojo.bean.MesProcessLabelMarking;
import cn.estsh.i3plus.ext.mes.pojo.repository.MesProcessLabelMarkingRepository;
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.tool.DdlPreparedPack;
import cn.estsh.impp.framework.boot.exception.ImppExceptionBuilder;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.List;

@Service
public class MesProcessLabelMarkingService implements IMesProcessLabelMarkingService {

    @Autowired
    private MesProcessLabelMarkingRepository processLabelMarkingRepository;

    /**
     * 工序标签标示分页查询
     * @param processLabelMarking
     * @param pager
     * @param organizeCode
     * @return
     */
    @Override
    public ListPager getProcessLabelMarkingPage(MesProcessLabelMarking processLabelMarking, Pager pager, String organizeCode) {
        if (null == processLabelMarking) {
            pager = PagerHelper.getPager(pager, processLabelMarkingRepository.listCount());
            return new ListPager(processLabelMarkingRepository.listPager(pager), pager);
        } else {
            DdlPackBean packBean = MesExtHqlPack.getProcessLabelMarkingSelectQuery(processLabelMarking, organizeCode);

            int count = processLabelMarkingRepository.findByHqlWhereCount(packBean);
            pager = PagerHelper.getPager(pager, count);
            DdlPreparedPack.getOrderByPack(new Object[]{CommonEnumUtil.ASC_OR_DESC.DESC.getValue()}, new String[]{MesExtConstWords.MODIFY_DATE_TIME}, packBean);
            List<MesProcessLabelMarking> processLabelMarkingList = processLabelMarkingRepository.findByHqlWherePage(packBean, pager);

            processLabelMarkingList.forEach(s -> {
                //s.setColorName(StringUtils.isEmpty(s.getColor()) ? null : MesExtEnumUtil.PROCESS_LABEL_MARKING_COLOR.valueOfDescription(s.getColor()));
                s.setColorName(s.getColor());
                //s.setLocationName(StringUtils.isEmpty(s.getLocation()) ? null : MesExtEnumUtil.PROCESS_LABEL_MARKING_LOCATION.valueOfDescription(s.getLocation()));
                s.setLocationName(s.getLocation());
            });

            return new ListPager(processLabelMarkingList, pager);
        }
    }

    /**
     * 新增工序标签标示
     * @param processLabelMarking
     * @param organizeCode
     * @param userName
     */
    @Override
    public void insertProcessLabelMarking(MesProcessLabelMarking processLabelMarking, String organizeCode, String userName) {
        checkRepeat(processLabelMarking, organizeCode);
        processLabelMarking.setOrganizeCode(organizeCode);
        ConvertBean.serviceModelInitialize(processLabelMarking, userName);
        processLabelMarkingRepository.insert(processLabelMarking);
    }

    /**
     * 更新工序标签标示
     * @param processLabelMarking
     * @param organizeCode
     * @param userName
     */
    @Override
    public void updateProcessLabelMarking(MesProcessLabelMarking processLabelMarking, String organizeCode, String userName) {

        processLabelMarking.setOrganizeCode(organizeCode);
        ConvertBean.serviceModelUpdate(processLabelMarking, userName);
        processLabelMarkingRepository.update(processLabelMarking);
    }

    private void checkRepeat(MesProcessLabelMarking processLabelMarking, String organizeCode) {

        DdlPackBean packBeanProcess = DdlPackBean.getDdlPackBean(organizeCode);
        DdlPreparedPack.getStringEqualPack(processLabelMarking.getProcessLabelCode(),"processLabelCode",packBeanProcess);
        MesProcessLabelMarking byProperty = processLabelMarkingRepository.getByProperty(packBeanProcess);

        DdlPackBean packBeanLocation = DdlPackBean.getDdlPackBean(organizeCode);
        DdlPreparedPack.getStringEqualPack(processLabelMarking.getLocation(),"location",packBeanLocation);
        MesProcessLabelMarking byProperty1 = processLabelMarkingRepository.getByProperty(packBeanLocation);

        if (null != byProperty || null != byProperty1) {
            throw ImppExceptionBuilder.newInstance()
                    .setSystemID(CommonEnumUtil.SOFT_TYPE.MES.getCode())
                    .setErrorCode(ImppExceptionEnum.VARIFY_EXCEPTION_DATA_NOT_EXIT.getCode())
                    .setErrorDetail("已存在相同团队管理介绍")
                    .build();
        }

        processLabelMarking.setOrganizeCode(organizeCode);
    }

}
