From bfe5bfb9a80ef2d0fb997d25091de6f55dc6aaf1 Mon Sep 17 00:00:00 2001 From: wolter Date: Mon, 6 May 2024 16:01:12 +0800 Subject: [PATCH] init --- app/http/controllers/backend/product.go | 34 +++++++++++++ .../controllers/backend/view_model/product.go | 35 +++++++++++++ app/http/domains/filter.go | 6 +++ app/http/domains/product.go | 20 ++++++++ app/http/routes/admin.go | 8 +++ app/http/routes/route.go | 6 +++ app/models/productsmodel/domain.go | 36 ++++++++++++++ app/models/productsmodel/products.go | 49 +++++++++++++++++++ app/services/product.go | 21 ++++++++ bootstrap/bootstrap.go | 34 ++++++------- 10 files changed, 231 insertions(+), 18 deletions(-) create mode 100644 app/http/controllers/backend/product.go create mode 100644 app/http/controllers/backend/view_model/product.go create mode 100644 app/http/domains/filter.go create mode 100644 app/http/domains/product.go create mode 100644 app/models/productsmodel/domain.go create mode 100644 app/models/productsmodel/products.go create mode 100644 app/services/product.go diff --git a/app/http/controllers/backend/product.go b/app/http/controllers/backend/product.go new file mode 100644 index 0000000..65cff89 --- /dev/null +++ b/app/http/controllers/backend/product.go @@ -0,0 +1,34 @@ +package backend + +import ( + "github.com/gin-gonic/gin" + "github.com/qit-team/snow-core/log/logger" + "qteam/app/constants/errorcode" + "qteam/app/http/controllers" + "qteam/app/http/controllers/backend/view_model" + "qteam/app/services" +) + +func ProductCreateHandler(c *gin.Context) { + + req, exists := c.Get("request") + if !exists { + controllers.Error(c, errorcode.ParamError) + logger.Error(c.Request.Context(), "req不存在") + return + } + in, ok := req.(view_model.ProductCreateRequest) + if !ok { + controllers.Error(c, errorcode.ParamError) + logger.Error(c.Request.Context(), "req不匹配") + return + } + + do, _ := in.ToDomain() + if err := services.ProductCreate(do); err != nil { + controllers.Error(c, errorcode.SystemError) + } else { + controllers.Success(c, errorcode.Success, "product create success") + } + +} diff --git a/app/http/controllers/backend/view_model/product.go b/app/http/controllers/backend/view_model/product.go new file mode 100644 index 0000000..f05b177 --- /dev/null +++ b/app/http/controllers/backend/view_model/product.go @@ -0,0 +1,35 @@ +package view_model + +import "qteam/app/http/domains" + +type ProductCreateRequest struct { + ProductName string `json:"product_name"` + ProductType int `json:"product_type"` + PacketRule int `json:"packet_rule"` + ProductKind int `json:"'product_kind'"` + Amount string `json:"'amount'"` + CouponType int `json:"'coupon_type'"` + IsSuperposition int `json:"'is_superposition' "` + TemplateId int `json:"'template_id'"` + Status int `json:"status"` + IsNeedBill int `json:"'is_need_bill'"` + //CreateTime string `json:"'create_time'"` + SupplierProductId int64 `json:"supplier_product_id"` + SupplierProductName string `json:"supplier_product_name"` +} + +func (p *ProductCreateRequest) ToDomain() (do domains.Product, err error) { + do.ProductName = p.ProductName + do.ProductType = p.ProductType + do.PacketRule = p.PacketRule + do.ProductKind = p.ProductKind + do.Amount = p.Amount + do.CouponType = p.CouponType + do.IsSuperposition = p.IsSuperposition + do.TemplateId = do.TemplateId + do.Status = p.Status + do.SupplierProductId = p.SupplierProductId + do.SupplierProductName = p.SupplierProductName + + return +} diff --git a/app/http/domains/filter.go b/app/http/domains/filter.go new file mode 100644 index 0000000..62301b1 --- /dev/null +++ b/app/http/domains/filter.go @@ -0,0 +1,6 @@ +package domains + +type Filter struct { + Page int `json:"page"` + PageSize int `json:"page_size"` +} diff --git a/app/http/domains/product.go b/app/http/domains/product.go new file mode 100644 index 0000000..091968e --- /dev/null +++ b/app/http/domains/product.go @@ -0,0 +1,20 @@ +package domains + +import "time" + +type Product struct { + Id int + ProductName string + ProductType int + PacketRule int + ProductKind int + Amount string + CouponType int + IsSuperposition int + TemplateId int + Status int + IsNeedBill int + CreateTime time.Time + SupplierProductId int64 + SupplierProductName string +} diff --git a/app/http/routes/admin.go b/app/http/routes/admin.go index ef2fc34..5e47549 100644 --- a/app/http/routes/admin.go +++ b/app/http/routes/admin.go @@ -4,6 +4,7 @@ import ( "github.com/gin-gonic/gin" "github.com/qit-team/snow-core/http/middleware" "qteam/app/http/controllers" + "qteam/app/http/controllers/backend" "qteam/app/http/middlewares" "qteam/app/http/trace" "qteam/app/utils" @@ -25,4 +26,11 @@ func RegisterAdminRoute(router *gin.Engine) { { //api.GET("/banner_list", controllers.GetBannerList) } + + v1 := router.Group("/backend/v1", middlewares.ValidateRequest()) + products := v1.Group("/products", backend.ProductCreateHandler) + products.POST("/create") + products.GET("/query") + products.PUT("/update") + products.DELETE("/delete") } diff --git a/app/http/routes/route.go b/app/http/routes/route.go index 96c677c..a130012 100644 --- a/app/http/routes/route.go +++ b/app/http/routes/route.go @@ -49,4 +49,10 @@ func RegisterRoute(router *gin.Engine) { } router.GET("/swagger/*any", ginSwagger.WrapHandler(swaggerFiles.Handler)) + + //router.GET("/hello", controllers.HelloHandler) + //router.GET("/create", controllers.HelloCreateHandler) + //router.GET("/update", controllers.UpdateHandler) + //router.GET("/delete", controllers.DeleteHandler) + //router.GET("/query", controllers.QueryHandler) } diff --git a/app/models/productsmodel/domain.go b/app/models/productsmodel/domain.go new file mode 100644 index 0000000..9257f99 --- /dev/null +++ b/app/models/productsmodel/domain.go @@ -0,0 +1,36 @@ +package productsmodel + +import "qteam/app/http/domains" + +func (p *Products) FromDomain(do domains.Product) { + p.Id = do.Id + p.Productname = do.ProductName + p.Producttype = do.ProductType + p.Packetrule = do.PacketRule + p.Productkind = do.ProductKind + p.Amount = do.Amount + p.Coupontype = do.CouponType + p.Issuperposition = do.IsSuperposition + p.Status = do.Status + p.Isneedbill = do.TemplateId + p.Createtime = do.CreateTime + p.Supplierproductname = do.SupplierProductName + p.Supplierproductid = do.SupplierProductId +} + +func (p *Products) ToDomain() (do domains.Product) { + do.Id = p.Id + do.ProductName = p.Productname + do.ProductType = p.Producttype + do.PacketRule = p.Packetrule + do.ProductKind = p.Productkind + do.Amount = p.Amount + do.CouponType = p.Coupontype + do.IsSuperposition = p.Issuperposition + do.Status = p.Status + do.TemplateId = p.Isneedbill + do.CreateTime = p.Createtime + do.SupplierProductName = p.Supplierproductname + do.SupplierProductId = p.Supplierproductid + return +} diff --git a/app/models/productsmodel/products.go b/app/models/productsmodel/products.go new file mode 100644 index 0000000..7a57ae4 --- /dev/null +++ b/app/models/productsmodel/products.go @@ -0,0 +1,49 @@ +package productsmodel + +import ( + "github.com/qit-team/snow-core/db" + "sync" + "time" +) + +var ( + once sync.Once + m *ProductsModel +) + +// 实体 +type Products struct { + Id int `xorm:"'Id' int(0)"` + Productname string `xorm:"'ProductName' varchar(255)"` + Producttype int `xorm:"'ProductType' int(0)"` + Packetrule int `xorm:"'PacketRule' TINYINT"` + Productkind int `xorm:"'ProductKind' TINYINT"` + Amount string `xorm:"'Amount' decimal(9,2)"` + Coupontype int `xorm:"'CouponType' TINYINT"` + Issuperposition int `xorm:"'IsSuperposition' TINYINT"` + Templateid int `xorm:"'TemplateId' int(0)"` + Status int `xorm:"'Status' TINYINT"` + Isneedbill int `xorm:"'IsNeedBill' TINYINT"` + Createtime time.Time `xorm:"'CreateTime' datetime"` + Supplierproductid int64 `xorm:"'SupplierProductId' bigint(0)"` + Supplierproductname string `xorm:"'SupplierProductName' varchar(255)"` +} + +// 表名 +func (m *Products) TableName() string { + return "Products" +} + +// 私有化,防止被外部new +type ProductsModel struct { + db.Model //组合基础Model,集成基础Model的属性和方法 +} + +// 单例模式 +func GetInstance() *ProductsModel { + once.Do(func() { + m = new(ProductsModel) + //m.DiName = "" //设置数据库实例连接,默认db.SingletonMain + }) + return m +} diff --git a/app/services/product.go b/app/services/product.go new file mode 100644 index 0000000..ee8f1af --- /dev/null +++ b/app/services/product.go @@ -0,0 +1,21 @@ +package services + +import ( + "errors" + "qteam/app/http/domains" + "qteam/app/models/productsmodel" +) + +func ProductCreate(do domains.Product) (err error) { + m := productsmodel.Products{} + m.FromDomain(do) + count, err := productsmodel.GetInstance().Insert(m) + if err != nil { + return + } + if count == 0 { + err = errors.New("product create fail") + } + + return +} diff --git a/bootstrap/bootstrap.go b/bootstrap/bootstrap.go index 6e22df4..785b058 100644 --- a/bootstrap/bootstrap.go +++ b/bootstrap/bootstrap.go @@ -1,16 +1,14 @@ package bootstrap import ( + "github.com/qit-team/snow-core/log/accesslogger" "qteam/app/jobs" "qteam/app/jobs/basejob" - "qteam/app/utils" "qteam/config" - "github.com/nacos-group/nacos-sdk-go/v2/vo" "github.com/qit-team/snow-core/db" "github.com/qit-team/snow-core/kernel/close" "github.com/qit-team/snow-core/kernel/container" - "github.com/qit-team/snow-core/log/accesslogger" "github.com/qit-team/snow-core/log/logger" "github.com/qit-team/snow-core/redis" ) @@ -59,21 +57,21 @@ func Bootstrap(conf *config.Config) (err error) { //注册应用停止时调用的关闭服务 close.MultiRegister(db.Pr, redis.Pr) - //Register - _, err = utils.GetNaocosClient().RegisterInstance(vo.RegisterInstanceParam{ - Ip: utils.GetHostIp(), - Port: uint64(conf.Api.Port), - ServiceName: "snow", - GroupName: "group-d", - ClusterName: "cluster-snow", - Weight: 10, - Enable: true, - Healthy: true, - Ephemeral: true, - Metadata: map[string]string{}, - }) - - utils.Log(nil, "nacos err", err) + ////Register + //_, err = utils.GetNaocosClient().RegisterInstance(vo.RegisterInstanceParam{ + // Ip: utils.GetHostIp(), + // Port: uint64(conf.Api.Port), + // ServiceName: "snow", + // GroupName: "group-d", + // ClusterName: "cluster-snow", + // Weight: 10, + // Enable: true, + // Healthy: true, + // Ephemeral: true, + // Metadata: map[string]string{}, + //}) + // + //utils.Log(nil, "nacos err", err) //注册job register,为了非job模式的消息入队调用 basejob.SetJobRegister(jobs.RegisterWorker)