package com.fdkankan.manage.service.impl;

import cn.hutool.core.collection.CollUtil;
import cn.hutool.core.util.StrUtil;
import com.fdkankan.manage.entity.OperLog;
import com.fdkankan.manage.service.OperLogService;
import com.fdkankan.manage.vo.OperLogPageParamVO;
import com.fdkankan.mongodb.base.MongoPageResult;
import com.fdkankan.mongodb.service.impl.MongodbBaseServiceImpl;
import java.util.ArrayList;
import java.util.List;
import java.util.Objects;
import org.springframework.data.mongodb.core.query.Criteria;
import org.springframework.data.mongodb.core.query.Query;
import org.springframework.stereotype.Service;

/**
 * <p>
 * TODO
 * </p>
 *
 * @author dengsixing
 * @since 2022/5/30
 **/
@Service
public class OperLogServiceImpl extends MongodbBaseServiceImpl<OperLog> implements OperLogService {

    @Override
    public MongoPageResult pageOperLog(OperLogPageParamVO param) {

        Query query = new Query();
        if(StrUtil.isNotEmpty(param.getNickName())){
            query.addCriteria(Criteria.where("nickName").is(param.getNickName()));
        }
        if(StrUtil.isNotEmpty(param.getUserName())){
            query.addCriteria(Criteria.where("userName").is(param.getUserName()));
        }
        Criteria createTimeCri = null;
        if(Objects.nonNull(param.getStartTime()) || Objects.nonNull(param.getEndTime())){
            createTimeCri = Criteria.where("createTime");
            query.addCriteria(createTimeCri);
        }
        if(Objects.nonNull(param.getStartTime())){
            createTimeCri.gte(param.getStartTime());
        }

        if(Objects.nonNull(param.getEndTime())){
            createTimeCri.lte(param.getEndTime());
        }

        //默认按操作时间降序排序
        param.setOrderBy("createTime");

        MongoPageResult<OperLog> operLogMongoPageResult = this.pageQuery(param, query);
        return operLogMongoPageResult;
    }
}
