refactor: reorganize modules and add Linux build tooling
This commit is contained in:
41
module/ec/order/internal/config/config.go
Normal file
41
module/ec/order/internal/config/config.go
Normal file
@@ -0,0 +1,41 @@
|
||||
package config
|
||||
|
||||
import (
|
||||
"net"
|
||||
|
||||
"git.apinb.com/bsm-sdk/core/conf"
|
||||
"git.apinb.com/bsm-sdk/core/crypto/encipher"
|
||||
"git.apinb.com/bsm-sdk/core/env"
|
||||
)
|
||||
|
||||
var (
|
||||
Spec SrvConfig
|
||||
)
|
||||
|
||||
type SrvConfig struct {
|
||||
conf.Base `yaml:",inline"`
|
||||
Databases *conf.DBConf `yaml:"Databases"`
|
||||
MicroService *conf.MicroServiceConf `yaml:"MicroService"`
|
||||
Rpc map[string]conf.RpcConf `yaml:"Rpc"`
|
||||
Gateway *conf.GatewayConf `yaml:"Gateway"`
|
||||
Apm *conf.ApmConf `yaml:"APM"`
|
||||
Etcd *conf.EtcdConf `yaml:"Etcd"`
|
||||
}
|
||||
|
||||
func New(srvKey string) {
|
||||
// 初始化配置 创建一个新的配置实例,用于服务配置
|
||||
conf.New(srvKey, &Spec)
|
||||
|
||||
// 配置校验 服务IP,端口; 端口如果不合规,则随机分配端口
|
||||
Spec.Port = conf.CheckPort(Spec.Port)
|
||||
Spec.BindIP = conf.CheckIP(Spec.BindIP)
|
||||
Spec.Addr = net.JoinHostPort(Spec.BindIP, Spec.Port)
|
||||
|
||||
// 配置校验 服务名称地址及监听地址不能为空
|
||||
conf.NotNil(Spec.Service, Spec.Cache)
|
||||
|
||||
// 初始化加密SecretKey
|
||||
encipher.New(env.Runtime.JwtSecretKey)
|
||||
|
||||
conf.PrintInfo(Spec.Addr)
|
||||
}
|
||||
12
module/ec/order/internal/impl/impl.go
Normal file
12
module/ec/order/internal/impl/impl.go
Normal file
@@ -0,0 +1,12 @@
|
||||
package impl
|
||||
|
||||
import (
|
||||
"git.apinb.com/bsm-sdk/core/vars"
|
||||
)
|
||||
|
||||
func NewImpl() {
|
||||
// with activating
|
||||
withRedisCache(vars.ServiceKey) // redis cache
|
||||
withDatabases() // model
|
||||
withEtcd() // etcd
|
||||
}
|
||||
84
module/ec/order/internal/impl/with.go
Normal file
84
module/ec/order/internal/impl/with.go
Normal file
@@ -0,0 +1,84 @@
|
||||
package impl
|
||||
|
||||
import (
|
||||
"time"
|
||||
|
||||
"bsm/full/module/ec/order/internal/config"
|
||||
"bsm/full/module/ec/order/internal/models"
|
||||
"git.apinb.com/bsm-sdk/core/cache/redis"
|
||||
"git.apinb.com/bsm-sdk/core/errcode"
|
||||
"git.apinb.com/bsm-sdk/core/printer"
|
||||
"git.apinb.com/bsm-sdk/core/vars"
|
||||
"go.etcd.io/etcd/client/pkg/v3/transport"
|
||||
clientv3 "go.etcd.io/etcd/client/v3"
|
||||
)
|
||||
|
||||
var (
|
||||
RedisCache *redis.RedisClient
|
||||
Etcd *clientv3.Client
|
||||
)
|
||||
|
||||
func withRedisCache(srvKey string) {
|
||||
if config.Spec.Cache != "" {
|
||||
RedisCache = redis.New(config.Spec.Cache, srvKey)
|
||||
}
|
||||
|
||||
// print inform.
|
||||
printer.Info("[BSM - %s] Cache: %s, DBIndex: %d", vars.ServiceKey, config.Spec.Cache, RedisCache.DB)
|
||||
}
|
||||
|
||||
func withDatabases() {
|
||||
if config.Spec.Databases == nil || len(config.Spec.Databases.Source) == 0 {
|
||||
panic("No Database Source Found !")
|
||||
}
|
||||
|
||||
// print inform.
|
||||
printer.Info("[BSM - %s] Databases: %v", vars.ServiceKey, config.Spec.Databases)
|
||||
|
||||
err := models.New(config.Spec.Databases.Driver, config.Spec.Databases.Source, nil)
|
||||
if err != nil {
|
||||
printer.Error("Database Init Failed !")
|
||||
panic(err)
|
||||
}
|
||||
|
||||
}
|
||||
|
||||
func withEtcd() {
|
||||
if config.Spec.Etcd != nil {
|
||||
if len(config.Spec.Etcd.Endpoints) == 0 {
|
||||
panic(errcode.ErrNotFound(0, "Etcd Endpoints"))
|
||||
}
|
||||
cfg := clientv3.Config{
|
||||
Endpoints: config.Spec.Etcd.Endpoints,
|
||||
DialTimeout: 5 * time.Second,
|
||||
}
|
||||
if config.Spec.Etcd.Passwd != nil {
|
||||
cfg.Username = config.Spec.Etcd.Passwd.Account
|
||||
cfg.Password = config.Spec.Etcd.Passwd.Password
|
||||
}
|
||||
if config.Spec.Etcd.TLS != nil {
|
||||
tlsInfo := transport.TLSInfo{
|
||||
TrustedCAFile: config.Spec.Etcd.TLS.CaFile,
|
||||
CertFile: config.Spec.Etcd.TLS.CertFile,
|
||||
KeyFile: config.Spec.Etcd.TLS.KeyFile,
|
||||
}
|
||||
tlsConfig, err := tlsInfo.ClientConfig()
|
||||
if err != nil {
|
||||
printer.Error(errcode.ErrEtcd.Error())
|
||||
panic(err)
|
||||
}
|
||||
cfg.TLS = tlsConfig
|
||||
}
|
||||
etcd, err := clientv3.New(cfg)
|
||||
|
||||
if err != nil {
|
||||
printer.Error(errcode.ErrEtcd.Error())
|
||||
panic(err)
|
||||
}
|
||||
|
||||
Etcd = etcd
|
||||
|
||||
// print inform.
|
||||
printer.Info("[BSM - %s] Service Center: %v", vars.ServiceKey, config.Spec.Etcd.Endpoints)
|
||||
}
|
||||
}
|
||||
62
module/ec/order/internal/logic/cart/create.go
Normal file
62
module/ec/order/internal/logic/cart/create.go
Normal file
@@ -0,0 +1,62 @@
|
||||
package cart
|
||||
|
||||
import (
|
||||
"context"
|
||||
"errors"
|
||||
"time"
|
||||
|
||||
"bsm/full/module/ec/order/internal/models"
|
||||
pb "bsm/full/module/ec/order/pb"
|
||||
"git.apinb.com/bsm-sdk/core/errcode"
|
||||
"git.apinb.com/bsm-sdk/core/printer"
|
||||
"git.apinb.com/bsm-sdk/core/service"
|
||||
"git.apinb.com/bsm-sdk/core/utils"
|
||||
"gorm.io/gorm"
|
||||
)
|
||||
|
||||
// 将商品增加至购物车
|
||||
func Create(ctx context.Context, in *pb.CartAddRequest) (reply *pb.StatusReply, err error) {
|
||||
auth, err := service.ParseMetaCtx(ctx, nil)
|
||||
if err != nil {
|
||||
return nil, err
|
||||
}
|
||||
var cart *models.OrderCart
|
||||
|
||||
err = models.DBService.Where("cart_identity=? and product_identity=? and spec_id = ? ", in.CartIdentity, in.ProductIdentity, in.SpecId).First(&cart).Error
|
||||
if err != nil {
|
||||
if !errors.Is(err, gorm.ErrRecordNotFound) {
|
||||
printer.Error(err.Error())
|
||||
return nil, errcode.ErrDB
|
||||
}
|
||||
}
|
||||
|
||||
var data = models.OrderCart{
|
||||
CartIdentity: in.CartIdentity,
|
||||
ProductID: in.ProductId,
|
||||
ProductArgs: in.ProductArgs,
|
||||
Number: in.Number,
|
||||
ProductIdentity: in.ProductIdentity,
|
||||
SpecID: in.SpecId,
|
||||
}
|
||||
data.PassportIdentity = auth.Identity
|
||||
data.Identity = utils.UUID()
|
||||
if cart.ID == 0 {
|
||||
err = models.DBService.Create(&data).Error
|
||||
} else {
|
||||
data.Number += cart.Number
|
||||
err = models.DBService.Where("cart_identity=? and product_identity=? and spec_id = ? ", in.CartIdentity, in.ProductIdentity, in.SpecId).Updates(&data).Error
|
||||
}
|
||||
|
||||
if err != nil {
|
||||
printer.Error(err.Error())
|
||||
return nil, errcode.ErrDB
|
||||
}
|
||||
|
||||
return &pb.StatusReply{
|
||||
Code: 0,
|
||||
Identity: data.Identity,
|
||||
Message: "OK",
|
||||
Timeseq: time.Now().UnixNano(),
|
||||
}, nil
|
||||
|
||||
}
|
||||
31
module/ec/order/internal/logic/cart/delete.go
Normal file
31
module/ec/order/internal/logic/cart/delete.go
Normal file
@@ -0,0 +1,31 @@
|
||||
package cart
|
||||
|
||||
import (
|
||||
"context"
|
||||
"time"
|
||||
|
||||
"bsm/full/module/ec/order/internal/models"
|
||||
pb "bsm/full/module/ec/order/pb"
|
||||
"git.apinb.com/bsm-sdk/core/service"
|
||||
)
|
||||
|
||||
// 删除购物车中的商品
|
||||
func Delete(ctx context.Context, in *pb.CartDelRequest) (reply *pb.StatusReply, err error) {
|
||||
// parse authorization meta.
|
||||
_, err = service.ParseMetaCtx(ctx, nil)
|
||||
if err != nil {
|
||||
return nil, err
|
||||
}
|
||||
|
||||
err = models.DBService.Where("id in ?", in.Id).Delete(&models.OrderCart{}).Error
|
||||
if err != nil {
|
||||
return nil, err
|
||||
}
|
||||
|
||||
return &pb.StatusReply{
|
||||
Code: 0,
|
||||
Message: "OK",
|
||||
Timeseq: time.Now().UnixNano(),
|
||||
}, nil
|
||||
|
||||
}
|
||||
69
module/ec/order/internal/logic/cart/fetch.go
Normal file
69
module/ec/order/internal/logic/cart/fetch.go
Normal file
@@ -0,0 +1,69 @@
|
||||
package cart
|
||||
|
||||
import (
|
||||
"context"
|
||||
|
||||
"bsm/full/module/ec/order/internal/models"
|
||||
pb "bsm/full/module/ec/order/pb"
|
||||
"git.apinb.com/bsm-sdk/core/errcode"
|
||||
"git.apinb.com/bsm-sdk/core/printer"
|
||||
"git.apinb.com/bsm-sdk/core/service"
|
||||
)
|
||||
|
||||
// 获取购物车的商品数据
|
||||
func Fetch(ctx context.Context, in *pb.CartGetRequest) (reply *pb.CartGetReply, err error) {
|
||||
auth, err := service.ParseMetaCtx(ctx, nil)
|
||||
if err != nil {
|
||||
return nil, err
|
||||
}
|
||||
var (
|
||||
result = make([]*pb.CartItem, 0)
|
||||
cart = make([]*models.OrderCart, 0)
|
||||
)
|
||||
if in.GetPassport() == "" {
|
||||
if in.GetCarIdentity() == "" && in.GetPassportIdentity() == "" {
|
||||
return nil, errcode.ErrInvalidArgument
|
||||
}
|
||||
}
|
||||
// 查询购物车数据
|
||||
err = models.DBService.Where("cart_identity=? or passport_identity=?", in.GetCarIdentity(), auth.Identity).Find(&cart).Error
|
||||
|
||||
if err != nil {
|
||||
printer.Error(err.Error())
|
||||
return nil, errcode.ErrDB
|
||||
}
|
||||
|
||||
for _, item := range cart {
|
||||
product := models.Product{}
|
||||
err := models.DBService.Raw(`
|
||||
SELECT
|
||||
p.id, p.identity, p.title, p.cover_image, p.args, p.cost_price,
|
||||
( SELECT MIN(mps.price)
|
||||
FROM product_spec ps
|
||||
JOIN mall_product_spec mps ON ps.spec_id = mps.id
|
||||
WHERE ps.product_identity = p.identity
|
||||
) as sales_price FROM mall_product p WHERE p.identity = ?`, item.ProductIdentity).Scan(&product).Error
|
||||
if err == nil {
|
||||
da := &pb.CartItem{
|
||||
Id: int64(item.ID),
|
||||
ProductId: product.Id,
|
||||
Title: product.Title,
|
||||
ProductIdentity: product.Identity,
|
||||
CoverImage: product.CoverImage,
|
||||
SalesPrice: product.SalesPrice,
|
||||
ProductArgs: item.ProductArgs,
|
||||
UnitPrice: product.CostPrice,
|
||||
TotalPrice: int64(item.Number) * product.SalesPrice,
|
||||
Number: item.Number,
|
||||
Identity: item.Identity,
|
||||
}
|
||||
result = append(result, da)
|
||||
|
||||
}
|
||||
}
|
||||
|
||||
return &pb.CartGetReply{
|
||||
Data: result,
|
||||
CarIdentity: in.GetCarIdentity(),
|
||||
}, nil
|
||||
}
|
||||
52
module/ec/order/internal/logic/cart/modify.go
Normal file
52
module/ec/order/internal/logic/cart/modify.go
Normal file
@@ -0,0 +1,52 @@
|
||||
package cart
|
||||
|
||||
import (
|
||||
"context"
|
||||
"time"
|
||||
|
||||
"bsm/full/module/ec/order/internal/models"
|
||||
pb "bsm/full/module/ec/order/pb"
|
||||
"git.apinb.com/bsm-sdk/core/errcode"
|
||||
"git.apinb.com/bsm-sdk/core/printer"
|
||||
"git.apinb.com/bsm-sdk/core/service"
|
||||
"gorm.io/gorm"
|
||||
)
|
||||
|
||||
// 修改购物车中的商品数量
|
||||
func Modify(ctx context.Context, in *pb.CartSetRequest) (reply *pb.StatusReply, err error) {
|
||||
// parse authorization meta.
|
||||
_, err = service.ParseMetaCtx(ctx, nil)
|
||||
if err != nil {
|
||||
return nil, err
|
||||
}
|
||||
if len(in.GetUpdates()) > 0 {
|
||||
err = models.DBService.Transaction(func(tx *gorm.DB) error {
|
||||
for _, update := range in.Updates {
|
||||
err := tx.Table("order_cart").Where("id = ?", update.Id).Update("number", update.Number).Error
|
||||
if err != nil {
|
||||
printer.Error(err.Error())
|
||||
return err
|
||||
}
|
||||
}
|
||||
return nil
|
||||
})
|
||||
|
||||
} else {
|
||||
if in.GetNumber() == 0 {
|
||||
return nil, errcode.ErrInvalidArgument
|
||||
}
|
||||
|
||||
err = models.DBService.Table("order_cart").Where("id = ?", in.Id).Update("number", in.Number).Error
|
||||
if err != nil {
|
||||
printer.Error(err.Error())
|
||||
return nil, errcode.ErrDB
|
||||
}
|
||||
}
|
||||
|
||||
return &pb.StatusReply{
|
||||
Code: 0,
|
||||
Message: "OK",
|
||||
Timeseq: time.Now().UnixNano(),
|
||||
}, nil
|
||||
|
||||
}
|
||||
33
module/ec/order/internal/logic/common/get.go
Normal file
33
module/ec/order/internal/logic/common/get.go
Normal file
@@ -0,0 +1,33 @@
|
||||
package common
|
||||
|
||||
import (
|
||||
"bsm/full/module/ec/order/internal/models"
|
||||
pb "bsm/full/module/ec/order/pb"
|
||||
)
|
||||
|
||||
// GetOrderSummaryByIdentity 根据订单号获取订单详情
|
||||
func GetOrderSummaryByIdentity(identity string) (*pb.OrderSummaryItem, error) {
|
||||
var (
|
||||
err error
|
||||
summary = new(models.OrderSummary)
|
||||
)
|
||||
err = models.DBService.Preload("OrderDetails").Where("identity = ?", identity).First(&summary).Error
|
||||
if err != nil {
|
||||
return nil, err
|
||||
}
|
||||
|
||||
return ReflectProtoOrderSummary(summary), nil
|
||||
}
|
||||
|
||||
func GetOrderSummaryByNo(no string) (*pb.OrderSummaryItem, error) {
|
||||
var (
|
||||
err error
|
||||
summary = new(models.OrderSummary)
|
||||
)
|
||||
err = models.DBService.Preload("OrderDetails").Where("order_no = ?", no).First(&summary).Error
|
||||
if err != nil {
|
||||
return nil, err
|
||||
}
|
||||
|
||||
return ReflectProtoOrderSummary(summary), nil
|
||||
}
|
||||
14
module/ec/order/internal/logic/common/no.go
Normal file
14
module/ec/order/internal/logic/common/no.go
Normal file
@@ -0,0 +1,14 @@
|
||||
package common
|
||||
|
||||
import (
|
||||
"fmt"
|
||||
"math/rand"
|
||||
"time"
|
||||
)
|
||||
|
||||
// 订单号:2位年份,月,日,时,分,秒,6位随机数共18位
|
||||
func CreateOrderNo() string {
|
||||
t := time.Now().Local()
|
||||
rand := fmt.Sprintf("%06v", rand.New(rand.NewSource(time.Now().UnixNano())).Int31n(1000000))
|
||||
return fmt.Sprintf("%d%d%d%d%d%d%s", t.Year()-2000, t.Month(), t.Day(), t.Hour(), t.Minute(), t.Second(), rand)
|
||||
}
|
||||
100
module/ec/order/internal/logic/common/reflect.go
Normal file
100
module/ec/order/internal/logic/common/reflect.go
Normal file
@@ -0,0 +1,100 @@
|
||||
package common
|
||||
|
||||
import (
|
||||
"bsm/full/module/ec/order/internal/models"
|
||||
order "bsm/full/module/ec/order/pb"
|
||||
pb "bsm/full/module/ec/order/pb"
|
||||
"git.apinb.com/bsm-sdk/core/vars"
|
||||
)
|
||||
|
||||
// ReflectProtoOrderDetails 订单详情
|
||||
func ReflectProtoOrderDetails(v []models.OrderDetails) []*pb.OrderDetails {
|
||||
if nil == v {
|
||||
return nil
|
||||
}
|
||||
result := make([]*pb.OrderDetails, 0)
|
||||
for _, val := range v {
|
||||
result = append(result, &pb.OrderDetails{
|
||||
Id: int64(val.ID),
|
||||
ProductId: val.ProductID,
|
||||
SpecNo: val.SpecNo,
|
||||
SpecId: val.SpecID,
|
||||
Type: int64(val.Type),
|
||||
Title: val.Title,
|
||||
CoverImage: val.CoverImage,
|
||||
SalesPrice: val.SalesPrice,
|
||||
ProductArgs: val.ProductArgs,
|
||||
Number: int64(val.Number),
|
||||
UnitPrice: val.UnitPrice,
|
||||
ProductIdentity: val.ProductIdentity,
|
||||
GasTypes: int64(val.GasType),
|
||||
Spec: val.SpecTitle,
|
||||
TotalPrice: int64(val.Number) * val.UnitPrice,
|
||||
})
|
||||
}
|
||||
|
||||
return result
|
||||
}
|
||||
|
||||
func ReflectProtoOrderSummary(summary *models.OrderSummary) *pb.OrderSummaryItem {
|
||||
if summary == nil {
|
||||
return nil
|
||||
}
|
||||
item := pb.OrderSummaryItem{
|
||||
Id: int64(summary.ID),
|
||||
Identity: summary.Identity,
|
||||
OrderNo: summary.OrderNo,
|
||||
PartnerId: int64(summary.PartnerID),
|
||||
TotalPrice: summary.TotalPrice,
|
||||
TransPrice: summary.TransPrice,
|
||||
RefundPrice: summary.RefundPrice,
|
||||
LogisticsFee: summary.LogisticsFee,
|
||||
CouponAmount: summary.CouponAmount,
|
||||
Remark: summary.Remark,
|
||||
Status: summary.Status,
|
||||
LogisticsNumber: summary.LogisticsNumber,
|
||||
AddressIdentity: summary.AddressIdentity,
|
||||
County: summary.County,
|
||||
Province: summary.Province,
|
||||
City: summary.City,
|
||||
Area: summary.Area,
|
||||
Address: summary.Province + summary.City + summary.Area + summary.Address,
|
||||
Contact: summary.Contact,
|
||||
Phone: summary.Phone,
|
||||
DeliveryTime: summary.DeliveryTime,
|
||||
DeliveryIdentity: summary.DeliveryIdentity,
|
||||
PayType: int32(summary.PayType),
|
||||
PayAmount: summary.PayAmount,
|
||||
PayTradeNo: summary.PayTradeNo,
|
||||
PayTime: summary.PayTime.Format(vars.YYYY_MM_DD_HH_MM_SS),
|
||||
PayRemark: summary.PayRemark,
|
||||
Created: summary.CreatedAt.Format(vars.YYYY_MM_DD_HH_MM_SS),
|
||||
Updated: summary.UpdatedAt.Format(vars.YYYY_MM_DD_HH_MM_SS),
|
||||
Addr: summary.Address,
|
||||
CarIdentity: summary.CarIdentity,
|
||||
DeliveryAddress: summary.DeliveryAddress,
|
||||
Brand: summary.CarBrand,
|
||||
Version: summary.CarVersion,
|
||||
LicenseNumber: summary.LicenseNumber,
|
||||
MemberName: summary.MemberName,
|
||||
MemberPhone: summary.MemberPhone,
|
||||
Details: make([]*order.OrderDetails, 0),
|
||||
Approve: int32(summary.Approve),
|
||||
}
|
||||
if summary.OrderDetails != nil {
|
||||
item.Details = ReflectProtoOrderDetails(summary.OrderDetails)
|
||||
}
|
||||
if summary.PayTime.IsZero() {
|
||||
item.PayTime = ""
|
||||
}
|
||||
|
||||
return &item
|
||||
}
|
||||
|
||||
func ListModelToReply(list []*models.OrderSummary) []*pb.OrderSummaryItem {
|
||||
var data = make([]*order.OrderSummaryItem, 0)
|
||||
for _, summary := range list {
|
||||
data = append(data, ReflectProtoOrderSummary(summary))
|
||||
}
|
||||
return data
|
||||
}
|
||||
53
module/ec/order/internal/logic/coupon/by_status.go
Normal file
53
module/ec/order/internal/logic/coupon/by_status.go
Normal file
@@ -0,0 +1,53 @@
|
||||
package coupon
|
||||
|
||||
import (
|
||||
"context"
|
||||
|
||||
"bsm/full/module/ec/order/internal/models"
|
||||
pb "bsm/full/module/ec/order/pb"
|
||||
"git.apinb.com/bsm-sdk/core/errcode"
|
||||
"git.apinb.com/bsm-sdk/core/printer"
|
||||
"git.apinb.com/bsm-sdk/core/service"
|
||||
)
|
||||
|
||||
// 按状态获取优惠卷
|
||||
func ByStatus(ctx context.Context, in *pb.Status) (reply *pb.CouponListReply, err error) {
|
||||
// parse authorization meta.
|
||||
auth, err := service.ParseMetaCtx(ctx, &service.ParseOptions{RoleValue: "Mall_Admin"})
|
||||
if err != nil {
|
||||
return nil, err
|
||||
}
|
||||
|
||||
var (
|
||||
coupon = make([]*models.OrderCoupon, 0)
|
||||
result = make([]*pb.CouponItem, 0)
|
||||
)
|
||||
|
||||
err = models.DBService.Where("passport_id=?", auth.ID).Find(&coupon).Error
|
||||
if err != nil {
|
||||
printer.Error(err.Error())
|
||||
return nil, errcode.ErrDB
|
||||
}
|
||||
|
||||
for _, item := range coupon {
|
||||
result = append(result, ReflectProtoOrderCoupon(item))
|
||||
}
|
||||
|
||||
return &pb.CouponListReply{
|
||||
Data: result,
|
||||
}, nil
|
||||
|
||||
}
|
||||
func ReflectProtoOrderCoupon(v *models.OrderCoupon) *pb.CouponItem {
|
||||
if nil == v {
|
||||
return nil
|
||||
}
|
||||
result := &pb.CouponItem{
|
||||
Id: int64(v.ID),
|
||||
Identity: v.Identity,
|
||||
Title: v.Title,
|
||||
Intro: v.Intro,
|
||||
Amount: v.Intro,
|
||||
}
|
||||
return result
|
||||
}
|
||||
99
module/ec/order/internal/logic/mgt/order_approve.go
Normal file
99
module/ec/order/internal/logic/mgt/order_approve.go
Normal file
@@ -0,0 +1,99 @@
|
||||
package mgt
|
||||
|
||||
import (
|
||||
"context"
|
||||
"time"
|
||||
|
||||
"bsm/full/module/ec/order/internal/models"
|
||||
pb "bsm/full/module/ec/order/pb"
|
||||
"git.apinb.com/bsm-sdk/core/errcode"
|
||||
"git.apinb.com/bsm-sdk/core/printer"
|
||||
"gorm.io/gorm"
|
||||
)
|
||||
|
||||
// 订单审批
|
||||
func OrderApprove(ctx context.Context, in *pb.IdentRequest) (reply *pb.StatusReply, err error) {
|
||||
// 验证输入参数是否有效。
|
||||
if in.GetIdentity() == "" {
|
||||
return nil, errcode.ErrInvalidArgument
|
||||
}
|
||||
// 获取当前订单信息。
|
||||
var order models.OrderSummary
|
||||
if err := models.DBService.Preload("OrderDetails").Where("identity=?", in.Identity).First(&order).Error; err != nil {
|
||||
printer.Error(err.Error())
|
||||
return nil, errcode.ErrDB
|
||||
}
|
||||
|
||||
// 根据订单审批状态更新订单状态:状态:-2:未通过,0:默认 1:申请退款 2:申请退货 3:申请退款退货 4:申请通过
|
||||
switch order.Approve {
|
||||
// 申请退款
|
||||
case 1:
|
||||
if in.GetApprove() == 4 {
|
||||
err = models.DBService.Model(&models.OrderSummary{}).Where("identity=?", in.Identity).
|
||||
Updates(map[string]any{
|
||||
"approve": in.GetApprove(),
|
||||
"status": 7,
|
||||
}).Error
|
||||
if err != nil {
|
||||
printer.Error(err.Error())
|
||||
return nil, errcode.ErrDB
|
||||
}
|
||||
}
|
||||
// 申请退货
|
||||
case 2:
|
||||
if in.GetApprove() == 4 {
|
||||
models.DBService.Transaction(func(tx *gorm.DB) error {
|
||||
err = tx.Model(&models.OrderSummary{}).Where("identity=?", in.Identity).
|
||||
Updates(map[string]any{
|
||||
"approve": in.GetApprove(),
|
||||
"status": 8,
|
||||
}).Error
|
||||
if err != nil {
|
||||
printer.Error(err.Error())
|
||||
return err
|
||||
}
|
||||
// 如果订单详情中的数量不为零,则更新产品规格的库存。
|
||||
for _, specs := range order.OrderDetails {
|
||||
if specs.Number != 0 {
|
||||
err = tx.Table("mall_product_spec").Where("product_identity = ?", specs.ProductIdentity).UpdateColumn("stock", gorm.Expr("stock + ?", specs.Number)).Error
|
||||
if err != nil {
|
||||
return err
|
||||
}
|
||||
}
|
||||
}
|
||||
return nil
|
||||
})
|
||||
}
|
||||
// 申请退款退货
|
||||
case 3:
|
||||
if in.GetApprove() == 4 {
|
||||
|
||||
err = models.DBService.Model(&models.OrderSummary{}).Where("identity=?", in.Identity).
|
||||
Updates(map[string]any{
|
||||
"approve": in.GetApprove(),
|
||||
"status": 6,
|
||||
}).Error
|
||||
if err != nil {
|
||||
printer.Error(err.Error())
|
||||
return nil, errcode.ErrDB
|
||||
}
|
||||
}
|
||||
}
|
||||
// 审批未通过
|
||||
if in.GetApprove() == -2 {
|
||||
if err := models.DBService.Model(&models.OrderSummary{}).Where("identity=?", in.Identity).
|
||||
Update("approve", in.GetApprove()).Error; err != nil {
|
||||
printer.Error(err.Error())
|
||||
return nil, errcode.ErrDB
|
||||
}
|
||||
|
||||
}
|
||||
|
||||
return &pb.StatusReply{
|
||||
Code: 0,
|
||||
Message: "OK",
|
||||
Identity: in.GetIdentity(),
|
||||
Timeseq: time.Now().UnixNano(),
|
||||
}, nil
|
||||
|
||||
}
|
||||
47
module/ec/order/internal/logic/mgt/order_cancel.go
Normal file
47
module/ec/order/internal/logic/mgt/order_cancel.go
Normal file
@@ -0,0 +1,47 @@
|
||||
package mgt
|
||||
|
||||
import (
|
||||
"context"
|
||||
"time"
|
||||
|
||||
"bsm/full/module/ec/order/internal/models"
|
||||
pb "bsm/full/module/ec/order/pb"
|
||||
"git.apinb.com/bsm-sdk/core/errcode"
|
||||
"git.apinb.com/bsm-sdk/core/printer"
|
||||
"git.apinb.com/bsm-sdk/core/service"
|
||||
)
|
||||
|
||||
// 取消订单
|
||||
func OrderCancel(ctx context.Context, in *pb.IdentRequest) (reply *pb.StatusReply, err error) {
|
||||
// parse authorization meta.
|
||||
auth, err := service.ParseMetaCtx(ctx, &service.ParseOptions{RoleValue: "Mall_Admin"})
|
||||
if err != nil {
|
||||
return nil, err
|
||||
}
|
||||
|
||||
if auth.Owner == nil {
|
||||
return nil, errcode.ErrPermissionDenied
|
||||
}
|
||||
|
||||
// valildate request id,identity.
|
||||
if in.Identity == "" {
|
||||
return nil, errcode.ErrInvalidArgument
|
||||
}
|
||||
|
||||
// 验证输入参数是否有效。
|
||||
if in.GetIdentity() == "" {
|
||||
return nil, errcode.ErrInvalidArgument
|
||||
}
|
||||
err = models.DBService.Model(&models.OrderSummary{}).Where("identity=?", in.Identity).Update("status", -1).Error
|
||||
if err != nil {
|
||||
printer.Error(err.Error())
|
||||
return nil, errcode.ErrDB
|
||||
}
|
||||
return &pb.StatusReply{
|
||||
Code: 0,
|
||||
Message: "OK",
|
||||
Identity: in.GetIdentity(),
|
||||
Timeseq: time.Now().UnixNano(),
|
||||
}, nil
|
||||
|
||||
}
|
||||
168
module/ec/order/internal/logic/mgt/order_create.go
Normal file
168
module/ec/order/internal/logic/mgt/order_create.go
Normal file
@@ -0,0 +1,168 @@
|
||||
package mgt
|
||||
|
||||
import (
|
||||
"context"
|
||||
"errors"
|
||||
"time"
|
||||
|
||||
"bsm/full/module/ec/order/internal/logic/common"
|
||||
"bsm/full/module/ec/order/internal/models"
|
||||
pb "bsm/full/module/ec/order/pb"
|
||||
"git.apinb.com/bsm-sdk/core/errcode"
|
||||
"git.apinb.com/bsm-sdk/core/printer"
|
||||
"git.apinb.com/bsm-sdk/core/service"
|
||||
"git.apinb.com/bsm-sdk/core/utils"
|
||||
"gorm.io/gorm"
|
||||
)
|
||||
|
||||
// 创建订单
|
||||
func OrderCreate(ctx context.Context, in *pb.CreateOrderRequest) (reply *pb.StatusReply, err error) {
|
||||
// parse authorization meta.
|
||||
auth, err := service.ParseMetaCtx(ctx, &service.ParseOptions{RoleValue: "Mall_Admin"})
|
||||
if err != nil {
|
||||
return nil, err
|
||||
}
|
||||
|
||||
if auth.Owner == nil {
|
||||
return nil, errcode.ErrPermissionDenied
|
||||
}
|
||||
var (
|
||||
TotalPrice int64 = 0
|
||||
summaryIdentity string = utils.UUID()
|
||||
orderNo = common.CreateOrderNo()
|
||||
details = &models.OrderDetails{}
|
||||
summary = &models.OrderSummary{}
|
||||
specList = make([]models.OrderDetails, 0)
|
||||
)
|
||||
|
||||
// 验证输入参数是否有效。
|
||||
for _, spec := range in.Spec {
|
||||
if spec.GetProductIdentity() == "" || spec.GetNumber() == 0 {
|
||||
return nil, errcode.ErrInvalidArgument
|
||||
}
|
||||
}
|
||||
if in.GetAddressIdentity() == "" {
|
||||
return nil, errcode.ErrInvalidArgument
|
||||
}
|
||||
|
||||
ownerStore := auth.Owner.(map[string]any)
|
||||
storeId := uint(ownerStore["store_id"].(float64))
|
||||
storeIdentity := ownerStore["store_identity"].(string)
|
||||
|
||||
// 查询产品信息以确保其存在且状态为启用。
|
||||
for _, specs := range in.Spec {
|
||||
product := map[string]any{}
|
||||
err = models.DBService.Table("mall_product").Take(&product, "identity=?", specs.ProductIdentity).Error
|
||||
if err != nil {
|
||||
printer.Error(err.Error())
|
||||
return nil, errcode.ErrDB
|
||||
}
|
||||
if product["status"].(int32) != 1 {
|
||||
return nil, errcode.ErrUnknown
|
||||
}
|
||||
// 查询产品规格信息。
|
||||
spec := map[string]any{}
|
||||
err = models.DBService.Table("mall_product_spec").Take(&spec, "product_identity=? ", specs.ProductIdentity).Error
|
||||
if err != nil {
|
||||
printer.Error(err.Error())
|
||||
return nil, errcode.ErrDB
|
||||
}
|
||||
|
||||
// 从产品信息中提取单价、ID等详情,并构建订单详情对象。
|
||||
productID := product["id"].(int64)
|
||||
unit_price := product["sales_price"].(int64)
|
||||
// 计算交易价格并构建订单摘要对象。
|
||||
itemTotal := int64(specs.Number) * unit_price
|
||||
TotalPrice += itemTotal
|
||||
details = &models.OrderDetails{
|
||||
SummaryIdentity: summaryIdentity,
|
||||
Type: 1,
|
||||
ProductID: productID,
|
||||
ProductIdentity: product["identity"].(string),
|
||||
OrderNo: orderNo,
|
||||
Title: product["title"].(string),
|
||||
CoverImage: product["cover_image"].(string),
|
||||
UnitPrice: unit_price,
|
||||
Number: specs.Number,
|
||||
ProductArgs: product["args"].(string),
|
||||
SpecID: spec["id"].(int64),
|
||||
SpecNo: spec["serial_number"].(string),
|
||||
SpecTitle: spec["title"].(string),
|
||||
SupplyId: product["supply_id"].(int64),
|
||||
GasType: product["gas_types"].(int32),
|
||||
}
|
||||
specList = append(specList, *details)
|
||||
}
|
||||
|
||||
// 查询地址信息以确保其存在。
|
||||
address := map[string]any{}
|
||||
err = models.DBService.Table("address_library").Take(&address, "identity=?", in.AddressIdentity).Error
|
||||
if err != nil {
|
||||
printer.Error(err.Error())
|
||||
return nil, errcode.ErrDB
|
||||
}
|
||||
|
||||
summary = &models.OrderSummary{
|
||||
StoreID: storeId,
|
||||
StoreIdentity: storeIdentity,
|
||||
OrderNo: orderNo,
|
||||
PartnerID: in.PartnerId,
|
||||
TransPrice: TotalPrice,
|
||||
TotalPrice: TotalPrice,
|
||||
Args: in.Args,
|
||||
County: address["country"].(string),
|
||||
Province: address["province"].(string),
|
||||
City: address["city"].(string),
|
||||
Area: address["area"].(string),
|
||||
Address: address["detail"].(string),
|
||||
Contact: address["contact"].(string),
|
||||
Phone: address["phone"].(string),
|
||||
AddressIdentity: address["identity"].(string),
|
||||
OrderDetails: specList,
|
||||
}
|
||||
summary.Identity = summaryIdentity
|
||||
summary.PassportID = auth.ID
|
||||
summary.PassportIdentity = auth.Identity
|
||||
summary.Status = 1
|
||||
|
||||
// 将订单摘要和订单详情记录到数据库。
|
||||
err = models.DBService.Transaction(func(tx *gorm.DB) error {
|
||||
// 创建订单摘要。
|
||||
if err := tx.Create(summary).Error; err != nil {
|
||||
printer.Error(err.Error())
|
||||
return err
|
||||
}
|
||||
|
||||
// 扣减库存
|
||||
for _, v := range summary.OrderDetails {
|
||||
if v.Number > 0 {
|
||||
result := tx.Table("mall_product_spec").
|
||||
Where("id = ? AND stock >= ?", v.SpecID, v.Number).
|
||||
UpdateColumn("stock", gorm.Expr("stock - ?", v.Number))
|
||||
|
||||
if result.Error != nil {
|
||||
return result.Error
|
||||
}
|
||||
|
||||
// 检查实际更新的行数
|
||||
if result.RowsAffected == 0 {
|
||||
return errors.New("库存不足或规格不存在")
|
||||
}
|
||||
}
|
||||
}
|
||||
return nil
|
||||
})
|
||||
|
||||
if err != nil {
|
||||
printer.Error(err.Error())
|
||||
return nil, errcode.ErrDB
|
||||
}
|
||||
|
||||
return &pb.StatusReply{
|
||||
Code: 0,
|
||||
Message: "OK",
|
||||
Timeseq: time.Now().UnixNano(),
|
||||
Identity: summary.Identity,
|
||||
}, nil
|
||||
|
||||
}
|
||||
54
module/ec/order/internal/logic/mgt/order_get.go
Normal file
54
module/ec/order/internal/logic/mgt/order_get.go
Normal file
@@ -0,0 +1,54 @@
|
||||
package mgt
|
||||
|
||||
import (
|
||||
"context"
|
||||
"errors"
|
||||
|
||||
"bsm/full/module/ec/order/internal/logic/common"
|
||||
"bsm/full/module/ec/order/internal/models"
|
||||
pb "bsm/full/module/ec/order/pb"
|
||||
"git.apinb.com/bsm-sdk/core/errcode"
|
||||
"git.apinb.com/bsm-sdk/core/printer"
|
||||
"git.apinb.com/bsm-sdk/core/service"
|
||||
"gorm.io/gorm"
|
||||
)
|
||||
|
||||
// 获取一个订单的详情数据
|
||||
func OrderGet(ctx context.Context, in *pb.IdentRequest) (reply *pb.OrderGetReply, err error) {
|
||||
// parse authorization meta.
|
||||
RoleValve := &service.ParseOptions{RoleValue: "Mall_Admin"}
|
||||
if in.GetAgency() != "" {
|
||||
RoleValve = nil
|
||||
}
|
||||
auth, err := service.ParseMetaCtx(ctx, RoleValve)
|
||||
if err != nil {
|
||||
return nil, err
|
||||
}
|
||||
|
||||
if auth.Owner == nil {
|
||||
return nil, errcode.ErrPermissionDenied
|
||||
}
|
||||
|
||||
// valildate request id,identity.
|
||||
if in.Id == 0 && in.Identity == "" {
|
||||
return nil, errcode.ErrInvalidArgument
|
||||
}
|
||||
var storeIdentity string
|
||||
if in.GetAgency() != "" {
|
||||
storeIdentity = in.GetStoreIdentity()
|
||||
} else {
|
||||
ownerStore := auth.Owner.(map[string]any)
|
||||
storeIdentity = ownerStore["store_identity"].(string)
|
||||
}
|
||||
order := &models.OrderSummary{}
|
||||
err = models.DBService.Model(&models.OrderSummary{}).Where("store_identity = ? and identity=?", storeIdentity, in.Identity).Preload("OrderDetails").Find(&order).Error
|
||||
if err != nil {
|
||||
if errors.Is(err, gorm.ErrRecordNotFound) {
|
||||
return nil, errcode.ErrRecordNotFound
|
||||
}
|
||||
printer.Error(err.Error())
|
||||
return nil, errcode.ErrDB
|
||||
}
|
||||
data := common.ReflectProtoOrderSummary(order)
|
||||
return &pb.OrderGetReply{Summary: data}, nil
|
||||
}
|
||||
90
module/ec/order/internal/logic/mgt/order_list_by_store.go
Normal file
90
module/ec/order/internal/logic/mgt/order_list_by_store.go
Normal file
@@ -0,0 +1,90 @@
|
||||
package mgt
|
||||
|
||||
import (
|
||||
"context"
|
||||
"errors"
|
||||
|
||||
"bsm/full/module/ec/order/internal/logic/common"
|
||||
"bsm/full/module/ec/order/internal/models"
|
||||
pb "bsm/full/module/ec/order/pb"
|
||||
"git.apinb.com/bsm-sdk/core/errcode"
|
||||
"git.apinb.com/bsm-sdk/core/printer"
|
||||
"git.apinb.com/bsm-sdk/core/service"
|
||||
"gorm.io/gorm"
|
||||
)
|
||||
|
||||
// 根据不同的类型获取店铺的订单列表
|
||||
func OrderListByStore(ctx context.Context, in *pb.OrderListByStoreRequest) (reply *pb.OrderListByStoreReply, err error) {
|
||||
// parse authorization meta.
|
||||
RoleValve := &service.ParseOptions{RoleValue: "Mall_Admin"}
|
||||
if in.GetAgency() != "" {
|
||||
RoleValve = nil
|
||||
}
|
||||
auth, err := service.ParseMetaCtx(ctx, RoleValve)
|
||||
if err != nil {
|
||||
return nil, err
|
||||
}
|
||||
|
||||
if auth.Owner == nil {
|
||||
return nil, errcode.ErrPermissionDenied
|
||||
}
|
||||
|
||||
var (
|
||||
page = 1
|
||||
size = 50
|
||||
payType int32 = 0
|
||||
cnt int64 = 0
|
||||
storeIdentity string
|
||||
)
|
||||
|
||||
if in.GetPayType() != 0 {
|
||||
payType = in.PayType
|
||||
}
|
||||
|
||||
if in.GetPageNo() > 0 {
|
||||
page = int(in.PageNo)
|
||||
}
|
||||
if in.GetPageSize() > 0 {
|
||||
size = int(in.PageSize)
|
||||
}
|
||||
if in.GetAgency() != "" {
|
||||
storeIdentity = in.GetStoreIdentity()
|
||||
} else {
|
||||
ownerStore := auth.Owner.(map[string]any)
|
||||
storeIdentity = ownerStore["store_identity"].(string)
|
||||
}
|
||||
|
||||
orderList := make([]*models.OrderSummary, 0)
|
||||
tx := models.DBService.Model(&models.OrderSummary{}).Where("store_identity = ?", storeIdentity).Where("partner_id = ?", auth.ID).Preload("OrderDetails")
|
||||
// 根据状态查询订单
|
||||
if in.GetStatus() != 0 {
|
||||
tx = tx.Where("status = ?", in.GetStatus())
|
||||
}
|
||||
// 根据支付类型查询订单
|
||||
if payType != 0 {
|
||||
tx = tx.Where("pay_type = ?", payType)
|
||||
|
||||
}
|
||||
// 根据订单状态查询订单
|
||||
if len(in.GetOrderStatus()) != 0 {
|
||||
tx = tx.Where("status in ?", in.GetOrderStatus())
|
||||
}
|
||||
if in.GetKeyword() != "" {
|
||||
tx = tx.Where("order_no like ?", "%"+in.GetKeyword()+"%")
|
||||
}
|
||||
err = tx.Order("created_at desc").Count(&cnt).Limit(size).Offset((page - 1) * size).Find(&orderList).Error
|
||||
if err != nil {
|
||||
if errors.Is(err, gorm.ErrRecordNotFound) {
|
||||
return nil, errcode.ErrRecordNotFound
|
||||
}
|
||||
printer.Error(err.Error())
|
||||
return nil, errcode.ErrDB
|
||||
}
|
||||
|
||||
data := common.ListModelToReply(orderList)
|
||||
|
||||
return &pb.OrderListByStoreReply{
|
||||
Count: int32(cnt),
|
||||
Data: data,
|
||||
}, nil
|
||||
}
|
||||
34
module/ec/order/internal/logic/mgt/order_modify.go
Normal file
34
module/ec/order/internal/logic/mgt/order_modify.go
Normal file
@@ -0,0 +1,34 @@
|
||||
package mgt
|
||||
|
||||
import (
|
||||
"context"
|
||||
"time"
|
||||
|
||||
pb "bsm/full/module/ec/order/pb"
|
||||
"git.apinb.com/bsm-sdk/core/errcode"
|
||||
"git.apinb.com/bsm-sdk/core/service"
|
||||
)
|
||||
|
||||
// 修改订单
|
||||
func OrderModify(ctx context.Context, in *pb.OrderSummaryItem) (reply *pb.StatusReply, err error) {
|
||||
// parse authorization meta.
|
||||
auth, err := service.ParseMetaCtx(ctx, &service.ParseOptions{RoleValue: "Mall_Admin"})
|
||||
if err != nil {
|
||||
return nil, err
|
||||
}
|
||||
|
||||
if auth.Owner == nil {
|
||||
return nil, errcode.ErrPermissionDenied
|
||||
}
|
||||
|
||||
// TODO: valid code
|
||||
|
||||
// TODO: add your logic code & delete this line.
|
||||
|
||||
return &pb.StatusReply{
|
||||
Code: 0,
|
||||
Message: "OK",
|
||||
Timeseq: time.Now().UnixNano(),
|
||||
}, nil
|
||||
|
||||
}
|
||||
61
module/ec/order/internal/logic/mgt/order_returnable.go
Normal file
61
module/ec/order/internal/logic/mgt/order_returnable.go
Normal file
@@ -0,0 +1,61 @@
|
||||
package mgt
|
||||
|
||||
import (
|
||||
"context"
|
||||
"errors"
|
||||
"time"
|
||||
|
||||
"bsm/full/module/ec/order/internal/models"
|
||||
pb "bsm/full/module/ec/order/pb"
|
||||
"git.apinb.com/bsm-sdk/core/errcode"
|
||||
"git.apinb.com/bsm-sdk/core/printer"
|
||||
"git.apinb.com/bsm-sdk/core/service"
|
||||
)
|
||||
|
||||
// OrderReturnable 订单申请售后
|
||||
func OrderReturnable(ctx context.Context, in *pb.IdentRequest) (reply *pb.StatusReply, err error) {
|
||||
_, err = service.ParseMetaCtx(ctx, &service.ParseOptions{RoleValue: "Mall_Admin"})
|
||||
if err != nil {
|
||||
return nil, err
|
||||
}
|
||||
// 验证订单的approve审核状态:状态:-2:未通过,0:默认 1:申请退款 2:申请退货 3:申请退款退货 4:申请通过
|
||||
if in.GetApprove() != 1 && in.GetApprove() != 2 && in.GetApprove() != 3 {
|
||||
return nil, errcode.ErrInvalidArgument
|
||||
}
|
||||
// 验证输入参数是否有效。
|
||||
if in.GetIdentity() == "" {
|
||||
return nil, errcode.ErrInvalidArgument
|
||||
}
|
||||
|
||||
// 获取当前订单信息
|
||||
order := models.OrderSummary{}
|
||||
if err := models.DBService.Preload("OrderDetails").Where("identity=?", in.Identity).First(&order).Error; err != nil {
|
||||
printer.Error(err.Error())
|
||||
return nil, errcode.ErrDB
|
||||
}
|
||||
|
||||
// 订单状态判断是否允许申请退款退货
|
||||
if order.Status == -1 || order.Status == 6 || order.Status == 7 || order.Status == 8 || order.Approve != 0 {
|
||||
return nil, errors.New("订单状态异常")
|
||||
}
|
||||
|
||||
// 订单状态更新
|
||||
summaryModel := models.OrderSummary{
|
||||
Approve: int8(in.GetApprove()),
|
||||
Reason: in.GetReason(),
|
||||
}
|
||||
err = models.DBService.Model(&models.OrderSummary{}).Where("identity = ?", in.Identity).Updates(&summaryModel).Error
|
||||
if err != nil {
|
||||
printer.Error(err.Error())
|
||||
return nil, errcode.ErrDB
|
||||
}
|
||||
|
||||
return &pb.StatusReply{
|
||||
Code: 0,
|
||||
Message: "OK",
|
||||
Identity: in.GetIdentity(),
|
||||
Reason: in.GetReason(),
|
||||
Timeseq: time.Now().UnixNano(),
|
||||
}, nil
|
||||
|
||||
}
|
||||
60
module/ec/order/internal/logic/summary/cancel.go
Normal file
60
module/ec/order/internal/logic/summary/cancel.go
Normal file
@@ -0,0 +1,60 @@
|
||||
package summary
|
||||
|
||||
import (
|
||||
"context"
|
||||
"time"
|
||||
|
||||
"bsm/full/module/ec/order/internal/models"
|
||||
pb "bsm/full/module/ec/order/pb"
|
||||
"git.apinb.com/bsm-sdk/core/errcode"
|
||||
"git.apinb.com/bsm-sdk/core/printer"
|
||||
"git.apinb.com/bsm-sdk/core/service"
|
||||
)
|
||||
|
||||
// 取消订单
|
||||
func Cancel(ctx context.Context, in *pb.CancelRequest) (reply *pb.StatusReply, err error) {
|
||||
// parse authorization meta.
|
||||
_, err = service.ParseMetaCtx(ctx, nil)
|
||||
if err != nil {
|
||||
return nil, err
|
||||
}
|
||||
|
||||
var (
|
||||
summary = new(models.OrderSummary)
|
||||
)
|
||||
|
||||
err = models.DBService.Where("order_no = ?", in.OrderNo).First(&summary).Error
|
||||
if err != nil {
|
||||
printer.Error(err.Error())
|
||||
return nil, errcode.ErrDB
|
||||
}
|
||||
|
||||
if summary.Status == 1 {
|
||||
values := &models.OrderSummary{
|
||||
CouponIdentity: "",
|
||||
CouponAmount: 0.00,
|
||||
}
|
||||
values.Status = -1
|
||||
|
||||
err := models.DBService.Where("order_no = ?", in.OrderNo).Updates(values).Error
|
||||
|
||||
if err != nil {
|
||||
printer.Error(err.Error())
|
||||
return nil, errcode.ErrDB
|
||||
}
|
||||
|
||||
err = models.DBService.Model(&models.OrderCoupon{}).Where("identity=?", summary.CouponIdentity).Update("status", 2).Error
|
||||
if err != nil {
|
||||
printer.Error(err.Error())
|
||||
return nil, errcode.ErrDB
|
||||
}
|
||||
}
|
||||
|
||||
return &pb.StatusReply{
|
||||
Code: 0,
|
||||
Identity: summary.Identity,
|
||||
Message: "OK",
|
||||
Timeseq: time.Now().UnixNano(),
|
||||
}, nil
|
||||
|
||||
}
|
||||
37
module/ec/order/internal/logic/summary/check.go
Normal file
37
module/ec/order/internal/logic/summary/check.go
Normal file
@@ -0,0 +1,37 @@
|
||||
package summary
|
||||
|
||||
import (
|
||||
"context"
|
||||
"time"
|
||||
|
||||
"bsm/full/module/ec/order/internal/models"
|
||||
pb "bsm/full/module/ec/order/pb"
|
||||
"git.apinb.com/bsm-sdk/core/errcode"
|
||||
"git.apinb.com/bsm-sdk/core/printer"
|
||||
"git.apinb.com/bsm-sdk/core/service"
|
||||
)
|
||||
|
||||
// 检测是否有未确认及付款的订单
|
||||
func Check(ctx context.Context, in *pb.Empty) (reply *pb.StatusReply, err error) {
|
||||
// parse authorization meta.
|
||||
auth, err := service.ParseMetaCtx(ctx, &service.ParseOptions{RoleValue: "Mall_Admin"})
|
||||
if err != nil {
|
||||
return nil, err
|
||||
}
|
||||
var (
|
||||
summary = new(models.OrderSummary)
|
||||
)
|
||||
err = models.DBService.Where("passport_id = ?", auth.ID).First(&summary).Error
|
||||
if err != nil {
|
||||
printer.Error(err.Error())
|
||||
return nil, errcode.ErrDB
|
||||
}
|
||||
|
||||
return &pb.StatusReply{
|
||||
Code: 0,
|
||||
Identity: summary.OrderNo,
|
||||
Message: "OK",
|
||||
Timeseq: time.Now().UnixNano(),
|
||||
}, nil
|
||||
|
||||
}
|
||||
84
module/ec/order/internal/logic/summary/confirm.go
Normal file
84
module/ec/order/internal/logic/summary/confirm.go
Normal file
@@ -0,0 +1,84 @@
|
||||
package summary
|
||||
|
||||
import (
|
||||
"context"
|
||||
|
||||
"bsm/full/module/ec/order/internal/models"
|
||||
pb "bsm/full/module/ec/order/pb"
|
||||
"git.apinb.com/bsm-sdk/core/errcode"
|
||||
"git.apinb.com/bsm-sdk/core/printer"
|
||||
"git.apinb.com/bsm-sdk/core/service"
|
||||
)
|
||||
|
||||
// 确认订单,物流,优惠卷等其它信息
|
||||
func Confirm(ctx context.Context, in *pb.ConfirmRequest) (reply *pb.ConfirmReply, err error) {
|
||||
// parse authorization meta.
|
||||
_, err = service.ParseMetaCtx(ctx, nil)
|
||||
if err != nil {
|
||||
return nil, err
|
||||
}
|
||||
|
||||
var (
|
||||
logisFee int64 = 0
|
||||
couponAmount int64 = 0
|
||||
summary = new(models.OrderSummary)
|
||||
coupon = new(models.OrderCoupon)
|
||||
)
|
||||
|
||||
err = models.DBService.Where("order_no = ?", in.OrderNo).First(&summary).Error
|
||||
if err != nil {
|
||||
printer.Error(err.Error())
|
||||
return nil, errcode.ErrDB
|
||||
}
|
||||
|
||||
if in.AddressIdentity != "" {
|
||||
/*
|
||||
err = models.DBService.Where("id=?", in.AddressId).First(&address).Error
|
||||
if err == nil {
|
||||
summary.Address = address.Detail
|
||||
summary.County = address.Country
|
||||
summary.Province = address.Province
|
||||
summary.City = address.City
|
||||
summary.Area = address.Area
|
||||
summary.Contact = address.Contact
|
||||
summary.Phone = address.Phone
|
||||
logisFee = GetLogisticsFee(summary.TotalPrice, address.Province)
|
||||
}
|
||||
*/
|
||||
}
|
||||
if logisFee > 0 {
|
||||
summary.TotalPrice = summary.TotalPrice + logisFee
|
||||
}
|
||||
|
||||
if in.CouponIdentity != "" {
|
||||
err = models.DBService.Where("identity=?", in.CouponIdentity).First(&coupon).Error
|
||||
if err == nil {
|
||||
if coupon.Status == 2 {
|
||||
models.DBService.Model(&models.OrderCoupon{}).Where("identity=?", in.CouponIdentity).Update("status", 3)
|
||||
couponAmount = coupon.Amount
|
||||
} else {
|
||||
printer.Error(err.Error())
|
||||
return nil, errcode.ErrUnavailable
|
||||
}
|
||||
} else {
|
||||
printer.Error(err.Error())
|
||||
return nil, errcode.ErrRecordNotFound
|
||||
}
|
||||
}
|
||||
|
||||
if couponAmount > 0 {
|
||||
summary.TotalPrice = summary.TotalPrice + couponAmount
|
||||
}
|
||||
|
||||
summary.Status = 1
|
||||
|
||||
err = models.DBService.Where("order_no = ?", in.OrderNo).Updates(summary).Error
|
||||
if err != nil {
|
||||
printer.Error(err.Error())
|
||||
return nil, errcode.ErrDB
|
||||
}
|
||||
|
||||
return &pb.ConfirmReply{
|
||||
TotalPrice: summary.TotalPrice,
|
||||
}, nil
|
||||
}
|
||||
35
module/ec/order/internal/logic/summary/get.go
Normal file
35
module/ec/order/internal/logic/summary/get.go
Normal file
@@ -0,0 +1,35 @@
|
||||
package summary
|
||||
|
||||
import (
|
||||
"context"
|
||||
|
||||
"bsm/full/module/ec/order/internal/logic/common"
|
||||
pb "bsm/full/module/ec/order/pb"
|
||||
"git.apinb.com/bsm-sdk/core/errcode"
|
||||
"git.apinb.com/bsm-sdk/core/printer"
|
||||
"git.apinb.com/bsm-sdk/core/service"
|
||||
)
|
||||
|
||||
// 获取一个订单的详情数据
|
||||
func Get(ctx context.Context, in *pb.IdentRequest) (reply *pb.SummaryGetReply, err error) {
|
||||
// parse authorization meta.
|
||||
_, err = service.ParseMetaCtx(ctx, nil)
|
||||
if err != nil {
|
||||
return nil, err
|
||||
}
|
||||
|
||||
// valildate request id,identity.
|
||||
if in.Identity == "" {
|
||||
return nil, errcode.ErrInvalidArgument
|
||||
}
|
||||
|
||||
summary, err := common.GetOrderSummaryByIdentity(in.Identity)
|
||||
if err != nil {
|
||||
printer.Error(err.Error())
|
||||
return nil, errcode.ErrDB
|
||||
}
|
||||
|
||||
return &pb.SummaryGetReply{
|
||||
Summary: summary,
|
||||
}, nil
|
||||
}
|
||||
70
module/ec/order/internal/logic/summary/list.go
Normal file
70
module/ec/order/internal/logic/summary/list.go
Normal file
@@ -0,0 +1,70 @@
|
||||
package summary
|
||||
|
||||
import (
|
||||
"context"
|
||||
"errors"
|
||||
|
||||
"bsm/full/module/ec/order/internal/logic/common"
|
||||
"bsm/full/module/ec/order/internal/models"
|
||||
pb "bsm/full/module/ec/order/pb"
|
||||
"git.apinb.com/bsm-sdk/core/errcode"
|
||||
"git.apinb.com/bsm-sdk/core/printer"
|
||||
"git.apinb.com/bsm-sdk/core/service"
|
||||
"gorm.io/gorm"
|
||||
)
|
||||
|
||||
// 根据不同的类型获取我的订单列表
|
||||
func List(ctx context.Context, in *pb.SummaryListRequest) (reply *pb.SummaryListReply, err error) {
|
||||
// parse authorization meta.
|
||||
auth, err := service.ParseMetaCtx(ctx, &service.ParseOptions{RoleValue: "Mall_Admin"})
|
||||
if err != nil {
|
||||
return nil, err
|
||||
}
|
||||
|
||||
var (
|
||||
page = 1
|
||||
size = 50
|
||||
payType int32 = 0 // 支付方式
|
||||
orderStatus int32 = 0 // 订单状态
|
||||
cnt int64 = 0
|
||||
)
|
||||
|
||||
if in.PayType != 0 {
|
||||
payType = in.PayType
|
||||
}
|
||||
if in.OrderStatus != 0 {
|
||||
orderStatus = in.OrderStatus
|
||||
}
|
||||
if in.GetPageNo() > 0 {
|
||||
page = int(in.PageNo)
|
||||
}
|
||||
if in.GetPageSize() > 0 {
|
||||
size = int(in.PageSize)
|
||||
}
|
||||
|
||||
orderList := make([]*models.OrderSummary, 0)
|
||||
tx := models.DBService.Model(&models.OrderSummary{}).Preload("OrderDetails").Where("passport_identity = ?", auth.Identity)
|
||||
if payType != 0 {
|
||||
tx = tx.Where("pay_type = ?", payType)
|
||||
|
||||
}
|
||||
if orderStatus != 0 {
|
||||
tx = tx.Where("status = ?", orderStatus)
|
||||
}
|
||||
|
||||
err = tx.Order("created_at desc").Count(&cnt).Limit(size).Offset((page - 1) * size).Find(&orderList).Error
|
||||
if err != nil {
|
||||
if errors.Is(err, gorm.ErrRecordNotFound) {
|
||||
return nil, errcode.ErrRecordNotFound
|
||||
}
|
||||
printer.Error(err.Error())
|
||||
return nil, errcode.ErrDB
|
||||
}
|
||||
|
||||
data := common.ListModelToReply(orderList)
|
||||
|
||||
return &pb.SummaryListReply{
|
||||
Count: int32(cnt),
|
||||
Data: data,
|
||||
}, nil
|
||||
}
|
||||
@@ -0,0 +1,165 @@
|
||||
package summary
|
||||
|
||||
import (
|
||||
"context"
|
||||
"errors"
|
||||
"strings"
|
||||
"time"
|
||||
|
||||
"bsm/full/module/ec/order/internal/logic/common"
|
||||
"bsm/full/module/ec/order/internal/models"
|
||||
pb "bsm/full/module/ec/order/pb"
|
||||
"git.apinb.com/bsm-sdk/core/errcode"
|
||||
"git.apinb.com/bsm-sdk/core/printer"
|
||||
"git.apinb.com/bsm-sdk/core/service"
|
||||
"git.apinb.com/bsm-sdk/core/utils"
|
||||
)
|
||||
|
||||
// 快速创建一个店铺订单
|
||||
func QuickCreateByProduct(ctx context.Context, in *pb.QuickCreateByProductRequest) (reply *pb.StatusReply, err error) {
|
||||
// parse authorization meta.
|
||||
auth, err := service.ParseMetaCtx(ctx, &service.ParseOptions{RoleValue: "Mall_Admin"})
|
||||
if err != nil {
|
||||
return nil, err
|
||||
}
|
||||
|
||||
// 验证输入参数是否有效。
|
||||
if in.GetProductIdentity() == "" || in.GetNumber() == 0 || in.GetStoreIdentity() == "" || in.GetAddressIdentity() == "" {
|
||||
return nil, errcode.ErrInvalidArgument
|
||||
}
|
||||
|
||||
// 查询商店信息以确保其存在且状态为启用。
|
||||
store := map[string]any{}
|
||||
err = models.DBService.Table("mall_store").Take(&store, "identity=?", in.StoreIdentity).Error
|
||||
if err != nil {
|
||||
printer.Error(err.Error())
|
||||
if errors.Is(err, models.ErrNotFound) {
|
||||
return nil, errcode.ErrRecordNotFound
|
||||
}
|
||||
return nil, errcode.ErrDB
|
||||
}
|
||||
if store["status"].(int16) != 1 {
|
||||
return nil, errcode.ErrUnknown
|
||||
}
|
||||
|
||||
// 查询产品信息以确保其存在且状态为启用。
|
||||
product := map[string]any{}
|
||||
var pi string = strings.ToLower(in.ProductIdentity)
|
||||
if pi == "gas_by_kg" || pi == "gas_by_bottle" {
|
||||
err = models.DBService.Table("mall_product").Take(&product, "store_identity=? and serial_id=?", in.StoreIdentity, pi).Error
|
||||
} else {
|
||||
err = models.DBService.Table("mall_product").Take(&product, "identity=?", in.ProductIdentity).Error
|
||||
}
|
||||
if err != nil {
|
||||
printer.Error(err.Error())
|
||||
if errors.Is(err, models.ErrNotFound) {
|
||||
return nil, errcode.ErrRecordNotFound
|
||||
}
|
||||
return nil, errcode.ErrDB
|
||||
}
|
||||
if product["status"].(int32) != 1 {
|
||||
return nil, errcode.ErrUnknown
|
||||
}
|
||||
|
||||
// 查询产品规格信息
|
||||
spec := map[string]any{}
|
||||
err = models.DBService.Table("mall_product_spec").Take(&spec, "product_identity=?", in.ProductIdentity).Error
|
||||
if err != nil {
|
||||
printer.Error(err.Error())
|
||||
if errors.Is(err, models.ErrNotFound) {
|
||||
return nil, errcode.ErrRecordNotFound
|
||||
}
|
||||
return nil, errcode.ErrDB
|
||||
}
|
||||
|
||||
// 查询地址信息以确保其存在。
|
||||
address := map[string]any{}
|
||||
err = models.DBService.Table("address_library").Take(&address, "identity=?", in.AddressIdentity).Error
|
||||
if err != nil {
|
||||
printer.Error(err.Error())
|
||||
if errors.Is(err, models.ErrNotFound) {
|
||||
return nil, errcode.ErrRecordNotFound
|
||||
}
|
||||
return nil, errcode.ErrDB
|
||||
}
|
||||
|
||||
// // 查询配送员信息
|
||||
// member := map[string]any{}
|
||||
// err = models.DBService.Table("delivery_member").Take(&member, "identity=?", in.MemberIdentity).Error
|
||||
// if err != nil {
|
||||
// printer.Error(err.Error())
|
||||
// if errors.Is(err, models.ErrNotFound) {
|
||||
// return nil, errcode.ErrRecordNotFound
|
||||
// }
|
||||
// return nil, errcode.ErrDB
|
||||
// }
|
||||
|
||||
// 生成订单号。
|
||||
orderNo := common.CreateOrderNo()
|
||||
// 计算交易价格并构建订单摘要对象。
|
||||
TransPrice := (int64(in.Number) * spec["price"].(int64))
|
||||
summary := &models.OrderSummary{
|
||||
StoreID: uint(store["id"].(int64)),
|
||||
StoreIdentity: in.StoreIdentity,
|
||||
OrderNo: orderNo,
|
||||
PartnerID: in.PartnerId,
|
||||
TransPrice: TransPrice,
|
||||
TotalPrice: TransPrice,
|
||||
Args: in.Args,
|
||||
AddressIdentity: in.AddressIdentity,
|
||||
Province: address["province"].(string),
|
||||
City: address["city"].(string),
|
||||
Area: address["area"].(string),
|
||||
Address: address["detail"].(string),
|
||||
Contact: address["contact"].(string),
|
||||
Phone: address["phone"].(string),
|
||||
DeliveryTime: in.GetDeliveryTime(),
|
||||
// DeliveryIdentity: member["identity"].(string),
|
||||
DeliveryAddress: in.DeliveryAddress,
|
||||
}
|
||||
summary.Identity = utils.UUID()
|
||||
summary.PassportID = auth.ID
|
||||
summary.PassportIdentity = auth.Identity
|
||||
summary.Status = 1
|
||||
|
||||
// 从产品信息中提取单价、ID等详情,并构建订单详情对象。
|
||||
unit_price := product["sales_price"].(int64)
|
||||
productID := product["id"].(int64)
|
||||
details := &models.OrderDetails{
|
||||
Type: 1,
|
||||
ProductID: productID,
|
||||
ProductIdentity: product["identity"].(string),
|
||||
SpecID: spec["id"].(int64),
|
||||
OrderNo: orderNo,
|
||||
Title: product["title"].(string),
|
||||
CoverImage: product["cover_image"].(string),
|
||||
UnitPrice: unit_price,
|
||||
Number: in.Number,
|
||||
ProductArgs: product["args"].(string),
|
||||
SummaryIdentity: summary.Identity,
|
||||
SpecTitle: spec["title"].(string),
|
||||
SpecNo: spec["serial_number"].(string),
|
||||
GasType: product["gas_types"].(int32),
|
||||
SupplyId: product["supply_id"].(int64),
|
||||
}
|
||||
|
||||
// 将订单摘要和订单详情记录到数据库。
|
||||
err = models.DBService.Create(summary).Error
|
||||
if err != nil {
|
||||
printer.Error(err.Error())
|
||||
return nil, errcode.ErrDB
|
||||
}
|
||||
|
||||
err = models.DBService.Create(details).Error
|
||||
if err != nil {
|
||||
printer.Error(err.Error())
|
||||
return nil, errcode.ErrDB
|
||||
}
|
||||
|
||||
return &pb.StatusReply{
|
||||
Code: 0,
|
||||
Message: "OK",
|
||||
Timeseq: time.Now().UnixNano(),
|
||||
}, nil
|
||||
|
||||
}
|
||||
38
module/ec/order/internal/logic/summary/simulate_pay.go
Normal file
38
module/ec/order/internal/logic/summary/simulate_pay.go
Normal file
@@ -0,0 +1,38 @@
|
||||
package summary
|
||||
|
||||
import (
|
||||
"context"
|
||||
"errors"
|
||||
"time"
|
||||
|
||||
"bsm/full/module/ec/order/internal/models"
|
||||
pb "bsm/full/module/ec/order/pb"
|
||||
"git.apinb.com/bsm-sdk/core/errcode"
|
||||
"git.apinb.com/bsm-sdk/core/printer"
|
||||
"git.apinb.com/bsm-sdk/core/service"
|
||||
)
|
||||
|
||||
// 模拟支付
|
||||
func SimulatePay(ctx context.Context, in *pb.SimulatePayRequest) (reply *pb.StatusReply, err error) {
|
||||
// parse authorization meta.
|
||||
_, err = service.ParseMetaCtx(ctx, nil)
|
||||
if err != nil {
|
||||
return nil, err
|
||||
}
|
||||
|
||||
err = models.DBService.Model(&models.OrderSummary{}).Where("identity in ?", in.Identity).Updates(models.OrderSummary{Status: 2, PayTime: time.Now(), PayType: 4, PayRemark: "支付备注", PayTradeNo: "Pay12371937812897", PayAmount: 10000000}).Error
|
||||
if err != nil {
|
||||
printer.Error(err.Error())
|
||||
if errors.Is(err, models.ErrNotFound) {
|
||||
return nil, errcode.ErrRecordNotFound
|
||||
}
|
||||
return nil, errcode.ErrDB
|
||||
}
|
||||
|
||||
return &pb.StatusReply{
|
||||
Code: 0,
|
||||
Message: "OK",
|
||||
Timeseq: time.Now().UnixNano(),
|
||||
}, nil
|
||||
|
||||
}
|
||||
42
module/ec/order/internal/logic/summary/simulate_receiving.go
Normal file
42
module/ec/order/internal/logic/summary/simulate_receiving.go
Normal file
@@ -0,0 +1,42 @@
|
||||
package summary
|
||||
|
||||
import (
|
||||
"context"
|
||||
"errors"
|
||||
"time"
|
||||
|
||||
"bsm/full/module/ec/order/internal/models"
|
||||
pb "bsm/full/module/ec/order/pb"
|
||||
"git.apinb.com/bsm-sdk/core/errcode"
|
||||
"git.apinb.com/bsm-sdk/core/printer"
|
||||
"git.apinb.com/bsm-sdk/core/service"
|
||||
)
|
||||
|
||||
// 模拟收货
|
||||
func SimulateReceiving(ctx context.Context, in *pb.IdentRequest) (reply *pb.StatusReply, err error) {
|
||||
// parse authorization meta.
|
||||
_, err = service.ParseMetaCtx(ctx, nil)
|
||||
if err != nil {
|
||||
return nil, err
|
||||
}
|
||||
// valildate request id,identity.
|
||||
if in.Id == 0 && in.Identity == "" {
|
||||
return nil, errcode.ErrInvalidArgument
|
||||
}
|
||||
|
||||
err = models.DBService.Model(&models.OrderSummary{}).Where("identity = ?", in.Identity).Update("status", 4).Error
|
||||
if err != nil {
|
||||
printer.Error(err.Error())
|
||||
if errors.Is(err, models.ErrNotFound) {
|
||||
return nil, errcode.ErrRecordNotFound
|
||||
}
|
||||
return nil, errcode.ErrDB
|
||||
}
|
||||
|
||||
return &pb.StatusReply{
|
||||
Code: 0,
|
||||
Message: "OK",
|
||||
Timeseq: time.Now().UnixNano(),
|
||||
}, nil
|
||||
|
||||
}
|
||||
41
module/ec/order/internal/logic/summary/simulate_shipments.go
Normal file
41
module/ec/order/internal/logic/summary/simulate_shipments.go
Normal file
@@ -0,0 +1,41 @@
|
||||
package summary
|
||||
|
||||
import (
|
||||
"context"
|
||||
"errors"
|
||||
"time"
|
||||
|
||||
"bsm/full/module/ec/order/internal/models"
|
||||
pb "bsm/full/module/ec/order/pb"
|
||||
"git.apinb.com/bsm-sdk/core/errcode"
|
||||
"git.apinb.com/bsm-sdk/core/printer"
|
||||
"git.apinb.com/bsm-sdk/core/service"
|
||||
)
|
||||
|
||||
// 模拟发货
|
||||
func SimulateShipments(ctx context.Context, in *pb.SimulateShipmentsRequest) (reply *pb.StatusReply, err error) {
|
||||
// parse authorization meta.
|
||||
_, err = service.ParseMetaCtx(ctx, nil)
|
||||
if err != nil {
|
||||
return nil, err
|
||||
}
|
||||
|
||||
if in.GetMemberIdentity() == "" {
|
||||
return nil, errcode.ErrInvalidArgument
|
||||
}
|
||||
|
||||
err = models.DBService.Where("identity = ?", in.Identity).Updates(&models.OrderSummary{Status: 3, DeliveryIdentity: in.MemberIdentity}).Error
|
||||
if err != nil {
|
||||
printer.Error(err.Error())
|
||||
if errors.Is(err, models.ErrNotFound) {
|
||||
return nil, errcode.ErrRecordNotFound
|
||||
}
|
||||
return nil, errcode.ErrDB
|
||||
}
|
||||
return &pb.StatusReply{
|
||||
Code: 0,
|
||||
Message: "OK",
|
||||
Timeseq: time.Now().UnixNano(),
|
||||
}, nil
|
||||
|
||||
}
|
||||
245
module/ec/order/internal/logic/summary/submit.go
Normal file
245
module/ec/order/internal/logic/summary/submit.go
Normal file
@@ -0,0 +1,245 @@
|
||||
package summary
|
||||
|
||||
import (
|
||||
"context"
|
||||
"errors"
|
||||
"log"
|
||||
"strings"
|
||||
"time"
|
||||
|
||||
"bsm/full/module/ec/order/internal/logic/common"
|
||||
"bsm/full/module/ec/order/internal/models"
|
||||
pb "bsm/full/module/ec/order/pb"
|
||||
"git.apinb.com/bsm-sdk/core/errcode"
|
||||
"git.apinb.com/bsm-sdk/core/printer"
|
||||
"git.apinb.com/bsm-sdk/core/service"
|
||||
"git.apinb.com/bsm-sdk/core/types"
|
||||
"git.apinb.com/bsm-sdk/core/utils"
|
||||
"gorm.io/gorm"
|
||||
)
|
||||
|
||||
// 将购物车的数据提交生成订单
|
||||
func Submit(ctx context.Context, in *pb.SubmitRequest) (reply *pb.StatusReply, err error) {
|
||||
// parse authorization meta.
|
||||
RoleValve := &service.ParseOptions{RoleValue: "Mall_Admin"}
|
||||
if in.GetAddress() != nil {
|
||||
RoleValve = nil
|
||||
}
|
||||
auth, err := service.ParseMetaCtx(ctx, RoleValve)
|
||||
if err != nil {
|
||||
return nil, err
|
||||
}
|
||||
|
||||
var (
|
||||
cart = make([]*models.OrderCart, 0)
|
||||
details = make([]*models.OrderDetails, 0)
|
||||
summary = make([]*models.OrderSummary, 0)
|
||||
keys = make([]string, 0)
|
||||
address = models.OrderAddress{}
|
||||
StoreId uint = 0
|
||||
)
|
||||
|
||||
// 获取购物车内数据信息
|
||||
err = models.DBService.Where("passport_identity = ?", auth.Identity).Find(&cart).Error
|
||||
if err != nil {
|
||||
printer.Error(err.Error())
|
||||
if errors.Is(err, models.ErrNotFound) {
|
||||
return nil, errcode.ErrRecordNotFound
|
||||
}
|
||||
return nil, errcode.ErrDB
|
||||
}
|
||||
|
||||
// 获取地址信息
|
||||
if in.GetAddress() != nil {
|
||||
address = models.OrderAddress{
|
||||
Country: in.Address.Country,
|
||||
Province: in.Address.Province,
|
||||
City: in.Address.City,
|
||||
Detail: in.Address.Detail,
|
||||
Contact: in.Address.Contact,
|
||||
Phone: in.Address.Phone,
|
||||
Area: in.Address.Area,
|
||||
Email: in.Address.Email,
|
||||
ZipCode: in.Address.ZipCode,
|
||||
}
|
||||
} else {
|
||||
err = models.DBService.Table("address_library").Where("identity = ?", in.AddressIdentity).Find(&address).Error
|
||||
if err != nil {
|
||||
printer.Error(err.Error())
|
||||
if errors.Is(err, models.ErrNotFound) {
|
||||
return nil, errcode.ErrRecordNotFound
|
||||
}
|
||||
return nil, errcode.ErrDB
|
||||
}
|
||||
}
|
||||
|
||||
// 按店铺分组购物车商品
|
||||
storeGroups := make(map[string][]*models.OrderCart)
|
||||
for k, item := range cart {
|
||||
product := models.Product{}
|
||||
err = models.DBService.Select("store_identity").Table("mall_product").Where("id = ?", item.ProductID).First(&product).Error
|
||||
if err != nil {
|
||||
printer.Error(err.Error())
|
||||
if errors.Is(err, models.ErrNotFound) {
|
||||
return nil, errcode.ErrRecordNotFound
|
||||
}
|
||||
return nil, errcode.ErrDB
|
||||
}
|
||||
|
||||
if k == 0 {
|
||||
StoreId = product.StoreId
|
||||
}
|
||||
storeGroups[product.StoreIdentity] = append(storeGroups[product.StoreIdentity], item)
|
||||
}
|
||||
|
||||
// 处理每个店铺
|
||||
for StoreIdentity, items := range storeGroups {
|
||||
var storeTotal int64 = 0
|
||||
var storeDetails []*models.OrderDetails
|
||||
|
||||
//订单号:2位年份,月,日,时,分,秒,6位随机数共18位
|
||||
summaryIdentity := utils.UUID()
|
||||
orderNo := common.CreateOrderNo()
|
||||
|
||||
// 处理店铺内所有商品
|
||||
for _, item := range items {
|
||||
product := models.Product{}
|
||||
spec := models.Spec{}
|
||||
|
||||
// 获取产品信息
|
||||
err = models.DBService.Select("title,cover_image,cost_price,supply_id,args,gas_types").Table("mall_product").
|
||||
Where("id = ?", item.ProductID).First(&product).Error
|
||||
if err != nil {
|
||||
printer.Error(err.Error())
|
||||
if errors.Is(err, models.ErrNotFound) {
|
||||
return nil, errcode.ErrRecordNotFound
|
||||
}
|
||||
return nil, errcode.ErrDB
|
||||
}
|
||||
|
||||
// 获取规格信息
|
||||
err := models.DBService.Select("title,serial_number,price").Table("mall_product_spec").
|
||||
Where("id = ?", item.SpecID).First(&spec).Error
|
||||
if err != nil {
|
||||
printer.Error(err.Error())
|
||||
if errors.Is(err, models.ErrNotFound) {
|
||||
return nil, errcode.ErrRecordNotFound
|
||||
}
|
||||
return nil, errcode.ErrDB
|
||||
}
|
||||
|
||||
// 计算商品总价
|
||||
itemTotal := int64(item.Number) * spec.Price
|
||||
storeTotal += itemTotal
|
||||
|
||||
// 添加到订单详情
|
||||
storeDetails = append(storeDetails, &models.OrderDetails{
|
||||
Type: 1,
|
||||
ProductID: item.ProductID,
|
||||
ProductIdentity: item.ProductIdentity,
|
||||
SpecID: item.SpecID,
|
||||
OrderNo: orderNo,
|
||||
Title: product.Title,
|
||||
CoverImage: product.CoverImage,
|
||||
UnitPrice: spec.Price,
|
||||
SalesPrice: spec.Price,
|
||||
Number: item.Number,
|
||||
ProductArgs: item.ProductArgs,
|
||||
SummaryIdentity: summaryIdentity,
|
||||
SpecTitle: spec.Title,
|
||||
SpecNo: spec.SerialNumber,
|
||||
GasType: int32(product.GasTypes),
|
||||
SupplyId: product.SupplyId,
|
||||
TotalPrice: int64(item.Number) * spec.Price,
|
||||
})
|
||||
}
|
||||
|
||||
// 创建店铺订单摘要
|
||||
summary = append(summary, &models.OrderSummary{
|
||||
OrderNo: orderNo,
|
||||
Std_Identity: types.Std_Identity{Identity: summaryIdentity},
|
||||
PartnerID: in.PartnerId,
|
||||
TransPrice: storeTotal,
|
||||
TotalPrice: storeTotal,
|
||||
StoreID: StoreId,
|
||||
StoreIdentity: StoreIdentity,
|
||||
LogisticsFee: 0,
|
||||
Remark: "",
|
||||
AddressIdentity: in.AddressIdentity,
|
||||
County: address.Country,
|
||||
Province: address.Province,
|
||||
City: address.City,
|
||||
Area: address.Area,
|
||||
Address: address.Detail,
|
||||
Contact: address.Contact,
|
||||
Phone: address.Phone,
|
||||
Std_Passport: types.Std_Passport{PassportID: auth.ID, PassportIdentity: auth.Identity},
|
||||
Status: 1,
|
||||
})
|
||||
|
||||
// 添加到总详情列表
|
||||
details = append(details, storeDetails...)
|
||||
keys = append(keys, StoreIdentity)
|
||||
}
|
||||
|
||||
err = models.DBService.Transaction(func(tx *gorm.DB) error {
|
||||
// 1. 创建订单摘要
|
||||
if err := tx.Create(summary).Error; err != nil {
|
||||
printer.Error(err.Error())
|
||||
return err
|
||||
}
|
||||
|
||||
// 2. 创建订单详情
|
||||
if err := tx.Model(&models.OrderDetails{}).CreateInBatches(details, len(details)).Error; err != nil {
|
||||
printer.Error(err.Error())
|
||||
return err
|
||||
}
|
||||
|
||||
// 3. 扣减库存
|
||||
for _, detail := range details {
|
||||
if detail.Number > 0 {
|
||||
var stock int32
|
||||
result := tx.Table("mall_product_spec").Select("stock").Where("id = ?", detail.SpecID).Scan(&stock)
|
||||
if result.Error != nil || stock < detail.Number {
|
||||
log.Printf("Insufficient stock or spec does not exist: %v", result.Error)
|
||||
return errors.New("库存不足或规格不存在")
|
||||
}
|
||||
updateErr := tx.Table("mall_product_spec").
|
||||
Where("id = ? AND stock >= ?", detail.SpecID, detail.Number).
|
||||
UpdateColumn("stock", gorm.Expr("stock - ?", detail.Number)).
|
||||
Error
|
||||
if updateErr != nil {
|
||||
printer.Error(updateErr.Error())
|
||||
return updateErr
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
// 4. 清空购物车
|
||||
if err := tx.Where("passport_identity = ?", auth.Identity).
|
||||
Delete(&models.OrderCart{}).Error; err != nil {
|
||||
printer.Error(err.Error())
|
||||
return err
|
||||
}
|
||||
|
||||
return nil
|
||||
})
|
||||
|
||||
if err != nil {
|
||||
printer.Error(err.Error())
|
||||
return nil, errcode.ErrDB
|
||||
}
|
||||
|
||||
// 返回第一个订单号作为标识
|
||||
firstOrderNo := ""
|
||||
if len(summary) > 0 {
|
||||
firstOrderNo = summary[0].OrderNo
|
||||
}
|
||||
|
||||
return &pb.StatusReply{
|
||||
Code: 0,
|
||||
Identity: firstOrderNo,
|
||||
Message: strings.Join(keys, ","),
|
||||
Timeseq: time.Now().UnixNano(),
|
||||
}, nil
|
||||
}
|
||||
87
module/ec/order/internal/models/impl.go
Normal file
87
module/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
module/ec/order/internal/models/order_cart.go
Normal file
30
module/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
module/ec/order/internal/models/order_coupon.go
Normal file
26
module/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
module/ec/order/internal/models/order_details.go
Normal file
33
module/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
module/ec/order/internal/models/order_summary.go
Normal file
151
module/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
module/ec/order/internal/models/query.go
Normal file
41
module/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
module/ec/order/internal/models/types.go
Normal file
55
module/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"`
|
||||
}
|
||||
36
module/ec/order/internal/server/cart_server.go
Normal file
36
module/ec/order/internal/server/cart_server.go
Normal file
@@ -0,0 +1,36 @@
|
||||
// Code generated by protoc-gen-slc. DO NOT EDIT.
|
||||
package server
|
||||
|
||||
import (
|
||||
"context"
|
||||
"bsm/full/module/ec/order/internal/logic/cart"
|
||||
pb "bsm/full/module/ec/order/pb"
|
||||
)
|
||||
|
||||
type CartServer struct {
|
||||
pb.UnimplementedCartServer
|
||||
}
|
||||
|
||||
func NewCartServer() *CartServer {
|
||||
return &CartServer{}
|
||||
}
|
||||
|
||||
// 获取购物车的商品数据
|
||||
func (s *CartServer) Fetch(ctx context.Context, in *pb.CartGetRequest) (*pb.CartGetReply, error) {
|
||||
return cart.Fetch(ctx, in)
|
||||
}
|
||||
|
||||
// 将商品增加至购物车
|
||||
func (s *CartServer) Create(ctx context.Context, in *pb.CartAddRequest) (*pb.StatusReply, error) {
|
||||
return cart.Create(ctx, in)
|
||||
}
|
||||
|
||||
// 修改购物车中的商品数量
|
||||
func (s *CartServer) Modify(ctx context.Context, in *pb.CartSetRequest) (*pb.StatusReply, error) {
|
||||
return cart.Modify(ctx, in)
|
||||
}
|
||||
|
||||
// 删除购物车中的商品
|
||||
func (s *CartServer) Delete(ctx context.Context, in *pb.CartDelRequest) (*pb.StatusReply, error) {
|
||||
return cart.Delete(ctx, in)
|
||||
}
|
||||
21
module/ec/order/internal/server/coupon_server.go
Normal file
21
module/ec/order/internal/server/coupon_server.go
Normal file
@@ -0,0 +1,21 @@
|
||||
// Code generated by protoc-gen-slc. DO NOT EDIT.
|
||||
package server
|
||||
|
||||
import (
|
||||
"context"
|
||||
"bsm/full/module/ec/order/internal/logic/coupon"
|
||||
pb "bsm/full/module/ec/order/pb"
|
||||
)
|
||||
|
||||
type CouponServer struct {
|
||||
pb.UnimplementedCouponServer
|
||||
}
|
||||
|
||||
func NewCouponServer() *CouponServer {
|
||||
return &CouponServer{}
|
||||
}
|
||||
|
||||
// 按状态获取优惠卷
|
||||
func (s *CouponServer) ByStatus(ctx context.Context, in *pb.Status) (*pb.CouponListReply, error) {
|
||||
return coupon.ByStatus(ctx, in)
|
||||
}
|
||||
51
module/ec/order/internal/server/mgt_server.go
Normal file
51
module/ec/order/internal/server/mgt_server.go
Normal file
@@ -0,0 +1,51 @@
|
||||
// Code generated by protoc-gen-slc. DO NOT EDIT.
|
||||
package server
|
||||
|
||||
import (
|
||||
"context"
|
||||
"bsm/full/module/ec/order/internal/logic/mgt"
|
||||
pb "bsm/full/module/ec/order/pb"
|
||||
)
|
||||
|
||||
type MgtServer struct {
|
||||
pb.UnimplementedMgtServer
|
||||
}
|
||||
|
||||
func NewMgtServer() *MgtServer {
|
||||
return &MgtServer{}
|
||||
}
|
||||
|
||||
// 创建订单
|
||||
func (s *MgtServer) OrderCreate(ctx context.Context, in *pb.CreateOrderRequest) (*pb.StatusReply, error) {
|
||||
return mgt.OrderCreate(ctx, in)
|
||||
}
|
||||
|
||||
// 修改订单
|
||||
func (s *MgtServer) OrderModify(ctx context.Context, in *pb.OrderSummaryItem) (*pb.StatusReply, error) {
|
||||
return mgt.OrderModify(ctx, in)
|
||||
}
|
||||
|
||||
// 获取一个订单的详情数据
|
||||
func (s *MgtServer) OrderGet(ctx context.Context, in *pb.IdentRequest) (*pb.OrderGetReply, error) {
|
||||
return mgt.OrderGet(ctx, in)
|
||||
}
|
||||
|
||||
// 根据不同的类型获取店铺的订单列表
|
||||
func (s *MgtServer) OrderListByStore(ctx context.Context, in *pb.OrderListByStoreRequest) (*pb.OrderListByStoreReply, error) {
|
||||
return mgt.OrderListByStore(ctx, in)
|
||||
}
|
||||
|
||||
// 取消订单
|
||||
func (s *MgtServer) OrderCancel(ctx context.Context, in *pb.IdentRequest) (*pb.StatusReply, error) {
|
||||
return mgt.OrderCancel(ctx, in)
|
||||
}
|
||||
|
||||
// 退货
|
||||
func (s *MgtServer) OrderReturnable(ctx context.Context, in *pb.IdentRequest) (*pb.StatusReply, error) {
|
||||
return mgt.OrderReturnable(ctx, in)
|
||||
}
|
||||
|
||||
// 订单审批
|
||||
func (s *MgtServer) OrderApprove(ctx context.Context, in *pb.IdentRequest) (*pb.StatusReply, error) {
|
||||
return mgt.OrderApprove(ctx, in)
|
||||
}
|
||||
77
module/ec/order/internal/server/new.go
Normal file
77
module/ec/order/internal/server/new.go
Normal file
@@ -0,0 +1,77 @@
|
||||
// Code generated by protoc-gen-slc. DO NOT EDIT.
|
||||
package server
|
||||
|
||||
import (
|
||||
"context"
|
||||
pb "bsm/full/module/ec/order/pb"
|
||||
"git.apinb.com/bsm-sdk/core/vars"
|
||||
gwRuntime "github.com/grpc-ecosystem/grpc-gateway/v2/runtime"
|
||||
"google.golang.org/grpc"
|
||||
"google.golang.org/grpc/reflection"
|
||||
"google.golang.org/protobuf/proto"
|
||||
"net/http"
|
||||
"os"
|
||||
"strings"
|
||||
)
|
||||
|
||||
type Server struct {
|
||||
Grpc *grpc.Server
|
||||
Ctx context.Context
|
||||
Mux *gwRuntime.ServeMux
|
||||
}
|
||||
|
||||
func New(addr string) *Server {
|
||||
srv := &Server{Ctx: context.Background(), Grpc: grpc.NewServer(), Mux: gwRuntime.NewServeMux(
|
||||
gwRuntime.WithForwardResponseRewriter(responseEnvelope),
|
||||
)}
|
||||
|
||||
// register service to grpc.Server
|
||||
pb.RegisterCartServer(srv.Grpc, NewCartServer())
|
||||
pb.RegisterCouponServer(srv.Grpc, NewCouponServer())
|
||||
pb.RegisterMgtServer(srv.Grpc, NewMgtServer())
|
||||
pb.RegisterSummaryServer(srv.Grpc, NewSummaryServer())
|
||||
|
||||
reflection.Register(srv.Grpc)
|
||||
|
||||
// 将服务注册到Gateway
|
||||
opts := []grpc.DialOption{grpc.WithInsecure()}
|
||||
pb.RegisterCartHandlerFromEndpoint(srv.Ctx, srv.Mux, addr, opts)
|
||||
pb.RegisterCouponHandlerFromEndpoint(srv.Ctx, srv.Mux, addr, opts)
|
||||
pb.RegisterMgtHandlerFromEndpoint(srv.Ctx, srv.Mux, addr, opts)
|
||||
pb.RegisterSummaryHandlerFromEndpoint(srv.Ctx, srv.Mux, addr, opts)
|
||||
|
||||
// Register services swagger
|
||||
srv.RegisterSwagger()
|
||||
|
||||
return srv
|
||||
}
|
||||
|
||||
// RegisterSwagger 注册swagger
|
||||
func (s *Server) RegisterSwagger() {
|
||||
srvKey := strings.ToLower(vars.ServiceKey)
|
||||
s.Mux.HandlePath("GET", "/"+srvKey+".swagger.json", func(w http.ResponseWriter, r *http.Request, pathParams map[string]string) {
|
||||
w.Header().Set("Content-Type", "application/json")
|
||||
bytes, err := os.ReadFile("./swagger/" + srvKey + ".swagger.json")
|
||||
if err != nil {
|
||||
w.WriteHeader(http.StatusNotFound)
|
||||
w.Write([]byte(err.Error()))
|
||||
return
|
||||
}
|
||||
w.Write(bytes)
|
||||
return
|
||||
|
||||
})
|
||||
}
|
||||
|
||||
// response envelope
|
||||
func responseEnvelope(_ context.Context, response proto.Message) (interface{}, error) {
|
||||
name := string(response.ProtoReflect().Descriptor().Name())
|
||||
if name == "Status" || name == "Error" || name == "StatusReply" {
|
||||
return response, nil
|
||||
}
|
||||
return map[string]any{
|
||||
"code": 0,
|
||||
"message": "OK",
|
||||
"result": response,
|
||||
}, nil
|
||||
}
|
||||
66
module/ec/order/internal/server/summary_server.go
Normal file
66
module/ec/order/internal/server/summary_server.go
Normal file
@@ -0,0 +1,66 @@
|
||||
// Code generated by protoc-gen-slc. DO NOT EDIT.
|
||||
package server
|
||||
|
||||
import (
|
||||
"context"
|
||||
"bsm/full/module/ec/order/internal/logic/summary"
|
||||
pb "bsm/full/module/ec/order/pb"
|
||||
)
|
||||
|
||||
type SummaryServer struct {
|
||||
pb.UnimplementedSummaryServer
|
||||
}
|
||||
|
||||
func NewSummaryServer() *SummaryServer {
|
||||
return &SummaryServer{}
|
||||
}
|
||||
|
||||
// 快速创建一个店铺订单
|
||||
func (s *SummaryServer) QuickCreateByProduct(ctx context.Context, in *pb.QuickCreateByProductRequest) (*pb.StatusReply, error) {
|
||||
return summary.QuickCreateByProduct(ctx, in)
|
||||
}
|
||||
|
||||
// 将购物车的数据提交生成订单
|
||||
func (s *SummaryServer) Submit(ctx context.Context, in *pb.SubmitRequest) (*pb.StatusReply, error) {
|
||||
return summary.Submit(ctx, in)
|
||||
}
|
||||
|
||||
// 检测是否有未确认及付款的订单
|
||||
func (s *SummaryServer) Check(ctx context.Context, in *pb.Empty) (*pb.StatusReply, error) {
|
||||
return summary.Check(ctx, in)
|
||||
}
|
||||
|
||||
// 获取一个订单的详情数据
|
||||
func (s *SummaryServer) Get(ctx context.Context, in *pb.IdentRequest) (*pb.SummaryGetReply, error) {
|
||||
return summary.Get(ctx, in)
|
||||
}
|
||||
|
||||
// 根据不同的类型获取我的订单列表
|
||||
func (s *SummaryServer) List(ctx context.Context, in *pb.SummaryListRequest) (*pb.SummaryListReply, error) {
|
||||
return summary.List(ctx, in)
|
||||
}
|
||||
|
||||
// 确认订单,物流,优惠卷等其它信息
|
||||
func (s *SummaryServer) Confirm(ctx context.Context, in *pb.ConfirmRequest) (*pb.ConfirmReply, error) {
|
||||
return summary.Confirm(ctx, in)
|
||||
}
|
||||
|
||||
// 取消订单
|
||||
func (s *SummaryServer) Cancel(ctx context.Context, in *pb.CancelRequest) (*pb.StatusReply, error) {
|
||||
return summary.Cancel(ctx, in)
|
||||
}
|
||||
|
||||
// 模拟支付
|
||||
func (s *SummaryServer) SimulatePay(ctx context.Context, in *pb.SimulatePayRequest) (*pb.StatusReply, error) {
|
||||
return summary.SimulatePay(ctx, in)
|
||||
}
|
||||
|
||||
// 模拟发货
|
||||
func (s *SummaryServer) SimulateShipments(ctx context.Context, in *pb.SimulateShipmentsRequest) (*pb.StatusReply, error) {
|
||||
return summary.SimulateShipments(ctx, in)
|
||||
}
|
||||
|
||||
// 模拟收货
|
||||
func (s *SummaryServer) SimulateReceiving(ctx context.Context, in *pb.IdentRequest) (*pb.StatusReply, error) {
|
||||
return summary.SimulateReceiving(ctx, in)
|
||||
}
|
||||
Reference in New Issue
Block a user