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

import cn.estsh.i3plus.ext.mes.pcn.apiservice.serviceimpl.rulematch.containerbind.ContainerBindManager;
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.MesContainerPackage;
import cn.estsh.i3plus.pojo.mes.bean.MesContainerPackageDetail;
import cn.estsh.i3plus.pojo.mes.bean.MesContainerPackageDetailFeeding;
import cn.estsh.i3plus.pojo.mes.repository.MesContainerPackageDetailFeedingRepository;
import cn.estsh.i3plus.pojo.mes.util.MesExtEnumUtil;
import io.swagger.annotations.ApiOperation;
import org.springframework.beans.factory.annotation.Autowired;

import java.util.List;
import java.util.Map;

public class MesAbstractContainerBindCheckQtyService {
    @Autowired
    protected MesContainerPackageDetailFeedingRepository packageDetailFeedingRDao;

    protected MesAbstractContainerBindCheckQtyService(MesExtEnumUtil.CONTAINER_TYPE_MIX_TYPE type) {
        ContainerBindManager.INSTANCE.registerCheckQty(type, this);
    }

    @ApiOperation(value = "校验数量")
    public Map<String, Object> doCheck(String organizeCode, MesContainerPackage containerPackage, List<MesContainerPackageDetail> packageDetails, List<MesContainerPartsModel> containerParts) { return null; }

    protected double getContainerPackageQty(String organizeCode, List<MesContainerPackageDetail> packageDetails) {
        double totalQty = 0;
        for (MesContainerPackageDetail detail : packageDetails) {
            if (detail.getQty() == null) {
                continue;
            }
            totalQty += detail.getQty();

            MesContainerPackageDetailFeeding feeding = packageDetailFeedingRDao.getByProperty(new String[]{MesPcnExtConstWords.ORGANIZE_CODE, MesPcnExtConstWords.IS_DELETED, MesPcnExtConstWords.IS_VALID, "pid", "sourceId"},
                    new Object[]{organizeCode, CommonEnumUtil.TRUE_OR_FALSE.FALSE.getValue(), CommonEnumUtil.IS_VAILD.VAILD.getValue(), detail.getPid(), detail.getId()});
            if (feeding != null && feeding.getQty() != null) {
                totalQty -= feeding.getQty();
            }
        }
        return totalQty;
    }
}
