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

import java.util.List;

@Repository
public class TaskMessageDaoImpl extends BaseDaoImpl<TaskMessageMapper, TaskMessage> implements ITaskMessageDao {

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

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

        //默认createTime倒序排序
        query.orderByDesc(TaskMessage::getCreateTime);
        List<TaskMessage> list = super.find(query);

        return new PageResult<>(list);
    }
}
