package com.ruoyi.system.service.impl;

import cn.hutool.core.date.DateUtil;
import com.baomidou.mybatisplus.annotation.InterceptorIgnore;
import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.github.yulichang.base.MPJBaseServiceImpl;
import com.ruoyi.common.utils.StringUtils;
import com.ruoyi.system.domain.SysOperLog;
import com.ruoyi.system.mapper.SysOperLogMapper;
import com.ruoyi.system.service.ISysOperLogService;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.util.Arrays;
import java.util.List;

/**
 * 操作日志 服务层处理
 * 
 * @author ruoyi
 */
@Service
public class SysOperLogServiceImpl extends MPJBaseServiceImpl<SysOperLogMapper, SysOperLog> implements ISysOperLogService
{

    /**
     * 新增操作日志
     * 
     * @param operLog 操作日志对象
     */
    @Override
    public void insertOperlog(SysOperLog operLog)
    {
        save(operLog);
    }

    /**
     * 查询系统操作日志集合
     * 
     * @param operLog 操作日志对象
     * @return 操作日志集合
     */
    @Override
    public List<SysOperLog> selectOperLogList(SysOperLog operLog)
    {
        LambdaQueryWrapper<SysOperLog> sysOperLogLambdaQueryWrapper = new LambdaQueryWrapper<SysOperLog>()
                .like(StringUtils.isNotBlank(operLog.getTitle()), SysOperLog::getTitle, operLog.getTitle())
                .eq(null != operLog.getBusinessType(), SysOperLog::getTitle, operLog.getTitle())
                .in(null != operLog.getBusinessTypes() && operLog.getBusinessTypes().length > 0, SysOperLog::getBusinessType, (Object) operLog.getBusinessTypes())
                .eq(null != operLog.getStatus(), SysOperLog::getStatus, operLog.getStatus())
                .eq(StringUtils.isNotBlank(operLog.getOperName()), SysOperLog::getOperName, operLog.getOperName())
                .ge(StringUtils.isNotNull(operLog.getParams().get("beginTime")), SysOperLog::getOperTime, operLog.getParams().get("beginTime"))
                .le(StringUtils.isNotNull(operLog.getParams().get("endTime")), SysOperLog::getOperTime, operLog.getParams().get("endTime"))
                .orderByDesc(SysOperLog::getOperId);
        return list(sysOperLogLambdaQueryWrapper);
    }

    /**
     * 批量删除系统操作日志
     * 
     * @param operIds 需要删除的操作日志ID
     * @return 结果
     */
    @Override
    @Transactional(
            rollbackFor = {Exception.class}
    )
    public boolean deleteOperLogByIds(Long[] operIds)
    {
        return removeBatchByIds(Arrays.asList(operIds));
    }

    /**
     * 查询操作日志详细
     * 
     * @param operId 操作ID
     * @return 操作日志对象
     */
    @Override
    public SysOperLog selectOperLogById(Long operId)
    {
        return getById(operId);
    }

    /**
     * 清空操作日志
     */
    @Override
    @InterceptorIgnore(blockAttack = "false")
    public void cleanOperLog()
    {
        remove(new LambdaQueryWrapper<SysOperLog>().le(SysOperLog::getOperTime, DateUtil.date()));
    }
}
