This commit is contained in:
wolter 2024-05-06 16:01:12 +08:00
parent 60601f3220
commit bfe5bfb9a8
10 changed files with 231 additions and 18 deletions

View File

@ -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")
}
}

View File

@ -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
}

View File

@ -0,0 +1,6 @@
package domains
type Filter struct {
Page int `json:"page"`
PageSize int `json:"page_size"`
}

View File

@ -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
}

View File

@ -4,6 +4,7 @@ import (
"github.com/gin-gonic/gin" "github.com/gin-gonic/gin"
"github.com/qit-team/snow-core/http/middleware" "github.com/qit-team/snow-core/http/middleware"
"qteam/app/http/controllers" "qteam/app/http/controllers"
"qteam/app/http/controllers/backend"
"qteam/app/http/middlewares" "qteam/app/http/middlewares"
"qteam/app/http/trace" "qteam/app/http/trace"
"qteam/app/utils" "qteam/app/utils"
@ -25,4 +26,11 @@ func RegisterAdminRoute(router *gin.Engine) {
{ {
//api.GET("/banner_list", controllers.GetBannerList) //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")
} }

View File

@ -49,4 +49,10 @@ func RegisterRoute(router *gin.Engine) {
} }
router.GET("/swagger/*any", ginSwagger.WrapHandler(swaggerFiles.Handler)) 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)
} }

View File

@ -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
}

View File

@ -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
}

21
app/services/product.go Normal file
View File

@ -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
}

View File

@ -1,16 +1,14 @@
package bootstrap package bootstrap
import ( import (
"github.com/qit-team/snow-core/log/accesslogger"
"qteam/app/jobs" "qteam/app/jobs"
"qteam/app/jobs/basejob" "qteam/app/jobs/basejob"
"qteam/app/utils"
"qteam/config" "qteam/config"
"github.com/nacos-group/nacos-sdk-go/v2/vo"
"github.com/qit-team/snow-core/db" "github.com/qit-team/snow-core/db"
"github.com/qit-team/snow-core/kernel/close" "github.com/qit-team/snow-core/kernel/close"
"github.com/qit-team/snow-core/kernel/container" "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/log/logger"
"github.com/qit-team/snow-core/redis" "github.com/qit-team/snow-core/redis"
) )
@ -59,21 +57,21 @@ func Bootstrap(conf *config.Config) (err error) {
//注册应用停止时调用的关闭服务 //注册应用停止时调用的关闭服务
close.MultiRegister(db.Pr, redis.Pr) close.MultiRegister(db.Pr, redis.Pr)
//Register ////Register
_, err = utils.GetNaocosClient().RegisterInstance(vo.RegisterInstanceParam{ //_, err = utils.GetNaocosClient().RegisterInstance(vo.RegisterInstanceParam{
Ip: utils.GetHostIp(), // Ip: utils.GetHostIp(),
Port: uint64(conf.Api.Port), // Port: uint64(conf.Api.Port),
ServiceName: "snow", // ServiceName: "snow",
GroupName: "group-d", // GroupName: "group-d",
ClusterName: "cluster-snow", // ClusterName: "cluster-snow",
Weight: 10, // Weight: 10,
Enable: true, // Enable: true,
Healthy: true, // Healthy: true,
Ephemeral: true, // Ephemeral: true,
Metadata: map[string]string{}, // Metadata: map[string]string{},
}) //})
//
utils.Log(nil, "nacos err", err) //utils.Log(nil, "nacos err", err)
//注册job register为了非job模式的消息入队调用 //注册job register为了非job模式的消息入队调用
basejob.SetJobRegister(jobs.RegisterWorker) basejob.SetJobRegister(jobs.RegisterWorker)