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

import cn.estsh.i3plus.ext.mes.pcn.api.busi.IMesConfigService;
import cn.estsh.i3plus.ext.mes.pcn.api.busi.IMesJisShippingService;
import cn.estsh.i3plus.ext.mes.pcn.api.busi.IMesPartShippingGroupService;
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.enumutil.CommonEnumUtil;
import cn.estsh.i3plus.pojo.mes.bean.MesConfig;
import cn.estsh.i3plus.pojo.mes.bean.MesJisShipping;
import cn.estsh.i3plus.pojo.mes.bean.MesPartShippingGroup;
import cn.estsh.i3plus.pojo.mes.bean.MesWorkOrderPart;
import cn.estsh.i3plus.pojo.mes.bean.shipping.MesShippingOrderManagement;
import cn.estsh.i3plus.pojo.mes.bean.shipping.MesShippingOrderManagementDetail;
import cn.estsh.i3plus.pojo.mes.util.MesExtEnumUtil;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.util.CollectionUtils;
import org.springframework.util.StringUtils;

import java.util.ArrayList;
import java.util.List;
import java.util.Objects;
import java.util.UUID;
import java.util.stream.Collectors;

/**
 * @Description :
 * @Reference :
 * @Author :
 * @CreateDate : 2024-05-10 14:38
 * @Modify:
 **/
@Service
@Slf4j
public class MesJisShippingServiceImpl implements IMesJisShippingService {

    @Autowired
    private IMesPartShippingGroupService mesPartShippingGroupService;

    @Autowired
    private IMesConfigService configService;

    @Override
    public MesJisShipping getMesJisShipping(MesShippingOrderManagement orderManagement,MesShippingOrderManagementDetail detail, String srcLocateNo, MesWorkOrderPart orderPart, String userName) {
        MesJisShipping jisShipping = new MesJisShipping();
        BeanUtils.copyProperties(orderManagement, jisShipping, MesPcnExtConstWords.BASE_BEAN_FIELDS);
        BeanUtils.copyProperties(detail, jisShipping, MesPcnExtConstWords.BASE_BEAN_FIELDS);
        jisShipping.setFactoryCode(orderManagement.getCustOrganizeCode());
        jisShipping.setShippingTime(TimeTool.getNowTime(true));
        jisShipping.setErpWarerhouse(srcLocateNo);
        jisShipping.setErpWarehous(srcLocateNo);
        jisShipping.setDock(orderManagement.getSendDockCode());
        jisShipping.setCusCode(orderManagement.getCustCode());
        jisShipping.setCusDock(orderManagement.getCustDockCode());
        jisShipping.setCustOrderNo(detail.getCustOrderNo());
        // 子零件信息赋值
        jisShipping.setPartNo(orderPart.getPartNo());
        jisShipping.setUnit(orderPart.getItemUnit());
        // 20241115 没有给carNo赋值
        jisShipping.setCarNo(detail.getVin());
        jisShipping.setSerialNumber(orderPart.getWorkOrderNo());
        jisShipping.setQuantityPerCar(orderPart.getItemQty());
        jisShipping.setOffTime(TimeTool.getNowTime(true));
        jisShipping.setShippingOrderCode(orderManagement.getShippingCode());
        jisShipping.setFid(UUID.randomUUID().toString());
        jisShipping.setSystemSyncStatus(CommonEnumUtil.FALSE);
        ConvertBean.serviceModelInitialize(jisShipping, userName);
        return jisShipping;
    }

    @Override
    public List<MesJisShipping> getJisShipping(MesShippingOrderManagement orderManagement, List<MesShippingOrderManagementDetail> orderManagementDetailList,String userName) {
        orderManagementDetailList = CollectionUtils.isEmpty(orderManagementDetailList) ? orderManagementDetailList : orderManagementDetailList.stream().filter(t -> CommonEnumUtil.TRUE_OR_FALSE.TRUE.getValue() == t.getSaveFlag()).collect(Collectors.toList());
        if (CollectionUtils.isEmpty(orderManagementDetailList)) return null;
        MesPartShippingGroup mesPartShippingGroup = mesPartShippingGroupService.getMesPartShippingGroup(orderManagement.getOrganizeCode(), orderManagement.getShippingGroupCode());
        String warerhouse = mesPartShippingGroup == null || StringUtils.isEmpty(mesPartShippingGroup.getSrcWarerhouse()) ? configService.getCfgValue(orderManagement.getOrganizeCode(), MesPcnExtConstWords.ERP_WARERHOUSE) : mesPartShippingGroup.getSrcWarerhouse();
        String erpWarehous = configService.getCfgValue(orderManagement.getOrganizeCode(), MesPcnExtConstWords.ERP_WAREHOUS);
        MesConfig useCustOrderNo = configService.getMesConfigNoError(orderManagement.getOrganizeCode(), MesPcnExtConstWords.USE_CUST_ORDER_NO);
        List<MesJisShipping> jisShippingList = new ArrayList<>();
        orderManagementDetailList.forEach(k -> {
            if (k.getStatus() == MesExtEnumUtil.SHIPPING_ORDER_DETAIL_SHIPPING_STATUS.SKIP.getValue()) {
                return;
            }
            MesJisShipping jisShipping = new MesJisShipping();
            BeanUtils.copyProperties(orderManagement, jisShipping, MesPcnExtConstWords.BASE_BEAN_FIELDS);
            BeanUtils.copyProperties(k, jisShipping, MesPcnExtConstWords.BASE_BEAN_FIELDS);
            jisShipping.setFactoryCode(orderManagement.getCustOrganizeCode());
            jisShipping.setShippingTime(TimeTool.getNowTime(true));
            jisShipping.setErpWarerhouse(warerhouse);
            jisShipping.setErpWarehous(erpWarehous);
            jisShipping.setDock(orderManagement.getSendDockCode());
            jisShipping.setCusCode(orderManagement.getCustCode());
            jisShipping.setCusDock(orderManagement.getCustDockCode());
            jisShipping.setCustOrderNo(Objects.isNull(useCustOrderNo) || Integer.parseInt(useCustOrderNo.getCfgValue()) == CommonEnumUtil.FALSE ? orderManagement.getOrderCode() : k.getCustOrderNo());
            // 20241115 没有给carNo赋值
            jisShipping.setCarNo(k.getVin());
            jisShipping.setSerialNumber(k.getBarcode());
            jisShipping.setQuantityPerCar(1d);
            jisShipping.setOffTime(TimeTool.getNowTime(true));
            jisShipping.setShippingOrderCode(orderManagement.getShippingCode());
            jisShipping.setFid(UUID.randomUUID().toString());
            jisShipping.setSystemSyncStatus(CommonEnumUtil.FALSE);
            ConvertBean.serviceModelInitialize(jisShipping, userName);
            jisShippingList.add(jisShipping);
        });

        return jisShippingList;
    }
}
