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.ITaskConfigRetryLogDao;
import com.docus.server.archivefile.infrastructure.mapper.TaskConfigRetryLogMapper;
import com.docus.server.entity.TaskConfigRetryLog;
import com.github.pagehelper.PageHelper;
import org.springframework.stereotype.Repository;

import java.util.List;

@Repository
public class TaskConfigRetryLogDaoImpl extends BaseDaoImpl<TaskConfigRetryLogMapper, TaskConfigRetryLog> implements ITaskConfigRetryLogDao {

    @Override
    public PageResult<TaskConfigRetryLog> search(SearchRequest searchRequest) {
        PageHelper.startPage(searchRequest.getPageNum(), searchRequest.getPageSize());

        LambdaQueryWrapper<TaskConfigRetryLog> query = Wrappers.lambdaQuery();
        //时间范围，默认按createTime
        if (searchRequest.getBeginTime() != null) {
            query.ge(TaskConfigRetryLog::getCreateTime, searchRequest.getBeginTime());
        }
        if (searchRequest.getEndTime() != null) {
            query.le(TaskConfigRetryLog::getCreateTime, searchRequest.getEndTime());
        }
        if (searchRequest.getParams("state") != null) {
            query.eq(TaskConfigRetryLog::getState, searchRequest.getParams("state"));
        }
        if (Func.isNotBlank(searchRequest.getKeyword())) {
            query.like(TaskConfigRetryLog::getName, String.format("%s%s%s", "%", searchRequest.getKeyword(), "%"));
        }
        if (searchRequest.getParams("type") != null) {
            query.eq(TaskConfigRetryLog::getType, searchRequest.getParams("type"));
        }
        if (searchRequest.getParams("messageId") != null) {
            query.eq(TaskConfigRetryLog::getMessageId, searchRequest.getParams("messageId"));
        }
        //默认createTime倒序排序
        query.orderByDesc(TaskConfigRetryLog::getCreateTime);
        List<TaskConfigRetryLog> list = super.find(query);
        return new PageResult<>(list);
    }
}
