fusenapi/utils/order/order.go

128 lines
3.6 KiB
Go
Raw Normal View History

2023-09-15 02:35:13 +00:00
package order
2023-09-15 09:58:45 +00:00
2023-09-18 07:39:12 +00:00
import (
2023-09-19 11:17:04 +00:00
"fmt"
"fusenapi/constants"
2023-09-18 07:39:12 +00:00
"fusenapi/model/gmodel"
2023-09-19 11:17:04 +00:00
"math/rand"
"strconv"
"time"
2023-09-18 07:39:12 +00:00
)
type AmountCurrencyReq struct {
ExchangeRate int64 `json:"exchange_rate"` // 换算汇率
CurrentAmount int64 `json:"current_amount"` // 当前金额
OriginalAmount int64 `json:"original_amount"` // 原始金额
CurrentCurrency string `json:"current_currency"` // 当前货币
OriginalCurrency string `json:"original_currency"` // 原始货币
2023-09-15 10:30:45 +00:00
}
2023-09-15 09:58:45 +00:00
2023-09-15 10:30:45 +00:00
// 汇率换算
2023-09-18 07:39:12 +00:00
func GetAmountCurrency(req *AmountCurrencyReq) gmodel.AmountCurrency {
if req.CurrentAmount != 0 {
if req.CurrentCurrency == req.OriginalCurrency {
req.CurrentAmount = req.OriginalAmount
} else {
req.CurrentAmount = req.OriginalAmount * req.ExchangeRate
}
}
return gmodel.AmountCurrency{
ExchangeRate: req.ExchangeRate,
CurrentAmount: req.CurrentAmount,
OriginalAmount: req.OriginalAmount,
CurrentCurrency: req.CurrentCurrency,
OriginalCurrency: req.OriginalCurrency,
}
}
type GetAmountInfoReq struct {
ExchangeRate int64
Initiate int64
Current int64
Change int64
ChangeRemark string
Metadata map[string]interface{}
CurrentCurrency string
OriginalCurrency string
}
// Change AmountCurrency `json:"change,omitempty"` // 变动金额
// ChangeRemark string `json:"change_remark,omitempty"` // 变动备注
// Current AmountCurrency `json:"current"` // 当前金额
// Initiate AmountCurrency `json:"initiate"` // 初始金额
// Metadata map[string]interface{} `json:"metadata"` // 额外明细
func GetAmountInfo(req GetAmountInfoReq) gmodel.AmountInfo {
return gmodel.AmountInfo{
Change: GetAmountCurrency(&AmountCurrencyReq{
ExchangeRate: req.ExchangeRate,
CurrentAmount: req.Change,
OriginalAmount: req.Change,
CurrentCurrency: req.OriginalCurrency,
OriginalCurrency: req.OriginalCurrency,
}),
ChangeRemark: req.ChangeRemark,
Current: GetAmountCurrency(&AmountCurrencyReq{
ExchangeRate: req.ExchangeRate,
CurrentAmount: req.Current,
OriginalAmount: req.Current,
CurrentCurrency: req.OriginalCurrency,
OriginalCurrency: req.OriginalCurrency,
}),
Initiate: GetAmountCurrency(&AmountCurrencyReq{
ExchangeRate: req.ExchangeRate,
CurrentAmount: req.Initiate,
OriginalAmount: req.Initiate,
CurrentCurrency: req.OriginalCurrency,
OriginalCurrency: req.OriginalCurrency,
}),
Metadata: req.Metadata,
2023-09-15 09:58:45 +00:00
}
}
2023-09-19 11:17:04 +00:00
func GenerateOrderNumber(userID int) string {
// 获取当前时间
now := time.Now()
// 生成年月日时分秒的字符串
year := strconv.Itoa(now.Year())
month := strconv.Itoa(int(now.Month()))
day := strconv.Itoa(now.Day())
hour := strconv.Itoa(now.Hour())
minute := strconv.Itoa(now.Minute())
second := strconv.Itoa(now.Second())
// 生成2位随机数
rand.Seed(time.Now().UnixNano())
randomNum := fmt.Sprintf("%02d", rand.Intn(100))
// 拼接订单号
orderNumber := year + month + day + hour + minute + second + randomNum + strconv.Itoa(userID)
return orderNumber
}
// 初始化订单状态
func GenerateOrderStatusLink(req []gmodel.OrderStatus) []gmodel.OrderStatus {
var list []gmodel.OrderStatus
for _, v := range constants.OrderStatusUser {
for _, v1 := range req {
if v1.StatusCode == v {
list = append(list, v1)
} else {
list = append(list, gmodel.OrderStatus{
StatusCode: v,
StatusTitle: constants.OrderStatusMessage[v],
})
}
}
}
return list
}
// 获取订单当前状态
func GenerateOrderStatusCurrent() gmodel.OrderStatus {
return gmodel.OrderStatus{}
}