package com.hys.app.service.erp.impl;

import cn.hutool.core.bean.BeanUtil;
import cn.hutool.core.collection.CollUtil;
import cn.hutool.core.io.IoUtil;
import cn.hutool.core.util.ObjectUtil;
import cn.hutool.core.util.StrUtil;
import cn.hutool.poi.excel.ExcelUtil;
import cn.hutool.poi.excel.ExcelWriter;
import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.hys.app.converter.erp.ProductStockConverter;
import com.hys.app.converter.erp.WarehouseOutConverter;
import com.hys.app.framework.context.user.AdminUserContext;
import com.hys.app.framework.database.BaseQueryParam;
import com.hys.app.framework.database.WebPage;
import com.hys.app.framework.database.mybatisplus.base.BaseServiceImpl;
import com.hys.app.framework.exception.ServiceException;
import com.hys.app.framework.rabbitmq.MessageSender;
import com.hys.app.framework.rabbitmq.MqMessage;
import com.hys.app.framework.util.BeanUtils;
import com.hys.app.framework.util.DateUtil;
import com.hys.app.framework.util.PageConvert;
import com.hys.app.mapper.erp.WarehouseOutMapper;
import com.hys.app.model.base.rabbitmq.AmqpExchange;
import com.hys.app.model.erp.dos.*;
import com.hys.app.model.erp.dto.*;
import com.hys.app.model.erp.dto.message.WarehouseOutShipMessage;
import com.hys.app.model.erp.enums.*;
import com.hys.app.model.erp.vo.*;
import com.hys.app.model.system.dos.AdminUser;
import com.hys.app.service.erp.*;
import com.hys.app.service.system.AdminUserManager;
import lombok.AllArgsConstructor;
import org.springframework.data.redis.core.RedisTemplate;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import javax.servlet.ServletOutputStream;
import javax.servlet.http.HttpServletResponse;
import java.io.IOException;
import java.util.*;
import java.util.stream.Collectors;

/**
 * 出库单业务层实现
 *
 * @author 张崧
 * @since 2023-12-07 16:50:20
 */
@Service
@AllArgsConstructor
public class WarehouseOutManagerImpl extends BaseServiceImpl<WarehouseOutMapper, WarehouseOutDO> implements WarehouseOutManager {

    private WarehouseOutConverter converter;
    private OrderManager orderManager;
    private NoGenerateManager noGenerateManager;
    private WarehouseOutItemManager warehouseOutItemManager;
    private WarehouseOutMapper warehouseOutMapper;
    private MessageSender messageSender;
    private AdminUserManager adminUserManager;
    private FinanceItemManager financeItemManager;
    private ProductStockManager productStockManager;
    private ProductManager productManager;
    private ProductStockConverter productStockConverter;
    private WarehouseManager warehouseManager;
    private AreaStockManager areaStockManager;
    private ItemSnCodeManager itemSnCodeManager;
    private StockDamageReportManager stockDamageReportManager;
    private LockedStockDetailManager lockedStockDetailManager;
    private RedisTemplate redisTemplate;

    @Override
    public WebPage<WarehouseOutVO> list(WarehouseOutQueryParams queryParams) {
        WebPage<WarehouseOutDO> webPage = baseMapper.selectPage(queryParams);
        WebPage<WarehouseOutVO> convert = converter.convert(webPage);
        return convert;
    }

    @Override
    @Transactional(rollbackFor = Exception.class)
    public Long add(WarehouseOutDTO warehouseOutDTO) {
        // 保存出库单
        WarehouseOutDO warehouseOutDO = BeanUtil.copyProperties(warehouseOutDTO, WarehouseOutDO.class);
        warehouseOutDO.setSn(noGenerateManager.generate(NoBusinessTypeEnum.WarehouseOut, warehouseOutDO.getDeptId()));
        warehouseOutDO.setCreateBy(adminUserManager.getCurrUserName());
        save(warehouseOutDO);
        // 保存出库单明细
        warehouseOutDTO.setId(warehouseOutDO.getId());
        List<WarehouseOutItemDO> productList = BeanUtils.toBean(warehouseOutDTO.getProductList(), WarehouseOutItemDO.class);
        productList.forEach(item -> item.setWarehouseOutId(warehouseOutDO.getId()));
        warehouseOutItemManager.saveBatch(productList);
//        //保存序列号和子表的关系
//        warehouseOutDTO.setId(warehouseOutDO.getId());
//        saveSnCode(warehouseOutDTO.getId(), productList);
        //如果是出库就要扣减库存
        if ("submit".equals(warehouseOutDTO.getSaveOrSubmit())) {
            updateStock(warehouseOutDTO, getById(warehouseOutDO.getId()), productList);
        }
        return warehouseOutDO.getId();
    }


    @Override
    @Transactional(rollbackFor = Exception.class)
    public void edit(WarehouseOutDTO warehouseOutDTO) {
        // 保存出库单
        WarehouseOutDO warehouseOutDO = BeanUtil.copyProperties(warehouseOutDTO, WarehouseOutDO.class);
        updateById(warehouseOutDO);
        // 保存出库单明细
        List<WarehouseOutItemDO> productList = BeanUtils.toBean(warehouseOutDTO.getProductList(), WarehouseOutItemDO.class);
        productList.forEach(item -> item.setWarehouseOutId(warehouseOutDO.getId()));
        warehouseOutItemManager.deleteByWarehouseOutId(Collections.singletonList(warehouseOutDTO.getId()));
        warehouseOutItemManager.saveBatch(productList);
        //保存序列号和子表的关系
//        saveSnCode(warehouseOutDO.getId(), productList);
        //如果是出库就要扣减库存
        if ("submit".equals(warehouseOutDTO.getSaveOrSubmit())) {
            updateStock(warehouseOutDTO, getById(warehouseOutDO.getId()), productList);
        }
    }


    private void updateStock(WarehouseOutDTO warehouseOutDTO, WarehouseOutDO warehouseOutDO, List<WarehouseOutItemDO> productList) {
        if ("Y".equals(warehouseOutDO.getHasSubmit())) {
            throw new ServiceException("数据已出库,请勿重复操作");
        }
//        for (WarehouseOutItemDO aDo : productList) {
//            ProductDO manager = productManager.getById(aDo.getProductId());
//            if (manager.getHaveSn()) {
//                if (CollUtil.isEmpty(aDo.getSnCodeList())) {
//                    throw new ServiceException(manager.getId() + ":有序列号的商品出库需要录入序列号");
//                }
//                if (aDo.getOutNum() != aDo.getSnCodeList().size()) {
//                    throw new ServiceException(manager.getId() + ":出库的数量须和序列号的个数相等");
//                }
//                Set<Long> set = aDo.getSnCodeList().stream().map(GoodsSnDO::getId).collect(Collectors.toSet());
//                if (set.size() != aDo.getSnCodeList().size()){
//                    throw new ServiceException("序列号重复");
//                }
//            }
//        }

        //如果是报损单改变报损单状态
        if (WarehouseOutTypeEnum.DAMAGE_OUT.equals(warehouseOutDTO.getType())) {
            StockDamageReport aDo = stockDamageReportManager.getById(warehouseOutDO.getOrderIdList());
            aDo.setStatus(DamageinventoryEnum.HAS_HANDLE);
            stockDamageReportManager.updateById(aDo);
        }

        List<StockDTO> arrayList = new ArrayList<>();
        //修改凍結库存明细
        List<LockedStockDetailDO> detailDOS = new ArrayList<>();
        for (WarehouseOutItemDO aDo : productList) {
            if (WarehouseOutTypeEnum.TRANSFER_OUT.equals(warehouseOutDO.getType())
//                    WarehouseOutTypeEnum.TRANSFER_OUT.equals(warehouseOutDO.getType()) ||
//                    WarehouseOutTypeEnum.DAMAGE_OUT.equals(warehouseOutDO.getType()) ||
//                    WarehouseOutTypeEnum.HAIL_OUT.equals(warehouseOutDO.getType())
                    ) {
                LockedStockDetailDO one = lockedStockDetailManager.getOne(new LambdaQueryWrapper<LockedStockDetailDO>()
                        .eq(LockedStockDetailDO::getOrderId, aDo.getOrderId()).eq(LockedStockDetailDO::getOrderItemId, aDo.getOrderItemId()));
                if (ObjectUtil.isNull(one)) {
                    throw new ServiceException("未找到冻结记录");
                }
                one.setUnLockedTime(new Date());
                if (one.getLockedNum() == (aDo.getOutNum() + one.getUnLockedNum())) {
                    one.setStatus(true);
                }
                one.setUnLockedNum(one.getUnLockedNum() + aDo.getOutNum());
                detailDOS.add(one);
            }
            WarehouseOutProductDTO productDO = warehouseOutDTO.getProductList().stream().filter(item -> item.getProductId().equals(aDo.getProductId())).findFirst().get();
            StockDTO dto = new StockDTO();
            dto.setId(aDo.getProductStockId());
            dto.setNum(-productDO.getOutNum());
            arrayList.add(dto);
        }
        lockedStockDetailManager.updateBatchById(detailDOS);
        //更爱可用库存实际库存冻结库存
        productStockManager.updateActualStock(arrayList, null);
        if (warehouseOutDTO.getType().equals(WarehouseOutTypeEnum.TRANSFER_OUT)) {
            //如果是调拨出库，锁定库存解冻
            productStockManager.updateLockedStock(arrayList);
        }
//        productStockManager.updateLockedStock(arrayList);
//        //更改在途库存
//        if (WarehouseOutTypeEnum.HAIL_OUT.equals(warehouseOutDTO.getType())) {
//            arrayList.forEach(item -> item.setNum(-item.getNum()));
//            productStockManager.updateTransitStock(arrayList);
//        }
        //入库之后数据不能被操作
        warehouseOutDO.setHasSubmit("Y");
        updateById(warehouseOutDO);
//        //更爱序列号的状态
//        List<ItemSnCodeDO> list1 = itemSnCodeManager.list(new LambdaQueryWrapper<ItemSnCodeDO>().eq(ItemSnCodeDO::getEntryOutHouseId, warehouseOutDO.getId()));
//        if (CollUtil.isNotEmpty(list1)) {
//            List<Long> collect1 = list1.stream().map(ItemSnCodeDO::getSnCodeId).collect(Collectors.toList());
//            List<GoodsSnDO> list = goodsSnManager.list(new LambdaQueryWrapper<GoodsSnDO>().in(GoodsSnDO::getId, collect1));
//            //更爱序列号状态
//            for (GoodsSnDO aDo : list) {
//                aDo.setStatus(SnCodeStatusEnum.OUT_WAREHOUSE);
//                aDo.setWarehouseId(warehouseOutDTO.getWarehouseId());
//                aDo.setOutHouseId(warehouseOutDO.getId());
//            }
//            goodsSnManager.updateBatchById(list);
//        }
        redisTemplate.delete(warehouseOutDO.getId().toString());
    }


    @Override
    public WarehouseOutVO getDetail(Long id) {
        WarehouseOutDO warehouseOutDO = getById(id);
        WarehouseOutVO outVO = BeanUtil.copyProperties(warehouseOutDO, WarehouseOutVO.class);
        // 查询出库明细
        List<WarehouseOutItemDO> itemList = warehouseOutItemManager.list(new LambdaQueryWrapper<WarehouseOutItemDO>().eq(WarehouseOutItemDO::getWarehouseOutId, id));
        List<WarehouseOutProductDTO> productList = BeanUtils.toBean(itemList, WarehouseOutProductDTO.class);

        List<Long> goodIdlist = itemList.stream().map(WarehouseOutItemDO::getProductId).collect(Collectors.toList());

        //所有的商品信息
        List<ProductDO> list = productManager.list(new LambdaQueryWrapper<ProductDO>().in(ProductDO::getId, goodIdlist));
        List<ProductStockDO> stockDOList = productStockManager.list(new LambdaQueryWrapper<ProductStockDO>()
                .eq(ProductStockDO::getWarehouseId, warehouseOutDO.getWarehouseId()).in(ProductStockDO::getProductId, goodIdlist));
        for (WarehouseOutProductDTO dto : productList) {
            ProductVO productVO = BeanUtil.copyProperties(list.stream().filter(item -> item.getId().equals(dto.getProductId())).findFirst().get(), ProductVO.class);
            dto.setProductVO(productVO);
            List<ItemSnCodeDO> list2 = itemSnCodeManager.list(new LambdaQueryWrapper<ItemSnCodeDO>().eq(ItemSnCodeDO::getItemId, dto.getId()));
            if (CollUtil.isNotEmpty(list2)) {
                List<Long> collected = list2.stream().map(ItemSnCodeDO::getSnCodeId).collect(Collectors.toList());
            }
            dto.setUsableStock(stockDOList.stream().filter(item -> item.getProductId().equals(dto.getProductId())).findFirst().get().getUsableStock());
            dto.setActualStock(stockDOList.stream().filter(item -> item.getProductId().equals(dto.getProductId())).findFirst().get().getActualStock());
            List<AreaStock> areaStockList = areaStockManager.list(new LambdaQueryWrapper<AreaStock>().eq(AreaStock::getProductStockId, dto.getProductStockId()));
            dto.setAreaStockList(areaStockList);
        }
        outVO.setProductList(productList);
        return outVO;
    }

    @Override
    @Transactional(rollbackFor = Exception.class)
    public void delete(Long id) {
        WarehouseOutDO aDo = getById(id);
        if ("Y".equals(aDo.getHasSubmit())) {
            throw new ServiceException("该数据已提交，不能删除");
        }
        removeById(id);
        // 删除明细
        warehouseOutItemManager.remove(new LambdaQueryWrapper<WarehouseOutItemDO>().eq(WarehouseOutItemDO::getWarehouseOutId, id));
    }

    @Override
    public WarehouseOutVO preview(WarehouseOutPreviewDTO warehouseOutPreviewDTO) {
//        // 校验订单编号是否正确
//        List<OrderDO> orderList = checkOrderData(warehouseOutPreviewDTO.getOrderIdList());
//
//        // 查询订单明细
//        List<OrderItemDO> orderItemList = orderItemManager.listByOrderIds(warehouseOutPreviewDTO.getOrderIdList());
//
//        // 查询这些商品的可用入库批次
//        List<Long> productIds = convertList(orderItemList, OrderItemDO::getProductId);
//        Map<Long, List<WarehouseEntryBatchDO>> warehouseBatchMap = batchManager.listAvailableBatch(orderList.get(0).getWarehouseId(), productIds);
//        return converter.convertPreview(orderList, orderItemList, warehouseBatchMap);
        return null;
    }

    @Override
    @Transactional(rollbackFor = Exception.class)
    public void ship(WarehouseOutShipDTO shipDTO) {
        Long id = shipDTO.getId();
        WarehouseOutDO warehouseOutDO = getById(id);
        if (!new WarehouseOutAllowable(warehouseOutDO).getShip()) {
            throw new ServiceException("当前状态不能进行发货操作");
        }
        // 扣减批次库存
        List<WarehouseOutItemDO> productList = warehouseOutItemManager.listByWarehouseOutId(id);
        List<StockUpdateDTO> stockUpdateList = converter.convertStockUpdateList(productList);

        // 更新出库单信息
        lambdaUpdate()
                .set(WarehouseOutDO::getStatus, WarehouseOutStatusEnum.SHIP)
                .set(WarehouseOutDO::getShipTime, DateUtil.getDateline())
                .set(WarehouseOutDO::getLogisticsCompanyId, shipDTO.getLogisticsCompanyId())
                .set(WarehouseOutDO::getLogisticsCompanyName, shipDTO.getLogisticsCompanyName())
                .set(WarehouseOutDO::getTrackingNumber, shipDTO.getTrackingNumber())
                .set(WarehouseOutDO::getFreightPrice, shipDTO.getFreightPrice())
                .set(WarehouseOutDO::getConsignee, shipDTO.getConsignee())
                .eq(WarehouseOutDO::getId, id)
                .update();

        // 将订单状态更新为已发货
        orderManager.warehouseOutShip(shipDTO);

        // 生成财务明细
        financeItemManager.addExpand(FinanceExpandTypeEnum.Logistics, warehouseOutDO.getSn(), shipDTO.getFreightPrice());

        // 发送出库单发货消息
        WarehouseOutShipMessage message = new WarehouseOutShipMessage();
        message.setId(id);
        message.setItemList(productList);
        this.messageSender.send(new MqMessage(AmqpExchange.WAREHOUSE_OUT_SHIP, AmqpExchange.WAREHOUSE_OUT_SHIP + "_ROUTING", message));
    }

    /**
     * 查询出库单统计分页列表数据
     *
     * @param params 查询参数
     * @return
     */
    @Override
    public WebPage statistics(WarehouseOutStatisticsParam params) {
        IPage<WarehouseOutStatistics> iPage = this.warehouseOutMapper.selectWarehouseOutPage(new Page(params.getPageNo(), params.getPageSize()), params);
        return PageConvert.convert(iPage);
    }

    /**
     * 导出出库单统计列表
     *
     * @param response
     * @param params   查询参数
     */
    @Override
    public void export(HttpServletResponse response, WarehouseOutStatisticsParam params) {
        //查询出库单商品列表
        List<WarehouseOutStatistics> list = this.warehouseOutMapper.selectWarehouseOutList(params);

        ArrayList<Map<String, Object>> rows = CollUtil.newArrayList();
        ExcelWriter writer = ExcelUtil.getWriter(true);
        for (WarehouseOutStatistics statistics : list) {
            Map<String, Object> map = new LinkedHashMap<>();
            map.put("所属部门", statistics.getDeptName());
            map.put("仓库名称", statistics.getWarehouseName());
            map.put("服务专员", statistics.getDistributionName());
            map.put("出库单编号", statistics.getSn());
            map.put("出库时间", DateUtil.toString(statistics.getOutTime(), "yyyy-MM-dd HH:mm:ss"));
            map.put("入库单编号", statistics.getWarehouseEntrySn());
            map.put("商品编号", statistics.getProductSn());
            map.put("商品名称", statistics.getProductName());
            map.put("商品类别", statistics.getCategoryName());
            map.put("规格型号", statistics.getProductSpecification());
            map.put("单位", statistics.getProductUnit());
            map.put("出库数量", statistics.getOutNum());
            rows.add(map);
        }

        writer.write(rows, true);

        ServletOutputStream out = null;
        try {
            out = response.getOutputStream();
        } catch (IOException e) {
            e.printStackTrace();
        }
        response.setContentType("application/vnd.openxmlformats-officedocument.spreadsheetml.sheet;charset=utf-8");
        response.setHeader("Content-Disposition", "attachment;filename=warehouse_out.xlsx");
        writer.flush(out, true);
        writer.close();
        IoUtil.close(out);
    }

    @Override
    public long countByWarehouseId(Long warehouseId) {
        return lambdaQuery().eq(WarehouseOutDO::getWarehouseId, warehouseId).count();
    }

    @Override
    public void audit(List<Long> ids, WarehouseOutStatusEnum status, String remark) {
        if (status != WarehouseOutStatusEnum.AUDIT_PASS && status != WarehouseOutStatusEnum.AUDIT_REJECT) {
            throw new ServiceException("审核参数错误");
        }

        List<WarehouseOutDO> list = listByIds(ids);
        for (WarehouseOutDO warehouseOutDO : list) {
            if (!new WarehouseOutAllowable(warehouseOutDO).getAudit()) {
                throw new ServiceException(warehouseOutDO.getSn() + "不能进行审核操作");
            }
        }

        AdminUser currUser = adminUserManager.getCurrUser();
        lambdaUpdate()
                .set(WarehouseOutDO::getStatus, status)
                .set(WarehouseOutDO::getAuditById, currUser.getId())
                .set(WarehouseOutDO::getAuditByName, currUser.getRealName())
                .in(WarehouseOutDO::getId, ids)
                .update();
    }


    @Override
    public Page<ProductStockVO> getGoodByWarehouse(Long warehouseId, BaseQueryParam queryParam, String orderType, List<Long> warehouseIds, String keyWord) {
        //找出所有上架的商品
//        LambdaQueryWrapper<GoodsDO> wrapper = new LambdaQueryWrapper<GoodsDO>().eq(GoodsDO::getMarketEnable, true);
//        if (StrUtil.isNotBlank(orderType)) {
//            switch (orderType) {
//                case "TO_B":
//                    wrapper.eq(GoodsDO::getHireOrSail, "sail");
//                    break;
//                case "TO_C":
//                    wrapper.eq(GoodsDO::getHireOrSail, "hire");
//                    break;
//                default:
//                    break;
//            }
//        }
//        List<GoodsDO> list = goodsMapper.selectList(wrapper);
//        List<Long> goodsId = list.stream().map(GoodsDO::getId).collect(Collectors.toList());
//        if (CollUtil.isEmpty(list)) {
//            throw new ServiceException("暂无对应类型的商品可供操作");
//        }
        Page<ProductStockDO> page1 = productStockManager.page(new Page<>(queryParam.getPageNo(), queryParam.getPageSize())
                , new LambdaQueryWrapper<ProductStockDO>()
                        .eq(ObjectUtil.isNotNull(warehouseId), ProductStockDO::getWarehouseId, warehouseId)
                        .eq(ObjectUtil.isNotNull(AdminUserContext.getCompanyId()), ProductStockDO::getCompanyId, AdminUserContext.getCompanyId())
                        .and(StrUtil.isNotBlank(keyWord), wa -> wa
                                .like(ProductStockDO::getGoodsName, keyWord)
                                .or()
                                .like(ProductStockDO::getGoodsBarCode, keyWord)
                                .or()
                                .like(ProductStockDO::getGoodsSnCode, keyWord))
                        .gt(ProductStockDO::getUsableStock, 0));
        Page<ProductStockVO> convert = productStockConverter.convert(page1);
        for (ProductStockVO record : convert.getRecords()) {
            record.setProductVO(BeanUtil.copyProperties(productManager.getById(record.getProductId()), ProductVO.class));
            record.setWarehouseVO(warehouseManager.getById(record.getWarehouseId()));
            List<AreaStock> areaStockList = areaStockManager.list(new LambdaQueryWrapper<AreaStock>()
                    .eq(ObjectUtil.isNotNull(AdminUserContext.getCompanyId()), AreaStock::getCompanyId, AdminUserContext.getCompanyId())
                    .eq(AreaStock::getProductStockId, record.getId()));
            record.setAreaStockList(areaStockList);
        }
        return convert;
    }

    @Override
    public IPage<ProductStockVO> getGoodByWarehouseInventory(Long warehouseId, BaseQueryParam queryParam, String keyWord, String type) {
        //找出所有上架的商品
//        List<GoodsDO> list = goodsMapper.selectList(new LambdaQueryWrapper<GoodsDO>().eq(GoodsDO::getMarketEnable, true));
//        List<Long> goodsId = list.stream().map(GoodsDO::getId).collect(Collectors.toList());
        //找到所有的商品
        Page<ProductStockDO> page = productStockManager.page(new Page<>(queryParam.getPageNo(), queryParam.getPageSize()), new LambdaQueryWrapper<ProductStockDO>()
                .eq(ProductStockDO::getWarehouseId, warehouseId)
                .gt(StrUtil.isNotBlank(type) && "gt".equals(type), ProductStockDO::getUsableStock, 0)
                .eq(ProductStockDO::getCompanyId, AdminUserContext.getCompanyId())
                .and(StrUtil.isNotBlank(keyWord), wa -> wa
                        .like(ProductStockDO::getGoodsName, keyWord)
                        .or()
                        .like(ProductStockDO::getGoodsBarCode, keyWord)
                        .or()
                        .like(ProductStockDO::getGoodsSnCode, keyWord)));
        Page<ProductStockVO> convert = productStockConverter.convert(page);
        for (ProductStockVO record : convert.getRecords()) {
            record.setProductVO(BeanUtil.copyProperties(productManager.getById(record.getProductId()), ProductVO.class));
            record.setWarehouseVO(warehouseManager.getById(record.getWarehouseId()));
            List<AreaStock> areaStockList = areaStockManager.list(new LambdaQueryWrapper<AreaStock>()
                    .eq(AreaStock::getProductStockId, record.getId())
                    .eq(ObjectUtil.isNotNull(AdminUserContext.getCompanyId()), AreaStock::getCompanyId, AdminUserContext.getCompanyId()));
            record.setAreaStockList(areaStockList);
        }
        return convert;
    }

    @Override
    public IPage<LockedStockDetailDO> getLockedStock(LockedStockParams queryParam) {
        LambdaQueryWrapper<LockedStockDetailDO> wrapper = new LambdaQueryWrapper<LockedStockDetailDO>().eq(ObjectUtil.isNotNull(queryParam.getStatus()), LockedStockDetailDO::getStatus, queryParam.getStatus());
        if (ObjectUtil.isNotNull(queryParam.getStartTime()) && ObjectUtil.isNotNull(queryParam.getEndTime())) {
            wrapper.between(LockedStockDetailDO::getLockedTime, new Date(queryParam.getStartTime()), new Date(queryParam.getEndTime()));
        }
        Page<LockedStockDetailDO> page = lockedStockDetailManager.page(new Page<>(queryParam.getPageNo(), queryParam.getPageSize()), wrapper);
        if (queryParam.getStartTime() != null) {
            System.out.println(new Date(queryParam.getStartTime()));
        }
        for (LockedStockDetailDO record : page.getRecords()) {
            ProductStockDO byId = productStockManager.getById(record.getProductStockId());
            record.setProductVO(productManager.getById(byId.getProductId()));
        }
        return page;
    }
}

