Compare commits
No commits in common. "cdbba6b3c0df7fb344a7c1035120ca8a3aa32cae" and "0365a24685f10e4d7b9cd26369c68839759a2981" have entirely different histories.
cdbba6b3c0
...
0365a24685
|
@ -10,10 +10,3 @@ const (
|
||||||
STATUS_DISABLE = 2
|
STATUS_DISABLE = 2
|
||||||
STATUS_ENABLE = 1
|
STATUS_ENABLE = 1
|
||||||
)
|
)
|
||||||
|
|
||||||
const (
|
|
||||||
CMD_STATUS_WAIT = 1
|
|
||||||
CMD_STATUS_RUN = 2
|
|
||||||
CMD_STATUS_STOP = 3
|
|
||||||
CMD_STATUS_FAIL = 4
|
|
||||||
)
|
|
||||||
|
|
|
@ -4,7 +4,9 @@ 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"
|
||||||
cmd_services "cron_admin/app/services/cmd_service"
|
cmd_services "cron_admin/app/services/cmd_service"
|
||||||
|
"github.com/ahmetb/go-linq/v3"
|
||||||
"github.com/gin-gonic/gin"
|
"github.com/gin-gonic/gin"
|
||||||
)
|
)
|
||||||
|
|
||||||
|
@ -14,37 +16,33 @@ func CmdList(c *gin.Context) {
|
||||||
if err != nil {
|
if err != nil {
|
||||||
controllers.HandRes(c, nil, errorcode.NotFound)
|
controllers.HandRes(c, nil, errorcode.NotFound)
|
||||||
} else {
|
} else {
|
||||||
cmdList, err := cmd_services.ListToRep(list)
|
var (
|
||||||
|
cmdList = make([]backend.CmdListResponse, 0)
|
||||||
|
//userIdList []int
|
||||||
|
)
|
||||||
|
|
||||||
|
linq.From(list).SelectT(func(in croncmdmodel.CronCmd) (d backend.CmdListResponse) {
|
||||||
|
d.ResponseFromDb(in)
|
||||||
|
return d
|
||||||
|
}).ToSlice(&cmdList)
|
||||||
controllers.HandRes(c, gin.H{"data": cmdList, "count": count}, err)
|
controllers.HandRes(c, gin.H{"data": cmdList, "count": count}, err)
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
func CmdAdd(c *gin.Context) {
|
func CmdAdd(c *gin.Context) {
|
||||||
request := controllers.GetRequest(c).(*backend.CmdAddRequest)
|
request := controllers.GetRequest(c).(*backend.CmdAddRequest)
|
||||||
err := cmd_services.Add(request)
|
err := cmd_services.UserAdd(request)
|
||||||
controllers.HandRes(c, nil, err)
|
controllers.HandRes(c, nil, err)
|
||||||
}
|
}
|
||||||
|
|
||||||
func CmdEdit(c *gin.Context) {
|
func CmdEdit(c *gin.Context) {
|
||||||
request := controllers.GetRequest(c).(*backend.CmdEditRequest)
|
request := controllers.GetRequest(c).(*backend.CmdEditRequest)
|
||||||
err := cmd_services.Edit(request)
|
err := cmd_services.UserEdit(request)
|
||||||
controllers.HandRes(c, nil, err)
|
controllers.HandRes(c, nil, err)
|
||||||
}
|
}
|
||||||
|
|
||||||
func CmdDel(c *gin.Context) {
|
func CmdDel(c *gin.Context) {
|
||||||
request := controllers.GetRequest(c).(*backend.CmdDeleteRequest)
|
request := controllers.GetRequest(c).(*backend.CmdDeleteRequest)
|
||||||
err := cmd_services.Del(request)
|
err := cmd_services.UserDel(request)
|
||||||
controllers.HandRes(c, nil, err)
|
|
||||||
}
|
|
||||||
|
|
||||||
func CmdStop(c *gin.Context) {
|
|
||||||
request := controllers.GetRequest(c).(*backend.CmdStopRequest)
|
|
||||||
err := cmd_services.Stop(request)
|
|
||||||
controllers.HandRes(c, nil, err)
|
|
||||||
}
|
|
||||||
|
|
||||||
func CmdStart(c *gin.Context) {
|
|
||||||
request := controllers.GetRequest(c).(*backend.CmdStartRequest)
|
|
||||||
err := cmd_services.Start(request)
|
|
||||||
controllers.HandRes(c, nil, err)
|
controllers.HandRes(c, nil, err)
|
||||||
}
|
}
|
||||||
|
|
|
@ -2,9 +2,6 @@ package backend
|
||||||
|
|
||||||
import (
|
import (
|
||||||
"cron_admin/app/models/croncmdmodel"
|
"cron_admin/app/models/croncmdmodel"
|
||||||
"cron_admin/app/models/crondbmodel"
|
|
||||||
"cron_admin/app/models/cronreportchannelmodel"
|
|
||||||
"cron_admin/app/models/cronusermodel"
|
|
||||||
"time"
|
"time"
|
||||||
)
|
)
|
||||||
|
|
||||||
|
@ -40,10 +37,10 @@ type CmdListResponse struct {
|
||||||
UpdateTime string `json:"update_time"`
|
UpdateTime string `json:"update_time"`
|
||||||
Status int `json:"status"`
|
Status int `json:"status"`
|
||||||
FailReason string `json:"fail_reason"`
|
FailReason string `json:"fail_reason"`
|
||||||
Users []*cronusermodel.CronUser `json:"users"`
|
Users []*UserListResponse `json:"users"`
|
||||||
ReadDb *crondbmodel.CronDb `json:"read_db"`
|
ReadDb DbListResponse `json:"read_db"`
|
||||||
WriteDb *crondbmodel.CronDb `json:"write_db"`
|
WriteDb DbListResponse `json:"write_db"`
|
||||||
ReportChannel *cronreportchannelmodel.CronReportChannel `json:"report_channel"`
|
ReportChannel ReportChannelListResponse `json:"report_channel"`
|
||||||
}
|
}
|
||||||
|
|
||||||
type CmdAddRequest struct {
|
type CmdAddRequest struct {
|
||||||
|
@ -78,15 +75,7 @@ type CmdEditRequest struct {
|
||||||
}
|
}
|
||||||
|
|
||||||
type CmdDeleteRequest struct {
|
type CmdDeleteRequest struct {
|
||||||
CmdId int `json:"cmd_id" validate:"required" form:"cmd_id" example:""`
|
CmdId int `json:"user_id" validate:"required" form:"user_id" example:""`
|
||||||
}
|
|
||||||
|
|
||||||
type CmdStartRequest struct {
|
|
||||||
CmdId int `json:"cmd_id" validate:"required" form:"cmd_id" example:""`
|
|
||||||
}
|
|
||||||
|
|
||||||
type CmdStopRequest struct {
|
|
||||||
CmdId int `json:"cmd_id" validate:"required" form:"cmd_id" example:""`
|
|
||||||
}
|
}
|
||||||
|
|
||||||
func (response *CmdListResponse) ResponseFromDb(l croncmdmodel.CronCmd) {
|
func (response *CmdListResponse) ResponseFromDb(l croncmdmodel.CronCmd) {
|
||||||
|
|
|
@ -17,9 +17,6 @@ var BackendRequestMap = map[string]func() interface{}{
|
||||||
common.ADMIN_OAUTH_V1 + "/user/edit": func() interface{} {
|
common.ADMIN_OAUTH_V1 + "/user/edit": func() interface{} {
|
||||||
return new(backend.UserEditRequest)
|
return new(backend.UserEditRequest)
|
||||||
},
|
},
|
||||||
common.ADMIN_OAUTH_V1 + "/user/del": func() interface{} {
|
|
||||||
return new(backend.UserDeleteRequest)
|
|
||||||
},
|
|
||||||
|
|
||||||
common.ADMIN_OAUTH_V1 + "/sql/list": func() interface{} { return new(backend.DbListRequest) },
|
common.ADMIN_OAUTH_V1 + "/sql/list": func() interface{} { return new(backend.DbListRequest) },
|
||||||
common.ADMIN_OAUTH_V1 + "/sql/add": func() interface{} { return new(backend.DbAddRequest) },
|
common.ADMIN_OAUTH_V1 + "/sql/add": func() interface{} { return new(backend.DbAddRequest) },
|
||||||
|
@ -36,12 +33,4 @@ var BackendRequestMap = map[string]func() interface{}{
|
||||||
// 日志
|
// 日志
|
||||||
common.ADMIN_OAUTH_V1 + "/log/cmd/list": func() interface{} { return new(backend.CronFuncLogsListRequest) },
|
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) },
|
common.ADMIN_OAUTH_V1 + "/log/mes/list": func() interface{} { return new(backend.CronReportLogsListRequest) },
|
||||||
|
|
||||||
//任务
|
|
||||||
common.ADMIN_OAUTH_V1 + "/cmd/list": func() interface{} { return new(backend.CmdListRequest) },
|
|
||||||
common.ADMIN_OAUTH_V1 + "/cmd/add": func() interface{} { return new(backend.CmdAddRequest) },
|
|
||||||
common.ADMIN_OAUTH_V1 + "/cmd/edit": func() interface{} { return new(backend.CmdEditRequest) },
|
|
||||||
common.ADMIN_OAUTH_V1 + "/cmd/del": func() interface{} { return new(backend.CmdDeleteRequest) },
|
|
||||||
common.ADMIN_OAUTH_V1 + "/cmd/start": func() interface{} { return new(backend.CmdStartRequest) },
|
|
||||||
common.ADMIN_OAUTH_V1 + "/cmd/stop": func() interface{} { return new(backend.CmdStopRequest) },
|
|
||||||
}
|
}
|
||||||
|
|
|
@ -49,12 +49,10 @@ func RegisterAdminRoute(router *gin.Engine) {
|
||||||
//任务
|
//任务
|
||||||
cmd := v1.Group("/cmd")
|
cmd := v1.Group("/cmd")
|
||||||
{
|
{
|
||||||
cmd.POST("/list", backend.CmdList)
|
cmd.GET("/query", backend.Empty)
|
||||||
cmd.POST("/add", backend.CmdAdd)
|
cmd.GET("/info", backend.Empty)
|
||||||
cmd.POST("/edit", backend.CmdEdit)
|
cmd.PUT("/update", backend.Empty)
|
||||||
cmd.DELETE("/del", backend.CmdDel)
|
cmd.DELETE("/delete", backend.Empty)
|
||||||
cmd.POST("/start", backend.CmdStart)
|
|
||||||
cmd.POST("/stop", backend.CmdStop)
|
|
||||||
}
|
}
|
||||||
//消息管理
|
//消息管理
|
||||||
mes := v1.Group("/channel")
|
mes := v1.Group("/channel")
|
||||||
|
|
|
@ -1,15 +1,11 @@
|
||||||
package models
|
package models
|
||||||
|
|
||||||
import (
|
import (
|
||||||
"cron_admin/app/models/croncmdmodel"
|
|
||||||
"cron_admin/app/models/crondbmodel"
|
|
||||||
"cron_admin/app/models/cronreportchannelmodel"
|
"cron_admin/app/models/cronreportchannelmodel"
|
||||||
"cron_admin/app/models/cronusermodel"
|
"cron_admin/app/models/cronusermodel"
|
||||||
)
|
)
|
||||||
|
|
||||||
type PO interface {
|
type PO interface {
|
||||||
cronreportchannelmodel.CronReportChannel |
|
cronreportchannelmodel.CronReportChannel |
|
||||||
cronusermodel.CronUser |
|
cronusermodel.CronUser
|
||||||
crondbmodel.CronDb |
|
|
||||||
croncmdmodel.CronCmd
|
|
||||||
}
|
}
|
||||||
|
|
|
@ -13,7 +13,7 @@ var (
|
||||||
|
|
||||||
// 实体
|
// 实体
|
||||||
type CronCmd struct {
|
type CronCmd struct {
|
||||||
CmdId int `xorm:"'cmd_id' UNSIGNED INT pk autoincr""`
|
CmdId int `xorm:"'cmd_id' UNSIGNED INT"`
|
||||||
CmdName string `xorm:"'cmd_name' varchar(20)"`
|
CmdName string `xorm:"'cmd_name' varchar(20)"`
|
||||||
UserIds string `xorm:"'user_ids' varchar(50)"`
|
UserIds string `xorm:"'user_ids' varchar(50)"`
|
||||||
EntryId int `xorm:"'entry_id' int(10)"`
|
EntryId int `xorm:"'entry_id' int(10)"`
|
||||||
|
@ -27,11 +27,11 @@ type CronCmd struct {
|
||||||
SendTimeType int `xorm:"'send_time_type' TINYINT"`
|
SendTimeType int `xorm:"'send_time_type' TINYINT"`
|
||||||
SendLimit int `xorm:"'send_limit' SMALLINT"`
|
SendLimit int `xorm:"'send_limit' SMALLINT"`
|
||||||
ReportChannelId int `xorm:"'report_channel_id' int(11)"`
|
ReportChannelId int `xorm:"'report_channel_id' int(11)"`
|
||||||
CreateTime *time.Time `xorm:"'create_time' created datetime"`
|
CreateTime *time.Time `xorm:"'create_time' datetime"`
|
||||||
UpdateTime time.Time `xorm:"'update_time' updated timestamp"`
|
UpdateTime time.Time `xorm:"'update_time' timestamp"`
|
||||||
DeletedTime time.Time `xorm:"'deleted_time' deleted datetime"`
|
|
||||||
Status int `xorm:"'status' TINYINT"`
|
Status int `xorm:"'status' TINYINT"`
|
||||||
FailReason string `xorm:"'fail_reason' varchar(200)"`
|
FailReason string `xorm:"'fail_reason' varchar(200)"`
|
||||||
|
DeletedTime time.Time `xorm:"'deleted_time' datetime"`
|
||||||
}
|
}
|
||||||
|
|
||||||
// 表名
|
// 表名
|
||||||
|
|
|
@ -3,12 +3,10 @@ package repository
|
||||||
import (
|
import (
|
||||||
"cron_admin/app/http/entities"
|
"cron_admin/app/http/entities"
|
||||||
"cron_admin/app/models"
|
"cron_admin/app/models"
|
||||||
"cron_admin/app/utils"
|
|
||||||
"cron_admin/app/utils/mapstructure"
|
|
||||||
"github.com/pkg/errors"
|
"github.com/pkg/errors"
|
||||||
"github.com/qit-team/snow-core/db"
|
"github.com/qit-team/snow-core/db"
|
||||||
|
|
||||||
"time"
|
"time"
|
||||||
"xorm.io/builder"
|
|
||||||
"xorm.io/xorm"
|
"xorm.io/xorm"
|
||||||
)
|
)
|
||||||
|
|
||||||
|
@ -16,10 +14,6 @@ type CommonRepo[P models.PO] struct {
|
||||||
repo *xorm.Session
|
repo *xorm.Session
|
||||||
}
|
}
|
||||||
|
|
||||||
type ExtraCommonRepo[P models.PO, T any] struct {
|
|
||||||
repo *xorm.Session
|
|
||||||
}
|
|
||||||
|
|
||||||
type ICommonRepo[P models.PO] interface {
|
type ICommonRepo[P models.PO] interface {
|
||||||
GetSession() *xorm.Session
|
GetSession() *xorm.Session
|
||||||
FindAll(list *[]P, opts ...DBOption) error
|
FindAll(list *[]P, opts ...DBOption) error
|
||||||
|
@ -94,50 +88,9 @@ func (this *CommonRepo[P]) InsertBatch(db *[]P, opts ...DBOption) (int64, error)
|
||||||
func getDb(repo *xorm.Session, opts ...DBOption) *xorm.Session {
|
func getDb(repo *xorm.Session, opts ...DBOption) *xorm.Session {
|
||||||
if repo == nil {
|
if repo == nil {
|
||||||
repo = db.GetDb().NewSession()
|
repo = db.GetDb().NewSession()
|
||||||
|
|
||||||
}
|
}
|
||||||
for _, opt := range opts {
|
for _, opt := range opts {
|
||||||
repo = opt(repo)
|
repo = opt(repo)
|
||||||
}
|
}
|
||||||
return repo
|
return repo
|
||||||
}
|
}
|
||||||
|
|
||||||
func InAndToMap[P models.PO, T string | int](engin *xorm.EngineGroup, key string, values []T) (map[T]*P, error) {
|
|
||||||
|
|
||||||
var (
|
|
||||||
results []*P
|
|
||||||
resultSlice []map[string]interface{}
|
|
||||||
)
|
|
||||||
cond := builder.NewCond()
|
|
||||||
|
|
||||||
cond = cond.And(builder.Eq{key: values})
|
|
||||||
|
|
||||||
err := engin.Where(cond).Find(&results)
|
|
||||||
if err != nil {
|
|
||||||
return nil, err
|
|
||||||
}
|
|
||||||
for _, v := range results {
|
|
||||||
var resultsm map[string]interface{}
|
|
||||||
mapstructure.Decode(v, &resultsm)
|
|
||||||
resultSlice = append(resultSlice, resultsm)
|
|
||||||
}
|
|
||||||
|
|
||||||
resultMap := make(map[T]*P, len(resultSlice))
|
|
||||||
|
|
||||||
for _, v := range resultSlice {
|
|
||||||
keyC := utils.SnakeToCamel(key)
|
|
||||||
if _, exist := v[keyC]; !exist {
|
|
||||||
resultMap = nil
|
|
||||||
break
|
|
||||||
}
|
|
||||||
if v[keyC] == nil || v[keyC] == "" {
|
|
||||||
continue
|
|
||||||
}
|
|
||||||
var vToP P
|
|
||||||
mapstructure.Decode(v, &vToP)
|
|
||||||
keyValue := v[keyC].(T)
|
|
||||||
resultMap[keyValue] = &vToP
|
|
||||||
}
|
|
||||||
|
|
||||||
return resultMap, nil
|
|
||||||
}
|
|
||||||
|
|
|
@ -1,15 +0,0 @@
|
||||||
package repository
|
|
||||||
|
|
||||||
import (
|
|
||||||
"cron_admin/app/models/crondbmodel"
|
|
||||||
)
|
|
||||||
|
|
||||||
type Db struct {
|
|
||||||
ICommonRepo[crondbmodel.CronDb]
|
|
||||||
}
|
|
||||||
|
|
||||||
func NewDbRepo() *Db {
|
|
||||||
return &Db{
|
|
||||||
ICommonRepo: NewCommonRepo[crondbmodel.CronDb](),
|
|
||||||
}
|
|
||||||
}
|
|
|
@ -4,15 +4,8 @@ import (
|
||||||
"cron_admin/app/constants/common"
|
"cron_admin/app/constants/common"
|
||||||
"cron_admin/app/http/entities/backend"
|
"cron_admin/app/http/entities/backend"
|
||||||
"cron_admin/app/models/croncmdmodel"
|
"cron_admin/app/models/croncmdmodel"
|
||||||
"cron_admin/app/models/crondbmodel"
|
|
||||||
"cron_admin/app/models/cronreportchannelmodel"
|
|
||||||
"cron_admin/app/models/cronusermodel"
|
|
||||||
"cron_admin/app/repository"
|
|
||||||
"cron_admin/app/utils/mapstructure"
|
"cron_admin/app/utils/mapstructure"
|
||||||
"fmt"
|
|
||||||
"github.com/ahmetb/go-linq/v3"
|
|
||||||
"strconv"
|
|
||||||
"strings"
|
|
||||||
"xorm.io/builder"
|
"xorm.io/builder"
|
||||||
)
|
)
|
||||||
|
|
||||||
|
@ -26,7 +19,7 @@ func GetListByWhere(request *backend.CmdListRequest, page int, limit int) (count
|
||||||
cond = cond.And(builder.Eq{"status": request.Status})
|
cond = cond.And(builder.Eq{"status": request.Status})
|
||||||
}
|
}
|
||||||
if request.ExecuteType != 0 {
|
if request.ExecuteType != 0 {
|
||||||
cond = cond.And(builder.Eq{"execute_type": request.ExecuteType})
|
cond = cond.And(builder.Eq{"status": request.Status})
|
||||||
}
|
}
|
||||||
if len(request.CmdIds) > 0 {
|
if len(request.CmdIds) > 0 {
|
||||||
// 使用IN查询
|
// 使用IN查询
|
||||||
|
@ -36,7 +29,7 @@ func GetListByWhere(request *backend.CmdListRequest, page int, limit int) (count
|
||||||
session := croncmdmodel.GetInstance().GetDb().Where(cond)
|
session := croncmdmodel.GetInstance().GetDb().Where(cond)
|
||||||
|
|
||||||
if page != 0 && limit != 0 {
|
if page != 0 && limit != 0 {
|
||||||
session = session.Limit(limit, (page-1)*limit)
|
session = session.Limit(page, (page-1)*limit)
|
||||||
}
|
}
|
||||||
count, err = session.FindAndCount(&cmdListInfo)
|
count, err = session.FindAndCount(&cmdListInfo)
|
||||||
|
|
||||||
|
@ -46,128 +39,22 @@ func GetListByWhere(request *backend.CmdListRequest, page int, limit int) (count
|
||||||
return
|
return
|
||||||
}
|
}
|
||||||
|
|
||||||
func Add(request *backend.CmdAddRequest) (err error) {
|
func UserAdd(request *backend.CmdAddRequest) (err error) {
|
||||||
var db croncmdmodel.CronCmd
|
var db croncmdmodel.CronCmd
|
||||||
_ = mapstructure.Decode(request, &db)
|
_ = mapstructure.Decode(request, &db)
|
||||||
db.Status = common.CMD_STATUS_STOP
|
db.Status = common.STATUS_ENABLE
|
||||||
|
|
||||||
_, err = croncmdmodel.GetInstance().GetDb().InsertOne(db)
|
_, err = croncmdmodel.GetInstance().GetDb().InsertOne(db)
|
||||||
return
|
return
|
||||||
}
|
}
|
||||||
|
|
||||||
func Edit(request *backend.CmdEditRequest) (err error) {
|
func UserEdit(request *backend.CmdEditRequest) (err error) {
|
||||||
var db croncmdmodel.CronCmd
|
var db croncmdmodel.CronCmd
|
||||||
_ = mapstructure.Decode(request, &db)
|
_ = mapstructure.Decode(request, &db)
|
||||||
_, err = croncmdmodel.GetInstance().GetDb().ID(request.CmdId).Update(&db)
|
_, err = croncmdmodel.GetInstance().GetDb().ID(request.CmdId).Update(&db)
|
||||||
return
|
return
|
||||||
}
|
}
|
||||||
|
|
||||||
func Del(request *backend.CmdDeleteRequest) (err error) {
|
func UserDel(request *backend.CmdDeleteRequest) (err error) {
|
||||||
_, err = croncmdmodel.GetInstance().GetDb().ID(request.CmdId).Delete(&croncmdmodel.CronCmd{})
|
_, err = croncmdmodel.GetInstance().GetDb().ID(request.CmdId).Delete(&croncmdmodel.CronCmd{})
|
||||||
return
|
return
|
||||||
}
|
}
|
||||||
|
|
||||||
func Start(request *backend.CmdStartRequest) (err error) {
|
|
||||||
var data croncmdmodel.CronCmd
|
|
||||||
exists, err := croncmdmodel.GetInstance().GetDb().ID(request.CmdId).Get(&data)
|
|
||||||
if !exists {
|
|
||||||
return fmt.Errorf("数据不存在")
|
|
||||||
}
|
|
||||||
if err != nil {
|
|
||||||
return
|
|
||||||
}
|
|
||||||
if data.EntryId != 0 {
|
|
||||||
return fmt.Errorf("该任务还没有完全关闭,请稍后再试或联系管理员")
|
|
||||||
}
|
|
||||||
if data.Status == common.CMD_STATUS_WAIT {
|
|
||||||
return fmt.Errorf("任务已在启动中")
|
|
||||||
}
|
|
||||||
if data.Status == common.CMD_STATUS_RUN {
|
|
||||||
return fmt.Errorf("任务已启动,无法再次进行此操作")
|
|
||||||
}
|
|
||||||
data.Status = common.CMD_STATUS_WAIT
|
|
||||||
_, err = croncmdmodel.GetInstance().GetDb().ID(request.CmdId).Update(&data)
|
|
||||||
return
|
|
||||||
}
|
|
||||||
|
|
||||||
func Stop(request *backend.CmdStopRequest) (err error) {
|
|
||||||
var data croncmdmodel.CronCmd
|
|
||||||
exists, err := croncmdmodel.GetInstance().GetDb().ID(request.CmdId).Get(&data)
|
|
||||||
if !exists {
|
|
||||||
return fmt.Errorf("数据不存在")
|
|
||||||
}
|
|
||||||
if err != nil {
|
|
||||||
return
|
|
||||||
}
|
|
||||||
data.Status = common.CMD_STATUS_STOP
|
|
||||||
_, err = croncmdmodel.GetInstance().GetDb().ID(request.CmdId).Update(&data)
|
|
||||||
return
|
|
||||||
}
|
|
||||||
|
|
||||||
func ListToRep(list []croncmdmodel.CronCmd) (cmdList []backend.CmdListResponse, err error) {
|
|
||||||
var (
|
|
||||||
userIdList []int
|
|
||||||
dbIdList []int
|
|
||||||
channelList []int
|
|
||||||
)
|
|
||||||
for _, v := range list {
|
|
||||||
if len(v.UserIds) != 0 {
|
|
||||||
for _, v := range strings.Split(v.UserIds, ",") {
|
|
||||||
intVal, _ := strconv.Atoi(v)
|
|
||||||
userIdList = append(userIdList, intVal)
|
|
||||||
}
|
|
||||||
|
|
||||||
}
|
|
||||||
if v.WriteDbId != 0 {
|
|
||||||
dbIdList = append(dbIdList, v.WriteDbId)
|
|
||||||
}
|
|
||||||
if v.ReadDbId != 0 {
|
|
||||||
dbIdList = append(dbIdList, v.ReadDbId)
|
|
||||||
}
|
|
||||||
if v.ReportChannelId != 0 {
|
|
||||||
channelList = append(channelList, v.ReportChannelId)
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
dbMap, err := repository.InAndToMap[crondbmodel.CronDb, int](crondbmodel.GetInstance().GetDb(), "db_id", dbIdList)
|
|
||||||
userMap, err := repository.InAndToMap[cronusermodel.CronUser, int](cronusermodel.GetInstance().GetDb(), "user_id", userIdList)
|
|
||||||
channelMap, err := repository.InAndToMap[cronreportchannelmodel.CronReportChannel, int](cronreportchannelmodel.GetInstance().GetDb(), "channel_id", channelList)
|
|
||||||
cmdList = make([]backend.CmdListResponse, len(list))
|
|
||||||
linq.From(list).SelectT(func(in croncmdmodel.CronCmd) (d backend.CmdListResponse) {
|
|
||||||
d.ResponseFromDb(in)
|
|
||||||
if in.WriteDbId != 0 {
|
|
||||||
if _, exist := dbMap[in.WriteDbId]; exist {
|
|
||||||
d.WriteDb = dbMap[in.WriteDbId]
|
|
||||||
d.WriteDb.Source = ""
|
|
||||||
} else {
|
|
||||||
d.WriteDb = nil
|
|
||||||
}
|
|
||||||
|
|
||||||
}
|
|
||||||
if in.ReadDbId != 0 {
|
|
||||||
if _, exist := dbMap[in.ReadDbId]; exist {
|
|
||||||
d.ReadDb = dbMap[in.ReadDbId]
|
|
||||||
d.ReadDb.Source = ""
|
|
||||||
} else {
|
|
||||||
d.ReadDb = nil
|
|
||||||
}
|
|
||||||
|
|
||||||
}
|
|
||||||
if in.ReportChannelId != 0 {
|
|
||||||
if _, exist := channelMap[in.ReportChannelId]; exist {
|
|
||||||
d.ReportChannel = channelMap[in.ReportChannelId]
|
|
||||||
} else {
|
|
||||||
d.ReportChannel = nil
|
|
||||||
}
|
|
||||||
|
|
||||||
}
|
|
||||||
if len(in.UserIds) != 0 {
|
|
||||||
for _, v := range strings.Split(in.UserIds, ",") {
|
|
||||||
intVal, _ := strconv.Atoi(v)
|
|
||||||
d.Users = append(d.Users, userMap[intVal])
|
|
||||||
}
|
|
||||||
}
|
|
||||||
return d
|
|
||||||
}).ToSlice(&cmdList)
|
|
||||||
return
|
|
||||||
}
|
|
||||||
|
|
|
@ -22,7 +22,7 @@ func GetListByWhere(request *backend.UserListRequest, page int, limit int) (coun
|
||||||
// 使用IN查询
|
// 使用IN查询
|
||||||
cond = cond.And(builder.In("user_id", request.UserIds))
|
cond = cond.And(builder.In("user_id", request.UserIds))
|
||||||
}
|
}
|
||||||
|
//model := repository.NewCommonRepo[userinfomodel.CronUserModel](userinfomodel.GetInstance().GetDb().NewSession())
|
||||||
session := cronusermodel.GetInstance().GetDb().Where(cond)
|
session := cronusermodel.GetInstance().GetDb().Where(cond)
|
||||||
|
|
||||||
if page != 0 && limit != 0 {
|
if page != 0 && limit != 0 {
|
||||||
|
|
|
@ -410,59 +410,3 @@ func ParseToken(tokenString string) (*jwt.Token, *Claims, error) {
|
||||||
})
|
})
|
||||||
return token, Claims, err
|
return token, Claims, err
|
||||||
}
|
}
|
||||||
|
|
||||||
// toMap 将结构体转换为map[string]interface{}
|
|
||||||
// StructToMap 将一个struct转换为map[string]interface{}
|
|
||||||
func StructToMap(obj interface{}) map[string]interface{} {
|
|
||||||
// 获取obj的类型
|
|
||||||
val := reflect.ValueOf(obj)
|
|
||||||
if val.Kind() == reflect.Ptr {
|
|
||||||
val = val.Elem()
|
|
||||||
}
|
|
||||||
|
|
||||||
// 确保obj是一个struct
|
|
||||||
if val.Kind() != reflect.Struct {
|
|
||||||
return nil
|
|
||||||
}
|
|
||||||
|
|
||||||
// 创建一个map来保存结果
|
|
||||||
data := make(map[string]interface{})
|
|
||||||
|
|
||||||
// 遍历struct的字段
|
|
||||||
for i := 0; i < val.NumField(); i++ {
|
|
||||||
// 获取字段的类型和值
|
|
||||||
valueField := val.Field(i)
|
|
||||||
typeField := val.Type().Field(i)
|
|
||||||
jsonTag := typeField.Tag.Get("json")
|
|
||||||
if idx := strings.Index(jsonTag, ","); idx != -1 {
|
|
||||||
// 如果有逗号,则取逗号之前的部分
|
|
||||||
jsonTag = jsonTag[:idx]
|
|
||||||
}
|
|
||||||
// 忽略未导出的字段(字段名首字母小写)
|
|
||||||
if !typeField.IsExported() {
|
|
||||||
continue
|
|
||||||
}
|
|
||||||
|
|
||||||
// 将字段名和值添加到map中
|
|
||||||
data[jsonTag] = valueField.Interface()
|
|
||||||
}
|
|
||||||
|
|
||||||
return data
|
|
||||||
}
|
|
||||||
|
|
||||||
// SnakeToCamel 将下划线命名法转换为驼峰命名法
|
|
||||||
func SnakeToCamel(s string) string {
|
|
||||||
// 用下划线分割字符串
|
|
||||||
parts := strings.Split(s, "_")
|
|
||||||
var camelCase string
|
|
||||||
|
|
||||||
for _, part := range parts {
|
|
||||||
// 将每个部分的首字母转换为大写,如果它不是第一个部分
|
|
||||||
|
|
||||||
part = strings.Title(strings.ToLower(part))
|
|
||||||
|
|
||||||
camelCase += part
|
|
||||||
}
|
|
||||||
|
|
||||||
return camelCase
|
|
||||||
}
|
|
||||||
|
|
Loading…
Reference in New Issue