Files
tyc-server-v2/app/main/api/internal/logic/query/querylistlogic.go

110 lines
3.3 KiB
Go
Raw Normal View History

2026-01-22 16:04:12 +08:00
package query
import (
"context"
2026-05-25 17:01:52 +08:00
"strings"
2026-01-22 16:04:12 +08:00
"tyc-server/app/main/api/internal/svc"
"tyc-server/app/main/api/internal/types"
"tyc-server/app/main/model"
"tyc-server/common/ctxdata"
"tyc-server/common/xerr"
"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,
}
}
2026-05-25 17:01:52 +08:00
// isVehicleProduct 判断 product_en 是否为车辆类产品
func isVehicleProduct(productEn string) bool {
vehiclePrefixes := []string{
"toc_Vehicle",
"toc_PersonVehicle",
}
for _, p := range vehiclePrefixes {
if strings.HasPrefix(productEn, p) {
return true
}
}
return false
}
2026-01-22 16:04:12 +08:00
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)
}
2026-05-25 17:01:52 +08:00
// 小程序端需要过滤非车辆产品,取更大分页后内存过滤
pageSize := req.PageSize
isMiniapp := req.Source == "miniapp"
if isMiniapp && pageSize < 200 {
pageSize = 200
}
2026-01-22 16:04:12 +08:00
// 直接构建查询query表的条件
build := l.svcCtx.QueryModel.SelectBuilder().Where(squirrel.Eq{
"user_id": userID,
})
// 直接从query表分页查询
2026-05-25 17:01:52 +08:00
queryList, total, err := l.svcCtx.QueryModel.FindPageListByPageWithTotal(l.ctx, build, req.Page, pageSize, "create_time DESC")
2026-01-22 16:04:12 +08:00
if err != nil {
return nil, errors.Wrapf(xerr.NewErrCode(xerr.DB_ERROR), "报告列表查询, 查找报告列表错误, %+v", err)
}
var list []types.Query
2026-05-25 17:01:52 +08:00
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)
}
// 小程序端过滤非车辆类产品H5 端 source 为空,不做过滤)
if isMiniapp && !isVehicleProduct(product.ProductEn) {
continue
}
2026-01-22 16:04:12 +08:00
2026-05-25 17:01:52 +08:00
// 检查订单状态,如果订单已退款,则设置查询状态为已退款
order, findOrderErr := l.svcCtx.OrderModel.FindOne(l.ctx, queryModel.OrderId)
if findOrderErr == nil && order.Status == model.OrderStatusRefunded {
query.QueryState = model.QueryStateRefunded
2026-01-22 16:04:12 +08:00
}
2026-05-25 17:01:52 +08:00
query.ProductName = product.ProductName
query.Product = product.ProductEn
list = append(list, query)
}
// H5 端返回数据库原始 total小程序端返回过滤后的实际数量
respTotal := total
if isMiniapp {
respTotal = int64(len(list))
2026-01-22 16:04:12 +08:00
}
return &types.QueryListResp{
2026-05-25 17:01:52 +08:00
Total: respTotal,
2026-01-22 16:04:12 +08:00
List: list,
}, nil
}