micro-bundle/internal/logic/orderRecordsLogic.go
2026-06-09 10:48:46 +08:00

282 lines
9.9 KiB
Go
Raw Blame History

This file contains ambiguous Unicode characters

This file contains Unicode characters that might be confused with other characters. If you think that this is intentional, you can safely ignore this warning. Use the Escape button to reveal them.

package logic
import (
"errors"
"fmt"
uuid "github.com/satori/go.uuid"
"micro-bundle/internal/dao"
"micro-bundle/internal/model"
"micro-bundle/pb/bundle"
"micro-bundle/pkg/app"
"micro-bundle/pkg/utils"
"strconv"
"github.com/jinzhu/copier"
)
func CreateOrderRecord(req *bundle.OrderCreateRecord) (res *bundle.CommonResponse, err error) {
res = new(bundle.CommonResponse)
// 先用后付下单前置校验
if eligibility, e := CheckOrderEligibility(&bundle.CheckOrderEligibilityRequest{
CustomerID: req.CustomerID,
OrderKind: model.OrderKindBundle,
OrderMode: req.OrderMode,
}); e != nil {
return nil, e
} else if !eligibility.Allow {
res.Msg = eligibility.Msg
return res, errors.New(eligibility.Reason)
}
//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
}
var addRecords []model.BundleOrderValueAdd
for _, i := range req.AddRecords {
addOrderMode := i.OrderMode
if addOrderMode == 0 {
addOrderMode = model.OrderModePayLater // 增值订单默认先用后付
}
addPayLaterStatus := i.PayLaterStatus
if addOrderMode == model.OrderModePayLater && addPayLaterStatus == 0 && i.PaymentStatus != 2 {
addPayLaterStatus = model.PayLaterStatusPending
}
addRecords = append(addRecords, model.BundleOrderValueAdd{
UUID: app.ModuleClients.SfNode.Generate().Base64(),
OrderNo: orderNo,
OrderUUID: orderUUID,
CustomerID: req.CustomerID,
CustomerNum: req.CustomerNum,
CustomerName: req.CustomerName,
ServiceType: i.ServiceType,
CurrencyType: i.CurrencyType,
Amount: float64(i.Amount),
Num: i.Num,
Unit: i.Unit,
ValueAddUUID: i.ValueUid,
Source: int(i.Source),
PaymentStatus: int(i.PaymentStatus),
SignContract: req.SignContract,
Signature: req.Signature,
SignedTime: req.SignedTime,
Snapshot: req.Snapshot,
HandlingFee: i.HandlingFee,
EquityType: i.EquityType,
QuotaType: i.QuotaType,
QuotaValue: i.QuotaValue,
IsExpired: i.IsExpired,
OrderMode: addOrderMode,
DueTime: i.DueTime,
PayLaterStatus: addPayLaterStatus,
ContractTplType: i.ContractTplType,
})
}
orderMode := req.OrderMode
if orderMode == 0 {
orderMode = model.OrderModeNormal
}
payLaterStatus := req.PayLaterStatus
if orderMode == model.OrderModePayLater && payLaterStatus == 0 && req.Status != model.BundleStatusPaid {
payLaterStatus = model.PayLaterStatusPending
}
orderRecord := &model.BundleOrderRecords{
UUID: orderUUID,
OrderNo: orderNo,
BundleUUID: req.BundleUuid,
BundleName: req.BundleName,
CustomerID: req.CustomerID,
CustomerNum: req.CustomerNum,
CustomerName: req.CustomerName,
Amount: req.Amount,
AmountType: req.AmountType,
TotalAmount: req.TotalAmount,
SignContract: req.SignContract,
Signature: req.Signature,
SignedTime: req.SignedTime,
PayType: int64(req.PayType),
PayTime: req.PayTime,
Status: req.Status,
ContractNo: req.ContractNo,
BundleCommonUid: req.BundleCommonUid,
FinancialConfirmation: model.ConfirmationNotConfirmed,
ExpirationTime: req.ExpirationTime,
Language: req.Language,
BundleOrderValueAdd: addRecords,
PlatformIds: req.PlatformIds,
InviterID: req.InviterId,
PurchaseType: req.PurchaseType,
RenewalOrderUUID: req.RenewalOrderUUID,
OrderMode: orderMode,
DueTime: req.DueTime,
PayLaterStatus: payLaterStatus,
ContractTplType: req.ContractTplType,
OrderType: model.OrderTypeBundle, // 套餐订单
}
res, err = dao.CreateOrderRecord(orderRecord)
return
}
func UpdateOrderRecord(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
res, err = dao.UpdateOrderRecord(orderRecord)
return
}
func UpdateOrderRecordByOrderNo(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("UpdateOrderRecordByOrderNo Status:", req.Status)
res, err = dao.UpdateOrderRecordByOrderNO(orderRecord)
return
}
func OrderRecordsList(req *bundle.OrderRecordsRequest) (res *bundle.OrderRecordsResponse, err error) {
res = new(bundle.OrderRecordsResponse)
res, err = dao.OrderRecordsList(req)
return
}
func OrderRecordsDetail(req *bundle.OrderRecordsDetailRequest) (res *bundle.OrderRecordsDetailResponse, err error) {
res = new(bundle.OrderRecordsDetailResponse)
res.OrderRecord = new(bundle.OrderRecord)
res.OrderRecord, err = dao.OrderRecordDetail(req)
if err != nil {
res.Msg = err.Error()
}
return
}
func GetInEffectOrderRecord(req *bundle.GetInEffectOrderRecordRequest) (res *bundle.OrderRecord, err error) {
res = new(bundle.OrderRecord)
orderReq := &bundle.OrderRecordsDetailRequest{
CustomerID: strconv.FormatUint(req.UserID, 10),
Status: 2,
}
res, err = dao.OrderRecordDetail(orderReq)
if err != nil {
return nil, errors.New("未找到有效订单")
}
return
}
func UpdateFinancialConfirmationStatus(req *bundle.FinancialConfirmationRequest) (res *bundle.CommonResponse, err error) {
res = new(bundle.CommonResponse)
err = app.ModuleClients.BundleDB.Model(&model.BundleOrderRecords{}).Where("order_no = ?", req.OrderNo).Update("financial_confirmation", model.ConfirmationConfirmed).Error
if err != nil {
res.Msg = "更新财务确认状态失败"
return res, err
}
res.Msg = "更新财务确认状态成功"
return
}
func PackagePriceAndTime(req *bundle.OrderRecord) (res *bundle.PackagePriceAndTimeResponse, err error) {
res = new(bundle.PackagePriceAndTimeResponse)
res, err = dao.PackagePriceAndTime(req)
return
}
func CreateOrderAddRecord(req *bundle.OrderAddRecord) (res *bundle.CommonResponse, err error) {
res = new(bundle.CommonResponse)
// 增值订单下单前置校验规则5增值订单不受套餐过期限制可无限续杯
if eligibility, e := CheckOrderEligibility(&bundle.CheckOrderEligibilityRequest{
CustomerID: req.CustomerID,
OrderKind: model.OrderKindValueAdd,
OrderMode: req.OrderMode,
}); e != nil {
return nil, e
} else if !eligibility.Allow {
res.Msg = eligibility.Msg
return res, errors.New(eligibility.Reason)
}
res, err = dao.CreateOrderAddRecord(req)
return
}
func OrderRecordsListV2(req *bundle.OrderRecordsRequestV2) (res *bundle.OrderRecordsResponseV2, err error) {
res = new(bundle.OrderRecordsResponseV2)
res, err = dao.OrderRecordsListV2(req)
return
}
func OrderListByOrderNo(req *bundle.OrderInfoByOrderNoRequest) (res *bundle.OrderInfoByOrderNoResp, err error) {
res = new(bundle.OrderInfoByOrderNoResp)
res, err = dao.OrderListByOrderNo(req)
return
}
func GetReconciliationList(req *bundle.GetReconciliationListReq) (res *bundle.GetReconciliationListResp, err error) {
res = new(bundle.GetReconciliationListResp)
res, err = dao.GetReconciliationList(req)
return
}
func CreateReconciliation(req *bundle.ReconciliationInfo) (res *bundle.CommonResponse, err error) {
res = new(bundle.CommonResponse)
res, err = dao.CreateReconciliation(req)
return
}
func UpdateReconciliation(req *bundle.ReconciliationInfo) (res *bundle.CommonResponse, err error) {
res = new(bundle.CommonResponse)
res, err = dao.UpdateReconciliation(req)
return
}
func OnlyAddValueListByOrderNo(req *bundle.OnlyAddValueListByOrderNoRequest) (res *bundle.OnlyAddValueListByOrderNoResp, err error) {
res = new(bundle.OnlyAddValueListByOrderNoResp)
res, err = dao.OnlyAddValueListByOrderNo(req)
return
}
func UpdateReconciliationStatusBySerialNumber(req *bundle.UpdateStatusAndPayTimeBySerialNumber) (res *bundle.CommonResponse, err error) {
res = new(bundle.CommonResponse)
res, err = dao.UpdateReconciliationStatusBySerialNumber(req)
return
}
func DeleteValueAddService(req *bundle.DeleteValueAddServiceRequest) (res *bundle.CommonResponse, err error) {
res = new(bundle.CommonResponse)
res, err = dao.DeleteValueAddService(req)
return
}
func ListUnfinishedInfos(req *bundle.AutoCreateUserAndOrderRequest) (res *bundle.UnfinishedInfos, err error) {
res = new(bundle.UnfinishedInfos)
res, err = dao.ListUnfinishedInfos(req)
return
}
func SoftDeleteUnfinishedInfo(req *bundle.SoftDeleteUnfinishedInfoRequest) (res *bundle.CommonResponse, err error) {
res = new(bundle.CommonResponse)
res, err = dao.SoftDeleteUnfinishedInfo(req)
return
}
func ReSignTheContract(req *bundle.ReSignTheContractRequest) (res *bundle.CommonResponse, err error) {
res = new(bundle.CommonResponse)
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
}