diff --git a/internal/controller/bundle.go b/internal/controller/bundle.go index 6a01c3e..87e03b6 100644 --- a/internal/controller/bundle.go +++ b/internal/controller/bundle.go @@ -134,3 +134,10 @@ func (b *BundleProvider) SoftDeleteUnfinishedInfo(_ context.Context, req *bundle func (b *BundleProvider) ReSignTheContract(_ context.Context, req *bundle.ReSignTheContractRequest) (res *bundle.CommonResponse, err error) { return logic.ReSignTheContract(req) } +func (b *BundleProvider) OrderListByOrderUuid(_ context.Context, req *bundle.OrderInfoByOrderUuidRequest) (res *bundle.OrderInfoByOrderNoResp, err error) { + return logic.OrderListByOrderUuid(req) +} + +func (b *BundleProvider) UpdateOrderRecordByOrderUuid(_ context.Context, req *bundle.OrderRecord) (res *bundle.CommonResponse, err error) { + return logic.UpdateOrderRecordByOrderUuid(req) +} diff --git a/internal/dao/orderRecordsDao.go b/internal/dao/orderRecordsDao.go index 213d94b..c12e632 100644 --- a/internal/dao/orderRecordsDao.go +++ b/internal/dao/orderRecordsDao.go @@ -1003,3 +1003,108 @@ func ReSignTheContract(req *bundle.ReSignTheContractRequest) (*bundle.CommonResp return res, nil } + +func UpdateOrderRecordByOrderUuid(orderRecord *model.BundleOrderRecords) (res *bundle.CommonResponse, err error) { + res = new(bundle.CommonResponse) + // Step 1: 先更新子订单(增值服务)的支付状态 + valueAdd := &model.BundleOrderValueAdd{ + PaymentStatus: int(orderRecord.Status), + PaymentTime: orderRecord.PayTime, + } + if orderRecord.CheckoutSessionId != "" { + valueAdd.CheckoutSessionId = orderRecord.CheckoutSessionId + valueAdd.CheckoutSessionUrl = orderRecord.CheckoutSessionUrl + } + + if orderRecord.Status == 2 { + tempValues := make([]*model.BundleOrderValueAdd, 0) + err = app.ModuleClients.BundleDB.Model(&model.BundleOrderValueAdd{}). + Where("order_uuid = ?", orderRecord.UUID). + Find(&tempValues).Error + if len(tempValues) > 0 { + for _, value := range tempValues { + if value.PaymentStatus == 2 { + fmt.Println("====================已经付过了") + return nil, errors.New("已付款了") + } + } + } + + tempRecord := new(model.BundleOrderRecords) + if err := app.ModuleClients.BundleDB.Where("deleted_at is null and uuid = ?", orderRecord.UUID).First(&tempRecord).Error; err != nil { + if errors.Is(err, gorm.ErrRecordNotFound) { + return nil, errors.New("订单记录不存在") + } + return nil, fmt.Errorf("查询订单失败: %v", err) + } + if orderRecord.Status == 2 && tempRecord.AmountType == 2 && tempRecord.TotalAmount > 0 { + // 当回调支付成功,币种是美元,且订单金额大于0,计算美元手续费:订单金额*0.019(四舍五入保留两位小数字)+0.1 + amount := decimal.NewFromFloat32(tempRecord.TotalAmount) + rate, _ := decimal.NewFromString("0.019") + fee := amount.Mul(rate) + // 4. 四舍五入保留两位小数 + feeRounded := fee.Round(2) + addition, _ := decimal.NewFromString("0.1") + result := feeRounded.Add(addition) + valueAdd.HandlingFee = result.String() + } + + } + + err = app.ModuleClients.BundleDB.Model(&model.BundleOrderValueAdd{}). + Where("order_uuid = ?", orderRecord.UUID). + Updates(valueAdd).Error + if err != nil { + res.Msg = msg.ErrorUpdateOrderInfo + return res, commonErr.ReturnError(err, msg.ErrorUpdateOrderInfo, "更新增值服务支付状态失败: ") + } + // Step 2: 再更新主订单信息(如果存在) + err = app.ModuleClients.BundleDB.Model(&model.BundleOrderRecords{}). + Where("uuid = ?", orderRecord.UUID). + Updates(orderRecord).Error + // Step 3: 返回结果(即使主订单更新失败,也视为成功) + res.Uuid = orderRecord.UUID + res.Msg = msg.SuccessUpdateOrderInfo + return res, nil +} +func OrderListByOrderUuid(req *bundle.OrderInfoByOrderUuidRequest) (*bundle.OrderInfoByOrderNoResp, error) { + if req == nil || req.OrderUuid == "" { + return nil, errors.New("invalid request") + } + var addOrder []*model.BundleOrderValueAdd + if err := app.ModuleClients.BundleDB.Model(&model.BundleOrderValueAdd{}). + Where("order_uuid = ?", req.OrderUuid). + Find(&addOrder).Error; err != nil { + return nil, err + } + if len(addOrder) == 0 { + return &bundle.OrderInfoByOrderNoResp{}, nil + } + //订单类型 1:套餐 2:单独购买 + userID, err := strconv.ParseInt(addOrder[0].CustomerID, 10, 64) + if err != nil { + return nil, err + } + res := &bundle.OrderInfoByOrderNoResp{ + Type: int32(addOrder[0].Source), + UserId: uint64(userID), + OrderUUID: addOrder[0].OrderUUID, + UserName: addOrder[0].CustomerName, + } + for _, item := range addOrder { + switch item.ServiceType { + case 1: + res.VideoNumber += item.Num // 使用 += 而不是直接赋值 + case 2: + res.ImageNumber += item.Num + case 3: + res.DataNumber += item.Num + case 4: + res.AccountNumber += item.Num + case 5: + res.Duration += item.Num + res.Unit = item.Unit // 如果有多个duration记录,最后一个unit会覆盖前面的 + } + } + return res, nil +} diff --git a/internal/logic/orderRecordsLogic.go b/internal/logic/orderRecordsLogic.go index d555b25..263e388 100644 --- a/internal/logic/orderRecordsLogic.go +++ b/internal/logic/orderRecordsLogic.go @@ -2,6 +2,8 @@ package logic import ( "errors" + "fmt" + uuid "github.com/satori/go.uuid" "micro-bundle/internal/dao" "micro-bundle/internal/model" "micro-bundle/pb/bundle" @@ -14,7 +16,13 @@ import ( func CreateOrderRecord(req *bundle.OrderCreateRecord) (res *bundle.CommonResponse, err error) { res = new(bundle.CommonResponse) - orderUUID := app.ModuleClients.SfNode.Generate().Base64() + //orderUUID := app.ModuleClients.SfNode.Generate().Base64() + uuidV4, err := uuid.NewV4() + if err != nil { + fmt.Println("生成错误", err) + return nil, errors.New("生成uuid失败") + } + orderUUID := uuidV4.String() orderNo := utils.GetOrderNo() if req.OrderNo != "" { orderNo = req.OrderNo @@ -95,6 +103,8 @@ func UpdateOrderRecordByOrderNo(req *bundle.OrderRecord) (res *bundle.CommonResp _ = copier.CopyWithOption(&orderRecord, req, copier.Option{DeepCopy: true}) orderRecord.UUID = req.Uuid orderRecord.BundleUUID = req.BundleUuid + orderRecord.Status = req.Status + fmt.Println("UpdateOrderRecordByOrderNo Status:", req.Status) res, err = dao.UpdateOrderRecordByOrderNO(orderRecord) return } @@ -205,3 +215,20 @@ func ReSignTheContract(req *bundle.ReSignTheContractRequest) (res *bundle.Common res, err = dao.ReSignTheContract(req) return } + +func UpdateOrderRecordByOrderUuid(req *bundle.OrderRecord) (res *bundle.CommonResponse, err error) { + res = new(bundle.CommonResponse) + orderRecord := new(model.BundleOrderRecords) + _ = copier.CopyWithOption(&orderRecord, req, copier.Option{DeepCopy: true}) + orderRecord.UUID = req.Uuid + orderRecord.BundleUUID = req.BundleUuid + orderRecord.Status = req.Status + fmt.Println("UpdateOrderRecordByOrderUuid Status:", req.Status) + res, err = dao.UpdateOrderRecordByOrderUuid(orderRecord) + return +} +func OrderListByOrderUuid(req *bundle.OrderInfoByOrderUuidRequest) (res *bundle.OrderInfoByOrderNoResp, err error) { + res = new(bundle.OrderInfoByOrderNoResp) + res, err = dao.OrderListByOrderUuid(req) + return +} diff --git a/pb/bundle.proto b/pb/bundle.proto index f3be63b..390292e 100644 --- a/pb/bundle.proto +++ b/pb/bundle.proto @@ -141,6 +141,10 @@ service Bundle { rpc GetContractDetail(ContractDetailRequest) returns (ContractDetailResponse) {} // 根据合同UUID查询详情 rpc GetDevelopmentCyclesByContractUUID(GetDevelopmentCyclesByContractUUIDRequest) returns (GetDevelopmentCyclesByContractUUIDResponse) {} // 根据合同UUID查询开发周期 rpc GetPaymentCyclesByContractUUID(GetPaymentCyclesByContractUUIDRequest) returns (GetPaymentCyclesByContractUUIDResponse) {} // 根据合同UUID查询支付周期 + + + rpc UpdateOrderRecordByOrderUuid(OrderRecord) returns (CommonResponse) {} + rpc OrderListByOrderUuid(OrderInfoByOrderUuidRequest) returns (OrderInfoByOrderNoResp) {} } message GetInEffectOrderRecordRequest{ uint64 userID = 1; @@ -720,13 +724,13 @@ message BundleExtendRequest{ uint32 imagesAdditional = 4; //图文 uint32 dataAdditional = 5;//数据分析 uint32 competitiveAdditional = 6;//竞品数 - uint32 availableDurationAdditional = 7; + uint32 availableDurationAdditional = 7; uint32 timeUnit = 8; // 1 日 2 月 3年 - string remark = 9; - string associatedorderNumber = 10; - uint64 operatorId = 11; - string operatorName = 12; - string operatorPhoneNumber = 13; + string remark = 9; + string associatedorderNumber = 10; + uint64 operatorId = 11; + string operatorName = 12; + string operatorPhoneNumber = 13; int32 type = 14; } @@ -1057,7 +1061,7 @@ message AddBundleBalanceReq{ int32 dataAnalysisConsumptionNumber = 11; int32 competitiveNumber = 12; int32 competitiveConsumptionNumber = 13; - int32 expansionPacksNumber = 14; + int32 expansionPacksNumber = 14; } message AddBundleBalanceResp{ @@ -1169,19 +1173,19 @@ message GetBundleBalanceByUserIdResp{ string paymentAmount = 8; int32 paymentType = 9; int32 accountNumber = 10; - int32 accountExtendNumber = 11; - int32 accountAdditional = 12; - int32 accountConsumptionNumber = 13; + int32 accountExtendNumber = 11; + int32 accountAdditional = 12; + int32 accountConsumptionNumber = 13; int32 videoNumber = 14; int32 videoExtendNumber = 15; int32 videoExtendConsumptionNumber = 16; - int32 videoAdditional = 17; - int32 videoConsumptionNumber = 18; - int32 imageNumber = 19; + int32 videoAdditional = 17; + int32 videoConsumptionNumber = 18; + int32 imageNumber = 19; int32 imageExtendNumber = 20; int32 imageExtendConsumptionNumber = 21; - int32 imageAdditional = 22; - int32 imageConsumptionNumber = 23; + int32 imageAdditional = 22; + int32 imageConsumptionNumber = 23; int32 dataAnalysisNumber = 24; int32 dataAnalysisExtendNumber = 25; int32 dataAnalysisExtendConsumptionNumber = 26; @@ -1859,7 +1863,7 @@ message MetricsOperatingStatusResp { int64 pendingUploadCompetitiveCount = 32; // 待发布竞品数 int64 uploadSuccessCompetitiveCount = 33; // 发布成功竞品数 int64 uploadFailedCompetitiveCount = 34; // 发布异常竞品数 - + int64 abnormalAccountAcount = 35; // 触发预警的账号数 } @@ -2295,3 +2299,7 @@ message GetLastInvoiceNoReq{ message GetLastInvoiceNoResp{ string lastNo = 1; } + +message OrderInfoByOrderUuidRequest{ + string orderUuid = 1; +} \ No newline at end of file