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.AppRecordScanMapper;
import org.wcs.mapper.intf.AppRecordScanService;
import org.wcs.model.bo.tuple.Tuple2;
import org.wcs.model.dto.serve.recordScan.QueryRecordScanReq;
import org.wcs.model.po.app.AppRecordScan;
import org.wcs.utils.AppStringUtils;

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

@Service
public class AppRecordScanDao extends ServiceImpl<AppRecordScanMapper, AppRecordScan> implements AppRecordScanService {

    /**
     * 分页查询记录
     * @param queryRecordScanReq 查询参数
     * @param pageSize 每页数量
     * @param pageIndex 页码
     * @return 查询结果
     */
    @Override
    public Tuple2<Long, List<AppRecordScan>> query(QueryRecordScanReq queryRecordScanReq, Integer pageSize, Integer pageIndex) {
        if(pageSize == null || pageSize <= 0 || pageIndex == null || pageIndex <= 0) {
            return null;
        }
        LambdaQueryWrapper<AppRecordScan> queryWrapper = new LambdaQueryWrapper<>();
        queryWrapper.eq(AppStringUtils.isNotEmpty(queryRecordScanReq.getLocation()), AppRecordScan::getLocation, queryRecordScanReq.getLocation());
        queryWrapper.like(AppStringUtils.isNotEmpty(queryRecordScanReq.getCode()), AppRecordScan::getCode, queryRecordScanReq.getCode());
        if(queryRecordScanReq.getScanTimes() != null && queryRecordScanReq.getScanTimes().length == 2) {
            queryWrapper.between(queryRecordScanReq.getScanTimes()[0] != null && queryRecordScanReq.getScanTimes()[1] != null, AppRecordScan::getScanTime, queryRecordScanReq.getScanTimes()[0], queryRecordScanReq.getScanTimes()[1]);
        }
        queryWrapper.orderByDesc(AppRecordScan::getScanTime);
        try {
            Page<AppRecordScan> pageResult = super.baseMapper.selectPage(Page.of(pageIndex, pageSize), queryWrapper);
            return new Tuple2<>(pageResult.getTotal(), pageResult.getRecords());
        } catch (Exception ex) {
            return null;
        }
    }

    /**
     * 查询记录
     * @param queryRecordScanReq 搜索参数
     * @return 查询结果
     */
    @Override
    public List<AppRecordScan> query(QueryRecordScanReq queryRecordScanReq) {
        LambdaQueryWrapper<AppRecordScan> queryWrapper = new LambdaQueryWrapper<>();
        queryWrapper.eq(AppStringUtils.isNotEmpty(queryRecordScanReq.getLocation()), AppRecordScan::getLocation, queryRecordScanReq.getLocation());
        queryWrapper.like(AppStringUtils.isNotEmpty(queryRecordScanReq.getCode()), AppRecordScan::getCode, queryRecordScanReq.getCode());
        if(queryRecordScanReq.getScanTimes() != null && queryRecordScanReq.getScanTimes().length == 2) {
            queryWrapper.between(queryRecordScanReq.getScanTimes()[0] != null && queryRecordScanReq.getScanTimes()[1] != null, AppRecordScan::getScanTime, queryRecordScanReq.getScanTimes()[0], queryRecordScanReq.getScanTimes()[1]);
        }
        queryWrapper.orderByDesc(AppRecordScan::getScanTime);
        try {
            return super.list(queryWrapper);
        } catch (Exception e) {
            return null;
        }
    }
}
