2025-11-27 13:09:54 +08:00
|
|
|
package query
|
|
|
|
|
|
|
|
|
|
import (
|
|
|
|
|
"context"
|
2025-12-23 14:35:57 +08:00
|
|
|
"encoding/hex"
|
|
|
|
|
"encoding/json"
|
|
|
|
|
"strings"
|
2025-11-27 13:09:54 +08:00
|
|
|
"ycc-server/app/main/api/internal/svc"
|
|
|
|
|
"ycc-server/app/main/api/internal/types"
|
|
|
|
|
"ycc-server/app/main/model"
|
|
|
|
|
"ycc-server/common/ctxdata"
|
|
|
|
|
"ycc-server/common/xerr"
|
2025-12-23 14:35:57 +08:00
|
|
|
"ycc-server/pkg/lzkit/crypto"
|
2025-11-27 13:09:54 +08:00
|
|
|
|
|
|
|
|
"github.com/Masterminds/squirrel"
|
|
|
|
|
"github.com/jinzhu/copier"
|
|
|
|
|
"github.com/pkg/errors"
|
|
|
|
|
"github.com/zeromicro/go-zero/core/logx"
|
|
|
|
|
)
|
|
|
|
|
|
|
|
|
|
type QueryListLogic struct {
|
|
|
|
|
logx.Logger
|
|
|
|
|
ctx context.Context
|
|
|
|
|
svcCtx *svc.ServiceContext
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
func NewQueryListLogic(ctx context.Context, svcCtx *svc.ServiceContext) *QueryListLogic {
|
|
|
|
|
return &QueryListLogic{
|
|
|
|
|
Logger: logx.WithContext(ctx),
|
|
|
|
|
ctx: ctx,
|
|
|
|
|
svcCtx: svcCtx,
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
func (l *QueryListLogic) QueryList(req *types.QueryListReq) (resp *types.QueryListResp, err error) {
|
|
|
|
|
userID, getUidErr := ctxdata.GetUidFromCtx(l.ctx)
|
|
|
|
|
if getUidErr != nil {
|
|
|
|
|
return nil, errors.Wrapf(xerr.NewErrCode(xerr.DB_ERROR), "报告列表查询, 获取用户信息失败, %+v", getUidErr)
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
// 直接构建查询query表的条件
|
|
|
|
|
build := l.svcCtx.QueryModel.SelectBuilder().Where(squirrel.Eq{
|
|
|
|
|
"user_id": userID,
|
|
|
|
|
})
|
|
|
|
|
|
|
|
|
|
// 直接从query表分页查询
|
|
|
|
|
queryList, total, err := l.svcCtx.QueryModel.FindPageListByPageWithTotal(l.ctx, build, req.Page, req.PageSize, "create_time DESC")
|
|
|
|
|
if err != nil {
|
|
|
|
|
return nil, errors.Wrapf(xerr.NewErrCode(xerr.DB_ERROR), "报告列表查询, 查找报告列表错误, %+v", err)
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
var list []types.Query
|
|
|
|
|
if len(queryList) > 0 {
|
|
|
|
|
for _, queryModel := range queryList {
|
|
|
|
|
var query types.Query
|
|
|
|
|
query.CreateTime = queryModel.CreateTime.Format("2006-01-02 15:04:05")
|
|
|
|
|
query.UpdateTime = queryModel.UpdateTime.Format("2006-01-02 15:04:05")
|
|
|
|
|
copyErr := copier.Copy(&query, queryModel)
|
|
|
|
|
if copyErr != nil {
|
|
|
|
|
return nil, errors.Wrapf(xerr.NewErrCode(xerr.SERVER_COMMON_ERROR), "报告列表查询, 报告结构体复制失败, %+v", err)
|
|
|
|
|
}
|
|
|
|
|
product, findProductErr := l.svcCtx.ProductModel.FindOne(l.ctx, queryModel.ProductId)
|
|
|
|
|
if findProductErr != nil {
|
|
|
|
|
return nil, errors.Wrapf(xerr.NewErrCode(xerr.SERVER_COMMON_ERROR), "报告列表查询, 获取商品信息失败, %+v", err)
|
|
|
|
|
}
|
2025-12-23 14:35:57 +08:00
|
|
|
|
2025-11-27 13:09:54 +08:00
|
|
|
// 检查订单状态,如果订单已退款,则设置查询状态为已退款
|
|
|
|
|
order, findOrderErr := l.svcCtx.OrderModel.FindOne(l.ctx, queryModel.OrderId)
|
2025-12-23 14:35:57 +08:00
|
|
|
if findOrderErr != nil {
|
|
|
|
|
return nil, errors.Wrapf(xerr.NewErrCode(xerr.DB_ERROR), "报告列表查询, 查询订单信息失败, %+v", findOrderErr)
|
|
|
|
|
}
|
|
|
|
|
if order.Status == model.OrderStatusRefunded {
|
2025-11-27 13:09:54 +08:00
|
|
|
query.QueryState = model.QueryStateRefunded
|
|
|
|
|
}
|
2025-12-23 14:35:57 +08:00
|
|
|
|
|
|
|
|
// 解密并脱敏params
|
|
|
|
|
var params map[string]interface{}
|
|
|
|
|
if queryModel.QueryParams != "" {
|
|
|
|
|
secretKey := l.svcCtx.Config.Encrypt.SecretKey
|
|
|
|
|
key, decodeErr := hex.DecodeString(secretKey)
|
|
|
|
|
if decodeErr != nil {
|
|
|
|
|
return nil, errors.Wrapf(xerr.NewErrCode(xerr.SERVER_COMMON_ERROR), "报告列表查询, 获取AES密钥失败, %+v", decodeErr)
|
|
|
|
|
}
|
|
|
|
|
decryptedData, decryptErr := crypto.AesDecrypt(queryModel.QueryParams, key)
|
|
|
|
|
if decryptErr != nil {
|
|
|
|
|
return nil, errors.Wrapf(xerr.NewErrCode(xerr.SERVER_COMMON_ERROR), "报告列表查询, 解密查询参数失败, %+v", decryptErr)
|
|
|
|
|
}
|
|
|
|
|
unmarshalErr := json.Unmarshal(decryptedData, ¶ms)
|
|
|
|
|
if unmarshalErr != nil {
|
|
|
|
|
return nil, errors.Wrapf(xerr.NewErrCode(xerr.SERVER_COMMON_ERROR), "报告列表查询, 解析查询参数失败, %+v", unmarshalErr)
|
|
|
|
|
}
|
|
|
|
|
// 脱敏处理
|
|
|
|
|
params = l.desensitizeParams(params)
|
|
|
|
|
} else {
|
|
|
|
|
params = make(map[string]interface{})
|
|
|
|
|
}
|
|
|
|
|
|
2025-11-27 13:09:54 +08:00
|
|
|
query.ProductName = product.ProductName
|
|
|
|
|
query.Product = product.ProductEn
|
2025-12-23 14:35:57 +08:00
|
|
|
query.Params = params
|
|
|
|
|
query.Price = order.Amount
|
2025-11-27 13:09:54 +08:00
|
|
|
list = append(list, query)
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
return &types.QueryListResp{
|
|
|
|
|
Total: total,
|
|
|
|
|
List: list,
|
|
|
|
|
}, nil
|
|
|
|
|
}
|
2025-12-23 14:35:57 +08:00
|
|
|
|
|
|
|
|
// desensitizeParams 对敏感数据进行脱敏处理
|
|
|
|
|
func (l *QueryListLogic) desensitizeParams(params map[string]interface{}) map[string]interface{} {
|
|
|
|
|
result := make(map[string]interface{})
|
|
|
|
|
for key, value := range params {
|
|
|
|
|
if strValue, ok := value.(string); ok {
|
|
|
|
|
keyLower := strings.ToLower(key)
|
|
|
|
|
if (strings.Contains(keyLower, "name") || strings.Contains(keyLower, "姓名")) && len(strValue) > 0 {
|
|
|
|
|
result[key] = l.maskName(strValue)
|
|
|
|
|
} else if (strings.Contains(keyLower, "idcard") || strings.Contains(keyLower, "id_card") || strings.Contains(keyLower, "身份证")) && len(strValue) > 10 {
|
|
|
|
|
result[key] = l.maskIDCard(strValue)
|
|
|
|
|
} else if (strings.Contains(keyLower, "mobile") || strings.Contains(keyLower, "phone") || strings.Contains(keyLower, "手机")) && len(strValue) >= 8 {
|
|
|
|
|
result[key] = l.maskPhone(strValue)
|
|
|
|
|
} else {
|
|
|
|
|
result[key] = strValue
|
|
|
|
|
}
|
|
|
|
|
} else {
|
|
|
|
|
result[key] = value
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
return result
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
// maskName 姓名脱敏
|
|
|
|
|
func (l *QueryListLogic) maskName(name string) string {
|
|
|
|
|
runes := []rune(name)
|
|
|
|
|
length := len(runes)
|
|
|
|
|
if length <= 1 {
|
|
|
|
|
return name
|
|
|
|
|
}
|
|
|
|
|
if length == 2 {
|
|
|
|
|
return string(runes[0]) + "*"
|
|
|
|
|
}
|
|
|
|
|
return string(runes[0]) + strings.Repeat("*", length-2) + string(runes[length-1])
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
// maskIDCard 身份证号脱敏
|
|
|
|
|
func (l *QueryListLogic) maskIDCard(idCard string) string {
|
|
|
|
|
length := len(idCard)
|
|
|
|
|
if length <= 10 {
|
|
|
|
|
return idCard
|
|
|
|
|
}
|
|
|
|
|
// 保留前3位和后4位
|
|
|
|
|
if length > 7 {
|
|
|
|
|
return idCard[:3] + strings.Repeat("*", length-7) + idCard[length-4:]
|
|
|
|
|
}
|
|
|
|
|
return idCard
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
// maskPhone 手机号脱敏
|
|
|
|
|
func (l *QueryListLogic) maskPhone(phone string) string {
|
|
|
|
|
length := len(phone)
|
|
|
|
|
if length < 8 {
|
|
|
|
|
return phone
|
|
|
|
|
}
|
|
|
|
|
// 保留前3位和后4位
|
|
|
|
|
if length > 7 {
|
|
|
|
|
return phone[:3] + strings.Repeat("*", length-7) + phone[length-4:]
|
|
|
|
|
}
|
|
|
|
|
return phone
|
|
|
|
|
}
|