package org.wcs.mapper.impl;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
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.AppConveyTaskBakMapper;
import org.wcs.mapper.intf.AppConveyTaskBakService;
import org.wcs.model.bo.tuple.Tuple2;
import org.wcs.model.dto.serve.conveyTask.QueryConveyTaskReq;
import org.wcs.model.po.app.AppConveyTaskBak;
import org.wcs.utils.AppStringUtils;

import java.util.List;

@Service
public class AppConveyTaskBakDao extends ServiceImpl<AppConveyTaskBakMapper, AppConveyTaskBak> implements AppConveyTaskBakService {

    /**
     * 查询 conveyTaskBak
     * @param request 查询参数
     * @param pageSize 每页数量
     * @param pageIndex 页码
     * @return 查询结果
     */
    @Override
    public Tuple2<Long, List<AppConveyTaskBak>> queryConveyTaskBak(QueryConveyTaskReq request, Integer pageSize, Integer pageIndex) {
        if(request == null || pageSize == null || pageIndex == null || pageSize <= 0 || pageIndex <= 0) {
            return null;
        }
        Integer plcTaskId = null;
        if(AppStringUtils.isNotEmpty(request.getPlcTaskId())) {
            plcTaskId = AppStringUtils.forceToInt(request.getPlcTaskId());
        }
        LambdaQueryWrapper<AppConveyTaskBak> queryWrapper = new LambdaQueryWrapper<>();
        queryWrapper.like(AppStringUtils.isNotEmpty(request.getTaskId()), AppConveyTaskBak::getTaskId, request.getTaskId());
        queryWrapper.like(AppStringUtils.isNotEmpty(request.getVehicleNo()), AppConveyTaskBak::getVehicleNo, request.getVehicleNo());
        queryWrapper.eq(request.getTaskStatus() != null, AppConveyTaskBak::getTaskStatus, request.getTaskStatus());
        queryWrapper.eq(request.getTaskType() != null, AppConveyTaskBak::getTaskType, request.getTaskType());
        queryWrapper.like(AppStringUtils.isNotEmpty(request.getLocation()), AppConveyTaskBak::getLocation, request.getLocation());
        queryWrapper.eq(plcTaskId != null, AppConveyTaskBak::getPlcId, plcTaskId);
        queryWrapper.like(AppStringUtils.isNotEmpty(request.getArriveLocation()), AppConveyTaskBak::getArriveLocation, request.getArriveLocation());
        if(request.getCreateTimeRange() != null && request.getCreateTimeRange().length == 2) {
            queryWrapper.between(request.getCreateTimeRange()[0] != null && request.getCreateTimeRange()[1] != null, AppConveyTaskBak::getCreateTime, request.getCreateTimeRange()[0], request.getCreateTimeRange()[1]);
        }
        if(request.getArriveTimeRange() != null && request.getArriveTimeRange().length == 2) {
            queryWrapper.between(request.getArriveTimeRange()[0] != null && request.getArriveTimeRange()[1] != null, AppConveyTaskBak::getArriveTime, request.getArriveTimeRange()[0], request.getArriveTimeRange()[1]);
        }
        queryWrapper.orderByDesc(AppConveyTaskBak::getCreateTime);
        Page<AppConveyTaskBak> page = new Page<>(pageIndex, pageSize);
        try {
            Page<AppConveyTaskBak> pageResult = super.baseMapper.selectPage(page, queryWrapper);
            return new Tuple2<>(pageResult.getTotal(), pageResult.getRecords());
        } catch (Exception e) {
            return null;
        }
    }

    /**
     * 根据 taskId 查询 conveyTaskBak
     * @param taskId 任务ID
     * @return 查询结果
     */
    @Override
    public List<AppConveyTaskBak> queryByTaskId(String taskId) {
        LambdaQueryWrapper<AppConveyTaskBak> queryWrapper = new LambdaQueryWrapper<>();
        queryWrapper.eq(AppConveyTaskBak::getTaskId, taskId);
        queryWrapper.orderByDesc(AppConveyTaskBak::getCreateTime);
        try {
            return super.list(queryWrapper);
        } catch (Exception e) {
            return null;
        }
    }

    /**
     * 查询 conveyTaskBak
     * @param request 搜索参数
     * @return 搜索结果
     */
    @Override
    public List<AppConveyTaskBak> queryConveyTaskBak(QueryConveyTaskReq request) {
        if(request == null) {
            return null;
        }
        Integer plcTaskId = null;
        if(AppStringUtils.isNotEmpty(request.getPlcTaskId())) {
            plcTaskId = AppStringUtils.forceToInt(request.getPlcTaskId());
        }
        LambdaQueryWrapper<AppConveyTaskBak> queryWrapper = new LambdaQueryWrapper<>();
        queryWrapper.like(AppStringUtils.isNotEmpty(request.getTaskId()), AppConveyTaskBak::getTaskId, request.getTaskId());
        queryWrapper.like(AppStringUtils.isNotEmpty(request.getVehicleNo()), AppConveyTaskBak::getVehicleNo, request.getVehicleNo());
        queryWrapper.eq(request.getTaskStatus() != null, AppConveyTaskBak::getTaskStatus, request.getTaskStatus());
        queryWrapper.eq(request.getTaskType() != null, AppConveyTaskBak::getTaskType, request.getTaskType());
        queryWrapper.like(AppStringUtils.isNotEmpty(request.getLocation()), AppConveyTaskBak::getLocation, request.getLocation());
        queryWrapper.eq(plcTaskId != null, AppConveyTaskBak::getPlcId, plcTaskId);
        queryWrapper.like(AppStringUtils.isNotEmpty(request.getArriveLocation()), AppConveyTaskBak::getArriveLocation, request.getArriveLocation());
        if(request.getCreateTimeRange() != null && request.getCreateTimeRange().length == 2) {
            queryWrapper.between(request.getCreateTimeRange()[0] != null && request.getCreateTimeRange()[1] != null, AppConveyTaskBak::getCreateTime, request.getCreateTimeRange()[0], request.getCreateTimeRange()[1]);
        }
        if(request.getArriveTimeRange() != null && request.getArriveTimeRange().length == 2) {
            queryWrapper.between(request.getArriveTimeRange()[0] != null && request.getArriveTimeRange()[1] != null, AppConveyTaskBak::getArriveTime, request.getArriveTimeRange()[0], request.getArriveTimeRange()[1]);
        }
        queryWrapper.orderByDesc(AppConveyTaskBak::getCreateTime);
        try {
            return super.list(queryWrapper);
        } catch (Exception e) {
            return null;
        }
    }


}
