diff --git a/app/http/controllers/backend/cron_func_logs_controller.go b/app/http/controllers/backend/cron_func_logs_controller.go index b084d69..5c79ce6 100644 --- a/app/http/controllers/backend/cron_func_logs_controller.go +++ b/app/http/controllers/backend/cron_func_logs_controller.go @@ -12,12 +12,13 @@ import ( func CronFuncLogsList(c *gin.Context) { request := controllers.GetRequest(c).(*backend.CronFuncLogsListRequest) - count, DbListInfo, err := services.CronFuncLogsList(request, request.Page, request.Limit) + count, funcLogsList, err := services.CronFuncLogsList(request, request.Page, request.Limit) if err != nil { controllers.HandRes(c, nil, errorcode.ParamError) } else { - var DbListResponse []backend.CronFuncLogsListResponse - _ = mapstructure.DecodeWithTime(DbListInfo, &DbListResponse, helper.DefaultFormatLayout) - controllers.HandRes(c, gin.H{"data": DbListResponse, "count": count}, err) + var funcLogsListResponse []backend.CronFuncLogsListResponse + _ = mapstructure.DecodeWithTime(funcLogsList, &funcLogsListResponse, helper.DefaultFormatLayout) + // todo 获取当前列表下的CmdName + controllers.HandRes(c, gin.H{"data": funcLogsListResponse, "count": count}, err) } } diff --git a/app/http/controllers/backend/cron_report_logs_controller.go b/app/http/controllers/backend/cron_report_logs_controller.go new file mode 100644 index 0000000..23ecc7c --- /dev/null +++ b/app/http/controllers/backend/cron_report_logs_controller.go @@ -0,0 +1,60 @@ +package backend + +import ( + "cron_admin/app/constants/errorcode" + "cron_admin/app/http/controllers" + "cron_admin/app/http/entities/backend" + "cron_admin/app/models/cronusermodel" + "cron_admin/app/services" + "cron_admin/app/utils/helper" + "cron_admin/app/utils/mapstructure" + "github.com/gin-gonic/gin" + "strings" +) + +func CronReportLogsList(c *gin.Context) { + request := controllers.GetRequest(c).(*backend.CronReportLogsListRequest) + count, reportLogsList, err := services.CronReportLogsList(request, request.Page, request.Limit) + if err != nil { + controllers.HandRes(c, nil, errorcode.ParamError) + } else { + var reportLogsListResponse []backend.CronReportLogsListResponse + _ = mapstructure.DecodeWithTime(reportLogsList, &reportLogsListResponse, helper.DefaultFormatLayout) + // 合并reportLogsListResponse下的所有user_id 并去重 + var userIds []string + userIdsMap := make(map[int64]bool) + for _, v := range reportLogsListResponse { + userIdsStr := strings.Split(v.UserId, ",") + for _, userId := range userIdsStr { + if userId != "" { + if _, ok := userIdsMap[helper.StringToInt64(userId)]; ok { + continue + } + userIdsMap[helper.StringToInt64(userId)] = true + userIds = append(userIds, userId) + } + } + } + _, userList, err := services.GetListByWhere(&backend.UserListRequest{UserIds: userIds}, 1, 100) + if err != nil { + return + } + userListMap := make(map[int64]cronusermodel.CronUser) + for _, v := range userList { + userListMap[int64(v.UserId)] = v + } + for k, v := range reportLogsListResponse { + userIdsStr := strings.Split(v.UserId, ",") + var userNames []string + for _, userId := range userIdsStr { + if userId != "" { + userNames = append(userNames, userListMap[helper.StringToInt64(userId)].Name) + } + } + userNamesStr := strings.Join(userNames, ",") + reportLogsListResponse[k].UserName = userNamesStr + } + + controllers.HandRes(c, gin.H{"data": reportLogsListResponse, "count": count}, err) + } +} diff --git a/app/http/entities/backend/cron_report_logs.go b/app/http/entities/backend/cron_report_logs.go new file mode 100644 index 0000000..aeed9c5 --- /dev/null +++ b/app/http/entities/backend/cron_report_logs.go @@ -0,0 +1,26 @@ +package backend + +import ( + "cron_admin/app/http/entities" +) + +type CronReportLogsListRequest struct { + entities.PageRequest + FuncLogId int64 `json:"func_log_id"` // 日志ID + CmdId int64 `json:"cmd_id"` // 任务ID + Status int `json:"status"` // 状态 + UserId int64 `json:"user_id"` // 用户ID +} + +type CronReportLogsListResponse struct { + ReportId int64 `json:"report_id"` + FuncLogId int64 `json:"func_log_id"` + CmdId int64 `json:"cmd_id"` + UserId string `json:"user_id"` + UserName string `json:"user_name"` + Data string `json:"data"` + FailReason string `json:"fail_reason"` + CreateTime string `json:"create_time"` + UpdateTime string `json:"update_time"` + Status int `json:"status"` +} diff --git a/app/http/entities/backend/user.go b/app/http/entities/backend/user.go index 48a1b8c..3a37919 100644 --- a/app/http/entities/backend/user.go +++ b/app/http/entities/backend/user.go @@ -6,11 +6,12 @@ import ( ) type UserListRequest struct { - Page int `json:"page" validate:"required" form:"page" example:"1"` - Limit int `json:"limit" validate:"required" form:"limit" example:"10"` - Tel string `json:"tel" form:"tel" example:"155555555"` - Status int `json:"status" form:"status" example:"1"` - Name string `json:"name" form:"name" example:"46516"` + Page int `json:"page" validate:"required" form:"page" example:"1"` + Limit int `json:"limit" validate:"required" form:"limit" example:"10"` + Tel string `json:"tel" form:"tel" example:"155555555"` + Status int `json:"status" form:"status" example:"1"` + Name string `json:"name" form:"name" example:"46516"` + UserIds []string `json:"user_ids"` } type UserInfoRequest struct { diff --git a/app/http/requestmapping/backend.go b/app/http/requestmapping/backend.go index 33b94a1..abe3156 100644 --- a/app/http/requestmapping/backend.go +++ b/app/http/requestmapping/backend.go @@ -24,4 +24,5 @@ var BackendRequestMap = map[string]func() interface{}{ // 日志 common.ADMIN_OAUTH_V1 + "/log/cmd/list": func() interface{} { return new(backend.CronFuncLogsListRequest) }, + common.ADMIN_OAUTH_V1 + "/log/mes/list": func() interface{} { return new(backend.CronReportLogsListRequest) }, } diff --git a/app/http/routes/admin.go b/app/http/routes/admin.go index 31c0e32..dde46c2 100644 --- a/app/http/routes/admin.go +++ b/app/http/routes/admin.go @@ -72,7 +72,7 @@ func RegisterAdminRoute(router *gin.Engine) { //消息日志 mesLog := log.Group("/mes") { - mesLog.GET("/list", backend.Empty) + mesLog.POST("/list", backend.CronReportLogsList) } } diff --git a/app/models/cronfunclogsmodel/cron_func_logs.go b/app/models/cronfunclogsmodel/cron_func_logs.go index b7e7e38..c58123d 100644 --- a/app/models/cronfunclogsmodel/cron_func_logs.go +++ b/app/models/cronfunclogsmodel/cron_func_logs.go @@ -13,14 +13,14 @@ var ( // 实体 type CronFuncLogs struct { - LogId int64 `xorm:"'log_id' UNSIGNED INT"` - CmdId int64 `xorm:"'cmd_id' INT"` - ReadExecute string `xorm:"'read_execute' json"` - WriteExecute string `xorm:"'write_execute' text"` - FailReason string `xorm:"'fail_reason' text"` - CreateTime time.Time `xorm:"'create_time' datetime"` - UpdateTime time.Time `xorm:"'update_time' timestamp"` - Status int `xorm:"'status' tinyint"` + LogId int64 `xorm:"'log_id' UNSIGNED INT"` + CmdId int64 `xorm:"'cmd_id' INT"` + ReadExecute string `xorm:"'read_execute' json"` + WriteExecute string `xorm:"'write_execute' text"` + FailReason string `xorm:"'fail_reason' text"` + CreateTime *time.Time `xorm:"'create_time' datetime"` + UpdateTime *time.Time `xorm:"'update_time' timestamp"` + Status int `xorm:"'status' tinyint"` } // 表名 diff --git a/app/models/cronreportlogsmodel/cron_report_logs.go b/app/models/cronreportlogsmodel/cron_report_logs.go index 30c3472..c3a100c 100644 --- a/app/models/cronreportlogsmodel/cron_report_logs.go +++ b/app/models/cronreportlogsmodel/cron_report_logs.go @@ -1,4 +1,4 @@ -package crondbmodel +package cronreportlogsmodel import ( "github.com/qit-team/snow-core/db" @@ -13,15 +13,15 @@ var ( // 实体 type CronReportLogs struct { - ReportId int64 `xorm:"'report_id' UNSIGNED INT"` - FuncLogId int64 `xorm:"'func_log_id' INT"` - CmdId int64 `xorm:"'cmd_id' INT"` - UserId string `xorm:"'user_id' varchar(100)"` - Data string `xorm:"'data' json"` - FailReason string `xorm:"'fail_reason' varchar(200)"` - CreateTime time.Time `xorm:"'create_time' datetime"` - UpdateTime time.Time `xorm:"'update_time' timestamp"` - Status int `xorm:"'status' tinyint"` + ReportId int64 `xorm:"'report_id' UNSIGNED INT"` + FuncLogId int64 `xorm:"'func_log_id' INT"` + CmdId int64 `xorm:"'cmd_id' INT"` + UserId string `xorm:"'user_id' varchar(100)"` + Data string `xorm:"'data' json"` + FailReason string `xorm:"'fail_reason' varchar(200)"` + CreateTime *time.Time `xorm:"'create_time' datetime"` + UpdateTime *time.Time `xorm:"'update_time' timestamp"` + Status int `xorm:"'status' tinyint"` } // 表名 diff --git a/app/services/cron_func_logs_service.go b/app/services/cron_func_logs_service.go index bbd7a9c..af7c045 100644 --- a/app/services/cron_func_logs_service.go +++ b/app/services/cron_func_logs_service.go @@ -16,7 +16,7 @@ func CronFuncLogsList(request *backend.CronFuncLogsListRequest, page int, limit } session := cronfunclogsmodel.GetInstance().GetDb().Where(conn) if page != 0 && limit != 0 { - session = session.Limit(limit, (page-1)*limit) + session = session.OrderBy("create_time desc").Limit(limit, (page-1)*limit) } count, err = session.FindAndCount(&listInfo) if err != nil { diff --git a/app/services/cron_report_logs_service.go b/app/services/cron_report_logs_service.go new file mode 100644 index 0000000..a985905 --- /dev/null +++ b/app/services/cron_report_logs_service.go @@ -0,0 +1,34 @@ +package services + +import ( + "cron_admin/app/http/entities/backend" + "cron_admin/app/models/cronreportlogsmodel" + "xorm.io/builder" +) + +func CronReportLogsList(request *backend.CronReportLogsListRequest, page int, limit int) (count int64, listInfo []cronreportlogsmodel.CronReportLogs, err error) { + conn := builder.NewCond() + if request.FuncLogId != 0 { + conn = conn.And(builder.Eq{"FuncLogId": request.FuncLogId}) + } + if request.CmdId != 0 { + conn = conn.And(builder.Eq{"CmdId": request.CmdId}) + } + if request.Status != 0 { + conn = conn.And(builder.Eq{"Status": request.Status}) + } + // 使用FIND_IN_SET 函数 + if request.UserId != 0 { + conn = conn.And(builder.Expr("FIND_IN_SET(?, user_id)", request.UserId)) + } + + session := cronreportlogsmodel.GetInstance().GetDb().Where(conn) + if page != 0 && limit != 0 { + session = session.OrderBy("create_time desc").Limit(limit, (page-1)*limit) + } + count, err = session.FindAndCount(&listInfo) + if err != nil { + return + } + return +} diff --git a/app/services/user_service.go b/app/services/user_service.go index 7dfd20e..6e3c754 100644 --- a/app/services/user_service.go +++ b/app/services/user_service.go @@ -18,11 +18,15 @@ func GetListByWhere(request *backend.UserListRequest, page int, limit int) (coun if request.Status != 0 { cond = cond.And(builder.Eq{"status": request.Status}) } + if len(request.UserIds) > 0 { + // 使用IN查询 + cond = cond.And(builder.In("user_id", request.UserIds)) + } //model := repository.NewCommonRepo[userinfomodel.CronUserModel](userinfomodel.GetInstance().GetDb().NewSession()) session := cronusermodel.GetInstance().GetDb().Where(cond) if page != 0 && limit != 0 { - session = session.Limit(page, (page-1)*limit) + session = session.Limit(limit, (page-1)*limit) } count, err = session.FindAndCount(&UserListInfo)