package com.rongwei.bscommon.sys.service.impl;

import cn.hutool.core.bean.BeanUtil;
import cn.hutool.core.util.ObjectUtil;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.rongwei.bscommon.sys.dao.ApsProductDetailDao;
import com.rongwei.bscommon.sys.service.*;
import com.rongwei.bsentity.domain.*;
import com.rongwei.bsentity.vo.ApsProcessOutputProductAndBlankStatusVo;
import com.rongwei.bsentity.vo.UpdateProductDetailsAndBlankOutputReq;
import com.rongwei.rwadmincommon.system.vo.SysUserVo;
import com.rongwei.rwcommon.base.R;
import com.rongwei.rwcommon.utils.SecurityUtil;
import com.rongwei.rwcommon.utils.StringUtils;
import com.rongwei.safecommon.utils.CXCommonUtils;
import com.rongwei.safecommon.utils.SaveConstans;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;
import org.springframework.web.bind.annotation.RequestBody;

import java.util.*;
import java.util.stream.Collectors;

/**
 * <p>
 * 生产订单_产品明细 服务实现类
 * </p>
 *
 * @author sc
 * @since 2024-03-28
 */
@Service
public class ApsProductDetailServiceImpl extends ServiceImpl<ApsProductDetailDao, ApsProductDetailDo> implements ApsProductDetailService {

    @Autowired
    private SlaveSourceService slaveSourceService;

    @Autowired
    private ApsProductionOrderService apsProductionOrderService;

    @Autowired
    private ApsProductionMergeOrderService apsProductionMergeOrderService;

    @Autowired
    private ApsProductDetailService apsProductDetailService;

    @Autowired
    private ApsProductDetailDao apsProductDetailDao;

    @Autowired
    private ApsProcessOutputProductService apsProcessOutputProductService;

    @Autowired
    private ApsBlankOrderService apsBlankOrderService;

    @Autowired
    private ApsProductionTechnicalRequirementService apsProductionTechnicalRequirementService;

    /*
    获取客户订单
     */
    @Override
    public R getCustomOrderData(Map<String, Object> params) {
        R r = new R();
        List<String> orderNos = new ArrayList<>();
        orderNos.add(params.get("CUSTOMORDERNO").toString());
        List<VNcOrderVo> vNcOrderVos = slaveSourceService.getVNcOrderVo(orderNos);
        if (vNcOrderVos.size() == 0) {
            return R.error("客户订单系统没有找到该订单");
        } else {
            VNcOrderVo vNcOrderVo = vNcOrderVos.get(0);
//            List<ApsCustomerManagementDo> customDatas = apsProductDetailDao.getCustomerData();
//            ApsCustomerManagementDo apsCustomerManagementDo = customDatas.stream().filter(cus -> cus.getCustomername().equals(vNcOrderVo.getCustomname())).findAny().orElse(null);
//            if (apsCustomerManagementDo != null) {
//                vNcOrderVo.setCustomid(apsCustomerManagementDo.getId());
//                vNcOrderVo.setDeliverytime(apsCustomerManagementDo.getDeliverytime());
//            }
            //明细 物料、合金、合金状态转换
            List<ApsProcessMaterialDo> materialDos = apsProductDetailDao.getMaterialData();
            List<ApsProcessMaterialDo> metalDos = apsProductDetailDao.getMetalData();
            List<ApsProcessMaterialDo> metalStateDos = apsProductDetailDao.getMetalStateData();
            List<VNcOrderDetailVo> vNcOrderDetailVoList = slaveSourceService.getVNcOrderDetailVo(orderNos);
            for (VNcOrderDetailVo detailVo : vNcOrderDetailVoList) {
                //物料
                ApsProcessMaterialDo apsProcessMaterialDo = materialDos.stream().filter(apm -> apm.getMaterialname().equals(detailVo.getMaterialname())).findAny().orElse(null);
                if (apsProcessMaterialDo != null) {
                    detailVo.setMaterialcode(apsProcessMaterialDo.getMaterialcode());
                    detailVo.setNumberunit(apsProcessMaterialDo.getMaterialunit());
                } else {
                    detailVo.setMaterialcode("");
                    detailVo.setMaterialname("");
                    detailVo.setNumberunit("");
                }

                //合金
                ApsProcessMaterialDo metalDo = metalDos.stream().filter(apm -> apm.getMaterialname().equals(detailVo.getAlloy())).findAny().orElse(null);
                if (metalDo != null) {
                    detailVo.setAlloy(metalDo.getMaterialcode());
                } else {
                    detailVo.setAlloy("");
                }

                //合金状态
                ApsProcessMaterialDo metalStateDo = metalStateDos.stream().filter(apm -> apm.getMaterialname().equals(detailVo.getAlloystatus())).findAny().orElse(null);
                if (metalStateDo != null) {
                    detailVo.setAlloystatus(metalStateDo.getMaterialcode());
                } else {
                    detailVo.setAlloystatus("");
                }
            }

            vNcOrderVo.setVNcOrderDetailVoList(vNcOrderDetailVoList);
            r.setCode("200");
            r.setData(vNcOrderVo);
            return r;
        }
    }

    /*
    更新客户订单
     */
    @Override
    public R updateCustomOrder(Map<String, Object> params) {
        StringBuffer noOrderNos = new StringBuffer();//不存在客户订单系统里的客户订单号
        List<String> hasOrderNos = new ArrayList<>();//存在客户订单系统里的客户订单号
        String curtenantid = params.get("CURTENANTID").toString();
        List<String> orderNos = (List<String>) params.get("ORDERNOS");

        //客户订单系统的数据
        List<VNcOrderVo> vNcOrderVoList = slaveSourceService.getVNcOrderVo(orderNos);
        List<VNcOrderDetailVo> vNcOrderDetailVos = slaveSourceService.getVNcOrderDetailVo(orderNos);
        for (String orderno : orderNos) {
            if (vNcOrderVoList.stream().filter(nco -> nco.getOrderno().equals(orderno)).collect(Collectors.toList()).size() == 0) {
                noOrderNos.append(orderno + ",");
            } else {
                hasOrderNos.add(orderno);
            }
        }

        //本系统的订单信息
        List<ApsProductionOrderDo> apsProductionOrderDoList = new ArrayList<>();
        List<ApsProductDetailDo> apsProductDetailDoList = new ArrayList<>();
        List<String> delProductDetailIdList = new ArrayList<>();
        if (hasOrderNos.size() > 0) {
            List<ApsProductionOrderDo> apsProductionOrderDos = apsProductDetailDao.getProductionOrders(curtenantid, hasOrderNos);
            List<ApsProductDetailDo> apsProductDetailDos = apsProductDetailDao.getProductDetails(curtenantid, hasOrderNos);
//        List<ApsCustomerManagementDo> customDatas = apsProductDetailDao.getCustomerData();
            //明细 物料、合金、合金状态转换
            List<ApsProcessMaterialDo> materialDos = apsProductDetailDao.getMaterialData();
            List<ApsProcessMaterialDo> metalDos = apsProductDetailDao.getMetalData();
            List<ApsProcessMaterialDo> metalStateDos = apsProductDetailDao.getMetalStateData();
            for (ApsProductionOrderDo apsProductionOrderDo : apsProductionOrderDos) {
                //主表
                VNcOrderVo vNcOrderVo = vNcOrderVoList.stream().filter(nco -> nco.getOrderno().equals(apsProductionOrderDo.getCustomorderno())).findAny().orElse(null);
//            ApsCustomerManagementDo apsCustomerManagementDo = customDatas.stream().filter(cus -> cus.getCustomername().equals(vNcOrderVo.getCustomname())).findAny().orElse(null);
//            if (apsCustomerManagementDo != null) {
//                apsProductionOrderDo.setCustomid(apsCustomerManagementDo.getId());
//                apsProductionOrderDo.setCustomname(apsCustomerManagementDo.getCustomername());
//                apsProductionOrderDo.setDeliverytime(apsCustomerManagementDo.getDeliverytime());
//            } else {
//                apsProductionOrderDo.setCustomid("");
//                apsProductionOrderDo.setCustomname("");
//                apsProductionOrderDo.setDeliverytime(null);
//            }

                apsProductionOrderDo.setOrderdate(vNcOrderVo.getOrderdate());
                apsProductionOrderDo.setRemark(vNcOrderVo.getRemark());
                apsProductionOrderDo.setDeliverydate(vNcOrderVo.getDeliverydate());
                apsProductionOrderDo.setBelongfactory(vNcOrderVo.getBelongfactory());
                apsProductionOrderDo.setIsfromcustom("1");

                //明细
                //客户订单系统的明细数据
                List<VNcOrderDetailVo> vNcOrderDetailVoList = vNcOrderDetailVos.stream().filter(vod -> vod.getOrderno().equals(apsProductionOrderDo.getCustomorderno())).collect(Collectors.toList());
                for (ApsProductDetailDo apsProductDetailDo : apsProductDetailDos) {
                    VNcOrderDetailVo vNcOrderDetailVo = vNcOrderDetailVoList.stream().filter(vodl -> vodl.getId().equals(apsProductDetailDo.getCustomorderdetailid())).findAny().orElse(null);
                    if (vNcOrderDetailVo != null) {
                        //存在则更新
                        //物料
                        ApsProcessMaterialDo apsProcessMaterialDo = materialDos.stream().filter(apm -> apm.getMaterialname().equals(vNcOrderDetailVo.getMaterialname())).findAny().orElse(null);
                        if (apsProcessMaterialDo != null) {
                            apsProductDetailDo.setMaterialcode(apsProcessMaterialDo.getMaterialcode());
                            apsProductDetailDo.setNumberunit(apsProcessMaterialDo.getMaterialunit());
                        } else {
                            apsProductDetailDo.setMaterialcode("");
                            apsProductDetailDo.setMaterialname("");
                            apsProductDetailDo.setNumberunit("");
                        }

                        //合金
                        ApsProcessMaterialDo metalDo = metalDos.stream().filter(apm -> apm.getMaterialname().equals(vNcOrderDetailVo.getAlloy())).findAny().orElse(null);
                        if (metalDo != null) {
                            apsProductDetailDo.setAlloy(metalDo.getMaterialcode());
                        } else {
                            apsProductDetailDo.setAlloy("");
                        }

                        //合金状态
                        ApsProcessMaterialDo metalStateDo = metalStateDos.stream().filter(apm -> apm.getMaterialname().equals(vNcOrderDetailVo.getAlloystatus())).findAny().orElse(null);
                        if (metalStateDo != null) {
                            apsProductDetailDo.setAlloystatus(metalStateDo.getMaterialcode());
                        } else {
                            apsProductDetailDo.setAlloystatus("");
                        }

                        apsProductDetailDo.setSpecialfication(vNcOrderDetailVo.getSpecialfication());
                        apsProductDetailDo.setThickness(vNcOrderDetailVo.getThickness());
                        apsProductDetailDo.setProwidth(vNcOrderDetailVo.getProwidth());
                        apsProductDetailDo.setProlength(vNcOrderDetailVo.getProlength());
                        apsProductDetailDo.setTotalordercount(vNcOrderDetailVo.getTotalordercount());
//                        apsProductDetailDo.setRollnum(vNcOrderDetailVo.getRollnum());
                        apsProductDetailDo.setRemark(vNcOrderDetailVo.getRemark());

                        apsProductDetailDoList.add(apsProductDetailDo);
                    } else {
                        //不存在则删除
                        delProductDetailIdList.add(apsProductDetailDo.getId());
                    }

                    vNcOrderDetailVoList.remove(vNcOrderDetailVo);
                }

                //客户系统新增的明细
                for (VNcOrderDetailVo vNcOrderDetailVo : vNcOrderDetailVoList) {
                    ApsProductDetailDo apsProductDetailDo = BeanUtil.toBean(vNcOrderDetailVo, ApsProductDetailDo.class);
                    apsProductDetailDo.setId(SecurityUtil.getUUID());
                    apsProductDetailDo.setProductstatus("10");
                    apsProductDetailDo.setMainid(apsProductionOrderDo.getId());
                    apsProductDetailDo.setCustomorderdetailid(vNcOrderDetailVo.getId());
                    apsProductDetailDo.setTenantid(apsProductionOrderDo.getTenantid());
                    apsProductDetailDo.setCustomorderno(apsProductionOrderDo.getCustomorderno());

                    //物料
                    ApsProcessMaterialDo apsProcessMaterialDo = materialDos.stream().filter(apm -> apm.getMaterialname().equals(vNcOrderDetailVo.getMaterialname())).findAny().orElse(null);
                    if (apsProcessMaterialDo != null) {
                        apsProductDetailDo.setMaterialcode(apsProcessMaterialDo.getMaterialcode());
                        apsProductDetailDo.setNumberunit(apsProcessMaterialDo.getMaterialunit());
                    } else {
                        apsProductDetailDo.setMaterialcode("");
                        apsProductDetailDo.setMaterialname("");
                        apsProductDetailDo.setNumberunit("");
                    }

                    //合金
                    ApsProcessMaterialDo metalDo = metalDos.stream().filter(apm -> apm.getMaterialname().equals(vNcOrderDetailVo.getAlloy())).findAny().orElse(null);
                    if (metalDo != null) {
                        apsProductDetailDo.setAlloy(metalDo.getMaterialcode());
                    } else {
                        apsProductDetailDo.setAlloy("");
                    }

                    //合金状态
                    ApsProcessMaterialDo metalStateDo = metalStateDos.stream().filter(apm -> apm.getMaterialname().equals(vNcOrderDetailVo.getAlloystatus())).findAny().orElse(null);
                    if (metalStateDo != null) {
                        apsProductDetailDo.setAlloystatus(metalStateDo.getMaterialcode());
                    } else {
                        apsProductDetailDo.setAlloystatus("");
                    }

                    apsProductDetailDoList.add(apsProductDetailDo);
                }

                apsProductionOrderDoList.add(apsProductionOrderDo);
            }
        }

        boolean bool1 = true;
        boolean bool2 = true;
        boolean bool3 = true;
        if (apsProductionOrderDoList.size() > 0) {
            bool1 = apsProductionOrderService.saveOrUpdateBatch(apsProductionOrderDoList);
        }
        if (apsProductDetailDoList.size() > 0) {
            bool2 = apsProductDetailService.saveOrUpdateBatch(apsProductDetailDoList);
        }
        if (delProductDetailIdList.size() > 0) {
            bool3 = apsProductDetailService.removeByIds(delProductDetailIdList);
        }

        if (bool1 && bool2 && bool3) {
            if (StringUtils.isBlank(noOrderNos.toString())) {
                return R.ok("更新成功");
            } else {
                return R.ok("客户订单：" + noOrderNos + "系统没有找到该订单，其他订单更新成功");
            }
        } else {
            return R.error("获取客户订单系统订单失败，请联系管理员");
        }
    }

    /*
    更新合并客户订单
     */
    @Override
    public R updateMergeCustomOrder(@RequestBody Map<String, Object> params) {
        StringBuffer noOrderNos = new StringBuffer();//不存在客户订单系统里的客户订单号
        List<String> hasOrderNos = new ArrayList<>();//存在客户订单系统里的客户订单号
        String curtenantid = params.get("CURTENANTID").toString();
        List<String> orderNos = (List<String>) params.get("ORDERNOS");

        //客户订单系统的数据
        List<VNcOrderVo> vNcOrderVoList = slaveSourceService.getVNcOrderVo(orderNos);
        List<VNcOrderDetailVo> vNcOrderDetailVos = slaveSourceService.getVNcOrderDetailVo(orderNos);
        for (String orderno : orderNos) {
            if (vNcOrderVoList.stream().filter(nco -> nco.getOrderno().equals(orderno)).collect(Collectors.toList()).size() == 0) {
                noOrderNos.append(orderno + ",");
            } else {
                hasOrderNos.add(orderno);
            }
        }

        //本系统的订单信息
        List<ApsProductionMergeOrderDo> apsProductionMergeOrderDos = apsProductDetailDao.getProductionMergeOrders(curtenantid, hasOrderNos);
        List<ApsProductDetailDo> apsProductDetailDos = apsProductDetailDao.getProductDetails(curtenantid, hasOrderNos);
        List<ApsProductionMergeOrderDo> apsProductionMergeOrderDoList = new ArrayList<>();
        List<ApsProductDetailDo> apsProductDetailDoList = new ArrayList<>();
        List<String> delProductDetailIdList = new ArrayList<>();
//        List<ApsCustomerManagementDo> customDatas = apsProductDetailDao.getCustomerData();
        //明细 物料、合金、合金状态转换
        List<ApsProcessMaterialDo> materialDos = apsProductDetailDao.getMaterialData();
        List<ApsProcessMaterialDo> metalDos = apsProductDetailDao.getMetalData();
        List<ApsProcessMaterialDo> metalStateDos = apsProductDetailDao.getMetalStateData();
        for (ApsProductionMergeOrderDo apsProductionMergeOrderDo : apsProductionMergeOrderDos) {
            //主表
            VNcOrderVo vNcOrderVo = vNcOrderVoList.stream().filter(nco -> nco.getOrderno().equals(apsProductionMergeOrderDo.getCustomorderno())).findAny().orElse(null);
//            ApsCustomerManagementDo apsCustomerManagementDo = customDatas.stream().filter(cus -> cus.getCustomername().equals(vNcOrderVo.getCustomname())).findAny().orElse(null);
//            if (apsCustomerManagementDo != null) {
//                apsProductionMergeOrderDo.setCustomid(apsCustomerManagementDo.getId());
//                apsProductionMergeOrderDo.setCustomname(apsCustomerManagementDo.getCustomername());
//                apsProductionMergeOrderDo.setDeliverytime(apsCustomerManagementDo.getDeliverytime());
//            } else {
//                apsProductionMergeOrderDo.setCustomid("");
//                apsProductionMergeOrderDo.setCustomname("");
//                apsProductionMergeOrderDo.setDeliverytime(null);
//            }

            apsProductionMergeOrderDo.setOrderdate(vNcOrderVo.getOrderdate());
            apsProductionMergeOrderDo.setRemark(vNcOrderVo.getRemark());
            apsProductionMergeOrderDo.setDeliverydate(vNcOrderVo.getDeliverydate());
            apsProductionMergeOrderDo.setBelongfactory(vNcOrderVo.getBelongfactory());
            apsProductionMergeOrderDo.setIsfromcustom("1");

            //明细
            //客户订单系统的明细数据
            List<VNcOrderDetailVo> vNcOrderDetailVoList = vNcOrderDetailVos.stream().filter(vod -> vod.getOrderno().equals(apsProductionMergeOrderDo.getCustomorderno())).collect(Collectors.toList());
            for (ApsProductDetailDo apsProductDetailDo : apsProductDetailDos) {
                VNcOrderDetailVo vNcOrderDetailVo = vNcOrderDetailVoList.stream().filter(vodl -> vodl.getId().equals(apsProductDetailDo.getCustomorderdetailid())).findAny().orElse(null);
                if (vNcOrderDetailVo != null) {
                    //存在则更新
                    //物料
                    ApsProcessMaterialDo apsProcessMaterialDo = materialDos.stream().filter(apm -> apm.getMaterialname().equals(vNcOrderDetailVo.getMaterialname())).findAny().orElse(null);
                    if (apsProcessMaterialDo != null) {
                        apsProductDetailDo.setMaterialcode(apsProcessMaterialDo.getMaterialcode());
                        apsProductDetailDo.setNumberunit(apsProcessMaterialDo.getMaterialunit());
                    } else {
                        apsProductDetailDo.setMaterialcode("");
                        apsProductDetailDo.setMaterialname("");
                        apsProductDetailDo.setNumberunit("");
                    }

                    //合金
                    ApsProcessMaterialDo metalDo = metalDos.stream().filter(apm -> apm.getMaterialname().equals(vNcOrderDetailVo.getAlloy())).findAny().orElse(null);
                    if (metalDo != null) {
                        apsProductDetailDo.setAlloy(metalDo.getMaterialcode());
                    } else {
                        apsProductDetailDo.setAlloy("");
                    }

                    //合金状态
                    ApsProcessMaterialDo metalStateDo = metalStateDos.stream().filter(apm -> apm.getMaterialname().equals(vNcOrderDetailVo.getAlloystatus())).findAny().orElse(null);
                    if (metalStateDo != null) {
                        apsProductDetailDo.setAlloystatus(metalStateDo.getMaterialcode());
                    } else {
                        apsProductDetailDo.setAlloystatus("");
                    }

                    apsProductDetailDo.setSpecialfication(vNcOrderDetailVo.getSpecialfication());
                    apsProductDetailDo.setThickness(vNcOrderDetailVo.getThickness());
                    apsProductDetailDo.setProwidth(vNcOrderDetailVo.getProwidth());
                    apsProductDetailDo.setProlength(vNcOrderDetailVo.getProlength());
                    apsProductDetailDo.setTotalordercount(vNcOrderDetailVo.getTotalordercount());
//                    apsProductDetailDo.setRollnum(vNcOrderDetailVo.getRollnum());
                    apsProductDetailDo.setRemark(vNcOrderDetailVo.getRemark());

                    apsProductDetailDoList.add(apsProductDetailDo);
                } else {
                    //不存在则删除
                    delProductDetailIdList.add(apsProductDetailDo.getId());
                }

                vNcOrderDetailVoList.remove(vNcOrderDetailVo);
            }

            //客户系统新增的明细
            for (VNcOrderDetailVo vNcOrderDetailVo : vNcOrderDetailVoList) {
                ApsProductDetailDo apsProductDetailDo = BeanUtil.toBean(vNcOrderDetailVo, ApsProductDetailDo.class);
                apsProductDetailDo.setId(SecurityUtil.getUUID());
                apsProductDetailDo.setProductstatus("10");
                apsProductDetailDo.setMainid(apsProductionMergeOrderDo.getId());
                apsProductDetailDo.setCustomorderdetailid(vNcOrderDetailVo.getId());
                apsProductDetailDo.setTenantid(apsProductionMergeOrderDo.getTenantid());
                apsProductDetailDo.setCustomorderno(apsProductionMergeOrderDo.getCustomorderno());

                //物料
                ApsProcessMaterialDo apsProcessMaterialDo = materialDos.stream().filter(apm -> apm.getMaterialname().equals(vNcOrderDetailVo.getMaterialname())).findAny().orElse(null);
                if (apsProcessMaterialDo != null) {
                    apsProductDetailDo.setMaterialcode(apsProcessMaterialDo.getMaterialcode());
                    apsProductDetailDo.setNumberunit(apsProcessMaterialDo.getMaterialunit());
                } else {
                    apsProductDetailDo.setMaterialcode("");
                    apsProductDetailDo.setMaterialname("");
                    apsProductDetailDo.setNumberunit("");
                }

                //合金
                ApsProcessMaterialDo metalDo = metalDos.stream().filter(apm -> apm.getMaterialname().equals(vNcOrderDetailVo.getAlloy())).findAny().orElse(null);
                if (metalDo != null) {
                    apsProductDetailDo.setAlloy(metalDo.getMaterialcode());
                } else {
                    apsProductDetailDo.setAlloy("");
                }

                //合金状态
                ApsProcessMaterialDo metalStateDo = metalStateDos.stream().filter(apm -> apm.getMaterialname().equals(vNcOrderDetailVo.getAlloystatus())).findAny().orElse(null);
                if (metalStateDo != null) {
                    apsProductDetailDo.setAlloystatus(metalStateDo.getMaterialcode());
                } else {
                    apsProductDetailDo.setAlloystatus("");
                }

                apsProductDetailDoList.add(apsProductDetailDo);
            }

            apsProductionMergeOrderDoList.add(apsProductionMergeOrderDo);
        }

        boolean bool1 = true;
        boolean bool2 = true;
        boolean bool3 = true;
        if (apsProductionMergeOrderDoList.size() > 0) {
            bool1 = apsProductionMergeOrderService.saveOrUpdateBatch(apsProductionMergeOrderDoList);
        }
        if (apsProductDetailDoList.size() > 0) {
            bool2 = apsProductDetailService.saveOrUpdateBatch(apsProductDetailDoList);
        }
        if (delProductDetailIdList.size() > 0) {
            bool3 = apsProductDetailService.removeByIds(delProductDetailIdList);
        }

        if (bool1 && bool2 && bool3) {
            if (StringUtils.isBlank(noOrderNos.toString())) {
                return R.ok("更新成功");
            } else {
                return R.ok("客户订单：" + noOrderNos + "系统没有找到该订单，其他订单更新成功");
            }
        } else {
            return R.error("获取客户订单系统订单失败，请联系管理员");
        }
    }

    @Override
    @Transactional
    public R updateOrSaveProductDetails(List<ApsProductDetailDo> req) {
        // 当前等路人所属工厂
        SysUserVo currentUser = CXCommonUtils.getCurrentUser();
        String tenantId = currentUser.getOrganizationDoList().get(0).getFullpid().split(",")[1];
        if (ObjectUtil.isNotEmpty(req) && req.size() > 0) {
            for (ApsProductDetailDo apsProductDetailDo : req) {
                apsProductDetailDo.setTenantid(tenantId);
            }
            this.saveOrUpdateBatch(req);
        }
        return R.ok();
    }

    @Override
    @Transactional
    public R updateProductDetailsAndBlankOutput(UpdateProductDetailsAndBlankOutputReq req) {
        //获取目前订单所有的产品明细
        List<ApsProductDetailDo> apsProductDetailDoList = req.getApsProductDetailDoList();
        //获取当前修改的产品明细的ID
        String nowId = req.getUpdateId();

        //区分 目前修改的产品明细和 暂未修改的产品明细
        List<ApsProductDetailDo> nowUpdateList = apsProductDetailDoList.stream().filter(item -> item.getId().equals(nowId)).collect(Collectors.toList());
        List<ApsProductDetailDo> updateOrInsertDetailList = apsProductDetailDoList.stream().filter(item -> !item.getId().equals(nowId)).collect(Collectors.toList());

        if (nowUpdateList.size() > 0 && ObjectUtil.isNotEmpty(nowUpdateList.get(0))) {
            //产品明细变更
            ApsProductDetailDo apsProductDetailDo = nowUpdateList.get(0);

            //复制新的产品明细
            apsProductDetailDo.setId(SecurityUtil.getUUID());
            updateOrInsertDetailList.add(apsProductDetailDo);

            //====================更新坯料计划及其输出成品===========================
            //检查订单产品明细对应的所有相关的坯料计划的输出成品，
            //如果订单产品明细的订单产品字段值不等于坯料计划输出成品的订单产品字段值，
            //则订单产品明细ID=（新ID），更新订单产品明细的坯料计划输出总重量=0
            //查询当前产品明细ID对应的坯料计划输出成品
            List<ApsProcessOutputProductAndBlankStatusVo> apsProcessOutputProductAndBlankStatusVoList = this.baseMapper.selectAboutBlankOutputByDetailId(nowId);
            //筛选其中待排程和待发布的坯料计划的输出成品
            List<ApsProcessOutputProductAndBlankStatusVo> needUpdateOutputList = apsProcessOutputProductAndBlankStatusVoList.stream().filter(item ->
                    ObjectUtil.isNotEmpty(item.getBlankStatus())
                            //坯料计划的生产状态为 待排程或待发布
                            && Integer.parseInt(item.getBlankStatus()) <= Integer.parseInt(SaveConstans.ProductionStatus.TO_BE_PUBLISHED)).collect(Collectors.toList());
            if (apsProcessOutputProductAndBlankStatusVoList.size() > 0) {
                Set<String> needUpdateBlankIds = new HashSet<>();
                for (ApsProcessOutputProductAndBlankStatusVo vo : needUpdateOutputList) {
                    if (!vo.getProductname().equals(apsProductDetailDo.getInputmaterialdescription())) {
                        //变更产品明细对应的输出成品
                        ApsProcessOutputProductDo apsProcessOutputProductDo = new ApsProcessOutputProductDo();
                        apsProcessOutputProductDo.setId(vo.getId());
                        apsProcessOutputProductDo.setAlloy(apsProductDetailDo.getAlloy());
                        apsProcessOutputProductDo.setAlloystatus(apsProductDetailDo.getAlloystatus());
                        apsProcessOutputProductDo.setThickness(apsProductDetailDo.getThickness());
                        apsProcessOutputProductDo.setProwidth(apsProductDetailDo.getProwidth());
                        apsProcessOutputProductDo.setProlength(apsProductDetailDo.getProlength());
                        apsProcessOutputProductDo.setRollweightrequirement(apsProductDetailDo.getRollweightrequirement());
                        apsProcessOutputProductDo.setRolldiameterrequirement(apsProductDetailDo.getRolldiameterrequirement());
                        apsProcessOutputProductDo.setRollnumberrequirement(apsProductDetailDo.getRollnumberrequirement());
                        apsProcessOutputProductDo.setProductname(apsProductDetailDo.getInputmaterialdescription());
                        apsProcessOutputProductDo.setProductid(apsProductDetailDo.getId());
                        String alloy = apsProcessOutputProductDo.getAlloy();
                        String alloyStatus = apsProcessOutputProductDo.getAlloystatus();
                        String productType = vo.getProducttype();
                        if (ObjectUtil.isNotEmpty(vo.getChangealloy())) {
                            alloy = vo.getChangealloy();
                        }
                        if (ObjectUtil.isNotEmpty(vo.getChangealloystatus())) {
                            alloyStatus = vo.getChangealloystatus();
                        }
                        if (ObjectUtil.isNotEmpty(vo.getChangeproducttype())) {
                            productType = vo.getChangeproducttype();
                        }

                        //拼接输出成品的输出物料类型字段
                        apsProcessOutputProductDo.setOutmatertype(alloy + productType + apsProcessOutputProductDo.getThickness());
                        //拼接输出成品的输出物料规格字段
                        apsProcessOutputProductDo.setOutmaterspecification(alloy + alloyStatus + productType + " " + apsProcessOutputProductDo.getProductname().split(" ")[1]);
                        //拼接输出成品的输出成品字段
                        apsProcessOutputProductDo.setOutputresult(apsProcessOutputProductDo.getOutmaterspecification() + " " + vo.getOutputonerollweigth() + "吨 * " + vo.getPlanoutputorderroll() + "卷");

                        apsProcessOutputProductService.updateById(apsProcessOutputProductDo);
                        needUpdateBlankIds.add(vo.getBlankId());
                    }
                }
                //遍历需要更新的坯料计划，更新 输出订单产品、输出成品、坯料输出物料类型、坯料输出物料规格
                for (String needUpdateBlankId : needUpdateBlankIds) {
                    apsBlankOrderService.updateBlankSplicingColumn(needUpdateBlankId);
                }
            }

            //=================更新技术要求==========================
            //查询相关的技术要求
            List<ApsProductionTechnicalRequirementDo> apsProductionTechnicalRequirementDoList = apsProductionTechnicalRequirementService.list(new QueryWrapper<ApsProductionTechnicalRequirementDo>().lambda().like(ApsProductionTechnicalRequirementDo::getOrderdetailids, nowId).orderByDesc(ApsProductionTechnicalRequirementDo::getModifydate));
            if (apsProductionTechnicalRequirementDoList.size() > 0) {
                //只会存在一个
                ApsProductionTechnicalRequirementDo apsProductionTechnicalRequirementDo = apsProductionTechnicalRequirementDoList.get(0);
                //更新原来的产品明细为新的产品明细
                ApsProductionTechnicalRequirementDo updateTechnicalRequirement = new ApsProductionTechnicalRequirementDo();
                updateTechnicalRequirement.setId(apsProductionTechnicalRequirementDo.getId());

                //获取原来的产品明细IDs和产品明细的名称
                String orderdetailids = apsProductionTechnicalRequirementDo.getOrderdetailids();
                String orderdetailnames = apsProductionTechnicalRequirementDo.getOrderdetailnames();
                List<String> orderDetailIdList = Arrays.asList(orderdetailids.split(","));
                List<String> orderDetailNameList = Arrays.asList(orderdetailnames.split(",\r\n"));
                //获取 原产品明细的ID的下标
                int index = orderDetailIdList.indexOf(nowId);
                //替换成新的产品明细
                orderDetailIdList.set(index, apsProductDetailDo.getId());
                orderDetailNameList.set(index, apsProductDetailDo.getInputmaterialdescription());

                updateTechnicalRequirement.setOrderdetailids(String.join(",", orderDetailIdList));
                updateTechnicalRequirement.setOrderdetailnames(String.join(",\r\n", orderDetailNameList));
                //更新技术要求
                apsProductionTechnicalRequirementService.updateById(updateTechnicalRequirement);
            }
        }

        //删除指定ID的产品明细
        this.removeById(nowId);
        //更新或新增 产品明细
        if (ObjectUtil.isNotEmpty(updateOrInsertDetailList)) {
            this.updateOrSaveProductDetails(updateOrInsertDetailList);
        }

        return R.ok();
    }
}
