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.AppStockComposeTaskBakMapper;
import org.wcs.mapper.intf.AppStockComposeTaskBakService;
import org.wcs.model.bo.tuple.Tuple2;
import org.wcs.model.dto.serve.stcokComposeTask.QueryStockComposeTaskReq;
import org.wcs.model.po.app.AppStockComposeTaskBak;
import org.wcs.utils.AppStringUtils;

import java.util.List;

@Service
public class AppStockComposeTaskBakDao extends ServiceImpl<AppStockComposeTaskBakMapper, AppStockComposeTaskBak> implements AppStockComposeTaskBakService {

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

    /**
     * 插入数据，如果插入数据失败，则抛出异常
     *
     * @param appStockComposeTaskBak 待插入数据
     * @return 插入结果
     */
    @Override
    public boolean insertThrowEx(AppStockComposeTaskBak appStockComposeTaskBak) {
        return super.save(appStockComposeTaskBak);
    }

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

    /**
     * 根据查询参数查询数据
     *
     * @param queryTaskReq 查询参数
     * @return 查询结果
     */
    @Override
    public List<AppStockComposeTaskBak> query(QueryStockComposeTaskReq queryTaskReq) {
        LambdaQueryWrapper<AppStockComposeTaskBak> queryWrapper = new LambdaQueryWrapper<>();
        queryWrapper.like(AppStringUtils.isNotEmpty(queryTaskReq.getTaskId()), AppStockComposeTaskBak::getTaskId, queryTaskReq.getTaskId());
        queryWrapper.like(AppStringUtils.isNotEmpty(queryTaskReq.getTaskGroup()), AppStockComposeTaskBak::getTaskGroup, queryTaskReq.getTaskGroup());
        queryWrapper.like(AppStringUtils.isNotEmpty(queryTaskReq.getUpperTaskId()), AppStockComposeTaskBak::getUpperTaskId, queryTaskReq.getUpperTaskId());
        queryWrapper.eq(queryTaskReq.getTaskType() != null, AppStockComposeTaskBak::getTaskType, queryTaskReq.getTaskType());
        queryWrapper.like(AppStringUtils.isNotEmpty(queryTaskReq.getVehicleNo()), AppStockComposeTaskBak::getVehicleNo, queryTaskReq.getVehicleNo());
        queryWrapper.like(AppStringUtils.isNotEmpty(queryTaskReq.getOrigin()), AppStockComposeTaskBak::getOrigin, queryTaskReq.getOrigin());
        queryWrapper.like(AppStringUtils.isNotEmpty(queryTaskReq.getDestination()), AppStockComposeTaskBak::getDestination, queryTaskReq.getDestination());
        queryWrapper.eq(queryTaskReq.getTaskStatus() != null, AppStockComposeTaskBak::getTaskStatus, queryTaskReq.getTaskStatus());
        if(queryTaskReq.getCreateTimeRange() != null && queryTaskReq.getCreateTimeRange().size() == 2) {
            queryWrapper.between(AppStockComposeTaskBak::getCreateTime, queryTaskReq.getCreateTimeRange().get(0), queryTaskReq.getCreateTimeRange().get(1));
        }
        if(queryTaskReq.getEndTimeRange() != null && queryTaskReq.getEndTimeRange().size() == 2) {
            queryWrapper.between(AppStockComposeTaskBak::getEndTime, queryTaskReq.getEndTimeRange().get(0), queryTaskReq.getEndTimeRange().get(1));
        }
        queryWrapper.orderByDesc(AppStockComposeTaskBak::getCreateTime);
        try {
            return super.baseMapper.selectList(queryWrapper);
        } catch (Exception ex) {
            return null;
        }
    }

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