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

import java.util.List;

@Service
public class AppStockSingleTaskBakDao extends ServiceImpl<AppStockSingleTaskBakMapper, AppStockSingleTaskBak> implements AppStockSingleTaskBakService {

    /**
     * 查询任务列表
     * @param queryTaskReq 查询参数
     * @param pageSize 每页数量
     * @param pageIndex 页码
     * @return 查询结果
     */
    @Override
    public Tuple2<Long, List<AppStockSingleTaskBak>> 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<AppStockSingleTaskBak> queryWrapper = new LambdaQueryWrapper<>();
        queryWrapper.like(AppStringUtils.isNotEmpty(queryTaskReq.getTaskId()), AppStockSingleTaskBak::getTaskId, queryTaskReq.getTaskId());
        queryWrapper.like(AppStringUtils.isNotEmpty(queryTaskReq.getTaskGroup()), AppStockSingleTaskBak::getTaskGroup, queryTaskReq.getTaskGroup());
        queryWrapper.eq(plcTaskId != null, AppStockSingleTaskBak::getPlcTaskId, plcTaskId);
        queryWrapper.eq(queryTaskReq.getExecuteMachine() != null, AppStockSingleTaskBak::getExecuteMachine, queryTaskReq.getExecuteMachine());
        queryWrapper.like(AppStringUtils.isNotEmpty(queryTaskReq.getUpperTaskId()), AppStockSingleTaskBak::getUpperTaskId, queryTaskReq.getUpperTaskId());
        queryWrapper.like(AppStringUtils.isNotEmpty(queryTaskReq.getVehicleNo()), AppStockSingleTaskBak::getVehicleNo, queryTaskReq.getVehicleNo());
        queryWrapper.eq(queryTaskReq.getTaskType() != null, AppStockSingleTaskBak::getTaskType, queryTaskReq.getTaskType());
        queryWrapper.like(AppStringUtils.isNotEmpty(queryTaskReq.getOrigin()), AppStockSingleTaskBak::getOrigin, queryTaskReq.getOrigin());
        queryWrapper.like(AppStringUtils.isNotEmpty(queryTaskReq.getDestination()), AppStockSingleTaskBak::getDestination, queryTaskReq.getDestination());
        queryWrapper.eq(queryTaskReq.getTaskStatus() != null, AppStockSingleTaskBak::getTaskStatus, queryTaskReq.getTaskStatus());
        if(queryTaskReq.getCreateTimeRange() != null && queryTaskReq.getCreateTimeRange().size() >= 2) {
            queryWrapper.between(AppStockSingleTaskBak::getCreateTime, queryTaskReq.getCreateTimeRange().get(0), queryTaskReq.getCreateTimeRange().get(1));
        }
        if(queryTaskReq.getEndTimeRange() != null && queryTaskReq.getEndTimeRange().size() >= 2) {
            queryWrapper.between(AppStockSingleTaskBak::getEndTime, queryTaskReq.getEndTimeRange().get(0), queryTaskReq.getEndTimeRange().get(1));
        }
        queryWrapper.orderByDesc(AppStockSingleTaskBak::getCreateTime);
        Page<AppStockSingleTaskBak> page = new Page<>(pageIndex, pageSize);
        try {
            Page<AppStockSingleTaskBak> pageResult = super.baseMapper.selectPage(page, queryWrapper);
            return new Tuple2<>(pageResult.getTotal(), pageResult.getRecords());
        } catch (Exception ex) {
            return null;
        }
    }

    /**
     * 查询任务列表
     * @param queryTaskReq 筛选参数
     * @return 查询结果
     */
    @Override
    public List<AppStockSingleTaskBak> query(QueryStockSingleTaskReq queryTaskReq) {
        if(AppStringUtils.isNotEmpty(queryTaskReq.getPlcTaskId()) && !AppStringUtils.isNumber(queryTaskReq.getPlcTaskId())) {
            return null;
        }
        Integer plcTaskId = AppStringUtils.isEmpty(queryTaskReq.getPlcTaskId()) ? null : Integer.parseInt(queryTaskReq.getPlcTaskId());
        LambdaQueryWrapper<AppStockSingleTaskBak> queryWrapper = new LambdaQueryWrapper<>();
        queryWrapper.like(AppStringUtils.isNotEmpty(queryTaskReq.getTaskId()), AppStockSingleTaskBak::getTaskId, queryTaskReq.getTaskId());
        queryWrapper.like(AppStringUtils.isNotEmpty(queryTaskReq.getTaskGroup()), AppStockSingleTaskBak::getTaskGroup, queryTaskReq.getTaskGroup());
        queryWrapper.eq(plcTaskId != null, AppStockSingleTaskBak::getPlcTaskId, plcTaskId);
        queryWrapper.eq(queryTaskReq.getExecuteMachine() != null, AppStockSingleTaskBak::getExecuteMachine, queryTaskReq.getExecuteMachine());
        queryWrapper.like(AppStringUtils.isNotEmpty(queryTaskReq.getUpperTaskId()), AppStockSingleTaskBak::getUpperTaskId, queryTaskReq.getUpperTaskId());
        queryWrapper.like(AppStringUtils.isNotEmpty(queryTaskReq.getVehicleNo()), AppStockSingleTaskBak::getVehicleNo, queryTaskReq.getVehicleNo());
        queryWrapper.eq(queryTaskReq.getTaskType() != null, AppStockSingleTaskBak::getTaskType, queryTaskReq.getTaskType());
        queryWrapper.like(AppStringUtils.isNotEmpty(queryTaskReq.getOrigin()), AppStockSingleTaskBak::getOrigin, queryTaskReq.getOrigin());
        queryWrapper.like(AppStringUtils.isNotEmpty(queryTaskReq.getDestination()), AppStockSingleTaskBak::getDestination, queryTaskReq.getDestination());
        queryWrapper.eq(queryTaskReq.getTaskStatus() != null, AppStockSingleTaskBak::getTaskStatus, queryTaskReq.getTaskStatus());
        if(queryTaskReq.getCreateTimeRange() != null && queryTaskReq.getCreateTimeRange().size() >= 2) {
            queryWrapper.between(AppStockSingleTaskBak::getCreateTime, queryTaskReq.getCreateTimeRange().get(0), queryTaskReq.getCreateTimeRange().get(1));
        }
        if(queryTaskReq.getEndTimeRange() != null && queryTaskReq.getEndTimeRange().size() >= 2) {
            queryWrapper.between(AppStockSingleTaskBak::getEndTime, queryTaskReq.getEndTimeRange().get(0), queryTaskReq.getEndTimeRange().get(1));
        }
        queryWrapper.orderByDesc(AppStockSingleTaskBak::getCreateTime);
        try {
            return list(queryWrapper);
        } catch (Exception ex) {
            return null;
        }
    }

    /**
     * 插入数据
     * @param appStockSingleTaskBak 数据
     * @return 是否成功
     */
    @Override
    public boolean insertThrowEx(AppStockSingleTaskBak appStockSingleTaskBak) {
        return super.save(appStockSingleTaskBak);
    }

    @Override
    public List<AppStockSingleTaskBak> queryTaskRecentDays(int days, Integer taskStatus) {
        try {
            LambdaQueryWrapper<AppStockSingleTaskBak> queryWrapper = new LambdaQueryWrapper<>();
            queryWrapper.eq(AppStockSingleTaskBak::getTaskStatus, taskStatus);
            queryWrapper.ge(AppStockSingleTaskBak::getCreateTime, AppTimeUtils.getTodayEndTime().minusDays(days));
            return super.list(queryWrapper);
        } catch (Exception e) {
            return null;
        }
    }

}
