package com.wms_main.service.controller.serviceImpl;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.wms_main.dao.ITAppStockService;
import com.wms_main.dao.ITAppStockUpdateService;
import com.wms_main.model.dto.query.StockQuery;
import com.wms_main.model.dto.query.StockUpdateQuery;
import com.wms_main.model.dto.response.wms.WmsApiResponse;
import com.wms_main.model.po.TAppStock;
import com.wms_main.model.po.TAppStockUpdate;
import com.wms_main.model.vo.wms.PageVo;
import com.wms_main.model.vo.wms.StockUpdateVo;
import com.wms_main.model.vo.wms.StockVo;
import com.wms_main.repository.utils.StringUtils;
import com.wms_main.service.controller.IStockControllerService;
import lombok.RequiredArgsConstructor;
import org.springframework.stereotype.Service;

/**
 * 库存控制类 服务实现
 */
@Service
@RequiredArgsConstructor
public class StockControllerServiceImpl implements IStockControllerService {
    private final ITAppStockService appStockService;// 库存服务
    private final ITAppStockUpdateService appStockUpdateService;// 库存更新服务

    /**
     * 查询库存---分页实现
     *
     * @param stockQuery 查询参数
     * @return 查询结果
     */
    @Override
    public WmsApiResponse<PageVo<StockVo>> queryStocksByPage(StockQuery stockQuery) {
        if (stockQuery == null) {
            return WmsApiResponse.error("查询参数不能为NULL", null);
        }
        Page<TAppStock> page = stockQuery.toMpPage();
        LambdaQueryWrapper<TAppStock> lambdaQueryWrapper = new LambdaQueryWrapper<TAppStock>()
                .like(StringUtils.isNotEmpty(stockQuery.getVehicleId()), TAppStock::getVehicleId, stockQuery.getVehicleId())
                .like(StringUtils.isNotEmpty(stockQuery.getLocationId()), TAppStock::getLocationId, stockQuery.getLocationId())
                .like(StringUtils.isNotEmpty(stockQuery.getGoodsId()), TAppStock::getGoodsId, stockQuery.getGoodsId())
                .eq(stockQuery.getStockStatus() != null, TAppStock::getStockStatus, stockQuery.getStockStatus())
                .eq(stockQuery.getGoodsStatus() != null, TAppStock::getGoodsStatus, stockQuery.getGoodsStatus());
        if (stockQuery.getFromDate() != null) {
            lambdaQueryWrapper.ge(TAppStock::getFirstInTime, stockQuery.getFromDate().atStartOfDay());
        }
        if (stockQuery.getToDate() != null) {
            // toDate + 1天的凌晨。
            lambdaQueryWrapper.lt(TAppStock::getFirstInTime, stockQuery.getToDate().plusDays(1).atStartOfDay());
        }
        Page<TAppStock> stockPage = appStockService.page(page, lambdaQueryWrapper);

        PageVo<StockVo> pageVo = PageVo.of(stockPage, StockVo::ofPo);
        return WmsApiResponse.success("查询库存数据成功", pageVo);
    }

    /**
     * 查询库存更新记录（分页）---实现
     * @param stockUpdateQuery 查询参数
     * @return 查询结果
     */
    @Override
    public WmsApiResponse<PageVo<StockUpdateVo>> queryStockUpdateByPage(StockUpdateQuery stockUpdateQuery) {
        if (stockUpdateQuery == null) {
            return WmsApiResponse.error("查询参数不能为NULL", null);
        }
        if (stockUpdateQuery.getFromTime() != null && stockUpdateQuery.getToTime() != null) {
            if (stockUpdateQuery.getFromTime().isAfter(stockUpdateQuery.getToTime())) {
                return WmsApiResponse.error("from时间不能晚于to时间。", null);
            }
        }
        Page<TAppStockUpdate> page = stockUpdateQuery.toMpPage();
        LambdaQueryWrapper<TAppStockUpdate> lambdaQueryWrapper = new LambdaQueryWrapper<TAppStockUpdate>()
                .like(StringUtils.isNotEmpty(stockUpdateQuery.getGoodsId()), TAppStockUpdate::getGoodsId, stockUpdateQuery.getGoodsId())
                .like(StringUtils.isNotEmpty(stockUpdateQuery.getVehicleId()), TAppStockUpdate::getVehicleId, stockUpdateQuery.getVehicleId());
        // 时间范围查询
        if (stockUpdateQuery.getFromTime() != null) {
            lambdaQueryWrapper.ge(TAppStockUpdate::getUpdateTime, stockUpdateQuery.getFromTime());
        }
        if (stockUpdateQuery.getToTime() != null) {
            lambdaQueryWrapper.le(TAppStockUpdate::getUpdateTime, stockUpdateQuery.getToTime());
        }
        // 查询库存更新记录
        Page<TAppStockUpdate> stockUpdatePage = appStockUpdateService.page(page, lambdaQueryWrapper);
        PageVo<StockUpdateVo> pageVo = PageVo.of(stockUpdatePage, StockUpdateVo::of);
        return WmsApiResponse.success("查询库存更新记录成功", pageVo);
    }
}
