fusenapi/utils/order/order.go

246 lines
7.5 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-21 04:11:15 +00:00
"fusenapi/utils/format"
2023-09-22 09:32:23 +00:00
"strconv"
2023-09-19 11:17:04 +00:00
"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
2023-09-21 02:30:36 +00:00
type GetAmountCurrencyUSDReq 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-21 04:11:15 +00:00
// 处理金额(元)
2023-10-27 08:36:09 +00:00
func GetAmountCurrencyFormat(req *gmodel.AmountCurrency, withThousandthPercentile bool) (res gmodel.AmountCurrency) {
var currentAmount = format.CentitoDollarStr(req.CurrentAmount.(float64))
if withThousandthPercentile {
currentAmount = format.NumToStringWithThousandthPercentile(currentAmount)
}
var originalAmount = format.CentitoDollarStr(req.OriginalAmount.(float64))
if withThousandthPercentile {
originalAmount = format.NumToStringWithThousandthPercentile(originalAmount)
}
2023-09-21 04:11:15 +00:00
return gmodel.AmountCurrency{
2023-10-27 08:36:09 +00:00
ExchangeRate: req.ExchangeRate.(float64),
CurrentAmount: currentAmount,
OriginalAmount: originalAmount,
2023-09-21 04:11:15 +00:00
CurrentCurrency: req.CurrentCurrency,
OriginalCurrency: req.OriginalCurrency,
}
2023-09-21 02:30:36 +00:00
}
2023-09-21 04:11:15 +00:00
// 处理金额(元)
2023-10-27 08:36:09 +00:00
func GetAmountInfoFormat(req *gmodel.AmountInfo, withThousandthPercentile bool) gmodel.AmountInfo {
2023-09-21 04:11:15 +00:00
return gmodel.AmountInfo{
2023-10-27 08:36:09 +00:00
Change: GetAmountCurrencyFormat(&req.Change, withThousandthPercentile),
2023-09-21 04:11:15 +00:00
ChangeRemark: req.ChangeRemark,
2023-10-27 08:36:09 +00:00
Current: GetAmountCurrencyFormat(&req.Current, withThousandthPercentile),
Initiate: GetAmountCurrencyFormat(&req.Initiate, withThousandthPercentile),
2023-09-21 04:11:15 +00:00
Metadata: req.Metadata,
2023-09-21 02:30:36 +00:00
}
}
2023-09-22 09:32:23 +00:00
// 处理商品数量
2023-10-27 08:36:09 +00:00
func GetPurchaseQuantity(req *gmodel.PurchaseQuantity, withThousandthPercentile bool) gmodel.PurchaseQuantity {
var initiate = strconv.FormatInt(int64(req.Initiate.(float64)), 10)
if withThousandthPercentile {
initiate = format.NumToStringWithThousandthPercentile(initiate)
}
var current = strconv.FormatInt(int64(req.Current.(float64)), 10)
if withThousandthPercentile {
current = format.NumToStringWithThousandthPercentile(current)
}
2023-09-22 09:32:23 +00:00
return gmodel.PurchaseQuantity{
2023-10-27 08:36:09 +00:00
Initiate: initiate,
Current: current,
2023-09-22 09:32:23 +00:00
}
}
2023-09-21 04:11:15 +00:00
// 生成订单编号
2023-09-21 02:30:36 +00:00
func GenerateOrderNumber() string {
2023-09-20 10:04:33 +00:00
t := time.Now()
2023-09-21 02:30:36 +00:00
orderNumber := fmt.Sprintf("%d%02d%02d%08d", t.Year(), t.Month(), t.Day(), t.UnixNano()%100000000)
2023-09-19 11:17:04 +00:00
return orderNumber
}
// 生成收据编号
func GenerateReceiptNumber() string {
t := time.Now()
return fmt.Sprintf("%02d%02d%02d%08d", t.Year()%100, t.Month(), t.Day(), t.UnixNano()%100000000)
}
2023-09-22 08:27:17 +00:00
// 初始化订单状态--链路
2023-09-20 07:07:12 +00:00
func GenerateOrderStatusLink(deliveryMethod int64, noTime time.Time, expectedTime time.Time) []gmodel.OrderStatus {
2023-09-19 11:17:04 +00:00
var list []gmodel.OrderStatus
2023-09-20 03:39:56 +00:00
var orderStatus []constants.OrderStatusCode
if deliveryMethod == constants.DELIVERYMETHODDIRECTMAIL {
orderStatus = constants.OrderStatusUserDIRECTMAIL
} else {
orderStatus = constants.OrderStatusUserCLOUDSTORE
}
for _, v := range orderStatus {
2023-09-28 09:53:41 +00:00
item := gmodel.OrderStatus{
2023-09-20 03:39:56 +00:00
StatusCode: v,
StatusTitle: constants.OrderStatusMessage[v],
2023-09-28 09:53:41 +00:00
}
if v == constants.ORDER_STATUS_UNPAIDDEPOSIT {
item.Ctime = &noTime
item.Utime = &noTime
}
if v == constants.ORDER_STATUS_DIRECTMAIL_ARRIVED || v == constants.ORDER_STATUS_CLOUDSTORE_ARRIVEDWAREHOUSE {
item.ExpectedTime = &expectedTime
}
list = append(list, item)
2023-09-19 11:17:04 +00:00
}
return list
}
2023-09-22 08:27:17 +00:00
// 更新订单状态--链路
func UpdateOrderStatusLink(statusLink []gmodel.OrderStatus, status gmodel.OrderStatus) []gmodel.OrderStatus {
var list []gmodel.OrderStatus
for _, v := range statusLink {
if v.StatusCode == status.StatusCode {
item := v
if status.StatusTitle != "" {
item.StatusTitle = status.StatusTitle
}
if status.StatusCode != 0 {
item.StatusCode = status.StatusCode
}
if status.Utime != nil {
item.Utime = status.Utime
2023-09-26 09:04:27 +00:00
}
if status.Ctime != nil {
item.Ctime = status.Ctime
2023-09-22 08:27:17 +00:00
}
if status.Metadata != nil {
item.Metadata = status.Metadata
}
if status.ExpectedTime != nil {
item.ExpectedTime = status.ExpectedTime
}
if status.Children != nil || len(status.Children) > 0 {
item.Children = status.Children
}
list = append(list, item)
} else {
list = append(list, v)
}
}
return list
}
2023-09-21 04:11:15 +00:00
// 获取订单状态
func GetOrderStatusLinkUser(deliveryMethod int64, statusLink []gmodel.OrderStatus) []gmodel.OrderStatus {
var list []gmodel.OrderStatus
var orderStatus []constants.OrderStatusCode
if deliveryMethod == constants.DELIVERYMETHODDIRECTMAIL {
orderStatus = constants.OrderStatusUserDIRECTMAIL
} else {
orderStatus = constants.OrderStatusUserCLOUDSTORE
}
for _, v := range statusLink {
for _, orderStatusCode := range orderStatus {
if v.StatusCode == orderStatusCode {
list = append(list, v)
}
}
}
return list
}
2023-09-19 11:17:04 +00:00
// 获取订单当前状态
2023-09-27 09:23:58 +00:00
func GetOrderStatusCurrent(statusLink []gmodel.OrderStatus, orderStatusCode constants.OrderStatusCode) gmodel.OrderStatus {
var status gmodel.OrderStatus
for _, v := range statusLink {
if v.StatusCode == orderStatusCode {
status = v
}
}
return status
2023-09-19 11:17:04 +00:00
}