Files
junhong_cmp_fiber/internal/service/refund/asset_post_attempt_query.go
邹进 3f593b912d
Some checks failed
构建并部署到测试环境(无 SSH) / build-and-deploy (push) Failing after 4m7s
feat: 记录查询列表补齐 ICCID、设备号、套餐名称、运营商名称与操作人
分页后按当前页 ID 批量查主键补齐展示字段,不在分页查询中关联,也不在记录表冗余:
- 采集成功记录补 ICCID、运营商名称;采集失败、卡日流量补运营商名称
- 套餐日流量详单、冲正流水补套餐名称、ICCID、设备号与 IMEI
- 冲正流水(含资产详情)补操作人用户名;退款后处理尝试记录补设备号与 IMEI
2026-11-07 17:07:27 +09:00

109 lines
4.4 KiB
Go

package refund
import (
"context"
"strings"
"time"
"github.com/break/junhong_cmp_fiber/internal/domain/cardflow"
"github.com/break/junhong_cmp_fiber/internal/model"
"github.com/break/junhong_cmp_fiber/internal/model/dto"
"github.com/break/junhong_cmp_fiber/internal/query/recorddisplay"
"github.com/break/junhong_cmp_fiber/pkg/constants"
"github.com/break/junhong_cmp_fiber/pkg/errors"
"github.com/break/junhong_cmp_fiber/pkg/middleware"
)
// attemptQueryMaxSpan 是尝试记录查询的最长时间跨度。
const attemptQueryMaxSpan = 31 * 24 * time.Hour
var attemptTimeLayouts = []string{time.RFC3339, "2006-01-02 15:04:05", "2006-01-02"}
// ListAssetPostProcessAttempts 分页查询退款资产后处理尝试记录(仅平台账号;可查单张退款单或单个资产,也可查全部)。
func (s *Service) ListAssetPostProcessAttempts(ctx context.Context, req *dto.RefundPostProcessAttemptListRequest) (*dto.RefundPostProcessAttemptListResponse, error) {
userType := middleware.GetUserTypeFromContext(ctx)
if userType != constants.UserTypeSuperAdmin && userType != constants.UserTypePlatform {
return nil, errors.New(errors.CodeForbidden, "只有平台账号可以查询退款资产后处理尝试记录")
}
from, err := parseAttemptTime(req.StartTime)
if err != nil {
return nil, err
}
to, err := parseAttemptTime(req.EndTime)
if err != nil {
return nil, err
}
if !to.After(from) || to.Sub(from) > attemptQueryMaxSpan {
return nil, errors.New(errors.CodeInvalidParam, "结束时间必须晚于开始时间,且跨度不超过 31 天")
}
page, size := req.Page, req.PageSize
if page <= 0 {
page = 1
}
if size <= 0 || size > 100 {
size = 20
}
resp := &dto.RefundPostProcessAttemptListResponse{Items: []dto.RefundPostProcessAttemptItem{}, Page: page, PageSize: size}
query := s.db.WithContext(ctx).Model(&model.RefundPostProcessAttempt{}).Where("created_at >= ? AND created_at < ?", from, to)
if req.RefundID != nil {
query = query.Where("refund_id = ?", *req.RefundID)
}
if refundNo := strings.TrimSpace(req.RefundNo); refundNo != "" {
query = query.Where("refund_no = ?", refundNo)
}
if identifier := strings.TrimSpace(req.Identifier); identifier != "" {
query = query.Where(`(iccid = ? OR (asset_type = 'iot_card' AND asset_id IN (SELECT id FROM tb_iot_card WHERE virtual_no = ? OR msisdn = ?))
OR (asset_type = 'device' AND asset_id IN (SELECT id FROM tb_device WHERE virtual_no = ? OR imei = ? OR sn = ?)))`,
identifier, identifier, identifier, identifier, identifier, identifier)
}
for column, value := range map[string]string{"step": req.Step, "result": req.Result, "error_code": req.ErrorCode, "trigger": req.Trigger} {
if value != "" {
query = query.Where(column+" = ?", value)
}
}
var total int64
if err := query.Count(&total).Error; err != nil {
return nil, errors.Wrap(errors.CodeDatabaseError, err, "查询退款资产后处理尝试记录失败")
}
resp.Total = total
if total == 0 {
return resp, nil
}
var rows []model.RefundPostProcessAttempt
if err := query.Order("created_at DESC, id DESC").Offset((page - 1) * size).Limit(size).Find(&rows).Error; err != nil {
return nil, errors.Wrap(errors.CodeDatabaseError, err, "查询退款资产后处理尝试记录失败")
}
displayReq := recorddisplay.Request{}
for _, row := range rows {
if row.AssetType == constants.AssetTypeDevice {
displayReq.DeviceIDs = append(displayReq.DeviceIDs, row.AssetID)
}
}
display, err := recorddisplay.Load(ctx, s.db, displayReq)
if err != nil {
return nil, err
}
for _, row := range rows {
var device recorddisplay.Device
if row.AssetType == constants.AssetTypeDevice {
device = display.Device(row.AssetID)
}
resp.Items = append(resp.Items, dto.RefundPostProcessAttemptItem{
ID: row.ID, RefundID: row.RefundID, RefundNo: row.RefundNo, Step: row.Step, AssetType: row.AssetType,
AssetID: row.AssetID, ICCID: row.ICCID, DeviceVirtualNo: device.VirtualNo, DeviceIMEI: device.IMEI, Attempt: row.Attempt, Trigger: row.Trigger, Result: row.Result,
ErrorCode: row.ErrorCode, ErrorMessage: row.ErrorMessage, DurationMS: row.DurationMS, CreatedAt: row.CreatedAt,
})
}
return resp, nil
}
func parseAttemptTime(value string) (time.Time, error) {
location := cardflow.ShanghaiDate(time.Now()).Location()
for _, layout := range attemptTimeLayouts {
if t, err := time.ParseInLocation(layout, value, location); err == nil {
return t, nil
}
}
return time.Time{}, errors.New(errors.CodeInvalidParam, "时间格式无效")
}