package controller import ( "fmt" "strconv" "strings" "time" "be.ems/src/framework/i18n" "be.ems/src/framework/utils/ctx" "be.ems/src/framework/utils/date" "be.ems/src/framework/utils/file" "be.ems/src/framework/utils/parse" "be.ems/src/framework/vo/result" "be.ems/src/modules/system/model" "be.ems/src/modules/system/service" dborm "be.ems/lib/core/datasource" "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) { language := ctx.AcceptLanguage(c) querys := ctx.QueryMap(c) // 多语言值转key查询 if v, ok := querys["title"]; ok && v != "" { querys["title"] = i18n.TFindKeyPrefix(language, "log.operate.title", v.(string)) } // multi-tenancy, only filter user setting tenant_id userName := ctx.LoginUserToUserName(c) if s.IsTenancyUser(userName) { querys["operName"] = userName } // data := s.SysLogOperateService.SelectSysLogOperatePage(querys) dataScopeSQL := "" // ctx.LoginUserToDataScopeSQL(c, "d", "u") data := s.SysLogOperateService.SelectSysLogOperatePage(querys, dataScopeSQL) rows := data["rows"].([]model.SysLogOperate) // 闭包函数处理多语言 converI18n := func(language string, arr *[]model.SysLogOperate) { for i := range *arr { (*arr)[i].Title = i18n.TKey(language, (*arr)[i].Title) (*arr)[i].OperLocation = i18n.TKey(language, (*arr)[i].OperLocation) } } converI18n(language, &rows) c.JSON(200, result.Ok(data)) } // 操作日志删除 // // DELETE /:operIds func (s *SysLogOperateController) Remove(c *gin.Context) { language := ctx.AcceptLanguage(c) operIds := c.Param("operIds") if operIds == "" { c.JSON(400, result.CodeMsg(400, i18n.TKey(language, "app.common.err400"))) 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 := i18n.TTemplate(language, "app.common.deleteSuccess", map[string]any{"num": 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) { language := ctx.AcceptLanguage(c) // 查询结果,根据查询条件结果,单页最大值限制 // querys := ctx.BodyJSONMap(c) // data := s.SysLogOperateService.SelectSysLogOperatePage(querys) // if data["total"].(int64) == 0 { // // 导出数据记录为空 // c.JSON(200, result.ErrMsg(i18n.TKey(language, "app.common.exportEmpty"))) // return // } // rows := data["rows"].([]model.SysLogOperate) rows := s.SysLogOperateService.SelectSysLogOperateList(model.SysLogOperate{}) if len(rows) <= 0 { // 导出数据记录为空 c.JSON(200, result.ErrMsg(i18n.TKey(language, "app.common.exportEmpty"))) return } // 闭包函数处理多语言 converI18n := func(language string, arr *[]model.SysLogOperate) { for i := range *arr { (*arr)[i].Title = i18n.TKey(language, (*arr)[i].Title) (*arr)[i].OperLocation = i18n.TKey(language, (*arr)[i].OperLocation) } } converI18n(language, &rows) // 导出文件名称 fileName := fmt.Sprintf("sys_log_operate_export_%d_%d.xlsx", len(rows), time.Now().UnixMilli()) // 第一行表头标题 headerCells := map[string]string{ "A1": i18n.TKey(language, "log.operate.export.id"), "B1": i18n.TKey(language, "log.operate.export.title"), "C1": i18n.TKey(language, "log.operate.export.businessType"), "D1": i18n.TKey(language, "log.operate.export.operName"), "E1": i18n.TKey(language, "log.operate.export.method"), "F1": i18n.TKey(language, "log.operate.export.ip"), "G1": i18n.TKey(language, "log.operate.export.status"), "H1": i18n.TKey(language, "log.operate.export.operTime"), "I1": i18n.TKey(language, "log.operate.export.costTime"), } // 从第二行开始的数据 dataCells := make([]map[string]any, 0) for i, row := range rows { idx := strconv.Itoa(i + 2) // 业务类型 businessType := "" switch row.BusinessType { case "0": // 业务操作类型-其它 businessType = i18n.TKey(language, "dictData.operType.other") case "1": // 业务操作类型-新增 businessType = i18n.TKey(language, "dictData.operType.add") case "2": // 业务操作类型-修改 businessType = i18n.TKey(language, "dictData.operType.edit") case "3": // 业务操作类型-删除 businessType = i18n.TKey(language, "dictData.operType.delete") case "4": // 业务操作类型-授权 businessType = i18n.TKey(language, "dictData.operType.auth") case "5": // 业务操作类型-导出 businessType = i18n.TKey(language, "dictData.operType.export") case "6": // 业务操作类型-导入 businessType = i18n.TKey(language, "dictData.operType.import") case "7": // 业务操作类型-强退 businessType = i18n.TKey(language, "dictData.operType.forced quit") case "8": // 业务操作类型-清空数据 businessType = i18n.TKey(language, "dictData.operType.clear") } // 状态 statusValue := i18n.TKey(language, "dictData.fail") if row.Status == "1" { statusValue = i18n.TKey(language, "dictData.success") } dataCells = append(dataCells, map[string]any{ "A" + idx: row.OperID, "B" + idx: row.Title, "C" + idx: businessType, "D" + idx: row.OperName, "E" + idx: row.RequestMethod, "F" + idx: row.OperIP, "G" + idx: statusValue, "H" + idx: date.ParseDateToStr(row.OperTime, date.YYYY_MM_DDTHH_MM_SSZ), "I" + idx: row.CostTime, }) } // 导出数据表格 saveFilePath, err := file.WriteSheet(headerCells, dataCells, fileName, "") if err != nil { c.JSON(200, result.ErrMsg(err.Error())) return } c.FileAttachment(saveFilePath, fileName) } func (s *SysLogOperateController) IsTenancyUser(userName string) bool { var tenantID []int64 dborm.DefaultDB().Table("sys_user").Where("user_name=?", userName).Cols("tenant_id").Find(&tenantID) if len(tenantID) > 0 { return true } return false }