Files
tyc-server-v2/app/main/api/internal/logic/query/querylistlogic.go
2026-05-25 17:01:52 +08:00

110 lines
3.3 KiB
Go
Raw Blame History

This file contains ambiguous Unicode characters

This file contains Unicode characters that might be confused with other characters. If you think that this is intentional, you can safely ignore this warning. Use the Escape button to reveal them.

package query
import (
"context"
"strings"
"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,
}
}
// 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
}
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)
}
// 小程序端需要过滤非车辆产品,取更大分页后内存过滤
pageSize := req.PageSize
isMiniapp := req.Source == "miniapp"
if isMiniapp && pageSize < 200 {
pageSize = 200
}
// 直接构建查询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, pageSize, "create_time DESC")
if err != nil {
return nil, errors.Wrapf(xerr.NewErrCode(xerr.DB_ERROR), "报告列表查询, 查找报告列表错误, %+v", err)
}
var list []types.Query
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
}
// 检查订单状态,如果订单已退款,则设置查询状态为已退款
order, findOrderErr := l.svcCtx.OrderModel.FindOne(l.ctx, queryModel.OrderId)
if findOrderErr == nil && order.Status == model.OrderStatusRefunded {
query.QueryState = model.QueryStateRefunded
}
query.ProductName = product.ProductName
query.Product = product.ProductEn
list = append(list, query)
}
// H5 端返回数据库原始 total小程序端返回过滤后的实际数量
respTotal := total
if isMiniapp {
respTotal = int64(len(list))
}
return &types.QueryListResp{
Total: respTotal,
List: list,
}, nil
}