package com.kym.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.github.yulichang.base.MPJBaseServiceImpl;
import com.kym.entity.SystemLog;
import com.kym.entity.queryParams.SystemLogQueryParam;
import com.kym.mapper.SystemLogMapper;
import com.kym.service.SystemLogService;
import org.springframework.stereotype.Service;
import org.springframework.util.StringUtils;

import java.util.HashMap;
import java.util.Map;

/**
 * <p>
 * 系统操作日志表 服务实现类
 * </p>
 *
 * @author skyline
 * @since 2023-07-10
 */
@Service
public class SystemLogServiceImpl extends MPJBaseServiceImpl<SystemLogMapper, SystemLog> implements SystemLogService {

    @Override
    public Map<String, Object> listSystemLog(SystemLogQueryParam params) {
        Page<SystemLog> page = new Page<>(params.getPageNum(), params.getPageSize());
        
        LambdaQueryWrapper<SystemLog> wrapper = new LambdaQueryWrapper<>();
        
        if (StringUtils.hasText(params.getUsername())) {
            wrapper.like(SystemLog::getUsername, params.getUsername());
        }
        if (StringUtils.hasText(params.getStartDate())) {
            wrapper.ge(SystemLog::getCreateTime, params.getStartDate() + " 00:00:00");
        }
        if (StringUtils.hasText(params.getEndDate())) {
            wrapper.le(SystemLog::getCreateTime, params.getEndDate() + " 23:59:59");
        }
        
        wrapper.orderByDesc(SystemLog::getCreateTime);
        
        Page<SystemLog> result = this.page(page, wrapper);
        
        Map<String, Object> resultMap = new HashMap<>();
        resultMap.put("list", result.getRecords());
        resultMap.put("total", result.getTotal());
        
        return resultMap;
    }

    @Override
    public void clearLog() {
        this.remove(new LambdaQueryWrapper<>());
    }
}
