package com.ema.admin.modules.system.service.impl;

import cn.hutool.core.util.StrUtil;
import com.ema.common.aspect.logger.LogTypeEnum;
import com.mybatisflex.core.paginate.Page;
import com.mybatisflex.core.query.QueryWrapper;
import com.mybatisflex.spring.service.impl.ServiceImpl;
import com.ema.admin.modules.system.entity.SysOperLog;
import com.ema.admin.modules.system.entity.SysOperLogQueryVo;
import com.ema.admin.modules.system.mapper.SysOperLogMapper;
import com.ema.admin.modules.system.service.SysOperLogService;
import org.springframework.stereotype.Service;


/**
 * 操作日志记录 服务层实现。
 *
 * @author LIJIAN
 * @since 2026-03-25
 */
@Service
public class SysOperLogServiceImpl extends ServiceImpl<SysOperLogMapper, SysOperLog> implements SysOperLogService {

    @Override
    public Page<SysOperLog> page(SysOperLogQueryVo queryVo) {
        QueryWrapper queryWrapper = QueryWrapper.create();
        queryWrapper.eq(SysOperLog::getType, queryVo.getType(), StrUtil.isNotBlank(queryVo.getType()));
        queryWrapper.eq(SysOperLog::getModule, queryVo.getModule(), StrUtil.isNotBlank(queryVo.getModule()));
        queryWrapper.like(SysOperLog::getTitle, queryVo.getTitle(), StrUtil.isNotBlank(queryVo.getTitle()));
        queryWrapper.like(SysOperLog::getUrl, queryVo.getUrl(), StrUtil.isNotBlank(queryVo.getUrl()));
        queryWrapper.eq(SysOperLog::getStatus, queryVo.getStatus(), StrUtil.isNotBlank(queryVo.getStatus()));
        queryWrapper.eq(SysOperLog::getUserId, queryVo.getUserId(), StrUtil.isNotBlank(queryVo.getUserId()));
        queryWrapper.orderBy(SysOperLog::getOperTime, false);
        Page<SysOperLog> page = this.page(new Page<>(queryVo.getPageNumber(), queryVo.getPageSize()), queryWrapper);
        page.getRecords().forEach(operLog -> {
            operLog.setUserName(operLog.getUserName());
            operLog.setType(LogTypeEnum.getDescribe(operLog.getType()));
        });
        return page;
    }
}
