refactor: reorganize modules and add Linux build tooling
This commit is contained in:
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
|
||||
|
||||
}
|
||||
Reference in New Issue
Block a user