package cn.estsh.i3plus.ext.mes.pcn.apiservice.serviceimpl.print.sortshipping.strategy;

import cn.estsh.i3plus.ext.mes.pcn.api.base.IMesPartPackageTypeService;
import cn.estsh.i3plus.ext.mes.pcn.api.busi.IMesPartShippingGroupService;
import cn.estsh.i3plus.ext.mes.pcn.apiservice.serviceimpl.print.IPrintTemplateStrategyService;
import cn.estsh.i3plus.ext.mes.pcn.pojo.util.MesPcnExtConstWords;
import cn.estsh.i3plus.platform.common.convert.ConvertBean;
import cn.estsh.i3plus.platform.common.tool.TimeTool;
import cn.estsh.i3plus.pojo.base.bean.DdlPackBean;
import cn.estsh.i3plus.pojo.base.enumutil.ResourceEnumUtil;
import cn.estsh.i3plus.pojo.base.tool.DdlPreparedPack;
import cn.estsh.i3plus.pojo.base.util.StringUtil;
import cn.estsh.i3plus.pojo.mes.bean.MesPart;
import cn.estsh.i3plus.pojo.mes.bean.MesPartPackageType;
import cn.estsh.i3plus.pojo.mes.bean.MesPartShippingGroup;
import cn.estsh.i3plus.pojo.mes.bean.edi.cd.MesCimVolvoJisRackId;
import cn.estsh.i3plus.pojo.mes.bean.edi.cd.MesCimVolvoJisRackIdDetail;
import cn.estsh.i3plus.pojo.mes.bean.shipping.MesShippingOrderManagement;
import cn.estsh.i3plus.pojo.mes.bean.shipping.MesShippingOrderManagementDetail;
import cn.estsh.i3plus.pojo.mes.model.ChengDuVolvoShippingPrintModel;
import cn.estsh.i3plus.pojo.mes.repository.MesPartRepository;
import cn.estsh.i3plus.pojo.mes.repository.MesShippingOrderManagementDetailRepository;
import cn.estsh.i3plus.pojo.mes.repository.edi.cd.MesCimVolvoJisRackIdDetailRepository;
import cn.estsh.i3plus.pojo.mes.repository.edi.cd.MesCimVolvoJisRackIdRepository;
import cn.estsh.impp.framework.boot.auth.AuthUtil;
import cn.estsh.impp.framework.boot.util.ResultBean;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Component;
import org.springframework.util.CollectionUtils;
import org.springframework.util.StringUtils;

import java.util.ArrayList;
import java.util.List;

/**
 * @Description : 成都volvo发运单打印策略
 * @Reference :
 * @Author : jason.niu
 * @CreateDate : 2025/03/20 16:43
 * @Modify:
 **/
@Component
@Slf4j
public class ChengDuVolvoShippingPrintStrategyService extends SortShippingDispatchStrategyService implements IPrintTemplateStrategyService {
    @Autowired
    private MesShippingOrderManagementDetailRepository shippingOrderDetailRDao;
    @Autowired
    private MesCimVolvoJisRackIdRepository rackIdRDao;
    @Autowired
    private MesCimVolvoJisRackIdDetailRepository rackIdDetailRDao;
    @Autowired
    private MesPartRepository partRDao;
    @Autowired
    private IMesPartShippingGroupService shippingGroupService;
    @Autowired
    private IMesPartPackageTypeService partPackageTypeService;

    @Override
    public List<MesShippingOrderManagement> getShippingOrderManagementList(MesShippingOrderManagement bean) {
        return super.getShippingOrderManagementList(bean);
    }

    @Override
    public List<MesShippingOrderManagementDetail> getShippingOrderManagementDetailList(String organizeCode, List<Long> idList, Object[] obj, String[] str) {
        return super.getShippingOrderManagementDetailList(organizeCode, idList, obj, str);
    }

    @Override
    public void saveShippingOrderManagementList(List<MesShippingOrderManagement> shippingOrderManagementList) {
        super.saveShippingOrderManagementList(shippingOrderManagementList);
    }

    @Override
    public ResultBean execute(MesShippingOrderManagement bean, List<MesShippingOrderManagement> shippingOrderManagementList) {
        List<ChengDuVolvoShippingPrintModel> modelList = new ArrayList<>();
        for (MesShippingOrderManagement shippingOrder : shippingOrderManagementList) {
            ChengDuVolvoShippingPrintModel model = getPrintData(shippingOrder);
            if (model != null) {
                model.setShippingCode(shippingOrder.getShippingCode());
                shippingOrder.setPrintCount(StringUtil.isEmpty(shippingOrder.getPrintCount()) ? MesPcnExtConstWords.ONE : shippingOrder.getPrintCount() + MesPcnExtConstWords.ONE);
                shippingOrder.setLastPrintTime(TimeTool.getNowTime(true));
                shippingOrder.setLastPrintUser(bean.getUserInfo());
                ConvertBean.serviceModelUpdate(shippingOrder, bean.getUserInfo());

                modelList.add(model);
            }
        }
        return ResultBean.success("发运单打印成功").setCode(ResourceEnumUtil.MESSAGE.SUCCESS.getCode()).setResultList(modelList);
    }

    private ChengDuVolvoShippingPrintModel getPrintData(MesShippingOrderManagement shippingOrder) {
        final String organizeCode = AuthUtil.getOrganize().getOrganizeCode();
        final String userName = AuthUtil.getSessionUser().getUserName();

        ChengDuVolvoShippingPrintModel model = new ChengDuVolvoShippingPrintModel();
        DdlPackBean shippingDetailPackBean = DdlPackBean.getDdlPackBean(organizeCode);
        DdlPreparedPack.getNumEqualPack(shippingOrder.getId(), "pid", shippingDetailPackBean);
        List<MesShippingOrderManagementDetail> shippingDetails = shippingOrderDetailRDao.findByHqlWhere(shippingDetailPackBean);
        if (CollectionUtils.isEmpty(shippingDetails)) {
            return null;
        }

        List<String> partNos = new ArrayList<>();
        List<Long> rackDetailIdList = new ArrayList<>();
        for (MesShippingOrderManagementDetail shippingDetail : shippingDetails) {
            if (shippingDetail.getSourceId() != null) {
                rackDetailIdList.add(shippingDetail.getSourceId());
            }
            if (!partNos.contains(shippingDetail.getPartNo())) {
                partNos.add(shippingDetail.getPartNo());
            }
        }
        DdlPackBean rackDetailPackBean = DdlPackBean.getDdlPackBean(organizeCode);
        DdlPreparedPack.getInPackList(rackDetailIdList, "id", rackDetailPackBean);
        List<MesCimVolvoJisRackIdDetail> rackIdDetails = rackIdDetailRDao.findByHqlWhere(rackDetailPackBean);
        if (CollectionUtils.isEmpty(rackIdDetails)) {
            return null;
        }

        List<Long> rackIdList = new ArrayList<>();
        for (MesCimVolvoJisRackIdDetail rackIdDetail : rackIdDetails) {
            if (rackIdDetail.getJisRackIdFid() != null && !rackIdList.contains(rackIdDetail.getJisRackIdFid())) {
                rackIdList.add(rackIdDetail.getJisRackIdFid());
            }
        }
        DdlPackBean rackPackBean = DdlPackBean.getDdlPackBean(organizeCode);
        DdlPreparedPack.getInPackList(rackIdList, "id", rackPackBean);
        List<MesCimVolvoJisRackId> rackIds = rackIdRDao.findByHqlWhere(rackPackBean);
        if (CollectionUtils.isEmpty(rackIds)) {
            return null;
        }

        // 净重
        double totalNetWeight = 0.0;
        // 净重单位
        String netWom = "";
        // 包装重量
        double packageWeight = 0.0;
        // 包装重量单位
        String tmpGwUom = "";
        DdlPackBean partPackBean = DdlPackBean.getDdlPackBean(organizeCode);
        DdlPreparedPack.getInPackList(partNos, "partNo", rackPackBean);
        List<MesPart> parts = partRDao.findByHqlWhere(partPackBean);
        if (!CollectionUtils.isEmpty(parts)) {
            for (MesPart mesPart : parts) {
                if (mesPart.getNetWeight() != null) {
                    totalNetWeight += mesPart.getNetWeight();
                }
                netWom = mesPart.getWeightUom();
            }
        }
        MesPartShippingGroup shippingGroup = shippingGroupService.getMesPartShippingGroup(organizeCode, shippingOrder.getShippingGroupCode());
        if (shippingGroup != null && !StringUtils.isEmpty(shippingGroup.getPackageTypeCode()) && (shippingGroup.getPackageTypeCode().startsWith("P") || shippingGroup.getPackageTypeCode().startsWith("p"))) {
            MesPartPackageType partPackageType = partPackageTypeService.getMesPartPackageType(organizeCode, shippingGroup.getPackageTypeCode());
            if (partPackageType != null) {
                packageWeight = partPackageType.getNetWeight() != null ? partPackageType.getNetWeight() : 0;
                tmpGwUom = partPackageType.getWeightUom();
            }
        }

        MesCimVolvoJisRackId rackId = rackIds.get(0);
        model.setRecelver(rackId.getShipToId());
        model.setDock(rackId.getIdForPlaceOfDischarge());
        model.setAdviceNoteNo(rackId.getAsnNo());
        model.setAdviceNoteBarCode(rackId.getAsnNo());
        model.setSupplierAddress(StringUtils.isEmpty(rackId.getShipFrom()) ? rackId.getShipFromId() : rackId.getShipFrom());
        model.setNetWeight(String.format("%.2f", totalNetWeight));
        model.setNWUom(netWom);
        model.setGrossWeight(String.format("%.2f", totalNetWeight + packageWeight));
        model.setGWUom(tmpGwUom);
        model.setRackId(rackId.getJisRackId());
        model.setRackBarCode(rackId.getJisRackId());
        model.setFirstPreNo(rackId.getFirstSequenceNumber());
        model.setFirstPreBarCode(rackId.getFirstSequenceNumber());
        model.setLastPreNo(rackId.getLastSequenceNumber());
        model.setLastPreBarCode(rackId.getLastSequenceNumber());
        model.setSupplierID(rackId.getSellerId());
        model.setSupplierBarCode(rackId.getSellerId());
        model.setCountryOfOrigin("CN");
        model.setNoofCars(rackDetailIdList.size());
        model.setDescription(rackId.getRackReference());
        model.setPackageId(rackId.getJisRackId());
        model.setPackageBarCode(rackId.getJisRackId());
        model.setProcess("LDJIS");

        return model;
    }
}
