package cn.estsh.i3plus.ext.mes.pcn.apiservice.serviceimpl.rulematch.containerbind;

import cn.estsh.i3plus.ext.mes.pcn.apiservice.util.MesPcnException;
import cn.estsh.i3plus.ext.mes.pcn.pojo.model.MesContainerPartsModel;
import cn.estsh.i3plus.ext.mes.pcn.pojo.util.MesPcnExtConstWords;
import cn.estsh.i3plus.pojo.base.enumutil.CommonEnumUtil;
import cn.estsh.i3plus.pojo.mes.bean.MesProduceSn;
import cn.estsh.i3plus.pojo.mes.repository.MesProduceSnRepository;
import cn.estsh.i3plus.pojo.mes.util.MesExtEnumUtil;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Component;
import org.springframework.util.CollectionUtils;

import java.util.HashMap;
import java.util.Map;
import java.util.Objects;

/**
 * 自制件条码加料
 */
@Slf4j
@Component
public class MesContainerBindProductSnService extends MesAbstractContainerBindDispatchService {
    @Autowired
    private MesProduceSnRepository produceSnRDao;

    public MesContainerBindProductSnService() {
        super(null, MesExtEnumUtil.CONTAINER_PART_MATCH_TYPE.MATCH_TYPE_10);
    }

    @Override
    public Map<String, Object> matchNumberRule(String organizeCode, String barcode, MesContainerPartsModel model) {
        Map<String, Object> result = new HashMap<>();
        result.put(MesPcnExtConstWords.RESULT, false);
        result.put(MesPcnExtConstWords.DATA_TYPE, MesExtEnumUtil.CONTAINER_BARCODE_DATA_TYPE.STATUS_10.getValue());

        MesProduceSn produceSn = produceSnRDao.getByProperty(
                new String[]{MesPcnExtConstWords.ORGANIZE_CODE, MesPcnExtConstWords.IS_DELETED, MesPcnExtConstWords.IS_VALID, "productSn"},
                new Object[]{organizeCode, CommonEnumUtil.TRUE_OR_FALSE.FALSE.getValue(), CommonEnumUtil.IS_VAILD.VAILD.getValue(), barcode});
        if (produceSn == null) {
            result.put(MesPcnExtConstWords.MESSAGE, String.format("扫描条码【%s】在条码表不存在, 请检查数据！", barcode));
            return result;
        }
        if (!Objects.equals(produceSn.getPartNo(), model.getPartNo())) {
            return result;
        }

        if (!Objects.equals(produceSn.getSnStatus(), MesExtEnumUtil.PRODUCE_SN_STATUS.CREATE.getValue()) &&
                !Objects.equals(produceSn.getSnStatus(), MesExtEnumUtil.PRODUCE_SN_STATUS.OFFLINE.getValue())) {
            MesPcnException.throwMesBusiException("扫描条码【%s】的条码状态必须为“创建”或“已完成”, 请检查数据！", barcode);
        }

        if (!Objects.equals(produceSn.getQcStatus(), MesExtEnumUtil.PRODUCE_QC_STATUS.QUALIFIED.getValue()) &&
                !Objects.equals(produceSn.getQcStatus(), MesExtEnumUtil.PRODUCE_QC_STATUS.SUSPICIOUS_TO_QUALIFIED.getValue())) {
            MesPcnException.throwMesBusiException("扫描条码【%s】的质量状态必须为“合格”或“可疑转正常”, 请检查数据！", barcode);
        }

        if (Objects.equals(model.getContainerType(), MesExtEnumUtil.CONTAINER_TYPE.CONTAINER_TYPE_50.getValue())) {
            Double netWeight = getRawSnWeightQty(organizeCode, barcode);
            result.put(MesPcnExtConstWords.QTY, netWeight);
        }
        result.put(MesPcnExtConstWords.RESULT, true);
        return result;
    }
}
