This commit is contained in:
laodaming
2023-06-09 12:07:54 +08:00
parent 78e5b2cc59
commit 2a18f0c21a
19 changed files with 573 additions and 16 deletions

31
server/canteen/canteen.go Normal file
View File

@@ -0,0 +1,31 @@
package main
import (
"flag"
"fmt"
"fusenapi/server/canteen/internal/config"
"fusenapi/server/canteen/internal/handler"
"fusenapi/server/canteen/internal/svc"
"github.com/zeromicro/go-zero/core/conf"
"github.com/zeromicro/go-zero/rest"
)
var configFile = flag.String("f", "etc/canteen.yaml", "the config file")
func main() {
flag.Parse()
var c config.Config
conf.MustLoad(*configFile, &c)
server := rest.MustNewServer(c.RestConf)
defer server.Stop()
ctx := svc.NewServiceContext(c)
handler.RegisterHandlers(server, ctx)
fmt.Printf("Starting server at %s:%d...\n", c.Host, c.Port)
server.Start()
}

View File

@@ -0,0 +1,8 @@
Name: canteen
Host: 0.0.0.0
Port: 8891
SourceMysql: fusentest:XErSYmLELKMnf3Dh@tcp(110.41.19.98:3306)/fusentest
Auth:
AccessSecret: fusen2023
AccessExpire: 60
RefreshAfter: 60

View File

@@ -0,0 +1,12 @@
package config
import (
"fusenapi/server/canteen/internal/types"
"github.com/zeromicro/go-zero/rest"
)
type Config struct {
rest.RestConf
SourceMysql string
Auth types.Auth
}

View File

@@ -0,0 +1,38 @@
package handler
import (
"errors"
"net/http"
"github.com/zeromicro/go-zero/core/logx"
"github.com/zeromicro/go-zero/rest/httpx"
"fusenapi/server/canteen/internal/logic"
"fusenapi/server/canteen/internal/svc"
"fusenapi/server/canteen/internal/types"
)
// 获取餐厅详情
func GetCanteenDetailHandler(svcCtx *svc.ServiceContext) http.HandlerFunc {
return func(w http.ResponseWriter, r *http.Request) {
var req types.GetCanteenDetailReq
if err := httpx.Parse(r, &req); err != nil {
httpx.OkJsonCtx(r.Context(), w, &types.Response{
Code: 510,
Message: "parameter error",
})
logx.Info(err)
return
}
l := logic.NewGetCanteenDetailLogic(r.Context(), svcCtx)
resp := l.GetCanteenDetail(&req)
if resp != nil {
httpx.OkJsonCtx(r.Context(), w, resp)
} else {
err := errors.New("server logic is error, resp must not be nil")
httpx.ErrorCtx(r.Context(), w, err)
logx.Error(err)
}
}
}

View File

@@ -0,0 +1,23 @@
// Code generated by goctl. DO NOT EDIT.
package handler
import (
"net/http"
"fusenapi/server/canteen/internal/svc"
"github.com/zeromicro/go-zero/rest"
)
func RegisterHandlers(server *rest.Server, serverCtx *svc.ServiceContext) {
server.AddRoutes(
[]rest.Route{
{
Method: http.MethodPost,
Path: "/canteen-type/detail",
Handler: GetCanteenDetailHandler(serverCtx),
},
},
rest.WithJwt(serverCtx.Config.Auth.AccessSecret),
)
}

View File

@@ -0,0 +1,121 @@
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.GetAllByiIds(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,
})
}

View File

@@ -0,0 +1,19 @@
package svc
import (
"fusenapi/server/canteen/internal/config"
"github.com/zeromicro/go-zero/core/stores/sqlx"
)
type ServiceContext struct {
Config config.Config
MysqlConn sqlx.SqlConn
}
func NewServiceContext(c config.Config) *ServiceContext {
return &ServiceContext{
Config: c,
MysqlConn: sqlx.NewMysql(c.SourceMysql),
}
}

View File

@@ -0,0 +1,83 @@
// Code generated by goctl. DO NOT EDIT.
package types
import (
"fusenapi/utils/basic"
)
type GetCanteenDetailReq struct {
Id int64 `json:"id"`
}
type GetCanteenDetailRsp struct {
Id int64 `json:"id"`
Name string `json:"name"`
ProductList []CanteenProduct `json:"product_list"`
}
type CanteenProduct struct {
Id int64 `json:"id"`
Name string `json:"name"`
SizeId int64 `json:"size_id"`
SId string `json:"s_id"`
}
type Response struct {
Code int `json:"code"`
Message string `json:"msg"`
Data interface{} `json:"data"`
}
type ResponseJwt struct {
Code int `json:"code"`
Message string `json:"msg"`
Data interface{} `json:"data"`
AccessSecret string `json:"accessSecret"`
AccessExpire int64 `json:"accessExpire"`
}
type Auth struct {
AccessSecret string `json:"accessSecret"`
AccessExpire int64 `json:"accessExpire"`
RefreshAfter int64 `json:"refreshAfter"`
}
// Set 设置Response的Code和Message值
func (resp *Response) Set(Code int, Message string) *Response {
return &Response{
Code: Code,
Message: Message,
}
}
// Set 设置整个Response
func (resp *Response) SetWithData(Code int, Message string, Data interface{}) *Response {
return &Response{
Code: Code,
Message: Message,
Data: Data,
}
}
// SetStatus 设置默认StatusResponse(内部自定义) 默认msg, 可以带data, data只使用一个参数
func (resp *Response) SetStatus(sr *basic.StatusResponse, data ...interface{}) *Response {
newResp := &Response{
Code: sr.Code,
}
if len(data) == 1 {
newResp.Data = data[0]
}
return newResp
}
// SetStatusWithMessage 设置默认StatusResponse(内部自定义) 非默认msg, 可以带data, data只使用一个参数
func (resp *Response) SetStatusWithMessage(sr *basic.StatusResponse, msg string, data ...interface{}) *Response {
newResp := &Response{
Code: sr.Code,
Message: msg,
}
if len(data) == 1 {
newResp.Data = data[0]
}
return newResp
}