From ed3e3291eefd9806e7c311a6c0d656e461c7c179 Mon Sep 17 00:00:00 2001 From: renzhiyuan <465386466@qq.com> Date: Thu, 28 Nov 2024 10:56:52 +0800 Subject: [PATCH] =?UTF-8?q?=E4=BB=BB=E5=8A=A1=E6=A8=A1=E5=9D=97?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- .../controllers/backend/cmd_controller.go | 44 ++++++++ .../controllers/backend/user_controller.go | 10 +- app/http/entities/backend/cmd.go | 103 ++++++++++++++++++ app/models/croncmdmodel/cron_cmd.go | 38 +++---- app/services/cmd_service/user_service.go | 56 ++++++++++ .../{ => user_service}/user_service.go | 2 +- 6 files changed, 228 insertions(+), 25 deletions(-) create mode 100644 app/http/controllers/backend/cmd_controller.go create mode 100644 app/http/entities/backend/cmd.go create mode 100644 app/services/cmd_service/user_service.go rename app/services/{ => user_service}/user_service.go (98%) diff --git a/app/http/controllers/backend/cmd_controller.go b/app/http/controllers/backend/cmd_controller.go new file mode 100644 index 0000000..2a9cb8c --- /dev/null +++ b/app/http/controllers/backend/cmd_controller.go @@ -0,0 +1,44 @@ +package backend + +import ( + "cron_admin/app/constants/errorcode" + "cron_admin/app/http/controllers" + "cron_admin/app/http/entities/backend" + "cron_admin/app/models/cronusermodel" + cmd_services "cron_admin/app/services/cmd_service" + "github.com/ahmetb/go-linq/v3" + "github.com/gin-gonic/gin" +) + +func CmdList(c *gin.Context) { + request := controllers.GetRequest(c).(*backend.CmdListRequest) + count, list, err := cmd_services.GetListByWhere(request, request.Page, request.Limit) + if err != nil { + controllers.HandRes(c, nil, errorcode.NotFound) + } else { + ulist := make([]backend.UserListResponse, 0) + linq.From(list).SelectT(func(in cronusermodel.CronUser) (d backend.UserListResponse) { + d.ResponseFromDb(in) + return d + }).ToSlice(&ulist) + controllers.HandRes(c, gin.H{"data": ulist, "count": count}, err) + } +} + +func CmdAdd(c *gin.Context) { + request := controllers.GetRequest(c).(*backend.CmdAddRequest) + err := cmd_services.UserAdd(request) + controllers.HandRes(c, nil, err) +} + +func CmdEdit(c *gin.Context) { + request := controllers.GetRequest(c).(*backend.CmdEditRequest) + err := cmd_services.UserEdit(request) + controllers.HandRes(c, nil, err) +} + +func CmdDel(c *gin.Context) { + request := controllers.GetRequest(c).(*backend.CmdDeleteRequest) + err := cmd_services.UserDel(request) + controllers.HandRes(c, nil, err) +} diff --git a/app/http/controllers/backend/user_controller.go b/app/http/controllers/backend/user_controller.go index 349c8b2..3b759c2 100644 --- a/app/http/controllers/backend/user_controller.go +++ b/app/http/controllers/backend/user_controller.go @@ -5,14 +5,14 @@ import ( "cron_admin/app/http/controllers" "cron_admin/app/http/entities/backend" "cron_admin/app/models/cronusermodel" - "cron_admin/app/services" + user_services "cron_admin/app/services/user_service" "github.com/ahmetb/go-linq/v3" "github.com/gin-gonic/gin" ) func UserList(c *gin.Context) { request := controllers.GetRequest(c).(*backend.UserListRequest) - count, list, err := services.GetListByWhere(request, request.Page, request.Limit) + count, list, err := user_services.GetListByWhere(request, request.Page, request.Limit) if err != nil { controllers.HandRes(c, nil, errorcode.NotFound) } else { @@ -27,18 +27,18 @@ func UserList(c *gin.Context) { func UserAdd(c *gin.Context) { request := controllers.GetRequest(c).(*backend.UserAddRequest) - err := services.UserAdd(request) + err := user_services.UserAdd(request) controllers.HandRes(c, nil, err) } func UserEdit(c *gin.Context) { request := controllers.GetRequest(c).(*backend.UserEditRequest) - err := services.UserEdit(request) + err := user_services.UserEdit(request) controllers.HandRes(c, nil, err) } func UserDel(c *gin.Context) { request := controllers.GetRequest(c).(*backend.UserDeleteRequest) - err := services.UserDel(request) + err := user_services.UserDel(request) controllers.HandRes(c, nil, err) } diff --git a/app/http/entities/backend/cmd.go b/app/http/entities/backend/cmd.go new file mode 100644 index 0000000..8d47c05 --- /dev/null +++ b/app/http/entities/backend/cmd.go @@ -0,0 +1,103 @@ +package backend + +import ( + "cron_admin/app/models/croncmdmodel" + "time" +) + +type CmdListRequest struct { + Page int `json:"page" validate:"required" form:"page" example:"1"` + Limit int `json:"limit" validate:"required" form:"limit" example:"10"` + CmdName string `json:"cmd_name" form:"cmd_name" example:"155555555"` + Status int `json:"status" form:"status" example:"1"` + ExecuteType int `json:"execute_type" form:"execute_type" example:"46516"` +} + +type CmdInfoRequest struct { + CmdId int `json:"cmd_id" form:"cmd_id" validate:"required" example:"1"` +} + +type CmdListResponse struct { + CmdId string `json:"cmd_id"` + CmdName string `json:"cmd_name"` + UserIds string `json:"user_ids"` + EntryId int `json:"entry_id"` + ReadDbId int `json:"read_db_id"` + WriteDbId int `json:"write_db_id"` + ExecuteType int `json:"execute_type"` + ExecuteRead string `json:"execute_read"` + ExecuteWrite string `json:"execute_write"` + Cron string `json:"cron"` + MatchJson string `json:"match_json"` + SendTimeType int `json:"send_time_type"` + SendLimit int `json:"send_limit"` + ReportChannelId int `json:"report_channel_id"` + CreateTime string `json:"create_time"` + UpdateTime string `json:"update_time"` + Status int `json:"status"` + FailReason string `json:"fail_reason"` + Users []*UserListResponse `json:"users"` + ReadDb DbListResponse `json:"read_db"` + WriteDb DbListResponse `json:"write_db"` + ReportChannel ReportChannelListResponse `json:"report_channel"` +} + +type CmdAddRequest struct { + CmdName string `json:"cmd_name"` + UserIds string `json:"user_ids"` + ReadDbId int `json:"read_db_id"` + WriteDbId int `json:"write_db_id"` + ExecuteType int `json:"execute_type"` + ExecuteRead string `json:"execute_read"` + ExecuteWrite string `json:"execute_write"` + Cron string `json:"cron"` + MatchJson string `json:"match_json"` + SendTimeType int `json:"send_time_type"` + SendLimit int `json:"send_limit"` + ReportChannelId int `json:"report_channel_id"` +} + +type CmdEditRequest struct { + CmdId int `json:"cmd_id" validate:"required" form:"cmd_id" example:""` + CmdName string `json:"cmd_name"` + UserIds string `json:"user_ids"` + ReadDbId int `json:"read_db_id"` + WriteDbId int `json:"write_db_id"` + ExecuteType int `json:"execute_type"` + ExecuteRead string `json:"execute_read"` + ExecuteWrite string `json:"execute_write"` + Cron string `json:"cron"` + MatchJson string `json:"match_json"` + SendTimeType int `json:"send_time_type"` + SendLimit int `json:"send_limit"` + ReportChannelId int `json:"report_channel_id"` +} + +type CmdDeleteRequest struct { + CmdId int `json:"user_id" validate:"required" form:"user_id" example:""` +} + +func (response *CmdListResponse) ResponseFromDb(l croncmdmodel.CronCmd) { + response.CmdId = l.CmdId + response.CmdName = l.CmdName + response.UserIds = l.UserIds + response.Status = l.Status + response.ReadDbId = l.ReadDbId + response.WriteDbId = l.WriteDbId + response.ExecuteType = l.ExecuteType + response.ExecuteRead = l.ExecuteRead + response.ExecuteWrite = l.ExecuteWrite + response.Cron = l.Cron + response.MatchJson = l.MatchJson + response.SendTimeType = l.SendTimeType + response.SendLimit = l.SendLimit + response.ReportChannelId = l.ReportChannelId + response.CreateTime = l.CreateTime.Format(time.DateTime) + response.UpdateTime = l.UpdateTime.Format(time.DateTime) + response.FailReason = l.FailReason + + //cronusermodel.GetInstance().GetDb().Where("user_id IN (?)", strings.Join(strings.Split(l.UserIds, ","), ",")).Find(&response.Users) + //crond.GetInstance().GetDb().Where("user_id = ?", strings.Join(strings.Split(l.UserIds, ","), ",")).Find(&response.Users) + return + +} diff --git a/app/models/croncmdmodel/cron_cmd.go b/app/models/croncmdmodel/cron_cmd.go index 9fa7520..ec9140b 100644 --- a/app/models/croncmdmodel/cron_cmd.go +++ b/app/models/croncmdmodel/cron_cmd.go @@ -13,25 +13,25 @@ var ( // 实体 type CronCmd struct { - CmdId string `xorm:"'cmd_id' UNSIGNED INT"` - CmdName string `xorm:"'cmd_name' varchar(20)"` - UserIds string `xorm:"'user_ids' varchar(50)"` - EntryId int `xorm:"'entry_id' int(10)"` - ReadDbId int `xorm:"'read_db_id' int(10)"` - WriteDbId int `xorm:"'write_db_id' int(11)"` - ExecuteType int `xorm:"'execute_type' TINYINT"` - ExecuteRead string `xorm:"'execute_read' TEXT"` - ExecuteWrite string `xorm:"'execute_write' TEXT"` - Cron string `xorm:"'cron' varchar(64)"` - MatchJson string `xorm:"'match_json' JSON"` - SendTimeType int `xorm:"'send_time_type' TINYINT"` - SendLimit int `xorm:"'send_limit' SMALLINT"` - ReportChannelId int `xorm:"'report_channel_id' int(11)"` - CreateTime time.Time `xorm:"'create_time' datetime"` - UpdateTime time.Time `xorm:"'update_time' timestamp"` - Status int `xorm:"'status' TINYINT"` - FailReason string `xorm:"'fail_reason' varchar(200)"` - DeletedTime time.Time `xorm:"'deleted_time' datetime"` + CmdId string `xorm:"'cmd_id' UNSIGNED INT"` + CmdName string `xorm:"'cmd_name' varchar(20)"` + UserIds string `xorm:"'user_ids' varchar(50)"` + EntryId int `xorm:"'entry_id' int(10)"` + ReadDbId int `xorm:"'read_db_id' int(10)"` + WriteDbId int `xorm:"'write_db_id' int(11)"` + ExecuteType int `xorm:"'execute_type' TINYINT"` + ExecuteRead string `xorm:"'execute_read' TEXT"` + ExecuteWrite string `xorm:"'execute_write' TEXT"` + Cron string `xorm:"'cron' varchar(64)"` + MatchJson string `xorm:"'match_json' JSON"` + SendTimeType int `xorm:"'send_time_type' TINYINT"` + SendLimit int `xorm:"'send_limit' SMALLINT"` + ReportChannelId int `xorm:"'report_channel_id' int(11)"` + CreateTime *time.Time `xorm:"'create_time' datetime"` + UpdateTime time.Time `xorm:"'update_time' timestamp"` + Status int `xorm:"'status' TINYINT"` + FailReason string `xorm:"'fail_reason' varchar(200)"` + DeletedTime time.Time `xorm:"'deleted_time' datetime"` } // 表名 diff --git a/app/services/cmd_service/user_service.go b/app/services/cmd_service/user_service.go new file mode 100644 index 0000000..3dc2a33 --- /dev/null +++ b/app/services/cmd_service/user_service.go @@ -0,0 +1,56 @@ +package cmd_services + +import ( + "cron_admin/app/constants/common" + "cron_admin/app/http/entities/backend" + "cron_admin/app/models/croncmdmodel" + + "cron_admin/app/utils/mapstructure" + "xorm.io/builder" +) + +func GetListByWhere(request *backend.CmdListRequest, page int, limit int) (count int64, cmdListInfo []croncmdmodel.CronCmd, err error) { + cond := builder.NewCond() + + if request.CmdName != "" { + cond = cond.And(builder.Like{"cmd_name", request.CmdName}) + } + if request.Status != 0 { + cond = cond.And(builder.Eq{"status": request.Status}) + } + if request.ExecuteType != 0 { + cond = cond.And(builder.Eq{"status": request.Status}) + } + + session := croncmdmodel.GetInstance().GetDb().Where(cond) + + if page != 0 && limit != 0 { + session = session.Limit(page, (page-1)*limit) + } + count, err = session.FindAndCount(&cmdListInfo) + + if err != nil { + return + } + return +} + +func UserAdd(request *backend.CmdAddRequest) (err error) { + var db croncmdmodel.CronCmd + _ = mapstructure.Decode(request, &db) + db.Status = common.STATUS_ENABLE + _, err = croncmdmodel.GetInstance().GetDb().InsertOne(db) + return +} + +func UserEdit(request *backend.CmdEditRequest) (err error) { + var db croncmdmodel.CronCmd + _ = mapstructure.Decode(request, &db) + _, err = croncmdmodel.GetInstance().GetDb().ID(request.CmdId).Update(&db) + return +} + +func UserDel(request *backend.CmdDeleteRequest) (err error) { + _, err = croncmdmodel.GetInstance().GetDb().ID(request.CmdId).Delete(&croncmdmodel.CronCmd{}) + return +} diff --git a/app/services/user_service.go b/app/services/user_service/user_service.go similarity index 98% rename from app/services/user_service.go rename to app/services/user_service/user_service.go index a01314b..d044636 100644 --- a/app/services/user_service.go +++ b/app/services/user_service/user_service.go @@ -1,4 +1,4 @@ -package services +package user_services import ( "cron_admin/app/constants/common"