package cn.estsh.i3plus.ext.mes.pcn.apiservice.serviceimpl.busi;

import cn.estsh.i3plus.ext.mes.pcn.api.base.IMesPartService;
import cn.estsh.i3plus.ext.mes.pcn.api.busi.IMesRawSnWeightRecordService;
import cn.estsh.i3plus.ext.mes.pcn.pojo.context.MesProdRuleContext;
import cn.estsh.i3plus.ext.mes.pcn.pojo.context.MesProductionAssemblyContext;
import cn.estsh.i3plus.ext.mes.pcn.pojo.context.MesProductionAssemblyNosortContext;
import cn.estsh.i3plus.ext.mes.pcn.pojo.context.MesProductionPsOutContext;
import cn.estsh.i3plus.platform.common.convert.ConvertBean;
import cn.estsh.i3plus.pojo.base.bean.DdlPackBean;
import cn.estsh.i3plus.pojo.base.enumutil.CommonEnumUtil;
import cn.estsh.i3plus.pojo.base.tool.DdlPreparedPack;
import cn.estsh.i3plus.pojo.mes.bean.MesPart;
import cn.estsh.i3plus.pojo.mes.bean.MesRawMixCfgDetail;
import cn.estsh.i3plus.pojo.mes.bean.MesRawSnWeightRecord;
import cn.estsh.i3plus.pojo.mes.bean.MesRawWeightCfg;
import cn.estsh.i3plus.pojo.mes.repository.MesRawMixCfgDetailRepository;
import cn.estsh.i3plus.pojo.mes.repository.MesRawSnWeightRecordRepository;
import cn.estsh.i3plus.pojo.mes.repository.MesRawWeightCfgRepository;
import cn.estsh.i3plus.pojo.mes.util.MesExtEnumUtil;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.List;
import java.util.Objects;

@Service
public class MesRawSnWeightRecordServiceImpl implements IMesRawSnWeightRecordService {
    @Autowired
    private MesRawSnWeightRecordRepository rawSnWeightRecordRDao;
    @Autowired
    private MesRawWeightCfgRepository rawWeightCfgRDao;
    @Autowired
    private MesRawMixCfgDetailRepository rawMixCfgDetailRDao;
    @Autowired
    private IMesPartService partService;

    @Override
    public void insertRawRemainWeightRecord(MesProductionAssemblyContext context, String organizeCode, String username) {
        MesRawMixCfgDetail detail = rawMixCfgDetailRDao.getByProperty(
                new String[]{"organizeCode", "isValid", "isDeleted", "outPartNo", "partNo"},
                new Object[]{organizeCode, CommonEnumUtil.IS_VAILD.VAILD.getValue(), CommonEnumUtil.IS_DEAL.NO.getValue(), context.getPartNo(), context.getAssemblyPartNo()});
        if (detail == null) {
            return;
        }

        MesRawWeightCfg weightCfg = rawWeightCfgRDao.getByProperty(new String[]{"organizeCode", "isValid", "isDeleted", "partNo"},
                new Object[]{organizeCode, CommonEnumUtil.IS_VAILD.VAILD.getValue(), CommonEnumUtil.IS_DEAL.NO.getValue(), context.getAssemblyPartNo()});
        if (weightCfg == null) {
            return;
        }
        double remainQty = getRemainQty(organizeCode, context.getAssemblyPartNo());

        MesRawSnWeightRecord record = new MesRawSnWeightRecord();
        record.setOrganizeCode(organizeCode);
        record.setBarCode(context.getAssemblySn());
        record.setPartNo(context.getAssemblyPartNo());
        record.setPartName(context.getAssemblyPartName());
        record.setNetWeight(remainQty - context.getQty());
        record.setNetUnit(weightCfg.getNetUnit());
        record.setRawType(detail.getRawType());
        ConvertBean.serviceModelInitialize(record, username);
        rawSnWeightRecordRDao.insert(record);
    }

    @Override
    public void insertOutPartNoWeightRecord(MesProductionPsOutContext productionPsOutContext, List<MesProductionAssemblyNosortContext> productionAssemblyNosortContextList, String organizeCode, String username) {
        MesPart mesPart = partService.getMesPartByPartNo(productionPsOutContext.getPartNo(), organizeCode);
        if (mesPart == null) {
            return;
        }
        double totalWeight = 0.0;
        for (MesProductionAssemblyNosortContext context : productionAssemblyNosortContextList) {
            if (Objects.equals(context.getMatchType(), MesExtEnumUtil.ASSEMBLY_MATCH_TYPE.MATCH_TYPE_90.getValue())) {
                totalWeight += context.getQty();
            }
        }

        MesRawSnWeightRecord record = new MesRawSnWeightRecord();
        record.setOrganizeCode(organizeCode);
        record.setBarCode(productionPsOutContext.getProductSn());
        record.setPartNo(mesPart.getPartNo());
        record.setPartName(mesPart.getPartName());
        record.setNetWeight(totalWeight);
        record.setNetUnit(mesPart.getWeightUom());
        record.setRawType(MesExtEnumUtil.RAW_MIX_DETAIL_TYPE.RAW_MIX_DETAIL_TYPE_10.getValue());
        ConvertBean.serviceModelInitialize(record, username);
        rawSnWeightRecordRDao.insert(record);
    }

    private double getRemainQty(String organizeCode, String partNo) {
        DdlPackBean ddlPackBean = DdlPackBean.getDdlPackBean(organizeCode);
        DdlPreparedPack.getStringEqualPack(partNo, "partNo", ddlPackBean);
        DdlPreparedPack.getOrderBy("createDatetime", CommonEnumUtil.ASC_OR_DESC.DESC.getValue(), ddlPackBean);
        MesRawSnWeightRecord record = rawSnWeightRecordRDao.getByProperty(ddlPackBean);
        if (record != null) {
            return record.getNetWeight();
        }

        MesRawWeightCfg weightCfg = rawWeightCfgRDao.getByProperty(new String[]{"organizeCode", "isValid", "isDeleted", "partNo"},
                new Object[]{organizeCode, CommonEnumUtil.IS_VAILD.VAILD.getValue(), CommonEnumUtil.IS_DEAL.NO.getValue(), partNo});
        if (weightCfg == null) {
            return 0.0;
        }
        return weightCfg.getNetWeight();
    }
}
