package com.jwipc.order.logic.impl;

import cn.hutool.core.bean.BeanUtil;
import cn.hutool.core.date.LocalDateTimeUtil;
import cn.hutool.core.util.StrUtil;
import com.alibaba.fastjson2.JSON;
import com.baomidou.dynamic.datasource.annotation.DS;
import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.conditions.update.LambdaUpdateWrapper;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.google.common.collect.Lists;
import com.google.common.collect.Sets;
import com.jwipc.api.constant.RestCode;
import com.jwipc.api.dto.SapResposeBody;
import com.jwipc.constant.DelFlag;
import com.jwipc.constant.FlagConst;
import com.jwipc.exception.DaoException;
import com.jwipc.order.constant.PoSapClient;
import com.jwipc.order.dto.DevPlanToSapDTO;
import com.jwipc.order.dto.PurchaseOrderDevPlanBaseDTO;
import com.jwipc.order.entity.PurchaseOrderDetail;
import com.jwipc.order.entity.PurchaseOrderDevPlan;
import com.jwipc.order.logic.PurchaseOrderPlanLogic;
import com.jwipc.order.model.po.PurchaseOrderPlanImportPO;
import com.jwipc.order.model.po.PurchaseOrderPlanPO;
import com.jwipc.order.model.query.PurchaseOrderPlanQuery;
import com.jwipc.order.service.IPurchaseOrderDetailService;
import com.jwipc.order.service.IPurchaseOrderDevPlanService;
import com.jwipc.page.PageUtil;
import com.jwipc.page.PageView;
import com.jwipc.prop.SysProp;
import com.jwipc.security.LoginUtil;
import com.jwipc.supplier.client.SupplierClient;
import com.jwipc.sys.client.ProductClient;
import com.jwipc.sys.client.UserPurchaseGroupClient;
import com.jwipc.sys.client.dto.ProductInfoDTO;
import com.jwipc.util.DateUtils;
import com.jwipc.util.EasyExcelImportUtils;
import lombok.RequiredArgsConstructor;
import lombok.extern.slf4j.Slf4j;
import org.apache.commons.lang3.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.redis.core.StringRedisTemplate;
import org.springframework.stereotype.Service;
import org.springframework.transaction.PlatformTransactionManager;
import org.springframework.transaction.annotation.Transactional;
import org.springframework.web.multipart.MultipartFile;

import java.math.BigDecimal;
import java.time.LocalDate;
import java.time.format.DateTimeFormatter;
import java.util.*;
import java.util.concurrent.TimeUnit;
import java.util.regex.Pattern;
import java.util.stream.Collectors;

import static com.baomidou.dynamic.datasource.support.DdConstants.SLAVE;
import static com.jwipc.util.DateUtils.yyyyMMdd;

/**
 * @Author: swq
 * @Date: 2024/4/1 11:22
 */
@Service
@RequiredArgsConstructor
@DS(SLAVE)
@Slf4j
public class PurchaseOrderPlanLogicImpl implements PurchaseOrderPlanLogic {
    private final IPurchaseOrderDevPlanService purchaseOrderPlanService;
    private final IPurchaseOrderDetailService purchaseOrderDetailService;
    private final PoSapClient poSapClient;
    private final SupplierClient supplierClient;
    private final UserPurchaseGroupClient userPurchaseGroupClient;
    private final ProductClient productClient;
    private final SysProp sysProp;
    private final StringRedisTemplate stringRedisTemplate;

    @Override
    public PageView getList(PurchaseOrderPlanQuery query) {
        final Page<PurchaseOrderPlanPO> page = PageUtil.toPage(query);
        if (LoginUtil.isSupplier()) {
            query.setLoginSupplierCode(LoginUtil.getSupplierCode());
            query.setSupplierVisible(FlagConst.Y.getCode());
        } else {
            final Set<String> userPurchaseGroupCode = userPurchaseGroupClient.getUserPurchaseGroupCode(
                    LoginUtil.getUserId());
            query.setUserPurchaseGroupCodes(userPurchaseGroupCode);
        }
        if (StringUtils.isNotEmpty(query.getSort()) && query.getSort().equals("purchaseGroupName") && StringUtils.isEmpty(query.getFactory())) {
//            throw new DaoException("要按采购组排序，请先选择工厂查询条件");
        }
        List<PurchaseOrderPlanPO> list = purchaseOrderPlanService.findList(page, query);
        initMaterialName(list);
        return PageUtil.getPV(list, Objects.isNull(page) ? list.size() : page.getTotal());
    }

    private void initMaterialName(List<PurchaseOrderPlanPO> list) {
        for (PurchaseOrderPlanPO po : list) {
            //设置最新的物料名称，如果物料名称有变化，则取最新的物料名称， 2024-08-07 邓京术
            ProductInfoDTO productInfoDTO = productClient.getByMaterialCodeAndFactoryCode(po.getMaterialCode(), po.getFactoryCode());
            if (Objects.nonNull(productInfoDTO)) {
                po.setMaterialName(productInfoDTO.getMaterialName());
            }
        }
    }

    @Override
    public List<PurchaseOrderPlanPO> listAll(PurchaseOrderPlanQuery query) {
        if (LoginUtil.isSupplier()) {
            query.setLoginSupplierCode(LoginUtil.getSupplierCode());
            query.setSupplierVisible(FlagConst.Y.getCode());
        } else {
            final Set<String> userPurchaseGroupCode = userPurchaseGroupClient.getUserPurchaseGroupCode(
                    LoginUtil.getUserId());
            query.setUserPurchaseGroupCodes(userPurchaseGroupCode);
        }
        //需要单独查询分页总数
        query.setRows(1);
        query.setPage(1);
        Page<PurchaseOrderPlanPO> page = PageUtil.toPage(query);
        purchaseOrderPlanService.findList(page, query);
        if (page.getTotal() > sysProp.getExportMaxCount()) {
            throw new DaoException(StrUtil.format("导出数据超过{}条", sysProp.getExportMaxCount()));
        }
        List<PurchaseOrderPlanPO> list = purchaseOrderPlanService.findList(null, query);
        initMaterialName(list);
        return list;
    }

    @Autowired
    private PlatformTransactionManager transactionManager;

    @Override
    @DS(SLAVE)
    @Transactional(rollbackFor = Exception.class)
    public Set<String> importPlan(MultipartFile file) {
        String go_published = stringRedisTemplate.opsForValue().get("orderPlan_" + LoginUtil.getUserId());
        if (null != go_published && go_published.equals("Y")) {
            throw new DaoException("数据正在导入中，请勿重复操作！");
        }
        stringRedisTemplate.opsForValue().set("orderPlan_" + LoginUtil.getUserId(), "Y", 30, TimeUnit.SECONDS);
        Set<String> msg = Sets.newHashSet();
        try {
            List<Map<String, String>> dataList = EasyExcelImportUtils.parseExcelToData(file.getInputStream(), 2);
            if (dataList.size() > 1000) {
                throw new DaoException("一次性导入最多不超过1000条!");
            }
            List<PurchaseOrderPlanImportPO> list = Lists.newArrayList();
            for (Map<String, String> stringStringMap : dataList) {
                list.add(BeanUtil.mapToBean(stringStringMap, PurchaseOrderPlanImportPO.class, false));
            }
            //过滤是否更新的数据
            //List<PurchaseOrderPlanPO> purList = list.stream().filter(t -> "Y".equals(t.getIsUpdate()) || "是".equals
            // (t.getIsUpdate())).collect(Collectors.toList());
            log.info("导入数据信息:{}", list);
            //需要做校验判断拆分数量和是否等于订单数量总和符合
            List<PurchaseOrderDevPlan> insertOrUpdateLists = Lists.newArrayList();
            List<DevPlanToSapDTO.Item> itemList = new ArrayList<>();
            // 定义日期格式化模式
            DateTimeFormatter inputFormatter = DateTimeFormatter.ofPattern("yyyy/M/d");
            DateTimeFormatter formatter = DateTimeFormatter.ofPattern("yyyy-MM-dd");
            // 定义正则表达式
            String regex1 = "\\d{4}/\\d{1,2}/\\d{1,2}";//是 yyyy/M/d 格式的日期数据"
            String regex2 = "\\d{4}-\\d{2}-\\d{2}";// 是 yyyy-MM-dd 格式的日期数据
            if (list != null && list.size() > 0) {
                for (PurchaseOrderPlanImportPO pop : list) {

                    final String orderCode = pop.getOrderCode();//
                    final String orderRow = pop.getOrderRow();
                    final String scheduleLine = pop.getScheduleLine();
                    final String s = "SAP订单号:" + orderCode + ",订单行号:" + orderRow + ",计划行号:" + scheduleLine;
                    final String ss = "SAP订单号:" + orderCode + ",订单行号:" + orderRow;
                    if (!pop.getDelFlag().equals("是") &&
                            !pop.getDelFlag().equals("否") &&
                            !pop.getDelFlag().equals("Y") &&
                            !pop.getDelFlag().equals("N")) {
                        throw new DaoException(s + "计划删除标识，只能填写是、否、Y、N!");
                    }
                    if (Objects.isNull(pop.getScheduleDeliveryDate())) {
                        throw new DaoException(s + "计划交货日期未填写!");
                    }
                    if (Objects.isNull(pop.getScheduleDeliveryQuantity())) {
                        throw new DaoException(s + "计划交货数量未填写!");
                    }
                    if (Objects.isNull(pop.getScheduleLine())) {
                        throw new DaoException(s + "计划行号未填写!");
                    }
                    //验证sap订单号，订单行号，计划单号，三个值出现同样的记录
                    List<PurchaseOrderPlanImportPO> disList = list.stream()
                            .filter(t -> t.getOrderCode().equals(orderCode) && t.getOrderRow().equals(orderRow) &&
                                    t.getScheduleLine().equals(scheduleLine)).collect(Collectors.toList());
                    if (disList != null && disList.size() > 1) {
                        throw new DaoException("不允许" + s + "三个值出现重复计划行号，请检查！");
                    }
                    String sdDate = pop.getScheduleDeliveryDate();
                    String outSdDate = "";
                    // 判断字符串格式
                    if (Pattern.matches(regex1, sdDate)) {//是 yyyy/M/d 格式的日期数据"
                        // 解析输入日期字符串
                        LocalDate date = LocalDate.parse(sdDate, inputFormatter);
                        // 格式化日期为输出格式
                        outSdDate = date.format(formatter);
                    } else if (Pattern.matches(regex2, sdDate)) {//是 yyyy-MM-dd 格式的日期数据"
                        outSdDate = sdDate;
                    }
                    if (StringUtils.isEmpty(outSdDate)) {
                        throw new DaoException(s + "计划交货日期填写有误，请按以下格式填写(YYYY-MM-DD)");
                    }
                    //过滤出sap订单号，订单行号相同的记录
                    List<PurchaseOrderPlanImportPO> sumList = list.stream()
                            .filter(t -> t.getOrderCode().equals(orderCode) && t.getOrderRow().equals(orderRow))
                            .collect(Collectors.toList());
                    List<PurchaseOrderDevPlan> pplanList = purchaseOrderPlanService.list(
                            new LambdaQueryWrapper<PurchaseOrderDevPlan>().eq(PurchaseOrderDevPlan::getPurchaseOrder,
                                    orderCode).eq(PurchaseOrderDevPlan::getLineItem, orderRow));
                    if (sumList != null && sumList.size() > 0) {
                        boolean flag = true;
                        for (PurchaseOrderPlanImportPO popp : sumList) {
                            int pdelFlag = (popp.getDelFlag().equals("否") || popp.getDelFlag().equals("N")) ? 0 : 1;
                            if (pplanList != null && pplanList.size() > 0) {
                                PurchaseOrderDevPlan pordeplan = pplanList.stream()
                                        .filter(t -> Objects.nonNull(t.getScheduleDeliveryDate()) &&
                                                t.getScheduleDeliveryDate().equals(popp.getScheduleDeliveryDate()) &&
                                                t.getScheduleDeliveryQuantity()
                                                        .compareTo(popp.getScheduleDeliveryQuantity()) == 0 &&
                                                t.getDelFlag() == pdelFlag).findFirst().orElse(null);
                                if (pordeplan == null) {
                                    flag = false;
                                }
                            }
                        }
                        //两个sumList与数据库的三个参数比较都相等的话，此条记录跳出循环
                        if (flag) {
                            continue;
                        }

                        //拆分的计划交货量总数
                        BigDecimal sum = new BigDecimal("0.00");
                        for (PurchaseOrderPlanImportPO pops : sumList) {
                            //过滤掉打删除标记的计划交货数量
                            if ("Y".equals(pops.getDelFlag()) || "是".equals(pops.getDelFlag())) {
                                continue;
                            }
                            sum = sum.add(pops.getScheduleDeliveryQuantity());
                        }
                        PurchaseOrderDetail orderDetail = purchaseOrderDetailService.getOne(
                                new LambdaQueryWrapper<PurchaseOrderDetail>().eq(PurchaseOrderDetail::getOrderCode,
                                                orderCode).eq(PurchaseOrderDetail::getOrderRow, orderRow)
                                        .eq(PurchaseOrderDetail::getDelFlag, 0));
                        if (orderDetail != null) {
                            //订单行采购量
                            BigDecimal num = orderDetail.getNum();
                            if (sum.compareTo(num) > 0 || sum.compareTo(num) < 0) {
                                throw new DaoException(ss + "的拆分的计划交货数量合计不等于订单行的采购量，请检查！");
                            }
                            DevPlanToSapDTO.Item item = new DevPlanToSapDTO.Item();
                            item.EBELN = orderCode;
                            item.EBELP = orderRow;
                            item.ETENR = scheduleLine;
                            item.ZSCBS = (pop.getDelFlag().equals("否") || pop.getDelFlag().equals("N")) ? "" : "X";
                            item.EINDT = LocalDateTimeUtil.format(LocalDateTimeUtil.parse(outSdDate, "yyyy-MM-dd"),
                                    yyyyMMdd);
                            item.MENGE = pop.getScheduleDeliveryQuantity().toString();
                            item.BANFN = orderDetail.getPurchApplyNo();
                            item.BNFPO = orderDetail.getPurchApplyRow();
                            itemList.add(item);
                        }
                    }
                    int delFlag = (pop.getDelFlag().equals("否") || pop.getDelFlag().equals("N")) ? 0 : 1;
                    PurchaseOrderDevPlan purchaseOrderDevPlan = purchaseOrderPlanService.getOne(
                            new LambdaQueryWrapper<PurchaseOrderDevPlan>().eq(PurchaseOrderDevPlan::getPurchaseOrder,
                                            orderCode).eq(PurchaseOrderDevPlan::getLineItem, orderRow)
                                    .eq(PurchaseOrderDevPlan::getScheduleLine, scheduleLine));
                    if (purchaseOrderDevPlan == null) {

                        purchaseOrderDevPlan = new PurchaseOrderDevPlan();
                        purchaseOrderDevPlan.setNoClearNum(pop.getNoClearNum());
                        purchaseOrderDevPlan.setPurchaseOrder(pop.getOrderCode());
                        purchaseOrderDevPlan.setLineItem(pop.getOrderRow());
                        purchaseOrderDevPlan.setDelFlag(delFlag);
                        purchaseOrderDevPlan.setScheduleDeliveryDate(LocalDate.parse(outSdDate, formatter));
                        purchaseOrderDevPlan.setScheduleDeliveryQuantity(pop.getScheduleDeliveryQuantity());
                        purchaseOrderDevPlan.setScheduleLine(pop.getScheduleLine());
                        purchaseOrderDevPlan.setRemark(pop.getRemark());
                        insertOrUpdateLists.add(purchaseOrderDevPlan);
                    } else {
                        //已收货数量
                        BigDecimal receivedQuantity = purchaseOrderDevPlan.getReceivedQuantity() ==
                                null ? BigDecimal.ZERO : purchaseOrderDevPlan.getReceivedQuantity();
                        //计划交货数量
                        BigDecimal scheduleDeliveryQuantity = pop.getScheduleDeliveryQuantity();
//                        if (scheduleDeliveryQuantity.compareTo(receivedQuantity) < 0) {
//                            throw new DaoException(s + "的计划交货数量:" + scheduleDeliveryQuantity +
//                                    "不允许比系统中计划行中的已收货数量:" + receivedQuantity + "小，请检查！");
//                        }
                        //收货数量不是空的都不能打删除标记10
                        if (purchaseOrderDevPlan.getReceivedQuantity() != null &&
                                purchaseOrderDevPlan.getReceivedQuantity().compareTo(BigDecimal.ZERO) > 0 &&
                                delFlag == 1) {
                            throw new DaoException(
                                    s + "的记录有已收货数量:" + purchaseOrderDevPlan.getReceivedQuantity() +
                                            ",计划删除标识不允许填是或者Y，请检查！");
                        } else {
                            purchaseOrderDevPlan.setDelFlag(delFlag);
                        }
                        purchaseOrderDevPlan.setNoClearNum(pop.getNoClearNum());
                        purchaseOrderDevPlan.setScheduleDeliveryDate(LocalDate.parse(outSdDate, formatter));
                        purchaseOrderDevPlan.setScheduleDeliveryQuantity(pop.getScheduleDeliveryQuantity());
                        purchaseOrderDevPlan.setScheduleLine(pop.getScheduleLine());
                        purchaseOrderDevPlan.setRemark(pop.getRemark());
                        insertOrUpdateLists.add(purchaseOrderDevPlan);
                    }

                }
            } else {
                throw new DaoException("导入的记录没有要更新的数据，是否更新列请填写是");
            }
            processBatch(insertOrUpdateLists, itemList);
        } catch (Exception e) {
            e.printStackTrace();
            throw new DaoException(e.getMessage());
        }
        stringRedisTemplate.opsForValue().set("orderPlan_" + LoginUtil.getUserId(), "N", 30, TimeUnit.SECONDS);
        return msg;
    }

    @DS(SLAVE)
    public void processBatch(List<PurchaseOrderDevPlan> purchaseOrderDevPlans, List<DevPlanToSapDTO.Item> itemList) {

        //判断输入的日期是否在当前日期3年之前
        final LocalDate now = LocalDate.now();
        //final LocalDate localDate = now.plusYears(3);
        final LocalDate localDate = now.plusDays(999) ;//now.plusYears(3);
        //取当前日期 和当前年份  2023-10-17
        int currentYear = LocalDate.now().getYear();//当前年
        for (PurchaseOrderDevPlan purchaseOrderDevPlan : purchaseOrderDevPlans) {
            final String orderCode = purchaseOrderDevPlan.getPurchaseOrder();//
            final String orderRow = purchaseOrderDevPlan.getLineItem();
            final String scheduleLine = purchaseOrderDevPlan.getScheduleLine();
            final String s = "SAP订单号:" + orderCode + ",订单行号:" + orderRow + ",计划行号:" + scheduleLine;

            if (purchaseOrderDevPlan.getScheduleDeliveryDate().isAfter(localDate)) {
                throw new DaoException(StrUtil.format("单号:{},行号:{},计划交货日期不能在当前日期3年之后",
                        purchaseOrderDevPlan.getPurchaseOrder(), purchaseOrderDevPlan.getLineItem()));
            }
            LocalDate specificDate = LocalDate.of(currentYear, 1, 2);//当年1月2号
            if(!purchaseOrderDevPlan.getScheduleDeliveryDate().equals(specificDate)){
                if(purchaseOrderDevPlan.getScheduleDeliveryDate().isBefore(LocalDate.now())){
                    if(purchaseOrderDevPlan.getNoClearNum() !=null && purchaseOrderDevPlan.getNoClearNum().compareTo(BigDecimal.ZERO) > 0){
                            if(!purchaseOrderDevPlan.getScheduleDeliveryDate().equals(specificDate)){
                                throw new DaoException(StrUtil.format("单号:{},行号:{},计划交货日期不能在当前日期之前",
                                        purchaseOrderDevPlan.getPurchaseOrder(), purchaseOrderDevPlan.getLineItem()));
                            }

                    }
                }

            }
            List<PurchaseOrderDevPlan> disList = purchaseOrderDevPlans.stream()
                    .filter(t -> t.getPurchaseOrder().equals(orderCode) && t.getLineItem().equals(orderRow) &&
                            t.getScheduleLine().equals(scheduleLine)).collect(Collectors.toList());
            if (disList != null && disList.size() > 1) {
                throw new DaoException("不允许" + s + "三个值出现重复计划行号，请检查！");
            }
        }
        //调用sap跟新计划行接口
        DevPlanToSapDTO sapDTO = new DevPlanToSapDTO();
        DevPlanToSapDTO.ItHead itHead = new DevPlanToSapDTO.ItHead();
        DevPlanToSapDTO.ItItem itItem = new DevPlanToSapDTO.ItItem();
        Set<String> cache = Sets.newHashSet();
        List<DevPlanToSapDTO.Item> head = Lists.newArrayList();
        for (DevPlanToSapDTO.Item item : itemList) {
            final String key = item.EBELN + item.EBELP;
            if (!cache.contains(key)) {
                cache.add(key);
                DevPlanToSapDTO.Item a = new DevPlanToSapDTO.Item();
                a.EBELN = item.EBELN;
                a.EBELP = item.EBELP;
                head.add(a);
            }
        }

        itHead.item = head;
        itItem.item = itemList;
        sapDTO.setIT_HEAD(itHead);
        sapDTO.setIT_ITEM(itItem);
        log.info("交货计划明细推送sap接口参数{}", JSON.toJSONString(sapDTO));
        //需要分批推送sap
        final SapResposeBody sapResposeBody = poSapClient.updateDevPlan(sapDTO);
        log.info("交货计划明细推送sap接口返回结果{}", sapResposeBody);
        if (sapResposeBody.getES_RET().getCODE().equals(RestCode.SAP_ERROR.getCode())) {
            throw new DaoException(
                    StrUtil.format("SAP返回消息:{}", sapResposeBody.getES_RET().getMSG()));
        } else {
            purchaseOrderPlanService.saveOrUpdateBatch(purchaseOrderDevPlans, 300);
            updatePurchaseDetailReplyDate(purchaseOrderDevPlans);
        }

    }

    private void updatePurchaseDetailReplyDate(List<PurchaseOrderDevPlan> insertOrUpdateLists) {
        //修改订单明细的回复交期
        final Map<String, List<PurchaseOrderDevPlan>> collect = insertOrUpdateLists.stream()
                .filter(a -> a.getDelFlag() == FlagConst.N.getCode())
                .collect(Collectors.groupingBy(PurchaseOrderDevPlan::getPurchaseOrder));
        for (String s : collect.keySet()) {
            final List<PurchaseOrderDevPlan> purchaseOrderDevPlans = collect.get(s);
            final Map<String, List<PurchaseOrderDevPlan>> collect1 = purchaseOrderDevPlans.stream()
                    .collect(Collectors.groupingBy(PurchaseOrderDevPlan::getLineItem));
            for (String string : collect1.keySet()) {
                final List<PurchaseOrderDevPlan> purchaseOrderDevPlans1 = collect1.get(string);
                //2024-12-02 qzc 增加交货计划行删除排除逻辑和已完成交货数量的
                final Optional<PurchaseOrderDevPlan> max = purchaseOrderDevPlans1.stream().filter(a -> (a.getDelFlag() == DelFlag.NORMAL.getStatus()) && (a.getScheduleDeliveryQuantity().compareTo(a.getReceivedQuantity() == null ? BigDecimal.ZERO:a.getReceivedQuantity()) != 0))
                        .max(Comparator.comparing(PurchaseOrderDevPlan::getScheduleDeliveryDate));
                if (max.isPresent()) {
                    final PurchaseOrderDevPlan planBaseDTO = max.get();
                    final LocalDate scheduleDeliveryDate = planBaseDTO.getScheduleDeliveryDate();
                    final String purchaseOrder = planBaseDTO.getPurchaseOrder();
                    final String orderRow = planBaseDTO.getLineItem();
                    purchaseOrderDetailService.update(new LambdaUpdateWrapper<>(PurchaseOrderDetail.class)
                            .set(PurchaseOrderDetail::getReplayRequireDate, scheduleDeliveryDate)
                            .eq(PurchaseOrderDetail::getOrderCode, purchaseOrder)
                            .eq(PurchaseOrderDetail::getOrderRow, orderRow));
                }
            }
        }
    }

    @Override
    @Transactional(rollbackFor = Exception.class)
    @DS(SLAVE)
    public void saveOrUpdate(List<PurchaseOrderDevPlanBaseDTO> param) {
        //需要修改计划交货日期
        Set<String> a = Sets.newHashSet();
        final String s = "&&&&";

        Map<Long, PurchaseOrderDevPlanBaseDTO> paramMap = new HashMap<>();
        for (PurchaseOrderDevPlanBaseDTO planBaseDTO : param) {
            purchaseOrderPlanService.update(new LambdaUpdateWrapper<>(PurchaseOrderDevPlan.class)
                    .set(PurchaseOrderDevPlan::getScheduleDeliveryDate, planBaseDTO.getScheduleDeliveryDate())
                    .eq(PurchaseOrderDevPlan::getId, planBaseDTO.getId()));

            a.add(planBaseDTO.getPurchaseOrder() + s + planBaseDTO.getLineItem());
            if(planBaseDTO.getId() != null){
                paramMap.put(planBaseDTO.getId(),planBaseDTO);
            }

        }
        List<PurchaseOrderDevPlan> list = Lists.newArrayList();
        for (String string : a) {
            final String[] split = string.split(s);
            final List<PurchaseOrderDevPlan> db = purchaseOrderPlanService.list(
                    new LambdaQueryWrapper<PurchaseOrderDevPlan>()
                            .eq(PurchaseOrderDevPlan::getPurchaseOrder, split[0])
                            .eq(PurchaseOrderDevPlan::getLineItem, split[1])
                            .eq(PurchaseOrderDevPlan::getDelFlag,0));
            list.addAll(db);
        }
        List<DevPlanToSapDTO.Item> itemList = Lists.newArrayList();
        for (PurchaseOrderDevPlan purchaseOrderDevPlan : list) {
            PurchaseOrderDetail orderDetail = purchaseOrderDetailService.getOne(
                    new LambdaQueryWrapper<PurchaseOrderDetail>().eq(PurchaseOrderDetail::getOrderCode,
                                    purchaseOrderDevPlan.getPurchaseOrder())
                            .eq(PurchaseOrderDetail::getOrderRow, purchaseOrderDevPlan.getLineItem())
                            .eq(PurchaseOrderDetail::getDelFlag, 0));
            if(purchaseOrderDevPlan.getId() != null){
                PurchaseOrderDevPlanBaseDTO purchaseOrderDevPlanBaseDTO = paramMap.get(purchaseOrderDevPlan.getId());
                if(purchaseOrderDevPlanBaseDTO != null){
                    purchaseOrderDevPlan.setNoClearNum(purchaseOrderDevPlanBaseDTO.getNoClearNum());
                }
            }
            if (orderDetail != null) {
                DevPlanToSapDTO.Item item = new DevPlanToSapDTO.Item();
                item.EBELN = orderDetail.getOrderCode();
                item.EBELP = orderDetail.getOrderRow();
                item.ETENR = purchaseOrderDevPlan.getScheduleLine();
                item.ZSCBS = StrUtil.EMPTY;
                item.EINDT = DateUtils.formatYYYYMMDD(purchaseOrderDevPlan.getScheduleDeliveryDate());
                item.MENGE = purchaseOrderDevPlan.getScheduleDeliveryQuantity().toString();
                item.BANFN = orderDetail.getPurchApplyNo();
                item.BNFPO = orderDetail.getPurchApplyRow();
                itemList.add(item);
            } else {
                throw new DaoException("订单数据不存在!");
            }

        }
        processBatch(list, itemList);
    }

    public static void main(String[] args) {
        PurchaseOrderDevPlan purchaseOrderDevPlan = new PurchaseOrderDevPlan();
        purchaseOrderDevPlan.setScheduleDeliveryDate(LocalDate.of(2025, 1, 2));
        System.out.println(purchaseOrderDevPlan.getScheduleDeliveryDate());
        int currentYear = LocalDate.now().getYear();//当前年
        LocalDate specificDate = LocalDate.of(currentYear, 1, 2);//当年1月2号
        if(specificDate.equals(purchaseOrderDevPlan.getScheduleDeliveryDate())){
            System.out.println("时间相同");
        }

    }
}
