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.constant.enums.wms.WmsTaskTypeEnums;
import com.wms_main.dao.*;
import com.wms_main.model.dto.query.InventoryQuery;
import com.wms_main.model.dto.query.OutsQuery;
import com.wms_main.model.dto.query.PickTaskQuery;
import com.wms_main.model.dto.query.WmsTaskQuery;
import com.wms_main.model.dto.response.wms.WmsApiResponse;
import com.wms_main.model.po.*;
import com.wms_main.model.vo.wms.*;
import com.wms_main.repository.utils.StringUtils;
import com.wms_main.service.controller.ITaskQueryControllerService;
import lombok.RequiredArgsConstructor;
import org.springframework.stereotype.Service;

/**
 * 任务查询控制类 服务实现
 */
@Service
@RequiredArgsConstructor
public class TaskQueryControllerServiceImpl implements ITaskQueryControllerService {
    private final ITAppTaskService appTaskService;// 任务服务
    private final ITAppTaskBakService appTaskBakService;// 任务记录服务
    private final ITAppPickTaskService appPickTaskService;// 拣选任务服务
    private final ITAppOutsService appOutsService;// 出库单服务
    private final ITAppOutsRecordService appOutsRecordService;// 出库单记录服务
    private final ITAppInventoryService appInventoryService;// 盘点请求服务
    private final ITAppInventoryRecordService appInventoryRecordService;// 盘点记录服务

    /**
     * 查询wms任务监控---分页
     * @param wmsTaskQuery 查询参数
     * @return 查询结果
     */
    @Override
    public WmsApiResponse<PageVo<TaskVo>> queryTasksByPage(WmsTaskQuery wmsTaskQuery) {
        if (wmsTaskQuery == null) {
            return WmsApiResponse.error("查询参数不能为NULL", null);
        }
        Page<TAppTask> page = wmsTaskQuery.toMpPage();
        LambdaQueryWrapper<TAppTask> lambdaQueryWrapper = new LambdaQueryWrapper<TAppTask>()
                .like(StringUtils.isNotEmpty(wmsTaskQuery.getTaskId()), TAppTask::getTaskId, wmsTaskQuery.getTaskId())
                .like(StringUtils.isNotEmpty(wmsTaskQuery.getGoodsId()), TAppTask::getGoodsId, wmsTaskQuery.getGoodsId())
                .like(StringUtils.isNotEmpty(wmsTaskQuery.getVehicleId()), TAppTask::getVehicleId, wmsTaskQuery.getVehicleId())
                .eq(wmsTaskQuery.getTaskType() != null, TAppTask::getTaskType, wmsTaskQuery.getTaskType())
                .eq(wmsTaskQuery.getTaskStatus() != null, TAppTask::getTaskStatus, wmsTaskQuery.getTaskStatus());
        Page<TAppTask> tasksPage = appTaskService.page(page, lambdaQueryWrapper);

        PageVo<TaskVo> pageVo = PageVo.of(tasksPage, TaskVo::of);
        return WmsApiResponse.success("查询任务成功。", pageVo);
    }

    /**
     * 查询入库记录（分页）---实现
     * @param wmsTaskQuery 查询参数
     * @return 查询结果
     */
    @Override
    public WmsApiResponse<PageVo<TaskRecordVo>> queryInTaskRecordByPage(WmsTaskQuery wmsTaskQuery) {
        if (wmsTaskQuery == null) {
            return WmsApiResponse.error("查询参数不能为NULL", null);
        }
        if (wmsTaskQuery.getFromTime() != null && wmsTaskQuery.getToTime() != null) {
            if (wmsTaskQuery.getFromTime().isAfter(wmsTaskQuery.getToTime())) {
                return WmsApiResponse.error("from时间不能晚于to时间。", null);
            }
        }
        Page<TAppTaskBak> page = wmsTaskQuery.toMpPage();
        LambdaQueryWrapper<TAppTaskBak> lambdaQueryWrapper = new LambdaQueryWrapper<TAppTaskBak>()
                .like(StringUtils.isNotEmpty(wmsTaskQuery.getTaskId()), TAppTaskBak::getTaskId, wmsTaskQuery.getTaskId())
                .like(StringUtils.isNotEmpty(wmsTaskQuery.getGoodsId()), TAppTaskBak::getGoodsId, wmsTaskQuery.getGoodsId())
                .like(StringUtils.isNotEmpty(wmsTaskQuery.getVehicleId()), TAppTaskBak::getVehicleId, wmsTaskQuery.getVehicleId())
                .eq(TAppTaskBak::getTaskType, WmsTaskTypeEnums.IN.getCode());
        if (wmsTaskQuery.getFromTime() != null) {
            lambdaQueryWrapper.ge(TAppTaskBak::getCreateTime, wmsTaskQuery.getFromTime());
        }
        if (wmsTaskQuery.getToTime() != null) {
            lambdaQueryWrapper.le(TAppTaskBak::getCreateTime, wmsTaskQuery.getToTime());
        }
        Page<TAppTaskBak> inTaskRecordsPage = appTaskBakService.page(page, lambdaQueryWrapper);

        PageVo<TaskRecordVo> pageVo = PageVo.of(inTaskRecordsPage, TaskRecordVo::of);
        return WmsApiResponse.success("查询入库记录成功。", pageVo);
    }

    /**
     * 查询出库记录（分页）---实现
     * @param wmsTaskQuery 查询参数
     * @return 查询结果
     */
    @Override
    public WmsApiResponse<PageVo<TaskRecordVo>> queryOutTaskRecordByPage(WmsTaskQuery wmsTaskQuery) {
        if (wmsTaskQuery == null) {
            return WmsApiResponse.error("查询参数不能为NULL", null);
        }
        if (wmsTaskQuery.getFromTime() != null && wmsTaskQuery.getToTime() != null) {
            if (wmsTaskQuery.getFromTime().isAfter(wmsTaskQuery.getToTime())) {
                return WmsApiResponse.error("from时间不能晚于to时间。", null);
            }
        }
        Page<TAppTaskBak> page = wmsTaskQuery.toMpPage();
        LambdaQueryWrapper<TAppTaskBak> lambdaQueryWrapper = new LambdaQueryWrapper<TAppTaskBak>()
                .like(StringUtils.isNotEmpty(wmsTaskQuery.getTaskId()), TAppTaskBak::getTaskId, wmsTaskQuery.getTaskId())
                .like(StringUtils.isNotEmpty(wmsTaskQuery.getGoodsId()), TAppTaskBak::getGoodsId, wmsTaskQuery.getGoodsId())
                .like(StringUtils.isNotEmpty(wmsTaskQuery.getVehicleId()), TAppTaskBak::getVehicleId, wmsTaskQuery.getVehicleId())
                .eq(TAppTaskBak::getTaskType, WmsTaskTypeEnums.OUT.getCode());
        if (wmsTaskQuery.getFromTime() != null) {
            lambdaQueryWrapper.ge(TAppTaskBak::getCreateTime, wmsTaskQuery.getFromTime());
        }
        if (wmsTaskQuery.getToTime() != null) {
            lambdaQueryWrapper.le(TAppTaskBak::getCreateTime, wmsTaskQuery.getToTime());
        }
        Page<TAppTaskBak> outTaskRecordsPage = appTaskBakService.page(page, lambdaQueryWrapper);

        PageVo<TaskRecordVo> pageVo = PageVo.of(outTaskRecordsPage, TaskRecordVo::of);
        return WmsApiResponse.success("查询出库记录成功。", pageVo);
    }

    /**
     * 分页查询拣选任务---实现
     * @param pickTaskQuery 查询参数
     * @return 查询结果
     */
    @Override
    public WmsApiResponse<PageVo<PickTaskVo>> queryPickTasksByPage(PickTaskQuery pickTaskQuery) {
        if (pickTaskQuery == null) {
            return WmsApiResponse.error("查询参数不能为NULL", null);
        }
        Page<TAppPickTask> page = pickTaskQuery.toMpPage();
        LambdaQueryWrapper<TAppPickTask> lambdaQueryWrapper = new LambdaQueryWrapper<TAppPickTask>()
                .like(StringUtils.isNotEmpty(pickTaskQuery.getPickId()), TAppPickTask::getPickId, pickTaskQuery.getPickId())
                .like(StringUtils.isNotEmpty(pickTaskQuery.getPickStand()), TAppPickTask::getPickStand, pickTaskQuery.getPickStand())
                .like(StringUtils.isNotEmpty(pickTaskQuery.getVehicleId()), TAppPickTask::getVehicleId, pickTaskQuery.getVehicleId())
                .eq(pickTaskQuery.getPickStatus() != null, TAppPickTask::getPickStatus, pickTaskQuery.getPickStatus());
        Page<TAppPickTask> tasksPage = appPickTaskService.page(page, lambdaQueryWrapper);

        PageVo<PickTaskVo> pageVo = PageVo.of(tasksPage, PickTaskVo::of);
        return WmsApiResponse.success("查询拣选任务成功。", pageVo);
    }

    /**
     * 分页查询出库单---实现
     * @param outsQuery 查询参数
     * @return 查询结果
     */
    @Override
    public WmsApiResponse<PageVo<OutsVo>> queryOutsByPage(OutsQuery outsQuery) {
        if (outsQuery == null) {
            return WmsApiResponse.error("查询参数不能为NULL", null);
        }
        if (outsQuery.getQueryType() == 1) {
            // 查询未关闭
            Page<TAppOuts> page = outsQuery.toMpPage();
            LambdaQueryWrapper<TAppOuts> lambdaQueryWrapper = new LambdaQueryWrapper<TAppOuts>()
                    .like(StringUtils.isNotEmpty(outsQuery.getTaskId()), TAppOuts::getTaskId, outsQuery.getTaskId())
                    .like(StringUtils.isNotEmpty(outsQuery.getGoodsId()), TAppOuts::getGoodsId, outsQuery.getGoodsId())
                    .like(StringUtils.isNotEmpty(outsQuery.getVehicleId()), TAppOuts::getVehicleId, outsQuery.getVehicleId())
                    .eq(outsQuery.getOutType() != null, TAppOuts::getOutType, outsQuery.getOutType());
            Page<TAppOuts> outsPage = appOutsService.page(page, lambdaQueryWrapper);

            PageVo<OutsVo> pageVo = PageVo.of(outsPage, OutsVo::of);
            return WmsApiResponse.success("查询出库单成功。", pageVo);
        } else {
            // 查询已关闭的记录
            Page<TAppOutsRecord> page = outsQuery.toMpPage();
            LambdaQueryWrapper<TAppOutsRecord> lambdaQueryWrapper = new LambdaQueryWrapper<TAppOutsRecord>()
                    .like(StringUtils.isNotEmpty(outsQuery.getTaskId()), TAppOutsRecord::getTaskId, outsQuery.getTaskId())
                    .like(StringUtils.isNotEmpty(outsQuery.getGoodsId()), TAppOutsRecord::getGoodsId, outsQuery.getGoodsId())
                    .like(StringUtils.isNotEmpty(outsQuery.getVehicleId()), TAppOutsRecord::getVehicleId, outsQuery.getVehicleId())
                    .eq(outsQuery.getOutType() != null, TAppOutsRecord::getOutType, outsQuery.getOutType());
            Page<TAppOutsRecord> outsPage = appOutsRecordService.page(page, lambdaQueryWrapper);

            PageVo<OutsVo> pageVo = PageVo.of(outsPage, OutsVo::ofRecord);
            return WmsApiResponse.success("查询出库单记录成功。", pageVo);
        }

    }

    /**
     * 分页查询盘点相关信息---实现
     * @param inventoryQuery 查询参数
     * @return 查询结果
     */
    @Override
    public WmsApiResponse<PageVo<InventoryRecordVo>> queryInventoryRecordByPage(InventoryQuery inventoryQuery) {
        if (inventoryQuery == null) {
            return WmsApiResponse.error("查询参数不能为NULL", null);
        }
        if (inventoryQuery.getQueryType() == 1) {
            // 查询盘点请求
            Page<TAppInventory> page = inventoryQuery.toMpPage();
            LambdaQueryWrapper<TAppInventory> lambdaQueryWrapper = new LambdaQueryWrapper<TAppInventory>()
                    .like(StringUtils.isNotEmpty(inventoryQuery.getInventoryId()), TAppInventory::getInventoryId, inventoryQuery.getInventoryId())
                    .like(StringUtils.isNotEmpty(inventoryQuery.getGoodsId()), TAppInventory::getGoodsId, inventoryQuery.getGoodsId())
                    .like(StringUtils.isNotEmpty(inventoryQuery.getVehicleId()), TAppInventory::getVehicleId, inventoryQuery.getVehicleId())
                    .eq(inventoryQuery.getInvResult() != null, TAppInventory::getInvResult, inventoryQuery.getInvResult());
            Page<TAppInventory> inventoryPage = appInventoryService.page(page, lambdaQueryWrapper);
            PageVo<InventoryRecordVo> pageVo = PageVo.of(inventoryPage, InventoryRecordVo::ofTaskPo);
            return WmsApiResponse.success("查询盘点任务成功。", pageVo);
        } else {
            // 默认；查询盘点记录
            Page<TAppInventoryRecord> page = inventoryQuery.toMpPage();
            LambdaQueryWrapper<TAppInventoryRecord> lambdaQueryWrapper = new LambdaQueryWrapper<TAppInventoryRecord>()
                    .like(StringUtils.isNotEmpty(inventoryQuery.getInventoryId()), TAppInventoryRecord::getInventoryId, inventoryQuery.getInventoryId())
                    .like(StringUtils.isNotEmpty(inventoryQuery.getGoodsId()), TAppInventoryRecord::getGoodsId, inventoryQuery.getGoodsId())
                    .like(StringUtils.isNotEmpty(inventoryQuery.getVehicleId()), TAppInventoryRecord::getVehicleId, inventoryQuery.getVehicleId())
                    .eq(inventoryQuery.getInvResult() != null, TAppInventoryRecord::getInvResult, inventoryQuery.getInvResult());
            Page<TAppInventoryRecord> inventoryRecordPage = appInventoryRecordService.page(page, lambdaQueryWrapper);
            PageVo<InventoryRecordVo> pageVo = PageVo.of(inventoryRecordPage, InventoryRecordVo::ofRecordPo);
            return WmsApiResponse.success("查询盘点记录成功。", pageVo);
        }
    }
}
