fusenapi/server/canteen/internal/logic/getcanteendetaillogic.go
laodaming 417ee59b39 fix
2023-06-09 16:45:29 +08:00

122 lines
3.7 KiB
Go

package logic
import (
"context"
"errors"
"fmt"
"fusenapi/model"
"fusenapi/utils/basic"
"github.com/zeromicro/go-zero/core/stores/sqlx"
"fusenapi/server/canteen/internal/svc"
"fusenapi/server/canteen/internal/types"
"github.com/zeromicro/go-zero/core/logx"
)
type GetCanteenDetailLogic struct {
logx.Logger
ctx context.Context
svcCtx *svc.ServiceContext
}
func NewGetCanteenDetailLogic(ctx context.Context, svcCtx *svc.ServiceContext) *GetCanteenDetailLogic {
return &GetCanteenDetailLogic{
Logger: logx.WithContext(ctx),
ctx: ctx,
svcCtx: svcCtx,
}
}
// 获取餐厅详情
func (l *GetCanteenDetailLogic) GetCanteenDetail(req *types.GetCanteenDetailReq) (resp *types.Response) {
//获取餐厅类型数据
canteenTypeModel := model.NewFsCanteenTypeModel(l.svcCtx.MysqlConn)
canteenTypeInfo, err := canteenTypeModel.FindOne(l.ctx, req.Id)
if err != nil && !errors.Is(err, sqlx.ErrNotFound) {
logx.Error(err)
return resp.SetStatusWithMessage(basic.CodeServiceErr, "failed to get canteen type info ")
}
if canteenTypeInfo == nil {
return resp.SetStatusWithMessage(basic.CodeServiceErr, "canteen type is not exists ")
}
//获取餐厅类型关联的所有产品
canteenProductModel := model.NewFsCanteenProductModel(l.svcCtx.MysqlConn)
canteenProductList, err := canteenProductModel.GetAllByCanteenTypeId(l.ctx, req.Id)
if err != nil {
logx.Error(err)
return resp.SetStatusWithMessage(basic.CodeServiceErr, "failed to get canteen product list")
}
if len(canteenProductList) == 0 {
return resp.SetStatusWithMessage(basic.CodeOK, "ok with no canteen product")
}
productIds := make([]string, 0, len(canteenProductList))
sizeIds := make([]string, 0, len(canteenProductList))
for _, v := range canteenProductList {
productIds = append(productIds, fmt.Sprintf("%d", v.ProductId))
sizeIds = append(sizeIds, fmt.Sprintf("%d", v.SizeId))
}
productModel := model.NewFsProductModel(l.svcCtx.MysqlConn)
productList, err := productModel.GetProductListByIds(l.ctx, productIds, "")
if err != nil {
logx.Error(err)
return resp.SetStatusWithMessage(basic.CodeServiceErr, "failed to get product list")
}
mapProduct := make(map[int64]model.FsProduct)
tagIds := make([]string, 0, len(productList))
for _, v := range productList {
mapProduct[v.Id] = v
tagIds = append(tagIds, fmt.Sprintf("%d", v.Type))
}
//获取分类列表
tagModel := model.NewFsTagsModel(l.svcCtx.MysqlConn)
tagList, err := tagModel.GetAllByIds(l.ctx, tagIds)
if err != nil {
logx.Error(err)
return resp.SetStatusWithMessage(basic.CodeServiceErr, "failed to get tag list")
}
mapTag := make(map[int64]model.FsTags)
for _, v := range tagList {
mapTag[v.Id] = v
}
//获取尺寸列表
productSizeModel := model.NewFsProductSizeModel(l.svcCtx.MysqlConn)
productSizeList, err := productSizeModel.GetAllByIds(l.ctx, sizeIds, "")
if err != nil {
logx.Error(err)
return resp.SetStatusWithMessage(basic.CodeServiceErr, "failed to get product size list")
}
mapSize := make(map[int64]model.FsProductSize)
for _, v := range productSizeList {
mapSize[v.Id] = v
}
//组装返回
list := make([]types.CanteenProduct, 0, len(canteenProductList))
for _, v := range canteenProductList {
data := types.CanteenProduct{
Id: v.Id,
SizeId: v.SizeId,
SId: v.Sid,
}
p, ok := mapProduct[v.ProductId]
if !ok {
continue
}
tag, ok := mapTag[p.Type]
if !ok {
continue
}
size, ok := mapSize[v.SizeId]
if !ok {
continue
}
data.Name = fmt.Sprintf("%s-%s-%s", tag.Title, p.Title, size.Title)
list = append(list, data)
}
return resp.SetStatusWithMessage(basic.CodeOK, "success", types.GetCanteenDetailRsp{
Id: canteenTypeInfo.Id,
Name: canteenTypeInfo.Name,
ProductList: list,
})
}