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.AppEtagTaskBakMapper;
import org.wcs.mapper.intf.AppEtagTaskBakService;
import org.wcs.model.bo.tuple.Tuple2;
import org.wcs.model.dto.serve.etagTask.EtagTaskQueryReq;
import org.wcs.model.po.app.AppEtagTaskBak;

import java.util.List;

@Service
public class AppEtagTaskBakDao extends ServiceImpl<AppEtagTaskBakMapper, AppEtagTaskBak> implements AppEtagTaskBakService {

    /**
     * 查询电子标签任务
     * @param request 查询参数
     * @param pageIndex 页码
     * @param pageSize 页大小
     * @return 响应
     */
    @Override
    public Tuple2<Long, List<AppEtagTaskBak>> queryEtagTask(EtagTaskQueryReq request, Integer pageIndex, Integer pageSize) {
        if(request == null || pageIndex == null || pageSize == null || pageIndex < 0 || pageSize < 0) {
            return null;
        }
        LambdaQueryWrapper<AppEtagTaskBak> queryWrapper = new LambdaQueryWrapper<>();
        queryWrapper.like(request.getTaskId() != null, AppEtagTaskBak::getTaskId, request.getTaskId());
        queryWrapper.eq(request.getTaskStatus() != null, AppEtagTaskBak::getTaskStatus, request.getTaskStatus());
        queryWrapper.eq(request.getTaskType() != null, AppEtagTaskBak::getTaskType, request.getTaskType());
        queryWrapper.like(request.getTaskGroup() != null, AppEtagTaskBak::getTaskGroup, request.getTaskGroup());
        queryWrapper.like(request.getGoodsId() != null, AppEtagTaskBak::getGoodsId, request.getGoodsId());
        queryWrapper.like(request.getVehicleNo() != null, AppEtagTaskBak::getVehicleNo, request.getVehicleNo());
        if(request.getCreateTime() != null && request.getCreateTime().size() == 2) {
            queryWrapper.between(request.getCreateTime().get(0) != null && request.getCreateTime().get(1) != null, AppEtagTaskBak::getCreateTime, request.getCreateTime().get(0), request.getCreateTime().get(1));
        }
        queryWrapper.orderByAsc(AppEtagTaskBak::getCreateTime);
        Page<AppEtagTaskBak> page = new Page<>(pageIndex, pageSize);
        try {
            Page<AppEtagTaskBak> etagTaskPage = baseMapper.selectPage(page, queryWrapper);
            return new Tuple2<>(etagTaskPage.getTotal(), etagTaskPage.getRecords());
        } catch (Exception e) {
            return null;
        }
    }
}
