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

71 lines
2.2 KiB
Go
Raw Normal View History

2024-11-21 12:18:28 +08:00
package query
import (
"context"
2025-04-27 12:17:18 +08:00
"tyc-server/app/main/api/internal/svc"
"tyc-server/app/main/api/internal/types"
2025-04-09 15:58:06 +08:00
"tyc-server/common/ctxdata"
"tyc-server/common/xerr"
2024-11-21 12:18:28 +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)
}
2025-04-12 11:13:23 +08:00
// 直接构建查询query表的条件
build := l.svcCtx.QueryModel.SelectBuilder().Where(squirrel.Eq{
2024-11-21 12:18:28 +08:00
"user_id": userID,
})
2025-04-12 11:13:23 +08:00
// 直接从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)
2024-11-21 12:18:28 +08:00
}
var list []types.Query
2025-04-12 11:13:23 +08:00
if len(queryList) > 0 {
for _, queryModel := range queryList {
2024-11-21 12:18:28 +08:00
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)
}
2024-12-24 11:37:25 +08:00
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)
}
query.ProductName = product.ProductName
2024-11-21 12:18:28 +08:00
list = append(list, query)
}
}
return &types.QueryListResp{
Total: total,
List: list,
}, nil
}