package com.erp.approval.repository;

import com.baomidou.mybatisplus.core.mapper.BaseMapper;
import com.erp.approval.entity.AuditLog;
import org.apache.ibatis.annotations.Mapper;
import org.apache.ibatis.annotations.Param;
import org.apache.ibatis.annotations.Select;

import java.util.List;
import java.util.Map;

/**
 * 审核日志Mapper
 */
@Mapper
public interface AuditLogRepository extends BaseMapper<AuditLog> {
    
    /**
     * 分页查询日志
     */
    @Select("SELECT * FROM audit_logs WHERE tenant_id = #{tenantId} AND deleted_at IS NULL ORDER BY created_at DESC LIMIT #{offset}, #{limit}")
    List<AuditLog> findByTenantIdWithPage(@Param("tenantId") Long tenantId, @Param("offset") Integer offset, @Param("limit") Integer limit);
    
    /**
     * 统计日志数量
     */
    @Select("SELECT COUNT(*) FROM audit_logs WHERE tenant_id = #{tenantId} AND deleted_at IS NULL")
    Long countByTenantId(@Param("tenantId") Long tenantId);
    
    /**
     * 按模块统计
     */
    @Select("SELECT module, action, COUNT(*) as count FROM audit_logs WHERE tenant_id = #{tenantId} AND created_at BETWEEN #{startDate} AND #{endDate} GROUP BY module, action")
    List<Map<String, Object>> statsByModuleAndAction(@Param("tenantId") Long tenantId, @Param("startDate") String startDate, @Param("endDate") String endDate);
}
