fusenapi/server/canteen/internal/logic/getcanteendetaillogic.go
laodaming 51bd2ee53c fix
2023-06-12 17:35:37 +08:00

120 lines
3.6 KiB
Go

package logic
import (
"context"
"fmt"
"fusenapi/model/gmodel"
"fusenapi/server/canteen/internal/svc"
"fusenapi/server/canteen/internal/types"
"fusenapi/utils/auth"
"fusenapi/utils/basic"
"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, loginInfo *auth.UserInfo) (resp *basic.Response) {
//获取餐厅类型数据
canteenTypeModel := gmodel.NewFsCanteenTypeModel(l.svcCtx.MysqlConn)
canteenTypeInfo, err := canteenTypeModel.FindOne(l.ctx, req.Id)
if err != nil {
logx.Error(err)
return resp.SetStatusWithMessage(basic.CodeServiceErr, "failed to get canteen type info ")
}
if canteenTypeInfo.Id == 0 {
return resp.SetStatusWithMessage(basic.CodeServiceErr, "canteen type is not exists ")
}
//获取餐厅类型关联的所有产品
canteenProductModel := gmodel.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([]int64, 0, len(canteenProductList))
sizeIds := make([]int64, 0, len(canteenProductList))
for _, v := range canteenProductList {
productIds = append(productIds, *v.ProductId)
sizeIds = append(sizeIds, *v.SizeId)
}
productModel := gmodel.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]gmodel.FsProduct)
tagIds := make([]int64, 0, len(productList))
for _, v := range productList {
mapProduct[v.Id] = v
tagIds = append(tagIds, *v.Type)
}
//获取分类列表
tagModel := gmodel.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]gmodel.FsTags)
for _, v := range tagList {
mapTag[v.Id] = v
}
//获取尺寸列表
productSizeModel := gmodel.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]gmodel.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,
})
}