修改
This commit is contained in:
parent
7b3e4f3522
commit
af14bcf74b
@ -2,18 +2,19 @@ package dao
|
||||
|
||||
import (
|
||||
"encoding/json"
|
||||
"gorm.io/gorm"
|
||||
"micro-bundle/internal/model"
|
||||
"micro-bundle/pb/bundle"
|
||||
"micro-bundle/pkg/app"
|
||||
commonErr "micro-bundle/pkg/err"
|
||||
"micro-bundle/pkg/msg"
|
||||
"micro-bundle/pkg/utils"
|
||||
"time"
|
||||
)
|
||||
|
||||
func CreateOrderRecord(orderRecord *model.BundleOrderRecords, req *bundle.OrderRecord) (res *bundle.CommonResponse, err error) {
|
||||
res = new(bundle.CommonResponse)
|
||||
bundleInfo := new(model.BundleProfile)
|
||||
|
||||
// 生成UUID和订单号
|
||||
orderRecord.UUID = app.ModuleClients.SfNode.Generate().Base64()
|
||||
orderRecord.OrderNo = utils.GetOrderNo()
|
||||
@ -40,7 +41,7 @@ func CreateOrderRecord(orderRecord *model.BundleOrderRecords, req *bundle.OrderR
|
||||
|
||||
// 填充BundleCommonJson字段
|
||||
if bundleJson, e := json.Marshal(bundleInfo); e == nil {
|
||||
orderRecord.BundleCommonJson = string(bundleJson)
|
||||
orderRecord.BundleCommonJson = bundleJson
|
||||
} else {
|
||||
tx.Rollback()
|
||||
res.Msg = msg.ErrorDataConvert
|
||||
@ -53,39 +54,56 @@ func CreateOrderRecord(orderRecord *model.BundleOrderRecords, req *bundle.OrderR
|
||||
res.Msg = msg.ErrorCreateOrderInfo
|
||||
return res, commonErr.ReturnError(err, msg.ErrorCreateOrderInfo, "创建订单信息失败: ")
|
||||
}
|
||||
|
||||
// 创建子订单
|
||||
var (
|
||||
childOrders []*model.BundleOrderValueAdd
|
||||
valueAddAmount float64
|
||||
expirationTime = "9999-12-31"
|
||||
)
|
||||
for _, service := range bundleInfo.BundleToValueAddService {
|
||||
amount, PriceType, serviceType, e := calculateAmount(service.ValueUid, req, req.Language)
|
||||
amount, info, num, day, e := calculateAmount(service.ValueUid, req, req.Language)
|
||||
if e != nil {
|
||||
tx.Rollback()
|
||||
res.Msg = msg.ErrorDataConvert
|
||||
return res, commonErr.ReturnError(e, msg.ErrorDataConvert, "子订单金额计算失败: ")
|
||||
}
|
||||
|
||||
if day != "" {
|
||||
expirationTime = day
|
||||
}
|
||||
valueAddAmount = valueAddAmount + amount
|
||||
childOrder := &model.BundleOrderValueAdd{
|
||||
UUID: app.ModuleClients.SfNode.Generate().Base64(),
|
||||
OrderUUID: orderRecord.UUID, // 修正: 这里应使用主订单UUID
|
||||
CustomerID: orderRecord.CustomerID,
|
||||
CustomerNum: orderRecord.CustomerNum,
|
||||
CustomerName: orderRecord.CustomerName,
|
||||
ServiceType: serviceType,
|
||||
CurrencyType: PriceType,
|
||||
ServiceType: info.ServiceType,
|
||||
CurrencyType: info.PriceType,
|
||||
Amount: amount,
|
||||
Num: 0,
|
||||
Unit: 0,
|
||||
Num: num,
|
||||
Unit: info.Unit,
|
||||
ValueAddUUID: service.ValueUid,
|
||||
Source: 1,
|
||||
PaymentStatus: 1,
|
||||
}
|
||||
|
||||
if err = tx.Model(&model.BundleOrderValueAdd{}).Create(childOrder).Error; err != nil {
|
||||
tx.Rollback()
|
||||
res.Msg = msg.ErrorCreateOrderInfo
|
||||
return res, commonErr.ReturnError(err, msg.ErrorCreateOrderInfo, "创建子订单信息失败: ")
|
||||
}
|
||||
childOrders = append(childOrders, childOrder)
|
||||
}
|
||||
// 批量创建子订单(提高性能)
|
||||
if err = tx.Model(&model.BundleOrderValueAdd{}).Create(childOrders).Error; err != nil {
|
||||
tx.Rollback()
|
||||
return res, commonErr.ReturnError(err, msg.ErrorCreateOrderInfo, "批量创建子订单失败")
|
||||
}
|
||||
// 更新总金额
|
||||
updateData := map[string]interface{}{
|
||||
"total_amount": gorm.Expr("total_amount + ?", valueAddAmount),
|
||||
"updated_at": time.Now(),
|
||||
"expiration_time": expirationTime,
|
||||
}
|
||||
if err = tx.Model(&model.BundleOrderRecords{}).
|
||||
Where("uuid = ?", orderRecord.UUID).
|
||||
Updates(updateData).Error; err != nil {
|
||||
tx.Rollback()
|
||||
return res, commonErr.ReturnError(err, msg.ErrorUpdateOrderInfo, "更新订单总金额失败")
|
||||
}
|
||||
|
||||
// 提交事务
|
||||
if err = tx.Commit().Error; err != nil {
|
||||
res.Msg = msg.ErrorCommitTransaction
|
||||
@ -99,28 +117,45 @@ func CreateOrderRecord(orderRecord *model.BundleOrderRecords, req *bundle.OrderR
|
||||
}
|
||||
|
||||
// calculateAmount 计算子订单金额
|
||||
func calculateAmount(valueUid string, req *bundle.OrderRecord, language string) (float64, int64, int32, error) {
|
||||
var valueAddServiceLang *model.ValueAddServiceLang
|
||||
err := app.ModuleClients.BundleDB.
|
||||
func calculateAmount(valueUid string, req *bundle.OrderRecord, language string) (amount float64, valueAddServiceLang *model.ValueAddServiceLang, num int32, expirationDay string, err error) {
|
||||
err = app.ModuleClients.BundleDB.
|
||||
Where("uuid = ? AND language = ?", valueUid, language).
|
||||
First(&valueAddServiceLang).Error
|
||||
if err != nil {
|
||||
return 0, valueAddServiceLang.PriceType, valueAddServiceLang.ServiceType, err
|
||||
return
|
||||
}
|
||||
|
||||
var amount float64
|
||||
for _, opt := range valueAddServiceLang.Options {
|
||||
if valueAddServiceLang.PriceMode == 1 {
|
||||
for _, p := range req.PriceOptionsInfo {
|
||||
if p.ValueUid == valueUid && opt.Id == p.Id {
|
||||
amount = float64(float32(opt.Num) * opt.Price)
|
||||
num = opt.Num
|
||||
}
|
||||
}
|
||||
} else if valueAddServiceLang.PriceMode == 2 {
|
||||
amount = float64(opt.Price)
|
||||
}
|
||||
// 计算过期时间
|
||||
if valueAddServiceLang.ServiceType == 5 {
|
||||
expirationDay = calculateExpirationDay(opt.Num, valueAddServiceLang.Unit)
|
||||
}
|
||||
}
|
||||
return
|
||||
}
|
||||
|
||||
// calculateExpirationDay 计算到期日
|
||||
func calculateExpirationDay(num int32, unit string) string {
|
||||
now := time.Now()
|
||||
switch unit {
|
||||
case "天":
|
||||
return now.AddDate(0, 0, int(num)).Format("2006-01-02")
|
||||
case "月":
|
||||
return now.AddDate(0, int(num), 0).Format("2006-01-02")
|
||||
case "年":
|
||||
return now.AddDate(int(num), 0, 0).Format("2006-01-02")
|
||||
default:
|
||||
return ""
|
||||
}
|
||||
return amount, valueAddServiceLang.PriceType, valueAddServiceLang.ServiceType, nil
|
||||
}
|
||||
func UpdateOrderRecord(orderRecord *model.BundleOrderRecords) (res *bundle.CommonResponse, err error) {
|
||||
res = new(bundle.CommonResponse)
|
||||
|
@ -1,41 +1,44 @@
|
||||
package model
|
||||
|
||||
import "gorm.io/gorm"
|
||||
import (
|
||||
"encoding/json"
|
||||
"gorm.io/gorm"
|
||||
)
|
||||
|
||||
// BundleOrderRecords struct
|
||||
type BundleOrderRecords struct {
|
||||
gorm.Model
|
||||
UUID string `json:"uuid" gorm:"column:uuid;type:varchar(1024);comment:UUID"`
|
||||
OrderNo string `json:"orderNo" gorm:"column:order_no;type:varchar(1024);comment:交易编号"`
|
||||
BundleUUID string `json:"bundleUUID" gorm:"column:bundle_uuid;type:varchar(1024);comment:套餐UUID"`
|
||||
BundleName string `json:"bundleName" gorm:"column:bundle_name;type:varchar(2048);comment:套餐名"`
|
||||
CustomerID string `json:"customerID" gorm:"column:customer_id;type:varchar(1024);comment:客户ID"`
|
||||
CustomerNum string `json:"customerNum" gorm:"column:customer_num;type:varchar(1024);comment:客户编号"`
|
||||
CustomerName string `json:"customerName" gorm:"column:customer_name;type:varchar(1024);comment:客户名"`
|
||||
Amount float32 `json:"amount" gorm:"column:amount;type:decimal(12,2);comment:套餐金额"`
|
||||
AmountType int64 `json:"amountType" gorm:"column:amount_type;type:int;comment:金额类型"`
|
||||
ValueAddBundleUUID string `json:"valueAddBundleUUID" gorm:"column:value_add_bundle_uuid;type:varchar(1024);comment:增值套餐记录UUID"`
|
||||
ValueAddBundleAmount float32 `json:"valueAddBundleAmount" gorm:"column:value_add_bundle_amount;type:decimal(12,2);comment:增值套餐金额"`
|
||||
ValueAddOriginalPrice float32 `json:"valueAddOriginalPrice" gorm:"column:value_add_original_price;type:decimal(12,2);comment:原单价"`
|
||||
ValueAddDiscountPrice float32 `json:"valueAddDiscountPrice" gorm:"column:value_add_discount_price;type:decimal(12,2);comment:优惠单价"`
|
||||
ValueAddSavedAmount float32 `json:"valueAddSavedAmount" gorm:"column:value_add_saved_amount;type:decimal(12,2);comment:节省金额"`
|
||||
TotalAmount float32 `json:"totalAmount" gorm:"column:total_amount;type:decimal(12,2);comment:总金额"`
|
||||
Num int32 `json:"num" gorm:"column:num;type:int;comment:视频数量"`
|
||||
SignContract string `json:"signContract" gorm:"column:sign_contract;type:varchar(1024);comment:签约合同"`
|
||||
Signature string `json:"signature" gorm:"column:signature;type:text;comment:签字"`
|
||||
SignedTime string `json:"signedTime" gorm:"column:signed_time;type:varchar(1024);comment:签约时间(北京时间)"`
|
||||
PayType int64 `json:"payType" gorm:"column:pay_type;type:int;comment:支付类型"`
|
||||
PayTime string `json:"payTime" gorm:"column:pay_time;type:varchar(1024);comment:支付时间(北京时间)"`
|
||||
CheckoutSessionId string `json:"checkoutSessionId" gorm:"column:checkout_session_id;type:varchar(1024);default:null;comment:checkoutSessionId"`
|
||||
CheckoutSessionUrl string `json:"checkoutSessionUrl" gorm:"column:checkout_session_url;type:varchar(1024);default:null;comment:checkoutSessionUrl"`
|
||||
Status int64 `json:"status" gorm:"column:status;type:int;comment:状态 1:已签未支付 2:已签已支付"`
|
||||
ContractNo string `json:"contractNo" gorm:"column:contract_no;type:varchar(1024);comment:合同编号"`
|
||||
BundleCommonUid string `json:"bundleCommonUid" gorm:"column:bundle_common_uid;type:text;comment:套餐公共ID"`
|
||||
AddBundleCommonUid string `json:"addBundleCommonUid" gorm:"column:add_bundle_common_uid;type:text;comment:附加套餐公共ID"`
|
||||
FinancialConfirmation int32 `json:"financialConfirmation" gorm:"column:financial_confirmation;type:int;comment:财务确认 1:未确认 2:已确认"`
|
||||
ExpirationTime string `json:"expirationTime" gorm:"column:expiration_time;comment:套餐过期时间"`
|
||||
BundleCommonJson string `json:"bundle_common_json" gorm:"column:bundle_common_json;type:json;serializer:json;comment:套餐信息"`
|
||||
Language string `gorm:"column:language;comment:语言" json:"language"`
|
||||
UUID string `json:"uuid" gorm:"column:uuid;type:varchar(1024);comment:UUID"`
|
||||
OrderNo string `json:"orderNo" gorm:"column:order_no;type:varchar(1024);comment:交易编号"`
|
||||
BundleUUID string `json:"bundleUUID" gorm:"column:bundle_uuid;type:varchar(1024);comment:套餐UUID"`
|
||||
BundleName string `json:"bundleName" gorm:"column:bundle_name;type:varchar(2048);comment:套餐名"`
|
||||
CustomerID string `json:"customerID" gorm:"column:customer_id;type:varchar(1024);comment:客户ID"`
|
||||
CustomerNum string `json:"customerNum" gorm:"column:customer_num;type:varchar(1024);comment:客户编号"`
|
||||
CustomerName string `json:"customerName" gorm:"column:customer_name;type:varchar(1024);comment:客户名"`
|
||||
Amount float32 `json:"amount" gorm:"column:amount;type:decimal(12,2);comment:套餐金额"`
|
||||
AmountType int64 `json:"amountType" gorm:"column:amount_type;type:int;comment:金额类型"`
|
||||
ValueAddBundleUUID string `json:"valueAddBundleUUID" gorm:"column:value_add_bundle_uuid;type:varchar(1024);comment:增值套餐记录UUID"`
|
||||
ValueAddBundleAmount float32 `json:"valueAddBundleAmount" gorm:"column:value_add_bundle_amount;type:decimal(12,2);comment:增值套餐金额"`
|
||||
ValueAddOriginalPrice float32 `json:"valueAddOriginalPrice" gorm:"column:value_add_original_price;type:decimal(12,2);comment:原单价"`
|
||||
ValueAddDiscountPrice float32 `json:"valueAddDiscountPrice" gorm:"column:value_add_discount_price;type:decimal(12,2);comment:优惠单价"`
|
||||
ValueAddSavedAmount float32 `json:"valueAddSavedAmount" gorm:"column:value_add_saved_amount;type:decimal(12,2);comment:节省金额"`
|
||||
TotalAmount float32 `json:"totalAmount" gorm:"column:total_amount;type:decimal(12,2);comment:总金额"`
|
||||
Num int32 `json:"num" gorm:"column:num;type:int;comment:视频数量"`
|
||||
SignContract string `json:"signContract" gorm:"column:sign_contract;type:varchar(1024);comment:签约合同"`
|
||||
Signature string `json:"signature" gorm:"column:signature;type:text;comment:签字"`
|
||||
SignedTime string `json:"signedTime" gorm:"column:signed_time;type:varchar(1024);comment:签约时间(北京时间)"`
|
||||
PayType int64 `json:"payType" gorm:"column:pay_type;type:int;comment:支付类型"`
|
||||
PayTime string `json:"payTime" gorm:"column:pay_time;type:varchar(1024);comment:支付时间(北京时间)"`
|
||||
CheckoutSessionId string `json:"checkoutSessionId" gorm:"column:checkout_session_id;type:varchar(1024);default:null;comment:checkoutSessionId"`
|
||||
CheckoutSessionUrl string `json:"checkoutSessionUrl" gorm:"column:checkout_session_url;type:varchar(1024);default:null;comment:checkoutSessionUrl"`
|
||||
Status int64 `json:"status" gorm:"column:status;type:int;comment:状态 1:已签未支付 2:已签已支付"`
|
||||
ContractNo string `json:"contractNo" gorm:"column:contract_no;type:varchar(1024);comment:合同编号"`
|
||||
BundleCommonUid string `json:"bundleCommonUid" gorm:"column:bundle_common_uid;type:text;comment:套餐公共ID"`
|
||||
AddBundleCommonUid string `json:"addBundleCommonUid" gorm:"column:add_bundle_common_uid;type:text;comment:附加套餐公共ID"`
|
||||
FinancialConfirmation int32 `json:"financialConfirmation" gorm:"column:financial_confirmation;type:int;comment:财务确认 1:未确认 2:已确认"`
|
||||
ExpirationTime string `json:"expirationTime" gorm:"column:expiration_time;comment:套餐过期时间"`
|
||||
BundleCommonJson json.RawMessage `json:"bundle_common_json" gorm:"column:bundle_common_json;type:json;serializer:json;comment:套餐信息"`
|
||||
Language string `gorm:"column:language;comment:语言" json:"language"`
|
||||
}
|
||||
type BundleOrderValueAdd struct {
|
||||
gorm.Model
|
||||
@ -48,7 +51,7 @@ type BundleOrderValueAdd struct {
|
||||
CurrencyType int64 `json:"currencyType" gorm:"column:currency_type;type:int;comment:货币类型"`
|
||||
Amount float64 `json:"amount" gorm:"column:amount;type:decimal(12,2);comment:金额"`
|
||||
Num int32 `json:"num" gorm:"column:num;type:int;comment:数量"`
|
||||
Unit int32 `json:"unit" gorm:"column:unit;type:varchar(1024);comment:单位 1个 2条 3天 4月 5年"`
|
||||
Unit string `json:"unit" gorm:"column:unit;type:varchar(1024);comment:单位 1个 2条 3天 4月 5年"`
|
||||
ValueAddUUID string `json:"valueAddUUID" gorm:"column:value_add_uuid;type:varchar(1024);comment:增值服务UUID"`
|
||||
Source int `json:"source" gorm:"column:source;comment:增加方式 1套餐 2单独购买 3拓展"`
|
||||
Remark string `json:"remark" gorm:"column:remark;comment:备注"`
|
||||
|
Loading…
Reference in New Issue
Block a user