feat: import services and standardize Go 1.26.5
This commit is contained in:
87
apps/ec/order/internal/models/impl.go
Normal file
87
apps/ec/order/internal/models/impl.go
Normal file
@@ -0,0 +1,87 @@
|
||||
package models
|
||||
|
||||
import (
|
||||
"log"
|
||||
"strings"
|
||||
|
||||
"git.apinb.com/bsm-sdk/core/database/sql"
|
||||
"git.apinb.com/bsm-sdk/core/types"
|
||||
"gorm.io/driver/mysql"
|
||||
"gorm.io/gorm"
|
||||
)
|
||||
|
||||
var DBService *gorm.DB
|
||||
|
||||
var migrateTables = []any{
|
||||
&OrderCart{},
|
||||
&OrderCoupon{},
|
||||
&OrderDetails{},
|
||||
&OrderSummary{},
|
||||
}
|
||||
|
||||
func New(driver string, dsn []string, options *types.SqlOptions) (err error) {
|
||||
driver = strings.ToLower(driver)
|
||||
|
||||
switch driver {
|
||||
case "mysql":
|
||||
DBService, err = NewMysql(dsn, options)
|
||||
case "postgres":
|
||||
DBService, err = NewPostgres(dsn, options)
|
||||
default:
|
||||
log.Fatalln("Unsupported database driver:", driver)
|
||||
return
|
||||
}
|
||||
|
||||
if err != nil {
|
||||
log.Fatalln(err)
|
||||
return err
|
||||
}
|
||||
|
||||
// auto migrate table.
|
||||
err = DBService.AutoMigrate(migrateTables...)
|
||||
if err != nil {
|
||||
log.Fatalln(err)
|
||||
return err
|
||||
}
|
||||
|
||||
return
|
||||
}
|
||||
|
||||
func NewMysql(dsn []string, options *types.SqlOptions) (gormDb *gorm.DB, err error) {
|
||||
//set connection default val.
|
||||
options = sql.SetOptions(options)
|
||||
|
||||
gormDb, err = gorm.Open(mysql.Open(dsn[0]), &gorm.Config{
|
||||
SkipDefaultTransaction: true,
|
||||
})
|
||||
if err != nil {
|
||||
return nil, err
|
||||
}
|
||||
|
||||
if options.Debug {
|
||||
gormDb = gormDb.Debug()
|
||||
}
|
||||
|
||||
// 获取通用数据库对象 sql.DB ,然后使用其提供的功能
|
||||
sqlDB, _ := gormDb.DB()
|
||||
// SetMaxIdleConns 用于设置连接池中空闲连接的最大数量。
|
||||
sqlDB.SetMaxIdleConns(options.MaxIdleConns)
|
||||
// SetMaxOpenConns 设置打开数据库连接的最大数量。
|
||||
sqlDB.SetMaxOpenConns(options.MaxOpenConns)
|
||||
// SetConnMaxLifetime 设置了连接可复用的最大时间。
|
||||
sqlDB.SetConnMaxLifetime(options.ConnMaxLifetime)
|
||||
|
||||
return gormDb, nil
|
||||
}
|
||||
|
||||
func NewPostgres(dsn []string, options *types.SqlOptions) (gormDb *gorm.DB, err error) {
|
||||
//set connection default val.
|
||||
options = sql.SetOptions(options)
|
||||
|
||||
db, err := sql.NewPostgreSql(dsn[0], options)
|
||||
if err != nil {
|
||||
return nil, err
|
||||
}
|
||||
db = db.Debug()
|
||||
return db, nil
|
||||
}
|
||||
30
apps/ec/order/internal/models/order_cart.go
Normal file
30
apps/ec/order/internal/models/order_cart.go
Normal file
@@ -0,0 +1,30 @@
|
||||
// Models generated by mesh dev cli,@Author: David Yan(david.yan@qq.com).
|
||||
package models
|
||||
|
||||
import (
|
||||
"git.apinb.com/bsm-sdk/core/types"
|
||||
"gorm.io/gorm"
|
||||
)
|
||||
|
||||
/*
|
||||
* OrderCart
|
||||
* Comment: 订单详情
|
||||
* Version: 10
|
||||
* Created: 2022-04-11 23:27:50 , Updated:0001-01-01 00:00:00
|
||||
*/
|
||||
type OrderCart struct {
|
||||
gorm.Model
|
||||
types.Std_Identity
|
||||
types.Std_Passport
|
||||
CartIdentity string `gorm:"column:cart_identity;type:varchar(36);index;not null';" json:"cart_identity"`
|
||||
ProductID int64 `gorm:"column:product_id;default:0;" json:"product_id"`
|
||||
ProductIdentity string `gorm:"column:product_identity;type:varchar(36);Index;" json:"product_identity"` // 产品唯一标识,24位NanoID,36位为UUID
|
||||
ProductArgs string `gorm:"column:product_args;type:text;default:'';" json:"product_args"` // 产品参数
|
||||
Number int32 `gorm:"column:number;default:0;" json:"number"` // 订单产品数量
|
||||
SpecID int64 `gorm:"column:spec_id;" json:"spec_id"` // 规格编号
|
||||
}
|
||||
|
||||
// TableName .
|
||||
func (table *OrderCart) TableName() string {
|
||||
return "order_cart" //对应数据库表名
|
||||
}
|
||||
26
apps/ec/order/internal/models/order_coupon.go
Normal file
26
apps/ec/order/internal/models/order_coupon.go
Normal file
@@ -0,0 +1,26 @@
|
||||
// Models generated by mesh dev cli,@Author: David Yan(david.yan@qq.com).
|
||||
package models
|
||||
|
||||
import (
|
||||
"git.apinb.com/bsm-sdk/core/types"
|
||||
"gorm.io/gorm"
|
||||
)
|
||||
|
||||
// OrderCoupon 优惠券
|
||||
type OrderCoupon struct {
|
||||
gorm.Model
|
||||
types.Std_Identity
|
||||
types.Std_Passport
|
||||
Title string `gorm:"column:title;type:varchar(255);default:'';" json:"title"` // 标题
|
||||
Intro string `gorm:"column:intro;type:varchar(255);default:'';" json:"intro"` // 描述
|
||||
Amount int64 `gorm:"column:amount;default:0;" json:"amount"` // 金额
|
||||
Condition string `gorm:"column:condition;type:varchar(255);default:'';" json:"condition"` // 条件
|
||||
Started string `gorm:"column:started;type:varchar(255);default:'';" json:"started"` // 开始时间
|
||||
Expired string `gorm:"column:expired;type:varchar(20);default:'';" json:"expired"` // 结束时间
|
||||
types.Std_Status
|
||||
}
|
||||
|
||||
// TableName .
|
||||
func (table *OrderCoupon) TableName() string {
|
||||
return "order_coupon" //对应数据库表名
|
||||
}
|
||||
33
apps/ec/order/internal/models/order_details.go
Normal file
33
apps/ec/order/internal/models/order_details.go
Normal file
@@ -0,0 +1,33 @@
|
||||
// Models generated by mesh dev cli,@Author: David Yan(david.yan@qq.com).
|
||||
package models
|
||||
|
||||
import (
|
||||
"gorm.io/gorm"
|
||||
)
|
||||
|
||||
// OrderDetails /* 订单详情 */
|
||||
type OrderDetails struct {
|
||||
gorm.Model
|
||||
SupplyId int64 `gorm:"column:supply_id;default:0;supply_id;"` // 供应商ID
|
||||
SummaryIdentity string `gorm:"column:summary_identity;type:varchar(36);Index;" json:"summary_identity"` // 订单identity
|
||||
OrderNo string `gorm:"column:order_no;type:varchar(36);index;not null;" json:"order_no"` // 订单号
|
||||
ProductID int64 `gorm:"column:product_id;not null;" json:"product_id"` // 商品ID
|
||||
ProductIdentity string `gorm:"column:product_identity;type:varchar(36);Index;" json:"product_identity"` // 产品唯一标识,24位NanoID,36位为UUID
|
||||
SpecID int64 `gorm:"column:spec_id;" json:"spec_id"` // 商品规格ID
|
||||
Type int8 `gorm:"column:type;default:1;" json:"type"` // 商品类型:Product=1 实体商品,Service=2 服务,Membership=3 会员服务,Other=4 其它
|
||||
Title string `gorm:"column:title;type:varchar(255);default:'';" json:"title"` // 产品标题
|
||||
SpecTitle string `gorm:"column:spec_title;type:varchar(255);default:'';" json:"spec_title"` // 产品规格标题
|
||||
SpecNo string `gorm:"column:spec_no;type:varchar(255);default:'';" json:"spec_no"` // 产品规格编号
|
||||
CoverImage string `gorm:"column:cover_image;type:varchar(255);default:'';" json:"cover_image"` // 封面
|
||||
ProductArgs string `gorm:"column:product_args;type:text;default:'';" json:"product_args"` // 产品参数
|
||||
Number int32 `gorm:"column:number;default:0;" json:"number"` // 订单产品数量
|
||||
UnitPrice int64 `gorm:"column:unit_price;default:0;" json:"unit_price"` // 单品实际价格
|
||||
SalesPrice int64 `gorm:"column:sales_price;default:0;" json:"sales_price"` // 单品原价
|
||||
GasType int32 `gorm:"column:gas_types;default:1;" json:"gas_types"` // 商品类型:1其它商品 2按瓶计费,3 按kg计费
|
||||
TotalPrice int64 `gorm:"column:total_price;default:0;" json:"total_price"` // 产品总价
|
||||
}
|
||||
|
||||
// TableName .
|
||||
func (table *OrderDetails) TableName() string {
|
||||
return "order_details" //对应数据库表名
|
||||
}
|
||||
151
apps/ec/order/internal/models/order_summary.go
Normal file
151
apps/ec/order/internal/models/order_summary.go
Normal file
@@ -0,0 +1,151 @@
|
||||
// Models generated by mesh dev cli,@Author: David Yan(david.yan@qq.com).
|
||||
package models
|
||||
|
||||
import (
|
||||
"time"
|
||||
|
||||
"git.apinb.com/bsm-sdk/core/types"
|
||||
"gorm.io/gorm"
|
||||
)
|
||||
|
||||
// OrderSummary 订单表/*
|
||||
type OrderSummary struct {
|
||||
gorm.Model
|
||||
types.Std_Identity
|
||||
types.Std_Passport
|
||||
|
||||
// 订单信息
|
||||
OrderNo string `gorm:"column:order_no;type:varchar(36);index;not null;" json:"order_no"` // 订单号
|
||||
StoreID uint `gorm:"column:store_id;Index;"` // 店铺ID
|
||||
StoreIdentity string `gorm:"column:store_identity;type:varchar(36);Index;"` // 店铺唯一标识,24位NanoID,36位为UUID
|
||||
PartnerID int32 `gorm:"column:partner_id;default:0;" json:"partner_id"` // 分销商id
|
||||
TotalPrice int64 `gorm:"column:total_price;default:0;" json:"total_price"` // 订单金额
|
||||
LogisticsFee int64 `gorm:"column:logistics_fee;default:0;" json:"logistics_fee"` // 运费
|
||||
TransPrice int64 `gorm:"column:trans_price;default:0;" json:"trans_price"` // 订单金额(实际)
|
||||
RefundPrice int64 `gorm:"column:refund_price;default:0;" json:"refund_price"` // 已退款金额
|
||||
CouponIdentity string `gorm:"column:coupon_identity;type:varchar(64);default:'';" json:"coupon_identity"` // 优惠卷唯一码
|
||||
CouponAmount int64 `gorm:"column:coupon_amount;default:0;" json:"coupon_amount"` // 优惠金额
|
||||
Remark string `gorm:"column:remark;type:text;default:'';" json:"remark"` // 备注
|
||||
Args string `gorm:"column:args;type:text;default:'';" json:"args"` // 相关参数
|
||||
Status int32 `gorm:"default:0;index;" json:"status"` // 订单状态 1:未支付 2:已支付 3:已发货 4:已收货 5:已完成 6:已收货退款 7:未发货退款 8:已退货 -1:已取消
|
||||
AddressIdentity string `gorm:"column:address_identity;type:varchar(36);"` // 地址库唯一标识,24位NanoID,36位为UUID
|
||||
County string `gorm:"column:county;type:varchar(255);default:'';" json:"county"` // 国家
|
||||
Province string `gorm:"column:province;type:varchar(50);default:'';" json:"province"` // 省
|
||||
City string `gorm:"column:city;type:varchar(255);default:'';" json:"city"` // 市
|
||||
Area string `gorm:"column:area;type:varchar(255);default:'';" json:"area"` // 县
|
||||
Address string `gorm:"column:address;type:varchar(255);default:'';" json:"address"` // 详细地址
|
||||
Contact string `gorm:"column:contact;type:varchar(50);default:'';" json:"contact"` // 联系人
|
||||
Phone string `gorm:"column:phone;type:varchar(50);default:'';" json:"phone"` // 联系电话
|
||||
Approve int8 `gorm:"column:approve;default:0;" json:"approve"` // 状态:-2:未通过,0:默认 1:申请退款 2:申请退货 3:申请退款退货 4:申请通过
|
||||
Reason string `gorm:"column:reason;type:varchar(255);default:'';" json:"reason"` //退货原因
|
||||
|
||||
// 配送信息
|
||||
LogisticsNumber string `gorm:"column:logistics_number;type:varchar(64);default:'';" json:"logistics_number"` // 物流号
|
||||
DeliveryTime string `gorm:"column:delivery_time;type:varchar(255);" json:"delivery_time"` // 配送时间
|
||||
DeliveryAddress string `gorm:"column:delivery_address;type:varchar(255);" json:"delivery_address"` // 配送地址
|
||||
DeliveryIdentity string `gorm:"column:delivery_identity;type:varchar(36);" json:"delivery_identity"` // 配送工作人员唯一标识,24位NanoID,36位为UUID
|
||||
CarIdentity string `gorm:"column:car_identity;type:varchar(36);" json:"car_identity"` // 配送车辆唯一标识,24位NanoID,36位为UUID
|
||||
CarBrand string `gorm:"column:car_brand;type:varchar(255);default:'';" json:"car_brand"` // 配送车辆品牌
|
||||
CarVersion string `gorm:"column:car_version;type:varchar(255);default:'';" json:"car_version"` // 配送车辆型号
|
||||
LicenseNumber string `gorm:"column:license_number;type:varchar(255);default:'';" json:"license_number"` // 配送车辆车牌号
|
||||
MemberName string `gorm:"column:member_name;type:varchar(255);default:'';" json:"member_name"` // 配送人员名称
|
||||
MemberPhone string `gorm:"column:member_phone;type:varchar(255);default:'';" json:"member_phone"` // 配送人员电话
|
||||
|
||||
// 支付信息
|
||||
PayType int8 `gorm:"column:pay_type;default:0;" json:"pay_type"` // 支付类型:0:待支付,1:线下支付,2:微信 3:支付宝,4:余额
|
||||
PayAmount int64 `gorm:"column:pay_amount;default:0;" json:"pay_amount"` // 支付金额
|
||||
PayTradeNo string `gorm:"column:pay_trade_no;type:varchar(36);default:'';" json:"pay_trade_no"` // 支付交易号
|
||||
PayTime time.Time `gorm:"column:pay_time;" json:"pay_time"` // 支付时间
|
||||
PayRemark string `gorm:"column:pay_remark;type:text;default:'';" json:"pay_remark"` // 支付备注
|
||||
|
||||
OrderDetails []OrderDetails `gorm:"foreignKey:SummaryIdentity;references:Identity;" json:"details"` // 订单详情
|
||||
}
|
||||
|
||||
// TableName .
|
||||
func (table *OrderSummary) TableName() string {
|
||||
return "order_summary" //对应数据库表名
|
||||
}
|
||||
|
||||
// GetSummaryCnt 获取各类型订单数量
|
||||
func GetSummaryCnt(identity, types string) ([]*SummaryCnt, error) {
|
||||
var data = make([]*SummaryCnt, 0)
|
||||
tx := DBService.Model(&OrderSummary{}).Select("status, count(status) cnt").Where("type = 1")
|
||||
if types == "buy" {
|
||||
tx = tx.Where("buyer_identity = ?", identity)
|
||||
} else if types == "sell" {
|
||||
tx = tx.Where("merchant_identity = ?", identity)
|
||||
}
|
||||
err := tx.Group("status").Find(&data).Error
|
||||
return data, err
|
||||
}
|
||||
|
||||
// GetSummaryList 获取订单列表
|
||||
func GetSummaryList(types, org, orderNo, productName, startTime, endTime string, page, size, status, minPrice, maxPrice, payStatus, logisticsStatus, invoiceStatus int) ([]*OrderSummary, int64, error) {
|
||||
var orderList = make([]*OrderSummary, 0)
|
||||
var cnt int64 = 0
|
||||
var tx = DBService.Model(&OrderSummary{}).Preload("OrderDetails").Where("type = 1")
|
||||
|
||||
if productName != "" {
|
||||
var idList = []string{} // 获取包含title的数据
|
||||
err := DBService.Model(&OrderDetails{}).Select("summary_identity").Where("title like ?", "%"+productName+"%").Find(&idList).Error
|
||||
if err != nil {
|
||||
return nil, 0, err
|
||||
}
|
||||
tx = tx.Where("identity in ?", idList)
|
||||
}
|
||||
if types == "buy" {
|
||||
// 采购订单
|
||||
tx = tx.Where("buyer_identity = ?", org)
|
||||
} else if types == "sell" {
|
||||
// 销售订单
|
||||
tx = tx.Where("merchant_identity = ?", org)
|
||||
}
|
||||
if orderNo != "" {
|
||||
tx = tx.Where("order_no = ?", orderNo)
|
||||
}
|
||||
if startTime != "" {
|
||||
// 按时间查询
|
||||
if endTime == "" {
|
||||
tx = tx.Where("created_at > ?", startTime)
|
||||
} else {
|
||||
tx = tx.Where("created_at between ? and ?", startTime, endTime)
|
||||
}
|
||||
} else {
|
||||
if endTime != "" {
|
||||
tx = tx.Where("created_at < ?", endTime)
|
||||
}
|
||||
}
|
||||
if minPrice != 0 {
|
||||
// 按价格查询
|
||||
if maxPrice == 0 {
|
||||
tx = tx.Where("trans_price > ?", minPrice)
|
||||
} else {
|
||||
tx = tx.Where("trans_price between ? and ?", minPrice, maxPrice)
|
||||
}
|
||||
} else {
|
||||
if maxPrice != 0 {
|
||||
tx = tx.Where("trans_price < ?", maxPrice)
|
||||
}
|
||||
}
|
||||
if payStatus != 0 {
|
||||
tx = tx.Where("pay_status = ?", maxPrice)
|
||||
|
||||
}
|
||||
if logisticsStatus != 0 {
|
||||
tx = tx.Where("logistics_status = ?", logisticsStatus)
|
||||
}
|
||||
if invoiceStatus != 0 {
|
||||
tx = tx.Where("invoice_status = ?", invoiceStatus)
|
||||
}
|
||||
if status != 0 {
|
||||
if status == 2 {
|
||||
tx = tx.Where("sign_status in ?", []int64{1, 2, 3})
|
||||
}
|
||||
tx = tx.Where("status = ?", status)
|
||||
}
|
||||
err := tx.Order("created_at desc").Count(&cnt).Limit(size).Offset((page - 1) * size).Find(&orderList).Error
|
||||
if err != nil {
|
||||
return nil, 0, err
|
||||
}
|
||||
return orderList, cnt, nil
|
||||
}
|
||||
41
apps/ec/order/internal/models/query.go
Normal file
41
apps/ec/order/internal/models/query.go
Normal file
@@ -0,0 +1,41 @@
|
||||
package models
|
||||
|
||||
type Product struct {
|
||||
Id int64 `json:"id"` // 商品ID
|
||||
Identity string `json:"identity"` // 商品唯一标识
|
||||
Title string `json:"title"` // 商品标题
|
||||
CoverImage string `json:"cover_image"` // 商品封面图
|
||||
SalesPrice int64 `json:"sales_price"` // 销售价格
|
||||
CostPrice int64 `json:"cost_price"` // 成本价格
|
||||
Args string `json:"args"` // 商品参数
|
||||
StoreId uint `json:"store_id"` // 门店ID
|
||||
StoreIdentity string `json:"store_identity"` // 门店标识
|
||||
GasTypes int64 `json:"gas_types"` // 配送方式
|
||||
SupplyId int64 `json:"supply_id"` // 供应商ID
|
||||
}
|
||||
type Spec struct {
|
||||
Title string `json:"title"`
|
||||
SerialNumber string `json:"serial_number"`
|
||||
Price int64 `json:"price"`
|
||||
}
|
||||
|
||||
func QuicklyCreateOrder(identity string, order *OrderSummary, detail []*OrderDetails) error {
|
||||
tx := DBService.Begin()
|
||||
defer func() {
|
||||
if r := recover(); r != nil {
|
||||
tx.Rollback()
|
||||
}
|
||||
}()
|
||||
if err := tx.Create(&order).Error; err != nil {
|
||||
tx.Rollback()
|
||||
return err
|
||||
}
|
||||
if len(detail) > 0 {
|
||||
if err := tx.Create(&detail).Error; err != nil {
|
||||
tx.Rollback()
|
||||
return err
|
||||
}
|
||||
}
|
||||
|
||||
return tx.Commit().Error
|
||||
}
|
||||
55
apps/ec/order/internal/models/types.go
Normal file
55
apps/ec/order/internal/models/types.go
Normal file
@@ -0,0 +1,55 @@
|
||||
package models
|
||||
|
||||
import "gorm.io/gorm"
|
||||
|
||||
var ErrNotFound = gorm.ErrRecordNotFound
|
||||
|
||||
type OrderAddress struct {
|
||||
Country string `json:"country"` // 国家
|
||||
Province string `json:"province"` // 省份
|
||||
City string `json:"city"` // 城市
|
||||
Area string `json:"area"` // 地区
|
||||
Detail string `json:"detail"` // 详细地址
|
||||
Contact string `json:"contact"` // 联系人
|
||||
Phone string `json:"phone"` // 手机号
|
||||
Email string `json:"email"` // 邮箱
|
||||
ZipCode string `json:"zip_code"` // 邮编
|
||||
CompanyName string `json:"company_name"` // 公司名称
|
||||
}
|
||||
type SummaryCnt struct {
|
||||
Status int64 `json:"status"`
|
||||
Cnt int64 `json:"cnt"`
|
||||
}
|
||||
type ListWithBuyerModel struct {
|
||||
CoverImage string `json:"cover_image"`
|
||||
Title string `json:"title"`
|
||||
Price int64 `json:"price"`
|
||||
PurchaseQuantity int64 `json:"purchase_quantity"`
|
||||
Province string `json:"province"`
|
||||
City string `json:"city"`
|
||||
Region string `json:"region"`
|
||||
Address string `json:"address"`
|
||||
Contacts string `json:"contacts"`
|
||||
Phone string `json:"phone"`
|
||||
SignTimeLimit int64 `json:"sign_time_limit"`
|
||||
PayTimeLimit int64 `json:"pay_time_limit"`
|
||||
ExpirationTime string `json:"expiration_time"`
|
||||
ReleaseTime string `json:"release_time"`
|
||||
PickupType int64 `json:"pickup_type"`
|
||||
|
||||
Id int64 `json:"id"`
|
||||
Identity string `json:"identity"`
|
||||
CreatedTime string `json:"created_at"`
|
||||
SerialNumber string `json:"serial_number"`
|
||||
Seller string `json:"seller"`
|
||||
Buyer string `json:"buyer"`
|
||||
Status int64 `json:"status"`
|
||||
|
||||
OsId int64 `json:"os_id"`
|
||||
OsIdentity string `json:"os_identity"`
|
||||
OsCreatedTime string `json:"os_created_at"`
|
||||
OsSerialNumber string `json:"os_serial_number"`
|
||||
OsSeller string `json:"os_seller"`
|
||||
OsBuyer string `json:"os_buyer"`
|
||||
OsStatus int64 `json:"os_status"`
|
||||
}
|
||||
Reference in New Issue
Block a user