package com.zfire.mall.manager.logic.stock;


import cn.hutool.core.bean.BeanUtil;
import cn.hutool.core.convert.Convert;
import cn.hutool.core.date.DateTime;
import cn.hutool.core.date.DateUtil;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.core.toolkit.CollectionUtils;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.zfire.mall.manager.bean.ExcelData;
import com.zfire.mall.manager.bean.admin.AdminUserCom;
import com.zfire.mall.manager.bean.sale.StorageFeeBean;
import com.zfire.mall.manager.bean.stock.StockBean;
import com.zfire.mall.manager.bean.stock.StockBeanToDay;
import com.zfire.mall.manager.bean.stock.StockStartAccVO;
import com.zfire.mall.manager.commonmapper.stock.StockCommonMapper;
import com.zfire.mall.manager.enums.OrderTypeStatusEnum;
import com.zfire.mall.manager.logic.common.CommonLogic;
import com.zfire.mall.manager.plus.entity.*;
import com.zfire.mall.manager.plus.service.*;
import com.zfire.mall.manager.zfire.bean.ZfireParamBean;
import com.zfire.mall.manager.zfire.util.FieldUtils;
import lombok.extern.slf4j.Slf4j;
import org.apache.commons.lang3.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.math.BigDecimal;
import java.util.ArrayList;
import java.util.Arrays;
import java.util.Date;
import java.util.List;
import java.util.stream.Collectors;

@Slf4j
@Service
public class StockStartAccLogic {

    @Autowired
    StockStartService stockStartService;

    @Autowired
    StockStartAccService stockStartAccService;

    @Autowired
    StockAccService stockAccService;

    @Autowired
    StockService stockService;

    @Autowired
    KingDeeOtherStockInService kingDeeOtherStockInService;

    @Autowired
    KingDeeOtherStockOutService kingDeeOtherStockOutService;

    @Autowired
    KingDeePurchaseStockInService kingDeePurchaseStockInService;

    @Autowired
    KingDeePurchaseStockOutService kingDeePurchaseStockOutService;

    @Autowired
    KingDeeMaterialService kingDeeMaterialService;

    @Autowired
    SalesOrderItemService salesOrderItemService;

    @Autowired
    SalesOrderService salesOrderService;

    @Autowired
    KingDeeStockService kingDeeStockService;


    @Autowired
    KingDeeTransferService kingDeeTransferService;

    @Autowired
    KingDeeTransferItemService kingDeeTransferItemService;

    @Autowired
    StockCommonLogic stockCommonLogic;

    @Autowired
    CommonLogic commonLogic;

    @Autowired
    StockCommonMapper stockCommonMapper;

    public IPage<StockStartAccVO> list(String correspondName, String materialName, String materialNumber, String specification,Integer pageSize,Integer pageNum) {

        Page<StockStartAcc> page = stockStartAccService.lambdaQuery()
                .eq(!StringUtils.isEmpty(correspondName), StockStartAcc::getStockName, correspondName)
                .like(!StringUtils.isEmpty(materialName), StockStartAcc::getMaterialName, materialName)
                .like(!StringUtils.isEmpty(materialNumber), StockStartAcc::getMaterialNum, materialNumber)
                .like(!StringUtils.isEmpty(specification), StockStartAcc::getSpec, specification)
                .page(new Page<>(pageNum, pageSize));


        IPage<StockStartAccVO> stockStartAccVOIPage = this.addAcc(page.getRecords());

        stockStartAccVOIPage.setCurrent(page.getCurrent());
        stockStartAccVOIPage.setSize(page.getSize());
        stockStartAccVOIPage.setPages(page.getPages());
        stockStartAccVOIPage.setTotal(page.getTotal());

        return stockStartAccVOIPage;

    }

    private IPage<StockStartAccVO> addAcc(List<StockStartAcc> page) {
        List<StockStartAcc> list = page;

        if (CollectionUtils.isEmpty(list))
            return new Page<>();

        List<String> numbers = list.stream().map(StockStartAcc::getMaterialNum).collect(Collectors.toList());

        List<String> appName=new ArrayList<>();
        appName.add("成思慧");
        appName.add("张辉");
        appName.add("温锋");
        appName.add("梁静雯");
        appName.add("黎晓君");

        List<String> GZ=new ArrayList<>();
        GZ.add("张辉");
        GZ.add("温锋");

        List<String> FS=new ArrayList<>();
        FS.add("成思慧");
        FS.add("梁静雯");


        List<String> FSGZ=new ArrayList<>();
        FSGZ.add("黎晓君");

        List<String> FSsupply=new ArrayList<>();
        FSsupply.add("佛山盛世欣兴格力贸易有限公司");
        FSsupply.add("258243");

        List<String> GZSupply=new ArrayList<>();
        GZSupply.add("广州盛世欣兴格力贸易有限公司");
        GZSupply.add("258242");

        String dateStr = "2024-06-28";
        Date date = DateUtil.parse(dateStr);

        List<StockAcc> stockAccs = stockAccService.lambdaQuery()
                .ge(StockAcc::getChangeTime,date)
                .in(StockAcc::getRefType,"采购退料单","采购入库单")
                .select(StockAcc::getChangeTime,StockAcc::getRef,StockAcc::getStockChangeQty,StockAcc::getRefType,StockAcc::getMaterialId,
                        StockAcc::getStockId,StockAcc::getDirectFlag)
                .list();

        List<KingDeeMaterial> kingDeeMaterials = kingDeeMaterialService.lambdaQuery()
                .in(KingDeeMaterial::getNumber,numbers)
                .select(KingDeeMaterial::getId,KingDeeMaterial::getName,KingDeeMaterial::getCategoryName,KingDeeMaterial::getNumber,
                        KingDeeMaterial::getOldNumber,
                        KingDeeMaterial::getSpecification)
                .list();

        List<String> ids = kingDeeMaterials.stream().map(KingDeeMaterial::getId).collect(Collectors.toList());


        List<KingDeePurchaseStockIn> kingDeePurchaseStockIns = kingDeePurchaseStockInService.lambdaQuery()
                .in(KingDeePurchaseStockIn::getApproverId, appName)
                .ge(KingDeePurchaseStockIn::getApproveDate, date)
                .select(KingDeePurchaseStockIn::getId,KingDeePurchaseStockIn::getApproverId,KingDeePurchaseStockIn::getApproveDate,
                        KingDeePurchaseStockIn::getSupplyName,
                        KingDeePurchaseStockIn::getSyncTime)
                .list();


        List<KingDeePurchaseStockOut> kingDeePurchaseStockOutList = kingDeePurchaseStockOutService.lambdaQuery()
                .in(KingDeePurchaseStockOut::getApproverId, appName)

                .ge(KingDeePurchaseStockOut::getApproveDate, date)
                .select(KingDeePurchaseStockOut::getId,KingDeePurchaseStockOut::getApproverId,KingDeePurchaseStockOut::getApproveDate,
                        KingDeePurchaseStockOut::getSyncTime)
                .list();



        List<SalesOrder> salesOrders = salesOrderService.lambdaQuery().ge(SalesOrder::getCreateTime, date).or().ge(SalesOrder::getApprovalTime,date)
                .or().eq(SalesOrder::getExamineStatus,"WAIT")
                .select(SalesOrder::getId,SalesOrder::getSalesOrderNo,SalesOrder::getExamineStatus,SalesOrder::getSalesOrderNo,SalesOrder::getOrderNo,
                        SalesOrder::getApprovalTime,SalesOrder::getCorrespondName)
                .list();

        List<String> salesId = salesOrders.stream().map(SalesOrder::getId).collect(Collectors.toList());

        List<SalesOrderItem> salesOrderItemList = salesOrderItemService.lambdaQuery()
                .in(SalesOrderItem::getSalesOrderNo,salesId)
                .in(SalesOrderItem::getMaterialId, ids)
                .select(SalesOrderItem::getRefundableQty,SalesOrderItem::getSalesOrderNo,SalesOrderItem::getMaterialId)
                .list();

        List<KingDeeStock> kingDeeStockList = kingDeeStockService.lambdaQuery().list();

        //调拨单
        List<KingDeeTransfer> kingDeeTransfers = kingDeeTransferService.lambdaQuery().ge(KingDeeTransfer::getApproveDate, date).list();
        List<String> kingTransfersId = kingDeeTransfers.stream().map(KingDeeTransfer::getId).collect(Collectors.toList());
        List<KingDeeTransferItem> kingDeeTransferItemList = new ArrayList<>();
        if (!CollectionUtils.isEmpty(kingTransfersId)) {
            kingDeeTransferItemList = kingDeeTransferItemService.lambdaQuery()
                    .in(KingDeeTransferItem::getFId, kingTransfersId)
                    .list();
        }


        List<String> stockNames=new ArrayList<>();
        stockNames.add("芦苞仓");
        stockNames.add("三水仓");
        stockNames.add("三水珠海仓");
        stockNames.add("芦苞次品仓");

        List<String> transferidS=new ArrayList<>();
        transferidS.add("ZJDB000399");
        transferidS.add("ZJDB000398");
        transferidS.add("ZJDB000397");
        transferidS.add("ZJDB000396");

        List<StockStartAccVO> stockStartAccVOS = new ArrayList<>();


        for (StockStartAcc stockStartAcc : list) {



            //获得物料
            KingDeeMaterial kingDeeMaterial = kingDeeMaterials.stream().filter(item -> item.getNumber().equals(stockStartAcc.getMaterialNum()))
                    .collect(Collectors.toList()).get(0);

            KingDeeStock kingDeeStock = kingDeeStockList.stream().filter(item -> item.getName().equals(stockStartAcc.getStockName()))
                    .collect(Collectors.toList()).get(0);

            StockStartAccVO stockStartAccVO = new StockStartAccVO();
            stockStartAccVO.setStockName(kingDeeStock.getName());
            stockStartAccVO.setCategoryName(kingDeeMaterial.getCategoryName());
            stockStartAccVO.setMaterialNumber(kingDeeMaterial.getNumber());
            stockStartAccVO.setMaterialName(kingDeeMaterial.getName());
            stockStartAccVO.setMaterialOldNumber(kingDeeMaterial.getOldNumber());
            stockStartAccVO.setSpec(kingDeeMaterial.getSpecification());

            stockStartAccVO.setFsNumber(stockStartAcc.getFsNumber());
            stockStartAccVO.setGzNumber(stockStartAcc.getGzNumber());

            stockStartAccVO.setGzTheTimeNumber(Convert.toBigDecimal("0"));
            stockStartAccVO.setFsTheTimeNumber(Convert.toBigDecimal("0"));

            //判断佛山广州仓库得期初
            if (stockNames.contains(stockStartAcc.getStockName())){
                stockStartAccVO.setGzNeverNumber(Convert.toBigDecimal(0));
                stockStartAccVO.setFsNeverNumber(Convert.toBigDecimal(0));

            }else {
                stockStartAccVO.setFsNeverNumber(Convert.toBigDecimal(0));
                stockStartAccVO.setGzNeverNumber(Convert.toBigDecimal(0));
            }

            //调拨单查询 调入
            List<KingDeeTransferItem> kingDeeTransferItems = kingDeeTransferItemList.stream()
                    .filter(item -> item.getDestStockName().equals(kingDeeStock.getName()) && item.getMaterialId().equals(kingDeeMaterial.getId())).collect(Collectors.toList());

            for (KingDeeTransferItem kingDeeTransferItem : kingDeeTransferItems) {
                KingDeeTransfer kingDeeTransfer = kingDeeTransfers.stream().filter(item -> item.getId().equals(kingDeeTransferItem.getFId())).collect(Collectors.toList()).get(0);

                if (FS.contains(kingDeeTransfer.getApproverId()) || transferidS.contains(kingDeeTransfer.getBillNo())){
                    stockStartAccVO.setFsNumber(stockStartAccVO.getFsNumber().add(kingDeeTransferItem.getQty()));
                }else {
                    stockStartAccVO.setGzNumber(stockStartAccVO.getGzNumber().add(kingDeeTransferItem.getQty()));
                }
            }

            //调拨单查询 调出
            List<KingDeeTransferItem> kingDeeTransferItemsOut = kingDeeTransferItemList.stream()
                    .filter(item -> item.getSrcStockName().equals(kingDeeStock.getName()) && item.getMaterialId().equals(kingDeeMaterial.getId()))
                    .collect(Collectors.toList());

            for (KingDeeTransferItem kingDeeTransferItem : kingDeeTransferItemsOut) {
                KingDeeTransfer kingDeeTransfer = kingDeeTransfers.stream().filter(item -> item.getId().equals(kingDeeTransferItem.getFId())).collect(Collectors.toList()).get(0);
                if (FS.contains(kingDeeTransfer.getApproverId()) || transferidS.contains(kingDeeTransfer.getBillNo())){
                    stockStartAccVO.setFsNumber(stockStartAccVO.getFsNumber().subtract(kingDeeTransferItem.getQty()));
                }else {
                    stockStartAccVO.setGzNumber(stockStartAccVO.getGzNumber().subtract(kingDeeTransferItem.getQty()));
                }
            }



            //销售单 广州销售
            List<SalesOrder> salesOrderList = salesOrders.stream().filter(item -> item.getCorrespondName().equals(kingDeeStock.getName())
                    && item.getOrderNo().indexOf("GZ") != -1
            ).collect(Collectors.toList());

            List<String> salesIds = salesOrderList.stream().map(SalesOrder::getId).collect(Collectors.toList());
            List<SalesOrderItem> salesOrderItems = salesOrderItemList.stream()
                    .filter(item -> salesIds.contains(item.getSalesOrderNo()) &&
                            item.getMaterialId().equals(kingDeeMaterial.getId())).collect(Collectors.toList());

            for (SalesOrderItem salesOrderItem : salesOrderItems) {

                SalesOrder salesOrder = salesOrderList.stream().filter(item -> item.getId().equals(salesOrderItem.getSalesOrderNo())).collect(Collectors.toList()).get(0);

                if (!salesOrder.getExamineStatus().equals("OK"))
                    stockStartAccVO.setGzNeverNumber(stockStartAccVO.getGzNeverNumber().add(Convert.toBigDecimal(salesOrderItem.getRefundableQty())));
                else
                    stockStartAccVO.setGzNumber(stockStartAccVO.getGzNumber().subtract(Convert.toBigDecimal(salesOrderItem.getRefundableQty())));
            }


            //销售单 佛山销售
            List<SalesOrder> salesOrderListFS = salesOrders.stream().filter(item -> item.getCorrespondName().equals(kingDeeStock.getName())
                    && item.getOrderNo().indexOf("FS") != -1
            ).collect(Collectors.toList());
            List<String> salesIdsFS = salesOrderListFS.stream().map(SalesOrder::getId).collect(Collectors.toList());
            List<SalesOrderItem> salesOrderItemsFS = salesOrderItemList.stream()
                    .filter(item -> salesIdsFS.contains(item.getSalesOrderNo()) &&
                            item.getMaterialId().equals(kingDeeMaterial.getId())).collect(Collectors.toList());

            for (SalesOrderItem salesOrderItem : salesOrderItemsFS) {

                SalesOrder salesOrder = salesOrderListFS.stream().filter(item -> item.getId().equals(salesOrderItem.getSalesOrderNo()))
                        .collect(Collectors.toList()).get(0);

                if (!salesOrder.getExamineStatus().equals("OK"))
                    stockStartAccVO.setFsNeverNumber(stockStartAccVO.getFsNeverNumber().add(Convert.toBigDecimal(salesOrderItem.getRefundableQty())));
                else
                    stockStartAccVO.setFsNumber(stockStartAccVO.getFsNumber().subtract(Convert.toBigDecimal(salesOrderItem.getRefundableQty())));
            }




            List<StockAcc> stockAccList = stockAccs.stream()
                    .filter(item -> item.getMaterialId().equals(kingDeeMaterial.getId())
                            && item.getStockId().equals(kingDeeStock.getId()))
                    .collect(Collectors.toList());

            if (CollectionUtils.isEmpty(stockAccList)){
                stockStartAccVO.setGzOnNumber(stockStartAccVO.getGzNumber().subtract(stockStartAccVO.getGzNeverNumber()));
                stockStartAccVO.setFsOnNumber(stockStartAccVO.getFsNumber().subtract(stockStartAccVO.getFsNeverNumber()));

                stockStartAccVO.setAllNumber(stockStartAccVO.getFsNumber().add(stockStartAccVO.getGzNumber()));
                stockStartAccVO.setAllNeverNumber(stockStartAccVO.getFsNeverNumber().add(stockStartAccVO.getGzNeverNumber()));
                stockStartAccVO.setAllOnNumber(stockStartAccVO.getFsOnNumber().add(stockStartAccVO.getGzOnNumber()));


                stockStartAcc.setFsNumber(stockStartAccVO.getFsNumber());
                stockStartAcc.setGzNumber(stockStartAccVO.getGzNumber());

                if (stockStartAccVO.getFsOnNumber().doubleValue() <= 0 && stockStartAccVO.getAllOnNumber().doubleValue() > 0){
                    stockStartAccVO.setGzTheTimeNumber(stockStartAccVO.getAllOnNumber());
                }

                if (stockStartAccVO.getGzOnNumber().doubleValue() <= 0 && stockStartAccVO.getAllOnNumber().doubleValue() > 0){
                    stockStartAccVO.setFsTheTimeNumber(stockStartAccVO.getAllOnNumber());
                }

                if (stockStartAccVO.getGzOnNumber().doubleValue() > 0 && stockStartAccVO.getFsOnNumber().doubleValue() > 0){
                    stockStartAccVO.setFsTheTimeNumber(stockStartAccVO.getFsOnNumber());
                    stockStartAccVO.setGzTheTimeNumber(stockStartAccVO.getGzOnNumber());
                }


                stockStartAccVOS.add(stockStartAccVO);
                continue;
            }


            List<String> refList = stockAccList.stream().map(StockAcc::getRef).collect(Collectors.toList());


            //采购入库单 广州入库
            List<KingDeePurchaseStockIn> kingDeePurchaseStockInList =
                    kingDeePurchaseStockIns.stream().filter(item -> refList.contains(item.getId())
                            && GZ.contains(item.getApproverId())
                    ).collect(Collectors.toList());

            kingDeePurchaseStockInList.addAll(
            kingDeePurchaseStockIns.stream().filter(item -> refList.contains(item.getId())
                    && FSGZ.contains(item.getApproverId()) && GZSupply.contains(item.getSupplyName())
            ).collect(Collectors.toList()));


            List<String> kingIns = kingDeePurchaseStockInList.stream().map(KingDeePurchaseStockIn::getId).collect(Collectors.toList());

            List<StockAcc> stockPurchases = stockAccList.stream().filter(item -> kingIns.contains(item.getRef()) && item.getRefType().equals("采购入库单")

            ).collect(Collectors.toList());

            for (StockAcc stockPurchase : stockPurchases) {
                if (stockPurchase.getDirectFlag())
                    stockStartAccVO.setGzNumber(stockStartAccVO.getGzNumber().add(stockPurchase.getStockChangeQty()));
                else
                    stockStartAccVO.setGzNumber(stockStartAccVO.getGzNumber().subtract(stockPurchase.getStockChangeQty()));

            }

            //采购入库单 佛山入库
            List<KingDeePurchaseStockIn> kingDeePurchaseStockInListFo =
                    kingDeePurchaseStockIns.stream().filter(item -> refList.contains(item.getId())
                            && FS.contains(item.getApproverId())
                    ).collect(Collectors.toList());


            kingDeePurchaseStockInListFo.addAll(
                    kingDeePurchaseStockIns.stream().filter(item -> refList.contains(item.getId())
                            && FSGZ.contains(item.getApproverId()) && FSsupply.contains(item.getSupplyName())
                    ).collect(Collectors.toList()));

            List<String> kingInsFO = kingDeePurchaseStockInListFo.stream().map(KingDeePurchaseStockIn::getId).collect(Collectors.toList());

            List<StockAcc> stockPurchasesFO = stockAccList.stream().filter(item -> kingInsFO.contains(item.getRef()) && item.getRefType().equals("采购入库单")

            ).collect(Collectors.toList());

            for (StockAcc stockPurchase : stockPurchasesFO) {
                if (stockPurchase.getDirectFlag())
                    stockStartAccVO.setFsNumber(stockStartAccVO.getFsNumber().add(stockPurchase.getStockChangeQty()));
                else
                    stockStartAccVO.setFsNumber(stockStartAccVO.getFsNumber().subtract(stockPurchase.getStockChangeQty()));
            }



            //采购退料单 广州退料
            List<KingDeePurchaseStockOut> purchaseStockOuts =
                    kingDeePurchaseStockOutList.stream().filter(item -> refList.contains(item.getId()) &&
                            GZ.contains(item.getApproverId())
                    ).collect(Collectors.toList());

            purchaseStockOuts.addAll(
                    kingDeePurchaseStockOutList.stream().filter(item -> refList.contains(item.getId())
                    && FSGZ.contains(item.getApproverId()) && GZSupply.contains(item.getSupplierId())
            ).collect(Collectors.toList()));

            List<String> kingOuts = purchaseStockOuts.stream().map(KingDeePurchaseStockOut::getId).collect(Collectors.toList());

            List<StockAcc> stockAccOutList = stockAccList.stream().filter(item -> kingOuts.contains(item.getRef()) && item.getRefType().equals("采购退料单")
            ).collect(Collectors.toList());

            for (StockAcc stockAcc : stockAccOutList) {
                if (stockAcc.getDirectFlag())
                    stockStartAccVO.setGzNumber(stockStartAccVO.getGzNumber().add(stockAcc.getStockChangeQty()));
                else
                    stockStartAccVO.setGzNumber(stockStartAccVO.getGzNumber().subtract(stockAcc.getStockChangeQty()));
            }

            //采购退料单 佛山退料
            List<KingDeePurchaseStockOut> purchaseStockOutsFo =
                    kingDeePurchaseStockOutList.stream().filter(item -> refList.contains(item.getId()) &&
                            FS.contains(item.getApproverId())
                    ).collect(Collectors.toList());

            purchaseStockOutsFo.addAll(
                    kingDeePurchaseStockOutList.stream().filter(item -> refList.contains(item.getId())
                            && FSGZ.contains(item.getApproverId()) && FSsupply.contains(item.getSupplierId())
                    ).collect(Collectors.toList()));

            List<String> kingOutsFO = purchaseStockOutsFo.stream().map(KingDeePurchaseStockOut::getId).collect(Collectors.toList());

            List<StockAcc> stockAccOutListFO = stockAccList.stream().filter(item -> kingOutsFO.contains(item.getRef()) && item.getRefType().equals("采购退料单")
            ).collect(Collectors.toList());

            for (StockAcc stockAcc : stockAccOutListFO) {
                if (stockAcc.getDirectFlag())
                    stockStartAccVO.setFsNumber(stockStartAccVO.getFsNumber().add(stockAcc.getStockChangeQty()));
                else
                    stockStartAccVO.setFsNumber(stockStartAccVO.getFsNumber().subtract(stockAcc.getStockChangeQty()));
            }






            stockStartAccVO.setGzOnNumber(stockStartAccVO.getGzNumber().subtract(stockStartAccVO.getGzNeverNumber()));
            stockStartAccVO.setFsOnNumber(stockStartAccVO.getFsNumber().subtract(stockStartAccVO.getFsNeverNumber()));


            stockStartAccVO.setAllNumber(stockStartAccVO.getFsNumber().add(stockStartAccVO.getGzNumber()));
            stockStartAccVO.setAllNeverNumber(stockStartAccVO.getFsNeverNumber().add(stockStartAccVO.getGzNeverNumber()));
            stockStartAccVO.setAllOnNumber(stockStartAccVO.getFsOnNumber().add(stockStartAccVO.getGzOnNumber()));


            stockStartAcc.setFsNumber(stockStartAccVO.getFsNumber());
            stockStartAcc.setGzNumber(stockStartAccVO.getGzNumber());




            if (stockStartAccVO.getFsOnNumber().doubleValue() <= 0 && stockStartAccVO.getAllOnNumber().doubleValue() > 0){
                stockStartAccVO.setGzTheTimeNumber(stockStartAccVO.getAllOnNumber());
            }

            if (stockStartAccVO.getGzOnNumber().doubleValue() <= 0 && stockStartAccVO.getAllOnNumber().doubleValue() > 0){
                stockStartAccVO.setFsTheTimeNumber(stockStartAccVO.getAllOnNumber());
            }

            if (stockStartAccVO.getGzOnNumber().doubleValue() > 0 && stockStartAccVO.getFsOnNumber().doubleValue() > 0){
                stockStartAccVO.setFsTheTimeNumber(stockStartAccVO.getFsOnNumber());
                stockStartAccVO.setGzTheTimeNumber(stockStartAccVO.getGzOnNumber());
            }



            stockStartAccVOS.add(stockStartAccVO);

        }


        /*   stockStartAccService.updateBatchById(list);*/

        IPage<StockStartAccVO> stockStartAccVOIPage = new Page<>();

        stockStartAccVOIPage.setRecords(stockStartAccVOS);


        return stockStartAccVOIPage;
    }

    public List<StockStartAccVO> listExport(String correspondName, String materialName, String materialNumber, String specification, int i, int i1) {

        List<StockStartAcc> list = stockStartAccService.lambdaQuery()
                .like(!StringUtils.isEmpty(correspondName), StockStartAcc::getStockName, correspondName)
                .like(!StringUtils.isEmpty(materialName), StockStartAcc::getMaterialName, materialName)
                .like(!StringUtils.isEmpty(materialNumber), StockStartAcc::getMaterialNum, materialNumber)
                .like(!StringUtils.isEmpty(specification), StockStartAcc::getSpec, specification)
                .last("limit 600")
                .list();

        if (CollectionUtils.isEmpty(list))
            return new ArrayList<>();


        IPage<StockStartAccVO> stockStartAccVOIPage = this.addAcc(list);


        return stockStartAccVOIPage.getRecords();
    }



    public List<StockStartAccVO> listExport2(String correspondName, String materialName, String materialNumber, String specification,
                                             Integer pageSize,Integer pageNum) {

        List<String> idsA = stockStartAccService.lambdaQuery()
                .like(!StringUtils.isEmpty(correspondName), StockStartAcc::getStockName, correspondName)
                .like(!StringUtils.isEmpty(materialName), StockStartAcc::getMaterialName, materialName)
                .like(!StringUtils.isEmpty(materialNumber), StockStartAcc::getMaterialNum, materialNumber)
                .like(!StringUtils.isEmpty(specification), StockStartAcc::getSpec, specification)
                .last("limit 600")
                .select(StockStartAcc::getId)
                .list().stream().map(StockStartAcc::getId).collect(Collectors.toList());

        List<StockStartAcc> list = stockStartAccService.lambdaQuery()
                .like(!StringUtils.isEmpty(correspondName), StockStartAcc::getStockName, correspondName)
                .like(!StringUtils.isEmpty(materialName), StockStartAcc::getMaterialName, materialName)
                .like(!StringUtils.isEmpty(materialNumber), StockStartAcc::getMaterialNum, materialNumber)
                .like(!StringUtils.isEmpty(specification), StockStartAcc::getSpec, specification)
                .notIn(StockStartAcc::getId,idsA)
                .last("limit 600")
                .list();

        if (CollectionUtils.isEmpty(list))
            return new ArrayList<>();

        IPage<StockStartAccVO> stockStartAccVOIPage = this.addAcc(list);


        return stockStartAccVOIPage.getRecords();

    }

    public void test3() {


        Date date = DateUtil.beginOfDay(new Date());

        List<KingDeeMaterial> kingDeeMaterials = kingDeeMaterialService.lambdaQuery().in(KingDeeMaterial::getCategoryName, "家用", "移动空调")
                .select(KingDeeMaterial::getId,KingDeeMaterial::getNumber,KingDeeMaterial::getName,KingDeeMaterial::getSpecification).list();


        List<String> mId = kingDeeMaterials.stream().map(KingDeeMaterial::getId).collect(Collectors.toList());

        List<Stock> stockList = stockService.lambdaQuery().in(Stock::getMaterialId, mId)
                .ge(Stock::getSyncTime,date)
                .list();

        List<KingDeeStock> kingDeeStocks = kingDeeStockService.lambdaQuery().list();

        List<StockStartAcc> stockStartAccs = new ArrayList<>();

        for (Stock stock : stockList) {
            KingDeeStock kingDeeStock = kingDeeStocks.stream()
                    .filter(item -> item.getId().equals(stock.getStockId())).collect(Collectors.toList()).get(0);

            KingDeeMaterial kingDeeMaterial = kingDeeMaterials.stream().filter(item -> item.getId().equals(stock.getMaterialId())).collect(Collectors.toList()).get(0);
            if (stockStartAccService.lambdaQuery().eq(StockStartAcc::getStockName,kingDeeStock.getName())
                    .eq(StockStartAcc::getMaterialNum,kingDeeMaterial.getNumber()).count() < 1) {
                StockStartAcc stockStartAcc = new StockStartAcc();
                stockStartAcc.setMaterialName(kingDeeMaterial.getName());
                stockStartAcc.setMaterialNum(kingDeeMaterial.getNumber());
                stockStartAcc.setNum(0);
                stockStartAcc.setFsNumber(Convert.toBigDecimal("0"));
                stockStartAcc.setGzNumber(Convert.toBigDecimal("0"));
                stockStartAcc.setSpec(kingDeeMaterial.getSpecification());
                stockStartAcc.setStockName(kingDeeStock.getName());
                stockStartAccs.add(stockStartAcc);
            }
        }

        stockStartAccService.saveBatch(stockStartAccs);


    }


    public ExcelData stockExport(List<StockStartAccVO> records) {
        String[] titles = new String[]{"仓库名称", "存货类别", "物料编码", "产品编码", "物料名称", "规格型号", "广州可用库存","佛山可用库存","总可用",
                "广州开单未提", "佛山开单未提","总开单未提", "广州结存数", "佛山结存数","总结存数量","广州实际库存","佛山实际库存"};
        List<List<Object>> rows = new ArrayList<>();
        for (StockStartAccVO stockStartAccVO : records) {
            List<Object> row = new ArrayList<>();
            row.add(stockStartAccVO.getStockName());
            row.add(stockStartAccVO.getCategoryName());
            row.add(stockStartAccVO.getMaterialNumber());
            row.add(stockStartAccVO.getMaterialOldNumber());
            row.add(stockStartAccVO.getMaterialName());
            row.add(stockStartAccVO.getSpec());
            row.add(stockStartAccVO.getGzOnNumber());
            row.add(stockStartAccVO.getFsOnNumber());
            row.add(stockStartAccVO.getAllOnNumber());
            row.add(stockStartAccVO.getGzNeverNumber());
            row.add(stockStartAccVO.getFsNeverNumber());
            row.add(stockStartAccVO.getAllNeverNumber());
            row.add(stockStartAccVO.getGzNumber());
            row.add(stockStartAccVO.getFsNumber());
            row.add(stockStartAccVO.getAllNumber());
            row.add(stockStartAccVO.getGzTheTimeNumber());
            row.add(stockStartAccVO.getFsTheTimeNumber());

            rows.add(row);
        }
        ExcelData excelData = new ExcelData();
        excelData.setRows(rows);
        excelData.setTitles(Arrays.asList(titles));
        return excelData;
    }

    public List<StockStartAccVO> listExport3(String correspondName, String materialName, String materialNumber, String specification, int i, int i1) {

        List<String> idsA = stockStartAccService.lambdaQuery()
                .like(!StringUtils.isEmpty(correspondName), StockStartAcc::getStockName, correspondName)
                .like(!StringUtils.isEmpty(materialName), StockStartAcc::getMaterialName, materialName)
                .like(!StringUtils.isEmpty(materialNumber), StockStartAcc::getMaterialNum, materialNumber)
                .like(!StringUtils.isEmpty(specification), StockStartAcc::getSpec, specification)
                .last("limit 1200")
                .select(StockStartAcc::getId)
                .list().stream().map(StockStartAcc::getId).collect(Collectors.toList());

        List<StockStartAcc> list = stockStartAccService.lambdaQuery()
                .like(!StringUtils.isEmpty(correspondName), StockStartAcc::getStockName, correspondName)
                .like(!StringUtils.isEmpty(materialName), StockStartAcc::getMaterialName, materialName)
                .like(!StringUtils.isEmpty(materialNumber), StockStartAcc::getMaterialNum, materialNumber)
                .like(!StringUtils.isEmpty(specification), StockStartAcc::getSpec, specification)
                .notIn(StockStartAcc::getId,idsA)
                .last("limit 600")
                .list();

        if (CollectionUtils.isEmpty(list))
            return new ArrayList<>();
        IPage<StockStartAccVO> stockStartAccVOIPage = this.addAcc(list);


        return stockStartAccVOIPage.getRecords();
    }




    public IPage<StockStartAccVO> list2(String correspondName, String materialName, String materialNumber, String specification,Integer pageSize,Integer pageNum) {

        Page<StockStartAcc> page = stockStartAccService.lambdaQuery()
                .like(!StringUtils.isEmpty(correspondName), StockStartAcc::getStockName, correspondName)
                .eq(!StringUtils.isEmpty(materialName), StockStartAcc::getMaterialName, materialName)
                .eq(!StringUtils.isEmpty(materialNumber), StockStartAcc::getMaterialNum, materialNumber)
                .eq(!StringUtils.isEmpty(specification), StockStartAcc::getSpec, specification)
                .page(new Page<>(pageNum, pageSize));

        List<StockStartAcc> list = page.getRecords();

        if (CollectionUtils.isEmpty(list))
            return new Page<>();

        List<String> numbers = list.stream().map(StockStartAcc::getMaterialNum).collect(Collectors.toList());

        List<String> appName=new ArrayList<>();
        appName.add("成思慧");
        appName.add("张辉");
        appName.add("温锋");

        List<String> GZ=new ArrayList<>();
        GZ.add("张辉");
        GZ.add("温锋");

        List<String> FS=new ArrayList<>();
        FS.add("成思慧");

        String dateStr = "2024-01-26";
        Date date = DateUtil.parse(dateStr);

        List<StockAcc> stockAccs = stockAccService.lambdaQuery()
                .ge(StockAcc::getChangeTime,date)
                .in(StockAcc::getRefType,"采购退料单","采购入库单")
                .select(StockAcc::getChangeTime,StockAcc::getRef,StockAcc::getStockChangeQty,StockAcc::getRefType,StockAcc::getMaterialId,
                        StockAcc::getStockId,StockAcc::getDirectFlag)
                .list();

        List<KingDeeMaterial> kingDeeMaterials = kingDeeMaterialService.lambdaQuery()
                .in(KingDeeMaterial::getNumber,numbers)
                .select(KingDeeMaterial::getId,KingDeeMaterial::getName,KingDeeMaterial::getCategoryName,KingDeeMaterial::getNumber,
                        KingDeeMaterial::getOldNumber,
                        KingDeeMaterial::getSpecification)
                .list();

        List<String> ids = kingDeeMaterials.stream().map(KingDeeMaterial::getId).collect(Collectors.toList());

        List<Stock> stocks = stockService.lambdaQuery()
                .in(Stock::getMaterialId,ids)
                .select(Stock::getId,Stock::getMaterialId,Stock::getStockQty,Stock::getStockId,Stock::getSyncTime,
                        Stock::getSupplyOrgId)
                .list();

        List<KingDeePurchaseStockIn> kingDeePurchaseStockIns = kingDeePurchaseStockInService.lambdaQuery()
                .in(KingDeePurchaseStockIn::getApproverId, appName)
                .ge(KingDeePurchaseStockIn::getApproveDate, date)
                .select(KingDeePurchaseStockIn::getId,KingDeePurchaseStockIn::getApproverId,KingDeePurchaseStockIn::getApproveDate,
                        KingDeePurchaseStockIn::getSupplyName,
                        KingDeePurchaseStockIn::getSyncTime)
                .list();


        List<KingDeePurchaseStockOut> kingDeePurchaseStockOutList = kingDeePurchaseStockOutService.lambdaQuery()
                .in(KingDeePurchaseStockOut::getApproverId, appName)

                .ge(KingDeePurchaseStockOut::getApproveDate, date)
                .select(KingDeePurchaseStockOut::getId,KingDeePurchaseStockOut::getApproverId,KingDeePurchaseStockOut::getApproveDate,
                        KingDeePurchaseStockOut::getSyncTime)
                .list();



        List<SalesOrder> salesOrders = salesOrderService.lambdaQuery().ge(SalesOrder::getCreateTime, date).or().ge(SalesOrder::getApprovalTime,date)
                .or().eq(SalesOrder::getExamineStatus,"WAIT")
                .select(SalesOrder::getId,SalesOrder::getSalesOrderNo,SalesOrder::getExamineStatus,SalesOrder::getSalesOrderNo,SalesOrder::getOrderNo,
                        SalesOrder::getApprovalTime,SalesOrder::getCorrespondName)
                .list();

        List<String> salesId = salesOrders.stream().map(SalesOrder::getId).collect(Collectors.toList());

        List<SalesOrderItem> salesOrderItemList = salesOrderItemService.lambdaQuery()
                .in(SalesOrderItem::getSalesOrderNo,salesId)
                .in(SalesOrderItem::getMaterialId, ids)
                .select(SalesOrderItem::getRefundableQty,SalesOrderItem::getSalesOrderNo,SalesOrderItem::getMaterialId)
                .list();

        List<KingDeeStock> kingDeeStockList = kingDeeStockService.lambdaQuery().list();

        //调拨单
        List<KingDeeTransfer> kingDeeTransfers = kingDeeTransferService.lambdaQuery().ge(KingDeeTransfer::getApproveDate, date).list();
        List<String> kingTransfersId = kingDeeTransfers.stream().map(KingDeeTransfer::getId).collect(Collectors.toList());
        List<KingDeeTransferItem> kingDeeTransferItemList = new ArrayList<>();
        if (!CollectionUtils.isEmpty(kingTransfersId)) {
            kingDeeTransferItemList = kingDeeTransferItemService.lambdaQuery()
                    .in(KingDeeTransferItem::getFId, kingTransfersId)
                    .list();
        }


        List<String> stockNames=new ArrayList<>();
        stockNames.add("芦苞仓");
        stockNames.add("三水仓");
        stockNames.add("三水珠海仓");
        stockNames.add("芦苞次品仓");

        List<String> transferidS=new ArrayList<>();
        transferidS.add("ZJDB000399");
        transferidS.add("ZJDB000398");
        transferidS.add("ZJDB000397");
        transferidS.add("ZJDB000396");

        List<StockStartAccVO> stockStartAccVOS = new ArrayList<>();


        for (StockStartAcc stockStartAcc : list) {



            //获得物料
            KingDeeMaterial kingDeeMaterial = kingDeeMaterials.stream().filter(item -> item.getNumber().equals(stockStartAcc.getMaterialNum()))
                    .collect(Collectors.toList()).get(0);

            KingDeeStock kingDeeStock = kingDeeStockList.stream().filter(item -> item.getName().equals(stockStartAcc.getStockName()))
                    .collect(Collectors.toList()).get(0);

            StockStartAccVO stockStartAccVO = new StockStartAccVO();
            stockStartAccVO.setStockName(kingDeeStock.getName());
            stockStartAccVO.setCategoryName(kingDeeMaterial.getCategoryName());
            stockStartAccVO.setMaterialNumber(kingDeeMaterial.getNumber());
            stockStartAccVO.setMaterialName(kingDeeMaterial.getName());
            stockStartAccVO.setMaterialOldNumber(kingDeeMaterial.getOldNumber());
            stockStartAccVO.setSpec(kingDeeMaterial.getSpecification());



            //判断佛山广州仓库得期初
            if (stockNames.contains(stockStartAcc.getStockName())){
                stockStartAccVO.setGzNumber(Convert.toBigDecimal(stockStartAcc.getNum()));
                stockStartAccVO.setFsNumber(Convert.toBigDecimal(0));
                stockStartAccVO.setGzNeverNumber(Convert.toBigDecimal(0));
                stockStartAccVO.setFsNeverNumber(Convert.toBigDecimal(0));
            }else {
                stockStartAccVO.setGzNumber(Convert.toBigDecimal(0));
                stockStartAccVO.setFsNumber(Convert.toBigDecimal(stockStartAcc.getNum()));
                stockStartAccVO.setGzNeverNumber(Convert.toBigDecimal(0));
                stockStartAccVO.setFsNeverNumber(Convert.toBigDecimal(0));
            }

            //调拨单查询 调入
            List<KingDeeTransferItem> kingDeeTransferItems = kingDeeTransferItemList.stream()
                    .filter(item -> item.getDestStockName().equals(kingDeeStock.getName()) && item.getMaterialId().equals(kingDeeMaterial.getId())).collect(Collectors.toList());

            for (KingDeeTransferItem kingDeeTransferItem : kingDeeTransferItems) {
                KingDeeTransfer kingDeeTransfer = kingDeeTransfers.stream().filter(item -> item.getId().equals(kingDeeTransferItem.getFId())).collect(Collectors.toList()).get(0);

                if (FS.contains(kingDeeTransfer.getApproverId()) || transferidS.contains(kingDeeTransfer.getBillNo())){
                    stockStartAccVO.setFsNumber(stockStartAccVO.getFsNumber().add(kingDeeTransferItem.getQty()));
                }else {
                    stockStartAccVO.setGzNumber(stockStartAccVO.getGzNumber().add(kingDeeTransferItem.getQty()));
                }
            }

            //调拨单查询 调出
            List<KingDeeTransferItem> kingDeeTransferItemsOut = kingDeeTransferItemList.stream()
                    .filter(item -> item.getSrcStockName().equals(kingDeeStock.getName()) && item.getMaterialId().equals(kingDeeMaterial.getId()))
                    .collect(Collectors.toList());

            for (KingDeeTransferItem kingDeeTransferItem : kingDeeTransferItemsOut) {
                KingDeeTransfer kingDeeTransfer = kingDeeTransfers.stream().filter(item -> item.getId().equals(kingDeeTransferItem.getFId())).collect(Collectors.toList()).get(0);
                if (FS.contains(kingDeeTransfer.getApproverId()) || transferidS.contains(kingDeeTransfer.getBillNo())){
                    stockStartAccVO.setFsNumber(stockStartAccVO.getFsNumber().subtract(kingDeeTransferItem.getQty()));
                }else {
                    stockStartAccVO.setGzNumber(stockStartAccVO.getGzNumber().subtract(kingDeeTransferItem.getQty()));
                }
            }



            //销售单 广州销售
            List<SalesOrder> salesOrderList = salesOrders.stream().filter(item -> item.getCorrespondName().equals(kingDeeStock.getName())
                    && item.getOrderNo().indexOf("GZ") != -1
            ).collect(Collectors.toList());

            List<String> salesIds = salesOrderList.stream().map(SalesOrder::getId).collect(Collectors.toList());
            List<SalesOrderItem> salesOrderItems = salesOrderItemList.stream()
                    .filter(item -> salesIds.contains(item.getSalesOrderNo()) &&
                            item.getMaterialId().equals(kingDeeMaterial.getId())).collect(Collectors.toList());

            for (SalesOrderItem salesOrderItem : salesOrderItems) {

                SalesOrder salesOrder = salesOrderList.stream().filter(item -> item.getId().equals(salesOrderItem.getSalesOrderNo())).collect(Collectors.toList()).get(0);

                if (!salesOrder.getExamineStatus().equals("OK"))
                    stockStartAccVO.setGzNeverNumber(stockStartAccVO.getGzNeverNumber().add(Convert.toBigDecimal(salesOrderItem.getRefundableQty())));
                else
                    stockStartAccVO.setGzNumber(stockStartAccVO.getGzNumber().subtract(Convert.toBigDecimal(salesOrderItem.getRefundableQty())));
            }


            //销售单 佛山销售
            List<SalesOrder> salesOrderListFS = salesOrders.stream().filter(item -> item.getCorrespondName().equals(kingDeeStock.getName())
                    && item.getOrderNo().indexOf("FS") != -1
            ).collect(Collectors.toList());
            List<String> salesIdsFS = salesOrderListFS.stream().map(SalesOrder::getId).collect(Collectors.toList());
            List<SalesOrderItem> salesOrderItemsFS = salesOrderItemList.stream()
                    .filter(item -> salesIdsFS.contains(item.getSalesOrderNo()) &&
                            item.getMaterialId().equals(kingDeeMaterial.getId())).collect(Collectors.toList());

            for (SalesOrderItem salesOrderItem : salesOrderItemsFS) {

                SalesOrder salesOrder = salesOrderListFS.stream().filter(item -> item.getId().equals(salesOrderItem.getSalesOrderNo()))
                        .collect(Collectors.toList()).get(0);

                if (!salesOrder.getExamineStatus().equals("OK"))
                    stockStartAccVO.setFsNeverNumber(stockStartAccVO.getFsNeverNumber().add(Convert.toBigDecimal(salesOrderItem.getRefundableQty())));
                else
                    stockStartAccVO.setFsNumber(stockStartAccVO.getFsNumber().subtract(Convert.toBigDecimal(salesOrderItem.getRefundableQty())));
            }




            List<StockAcc> stockAccList = stockAccs.stream()
                    .filter(item -> item.getMaterialId().equals(kingDeeMaterial.getId())
                            && item.getStockId().equals(kingDeeStock.getId()))
                    .collect(Collectors.toList());

            if (CollectionUtils.isEmpty(stockAccList)){
                stockStartAccVO.setGzOnNumber(stockStartAccVO.getGzNumber().subtract(stockStartAccVO.getGzNeverNumber()));
                stockStartAccVO.setFsOnNumber(stockStartAccVO.getFsNumber().subtract(stockStartAccVO.getFsNeverNumber()));

                stockStartAccVO.setAllNumber(stockStartAccVO.getFsNumber().add(stockStartAccVO.getGzNumber()));
                stockStartAccVO.setAllNeverNumber(stockStartAccVO.getFsNeverNumber().add(stockStartAccVO.getGzNeverNumber()));
                stockStartAccVO.setAllOnNumber(stockStartAccVO.getFsOnNumber().add(stockStartAccVO.getGzOnNumber()));



                stockStartAccVOS.add(stockStartAccVO);
                continue;
            }


            List<String> refList = stockAccList.stream().map(StockAcc::getRef).collect(Collectors.toList());


            //采购入库单 广州入库
            List<KingDeePurchaseStockIn> kingDeePurchaseStockInList =
                    kingDeePurchaseStockIns.stream().filter(item -> refList.contains(item.getId())
                            && GZ.contains(item.getApproverId())
                    ).collect(Collectors.toList());
            List<String> kingIns = kingDeePurchaseStockInList.stream().map(KingDeePurchaseStockIn::getId).collect(Collectors.toList());

            List<StockAcc> stockPurchases = stockAccList.stream().filter(item -> kingIns.contains(item.getRef()) && item.getRefType().equals("采购入库单")

            ).collect(Collectors.toList());

            for (StockAcc stockPurchase : stockPurchases) {
                if (stockPurchase.getDirectFlag())
                    stockStartAccVO.setGzNumber(stockStartAccVO.getGzNumber().add(stockPurchase.getStockChangeQty()));
                else
                    stockStartAccVO.setGzNumber(stockStartAccVO.getGzNumber().subtract(stockPurchase.getStockChangeQty()));

            }

            //采购入库单 佛山入库
            List<KingDeePurchaseStockIn> kingDeePurchaseStockInListFo =
                    kingDeePurchaseStockIns.stream().filter(item -> refList.contains(item.getId())
                            && FS.contains(item.getApproverId())
                    ).collect(Collectors.toList());
            List<String> kingInsFO = kingDeePurchaseStockInListFo.stream().map(KingDeePurchaseStockIn::getId).collect(Collectors.toList());

            List<StockAcc> stockPurchasesFO = stockAccList.stream().filter(item -> kingInsFO.contains(item.getRef())
                    && item.getRefType().equals("采购入库单")

            ).collect(Collectors.toList());

            for (StockAcc stockPurchase : stockPurchasesFO) {
                if (stockPurchase.getDirectFlag())
                    stockStartAccVO.setFsNumber(stockStartAccVO.getFsNumber().add(stockPurchase.getStockChangeQty()));
                else
                    stockStartAccVO.setFsNumber(stockStartAccVO.getFsNumber().subtract(stockPurchase.getStockChangeQty()));
            }



            //采购退料单 广州退料
            List<KingDeePurchaseStockOut> purchaseStockOuts =
                    kingDeePurchaseStockOutList.stream().filter(item -> refList.contains(item.getId()) &&
                            GZ.contains(item.getApproverId())
                    ).collect(Collectors.toList());

            List<String> kingOuts = purchaseStockOuts.stream().map(KingDeePurchaseStockOut::getId).collect(Collectors.toList());

            List<StockAcc> stockAccOutList = stockAccList.stream().filter(item -> kingOuts.contains(item.getRef()) && item.getRefType().equals("采购退料单")
            ).collect(Collectors.toList());

            for (StockAcc stockAcc : stockAccOutList) {
                stockStartAccVO.setGzNumber(stockStartAccVO.getGzNumber().subtract(stockAcc.getStockChangeQty()));
            }

            //采购退料单 佛山退料
            List<KingDeePurchaseStockOut> purchaseStockOutsFo =
                    kingDeePurchaseStockOutList.stream().filter(item -> refList.contains(item.getId()) &&
                            FS.contains(item.getApproverId())
                    ).collect(Collectors.toList());

            List<String> kingOutsFO = purchaseStockOutsFo.stream().map(KingDeePurchaseStockOut::getId).collect(Collectors.toList());

            List<StockAcc> stockAccOutListFO = stockAccList.stream().filter(item -> kingOutsFO.contains(item.getRef()) && item.getRefType().equals("采购退料单")
            ).collect(Collectors.toList());

            for (StockAcc stockAcc : stockAccOutListFO) {
                stockStartAccVO.setFsNumber(stockStartAccVO.getFsNumber().subtract(stockAcc.getStockChangeQty()));
            }






            stockStartAccVO.setGzOnNumber(stockStartAccVO.getGzNumber().subtract(stockStartAccVO.getGzNeverNumber()));
            stockStartAccVO.setFsOnNumber(stockStartAccVO.getFsNumber().subtract(stockStartAccVO.getFsNeverNumber()));


            stockStartAccVO.setAllNumber(stockStartAccVO.getFsNumber().add(stockStartAccVO.getGzNumber()));
            stockStartAccVO.setAllNeverNumber(stockStartAccVO.getFsNeverNumber().add(stockStartAccVO.getGzNeverNumber()));
            stockStartAccVO.setAllOnNumber(stockStartAccVO.getFsOnNumber().add(stockStartAccVO.getGzOnNumber()));


            stockStartAcc.setFsNumber(stockStartAccVO.getFsNumber());
            stockStartAcc.setGzNumber(stockStartAccVO.getGzNumber());

            stockStartAccVOS.add(stockStartAccVO);

        }


        /*   stockStartAccService.updateBatchById(list);*/

        IPage<StockStartAccVO> stockStartAccVOIPage = new Page<>();
        stockStartAccVOIPage.setTotal(page.getTotal());
        stockStartAccVOIPage.setRecords(stockStartAccVOS);
        stockStartAccVOIPage.setCurrent(page.getCurrent());
        stockStartAccVOIPage.setSize(page.getSize());
        stockStartAccVOIPage.setPages(page.getPages());


        return stockStartAccVOIPage;

    }

    public void listDuiBi() {
        ZfireParamBean zfireParamBean = new ZfireParamBean();
        zfireParamBean.setPageNum(1);
        zfireParamBean.setPageSize(-1);
        IPage<StockStartAccVO> stockStartAccVOIPage = this.list(null,null,null,null,-1,1);
        IPage<StockBeanToDay> list = this.listStockToDayV2(zfireParamBean);

        List<StockStartAccVO> stockStartAccVOS = new ArrayList<>();

        for (StockStartAccVO record : stockStartAccVOIPage.getRecords()) {
            List<StockBeanToDay> collect = list.getRecords().stream().filter(item -> item.getCorrespondName().equals(record.getStockName()) &&
                    item.getMaterialNumber().equals(record.getMaterialNumber())).collect(Collectors.toList());
            if (CollectionUtils.isEmpty(collect)) {
                stockStartAccVOS.add(record);
                continue;
            }

            StockBeanToDay stockBeanToDay = collect.get(0);

            if (!stockBeanToDay.getBalanceNumber().equals(Convert.toInt(record.getAllNumber()))) {
                stockStartAccVOS.add(record);
            }
        }

        List<String> collect = stockStartAccVOS.stream().map(StockStartAccVO::getMaterialNumber).collect(Collectors.toList());
        List<String> stockNames = stockStartAccVOS.stream().map(StockStartAccVO::getStockName).collect(Collectors.toList());

  /*      List<StockStartAcc> re = stockStartAccService.lambdaQuery()
                .in(StockStartAcc::getMaterialNum,collect)
                .in(StockStartAcc::getStockName,stockNames)

                .list();


        this.addAccSync(re);*/

        log.info(stockStartAccVOS.toString());
    }



    public IPage<StockBeanToDay> listStockToDayV2(ZfireParamBean zfireParamBean) {
        AdminUserCom adminUser = commonLogic.getAdminUser();
        zfireParamBean = FieldUtils.supplyParam(zfireParamBean);
        IPage<StockBeanToDay> stockBeanToDayIPage = stockCommonMapper.listStockToDayV2(new Page(zfireParamBean.getPageNum(), zfireParamBean.getPageSize()), zfireParamBean, null);
        List<String> collect = stockBeanToDayIPage.getRecords().stream().map(StockBeanToDay::getCorrespondId).collect(Collectors.toList());

        List<StockBean> num = stockCommonMapper.countAllList(null);
        List<StockBean> reservedNum = stockCommonMapper.countReservedList(collect);
        for (StockBeanToDay record : stockBeanToDayIPage.getRecords()) {
            //开单未提数量
            //开单未提数量
            List<StockBean> collectNum = num.stream()
                    .filter(item -> (item.getMaterialId().equals(record.getMaterialId()) && StringUtils.isNotEmpty(record.getCorrespondId()) &&  item.getCorrespondId().equals(record.getCorrespondId())))
                    .collect(Collectors.toList());
            if (CollectionUtils.isNotEmpty(collectNum))
                record.setNum(collectNum.stream().mapToInt(StockBean::getNum).sum());
            else
                record.setNum(0);
            record.setStockAdequate(record.getBalanceNumber() - record.getNum());

            //经销商预留库存
            List<StockBean> collectReservedNum = reservedNum.stream()
                    .filter(item -> (item.getMaterialId().equals(record.getMaterialId()) && StringUtils.isEmpty(record.getCorrespondId())) ||
                            (item.getMaterialId().equals(record.getMaterialId()) && StringUtils.isNotEmpty(record.getCorrespondId()) &&  record.getCorrespondId().contains(item.getCorrespondId())))
                    .collect(Collectors.toList());
            if (CollectionUtils.isNotEmpty(collectReservedNum))
                record.setReservedNum(collectReservedNum.stream().mapToInt(StockBean::getReservedNum).sum());
            else
                record.setReservedNum(0);
        }
        return stockBeanToDayIPage;
    }



    private IPage<StockStartAccVO> addAccSync(List<StockStartAcc> page) {
        List<StockStartAcc> list = page;

        if (CollectionUtils.isEmpty(list))
            return new Page<>();

        List<String> numbers = list.stream().map(StockStartAcc::getMaterialNum).collect(Collectors.toList());

        List<String> appName=new ArrayList<>();
        appName.add("成思慧");
        appName.add("张辉");
        appName.add("温锋");
        appName.add("梁静雯");

        List<String> GZ=new ArrayList<>();
        GZ.add("张辉");
        GZ.add("温锋");

        List<String> FS=new ArrayList<>();
        FS.add("成思慧");
        FS.add("梁静雯");

        String dateStr = "2024-01-26";
        Date date = DateUtil.parse(dateStr);
       Date endDate = DateUtil.beginOfDay(new Date());

        List<StockAcc> stockAccs = stockAccService.lambdaQuery()
                .ge(StockAcc::getChangeTime,date)
                .le(StockAcc::getChangeTime,endDate)
                .in(StockAcc::getRefType,"采购退料单","采购入库单")
                .select(StockAcc::getChangeTime,StockAcc::getRef,StockAcc::getStockChangeQty,StockAcc::getRefType,StockAcc::getMaterialId,
                        StockAcc::getStockId,StockAcc::getDirectFlag)
                .list();

        List<KingDeeMaterial> kingDeeMaterials = kingDeeMaterialService.lambdaQuery()
                .in(KingDeeMaterial::getNumber,numbers)
                .select(KingDeeMaterial::getId,KingDeeMaterial::getName,KingDeeMaterial::getCategoryName,KingDeeMaterial::getNumber,
                        KingDeeMaterial::getOldNumber,
                        KingDeeMaterial::getSpecification)
                .list();

        List<String> ids = kingDeeMaterials.stream().map(KingDeeMaterial::getId).collect(Collectors.toList());

        List<Stock> stocks = stockService.lambdaQuery()
                .in(Stock::getMaterialId,ids)
                .select(Stock::getId,Stock::getMaterialId,Stock::getStockQty,Stock::getStockId,Stock::getSyncTime,
                        Stock::getSupplyOrgId)
                .list();

        List<KingDeePurchaseStockIn> kingDeePurchaseStockIns = kingDeePurchaseStockInService.lambdaQuery()
                .in(KingDeePurchaseStockIn::getApproverId, appName)
                .ge(KingDeePurchaseStockIn::getApproveDate, date)
                .le(KingDeePurchaseStockIn::getApproveDate, endDate)
                .select(KingDeePurchaseStockIn::getId,KingDeePurchaseStockIn::getApproverId,KingDeePurchaseStockIn::getApproveDate,
                        KingDeePurchaseStockIn::getSupplyName,
                        KingDeePurchaseStockIn::getSyncTime)
                .list();


        List<KingDeePurchaseStockOut> kingDeePurchaseStockOutList = kingDeePurchaseStockOutService.lambdaQuery()
                .in(KingDeePurchaseStockOut::getApproverId, appName)

                .ge(KingDeePurchaseStockOut::getApproveDate, date)
                .le(KingDeePurchaseStockOut::getApproveDate, endDate)
                .select(KingDeePurchaseStockOut::getId,KingDeePurchaseStockOut::getApproverId,KingDeePurchaseStockOut::getApproveDate,
                        KingDeePurchaseStockOut::getSyncTime)
                .list();



        List<SalesOrder> salesOrders = salesOrderService.lambdaQuery()
                .le(SalesOrder::getCreateTime,endDate)
                .and(item ->  item.ge(SalesOrder::getCreateTime, date)
                .or().ge(SalesOrder::getApprovalTime,date))
                .select(SalesOrder::getId,SalesOrder::getSalesOrderNo,SalesOrder::getExamineStatus,SalesOrder::getSalesOrderNo,SalesOrder::getOrderNo,
                        SalesOrder::getApprovalTime,SalesOrder::getCorrespondName)
                .list();

        List<String> salesId = salesOrders.stream().map(SalesOrder::getId).collect(Collectors.toList());

        List<SalesOrderItem> salesOrderItemList = salesOrderItemService.lambdaQuery()
                .in(SalesOrderItem::getSalesOrderNo,salesId)
                .in(SalesOrderItem::getMaterialId, ids)
                .select(SalesOrderItem::getRefundableQty,SalesOrderItem::getSalesOrderNo,SalesOrderItem::getMaterialId)
                .list();

        List<KingDeeStock> kingDeeStockList = kingDeeStockService.lambdaQuery().list();

        //调拨单
        List<KingDeeTransfer> kingDeeTransfers = kingDeeTransferService.lambdaQuery().ge(KingDeeTransfer::getApproveDate, date)
                .le(KingDeeTransfer::getApproveDate, endDate)
                .list();
        List<String> kingTransfersId = kingDeeTransfers.stream().map(KingDeeTransfer::getId).collect(Collectors.toList());
        List<KingDeeTransferItem> kingDeeTransferItemList = new ArrayList<>();
        if (!CollectionUtils.isEmpty(kingTransfersId)) {
            kingDeeTransferItemList = kingDeeTransferItemService.lambdaQuery()
                    .in(KingDeeTransferItem::getFId, kingTransfersId)
                    .list();
        }


        List<String> stockNames=new ArrayList<>();
        stockNames.add("芦苞仓");
        stockNames.add("三水仓");
        stockNames.add("三水珠海仓");
        stockNames.add("芦苞次品仓");

        List<String> transferidS=new ArrayList<>();
        transferidS.add("ZJDB000399");
        transferidS.add("ZJDB000398");
        transferidS.add("ZJDB000397");
        transferidS.add("ZJDB000396");

        List<StockStartAccVO> stockStartAccVOS = new ArrayList<>();


        for (StockStartAcc stockStartAcc : list) {



            //获得物料
            KingDeeMaterial kingDeeMaterial = kingDeeMaterials.stream().filter(item -> item.getNumber().equals(stockStartAcc.getMaterialNum()))
                    .collect(Collectors.toList()).get(0);

            KingDeeStock kingDeeStock = kingDeeStockList.stream().filter(item -> item.getName().equals(stockStartAcc.getStockName()))
                    .collect(Collectors.toList()).get(0);

            StockStartAccVO stockStartAccVO = new StockStartAccVO();
            stockStartAccVO.setStockName(kingDeeStock.getName());
            stockStartAccVO.setCategoryName(kingDeeMaterial.getCategoryName());
            stockStartAccVO.setMaterialNumber(kingDeeMaterial.getNumber());
            stockStartAccVO.setMaterialName(kingDeeMaterial.getName());
            stockStartAccVO.setMaterialOldNumber(kingDeeMaterial.getOldNumber());
            stockStartAccVO.setSpec(kingDeeMaterial.getSpecification());




            //判断佛山广州仓库得期初
            if (stockNames.contains(stockStartAcc.getStockName())){
                stockStartAccVO.setGzNeverNumber(Convert.toBigDecimal(0));
                stockStartAccVO.setFsNeverNumber(Convert.toBigDecimal(0));
                stockStartAccVO.setGzNumber(Convert.toBigDecimal(stockStartAcc.getNum()));
                stockStartAccVO.setFsNumber(Convert.toBigDecimal(0));

            }else {
                stockStartAccVO.setFsNeverNumber(Convert.toBigDecimal(0));
                stockStartAccVO.setGzNeverNumber(Convert.toBigDecimal(0));
                stockStartAccVO.setFsNumber(Convert.toBigDecimal(stockStartAcc.getNum()));
                stockStartAccVO.setGzNumber(Convert.toBigDecimal(0));

            }

            //调拨单查询 调入
            List<KingDeeTransferItem> kingDeeTransferItems = kingDeeTransferItemList.stream()
                    .filter(item -> item.getDestStockName().equals(kingDeeStock.getName()) && item.getMaterialId().equals(kingDeeMaterial.getId())).collect(Collectors.toList());

            for (KingDeeTransferItem kingDeeTransferItem : kingDeeTransferItems) {
                KingDeeTransfer kingDeeTransfer = kingDeeTransfers.stream().filter(item -> item.getId().equals(kingDeeTransferItem.getFId())).collect(Collectors.toList()).get(0);

                if (FS.contains(kingDeeTransfer.getApproverId()) || transferidS.contains(kingDeeTransfer.getBillNo())){
                    stockStartAccVO.setFsNumber(stockStartAccVO.getFsNumber().add(kingDeeTransferItem.getQty()));
                }else {
                    stockStartAccVO.setGzNumber(stockStartAccVO.getGzNumber().add(kingDeeTransferItem.getQty()));
                }
            }

            //调拨单查询 调出
            List<KingDeeTransferItem> kingDeeTransferItemsOut = kingDeeTransferItemList.stream()
                    .filter(item -> item.getSrcStockName().equals(kingDeeStock.getName()) && item.getMaterialId().equals(kingDeeMaterial.getId()))
                    .collect(Collectors.toList());

            for (KingDeeTransferItem kingDeeTransferItem : kingDeeTransferItemsOut) {
                KingDeeTransfer kingDeeTransfer = kingDeeTransfers.stream().filter(item -> item.getId().equals(kingDeeTransferItem.getFId())).collect(Collectors.toList()).get(0);
                if (FS.contains(kingDeeTransfer.getApproverId()) || transferidS.contains(kingDeeTransfer.getBillNo())){
                    stockStartAccVO.setFsNumber(stockStartAccVO.getFsNumber().subtract(kingDeeTransferItem.getQty()));
                }else {
                    stockStartAccVO.setGzNumber(stockStartAccVO.getGzNumber().subtract(kingDeeTransferItem.getQty()));
                }
            }



            //销售单 广州销售
            List<SalesOrder> salesOrderList = salesOrders.stream().filter(item -> item.getCorrespondName().equals(kingDeeStock.getName())
                    && item.getOrderNo().indexOf("GZ") != -1
            ).collect(Collectors.toList());

            List<String> salesIds = salesOrderList.stream().map(SalesOrder::getId).collect(Collectors.toList());
            List<SalesOrderItem> salesOrderItems = salesOrderItemList.stream()
                    .filter(item -> salesIds.contains(item.getSalesOrderNo()) &&
                            item.getMaterialId().equals(kingDeeMaterial.getId())).collect(Collectors.toList());

            for (SalesOrderItem salesOrderItem : salesOrderItems) {

                SalesOrder salesOrder = salesOrderList.stream().filter(item -> item.getId().equals(salesOrderItem.getSalesOrderNo())).collect(Collectors.toList()).get(0);

                if (!salesOrder.getExamineStatus().equals("OK"))
                    stockStartAccVO.setGzNeverNumber(stockStartAccVO.getGzNeverNumber().add(Convert.toBigDecimal(salesOrderItem.getRefundableQty())));
                else
                    stockStartAccVO.setGzNumber(stockStartAccVO.getGzNumber().subtract(Convert.toBigDecimal(salesOrderItem.getRefundableQty())));
            }


            //销售单 佛山销售
            List<SalesOrder> salesOrderListFS = salesOrders.stream().filter(item -> item.getCorrespondName().equals(kingDeeStock.getName())
                    && item.getOrderNo().indexOf("FS") != -1
            ).collect(Collectors.toList());
            List<String> salesIdsFS = salesOrderListFS.stream().map(SalesOrder::getId).collect(Collectors.toList());
            List<SalesOrderItem> salesOrderItemsFS = salesOrderItemList.stream()
                    .filter(item -> salesIdsFS.contains(item.getSalesOrderNo()) &&
                            item.getMaterialId().equals(kingDeeMaterial.getId())).collect(Collectors.toList());

            for (SalesOrderItem salesOrderItem : salesOrderItemsFS) {

                SalesOrder salesOrder = salesOrderListFS.stream().filter(item -> item.getId().equals(salesOrderItem.getSalesOrderNo()))
                        .collect(Collectors.toList()).get(0);

                if (!salesOrder.getExamineStatus().equals("OK"))
                    stockStartAccVO.setFsNeverNumber(stockStartAccVO.getFsNeverNumber().add(Convert.toBigDecimal(salesOrderItem.getRefundableQty())));
                else
                    stockStartAccVO.setFsNumber(stockStartAccVO.getFsNumber().subtract(Convert.toBigDecimal(salesOrderItem.getRefundableQty())));
            }




            List<StockAcc> stockAccList = stockAccs.stream()
                    .filter(item -> item.getMaterialId().equals(kingDeeMaterial.getId())
                            && item.getStockId().equals(kingDeeStock.getId()))
                    .collect(Collectors.toList());

            if (CollectionUtils.isEmpty(stockAccList)){
                stockStartAccVO.setGzOnNumber(stockStartAccVO.getGzNumber().subtract(stockStartAccVO.getGzNeverNumber()));
                stockStartAccVO.setFsOnNumber(stockStartAccVO.getFsNumber().subtract(stockStartAccVO.getFsNeverNumber()));

                stockStartAccVO.setAllNumber(stockStartAccVO.getFsNumber().add(stockStartAccVO.getGzNumber()));
                stockStartAccVO.setAllNeverNumber(stockStartAccVO.getFsNeverNumber().add(stockStartAccVO.getGzNeverNumber()));
                stockStartAccVO.setAllOnNumber(stockStartAccVO.getFsOnNumber().add(stockStartAccVO.getGzOnNumber()));


                stockStartAcc.setFsNumber(stockStartAccVO.getFsNumber());
                stockStartAcc.setGzNumber(stockStartAccVO.getGzNumber());

                stockStartAccVOS.add(stockStartAccVO);
                continue;
            }


            List<String> refList = stockAccList.stream().map(StockAcc::getRef).collect(Collectors.toList());


            //采购入库单 广州入库
            List<KingDeePurchaseStockIn> kingDeePurchaseStockInList =
                    kingDeePurchaseStockIns.stream().filter(item -> refList.contains(item.getId())
                            && GZ.contains(item.getApproverId())
                    ).collect(Collectors.toList());
            List<String> kingIns = kingDeePurchaseStockInList.stream().map(KingDeePurchaseStockIn::getId).collect(Collectors.toList());

            List<StockAcc> stockPurchases = stockAccList.stream().filter(item -> kingIns.contains(item.getRef()) && item.getRefType().equals("采购入库单")

            ).collect(Collectors.toList());

            for (StockAcc stockPurchase : stockPurchases) {
                if (stockPurchase.getDirectFlag())
                    stockStartAccVO.setGzNumber(stockStartAccVO.getGzNumber().add(stockPurchase.getStockChangeQty()));
                else
                    stockStartAccVO.setGzNumber(stockStartAccVO.getGzNumber().subtract(stockPurchase.getStockChangeQty()));

            }

            //采购入库单 佛山入库
            List<KingDeePurchaseStockIn> kingDeePurchaseStockInListFo =
                    kingDeePurchaseStockIns.stream().filter(item -> refList.contains(item.getId())
                            && FS.contains(item.getApproverId())
                    ).collect(Collectors.toList());
            List<String> kingInsFO = kingDeePurchaseStockInListFo.stream().map(KingDeePurchaseStockIn::getId).collect(Collectors.toList());

            List<StockAcc> stockPurchasesFO = stockAccList.stream().filter(item -> kingInsFO.contains(item.getRef()) && item.getRefType().equals("采购入库单")

            ).collect(Collectors.toList());

            for (StockAcc stockPurchase : stockPurchasesFO) {
                if (stockPurchase.getDirectFlag())
                    stockStartAccVO.setFsNumber(stockStartAccVO.getFsNumber().add(stockPurchase.getStockChangeQty()));
                else
                    stockStartAccVO.setFsNumber(stockStartAccVO.getFsNumber().subtract(stockPurchase.getStockChangeQty()));
            }



            //采购退料单 广州退料
            List<KingDeePurchaseStockOut> purchaseStockOuts =
                    kingDeePurchaseStockOutList.stream().filter(item -> refList.contains(item.getId()) &&
                            GZ.contains(item.getApproverId())
                    ).collect(Collectors.toList());

            List<String> kingOuts = purchaseStockOuts.stream().map(KingDeePurchaseStockOut::getId).collect(Collectors.toList());

            List<StockAcc> stockAccOutList = stockAccList.stream().filter(item -> kingOuts.contains(item.getRef()) && item.getRefType().equals("采购退料单")
            ).collect(Collectors.toList());

            for (StockAcc stockAcc : stockAccOutList) {
                if (stockAcc.getDirectFlag())
                    stockStartAccVO.setGzNumber(stockStartAccVO.getGzNumber().add(stockAcc.getStockChangeQty()));
                else
                    stockStartAccVO.setGzNumber(stockStartAccVO.getGzNumber().subtract(stockAcc.getStockChangeQty()));
            }

            //采购退料单 佛山退料
            List<KingDeePurchaseStockOut> purchaseStockOutsFo =
                    kingDeePurchaseStockOutList.stream().filter(item -> refList.contains(item.getId()) &&
                            FS.contains(item.getApproverId())
                    ).collect(Collectors.toList());

            List<String> kingOutsFO = purchaseStockOutsFo.stream().map(KingDeePurchaseStockOut::getId).collect(Collectors.toList());

            List<StockAcc> stockAccOutListFO = stockAccList.stream().filter(item -> kingOutsFO.contains(item.getRef()) && item.getRefType().equals("采购退料单")
            ).collect(Collectors.toList());

            for (StockAcc stockAcc : stockAccOutListFO) {
                if (stockAcc.getDirectFlag())
                    stockStartAccVO.setFsNumber(stockStartAccVO.getFsNumber().add(stockAcc.getStockChangeQty()));
                else
                    stockStartAccVO.setFsNumber(stockStartAccVO.getFsNumber().subtract(stockAcc.getStockChangeQty()));
            }






            stockStartAccVO.setGzOnNumber(stockStartAccVO.getGzNumber().subtract(stockStartAccVO.getGzNeverNumber()));
            stockStartAccVO.setFsOnNumber(stockStartAccVO.getFsNumber().subtract(stockStartAccVO.getFsNeverNumber()));


            stockStartAccVO.setAllNumber(stockStartAccVO.getFsNumber().add(stockStartAccVO.getGzNumber()));
            stockStartAccVO.setAllNeverNumber(stockStartAccVO.getFsNeverNumber().add(stockStartAccVO.getGzNeverNumber()));
            stockStartAccVO.setAllOnNumber(stockStartAccVO.getFsOnNumber().add(stockStartAccVO.getGzOnNumber()));


            stockStartAcc.setFsNumber(stockStartAccVO.getFsNumber());
            stockStartAcc.setGzNumber(stockStartAccVO.getGzNumber());

            stockStartAccVOS.add(stockStartAccVO);

        }


        stockStartAccService.updateBatchById(list);

        IPage<StockStartAccVO> stockStartAccVOIPage = new Page<>();

        stockStartAccVOIPage.setRecords(stockStartAccVOS);


        return stockStartAccVOIPage;
    }

    public void test4() {

        List<StockStartAcc> list = stockStartAccService.lambdaQuery()
                .list();



        IPage<StockStartAccVO> stockStartAccVOIPage = this.addAccSync(list);
    }

    private IPage<StockStartAccVO> addAccto(List<StockStartAcc> page) {
        List<StockStartAcc> list = page;

        if (CollectionUtils.isEmpty(list))
            return new Page<>();

        List<String> numbers = list.stream().map(StockStartAcc::getMaterialNum).collect(Collectors.toList());

        List<String> appName=new ArrayList<>();
        appName.add("成思慧");
        appName.add("张辉");
        appName.add("温锋");
        appName.add("梁静雯");

        List<String> GZ=new ArrayList<>();
        GZ.add("张辉");
        GZ.add("温锋");

        List<String> FS=new ArrayList<>();
        FS.add("成思慧");
        FS.add("梁静雯");

        String dateStr = "2024-05-08";
        Date date = DateUtil.parse(dateStr);

        DateTime endTime = DateUtil.beginOfDay(new Date());

        List<StockAcc> stockAccs = stockAccService.lambdaQuery()
                .ge(StockAcc::getChangeTime,date)
                .le(StockAcc::getChangeTime,endTime)
                .in(StockAcc::getRefType,"采购退料单","采购入库单")
                .select(StockAcc::getChangeTime,StockAcc::getRef,StockAcc::getStockChangeQty,StockAcc::getRefType,StockAcc::getMaterialId,
                        StockAcc::getStockId,StockAcc::getDirectFlag)
                .list();

        List<KingDeeMaterial> kingDeeMaterials = kingDeeMaterialService.lambdaQuery()
                .in(KingDeeMaterial::getNumber,numbers)
                .select(KingDeeMaterial::getId,KingDeeMaterial::getName,KingDeeMaterial::getCategoryName,KingDeeMaterial::getNumber,
                        KingDeeMaterial::getOldNumber,
                        KingDeeMaterial::getSpecification)
                .list();

        List<String> ids = kingDeeMaterials.stream().map(KingDeeMaterial::getId).collect(Collectors.toList());


        List<KingDeePurchaseStockIn> kingDeePurchaseStockIns = kingDeePurchaseStockInService.lambdaQuery()
                .in(KingDeePurchaseStockIn::getApproverId, appName)
                .ge(KingDeePurchaseStockIn::getApproveDate, date)
                .select(KingDeePurchaseStockIn::getId,KingDeePurchaseStockIn::getApproverId,KingDeePurchaseStockIn::getApproveDate,
                        KingDeePurchaseStockIn::getSupplyName,
                        KingDeePurchaseStockIn::getSyncTime)
                .list();


        List<KingDeePurchaseStockOut> kingDeePurchaseStockOutList = kingDeePurchaseStockOutService.lambdaQuery()
                .in(KingDeePurchaseStockOut::getApproverId, appName)

                .ge(KingDeePurchaseStockOut::getApproveDate, date)
                .select(KingDeePurchaseStockOut::getId,KingDeePurchaseStockOut::getApproverId,KingDeePurchaseStockOut::getApproveDate,
                        KingDeePurchaseStockOut::getSyncTime)
                .list();



        List<SalesOrder> salesOrders = salesOrderService.lambdaQuery()
                .and(item -> item.ge(SalesOrder::getCreateTime, date).or().ge(SalesOrder::getApprovalTime,date))
                .and(item -> item.le(SalesOrder::getApprovalName,endTime).or().le(SalesOrder::getApprovalTime,endTime))
                .select(SalesOrder::getId,SalesOrder::getSalesOrderNo,SalesOrder::getExamineStatus,SalesOrder::getSalesOrderNo,SalesOrder::getOrderNo,
                        SalesOrder::getApprovalTime,SalesOrder::getCorrespondName)
                .list();

        List<String> salesId = salesOrders.stream().map(SalesOrder::getId).collect(Collectors.toList());

        List<SalesOrderItem> salesOrderItemList = salesOrderItemService.lambdaQuery()
                .in(SalesOrderItem::getSalesOrderNo,salesId)
                .in(SalesOrderItem::getMaterialId, ids)
                .select(SalesOrderItem::getRefundableQty,SalesOrderItem::getSalesOrderNo,SalesOrderItem::getMaterialId)
                .list();

        List<KingDeeStock> kingDeeStockList = kingDeeStockService.lambdaQuery().list();

        //调拨单
        List<KingDeeTransfer> kingDeeTransfers = kingDeeTransferService.lambdaQuery().ge(KingDeeTransfer::getApproveDate, date)
                .le(KingDeeTransfer::getApproveDate,endTime)
                .list();
        List<String> kingTransfersId = kingDeeTransfers.stream().map(KingDeeTransfer::getId).collect(Collectors.toList());
        List<KingDeeTransferItem> kingDeeTransferItemList = new ArrayList<>();
        if (!CollectionUtils.isEmpty(kingTransfersId)) {
            kingDeeTransferItemList = kingDeeTransferItemService.lambdaQuery()
                    .in(KingDeeTransferItem::getFId, kingTransfersId)
                    .list();
        }


        List<String> stockNames=new ArrayList<>();
        stockNames.add("芦苞仓");
        stockNames.add("三水仓");
        stockNames.add("三水珠海仓");
        stockNames.add("芦苞次品仓");

        List<String> transferidS=new ArrayList<>();
        transferidS.add("ZJDB000399");
        transferidS.add("ZJDB000398");
        transferidS.add("ZJDB000397");
        transferidS.add("ZJDB000396");

        List<StockStartAccVO> stockStartAccVOS = new ArrayList<>();


        for (StockStartAcc stockStartAcc : list) {



            //获得物料
            KingDeeMaterial kingDeeMaterial = kingDeeMaterials.stream().filter(item -> item.getNumber().equals(stockStartAcc.getMaterialNum()))
                    .collect(Collectors.toList()).get(0);

            KingDeeStock kingDeeStock = kingDeeStockList.stream().filter(item -> item.getName().equals(stockStartAcc.getStockName()))
                    .collect(Collectors.toList()).get(0);

            StockStartAccVO stockStartAccVO = new StockStartAccVO();
            stockStartAccVO.setStockName(kingDeeStock.getName());
            stockStartAccVO.setCategoryName(kingDeeMaterial.getCategoryName());
            stockStartAccVO.setMaterialNumber(kingDeeMaterial.getNumber());
            stockStartAccVO.setMaterialName(kingDeeMaterial.getName());
            stockStartAccVO.setMaterialOldNumber(kingDeeMaterial.getOldNumber());
            stockStartAccVO.setSpec(kingDeeMaterial.getSpecification());

            stockStartAccVO.setFsNumber(stockStartAcc.getFsNumber());
            stockStartAccVO.setGzNumber(stockStartAcc.getGzNumber());

            //判断佛山广州仓库得期初
            if (stockNames.contains(stockStartAcc.getStockName())){
                stockStartAccVO.setGzNeverNumber(Convert.toBigDecimal(0));
                stockStartAccVO.setFsNeverNumber(Convert.toBigDecimal(0));

            }else {
                stockStartAccVO.setFsNeverNumber(Convert.toBigDecimal(0));
                stockStartAccVO.setGzNeverNumber(Convert.toBigDecimal(0));
            }

            //调拨单查询 调入
            List<KingDeeTransferItem> kingDeeTransferItems = kingDeeTransferItemList.stream()
                    .filter(item -> item.getDestStockName().equals(kingDeeStock.getName()) && item.getMaterialId().equals(kingDeeMaterial.getId())).collect(Collectors.toList());

            for (KingDeeTransferItem kingDeeTransferItem : kingDeeTransferItems) {
                KingDeeTransfer kingDeeTransfer = kingDeeTransfers.stream().filter(item -> item.getId().equals(kingDeeTransferItem.getFId())).collect(Collectors.toList()).get(0);

                if (FS.contains(kingDeeTransfer.getApproverId()) || transferidS.contains(kingDeeTransfer.getBillNo())){
                    stockStartAccVO.setFsNumber(stockStartAccVO.getFsNumber().add(kingDeeTransferItem.getQty()));
                }else {
                    stockStartAccVO.setGzNumber(stockStartAccVO.getGzNumber().add(kingDeeTransferItem.getQty()));
                }
            }

            //调拨单查询 调出
            List<KingDeeTransferItem> kingDeeTransferItemsOut = kingDeeTransferItemList.stream()
                    .filter(item -> item.getSrcStockName().equals(kingDeeStock.getName()) && item.getMaterialId().equals(kingDeeMaterial.getId()))
                    .collect(Collectors.toList());

            for (KingDeeTransferItem kingDeeTransferItem : kingDeeTransferItemsOut) {
                KingDeeTransfer kingDeeTransfer = kingDeeTransfers.stream().filter(item -> item.getId().equals(kingDeeTransferItem.getFId())).collect(Collectors.toList()).get(0);
                if (FS.contains(kingDeeTransfer.getApproverId()) || transferidS.contains(kingDeeTransfer.getBillNo())){
                    stockStartAccVO.setFsNumber(stockStartAccVO.getFsNumber().subtract(kingDeeTransferItem.getQty()));
                }else {
                    stockStartAccVO.setGzNumber(stockStartAccVO.getGzNumber().subtract(kingDeeTransferItem.getQty()));
                }
            }



            //销售单 广州销售
            List<SalesOrder> salesOrderList = salesOrders.stream().filter(item -> item.getCorrespondName().equals(kingDeeStock.getName())
                    && item.getOrderNo().indexOf("GZ") != -1
            ).collect(Collectors.toList());

            List<String> salesIds = salesOrderList.stream().map(SalesOrder::getId).collect(Collectors.toList());
            List<SalesOrderItem> salesOrderItems = salesOrderItemList.stream()
                    .filter(item -> salesIds.contains(item.getSalesOrderNo()) &&
                            item.getMaterialId().equals(kingDeeMaterial.getId())).collect(Collectors.toList());

            for (SalesOrderItem salesOrderItem : salesOrderItems) {

                SalesOrder salesOrder = salesOrderList.stream().filter(item -> item.getId().equals(salesOrderItem.getSalesOrderNo())).collect(Collectors.toList()).get(0);

                if (!salesOrder.getExamineStatus().equals("OK"))
                    stockStartAccVO.setGzNeverNumber(stockStartAccVO.getGzNeverNumber().add(Convert.toBigDecimal(salesOrderItem.getRefundableQty())));
                else
                    stockStartAccVO.setGzNumber(stockStartAccVO.getGzNumber().subtract(Convert.toBigDecimal(salesOrderItem.getRefundableQty())));
            }


            //销售单 佛山销售
            List<SalesOrder> salesOrderListFS = salesOrders.stream().filter(item -> item.getCorrespondName().equals(kingDeeStock.getName())
                    && item.getOrderNo().indexOf("FS") != -1
            ).collect(Collectors.toList());
            List<String> salesIdsFS = salesOrderListFS.stream().map(SalesOrder::getId).collect(Collectors.toList());
            List<SalesOrderItem> salesOrderItemsFS = salesOrderItemList.stream()
                    .filter(item -> salesIdsFS.contains(item.getSalesOrderNo()) &&
                            item.getMaterialId().equals(kingDeeMaterial.getId())).collect(Collectors.toList());

            for (SalesOrderItem salesOrderItem : salesOrderItemsFS) {

                SalesOrder salesOrder = salesOrderListFS.stream().filter(item -> item.getId().equals(salesOrderItem.getSalesOrderNo()))
                        .collect(Collectors.toList()).get(0);

                if (!salesOrder.getExamineStatus().equals("OK"))
                    stockStartAccVO.setFsNeverNumber(stockStartAccVO.getFsNeverNumber().add(Convert.toBigDecimal(salesOrderItem.getRefundableQty())));
                else
                    stockStartAccVO.setFsNumber(stockStartAccVO.getFsNumber().subtract(Convert.toBigDecimal(salesOrderItem.getRefundableQty())));
            }




            List<StockAcc> stockAccList = stockAccs.stream()
                    .filter(item -> item.getMaterialId().equals(kingDeeMaterial.getId())
                            && item.getStockId().equals(kingDeeStock.getId()))
                    .collect(Collectors.toList());

            if (CollectionUtils.isEmpty(stockAccList)){
                stockStartAccVO.setGzOnNumber(stockStartAccVO.getGzNumber().subtract(stockStartAccVO.getGzNeverNumber()));
                stockStartAccVO.setFsOnNumber(stockStartAccVO.getFsNumber().subtract(stockStartAccVO.getFsNeverNumber()));

                stockStartAccVO.setAllNumber(stockStartAccVO.getFsNumber().add(stockStartAccVO.getGzNumber()));
                stockStartAccVO.setAllNeverNumber(stockStartAccVO.getFsNeverNumber().add(stockStartAccVO.getGzNeverNumber()));
                stockStartAccVO.setAllOnNumber(stockStartAccVO.getFsOnNumber().add(stockStartAccVO.getGzOnNumber()));


                stockStartAcc.setFsNumber(stockStartAccVO.getFsNumber());
                stockStartAcc.setGzNumber(stockStartAccVO.getGzNumber());

                stockStartAccVOS.add(stockStartAccVO);
                continue;
            }


            List<String> refList = stockAccList.stream().map(StockAcc::getRef).collect(Collectors.toList());


            //采购入库单 广州入库
            List<KingDeePurchaseStockIn> kingDeePurchaseStockInList =
                    kingDeePurchaseStockIns.stream().filter(item -> refList.contains(item.getId())
                            && GZ.contains(item.getApproverId())
                    ).collect(Collectors.toList());
            List<String> kingIns = kingDeePurchaseStockInList.stream().map(KingDeePurchaseStockIn::getId).collect(Collectors.toList());

            List<StockAcc> stockPurchases = stockAccList.stream().filter(item -> kingIns.contains(item.getRef()) && item.getRefType().equals("采购入库单")

            ).collect(Collectors.toList());

            for (StockAcc stockPurchase : stockPurchases) {
                if (stockPurchase.getDirectFlag())
                    stockStartAccVO.setGzNumber(stockStartAccVO.getGzNumber().add(stockPurchase.getStockChangeQty()));
                else
                    stockStartAccVO.setGzNumber(stockStartAccVO.getGzNumber().subtract(stockPurchase.getStockChangeQty()));

            }

            //采购入库单 佛山入库
            List<KingDeePurchaseStockIn> kingDeePurchaseStockInListFo =
                    kingDeePurchaseStockIns.stream().filter(item -> refList.contains(item.getId())
                            && FS.contains(item.getApproverId())
                    ).collect(Collectors.toList());
            List<String> kingInsFO = kingDeePurchaseStockInListFo.stream().map(KingDeePurchaseStockIn::getId).collect(Collectors.toList());

            List<StockAcc> stockPurchasesFO = stockAccList.stream().filter(item -> kingInsFO.contains(item.getRef()) && item.getRefType().equals("采购入库单")

            ).collect(Collectors.toList());

            for (StockAcc stockPurchase : stockPurchasesFO) {
                if (stockPurchase.getDirectFlag())
                    stockStartAccVO.setFsNumber(stockStartAccVO.getFsNumber().add(stockPurchase.getStockChangeQty()));
                else
                    stockStartAccVO.setFsNumber(stockStartAccVO.getFsNumber().subtract(stockPurchase.getStockChangeQty()));
            }



            //采购退料单 广州退料
            List<KingDeePurchaseStockOut> purchaseStockOuts =
                    kingDeePurchaseStockOutList.stream().filter(item -> refList.contains(item.getId()) &&
                            GZ.contains(item.getApproverId())
                    ).collect(Collectors.toList());

            List<String> kingOuts = purchaseStockOuts.stream().map(KingDeePurchaseStockOut::getId).collect(Collectors.toList());

            List<StockAcc> stockAccOutList = stockAccList.stream().filter(item -> kingOuts.contains(item.getRef()) && item.getRefType().equals("采购退料单")
            ).collect(Collectors.toList());

            for (StockAcc stockAcc : stockAccOutList) {
                if (stockAcc.getDirectFlag())
                    stockStartAccVO.setGzNumber(stockStartAccVO.getGzNumber().add(stockAcc.getStockChangeQty()));
                else
                    stockStartAccVO.setGzNumber(stockStartAccVO.getGzNumber().subtract(stockAcc.getStockChangeQty()));
            }

            //采购退料单 佛山退料
            List<KingDeePurchaseStockOut> purchaseStockOutsFo =
                    kingDeePurchaseStockOutList.stream().filter(item -> refList.contains(item.getId()) &&
                            FS.contains(item.getApproverId())
                    ).collect(Collectors.toList());

            List<String> kingOutsFO = purchaseStockOutsFo.stream().map(KingDeePurchaseStockOut::getId).collect(Collectors.toList());

            List<StockAcc> stockAccOutListFO = stockAccList.stream().filter(item -> kingOutsFO.contains(item.getRef()) && item.getRefType().equals("采购退料单")
            ).collect(Collectors.toList());

            for (StockAcc stockAcc : stockAccOutListFO) {
                if (stockAcc.getDirectFlag())
                    stockStartAccVO.setFsNumber(stockStartAccVO.getFsNumber().add(stockAcc.getStockChangeQty()));
                else
                    stockStartAccVO.setFsNumber(stockStartAccVO.getFsNumber().subtract(stockAcc.getStockChangeQty()));
            }






            stockStartAccVO.setGzOnNumber(stockStartAccVO.getGzNumber().subtract(stockStartAccVO.getGzNeverNumber()));
            stockStartAccVO.setFsOnNumber(stockStartAccVO.getFsNumber().subtract(stockStartAccVO.getFsNeverNumber()));


            stockStartAccVO.setAllNumber(stockStartAccVO.getFsNumber().add(stockStartAccVO.getGzNumber()));
            stockStartAccVO.setAllNeverNumber(stockStartAccVO.getFsNeverNumber().add(stockStartAccVO.getGzNeverNumber()));
            stockStartAccVO.setAllOnNumber(stockStartAccVO.getFsOnNumber().add(stockStartAccVO.getGzOnNumber()));


            stockStartAcc.setFsNumber(stockStartAccVO.getFsNumber());
            stockStartAcc.setGzNumber(stockStartAccVO.getGzNumber());

            stockStartAccVOS.add(stockStartAccVO);

        }


        stockStartAccService.updateBatchById(list);

        IPage<StockStartAccVO> stockStartAccVOIPage = new Page<>();

        stockStartAccVOIPage.setRecords(stockStartAccVOS);


        return stockStartAccVOIPage;
    }
}
