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.mapper.AppStockSingleTaskMapper;
import org.wcs.mapper.intf.AppStockSingleTaskService;
import org.wcs.model.bo.tuple.Tuple2;
import org.wcs.model.dto.serve.stockSingleTask.QueryStockSingleTaskReq;
import org.wcs.model.po.app.AppStockSingleTask;
import org.wcs.utils.AppStringUtils;
import org.wcs.utils.AppTimeUtils;

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

@Service
public class AppStockSingleTaskDao extends ServiceImpl<AppStockSingleTaskMapper, AppStockSingleTask> implements AppStockSingleTaskService {

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

    /**
     * 根据PLC任务ID查询数据
     * @param plcTaskId PLC任务ID
     * @return 列表
     */
    @Override
    public List<AppStockSingleTask> queryByPlcTaskId(Integer plcTaskId) {
        LambdaQueryWrapper<AppStockSingleTask> queryWrapper = new LambdaQueryWrapper<>();
        queryWrapper.eq(AppStockSingleTask::getPlcTaskId, plcTaskId);
        queryWrapper.orderByDesc(AppStockSingleTask::getCreateTime);
        try {
            return super.list(queryWrapper);
        } catch (Exception e) {
            return null;
        }
    }

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

    /**
     * 查询
     * @param queryTaskReq 查询参数
     * @param pageSize 分页单页大小
     * @param pageIndex 分页页码
     * @return 结果
     */
    @Override
    public Tuple2<Long, List<AppStockSingleTask>> query(QueryStockSingleTaskReq queryTaskReq, Integer pageSize, Integer pageIndex) {
        if(pageIndex == null || pageIndex < 1 || pageSize == null || pageSize < 1) {
            return null;
        }
        if(AppStringUtils.isNotEmpty(queryTaskReq.getPlcTaskId()) && !AppStringUtils.isNumber(queryTaskReq.getPlcTaskId())) {
            return null;
        }
        Integer plcTaskId = AppStringUtils.isEmpty(queryTaskReq.getPlcTaskId()) ? null : Integer.parseInt(queryTaskReq.getPlcTaskId());
        LambdaQueryWrapper<AppStockSingleTask> queryWrapper = new LambdaQueryWrapper<>();
        queryWrapper.like(AppStringUtils.isNotEmpty(queryTaskReq.getTaskId()), AppStockSingleTask::getTaskId, queryTaskReq.getTaskId());
        queryWrapper.like(AppStringUtils.isNotEmpty(queryTaskReq.getTaskGroup()), AppStockSingleTask::getTaskGroup, queryTaskReq.getTaskGroup());
        queryWrapper.eq(plcTaskId != null, AppStockSingleTask::getPlcTaskId, plcTaskId);
        queryWrapper.eq(queryTaskReq.getExecuteMachine() != null, AppStockSingleTask::getExecuteMachine, queryTaskReq.getExecuteMachine());
        queryWrapper.like(AppStringUtils.isNotEmpty(queryTaskReq.getUpperTaskId()), AppStockSingleTask::getUpperTaskId, queryTaskReq.getUpperTaskId());
        queryWrapper.like(AppStringUtils.isNotEmpty(queryTaskReq.getVehicleNo()), AppStockSingleTask::getVehicleNo, queryTaskReq.getVehicleNo());
        queryWrapper.eq(queryTaskReq.getTaskType() != null, AppStockSingleTask::getTaskType, queryTaskReq.getTaskType());
        queryWrapper.like(AppStringUtils.isNotEmpty(queryTaskReq.getOrigin()), AppStockSingleTask::getOrigin, queryTaskReq.getOrigin());
        queryWrapper.like(AppStringUtils.isNotEmpty(queryTaskReq.getDestination()), AppStockSingleTask::getDestination, queryTaskReq.getDestination());
        queryWrapper.eq(queryTaskReq.getTaskStatus() != null, AppStockSingleTask::getTaskStatus, queryTaskReq.getTaskStatus());
        if(queryTaskReq.getCreateTimeRange() != null && queryTaskReq.getCreateTimeRange().size() >= 2) {
            queryWrapper.between(AppStockSingleTask::getCreateTime, queryTaskReq.getCreateTimeRange().get(0), queryTaskReq.getCreateTimeRange().get(1));
        }
        if(queryTaskReq.getEndTimeRange() != null && queryTaskReq.getEndTimeRange().size() >= 2) {
            queryWrapper.between(AppStockSingleTask::getEndTime, queryTaskReq.getEndTimeRange().get(0), queryTaskReq.getEndTimeRange().get(1));
        }
        queryWrapper.orderByDesc(AppStockSingleTask::getCreateTime);
        Page<AppStockSingleTask> page = new Page<>(pageIndex, pageSize);
        try {
            Page<AppStockSingleTask> pageResult = super.baseMapper.selectPage(page, queryWrapper);
            return new Tuple2<>(pageResult.getTotal(), pageResult.getRecords());
        } catch (Exception ex) {
            return null;
        }
    }

    /**
     * 插入一条记录
     * @param record 数据
     * @return 插入结果
     */
    @Override
    public boolean insert(AppStockSingleTask record) {
        if(record == null) {
            return false;
        }
        record.setCreateTime(LocalDateTime.now());
        record.setUpdateTime(LocalDateTime.now());
        try {
            return super.save(record);
        } catch (Exception e) {
            return false;
        }
    }

    /**
     * 根据主键更新一条记录，为 null 的不更新
     * @param record 数据
     * @return 更新结果
     */
    @Override
    public boolean update(AppStockSingleTask record) {
        record.setUpdateTime(LocalDateTime.now());
        LambdaUpdateWrapper<AppStockSingleTask> updateWrapper = new LambdaUpdateWrapper<>();
        updateWrapper.eq(AppStockSingleTask::getTaskId, record.getTaskId());
        updateWrapper.set(record.getTaskGroup() != null, AppStockSingleTask::getTaskGroup, record.getTaskGroup());
        updateWrapper.set(record.getPlcTaskId() != null, AppStockSingleTask::getPlcTaskId, record.getPlcTaskId());
        updateWrapper.set(record.getUpperTaskId() != null, AppStockSingleTask::getUpperTaskId, record.getUpperTaskId());
        updateWrapper.set(record.getTaskType() != null, AppStockSingleTask::getTaskType, record.getTaskType());
        updateWrapper.set(record.getExecuteMachine() != null, AppStockSingleTask::getExecuteMachine, record.getExecuteMachine());
        updateWrapper.set(record.getOrigin() != null, AppStockSingleTask::getOrigin, record.getOrigin());
        updateWrapper.set(record.getDestination() != null, AppStockSingleTask::getDestination, record.getDestination());
        updateWrapper.set(record.getComposeDestination() != null, AppStockSingleTask::getComposeDestination, record.getComposeDestination());
        updateWrapper.set(record.getTaskStatus() != null, AppStockSingleTask::getTaskStatus, record.getTaskStatus());
        updateWrapper.set(record.getCanCancel() != null, AppStockSingleTask::getCanCancel, record.getCanCancel());
        updateWrapper.set(record.getPriority() != null, AppStockSingleTask::getPriority, record.getPriority());
        updateWrapper.set(record.getVehicleNo() != null, AppStockSingleTask::getVehicleNo, record.getVehicleNo());
        updateWrapper.set(record.getVehicleNoNumber() != null, AppStockSingleTask::getVehicleNoNumber, record.getVehicleNoNumber());
        updateWrapper.set(record.getVehicleSize() != null, AppStockSingleTask::getVehicleSize, record.getVehicleSize());
        updateWrapper.set(record.getWeight() != null, AppStockSingleTask::getWeight, record.getWeight());
        updateWrapper.set(record.getUpdateTime() != null, AppStockSingleTask::getUpdateTime, record.getUpdateTime());
        updateWrapper.set(record.getStartTime() != null, AppStockSingleTask::getStartTime, record.getStartTime());
        updateWrapper.set(record.getCompleteTime() != null, AppStockSingleTask::getCompleteTime, record.getCompleteTime());
        updateWrapper.set(record.getEndTime() != null, AppStockSingleTask::getEndTime, record.getEndTime());
        updateWrapper.set(record.getTaskSource() != null, AppStockSingleTask::getTaskSource, record.getTaskSource());
        updateWrapper.set(record.getCreatePerson() != null, AppStockSingleTask::getCreatePerson, record.getCreatePerson());
        updateWrapper.set(record.getTaskMsg() != null, AppStockSingleTask::getTaskMsg, record.getTaskMsg());
        try {
            return super.update(record, updateWrapper);
        } catch (Exception e) {
            return false;
        }
    }

    /**
     * 根据任务ID更新任务状态,意外就抛出异常
     * @param upperTaskId 上位任务ID
     * @param taskStatus 任务状态
     * @param newStatus 新状态
     * @param msg 状态信息
     * @return 更新结果
     */
    @Override
    public boolean updateWithUpperTaskIdAndStatusThrowEx(String upperTaskId, List<Integer> taskStatus, Integer newStatus, String msg) {
        LambdaUpdateWrapper<AppStockSingleTask> updateWrapper = new LambdaUpdateWrapper<>();
        updateWrapper.eq(AppStockSingleTask::getUpperTaskId, upperTaskId);
        updateWrapper.in(AppStockSingleTask::getTaskStatus, taskStatus);
        updateWrapper.set(AppStockSingleTask::getTaskStatus, newStatus);
        updateWrapper.set(AppStockSingleTask::getTaskMsg, msg);
        return super.update(updateWrapper);
    }

    /**
     * 根据PLC任务ID更新一条记录，为 null 的不更新
     * @param record 数据
     * @return 更新结果
     */
    @Override
    public boolean updateWithPlcTaskId(Integer plcTaskId, AppStockSingleTask record) {
        record.setUpdateTime(LocalDateTime.now());
        LambdaUpdateWrapper<AppStockSingleTask> updateWrapper = new LambdaUpdateWrapper<>();
        updateWrapper.eq(AppStockSingleTask::getPlcTaskId, plcTaskId);
        updateWrapper.set(record.getTaskGroup() != null, AppStockSingleTask::getTaskGroup, record.getTaskGroup());
        updateWrapper.set(record.getPlcTaskId() != null, AppStockSingleTask::getPlcTaskId, record.getPlcTaskId());
        updateWrapper.set(record.getUpperTaskId() != null, AppStockSingleTask::getUpperTaskId, record.getUpperTaskId());
        updateWrapper.set(record.getTaskType() != null, AppStockSingleTask::getTaskType, record.getTaskType());
        updateWrapper.set(record.getExecuteMachine() != null, AppStockSingleTask::getExecuteMachine, record.getExecuteMachine());
        updateWrapper.set(record.getOrigin() != null, AppStockSingleTask::getOrigin, record.getOrigin());
        updateWrapper.set(record.getDestination() != null, AppStockSingleTask::getDestination, record.getDestination());
        updateWrapper.set(record.getComposeDestination() != null, AppStockSingleTask::getComposeDestination, record.getComposeDestination());
        updateWrapper.set(record.getTaskStatus() != null, AppStockSingleTask::getTaskStatus, record.getTaskStatus());
        updateWrapper.set(record.getCanCancel() != null, AppStockSingleTask::getCanCancel, record.getCanCancel());
        updateWrapper.set(record.getPriority() != null, AppStockSingleTask::getPriority, record.getPriority());
        updateWrapper.set(record.getVehicleNo() != null, AppStockSingleTask::getVehicleNo, record.getVehicleNo());
        updateWrapper.set(record.getVehicleNoNumber() != null, AppStockSingleTask::getVehicleNoNumber, record.getVehicleNoNumber());
        updateWrapper.set(record.getVehicleSize() != null, AppStockSingleTask::getVehicleSize, record.getVehicleSize());
        updateWrapper.set(record.getWeight() != null, AppStockSingleTask::getWeight, record.getWeight());
        updateWrapper.set(record.getUpdateTime() != null, AppStockSingleTask::getUpdateTime, record.getUpdateTime());
        updateWrapper.set(record.getStartTime() != null, AppStockSingleTask::getStartTime, record.getStartTime());
        updateWrapper.set(record.getCompleteTime() != null, AppStockSingleTask::getCompleteTime, record.getCompleteTime());
        updateWrapper.set(record.getEndTime() != null, AppStockSingleTask::getEndTime, record.getEndTime());
        updateWrapper.set(record.getTaskSource() != null, AppStockSingleTask::getTaskSource, record.getTaskSource());
        updateWrapper.set(record.getCreatePerson() != null, AppStockSingleTask::getCreatePerson, record.getCreatePerson());
        updateWrapper.set(record.getTaskMsg() != null, AppStockSingleTask::getTaskMsg, record.getTaskMsg());
        try {
            return super.update(record, updateWrapper);
        } catch (Exception e) {
            return false;
        }
    }

    @Override
    public boolean updateThrowException(AppStockSingleTask record) {
        record.setUpdateTime(LocalDateTime.now());
        LambdaUpdateWrapper<AppStockSingleTask> updateWrapper = new LambdaUpdateWrapper<>();
        updateWrapper.eq(AppStockSingleTask::getTaskId, record.getTaskId());
        updateWrapper.set(record.getTaskGroup() != null, AppStockSingleTask::getTaskGroup, record.getTaskGroup());
        updateWrapper.set(record.getPlcTaskId() != null, AppStockSingleTask::getPlcTaskId, record.getPlcTaskId());
        updateWrapper.set(record.getUpperTaskId() != null, AppStockSingleTask::getUpperTaskId, record.getUpperTaskId());
        updateWrapper.set(record.getTaskType() != null, AppStockSingleTask::getTaskType, record.getTaskType());
        updateWrapper.set(record.getExecuteMachine() != null, AppStockSingleTask::getExecuteMachine, record.getExecuteMachine());
        updateWrapper.set(record.getOrigin() != null, AppStockSingleTask::getOrigin, record.getOrigin());
        updateWrapper.set(record.getDestination() != null, AppStockSingleTask::getDestination, record.getDestination());
        updateWrapper.set(record.getComposeDestination() != null, AppStockSingleTask::getComposeDestination, record.getComposeDestination());
        updateWrapper.set(record.getTaskStatus() != null, AppStockSingleTask::getTaskStatus, record.getTaskStatus());
        updateWrapper.set(record.getCanCancel() != null, AppStockSingleTask::getCanCancel, record.getCanCancel());
        updateWrapper.set(record.getPriority() != null, AppStockSingleTask::getPriority, record.getPriority());
        updateWrapper.set(record.getVehicleNo() != null, AppStockSingleTask::getVehicleNo, record.getVehicleNo());
        updateWrapper.set(record.getVehicleNoNumber() != null, AppStockSingleTask::getVehicleNoNumber, record.getVehicleNoNumber());
        updateWrapper.set(record.getVehicleSize() != null, AppStockSingleTask::getVehicleSize, record.getVehicleSize());
        updateWrapper.set(record.getWeight() != null, AppStockSingleTask::getWeight, record.getWeight());
        updateWrapper.set(record.getUpdateTime() != null, AppStockSingleTask::getUpdateTime, record.getUpdateTime());
        updateWrapper.set(record.getStartTime() != null, AppStockSingleTask::getStartTime, record.getStartTime());
        updateWrapper.set(record.getCompleteTime() != null, AppStockSingleTask::getCompleteTime, record.getCompleteTime());
        updateWrapper.set(record.getEndTime() != null, AppStockSingleTask::getEndTime, record.getEndTime());
        updateWrapper.set(record.getTaskSource() != null, AppStockSingleTask::getTaskSource, record.getTaskSource());
        updateWrapper.set(record.getCreatePerson() != null, AppStockSingleTask::getCreatePerson, record.getCreatePerson());
        updateWrapper.set(record.getTaskMsg() != null, AppStockSingleTask::getTaskMsg, record.getTaskMsg());
        return super.update(record, updateWrapper);
    }

    /**
     * 删除任务
     * @param taskId 任务ID
     * @return 删除结果
     */
    @Override
    public boolean deleteByTaskIdThrowEx(String taskId) {
        return super.removeById(taskId);
    }

    /**
     * 查询指定状态的任务
     * @param taskStatus 任务状态
     * @return 结果
     */
    @Override
    public List<AppStockSingleTask> queryTask(Integer taskStatus) {
        LambdaQueryWrapper<AppStockSingleTask> queryWrapper = new LambdaQueryWrapper<>();
        queryWrapper.eq(AppStockSingleTask::getTaskStatus, taskStatus);
        queryWrapper.orderByDesc(AppStockSingleTask::getPriority);
        queryWrapper.orderByAsc(AppStockSingleTask::getCreateTime);
        try {
            return super.list(queryWrapper);
        } catch (Exception e) {
            return null;
        }
    }

    /**
     * 批量查询指定状态的任务
     * @param taskTypes 任务类型
     * @param taskStatus 任务状态
     * @return 结果
     */
    @Override
    public List<AppStockSingleTask> queryTask(List<Integer> taskTypes, Integer taskStatus) {
        LambdaQueryWrapper<AppStockSingleTask> queryWrapper = new LambdaQueryWrapper<>();
        queryWrapper.eq(AppStockSingleTask::getTaskStatus, taskStatus);
        queryWrapper.in(AppStockSingleTask::getTaskType, taskTypes);
        queryWrapper.orderByDesc(AppStockSingleTask::getPriority);
        queryWrapper.orderByAsc(AppStockSingleTask::getCreateTime);
        try {
            return super.list(queryWrapper);
        } catch (Exception e) {
            return null;
        }
    }

    /**
     * 查询指定时间段内指定状态的任务
     * @param days 天数
     * @param taskStatus 状态
     * @return 列表
     */
    @Override
    public List<AppStockSingleTask> queryTaskRecentDays(int days, Integer taskStatus) {
        try {
            LambdaQueryWrapper<AppStockSingleTask> queryWrapper = new LambdaQueryWrapper<>();
            queryWrapper.eq(AppStockSingleTask::getTaskStatus, taskStatus);
            queryWrapper.ge(AppStockSingleTask::getCreateTime, AppTimeUtils.getTodayEndTime().minusDays(days));
            return super.list(queryWrapper);
        } catch (Exception e) {
            return null;
        }
    }

    /**
     * 查询指定堆垛机指定箱号的任务
     * @param stackerId 堆垛机ID
     * @param vehicleNo 载具编号
     * @param taskStatus 任务状态
     * @param taskType 任务类型
     * @return 结果
     */
    @Override
    public List<AppStockSingleTask> queryStackerTask(Integer stackerId, String vehicleNo, Integer taskStatus, Integer taskType) {
        try {
            return baseMapper.queryStackerTaskWithVehicle(stackerId, vehicleNo, taskStatus, taskType);
        } catch (Exception e) {
            return null;
        }
    }

    /**
     * 查询指定堆垛机入库任务
     * @param stackerId 堆垛机ID
     * @param taskStatus 状态
     * @param taskType 任务类型
     * @return 移动任务
     */
    @Override
    public List<AppStockSingleTask> queryStackerTask(Integer stackerId, Integer taskStatus, Integer taskType) {
        try{
            return baseMapper.queryStackerTask(stackerId, taskStatus, taskType);
        } catch (Exception e) {
            return null;
        }
    }



}
