Files
be.ems/src/modules/system/controller/sys_log_operate.go

157 lines
3.7 KiB
Go

package controller
import (
"fmt"
"strconv"
"strings"
"time"
"ems.agt/src/framework/utils/ctx"
"ems.agt/src/framework/utils/date"
"ems.agt/src/framework/utils/file"
"ems.agt/src/framework/utils/parse"
"ems.agt/src/framework/vo/result"
"ems.agt/src/modules/system/model"
"ems.agt/src/modules/system/service"
"github.com/gin-gonic/gin"
)
// 实例化控制层 SysLogOperateController 结构体
var NewSysLogOperate = &SysLogOperateController{
SysLogOperateService: service.NewSysLogOperateImpl,
}
// 操作日志记录信息
//
// PATH /system/log/operate
type SysLogOperateController struct {
// 操作日志服务
SysLogOperateService service.ISysLogOperate
}
// 操作日志列表
//
// GET /list
func (s *SysLogOperateController) List(c *gin.Context) {
querys := ctx.QueryMap(c)
data := s.SysLogOperateService.SelectSysLogOperatePage(querys)
c.JSON(200, result.Ok(data))
}
// 操作日志删除
//
// DELETE /:operIds
func (s *SysLogOperateController) Remove(c *gin.Context) {
operIds := c.Param("operIds")
if operIds == "" {
c.JSON(400, result.CodeMsg(400, "parameter error"))
return
}
// 处理字符转id数组后去重
ids := strings.Split(operIds, ",")
uniqueIDs := parse.RemoveDuplicates(ids)
if len(uniqueIDs) <= 0 {
c.JSON(200, result.Err(nil))
return
}
rows := s.SysLogOperateService.DeleteSysLogOperateByIds(uniqueIDs)
if rows > 0 {
msg := fmt.Sprintf("Deleted successfully: %d", rows)
c.JSON(200, result.OkMsg(msg))
return
}
c.JSON(200, result.Err(nil))
}
// 操作日志清空
//
// DELETE /clean
func (s *SysLogOperateController) Clean(c *gin.Context) {
err := s.SysLogOperateService.CleanSysLogOperate()
if err != nil {
c.JSON(200, result.ErrMsg(err.Error()))
return
}
c.JSON(200, result.Ok(nil))
}
// 导出操作日志
//
// POST /export
func (s *SysLogOperateController) Export(c *gin.Context) {
// 查询结果,根据查询条件结果,单页最大值限制
querys := ctx.BodyJSONMap(c)
data := s.SysLogOperateService.SelectSysLogOperatePage(querys)
if data["total"].(int64) == 0 {
// 导出数据记录为空
c.JSON(200, result.ErrMsg("Export data record is empty"))
return
}
rows := data["rows"].([]model.SysLogOperate)
// 导出文件名称
fileName := fmt.Sprintf("sys_log_operate_export_%d_%d.xlsx", len(rows), time.Now().UnixMilli())
// 第一行表头标题
headerCells := map[string]string{
"A1": "ID",
"B1": "Title",
"C1": "BusinessType",
"D1": "Method",
"E1": "RequestMethod",
"F1": "OperatorType",
"G1": "OperName",
"H1": "DeptName",
"I1": "URL",
"J1": "IP",
"K1": "Location",
"L1": "Param",
"M1": "Msg",
"N1": "Status",
"O1": "CostTime (ms)",
"P1": "OperTime",
}
// 从第二行开始的数据
dataCells := make([]map[string]any, 0)
for i, row := range rows {
idx := strconv.Itoa(i + 2)
// 业务类型
businessType := ""
// 操作类别
operatorType := ""
// 状态
statusValue := "fail"
if row.Status == "1" {
statusValue = "success"
}
dataCells = append(dataCells, map[string]any{
"A" + idx: row.OperID,
"B" + idx: row.Title,
"C" + idx: businessType,
"D" + idx: row.Method,
"E" + idx: row.RequestMethod,
"F" + idx: operatorType,
"G" + idx: row.OperName,
"H" + idx: row.DeptName,
"I" + idx: row.OperURL,
"J" + idx: row.OperIP,
"K" + idx: row.OperLocation,
"L" + idx: row.OperParam,
"M" + idx: row.OperMsg,
"N" + idx: statusValue,
"O" + idx: row.CostTime,
"P" + idx: date.ParseDateToStr(row.OperTime, date.YYYY_MM_DD_HH_MM_SS),
})
}
// 导出数据表格
saveFilePath, err := file.WriteSheet(headerCells, dataCells, fileName, "")
if err != nil {
c.JSON(200, result.ErrMsg(err.Error()))
return
}
c.FileAttachment(saveFilePath, fileName)
}