fusenapi/server/canteen/internal/logic/getcanteendetaillogic.go

125 lines
3.8 KiB
Go
Raw Normal View History

2023-06-09 04:07:54 +00:00
package logic
import (
"context"
2023-06-20 09:28:28 +00:00
"errors"
2023-06-09 04:07:54 +00:00
"fmt"
2023-06-12 08:47:48 +00:00
"fusenapi/model/gmodel"
2023-06-09 04:07:54 +00:00
"fusenapi/server/canteen/internal/svc"
"fusenapi/server/canteen/internal/types"
2023-06-12 08:47:48 +00:00
"fusenapi/utils/auth"
"fusenapi/utils/basic"
2023-06-20 09:28:28 +00:00
"gorm.io/gorm"
2023-06-09 04:07:54 +00:00
"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,
}
}
// 获取餐厅详情
2023-06-15 04:00:32 +00:00
func (l *GetCanteenDetailLogic) GetCanteenDetail(req *types.GetCanteenDetailReq, userinfo *auth.UserInfo) (resp *basic.Response) {
if userinfo.GetIdType() != auth.IDTYPE_User {
2023-09-15 02:39:08 +00:00
return resp.SetStatusWithMessage(basic.CodeServiceErr, "please sign in first")
2023-06-15 04:00:32 +00:00
}
2023-06-09 04:07:54 +00:00
//获取餐厅类型数据
2023-06-12 08:47:48 +00:00
canteenTypeModel := gmodel.NewFsCanteenTypeModel(l.svcCtx.MysqlConn)
2023-06-09 04:07:54 +00:00
canteenTypeInfo, err := canteenTypeModel.FindOne(l.ctx, req.Id)
2023-06-12 08:47:48 +00:00
if err != nil {
2023-06-20 09:28:28 +00:00
if errors.Is(err, gorm.ErrRecordNotFound) {
return resp.SetStatusWithMessage(basic.CodeServiceErr, "canteen type is not exists ")
}
2023-06-09 04:07:54 +00:00
logx.Error(err)
return resp.SetStatusWithMessage(basic.CodeServiceErr, "failed to get canteen type info ")
}
//获取餐厅类型关联的所有产品
2023-06-12 08:47:48 +00:00
canteenProductModel := gmodel.NewFsCanteenProductModel(l.svcCtx.MysqlConn)
2023-06-09 04:07:54 +00:00
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")
}
2023-06-12 08:47:48 +00:00
productIds := make([]int64, 0, len(canteenProductList))
sizeIds := make([]int64, 0, len(canteenProductList))
2023-06-09 04:07:54 +00:00
for _, v := range canteenProductList {
2023-06-12 08:47:48 +00:00
productIds = append(productIds, *v.ProductId)
sizeIds = append(sizeIds, *v.SizeId)
2023-06-09 04:07:54 +00:00
}
2023-06-12 08:47:48 +00:00
productModel := gmodel.NewFsProductModel(l.svcCtx.MysqlConn)
2023-06-09 04:07:54 +00:00
productList, err := productModel.GetProductListByIds(l.ctx, productIds, "")
if err != nil {
logx.Error(err)
return resp.SetStatusWithMessage(basic.CodeServiceErr, "failed to get product list")
}
2023-06-12 08:47:48 +00:00
mapProduct := make(map[int64]gmodel.FsProduct)
tagIds := make([]int64, 0, len(productList))
2023-06-09 04:07:54 +00:00
for _, v := range productList {
mapProduct[v.Id] = v
2023-06-12 08:47:48 +00:00
tagIds = append(tagIds, *v.Type)
2023-06-09 04:07:54 +00:00
}
//获取分类列表
2023-06-12 08:47:48 +00:00
tagModel := gmodel.NewFsTagsModel(l.svcCtx.MysqlConn)
2023-06-09 04:07:54 +00:00
tagList, err := tagModel.GetAllByIds(l.ctx, tagIds)
if err != nil {
logx.Error(err)
return resp.SetStatusWithMessage(basic.CodeServiceErr, "failed to get tag list")
}
2023-06-12 08:47:48 +00:00
mapTag := make(map[int64]gmodel.FsTags)
2023-06-09 04:07:54 +00:00
for _, v := range tagList {
mapTag[v.Id] = v
}
//获取尺寸列表
2023-06-12 08:47:48 +00:00
productSizeModel := gmodel.NewFsProductSizeModel(l.svcCtx.MysqlConn)
2023-06-09 08:45:29 +00:00
productSizeList, err := productSizeModel.GetAllByIds(l.ctx, sizeIds, "")
2023-06-09 04:07:54 +00:00
if err != nil {
logx.Error(err)
return resp.SetStatusWithMessage(basic.CodeServiceErr, "failed to get product size list")
}
2023-06-12 08:47:48 +00:00
mapSize := make(map[int64]gmodel.FsProductSize)
2023-06-09 04:07:54 +00:00
for _, v := range productSizeList {
mapSize[v.Id] = v
}
//组装返回
2023-06-20 10:37:56 +00:00
list := make([]*types.CanteenProduct, 0, len(canteenProductList))
2023-06-09 04:07:54 +00:00
for _, v := range canteenProductList {
2023-06-20 10:37:56 +00:00
data := &types.CanteenProduct{
2023-06-09 04:07:54 +00:00
Id: v.Id,
2023-06-12 08:47:48 +00:00
SizeId: *v.SizeId,
SId: *v.Sid,
2023-06-09 04:07:54 +00:00
}
2023-06-12 08:47:48 +00:00
p, ok := mapProduct[*v.ProductId]
2023-06-09 04:07:54 +00:00
if !ok {
continue
}
2023-06-12 08:47:48 +00:00
tag, ok := mapTag[*p.Type]
2023-06-09 04:07:54 +00:00
if !ok {
continue
}
2023-06-12 08:47:48 +00:00
size, ok := mapSize[*v.SizeId]
2023-06-09 04:07:54 +00:00
if !ok {
continue
}
2023-06-12 10:06:47 +00:00
data.Name = fmt.Sprintf("%s-%s-%s", *tag.Title, *p.Title, *size.Title)
2023-06-09 04:07:54 +00:00
list = append(list, data)
}
return resp.SetStatusWithMessage(basic.CodeOK, "success", types.GetCanteenDetailRsp{
Id: canteenTypeInfo.Id,
2023-06-12 08:47:48 +00:00
Name: *canteenTypeInfo.Name,
2023-06-09 04:07:54 +00:00
ProductList: list,
})
}