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

import cn.hutool.core.bean.BeanUtil;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.rongwei.bscommon.sys.dao.ApsProductDetailDao;
import com.rongwei.bscommon.sys.service.ApsProductDetailService;
import com.rongwei.bscommon.sys.service.ApsProductionMergeOrderService;
import com.rongwei.bscommon.sys.service.ApsProductionOrderService;
import com.rongwei.bscommon.sys.service.SlaveSourceService;
import com.rongwei.bsentity.domain.*;
import com.rongwei.rwcommon.base.R;
import com.rongwei.rwcommon.utils.SecurityUtil;
import com.rongwei.rwcommon.utils.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.ArrayList;
import java.util.List;
import java.util.Map;
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;

    /*
    获取客户订单
     */
    @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(List<String> orderNos) {
        StringBuffer noOrderNos = new StringBuffer();//不存在客户订单系统里的客户订单号
        List<String> hasOrderNos = new ArrayList<>();//存在客户订单系统里的客户订单号

        //客户订单系统的数据
        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(hasOrderNos);
            List<ApsProductDetailDo> apsProductDetailDos = apsProductDetailDao.getProductDetails(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());

                //明细
                //客户订单系统的明细数据
                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(List<String> orderNos) {
        StringBuffer noOrderNos = new StringBuffer();//不存在客户订单系统里的客户订单号
        List<String> hasOrderNos = new ArrayList<>();//存在客户订单系统里的客户订单号

        //客户订单系统的数据
        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(hasOrderNos);
        List<ApsProductDetailDo> apsProductDetailDos = apsProductDetailDao.getProductDetails(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());

            //明细
            //客户订单系统的明细数据
            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("获取客户订单系统订单失败，请联系管理员");
        }
    }
}
