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.MesPart;
import cn.estsh.i3plus.pojo.mes.bean.MesProduceSn;
import cn.estsh.i3plus.pojo.mes.repository.MesPartRepository;
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.StringUtils;

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

/**
 * 自制件箱码加料
 * YFWHU|YFNSC901C241223042722|C901|401007574AAABN|9.0|EA|C901|20241223|
 */
@Slf4j
@Component
public class MesContainerBindPackageSnService extends MesAbstractContainerBindDispatchService {
    @Autowired
    private MesProduceSnRepository produceSnRDao;
    @Autowired
    private MesPartRepository mesPartRDao;

    public MesContainerBindPackageSnService() {
        super(null, MesExtEnumUtil.CONTAINER_PART_MATCH_TYPE.MATCH_TYPE_30);
    }

    @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_20.getValue());

        String[] splitBarcode = barcode.split("\\|");
        if (splitBarcode.length < 8) {
            result.put(MesPcnExtConstWords.MESSAGE, String.format("自制件箱码【%s】格式不对, 请检查数据！", barcode));
            return result;
        }

        String partNo = splitBarcode[3];
        MesPart mesPart = mesPartRDao.getByProperty(
                new String[]{MesPcnExtConstWords.ORGANIZE_CODE, MesPcnExtConstWords.IS_DELETED, MesPcnExtConstWords.IS_VALID, "partNo"},
                new Object[]{organizeCode, CommonEnumUtil.TRUE_OR_FALSE.FALSE.getValue(), CommonEnumUtil.IS_VAILD.VAILD.getValue(), partNo});
        if (mesPart == null) {
            MesPcnException.throwMesBusiException("零件号【%s】在物料表不存在, 请检查数据！", model.getPartNo());
        }
        if (!Objects.equals(mesPart.getPartNo(), model.getPartNo())) {
            return result;
        }

        String barOrganizeCode = splitBarcode[2];
        if (!Objects.equals(barOrganizeCode, organizeCode)) {
            MesPcnException.throwMesBusiException("自制件箱码【%s】的工厂代码【%s】与当前工厂不匹配, 请检查数据！", barcode, barOrganizeCode);
        }

        String barQty = splitBarcode[4];
        if (StringUtils.isEmpty(barQty)) {
            MesPcnException.throwMesBusiException("自制件箱码【%s】的数量为空, 请检查数据！", barcode);
        }
        try {
            result.put(MesPcnExtConstWords.QTY, Double.valueOf(barQty));
        } catch (NumberFormatException e) {
            MesPcnException.throwMesBusiException("自制件箱码【%s】的数量【%s】格式错误, 请检查数据！", barcode, barQty);
        }
        result.put(MesPcnExtConstWords.RESULT, true);
        return result;
    }

}
