package org.wcs.mapper.impl;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.conditions.update.LambdaUpdateWrapper;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import org.springframework.stereotype.Service;
import org.wcs.constant.enums.database.StockComposeTaskStatusEnum;
import org.wcs.constant.enums.database.StockComposeTaskStepStatusEnum;
import org.wcs.mapper.AppStockComposeTaskMapper;
import org.wcs.mapper.intf.AppStockComposeTaskService;
import org.wcs.model.bo.tuple.Tuple2;
import org.wcs.model.dto.serve.stcokComposeTask.QueryStockComposeTaskReq;
import org.wcs.model.po.app.AppStockComposeTask;
import org.wcs.utils.AppStringUtils;

import java.time.LocalDateTime;
import java.util.List;

@Service
public class AppStockComposeTaskDao extends ServiceImpl<AppStockComposeTaskMapper, AppStockComposeTask> implements AppStockComposeTaskService {

    /**
     * 模糊条件查询数据
     *
     * @param queryTaskReq 查询条件
     * @param pageSize     每页大小
     * @param pageIndex    页码
     * @return 查询结果
     */
    @Override
    public Tuple2<Long, List<AppStockComposeTask>> query(QueryStockComposeTaskReq queryTaskReq, Integer pageSize, Integer pageIndex) {
        LambdaQueryWrapper<AppStockComposeTask> queryWrapper = new LambdaQueryWrapper<>();
        queryWrapper.like(AppStringUtils.isNotEmpty(queryTaskReq.getTaskId()), AppStockComposeTask::getTaskId, queryTaskReq.getTaskId());
        queryWrapper.like(AppStringUtils.isNotEmpty(queryTaskReq.getTaskGroup()), AppStockComposeTask::getTaskGroup, queryTaskReq.getTaskGroup());
        queryWrapper.like(AppStringUtils.isNotEmpty(queryTaskReq.getUpperTaskId()), AppStockComposeTask::getUpperTaskId, queryTaskReq.getUpperTaskId());
        queryWrapper.eq(queryTaskReq.getTaskType() != null, AppStockComposeTask::getTaskType, queryTaskReq.getTaskType());
        queryWrapper.like(AppStringUtils.isNotEmpty(queryTaskReq.getVehicleNo()), AppStockComposeTask::getVehicleNo, queryTaskReq.getVehicleNo());
        queryWrapper.like(AppStringUtils.isNotEmpty(queryTaskReq.getOrigin()), AppStockComposeTask::getOrigin, queryTaskReq.getOrigin());
        queryWrapper.like(AppStringUtils.isNotEmpty(queryTaskReq.getDestination()), AppStockComposeTask::getDestination, queryTaskReq.getDestination());
        queryWrapper.eq(queryTaskReq.getTaskStatus() != null, AppStockComposeTask::getTaskStatus, queryTaskReq.getTaskStatus());
        if(queryTaskReq.getCreateTimeRange() != null && queryTaskReq.getCreateTimeRange().size() == 2) {
            queryWrapper.between(AppStockComposeTask::getCreateTime, queryTaskReq.getCreateTimeRange().get(0), queryTaskReq.getCreateTimeRange().get(1));
        }
        if(queryTaskReq.getEndTimeRange() != null && queryTaskReq.getEndTimeRange().size() == 2) {
            queryWrapper.between(AppStockComposeTask::getEndTime, queryTaskReq.getEndTimeRange().get(0), queryTaskReq.getEndTimeRange().get(1));
        }
        queryWrapper.orderByDesc(AppStockComposeTask::getCreateTime);
        Page<AppStockComposeTask> page = new Page<>(pageIndex, pageSize);
        try {
            Page<AppStockComposeTask> pageResult = super.baseMapper.selectPage(page, queryWrapper);
            return new Tuple2<>(pageResult.getTotal(), pageResult.getRecords());
        } catch (Exception ex) {
            return null;
        }
    }

    @Override
    public List<AppStockComposeTask> query(AppStockComposeTask appStockComposeTask) {
        if(appStockComposeTask == null) {
            return null;
        }
        LambdaQueryWrapper<AppStockComposeTask> queryWrapper = new LambdaQueryWrapper<>();
        queryWrapper.eq(appStockComposeTask.getTaskId() != null, AppStockComposeTask::getTaskId, appStockComposeTask.getTaskId());
        queryWrapper.eq(appStockComposeTask.getTaskGroup() != null, AppStockComposeTask::getTaskGroup, appStockComposeTask.getTaskGroup());
        queryWrapper.eq(appStockComposeTask.getUpperTaskId() != null, AppStockComposeTask::getUpperTaskId, appStockComposeTask.getUpperTaskId());
        queryWrapper.eq(appStockComposeTask.getTaskType() != null, AppStockComposeTask::getTaskType, appStockComposeTask.getTaskType());
        queryWrapper.eq(appStockComposeTask.getOrigin() != null, AppStockComposeTask::getOrigin, appStockComposeTask.getOrigin());
        queryWrapper.eq(appStockComposeTask.getDestination() != null, AppStockComposeTask::getDestination, appStockComposeTask.getDestination());
        queryWrapper.eq(appStockComposeTask.getTaskStatus() != null, AppStockComposeTask::getTaskStatus, appStockComposeTask.getTaskStatus());
        queryWrapper.eq(appStockComposeTask.getStepStatus() != null, AppStockComposeTask::getStepStatus, appStockComposeTask.getStepStatus());
        queryWrapper.eq(appStockComposeTask.getCanCancel() != null, AppStockComposeTask::getCanCancel, appStockComposeTask.getCanCancel());
        queryWrapper.eq(appStockComposeTask.getPriority() != null, AppStockComposeTask::getPriority, appStockComposeTask.getPriority());
        queryWrapper.eq(appStockComposeTask.getVehicleNo() != null, AppStockComposeTask::getVehicleNo, appStockComposeTask.getVehicleNo());
        queryWrapper.eq(appStockComposeTask.getVehicleSize() != null, AppStockComposeTask::getVehicleSize, appStockComposeTask.getVehicleSize());
        queryWrapper.eq(appStockComposeTask.getWeight() != null, AppStockComposeTask::getWeight, appStockComposeTask.getWeight());
        queryWrapper.eq(appStockComposeTask.getTaskSource() != null, AppStockComposeTask::getTaskSource, appStockComposeTask.getTaskSource());
        queryWrapper.eq(appStockComposeTask.getCreatePerson() != null, AppStockComposeTask::getCreatePerson, appStockComposeTask.getCreatePerson());
        queryWrapper.like(appStockComposeTask.getTaskMsg() != null, AppStockComposeTask::getTaskMsg, appStockComposeTask.getTaskMsg());
        queryWrapper.orderByDesc(AppStockComposeTask::getPriority).orderByAsc(AppStockComposeTask::getCreateTime);
        try {
            return list(queryWrapper);
        } catch (Exception e) {
            return null;
        }
    }

    /**
     * 直接查询，不分页，用于导出使用
     * @param queryTaskReq 查询条件
     * @return 查询结果
     */
    @Override
    public List<AppStockComposeTask> query(QueryStockComposeTaskReq queryTaskReq) {
        LambdaQueryWrapper<AppStockComposeTask> queryWrapper = new LambdaQueryWrapper<>();
        queryWrapper.like(AppStringUtils.isNotEmpty(queryTaskReq.getTaskId()), AppStockComposeTask::getTaskId, queryTaskReq.getTaskId());
        queryWrapper.like(AppStringUtils.isNotEmpty(queryTaskReq.getTaskGroup()), AppStockComposeTask::getTaskGroup, queryTaskReq.getTaskGroup());
        queryWrapper.like(AppStringUtils.isNotEmpty(queryTaskReq.getUpperTaskId()), AppStockComposeTask::getUpperTaskId, queryTaskReq.getUpperTaskId());
        queryWrapper.eq(queryTaskReq.getTaskType() != null, AppStockComposeTask::getTaskType, queryTaskReq.getTaskType());
        queryWrapper.like(AppStringUtils.isNotEmpty(queryTaskReq.getVehicleNo()), AppStockComposeTask::getVehicleNo, queryTaskReq.getVehicleNo());
        queryWrapper.like(AppStringUtils.isNotEmpty(queryTaskReq.getOrigin()), AppStockComposeTask::getOrigin, queryTaskReq.getOrigin());
        queryWrapper.like(AppStringUtils.isNotEmpty(queryTaskReq.getDestination()), AppStockComposeTask::getDestination, queryTaskReq.getDestination());
        queryWrapper.eq(queryTaskReq.getTaskStatus() != null, AppStockComposeTask::getTaskStatus, queryTaskReq.getTaskStatus());
        if(queryTaskReq.getCreateTimeRange() != null && queryTaskReq.getCreateTimeRange().size() == 2) {
            queryWrapper.between(AppStockComposeTask::getCreateTime, queryTaskReq.getCreateTimeRange().get(0), queryTaskReq.getCreateTimeRange().get(1));
        }
        if(queryTaskReq.getEndTimeRange() != null && queryTaskReq.getEndTimeRange().size() == 2) {
            queryWrapper.between(AppStockComposeTask::getEndTime, queryTaskReq.getEndTimeRange().get(0), queryTaskReq.getEndTimeRange().get(1));
        }
        queryWrapper.orderByDesc(AppStockComposeTask::getCreateTime);
        try {
            return super.baseMapper.selectList(queryWrapper);
        } catch (Exception ex) {
            return null;
        }
    }

    /**
     * 查询指定状态的任务
     *
     * @param taskStatus 任务状态
     * @return 任务列表
     */
    @Override
    public List<AppStockComposeTask> queryWithStatus(Integer... taskStatus) {
        if(taskStatus == null || taskStatus.length == 0) {
            return null;
        }
        List<Integer> statusList = List.of(taskStatus);
        LambdaQueryWrapper<AppStockComposeTask> queryWrapper = new LambdaQueryWrapper<>();
        queryWrapper.in(AppStockComposeTask::getTaskStatus, statusList);
        queryWrapper.orderByDesc(AppStockComposeTask::getPriority).orderByAsc(AppStockComposeTask::getCreateTime);
        try {
            return super.list(queryWrapper);
        } catch (Exception e) {
            return null;
        }
    }

    /**
     * 插入数据
     *
     * @param appStockComposeTask 待插入数据
     * @return 插入结果
     */
    @Override
    public int insert(AppStockComposeTask appStockComposeTask) {
        appStockComposeTask.setCreateTime(LocalDateTime.now());
        appStockComposeTask.setUpdateTime(LocalDateTime.now());
        try {
            return super.save(appStockComposeTask) ? 1 : 0;
        } catch (Exception e) {
            return 0;
        }
    }

    /**
     * 查找未完成的任务
     * @param vehicleNo 载具号
     * @return 未完成的任务
     */
    @Override
    public List<AppStockComposeTask> queryNotCompleteTask(String vehicleNo) {
        if(AppStringUtils.isEmpty(vehicleNo)) {
            return null;
        }
        LambdaQueryWrapper<AppStockComposeTask> queryWrapper = new LambdaQueryWrapper<>();
        queryWrapper.eq(AppStockComposeTask::getVehicleNo, vehicleNo);
        queryWrapper.in(AppStockComposeTask::getTaskStatus, StockComposeTaskStatusEnum.CREATE.getCode(),
                StockComposeTaskStatusEnum.QUEUE.getCode(), StockComposeTaskStatusEnum.EXECUTING.getCode());
        try {
            return super.baseMapper.selectList(queryWrapper);
        } catch (Exception e) {
            return null;
        }
    }

    /**
     * 根据任务ID查询任务
     * @param taskId 任务ID
     * @return 任务
     */
    @Override
    public AppStockComposeTask queryByTaskId(String taskId) {
        try {
            return super.getById(taskId);
        } catch (Exception ex) {
            return null;
        }
    }

    /**
     * 根据上级任务ID查询任务
     * @param upperTaskId 上级任务ID
     * @return 任务列表
     */
    @Override
    public List<AppStockComposeTask> queryByUpperTaskId(String upperTaskId) {
        LambdaQueryWrapper<AppStockComposeTask> queryWrapper = new LambdaQueryWrapper<>();
        queryWrapper.eq(AppStockComposeTask::getUpperTaskId, upperTaskId);
        queryWrapper.orderByDesc(AppStockComposeTask::getCreateTime);
        try {
            return super.list(queryWrapper);
        } catch (Exception e) {
            return null;
        }
    }

    /**
     * 更新任务
     * @param appStockComposeTask 待更新任务
     * @return 更新结果
     */
    @Override
    public boolean update(AppStockComposeTask appStockComposeTask) {
        LambdaUpdateWrapper<AppStockComposeTask> updateWrapper = new LambdaUpdateWrapper<>();
        updateWrapper.eq(AppStockComposeTask::getTaskId, appStockComposeTask.getTaskId());
        updateWrapper.set(appStockComposeTask.getTaskGroup() != null, AppStockComposeTask::getTaskGroup, appStockComposeTask.getTaskGroup());
        updateWrapper.set(appStockComposeTask.getUpperTaskId() != null, AppStockComposeTask::getUpperTaskId, appStockComposeTask.getUpperTaskId());
        updateWrapper.set(appStockComposeTask.getTaskType() != null, AppStockComposeTask::getTaskType, appStockComposeTask.getTaskType());
        updateWrapper.set(appStockComposeTask.getOrigin() != null, AppStockComposeTask::getOrigin, appStockComposeTask.getOrigin());
        updateWrapper.set(appStockComposeTask.getDestination() != null, AppStockComposeTask::getDestination, appStockComposeTask.getDestination());
        updateWrapper.set(appStockComposeTask.getTaskStatus() != null, AppStockComposeTask::getTaskStatus, appStockComposeTask.getTaskStatus());
        updateWrapper.set(appStockComposeTask.getStepStatus() != null, AppStockComposeTask::getStepStatus, appStockComposeTask.getStepStatus());
        updateWrapper.set(appStockComposeTask.getCanCancel() != null, AppStockComposeTask::getCanCancel, appStockComposeTask.getCanCancel());
        updateWrapper.set(appStockComposeTask.getPriority() != null, AppStockComposeTask::getPriority, appStockComposeTask.getPriority());
        updateWrapper.set(appStockComposeTask.getVehicleNo() != null, AppStockComposeTask::getVehicleNo, appStockComposeTask.getVehicleNo());
        updateWrapper.set(appStockComposeTask.getVehicleSize() != null, AppStockComposeTask::getVehicleSize, appStockComposeTask.getVehicleSize());
        updateWrapper.set(appStockComposeTask.getWeight() != null, AppStockComposeTask::getWeight, appStockComposeTask.getWeight());
        updateWrapper.set(AppStockComposeTask::getUpdateTime, LocalDateTime.now());
        updateWrapper.set(appStockComposeTask.getStartTime() != null, AppStockComposeTask::getStartTime, appStockComposeTask.getStartTime());
        updateWrapper.set(appStockComposeTask.getCompleteTime() != null, AppStockComposeTask::getCompleteTime, appStockComposeTask.getCompleteTime());
        updateWrapper.set(appStockComposeTask.getEndTime() != null, AppStockComposeTask::getEndTime, appStockComposeTask.getEndTime());
        updateWrapper.set(appStockComposeTask.getTaskSource() != null, AppStockComposeTask::getTaskSource, appStockComposeTask.getTaskSource());
        updateWrapper.set(appStockComposeTask.getCreatePerson() != null, AppStockComposeTask::getCreatePerson, appStockComposeTask.getCreatePerson());
        updateWrapper.set(appStockComposeTask.getTaskMsg() != null, AppStockComposeTask::getTaskMsg, appStockComposeTask.getTaskMsg());;
        try {
            return super.update(appStockComposeTask, updateWrapper);
        } catch (Exception e) {
            return false;
        }
    }

    /**
     * 更新任务，失败跑出异常
     * @param appStockComposeTask 待更新任务
     * @return 更新结果
     */
    @Override
    public boolean updateThrowEx(AppStockComposeTask appStockComposeTask) {
        LambdaUpdateWrapper<AppStockComposeTask> updateWrapper = new LambdaUpdateWrapper<>();
        updateWrapper.eq(AppStockComposeTask::getTaskId, appStockComposeTask.getTaskId());
        updateWrapper.set(appStockComposeTask.getTaskGroup() != null, AppStockComposeTask::getTaskGroup, appStockComposeTask.getTaskGroup());
        updateWrapper.set(appStockComposeTask.getUpperTaskId() != null, AppStockComposeTask::getUpperTaskId, appStockComposeTask.getUpperTaskId());
        updateWrapper.set(appStockComposeTask.getTaskType() != null, AppStockComposeTask::getTaskType, appStockComposeTask.getTaskType());
        updateWrapper.set(appStockComposeTask.getOrigin() != null, AppStockComposeTask::getOrigin, appStockComposeTask.getOrigin());
        updateWrapper.set(appStockComposeTask.getDestination() != null, AppStockComposeTask::getDestination, appStockComposeTask.getDestination());
        updateWrapper.set(appStockComposeTask.getTaskStatus() != null, AppStockComposeTask::getTaskStatus, appStockComposeTask.getTaskStatus());
        updateWrapper.set(appStockComposeTask.getStepStatus() != null, AppStockComposeTask::getStepStatus, appStockComposeTask.getStepStatus());
        updateWrapper.set(appStockComposeTask.getCanCancel() != null, AppStockComposeTask::getCanCancel, appStockComposeTask.getCanCancel());
        updateWrapper.set(appStockComposeTask.getPriority() != null, AppStockComposeTask::getPriority, appStockComposeTask.getPriority());
        updateWrapper.set(appStockComposeTask.getVehicleNo() != null, AppStockComposeTask::getVehicleNo, appStockComposeTask.getVehicleNo());
        updateWrapper.set(appStockComposeTask.getVehicleSize() != null, AppStockComposeTask::getVehicleSize, appStockComposeTask.getVehicleSize());
        updateWrapper.set(appStockComposeTask.getWeight() != null, AppStockComposeTask::getWeight, appStockComposeTask.getWeight());
        updateWrapper.set(AppStockComposeTask::getUpdateTime, LocalDateTime.now());
        updateWrapper.set(appStockComposeTask.getStartTime() != null, AppStockComposeTask::getStartTime, appStockComposeTask.getStartTime());
        updateWrapper.set(appStockComposeTask.getCompleteTime() != null, AppStockComposeTask::getCompleteTime, appStockComposeTask.getCompleteTime());
        updateWrapper.set(appStockComposeTask.getEndTime() != null, AppStockComposeTask::getEndTime, appStockComposeTask.getEndTime());
        updateWrapper.set(appStockComposeTask.getTaskSource() != null, AppStockComposeTask::getTaskSource, appStockComposeTask.getTaskSource());
        updateWrapper.set(appStockComposeTask.getCreatePerson() != null, AppStockComposeTask::getCreatePerson, appStockComposeTask.getCreatePerson());
        updateWrapper.set(appStockComposeTask.getTaskMsg() != null, AppStockComposeTask::getTaskMsg, appStockComposeTask.getTaskMsg());;
        return super.update(appStockComposeTask, updateWrapper);
    }

    /**
     * 设置任务状态和消息
     * @param taskId 任务ID
     * @param status 状态
     * @param stepStatus 步骤状态
     * @param msg 消息
     * @return 结果
     */
    @Override
    public boolean setTaskStatusAndMsg(String taskId, StockComposeTaskStatusEnum status, StockComposeTaskStepStatusEnum stepStatus, String msg) {
        if(taskId == null || taskId.isEmpty() || status == null || status.getCode() == null ||  status.getCode() < 0) {
            return false;
        }
        LambdaUpdateWrapper<AppStockComposeTask> updateWrapper = new LambdaUpdateWrapper<>();
        updateWrapper.eq(AppStockComposeTask::getTaskId, taskId)
                .set(AppStockComposeTask::getTaskStatus, status.getCode())
                .set(msg != null, AppStockComposeTask::getTaskMsg, msg);
        try {
            return super.update(updateWrapper);
        } catch (Exception e) {
            return false;
        }
    }

    /**
     * 根据任务ID和任务状态修改任务状态和消息
     * @param taskId 任务ID
     * @param oldStatus 旧任务状态
     * @param newStatus 新任务状态
     * @param msg 消息
     * @return 结果
     */
    @Override
    public int setTaskStatusAndMsgWithOldTaskStatus(String taskId, Integer oldStatus, StockComposeTaskStatusEnum newStatus, String msg) {
        LambdaUpdateWrapper<AppStockComposeTask> updateWrapper = new LambdaUpdateWrapper<>();
        updateWrapper.eq(AppStockComposeTask::getTaskId, taskId)
                .eq(AppStockComposeTask::getTaskStatus, oldStatus)
                .set(AppStockComposeTask::getTaskStatus, newStatus.getCode())
                .set(msg != null, AppStockComposeTask::getTaskMsg, msg);
        try {
            return baseMapper.update(updateWrapper);
        } catch (Exception e) {
            return 0;
        }
    }


    /**
     * 根据任务ID删除数据并抛出异常
     * @param taskId 待删除任务ID
     * @return 删除结果
     */
    @Override
    public boolean deleteWithTaskIdThrowEx(String taskId) {
        return super.removeById(taskId);
    }
}
