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

import cn.estsh.i3plus.ext.mes.pcn.api.busi.IMesContainerBindDispatchService;
import cn.estsh.i3plus.ext.mes.pcn.apiservice.util.MesPcnException;
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.MesRawSnWeightRecord;
import cn.estsh.i3plus.pojo.mes.repository.MesContainerSnPartRepository;
import cn.estsh.i3plus.pojo.mes.repository.MesContainerTypePartRepository;
import cn.estsh.i3plus.pojo.mes.repository.MesRawSnWeightRecordRepository;
import cn.estsh.i3plus.pojo.mes.util.MesExtEnumUtil;
import org.springframework.beans.factory.annotation.Autowired;

/**
 * @author jason
 */
public class MesAbstractContainerBindDispatchService implements IMesContainerBindDispatchService {
    @Autowired
    protected MesContainerSnPartRepository containerSnPartRDao;
    @Autowired
    protected MesContainerTypePartRepository containerTypePartRDao;
    @Autowired
    protected MesRawSnWeightRecordRepository rawSnWeightRecordRDao;

    protected MesAbstractContainerBindDispatchService(MesExtEnumUtil.CONTAINER_TYPE containerType, MesExtEnumUtil.CONTAINER_PART_MATCH_TYPE matchType) {
        ContainerBindManager.INSTANCE.register(containerType, matchType, this);
    }

    protected Double getRawSnWeightQty(String organizeCode, String barcode) {
        MesRawSnWeightRecord weightRecord = rawSnWeightRecordRDao.getByProperty(
                new String[]{MesPcnExtConstWords.ORGANIZE_CODE, MesPcnExtConstWords.IS_DELETED, MesPcnExtConstWords.IS_VALID, "barCode"},
                new Object[]{organizeCode, CommonEnumUtil.TRUE_OR_FALSE.FALSE.getValue(), CommonEnumUtil.IS_VAILD.VAILD.getValue(), barcode});
        if (weightRecord == null) {
            MesPcnException.throwMesBusiException("调配器条码【%s】不存在于原料重量记录表，请检查数据", barcode);
        }
        if (weightRecord.getNetWeight() == null) {
            MesPcnException.throwMesBusiException("调配器条码【%s】在原料重量记录表中的数量不能为空，请检查数据", barcode);
        }
        return weightRecord.getNetWeight();
    }
}
