fix: 第1批 M2、M3、M16、M23 导入与展示类缺陷
Some checks failed
构建并部署到测试环境(无 SSH) / build-and-deploy (push) Failing after 8h37m36s
Some checks failed
构建并部署到测试环境(无 SSH) / build-and-deploy (push) Failing after 8h37m36s
- M2 批量换绑表头改为“平台登录账号”,只认手机号,细分格式、不存在、多匹配、非平台、已禁用失败原因 - M3 后台订单列表与详情返回 C 端在线购买的交易流水号、支付渠道、商户名称,详情新增全部支付单 payments - M16 预警规则不再返回 real_data_mb,预警列表/详情新增 threshold_mb,导出末列追加“阈值流量(MB)” - M23 设备导入 IMEI 必填,空 IMEI 行记为失败 - OpenSpec Change fix-august-iteration-follow-up-gaps:13/18,导入任务、导出任务的 Worker 端到端验证与前端/运营通知待部署后完成
This commit is contained in:
@@ -86,7 +86,7 @@ func (s *RuleService) Create(ctx context.Context, request *dto.CreatePackageTraf
|
||||
}
|
||||
return errors.Wrap(errors.CodeDatabaseError, createErr, "创建套餐真流量预警规则失败")
|
||||
}
|
||||
item := toRuleItem(rule, pkg.PackageName, pkg.RealDataMB)
|
||||
item := toRuleItem(rule, pkg.PackageName)
|
||||
if auditErr := s.appendRuleAudit(ctx, tx, constants.AuditActionPackageTrafficAlertRuleCreated,
|
||||
"创建套餐真流量预警规则", rule, pkg.PackageName, nil, ruleAuditSnapshot(rule, pkg.PackageName)); auditErr != nil {
|
||||
return auditErr
|
||||
@@ -161,7 +161,7 @@ func (s *RuleService) Update(ctx context.Context, ruleID uint, request *dto.Upda
|
||||
return auditErr
|
||||
}
|
||||
}
|
||||
response = toRuleItem(rule, packageName, pkg.RealDataMB)
|
||||
response = toRuleItem(rule, packageName)
|
||||
return nil
|
||||
})
|
||||
if err != nil {
|
||||
@@ -273,7 +273,8 @@ func ruleAuditIdentity(rule *model.PackageTrafficAlertRule, packageName string)
|
||||
}
|
||||
|
||||
// toRuleItem 把规则投影为对外响应项。
|
||||
func toRuleItem(rule *model.PackageTrafficAlertRule, packageName string, realDataMB int64) *dto.PackageTrafficAlertRuleItem {
|
||||
// 不返回套餐商品真流量额度:规则按百分比配置,判定分母是资产主套餐与加油包汇总,单个额度会被误读为分母。
|
||||
func toRuleItem(rule *model.PackageTrafficAlertRule, packageName string) *dto.PackageTrafficAlertRuleItem {
|
||||
if rule == nil {
|
||||
return nil
|
||||
}
|
||||
@@ -281,7 +282,6 @@ func toRuleItem(rule *model.PackageTrafficAlertRule, packageName string, realDat
|
||||
ID: rule.ID,
|
||||
PackageID: rule.PackageID,
|
||||
PackageName: packageName,
|
||||
RealDataMB: realDataMB,
|
||||
ThresholdPercent: rule.ThresholdPercent,
|
||||
Enabled: rule.Enabled == constants.StatusEnabled,
|
||||
EnabledName: enabledName(rule.Enabled),
|
||||
|
||||
@@ -46,6 +46,11 @@ func Decide(usedMB, limitMB, thresholdBasisPoints int64) (bool, int64) {
|
||||
return hit, ratioBasisPoints
|
||||
}
|
||||
|
||||
// ThresholdMB 返回阈值对应流量:真流量额度汇总 × 阈值百分比 ÷ 100,保留两位小数,口径与额度一致(含加油包)。
|
||||
func ThresholdMB(limitMB int64, thresholdPercent float64) float64 {
|
||||
return math.Round(float64(limitMB)*NormalizeThresholdPercent(thresholdPercent)) / 100
|
||||
}
|
||||
|
||||
// PercentFromBasisPoints 把万分比换算为保留两位小数的百分比展示值。
|
||||
func PercentFromBasisPoints(basisPoints int64) float64 {
|
||||
return float64(basisPoints) / 100
|
||||
|
||||
@@ -56,9 +56,18 @@ func (s *PackageTrafficAlertDataSource) Headers(context.Context, ExportParams) (
|
||||
"资产类型", "资产标识", "对应标识符", "卡标识", "设备类型", "设备型号",
|
||||
"套餐名称", "真流量已用量(MB)", "真流量额度(MB)", "比例(%)", "阈值快照(%)",
|
||||
"到期时间", "剩余天数", "触发时间", "店铺", "业务员", "用户组", "通知投递结果",
|
||||
packageTrafficAlertThresholdMBHeader,
|
||||
}, nil
|
||||
}
|
||||
|
||||
// packageTrafficAlertThresholdMBHeader 阈值对应流量列,固定追加在最后一列;历史任务冻结表头没有该列。
|
||||
const packageTrafficAlertThresholdMBHeader = "阈值流量(MB)"
|
||||
|
||||
// alertHeadersIncludeThresholdMB 判断冻结表头是否为带“阈值流量(MB)”末列的新表头。
|
||||
func alertHeadersIncludeThresholdMB(headers []string) bool {
|
||||
return len(headers) == 0 || headers[len(headers)-1] == packageTrafficAlertThresholdMBHeader
|
||||
}
|
||||
|
||||
// Fetch 按 offset/limit 查询预警导出数据。
|
||||
func (s *PackageTrafficAlertDataSource) Fetch(ctx context.Context, params ExportParams, offset, limit int) ([][]string, error) {
|
||||
if limit <= 0 {
|
||||
@@ -109,9 +118,10 @@ func (s *PackageTrafficAlertDataSource) Fetch(ctx context.Context, params Export
|
||||
return nil, err
|
||||
}
|
||||
now := time.Now().UTC()
|
||||
includeThresholdMB := alertHeadersIncludeThresholdMB(params.ResolvedHeaders)
|
||||
rows := make([][]string, 0, len(items))
|
||||
for _, item := range items {
|
||||
rows = append(rows, []string{
|
||||
row := []string{
|
||||
assetTypeName(item.AssetType),
|
||||
item.AssetIdentifier,
|
||||
item.CounterpartIdentifier,
|
||||
@@ -130,7 +140,11 @@ func (s *PackageTrafficAlertDataSource) Fetch(ctx context.Context, params Export
|
||||
item.CurrentOwnerName,
|
||||
currentOwnerGroupName(groupNames, item.CurrentOwnerID),
|
||||
constants.GetPackageTrafficAlertNotifyStatusName(resolveAlertNotifyStatus(item)),
|
||||
})
|
||||
}
|
||||
if includeThresholdMB {
|
||||
row = append(row, strconv.FormatFloat(packagetrafficalert.ThresholdMB(item.LimitMB, item.ThresholdPercent), 'f', 2, 64))
|
||||
}
|
||||
rows = append(rows, row)
|
||||
}
|
||||
return rows, nil
|
||||
}
|
||||
|
||||
@@ -79,7 +79,7 @@ func (h *OrderHandler) Get(c *fiber.Ctx) error {
|
||||
return errors.New(errors.CodeInvalidParam, "无效的订单ID")
|
||||
}
|
||||
|
||||
order, err := h.service.Get(c.UserContext(), uint(id))
|
||||
order, err := h.service.GetDetail(c.UserContext(), uint(id))
|
||||
if err != nil {
|
||||
return err
|
||||
}
|
||||
|
||||
@@ -95,6 +95,32 @@ type OrderResponse struct {
|
||||
// 资产标识符快照
|
||||
AssetIdentifier string `json:"asset_identifier,omitempty" description:"下单时资产的标识符快照(卡为 ICCID,设备优先使用 VirtualNo,缺失时使用 IMEI)"`
|
||||
AssetType string `json:"asset_type,omitempty" description:"资产类型 (card:单卡, device:设备)"`
|
||||
|
||||
// C 端在线购买的原始支付信息:仅个人客户以微信或支付宝支付、订单已支付或已退款时有值,其余为空串
|
||||
ThirdPartyTradeNo string `json:"third_party_trade_no" description:"交易流水号(C 端微信/支付宝在线购买且已支付或已退款时取已支付支付单,其余为空串)"`
|
||||
PaymentChannel string `json:"payment_channel" description:"支付渠道类型 (wechat:微信直连v3, wechat_v2:微信直连v2, fuiou:富友);取已支付支付单快照,无快照按原支付配置推断,取不到为空串"`
|
||||
PaymentMerchantName string `json:"payment_merchant_name" description:"收款商户名称快照,没有为空串"`
|
||||
}
|
||||
|
||||
// OrderDetailResponse 后台订单详情响应:在订单字段之外返回该订单的全部支付单。
|
||||
type OrderDetailResponse struct {
|
||||
*OrderResponse
|
||||
Payments []*OrderPaymentResponse `json:"payments" description:"该订单全部支付单(仅个人客户微信/支付宝订单,按创建时间倒序;其他订单为空数组)"`
|
||||
}
|
||||
|
||||
// OrderPaymentResponse 后台订单详情中的单条支付单;不含二维码、商户号、路由与支付配置等内部字段。
|
||||
type OrderPaymentResponse struct {
|
||||
PaymentNo string `json:"payment_no" description:"支付单号"`
|
||||
PaymentMethod string `json:"payment_method" description:"支付方式 (wechat:微信支付, alipay:支付宝支付)"`
|
||||
PaymentChannel string `json:"payment_channel" description:"支付渠道类型 (wechat:微信直连v3, wechat_v2:微信直连v2, fuiou:富友),取不到为空串"`
|
||||
PaymentMerchantName string `json:"payment_merchant_name" description:"收款商户名称快照,没有为空串"`
|
||||
ThirdPartyTradeNo string `json:"third_party_trade_no" description:"交易流水号,没有为空串"`
|
||||
Amount int64 `json:"amount" description:"支付金额(分)"`
|
||||
Status int `json:"status" description:"支付单状态 (0:待支付, 1:已支付, 2:已失败, 3:已退款)"`
|
||||
StatusName string `json:"status_name" description:"支付单状态名称"`
|
||||
CreatedAt time.Time `json:"created_at" description:"创建时间"`
|
||||
PaidAt *time.Time `json:"paid_at" description:"支付时间"`
|
||||
ExpireAt *time.Time `json:"expire_at" description:"过期时间"`
|
||||
}
|
||||
|
||||
type OrderListResponse struct {
|
||||
|
||||
@@ -38,7 +38,6 @@ type PackageTrafficAlertRuleItem struct {
|
||||
ID uint `json:"id" description:"预警规则ID"`
|
||||
PackageID uint `json:"package_id" description:"套餐商品ID"`
|
||||
PackageName string `json:"package_name" description:"套餐名称"`
|
||||
RealDataMB int64 `json:"real_data_mb" description:"套餐商品当前真流量额度(MB),仅用于配置校验展示,不作为预警分母"`
|
||||
ThresholdPercent float64 `json:"threshold_percent" description:"真流量预警阈值百分比"`
|
||||
Enabled bool `json:"enabled" description:"是否启用"`
|
||||
EnabledName string `json:"enabled_name" description:"启用状态名称(中文)"`
|
||||
@@ -84,10 +83,11 @@ type PackageTrafficAlertItem struct {
|
||||
CounterpartIdentifier string `json:"counterpart_identifier" description:"对应标识符快照(卡→触发时绑定设备标识,设备→触发时绑定卡 ICCID)"`
|
||||
DeviceType string `json:"device_type" description:"设备类型快照"`
|
||||
DeviceModel string `json:"device_model" description:"设备型号快照"`
|
||||
UsedMB int64 `json:"used_mb" description:"触发时真已用量汇总快照(MB)"`
|
||||
LimitMB int64 `json:"limit_mb" description:"触发时真总量快照汇总(MB)"`
|
||||
UsedMB int64 `json:"used_mb" description:"触发时真流量已用量快照(MB),主套餐与加油包汇总"`
|
||||
LimitMB int64 `json:"limit_mb" description:"触发时真流量额度快照(MB),主套餐与加油包汇总"`
|
||||
UsagePercent float64 `json:"usage_percent" description:"触发时汇总比例快照(%),可能大于 100"`
|
||||
ThresholdPercent float64 `json:"threshold_percent" description:"触发阈值快照(%)"`
|
||||
ThresholdMB float64 `json:"threshold_mb" description:"阈值对应流量(MB):真流量额度快照 × 阈值快照 ÷ 100,保留两位小数,口径含加油包"`
|
||||
ExpiresAt *time.Time `json:"expires_at" description:"主套餐到期时间快照,无法推算时为 null"`
|
||||
DaysRemaining *int `json:"days_remaining" description:"按到期时间快照推算的剩余上海自然日天数,负数表示已过期;无到期时间时为 null"`
|
||||
TriggeredAt time.Time `json:"triggered_at" description:"触发时间"`
|
||||
|
||||
@@ -57,7 +57,7 @@ func (q *Query) ListRules(ctx context.Context, request dto.ListPackageTrafficAle
|
||||
return nil, errors.Wrap(errors.CodeDatabaseError, err, "查询套餐真流量预警规则总数失败")
|
||||
}
|
||||
if err := query.Select("r.id, r.package_id, r.threshold_percent, r.enabled, r.remark, r.updated_at, " +
|
||||
"COALESCE(p.package_name, '') AS package_name, COALESCE(p.real_data_mb, 0) AS real_data_mb").
|
||||
"COALESCE(p.package_name, '') AS package_name").
|
||||
Order("r.updated_at DESC, r.id DESC").
|
||||
Offset((page - 1) * pageSize).Limit(pageSize).
|
||||
Scan(&rows).Error; err != nil {
|
||||
@@ -69,7 +69,6 @@ func (q *Query) ListRules(ctx context.Context, request dto.ListPackageTrafficAle
|
||||
ID: row.ID,
|
||||
PackageID: row.PackageID,
|
||||
PackageName: row.PackageName,
|
||||
RealDataMB: row.RealDataMB,
|
||||
ThresholdPercent: row.ThresholdPercent,
|
||||
Enabled: row.Enabled == constants.StatusEnabled,
|
||||
EnabledName: enabledStatusName(row.Enabled),
|
||||
@@ -84,7 +83,6 @@ type ruleRow struct {
|
||||
ID uint `gorm:"column:id"`
|
||||
PackageID uint `gorm:"column:package_id"`
|
||||
PackageName string `gorm:"column:package_name"`
|
||||
RealDataMB int64 `gorm:"column:real_data_mb"`
|
||||
ThresholdPercent float64 `gorm:"column:threshold_percent"`
|
||||
Enabled int `gorm:"column:enabled"`
|
||||
Remark string `gorm:"column:remark"`
|
||||
@@ -241,6 +239,7 @@ func (q *Query) projectAlerts(ctx context.Context, alerts []*model.PackageTraffi
|
||||
LimitMB: alert.LimitMBSnapshot,
|
||||
UsagePercent: alert.UsagePercentSnapshot,
|
||||
ThresholdPercent: alert.ThresholdPercentSnapshot,
|
||||
ThresholdMB: domainpackagetrafficalert.ThresholdMB(alert.LimitMBSnapshot, alert.ThresholdPercentSnapshot),
|
||||
ExpiresAt: alert.ExpiresAtSnapshot,
|
||||
TriggeredAt: alert.TriggeredAt,
|
||||
ShopName: alert.ShopNameSnapshot,
|
||||
|
||||
@@ -21,11 +21,11 @@ const shopOwnerImportDoc = `仅超级管理员和平台账号可操作,代理
|
||||
|
||||
### 模板列序(首行表头必须完全一致)
|
||||
|
||||
` + "`店铺编码`" + `、` + "`操作类型`" + `、` + "`业务员登录账号`" + `、` + "`备注`" + `
|
||||
` + "`店铺编码`" + `、` + "`操作类型`" + `、` + "`平台登录账号`" + `、` + "`备注`" + `
|
||||
|
||||
- 店铺编码:以店铺编号唯一定位店铺;不存在或已删除时该行失败
|
||||
- 操作类型:取值仅 ` + "`换绑`" + ` 与 ` + "`清空`" + `;换绑必须填写业务员登录账号,清空不得填写
|
||||
- 业务员登录账号:以登录账号唯一定位业务员;必须是启用平台用户
|
||||
- 操作类型:取值仅 ` + "`换绑`" + ` 与 ` + "`清空`" + `;换绑必须填写平台登录账号,清空不得填写
|
||||
- 平台登录账号:填写平台用户的登录手机号(11 位数字),不接受用户名;格式不正确、手机号不存在、匹配多个账号、不是平台用户、账号已禁用时该行失败并给出对应原因。旧表头「业务员登录账号」整批失败
|
||||
- 备注:可选,填写时写入该行审计
|
||||
|
||||
### 编码要求
|
||||
|
||||
@@ -121,7 +121,7 @@ func registerDeviceRoutes(router fiber.Router, handler *admin.DeviceHandler, imp
|
||||
- ` + "`device_name`" + `: 设备名称
|
||||
- ` + "`device_model`" + `: 设备型号
|
||||
- ` + "`device_type`" + `: 设备类型
|
||||
- ` + "`imei`" + `: 设备 IMEI
|
||||
- ` + "`imei`" + `: 设备 IMEI(**必填**,只校验非空;为空或仅空白的行记为失败,原因“设备IMEI(imei)不能为空”)
|
||||
- ` + "`manufacturer`" + `: 制造商
|
||||
- ` + "`max_sim_slots`" + `: 最大插槽数(默认4,范围1-4)
|
||||
- ` + "`iccid_1`" + ` ~ ` + "`iccid_4`" + `: 固定槽位的卡 ICCID(` + "`iccid_1`" + `=槽1,` + "`iccid_2`" + `=槽2,` + "`iccid_3`" + `=槽3,` + "`iccid_4`" + `=槽4;卡必须已存在且未绑定)
|
||||
|
||||
@@ -31,7 +31,7 @@ func registerAdminOrderRoutes(router fiber.Router, handler *admin.OrderHandler,
|
||||
Summary: "获取订单详情",
|
||||
Tags: []string{"订单管理"},
|
||||
Input: new(dto.GetOrderRequest),
|
||||
Output: new(dto.OrderResponse),
|
||||
Output: new(dto.OrderDetailResponse),
|
||||
Auth: true,
|
||||
})
|
||||
|
||||
|
||||
@@ -29,7 +29,7 @@ func registerPackageTrafficAlertRoutes(router fiber.Router, handler *admin.Packa
|
||||
|
||||
Register(ruleGroup, doc, rulePath, "GET", "", handler.ListRules, RouteSpec{
|
||||
Summary: "查询套餐真流量预警规则列表",
|
||||
Description: "返回套餐、商品当前真流量额度、阈值、启用状态、备注与更新时间;仅超级管理员与平台账号可访问",
|
||||
Description: "返回套餐、阈值百分比、启用状态、备注与更新时间,不返回套餐流量额度:判定时按资产当前有效的主套餐与加油包真流量汇总计算,规则创建时无法得知汇总额度;仅超级管理员与平台账号可访问",
|
||||
Tags: []string{"套餐管理"},
|
||||
Input: new(dto.ListPackageTrafficAlertRuleRequest),
|
||||
Output: new(dto.PackageTrafficAlertRuleListResponse),
|
||||
@@ -59,7 +59,7 @@ func registerPackageTrafficAlertRoutes(router fiber.Router, handler *admin.Packa
|
||||
|
||||
Register(alertGroup, doc, alertPath, "GET", "", handler.ListAlerts, RouteSpec{
|
||||
Summary: "查询套餐真流量达量预警列表",
|
||||
Description: "先应用既有资产数据范围;资产、套餐、用量、阈值、到期时间与触发时归属均为触发快照,支持按触发时间闭区间与通知投递结果筛选",
|
||||
Description: "先应用既有资产数据范围;资产、套餐、用量、阈值、到期时间与触发时归属均为触发快照,真流量已用量与额度为主套餐与加油包汇总,threshold_mb 为额度 × 阈值 ÷ 100;支持按触发时间闭区间与通知投递结果筛选",
|
||||
Tags: []string{"套餐流量预警"},
|
||||
Input: new(dto.ListPackageTrafficAlertRequest),
|
||||
Output: new(dto.PackageTrafficAlertListResponse),
|
||||
|
||||
139
internal/service/order/payment_info.go
Normal file
139
internal/service/order/payment_info.go
Normal file
@@ -0,0 +1,139 @@
|
||||
package order
|
||||
|
||||
import (
|
||||
"context"
|
||||
|
||||
"github.com/break/junhong_cmp_fiber/internal/model"
|
||||
"github.com/break/junhong_cmp_fiber/internal/model/dto"
|
||||
"github.com/break/junhong_cmp_fiber/pkg/constants"
|
||||
"github.com/break/junhong_cmp_fiber/pkg/errors"
|
||||
)
|
||||
|
||||
// orderHasOnlinePaymentInfo 判断订单是否为 C 端在线购买且已支付或已退款,只有这类订单返回原始支付信息。
|
||||
func orderHasOnlinePaymentInfo(order *model.Order) bool {
|
||||
if order == nil || order.BuyerType != model.BuyerTypePersonal {
|
||||
return false
|
||||
}
|
||||
if order.PaymentMethod != model.PaymentMethodWechat && order.PaymentMethod != model.PaymentMethodAlipay {
|
||||
return false
|
||||
}
|
||||
return order.PaymentStatus == model.PaymentStatusPaid || order.PaymentStatus == model.PaymentStatusRefunded
|
||||
}
|
||||
|
||||
// orderIsOnlinePurchase 判断订单是否为个人客户微信或支付宝订单,详情支付单列表按此范围返回。
|
||||
func orderIsOnlinePurchase(order *model.Order) bool {
|
||||
if order == nil || order.BuyerType != model.BuyerTypePersonal {
|
||||
return false
|
||||
}
|
||||
return order.PaymentMethod == model.PaymentMethodWechat || order.PaymentMethod == model.PaymentMethodAlipay
|
||||
}
|
||||
|
||||
// fillOrderPaymentInfo 批量为订单响应填充交易流水号、支付渠道与商户名称;一次查询支付单、一次查询支付配置。
|
||||
func (s *Service) fillOrderPaymentInfo(ctx context.Context, orders []*model.Order, responses []*dto.OrderResponse) error {
|
||||
orderIDs := make([]uint, 0, len(orders))
|
||||
for _, order := range orders {
|
||||
if orderHasOnlinePaymentInfo(order) {
|
||||
orderIDs = append(orderIDs, order.ID)
|
||||
}
|
||||
}
|
||||
if len(orderIDs) == 0 {
|
||||
return nil
|
||||
}
|
||||
var payments []*model.Payment
|
||||
// 带 order_type 过滤:tb_payment 同时存充值类支付,业务单 ID 空间与套餐订单不同。
|
||||
if err := s.db.WithContext(ctx).
|
||||
Where("order_id IN ? AND order_type = ? AND status = ?", orderIDs, model.PaymentOrderTypePackage, constants.PaymentRecordStatusPaid).
|
||||
Order("order_id ASC, paid_at DESC NULLS LAST, id DESC").
|
||||
Find(&payments).Error; err != nil {
|
||||
return errors.Wrap(errors.CodeDatabaseError, err, "查询订单支付记录失败")
|
||||
}
|
||||
channels, err := s.resolvePaymentChannels(ctx, payments)
|
||||
if err != nil {
|
||||
return err
|
||||
}
|
||||
paidByOrder := make(map[uint]*model.Payment, len(payments))
|
||||
for _, payment := range payments {
|
||||
if _, exists := paidByOrder[payment.OrderID]; !exists {
|
||||
paidByOrder[payment.OrderID] = payment
|
||||
}
|
||||
}
|
||||
for index, order := range orders {
|
||||
payment, ok := paidByOrder[order.ID]
|
||||
if !ok || !orderHasOnlinePaymentInfo(order) {
|
||||
continue
|
||||
}
|
||||
responses[index].ThirdPartyTradeNo = payment.ThirdPartyTradeNo
|
||||
responses[index].PaymentChannel = paymentChannel(payment, channels)
|
||||
responses[index].PaymentMerchantName = payment.MerchantNameSnapshot
|
||||
}
|
||||
return nil
|
||||
}
|
||||
|
||||
// listOrderPayments 返回订单详情的全部支付单(任何状态),按创建时间倒序;不适用的订单返回空数组。
|
||||
func (s *Service) listOrderPayments(ctx context.Context, order *model.Order) ([]*dto.OrderPaymentResponse, error) {
|
||||
result := make([]*dto.OrderPaymentResponse, 0)
|
||||
if !orderIsOnlinePurchase(order) {
|
||||
return result, nil
|
||||
}
|
||||
var payments []*model.Payment
|
||||
if err := s.db.WithContext(ctx).
|
||||
Where("order_id = ? AND order_type = ?", order.ID, model.PaymentOrderTypePackage).
|
||||
Order("created_at DESC, id DESC").
|
||||
Find(&payments).Error; err != nil {
|
||||
return nil, errors.Wrap(errors.CodeDatabaseError, err, "查询订单支付单失败")
|
||||
}
|
||||
channels, err := s.resolvePaymentChannels(ctx, payments)
|
||||
if err != nil {
|
||||
return nil, err
|
||||
}
|
||||
for _, payment := range payments {
|
||||
result = append(result, &dto.OrderPaymentResponse{
|
||||
PaymentNo: payment.PaymentNo,
|
||||
PaymentMethod: payment.PaymentMethod,
|
||||
PaymentChannel: paymentChannel(payment, channels),
|
||||
PaymentMerchantName: payment.MerchantNameSnapshot,
|
||||
ThirdPartyTradeNo: payment.ThirdPartyTradeNo,
|
||||
Amount: payment.Amount,
|
||||
Status: payment.Status,
|
||||
StatusName: constants.GetPaymentRecordStatusName(payment.Status),
|
||||
CreatedAt: payment.CreatedAt,
|
||||
PaidAt: payment.PaidAt,
|
||||
ExpireAt: payment.ExpireAt,
|
||||
})
|
||||
}
|
||||
return result, nil
|
||||
}
|
||||
|
||||
// resolvePaymentChannels 为缺少渠道快照的历史支付单批量读取原支付配置的渠道类型(含已删除配置)。
|
||||
func (s *Service) resolvePaymentChannels(ctx context.Context, payments []*model.Payment) (map[uint]string, error) {
|
||||
configIDs := make([]uint, 0)
|
||||
for _, payment := range payments {
|
||||
if payment.MerchantProviderTypeSnapshot == "" && payment.PaymentConfigID != nil {
|
||||
configIDs = append(configIDs, *payment.PaymentConfigID)
|
||||
}
|
||||
}
|
||||
channels := make(map[uint]string, len(configIDs))
|
||||
if len(configIDs) == 0 {
|
||||
return channels, nil
|
||||
}
|
||||
var configs []model.WechatConfig
|
||||
if err := s.db.WithContext(ctx).Unscoped().Select("id", "provider_type").
|
||||
Where("id IN ?", configIDs).Find(&configs).Error; err != nil {
|
||||
return nil, errors.Wrap(errors.CodeDatabaseError, err, "查询历史支付配置失败")
|
||||
}
|
||||
for _, config := range configs {
|
||||
channels[config.ID] = config.ProviderType
|
||||
}
|
||||
return channels, nil
|
||||
}
|
||||
|
||||
// paymentChannel 取支付单渠道类型快照,没有快照时按原支付配置推断,推断不出为空串。
|
||||
func paymentChannel(payment *model.Payment, channels map[uint]string) string {
|
||||
if payment.MerchantProviderTypeSnapshot != "" {
|
||||
return payment.MerchantProviderTypeSnapshot
|
||||
}
|
||||
if payment.PaymentConfigID == nil {
|
||||
return ""
|
||||
}
|
||||
return channels[*payment.PaymentConfigID]
|
||||
}
|
||||
@@ -1297,6 +1297,27 @@ func (s *Service) Get(ctx context.Context, id uint) (*dto.OrderResponse, error)
|
||||
return s.buildOrderResponse(ctx, order, items), nil
|
||||
}
|
||||
|
||||
// GetDetail 返回后台订单详情,含 C 端在线购买的原始支付信息与该订单全部支付单。
|
||||
func (s *Service) GetDetail(ctx context.Context, id uint) (*dto.OrderDetailResponse, error) {
|
||||
order, items, err := s.orderStore.GetByIDWithItems(ctx, id)
|
||||
if err != nil {
|
||||
if err == gorm.ErrRecordNotFound {
|
||||
return nil, errors.New(errors.CodeNotFound, "订单不存在")
|
||||
}
|
||||
return nil, err
|
||||
}
|
||||
|
||||
resp := s.buildOrderResponse(ctx, order, items)
|
||||
if err := s.fillOrderPaymentInfo(ctx, []*model.Order{order}, []*dto.OrderResponse{resp}); err != nil {
|
||||
return nil, err
|
||||
}
|
||||
payments, err := s.listOrderPayments(ctx, order)
|
||||
if err != nil {
|
||||
return nil, err
|
||||
}
|
||||
return &dto.OrderDetailResponse{OrderResponse: resp, Payments: payments}, nil
|
||||
}
|
||||
|
||||
func (s *Service) List(ctx context.Context, req *dto.OrderListRequest, startTime, endTime *time.Time, buyerType string, buyerID uint) (*dto.OrderListResponse, error) {
|
||||
page := req.Page
|
||||
pageSize := req.PageSize
|
||||
@@ -1387,6 +1408,9 @@ func (s *Service) List(ctx context.Context, req *dto.OrderListRequest, startTime
|
||||
for _, o := range orders {
|
||||
list = append(list, s.buildOrderResponse(ctx, o, itemsMap[o.ID]))
|
||||
}
|
||||
if err := s.fillOrderPaymentInfo(ctx, orders, list); err != nil {
|
||||
return nil, err
|
||||
}
|
||||
|
||||
return &dto.OrderListResponse{
|
||||
List: list,
|
||||
|
||||
@@ -195,7 +195,7 @@ type shopBusinessOwnerImportRow struct {
|
||||
ColumnCountMatched bool
|
||||
ShopCode string
|
||||
OperationType string
|
||||
OwnerUsername string
|
||||
OwnerPhone string
|
||||
Remark string
|
||||
}
|
||||
|
||||
@@ -236,7 +236,7 @@ func parseShopBusinessOwnerImportCSV(data []byte) ([]shopBusinessOwnerImportRow,
|
||||
row.ColumnCountMatched = true
|
||||
row.ShopCode = strings.TrimSpace(record[0])
|
||||
row.OperationType = strings.TrimSpace(record[1])
|
||||
row.OwnerUsername = strings.TrimSpace(record[2])
|
||||
row.OwnerPhone = strings.TrimSpace(record[2])
|
||||
row.Remark = strings.TrimSpace(record[3])
|
||||
rows = append(rows, row)
|
||||
}
|
||||
@@ -303,11 +303,11 @@ func (h *ShopBusinessOwnerImportHandler) processRow(ctx context.Context, taskRec
|
||||
clear := false
|
||||
switch row.OperationType {
|
||||
case constants.ShopBusinessOwnerImportOperationRebind:
|
||||
if row.OwnerUsername == "" {
|
||||
if row.OwnerPhone == "" {
|
||||
return failedShopBusinessOwnerImportItem(item, constants.ShopBusinessOwnerImportRowErrorOwnerRequired), nil
|
||||
}
|
||||
case constants.ShopBusinessOwnerImportOperationClear:
|
||||
if row.OwnerUsername != "" {
|
||||
if row.OwnerPhone != "" {
|
||||
return failedShopBusinessOwnerImportItem(item, constants.ShopBusinessOwnerImportRowErrorOwnerForbidden), nil
|
||||
}
|
||||
clear = true
|
||||
@@ -316,17 +316,14 @@ func (h *ShopBusinessOwnerImportHandler) processRow(ctx context.Context, taskRec
|
||||
}
|
||||
var ownerID *uint
|
||||
if !clear {
|
||||
var account model.Account
|
||||
if err := h.db.WithContext(ctx).
|
||||
Where("username = ? AND user_type = ? AND status = ?", row.OwnerUsername, constants.UserTypePlatform, constants.StatusEnabled).
|
||||
First(&account).Error; err != nil {
|
||||
if err != gorm.ErrRecordNotFound {
|
||||
return item, pkgerrors.Wrap(pkgerrors.CodeDatabaseError, err, "查询导入目标业务员失败")
|
||||
}
|
||||
return failedShopBusinessOwnerImportItem(item, constants.ShopBusinessOwnerImportRowErrorOwnerInvalid), nil
|
||||
resolved, reason, err := h.resolveImportOwner(ctx, row.OwnerPhone)
|
||||
if err != nil {
|
||||
return item, err
|
||||
}
|
||||
value := account.ID
|
||||
ownerID = &value
|
||||
if reason != "" {
|
||||
return failedShopBusinessOwnerImportItem(item, reason), nil
|
||||
}
|
||||
ownerID = &resolved
|
||||
}
|
||||
// 每行独立事务:成功行提交,失败行回滚并保留原值。
|
||||
rowErr := h.db.WithContext(ctx).Transaction(func(tx *gorm.DB) error {
|
||||
@@ -357,6 +354,46 @@ func (h *ShopBusinessOwnerImportHandler) processRow(ctx context.Context, taskRec
|
||||
return item, nil
|
||||
}
|
||||
|
||||
// resolveImportOwner 按手机号定位换绑目标平台账号;返回非空 reason 表示该行失败。
|
||||
// 先校验格式再查库;查询不按类型与状态过滤,才能区分不存在、多匹配、非平台与已禁用。
|
||||
func (h *ShopBusinessOwnerImportHandler) resolveImportOwner(ctx context.Context, phone string) (uint, string, error) {
|
||||
if !isShopBusinessOwnerImportPhone(phone) {
|
||||
return 0, constants.ShopBusinessOwnerImportRowErrorOwnerPhoneFormat, nil
|
||||
}
|
||||
var accounts []model.Account
|
||||
if err := h.db.WithContext(ctx).Select("id", "user_type", "status").
|
||||
Where("phone = ?", phone).Limit(2).Find(&accounts).Error; err != nil {
|
||||
return 0, "", pkgerrors.Wrap(pkgerrors.CodeDatabaseError, err, "查询导入目标业务员失败")
|
||||
}
|
||||
switch {
|
||||
case len(accounts) == 0:
|
||||
return 0, constants.ShopBusinessOwnerImportRowErrorOwnerNotFound, nil
|
||||
case len(accounts) > 1:
|
||||
return 0, constants.ShopBusinessOwnerImportRowErrorOwnerAmbiguous, nil
|
||||
case accounts[0].UserType != constants.UserTypePlatform:
|
||||
return 0, constants.ShopBusinessOwnerImportRowErrorOwnerNotPlatform, nil
|
||||
case accounts[0].Status != constants.StatusEnabled:
|
||||
return 0, constants.ShopBusinessOwnerImportRowErrorOwnerDisabled, nil
|
||||
}
|
||||
return accounts[0].ID, "", nil
|
||||
}
|
||||
|
||||
// isShopBusinessOwnerImportPhone 判断平台登录账号是否为 11 位数字手机号,与项目 DTO 的 len=11,numeric 口径一致。
|
||||
func isShopBusinessOwnerImportPhone(value string) bool {
|
||||
if len(value) != shopBusinessOwnerImportPhoneLength {
|
||||
return false
|
||||
}
|
||||
for i := 0; i < len(value); i++ {
|
||||
if value[i] < '0' || value[i] > '9' {
|
||||
return false
|
||||
}
|
||||
}
|
||||
return true
|
||||
}
|
||||
|
||||
// shopBusinessOwnerImportPhoneLength 平台登录账号手机号位数。
|
||||
const shopBusinessOwnerImportPhoneLength = 11
|
||||
|
||||
// appendRowAudit 在行事务内写实际变更审计,含负责人前后值与行备注。
|
||||
// 行备注写入事件 Metadata,不进入资源前后值字段。
|
||||
func (h *ShopBusinessOwnerImportHandler) appendRowAudit(ctx context.Context, tx *gorm.DB, taskRecord *model.ShopBusinessOwnerImportTask, shop *model.Shop, before, after *uint, row shopBusinessOwnerImportRow) error {
|
||||
|
||||
Reference in New Issue
Block a user