package com.docus.server.archivefile.infrastructure.dao.impl;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.toolkit.Wrappers;
import com.docus.core.util.Func;
import com.docus.infrastructure.core.db.dao.impl.BaseDaoImpl;
import com.docus.infrastructure.web.request.SearchRequest;
import com.docus.infrastructure.web.response.PageResult;
import com.docus.server.archivefile.infrastructure.dao.ITaskMessageRetryLogDao;
import com.docus.server.archivefile.infrastructure.mapper.TaskMessageRetryLogMapper;
import com.docus.server.entity.TaskMessageRetryLog;
import com.github.pagehelper.PageHelper;
import org.springframework.stereotype.Repository;

import java.util.List;

@Repository
public class TaskMessageRetryLogDaoImpl extends BaseDaoImpl<TaskMessageRetryLogMapper, TaskMessageRetryLog> implements ITaskMessageRetryLogDao {
    @Override
    public PageResult<TaskMessageRetryLog> search(SearchRequest searchRequest) {
        PageHelper.startPage(searchRequest.getPageNum(), searchRequest.getPageSize());

        LambdaQueryWrapper<TaskMessageRetryLog> query = Wrappers.lambdaQuery();
        //时间范围，默认按createTime
        if (searchRequest.getBeginTime() != null) {
            query.ge(TaskMessageRetryLog::getCreateTime, searchRequest.getBeginTime());
        }
        if (searchRequest.getEndTime() != null) {
            query.le(TaskMessageRetryLog::getCreateTime, searchRequest.getEndTime());
        }
        if (searchRequest.getParams("state") != null) {
            query.eq(TaskMessageRetryLog::getState, searchRequest.getParams("state"));
        }
        if (Func.isNotBlank(searchRequest.getKeyword())) {
            query.like(TaskMessageRetryLog::getName, String.format("%s%s%s", "%", searchRequest.getKeyword(), "%"));
        }
        if (searchRequest.getParams("collectType") != null) {
            query.eq(TaskMessageRetryLog::getCollectType, searchRequest.getParams("collectType"));
        }
        if (searchRequest.getParams("messageId") != null) {
            query.eq(TaskMessageRetryLog::getMessageId, searchRequest.getParams("messageId"));
        }

        //默认createTime倒序排序
        query.orderByDesc(TaskMessageRetryLog::getCreateTime);
        List<TaskMessageRetryLog> list = super.find(query);
        return new PageResult<>(list);
    }
}
