package logic import ( "context" "errors" "fmt" "fusenapi/model/gmodel" "fusenapi/server/canteen/internal/svc" "fusenapi/server/canteen/internal/types" "fusenapi/utils/auth" "fusenapi/utils/basic" "gorm.io/gorm" "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, userinfo *auth.UserInfo) (resp *basic.Response) { if userinfo.GetIdType() != auth.IDTYPE_User { return resp.SetStatusWithMessage(basic.CodeServiceErr, "please login first") } //获取餐厅类型数据 canteenTypeModel := gmodel.NewFsCanteenTypeModel(l.svcCtx.MysqlConn) canteenTypeInfo, err := canteenTypeModel.FindOne(l.ctx, req.Id) if err != nil { if errors.Is(err, gorm.ErrRecordNotFound) { return resp.SetStatusWithMessage(basic.CodeServiceErr, "canteen type is not exists ") } logx.Error(err) return resp.SetStatusWithMessage(basic.CodeServiceErr, "failed to get canteen type info ") } //获取餐厅类型关联的所有产品 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, }) }