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.ITAppTaskBakService;
import com.wms_main.dao.ITAppTaskService;
import com.wms_main.model.dto.query.WmsTaskQuery;
import com.wms_main.model.dto.response.wms.WmsApiResponse;
import com.wms_main.model.po.TAppTask;
import com.wms_main.model.po.TAppTaskBak;
import com.wms_main.model.vo.wms.PageVo;
import com.wms_main.model.vo.wms.TaskRecordVo;
import com.wms_main.model.vo.wms.TaskVo;
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;// 任务记录服务

    /**
     * 查询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);
    }
}
