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

import cn.estsh.i3plus.ext.mes.api.base.IProcessBomService;
import cn.estsh.i3plus.ext.mes.pojo.bean.MesProcessBom;
import cn.estsh.i3plus.ext.mes.pojo.model.SxProcessBomModel;
import cn.estsh.i3plus.ext.mes.pojo.repository.MesProcessBomRepository;
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.platform.common.tool.TimeTool;
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.i3plus.pojo.mes.sqlpack.MesHqlPack;
import cn.estsh.impp.framework.boot.auth.AuthUtil;
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 org.springframework.util.CollectionUtils;
import org.springframework.util.StringUtils;

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

/**
 * @Author: xiangming.liao
 * @CreateDate: 2021/01/17 17:59 下午
 * @Description:
 **/
@Service
public class ProcessBomService implements IProcessBomService {

    @Autowired
    private MesProcessBomRepository mesProcessBomRDao;
    @Override
    public MesProcessBom insertProcessBom(MesProcessBom processBom) {
        ConvertBean.saveOrUpdate(processBom, AuthUtil.getSessionUser().getUserName());
        processBom.setOrganizeCode(AuthUtil.getOrganize().getOrganizeCode());
        MesProcessBom processBomDb = mesProcessBomRDao.getByProperty(
                new String[]{MesExtConstWords.ORGANIZE_CODE, MesExtConstWords.IS_DELETED, MesExtConstWords.PROCESS_CODE, MesExtConstWords.PART_NO, MesExtConstWords.ITEM_PART_NO, MesExtConstWords.IS_BIND_KEY},
                new Object[]{processBom.getOrganizeCode(), CommonEnumUtil.TRUE_OR_FALSE.FALSE.getValue(), processBom.getProcessCode(), processBom.getPartNo(), processBom.getItemPartNo(), processBom.getIsBindKey()});
        if(null != processBomDb){
            throw ImppExceptionBuilder.newInstance()
                    .setSystemID(CommonEnumUtil.SOFT_TYPE.MES.getCode())
                    .setErrorCode(ImppExceptionEnum.VARIFY_EXCEPTION_DATA_EXIT.getCode())
                    .setErrorDetail("已存在成品物料【%s】、原材料【%s】、工序【%s】、是否绑定关键件【%s】的记录",
                            processBom.getPartNo(), processBom.getItemPartNo(), processBom.getProcessCode(), MesExtEnumUtil.IS_BIND_KEY.valueOfDescription(processBom.getIsBindKey()))
                    .build();
        }
        return mesProcessBomRDao.insert(processBom);
    }

    @Override
    public MesProcessBom updateProcessBom(MesProcessBom processBom) {
        MesProcessBom processBomDb2 = mesProcessBomRDao.getByProperty(
                new String[]{MesExtConstWords.ORGANIZE_CODE, MesExtConstWords.IS_DELETED, MesExtConstWords.PROCESS_CODE, MesExtConstWords.PART_NO, MesExtConstWords.ITEM_PART_NO, MesExtConstWords.IS_BIND_KEY},
                new Object[]{processBom.getOrganizeCode(), CommonEnumUtil.TRUE_OR_FALSE.FALSE.getValue(), processBom.getProcessCode(), processBom.getPartNo(), processBom.getItemPartNo(), processBom.getIsBindKey()});
        if(null != processBomDb2 && processBomDb2.getId().compareTo(processBom.getId()) != 0){
            throw ImppExceptionBuilder.newInstance()
                    .setSystemID(CommonEnumUtil.SOFT_TYPE.MES.getCode())
                    .setErrorCode(ImppExceptionEnum.VARIFY_EXCEPTION_DATA_EXIT.getCode())
                    .setErrorDetail("已存在成品物料【%s】、原材料【%s】、工序【%s】、是否绑定关键件【%s】的记录",
                            processBom.getPartNo(), processBom.getItemPartNo(), processBom.getProcessCode(), MesExtEnumUtil.IS_BIND_KEY.valueOfDescription(processBom.getIsBindKey()))
                    .build();
        }
        MesProcessBom processBomDb = mesProcessBomRDao.getById(processBom.getId());
        if(null == processBomDb){
            throw ImppExceptionBuilder.newInstance()
                    .setSystemID(CommonEnumUtil.SOFT_TYPE.MES.getCode())
                    .setErrorCode(ImppExceptionEnum.VARIFY_EXCEPTION_DATA_EXIT.getCode())
                    .setErrorDetail("ID【%s】数据不存在", processBom.getId())
                    .build();
        }
        processBomDb.setProcessCode(processBom.getProcessCode());
        processBomDb.setProcessName(processBom.getProcessName());
        processBomDb.setQty(processBom.getQty());
        processBomDb.setIsBindKey(processBom.getIsBindKey());
        processBomDb.setIsCheck(processBom.getIsCheck());
        processBomDb.setIsRepeat(processBom.getIsRepeat());
        processBomDb.setMatchRule(processBom.getMatchRule());
        ConvertBean.serviceModelUpdate(processBomDb, AuthUtil.getSessionUser().getUserName());
        return mesProcessBomRDao.save(processBomDb);
    }

    @Override
    public void updateStatusBatchProcessBom(String[] ids, Integer status, String userName) {
        if (status != CommonEnumUtil.IS_VAILD.INVAILD.getValue() && status != CommonEnumUtil.IS_VAILD.VAILD.getValue()) {
            throw ImppExceptionBuilder.newInstance()
                    .setSystemID(CommonEnumUtil.SOFT_TYPE.MES.getCode())
                    .setErrorCode(ImppExceptionEnum.VARIFY_EXCEPTION.getCode())
                    .setErrorDetail("启用，禁用枚举状态值【%s】未被定义", status)
                    .build();
        }
        for (String id : ids) {
            if (StringUtils.isEmpty(id)) {
                continue;
            }
            mesProcessBomRDao.updateByProperties(new String[]{MesExtConstWords.ID}, new Object[]{Long.parseLong(id)},
                    new String[]{MesExtConstWords.IS_VALID, MesExtConstWords.MODIFY_USER, MesExtConstWords.MODIFY_DATE_TIME},
                    new Object[]{status, userName, TimeTool.getNowTime(true)});
        }

    }

    @Override
    public ListPager<SxProcessBomModel> queryProcessBomByPager(MesProcessBom processBom, Pager pager) {
        List<SxProcessBomModel> resultList = new ArrayList<>();
        DdlPackBean packBean = MesHqlPack.getMesAllData(AuthUtil.getOrganize().getOrganizeCode());
        DdlPreparedPack.getNumEqualPack(processBom.getIsValid(), MesExtConstWords.IS_VALID, packBean);
        DdlPreparedPack.getStringEqualPack(processBom.getPartNo(), MesExtConstWords.PART_NO, packBean);
        DdlPreparedPack.getStringEqualPack(processBom.getItemPartNo(), MesExtConstWords.ITEM_PART_NO, packBean);
        DdlPreparedPack.getStringLikerPack(processBom.getProcessCode(), MesExtConstWords.PROCESS_CODE, packBean);
        DdlPreparedPack.getNumEqualPack(processBom.getIsBindKey(), MesExtConstWords.IS_BIND_KEY, packBean);
        pager = PagerHelper.getPager(pager, mesProcessBomRDao.findByHqlWhereCount(packBean));
        packBean.setOrderByStr(processBom.orderBy());
        List<MesProcessBom> processBomList = mesProcessBomRDao.findByHqlWherePage(packBean, pager);
        if (!CollectionUtils.isEmpty(processBomList)) {
            for (MesProcessBom item : processBomList) {
                if (null == item) {
                    continue;
                }
                SxProcessBomModel result = new SxProcessBomModel();
                BeanUtils.copyProperties(item, result);
                result.setIsCheckName(
                        (!StringUtils.isEmpty(result.getIsCheck()) && !StringUtils.isEmpty(MesExtEnumUtil.IS_CHECK.valueOfDescription(result.getIsCheck())))
                                ? MesExtEnumUtil.IS_CHECK.valueOfDescription(result.getIsCheck()) : null);
                result.setIsRepeatName(
                        (!StringUtils.isEmpty(result.getIsRepeat()) && !StringUtils.isEmpty(MesExtEnumUtil.IS_REPEAT.valueOfDescription(result.getIsRepeat())))
                                ? MesExtEnumUtil.IS_REPEAT.valueOfDescription(result.getIsRepeat()) : null);
                result.setIsBindKeyName(
                        (!StringUtils.isEmpty(result.getIsBindKey()) && !StringUtils.isEmpty(MesExtEnumUtil.IS_BIND_KEY.valueOfDescription(result.getIsBindKey())))
                                ? MesExtEnumUtil.IS_BIND_KEY.valueOfDescription(result.getIsBindKey()) : null);
                resultList.add(result);
            }
        }
        return new ListPager(resultList, pager);
    }
}
