Merge remote-tracking branch 'origin/main' into main
# Conflicts: # app/services/db_service/db_service.go
This commit is contained in:
commit
cdbba6b3c0
|
@ -4,10 +4,13 @@ import (
|
||||||
"cron_admin/app/constants/errorcode"
|
"cron_admin/app/constants/errorcode"
|
||||||
"cron_admin/app/http/controllers"
|
"cron_admin/app/http/controllers"
|
||||||
"cron_admin/app/http/entities/backend"
|
"cron_admin/app/http/entities/backend"
|
||||||
|
"cron_admin/app/models/croncmdmodel"
|
||||||
"cron_admin/app/services"
|
"cron_admin/app/services"
|
||||||
|
cmd_services "cron_admin/app/services/cmd_service"
|
||||||
"cron_admin/app/utils/helper"
|
"cron_admin/app/utils/helper"
|
||||||
"cron_admin/app/utils/mapstructure"
|
"cron_admin/app/utils/mapstructure"
|
||||||
"github.com/gin-gonic/gin"
|
"github.com/gin-gonic/gin"
|
||||||
|
"strconv"
|
||||||
)
|
)
|
||||||
|
|
||||||
func CronFuncLogsList(c *gin.Context) {
|
func CronFuncLogsList(c *gin.Context) {
|
||||||
|
@ -18,7 +21,33 @@ func CronFuncLogsList(c *gin.Context) {
|
||||||
} else {
|
} else {
|
||||||
var funcLogsListResponse []backend.CronFuncLogsListResponse
|
var funcLogsListResponse []backend.CronFuncLogsListResponse
|
||||||
_ = mapstructure.DecodeWithTime(funcLogsList, &funcLogsListResponse, helper.DefaultFormatLayout)
|
_ = mapstructure.DecodeWithTime(funcLogsList, &funcLogsListResponse, helper.DefaultFormatLayout)
|
||||||
// todo 获取当前列表下的CmdName
|
// 获取当前列表下的CmdName
|
||||||
|
// 获取所有的cmd_id
|
||||||
|
var cmdIds []string
|
||||||
|
cmdIdsMap := make(map[int64]bool)
|
||||||
|
for _, v := range funcLogsListResponse {
|
||||||
|
if _, ok := cmdIdsMap[v.CmdId]; ok {
|
||||||
|
continue
|
||||||
|
}
|
||||||
|
cmdIdsMap[v.CmdId] = true
|
||||||
|
// v.cmdId 转成string
|
||||||
|
cmdIdStr := strconv.FormatInt(v.CmdId, 10)
|
||||||
|
cmdIds = append(cmdIds, cmdIdStr)
|
||||||
|
}
|
||||||
|
// 获取所有的cmd_id对应的CmdName
|
||||||
|
_, cmdList, err := cmd_services.GetListByWhere(&backend.CmdListRequest{CmdIds: cmdIds}, 1, 100)
|
||||||
|
if err != nil {
|
||||||
|
return
|
||||||
|
}
|
||||||
|
cmdListMap := make(map[int64]croncmdmodel.CronCmd)
|
||||||
|
for _, v := range cmdList {
|
||||||
|
cmdListMap[int64(v.CmdId)] = v
|
||||||
|
}
|
||||||
|
for k, v := range funcLogsListResponse {
|
||||||
|
if cmdListMap != nil && cmdListMap[v.CmdId].CmdName != "" {
|
||||||
|
funcLogsListResponse[k].CmdName = cmdListMap[v.CmdId].CmdName
|
||||||
|
}
|
||||||
|
}
|
||||||
controllers.HandRes(c, gin.H{"data": funcLogsListResponse, "count": count}, err)
|
controllers.HandRes(c, gin.H{"data": funcLogsListResponse, "count": count}, err)
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
|
@ -4,12 +4,15 @@ import (
|
||||||
"cron_admin/app/constants/errorcode"
|
"cron_admin/app/constants/errorcode"
|
||||||
"cron_admin/app/http/controllers"
|
"cron_admin/app/http/controllers"
|
||||||
"cron_admin/app/http/entities/backend"
|
"cron_admin/app/http/entities/backend"
|
||||||
|
"cron_admin/app/models/croncmdmodel"
|
||||||
"cron_admin/app/models/cronusermodel"
|
"cron_admin/app/models/cronusermodel"
|
||||||
"cron_admin/app/services"
|
"cron_admin/app/services"
|
||||||
|
cmd_services "cron_admin/app/services/cmd_service"
|
||||||
userServices "cron_admin/app/services/user_service"
|
userServices "cron_admin/app/services/user_service"
|
||||||
"cron_admin/app/utils/helper"
|
"cron_admin/app/utils/helper"
|
||||||
"cron_admin/app/utils/mapstructure"
|
"cron_admin/app/utils/mapstructure"
|
||||||
"github.com/gin-gonic/gin"
|
"github.com/gin-gonic/gin"
|
||||||
|
"strconv"
|
||||||
"strings"
|
"strings"
|
||||||
)
|
)
|
||||||
|
|
||||||
|
@ -24,6 +27,8 @@ func CronReportLogsList(c *gin.Context) {
|
||||||
// 合并reportLogsListResponse下的所有user_id 并去重
|
// 合并reportLogsListResponse下的所有user_id 并去重
|
||||||
var userIds []string
|
var userIds []string
|
||||||
userIdsMap := make(map[int64]bool)
|
userIdsMap := make(map[int64]bool)
|
||||||
|
var cmdIds []string
|
||||||
|
cmdIdsMap := make(map[int64]bool)
|
||||||
for _, v := range reportLogsListResponse {
|
for _, v := range reportLogsListResponse {
|
||||||
userIdsStr := strings.Split(v.UserId, ",")
|
userIdsStr := strings.Split(v.UserId, ",")
|
||||||
for _, userId := range userIdsStr {
|
for _, userId := range userIdsStr {
|
||||||
|
@ -35,6 +40,13 @@ func CronReportLogsList(c *gin.Context) {
|
||||||
userIds = append(userIds, userId)
|
userIds = append(userIds, userId)
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
if _, ok := cmdIdsMap[v.CmdId]; ok {
|
||||||
|
continue
|
||||||
|
}
|
||||||
|
cmdIdsMap[v.CmdId] = true
|
||||||
|
// v.cmdId 转成string
|
||||||
|
cmdIdStr := strconv.FormatInt(v.CmdId, 10)
|
||||||
|
cmdIds = append(cmdIds, cmdIdStr)
|
||||||
}
|
}
|
||||||
_, userList, err := userServices.GetListByWhere(&backend.UserListRequest{UserIds: userIds}, 1, 100)
|
_, userList, err := userServices.GetListByWhere(&backend.UserListRequest{UserIds: userIds}, 1, 100)
|
||||||
if err != nil {
|
if err != nil {
|
||||||
|
@ -44,6 +56,15 @@ func CronReportLogsList(c *gin.Context) {
|
||||||
for _, v := range userList {
|
for _, v := range userList {
|
||||||
userListMap[int64(v.UserId)] = v
|
userListMap[int64(v.UserId)] = v
|
||||||
}
|
}
|
||||||
|
// 获取所有的cmd_id对应的CmdName
|
||||||
|
_, cmdList, err := cmd_services.GetListByWhere(&backend.CmdListRequest{CmdIds: cmdIds}, 1, 100)
|
||||||
|
if err != nil {
|
||||||
|
return
|
||||||
|
}
|
||||||
|
cmdListMap := make(map[int64]croncmdmodel.CronCmd)
|
||||||
|
for _, v := range cmdList {
|
||||||
|
cmdListMap[int64(v.CmdId)] = v
|
||||||
|
}
|
||||||
for k, v := range reportLogsListResponse {
|
for k, v := range reportLogsListResponse {
|
||||||
userIdsStr := strings.Split(v.UserId, ",")
|
userIdsStr := strings.Split(v.UserId, ",")
|
||||||
var userNames []string
|
var userNames []string
|
||||||
|
@ -54,6 +75,9 @@ func CronReportLogsList(c *gin.Context) {
|
||||||
}
|
}
|
||||||
userNamesStr := strings.Join(userNames, ",")
|
userNamesStr := strings.Join(userNames, ",")
|
||||||
reportLogsListResponse[k].UserName = userNamesStr
|
reportLogsListResponse[k].UserName = userNamesStr
|
||||||
|
if cmdListMap != nil && cmdListMap[v.CmdId].CmdName != "" {
|
||||||
|
reportLogsListResponse[k].CmdName = cmdListMap[v.CmdId].CmdName
|
||||||
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
controllers.HandRes(c, gin.H{"data": reportLogsListResponse, "count": count}, err)
|
controllers.HandRes(c, gin.H{"data": reportLogsListResponse, "count": count}, err)
|
||||||
|
|
|
@ -39,3 +39,10 @@ func DbDel(c *gin.Context) {
|
||||||
err := db_service.DbDel(request)
|
err := db_service.DbDel(request)
|
||||||
controllers.HandRes(c, nil, err)
|
controllers.HandRes(c, nil, err)
|
||||||
}
|
}
|
||||||
|
|
||||||
|
// 测试连接
|
||||||
|
func DbTest(c *gin.Context) {
|
||||||
|
request := controllers.GetRequest(c).(*backend.DbTestRequest)
|
||||||
|
err := db_service.DbTest(request)
|
||||||
|
controllers.HandRes(c, nil, err)
|
||||||
|
}
|
||||||
|
|
|
@ -14,6 +14,7 @@ type CmdListRequest struct {
|
||||||
CmdName string `json:"cmd_name" form:"cmd_name" example:"155555555"`
|
CmdName string `json:"cmd_name" form:"cmd_name" example:"155555555"`
|
||||||
Status int `json:"status" form:"status" example:"1"`
|
Status int `json:"status" form:"status" example:"1"`
|
||||||
ExecuteType int `json:"execute_type" form:"execute_type" example:"46516"`
|
ExecuteType int `json:"execute_type" form:"execute_type" example:"46516"`
|
||||||
|
CmdIds []string `json:"cmd_id"`
|
||||||
}
|
}
|
||||||
|
|
||||||
type CmdInfoRequest struct {
|
type CmdInfoRequest struct {
|
||||||
|
|
|
@ -64,6 +64,7 @@ type ReportChannelListResponse struct {
|
||||||
Config string `json:"config"`
|
Config string `json:"config"`
|
||||||
CreateTime string `json:"create_time"`
|
CreateTime string `json:"create_time"`
|
||||||
Status int `json:"status"`
|
Status int `json:"status"`
|
||||||
|
UpdateTime string `json:"update_time"`
|
||||||
}
|
}
|
||||||
|
|
||||||
func (this *ReportChannelListResponse) FromDb(in cronreportchannelmodel.CronReportChannel) {
|
func (this *ReportChannelListResponse) FromDb(in cronreportchannelmodel.CronReportChannel) {
|
||||||
|
@ -73,6 +74,7 @@ func (this *ReportChannelListResponse) FromDb(in cronreportchannelmodel.CronRepo
|
||||||
this.Config = in.Config
|
this.Config = in.Config
|
||||||
this.CreateTime = in.CreateTime.Format("2006-01-02 15:04:05")
|
this.CreateTime = in.CreateTime.Format("2006-01-02 15:04:05")
|
||||||
this.Status = in.Status
|
this.Status = in.Status
|
||||||
|
this.UpdateTime = in.UpdateTime.Format("2006-01-02 15:04:05")
|
||||||
}
|
}
|
||||||
|
|
||||||
type ReportChannelCreateRequest struct {
|
type ReportChannelCreateRequest struct {
|
||||||
|
|
|
@ -16,6 +16,7 @@ type CronReportLogsListResponse struct {
|
||||||
ReportId int64 `json:"report_id"`
|
ReportId int64 `json:"report_id"`
|
||||||
FuncLogId int64 `json:"func_log_id"`
|
FuncLogId int64 `json:"func_log_id"`
|
||||||
CmdId int64 `json:"cmd_id"`
|
CmdId int64 `json:"cmd_id"`
|
||||||
|
CmdName string `json:"cmd_name"`
|
||||||
UserId string `json:"user_id"`
|
UserId string `json:"user_id"`
|
||||||
UserName string `json:"user_name"`
|
UserName string `json:"user_name"`
|
||||||
Data string `json:"data"`
|
Data string `json:"data"`
|
||||||
|
|
|
@ -29,12 +29,16 @@ type DbAddRequest struct {
|
||||||
Status int `json:"status" form:"status" example:"1"`
|
Status int `json:"status" form:"status" example:"1"`
|
||||||
}
|
}
|
||||||
|
|
||||||
|
type DbTestRequest struct {
|
||||||
|
Source string `json:"source" validate:"required" form:"source" example:""`
|
||||||
|
}
|
||||||
|
|
||||||
type DbEditRequest struct {
|
type DbEditRequest struct {
|
||||||
DbId int `json:"db_id" validate:"required" form:"db_id" example:""`
|
DbId int `json:"db_id" validate:"required" form:"db_id" example:""`
|
||||||
DbName string `json:"db_name" validate:"required" form:"db_name" example:""`
|
DbName string `json:"db_name" validate:"required" form:"db_name" example:""`
|
||||||
DbType string `json:"db_type" validate:"required" form:"db_type" example:"mysql"`
|
DbType string `json:"db_type" validate:"required" form:"db_type" example:"mysql"`
|
||||||
DbPermission int `json:"db_permission" validate:"required" form:"db_permission" example:"1"`
|
DbPermission int `json:"db_permission" validate:"required" form:"db_permission" example:"1"`
|
||||||
Source string `json:"source" validate:"required" form:"source" example:""`
|
Source string `json:"source" form:"source" example:""`
|
||||||
Desc string `json:"desc" form:"desc" example:""`
|
Desc string `json:"desc" form:"desc" example:""`
|
||||||
Status int `json:"status" form:"status" example:"1"`
|
Status int `json:"status" form:"status" example:"1"`
|
||||||
}
|
}
|
||||||
|
|
|
@ -25,6 +25,7 @@ var BackendRequestMap = map[string]func() interface{}{
|
||||||
common.ADMIN_OAUTH_V1 + "/sql/add": func() interface{} { return new(backend.DbAddRequest) },
|
common.ADMIN_OAUTH_V1 + "/sql/add": func() interface{} { return new(backend.DbAddRequest) },
|
||||||
common.ADMIN_OAUTH_V1 + "/sql/edit": func() interface{} { return new(backend.DbEditRequest) },
|
common.ADMIN_OAUTH_V1 + "/sql/edit": func() interface{} { return new(backend.DbEditRequest) },
|
||||||
common.ADMIN_OAUTH_V1 + "/sql/del": func() interface{} { return new(backend.DbDeleteRequest) },
|
common.ADMIN_OAUTH_V1 + "/sql/del": func() interface{} { return new(backend.DbDeleteRequest) },
|
||||||
|
common.ADMIN_OAUTH_V1 + "/sql/test": func() interface{} { return new(backend.DbTestRequest) },
|
||||||
|
|
||||||
// 消息渠道
|
// 消息渠道
|
||||||
common.ADMIN_OAUTH_V1 + "/channel/create": func() interface{} { return new(backend.ReportChannelCreateRequest) },
|
common.ADMIN_OAUTH_V1 + "/channel/create": func() interface{} { return new(backend.ReportChannelCreateRequest) },
|
||||||
|
|
|
@ -44,6 +44,7 @@ func RegisterAdminRoute(router *gin.Engine) {
|
||||||
sql.POST("/add", backend.DbAdd)
|
sql.POST("/add", backend.DbAdd)
|
||||||
sql.POST("/edit", backend.DbEdit)
|
sql.POST("/edit", backend.DbEdit)
|
||||||
sql.DELETE("/del", backend.DbDel)
|
sql.DELETE("/del", backend.DbDel)
|
||||||
|
sql.POST("/test", backend.DbTest)
|
||||||
}
|
}
|
||||||
//任务
|
//任务
|
||||||
cmd := v1.Group("/cmd")
|
cmd := v1.Group("/cmd")
|
||||||
|
|
|
@ -28,6 +28,10 @@ func GetListByWhere(request *backend.CmdListRequest, page int, limit int) (count
|
||||||
if request.ExecuteType != 0 {
|
if request.ExecuteType != 0 {
|
||||||
cond = cond.And(builder.Eq{"execute_type": request.ExecuteType})
|
cond = cond.And(builder.Eq{"execute_type": request.ExecuteType})
|
||||||
}
|
}
|
||||||
|
if len(request.CmdIds) > 0 {
|
||||||
|
// 使用IN查询
|
||||||
|
cond = cond.And(builder.In("cmd_id", request.CmdIds))
|
||||||
|
}
|
||||||
|
|
||||||
session := croncmdmodel.GetInstance().GetDb().Where(cond)
|
session := croncmdmodel.GetInstance().GetDb().Where(cond)
|
||||||
|
|
||||||
|
|
|
@ -22,7 +22,7 @@ func ReportChannelList(param backend.ReportChannelList) (list []cronreportchanne
|
||||||
repo = repository.NewReportChannelRepo()
|
repo = repository.NewReportChannelRepo()
|
||||||
opts = make([]repository.DBOption, 0)
|
opts = make([]repository.DBOption, 0)
|
||||||
)
|
)
|
||||||
|
opts = append(opts, repo.WithDesc("report_channel_id"))
|
||||||
if param.ReportChannelId > 0 {
|
if param.ReportChannelId > 0 {
|
||||||
opts = append(opts, repo.WithByID(uint(param.ReportChannelId)))
|
opts = append(opts, repo.WithByID(uint(param.ReportChannelId)))
|
||||||
}
|
}
|
||||||
|
|
|
@ -9,13 +9,13 @@ import (
|
||||||
func CronReportLogsList(request *backend.CronReportLogsListRequest, page int, limit int) (count int64, listInfo []cronreportlogsmodel.CronReportLogs, err error) {
|
func CronReportLogsList(request *backend.CronReportLogsListRequest, page int, limit int) (count int64, listInfo []cronreportlogsmodel.CronReportLogs, err error) {
|
||||||
conn := builder.NewCond()
|
conn := builder.NewCond()
|
||||||
if request.FuncLogId != 0 {
|
if request.FuncLogId != 0 {
|
||||||
conn = conn.And(builder.Eq{"FuncLogId": request.FuncLogId})
|
conn = conn.And(builder.Eq{"func_log_id": request.FuncLogId})
|
||||||
}
|
}
|
||||||
if request.CmdId != 0 {
|
if request.CmdId != 0 {
|
||||||
conn = conn.And(builder.Eq{"CmdId": request.CmdId})
|
conn = conn.And(builder.Eq{"cmd_id": request.CmdId})
|
||||||
}
|
}
|
||||||
if request.Status != 0 {
|
if request.Status != 0 {
|
||||||
conn = conn.And(builder.Eq{"Status": request.Status})
|
conn = conn.And(builder.Eq{"status": request.Status})
|
||||||
}
|
}
|
||||||
// 使用FIND_IN_SET 函数
|
// 使用FIND_IN_SET 函数
|
||||||
if request.UserId != 0 {
|
if request.UserId != 0 {
|
||||||
|
|
|
@ -1,10 +1,15 @@
|
||||||
package db_service
|
package db_service
|
||||||
|
|
||||||
import (
|
import (
|
||||||
|
"context"
|
||||||
"cron_admin/app/constants/common"
|
"cron_admin/app/constants/common"
|
||||||
|
"cron_admin/app/constants/errorcode"
|
||||||
"cron_admin/app/http/entities/backend"
|
"cron_admin/app/http/entities/backend"
|
||||||
"cron_admin/app/models/crondbmodel"
|
"cron_admin/app/models/crondbmodel"
|
||||||
"cron_admin/app/utils/mapstructure"
|
"cron_admin/app/utils/mapstructure"
|
||||||
|
"database/sql"
|
||||||
|
"fmt"
|
||||||
|
"time"
|
||||||
"xorm.io/builder"
|
"xorm.io/builder"
|
||||||
)
|
)
|
||||||
|
|
||||||
|
@ -26,7 +31,7 @@ func DbList(request *backend.DbListRequest, page int, limit int) (count int64, D
|
||||||
if request.DbPermission != 0 {
|
if request.DbPermission != 0 {
|
||||||
session = session.And(builder.Eq{"DbPermission": request.DbPermission})
|
session = session.And(builder.Eq{"DbPermission": request.DbPermission})
|
||||||
}
|
}
|
||||||
count, err = session.Omit("source", "UpdateTime").FindAndCount(&DbListInfo)
|
count, err = session.OrderBy("db_id desc").Omit("source", "UpdateTime").FindAndCount(&DbListInfo)
|
||||||
if err != nil {
|
if err != nil {
|
||||||
return
|
return
|
||||||
}
|
}
|
||||||
|
@ -42,8 +47,16 @@ func DbAdd(request *backend.DbAddRequest) (err error) {
|
||||||
}
|
}
|
||||||
|
|
||||||
func DbEdit(request *backend.DbEditRequest) (err error) {
|
func DbEdit(request *backend.DbEditRequest) (err error) {
|
||||||
|
var info crondbmodel.CronDb
|
||||||
|
flag, _ := crondbmodel.GetInstance().GetDb().ID(request.DbId).Get(&info)
|
||||||
|
if !flag {
|
||||||
|
return
|
||||||
|
}
|
||||||
var db crondbmodel.CronDb
|
var db crondbmodel.CronDb
|
||||||
_ = mapstructure.Decode(request, &db)
|
_ = mapstructure.Decode(request, &db)
|
||||||
|
if request.Source == "" {
|
||||||
|
request.Source = info.Source
|
||||||
|
}
|
||||||
_, err = crondbmodel.GetInstance().GetDb().ID(request.DbId).Update(&db)
|
_, err = crondbmodel.GetInstance().GetDb().ID(request.DbId).Update(&db)
|
||||||
return
|
return
|
||||||
}
|
}
|
||||||
|
@ -53,7 +66,20 @@ func DbDel(request *backend.DbDeleteRequest) (err error) {
|
||||||
return
|
return
|
||||||
}
|
}
|
||||||
|
|
||||||
//func DbRange(value []int) (results []crondbmodel.CronDb, err error) {
|
func DbTest(request *backend.DbTestRequest) (err error) {
|
||||||
// _, err = crondbmodel.GetInstance().GetDb().FindAndCount(&results)
|
// 打开数据库连接
|
||||||
// return
|
db, err := sql.Open("mysql", request.Source)
|
||||||
//}
|
if err != nil {
|
||||||
|
return err
|
||||||
|
}
|
||||||
|
defer db.Close()
|
||||||
|
// 使用 context 来设置 Ping 的超时时间
|
||||||
|
ctx, cancel := context.WithTimeout(context.Background(), 3*time.Second)
|
||||||
|
defer cancel()
|
||||||
|
// 测试连接
|
||||||
|
err = db.PingContext(ctx)
|
||||||
|
if err != nil {
|
||||||
|
return errorcode.NewBusinessErr(301, fmt.Sprintf("数据库连接失败: %v", err))
|
||||||
|
}
|
||||||
|
return
|
||||||
|
}
|
||||||
|
|
Loading…
Reference in New Issue