From 2a18f0c21a9ad25f544d80ec439510f18c2c2928 Mon Sep 17 00:00:00 2001 From: laodaming <11058467+laudamine@user.noreply.gitee.com> Date: Fri, 9 Jun 2023 12:07:54 +0800 Subject: [PATCH] fix --- ddl/fs_canteen_product.sql | 14 ++ ddl/fs_canteen_type.sql | 12 +- model/fscanteenproductmodel.go | 38 ++++++ model/fscanteenproductmodel_gen.go | 90 +++++++++++++ model/fscanteentypemodel_gen.go | 2 + model/fsproductmodel.go | 18 +++ model/fsproductsizemodel.go | 24 +++- model/fstagsmodel.go | 16 ++- server/canteen/canteen.go | 31 +++++ server/canteen/etc/canteen.yaml | 8 ++ server/canteen/internal/config/config.go | 12 ++ .../handler/getcanteendetailhandler.go | 38 ++++++ server/canteen/internal/handler/routes.go | 23 ++++ .../internal/logic/getcanteendetaillogic.go | 121 ++++++++++++++++++ server/canteen/internal/svc/servicecontext.go | 19 +++ server/canteen/internal/types/types.go | 83 ++++++++++++ .../data-transfer/internal/config/config.go | 4 +- .../internal/logic/getsizebyproductlogic.go | 2 +- server_api/canteen.api | 34 +++++ 19 files changed, 573 insertions(+), 16 deletions(-) create mode 100644 ddl/fs_canteen_product.sql create mode 100755 model/fscanteenproductmodel.go create mode 100755 model/fscanteenproductmodel_gen.go create mode 100644 server/canteen/canteen.go create mode 100644 server/canteen/etc/canteen.yaml create mode 100644 server/canteen/internal/config/config.go create mode 100644 server/canteen/internal/handler/getcanteendetailhandler.go create mode 100644 server/canteen/internal/handler/routes.go create mode 100644 server/canteen/internal/logic/getcanteendetaillogic.go create mode 100644 server/canteen/internal/svc/servicecontext.go create mode 100644 server/canteen/internal/types/types.go create mode 100644 server_api/canteen.api diff --git a/ddl/fs_canteen_product.sql b/ddl/fs_canteen_product.sql new file mode 100644 index 00000000..0c899c4b --- /dev/null +++ b/ddl/fs_canteen_product.sql @@ -0,0 +1,14 @@ +-- fusentest.fs_canteen_product definition + +CREATE TABLE `fs_canteen_product` ( + `id` int(10) NOT NULL AUTO_INCREMENT COMMENT 'ID', + `canteen_type` int(10) NOT NULL DEFAULT '0' COMMENT '餐厅类别id', + `product_id` int(10) NOT NULL COMMENT '产品id', + `size_id` int(10) NOT NULL COMMENT '尺寸id', + `sort` tinyint(3) NOT NULL DEFAULT '0' COMMENT '排序', + `status` tinyint(1) NOT NULL DEFAULT '0' COMMENT '状态位 1启用0停用', + `ctime` int(11) NOT NULL DEFAULT '0' COMMENT '添加时间', + `sid` varchar(50) NOT NULL DEFAULT '' COMMENT '前端带入的id', + PRIMARY KEY (`id`), + KEY `canteen_type` (`canteen_type`) +) ENGINE=MyISAM AUTO_INCREMENT=46 DEFAULT CHARSET=utf8 COMMENT='餐厅类别产品对应表'; \ No newline at end of file diff --git a/ddl/fs_canteen_type.sql b/ddl/fs_canteen_type.sql index ea03ec71..8ceebe85 100644 --- a/ddl/fs_canteen_type.sql +++ b/ddl/fs_canteen_type.sql @@ -1,10 +1,10 @@ -- fusentest.fs_canteen_type definition CREATE TABLE `fs_canteen_type` ( - `id` int(10) NOT NULL AUTO_INCREMENT COMMENT 'ID', - `name` varchar(50) NOT NULL DEFAULT '' COMMENT '餐厅名字', - `sort` smallint(6) NOT NULL DEFAULT '0' COMMENT '排序', - `status` tinyint(1) NOT NULL DEFAULT '0' COMMENT '状态位 1启用0停用', - `ctime` int(11) NOT NULL DEFAULT '0' COMMENT '添加时间', - PRIMARY KEY (`id`) + `id` int(10) NOT NULL AUTO_INCREMENT COMMENT 'ID', + `name` varchar(50) NOT NULL DEFAULT '' COMMENT '餐厅名字', + `sort` smallint(6) NOT NULL DEFAULT '0' COMMENT '排序', + `status` tinyint(1) NOT NULL DEFAULT '0' COMMENT '状态位 1启用0停用', + `ctime` int(11) NOT NULL DEFAULT '0' COMMENT '添加时间', + PRIMARY KEY (`id`) ) ENGINE=MyISAM AUTO_INCREMENT=24 DEFAULT CHARSET=utf8 COMMENT='餐厅类型表'; \ No newline at end of file diff --git a/model/fscanteenproductmodel.go b/model/fscanteenproductmodel.go new file mode 100755 index 00000000..839cf956 --- /dev/null +++ b/model/fscanteenproductmodel.go @@ -0,0 +1,38 @@ +package model + +import ( + "context" + "fmt" + "github.com/zeromicro/go-zero/core/stores/sqlx" +) + +var _ FsCanteenProductModel = (*customFsCanteenProductModel)(nil) + +type ( + // FsCanteenProductModel is an interface to be customized, add more methods here, + // and implement the added methods in customFsCanteenProductModel. + FsCanteenProductModel interface { + fsCanteenProductModel + GetAllByCanteenTypeId(ctx context.Context, canteenTypeId int64) (resp []FsCanteenProduct, err error) + } + + customFsCanteenProductModel struct { + *defaultFsCanteenProductModel + } +) + +// NewFsCanteenProductModel returns a model for the database table. +func NewFsCanteenProductModel(conn sqlx.SqlConn) FsCanteenProductModel { + return &customFsCanteenProductModel{ + defaultFsCanteenProductModel: newFsCanteenProductModel(conn), + } +} + +func (m *defaultFsCanteenProductModel) GetAllByCanteenTypeId(ctx context.Context, canteenTypeId int64) (resp []FsCanteenProduct, err error) { + query := fmt.Sprintf("select %s from %s where `canteen_type` = ? and `status` = ?", fsCanteenProductRows, m.table) + err = m.conn.QueryRowsCtx(ctx, &resp, query, canteenTypeId, 1) + if err != nil { + return nil, err + } + return +} diff --git a/model/fscanteenproductmodel_gen.go b/model/fscanteenproductmodel_gen.go new file mode 100755 index 00000000..dc30d4dc --- /dev/null +++ b/model/fscanteenproductmodel_gen.go @@ -0,0 +1,90 @@ +// Code generated by goctl. DO NOT EDIT. + +package model + +import ( + "context" + "database/sql" + "fmt" + "strings" + + "github.com/zeromicro/go-zero/core/stores/builder" + "github.com/zeromicro/go-zero/core/stores/sqlc" + "github.com/zeromicro/go-zero/core/stores/sqlx" + "github.com/zeromicro/go-zero/core/stringx" +) + +var ( + fsCanteenProductFieldNames = builder.RawFieldNames(&FsCanteenProduct{}) + fsCanteenProductRows = strings.Join(fsCanteenProductFieldNames, ",") + fsCanteenProductRowsExpectAutoSet = strings.Join(stringx.Remove(fsCanteenProductFieldNames, "`id`", "`create_at`", "`create_time`", "`created_at`", "`update_at`", "`update_time`", "`updated_at`"), ",") + fsCanteenProductRowsWithPlaceHolder = strings.Join(stringx.Remove(fsCanteenProductFieldNames, "`id`", "`create_at`", "`create_time`", "`created_at`", "`update_at`", "`update_time`", "`updated_at`"), "=?,") + "=?" +) + +type ( + fsCanteenProductModel interface { + Insert(ctx context.Context, data *FsCanteenProduct) (sql.Result, error) + FindOne(ctx context.Context, id int64) (*FsCanteenProduct, error) + Update(ctx context.Context, data *FsCanteenProduct) error + Delete(ctx context.Context, id int64) error + } + + defaultFsCanteenProductModel struct { + conn sqlx.SqlConn + table string + } + + FsCanteenProduct struct { + Id int64 `db:"id"` // ID + CanteenType int64 `db:"canteen_type"` // 餐厅类别id + ProductId int64 `db:"product_id"` // 产品id + SizeId int64 `db:"size_id"` // 尺寸id + Sort int64 `db:"sort"` // 排序 + Status int64 `db:"status"` // 状态位 1启用0停用 + Ctime int64 `db:"ctime"` // 添加时间 + Sid string `db:"sid"` // 前端带入的id + } +) + +func newFsCanteenProductModel(conn sqlx.SqlConn) *defaultFsCanteenProductModel { + return &defaultFsCanteenProductModel{ + conn: conn, + table: "`fs_canteen_product`", + } +} + +func (m *defaultFsCanteenProductModel) Delete(ctx context.Context, id int64) error { + query := fmt.Sprintf("delete from %s where `id` = ?", m.table) + _, err := m.conn.ExecCtx(ctx, query, id) + return err +} + +func (m *defaultFsCanteenProductModel) FindOne(ctx context.Context, id int64) (*FsCanteenProduct, error) { + query := fmt.Sprintf("select %s from %s where `id` = ? limit 1", fsCanteenProductRows, m.table) + var resp FsCanteenProduct + err := m.conn.QueryRowCtx(ctx, &resp, query, id) + switch err { + case nil: + return &resp, nil + case sqlc.ErrNotFound: + return nil, ErrNotFound + default: + return nil, err + } +} + +func (m *defaultFsCanteenProductModel) Insert(ctx context.Context, data *FsCanteenProduct) (sql.Result, error) { + query := fmt.Sprintf("insert into %s (%s) values (?, ?, ?, ?, ?, ?, ?)", m.table, fsCanteenProductRowsExpectAutoSet) + ret, err := m.conn.ExecCtx(ctx, query, data.CanteenType, data.ProductId, data.SizeId, data.Sort, data.Status, data.Ctime, data.Sid) + return ret, err +} + +func (m *defaultFsCanteenProductModel) Update(ctx context.Context, data *FsCanteenProduct) error { + query := fmt.Sprintf("update %s set %s where `id` = ?", m.table, fsCanteenProductRowsWithPlaceHolder) + _, err := m.conn.ExecCtx(ctx, query, data.CanteenType, data.ProductId, data.SizeId, data.Sort, data.Status, data.Ctime, data.Sid, data.Id) + return err +} + +func (m *defaultFsCanteenProductModel) tableName() string { + return m.table +} diff --git a/model/fscanteentypemodel_gen.go b/model/fscanteentypemodel_gen.go index 9c1e4427..1c44ca2b 100755 --- a/model/fscanteentypemodel_gen.go +++ b/model/fscanteentypemodel_gen.go @@ -1,3 +1,5 @@ +// Code generated by goctl. DO NOT EDIT. + package model import ( diff --git a/model/fsproductmodel.go b/model/fsproductmodel.go index c6ae4356..3db3ba48 100755 --- a/model/fsproductmodel.go +++ b/model/fsproductmodel.go @@ -16,6 +16,7 @@ type ( fsProductModel GetProductListByConditions(ctx context.Context, productTypes []string, sort string) ([]FsProduct, error) GetRandomProductList(ctx context.Context, limit int) (resp []FsProduct, err error) + GetProductListByIds(ctx context.Context, ids []string, sort string) (resp []FsProduct, err error) } customFsProductModel struct { @@ -55,3 +56,20 @@ func (m *defaultFsProductModel) GetRandomProductList(ctx context.Context, limit } return } +func (m *defaultFsProductModel) GetProductListByIds(ctx context.Context, ids []string, sort string) (resp []FsProduct, err error) { + query := fmt.Sprintf("select %s from %s where `id` in (?) and `is_del` =? and `is_shelf` = ? and `status` =?", + fsProductRows, m.table) + switch sort { + case "sort-asc": + query = fmt.Sprintf("%s order by sort ASC", query) + case "sort-desc": + query = fmt.Sprintf("%s order by sort DESC", query) + default: + query = fmt.Sprintf("%s order by sort DESC", query) + } + err = m.conn.QueryRowsCtx(ctx, &resp, query, strings.Join(ids, ","), 0, 1, 1) + if err != nil { + return nil, err + } + return +} diff --git a/model/fsproductsizemodel.go b/model/fsproductsizemodel.go index c33f33f6..45248476 100755 --- a/model/fsproductsizemodel.go +++ b/model/fsproductsizemodel.go @@ -15,7 +15,8 @@ type ( FsProductSizeModel interface { fsProductSizeModel CountByStatus(ctx context.Context, status int) (total int, err error) - FindAllByProductIds(ctx context.Context, productIds []string, sort int) (resp []FsProductSize, err error) + GetAllByProductIds(ctx context.Context, productIds []string, sort string) (resp []FsProductSize, err error) + GetAllByiIds(ctx context.Context, ids []string, sort string) (resp []FsProductSize, err error) } customFsProductSizeModel struct { @@ -37,12 +38,12 @@ func (m *defaultFsProductSizeModel) CountByStatus(ctx context.Context, status in } return } -func (m *defaultFsProductSizeModel) FindAllByProductIds(ctx context.Context, productIds []string, sort int) (resp []FsProductSize, err error) { +func (m *defaultFsProductSizeModel) GetAllByProductIds(ctx context.Context, productIds []string, sort string) (resp []FsProductSize, err error) { query := fmt.Sprintf("select %s from %s where `product_id` in(?) and `status` = ? ", fsProductSizeRows, m.table) switch sort { - case 1: + case "sort-asc": query = fmt.Sprintf("%s order by `sort` ASC", query) - case 2: + case "sort-desc": query = fmt.Sprintf("%s order by `sort` DESC", query) } err = m.conn.QueryRowsCtx(ctx, &resp, query, strings.Join(productIds, ","), 1) @@ -51,3 +52,18 @@ func (m *defaultFsProductSizeModel) FindAllByProductIds(ctx context.Context, pro } return } + +func (m *defaultFsProductSizeModel) GetAllByiIds(ctx context.Context, ids []string, sort string) (resp []FsProductSize, err error) { + query := fmt.Sprintf("select %s from %s where `id` in(?) and `status` = ? ", fsProductSizeRows, m.table) + switch sort { + case "sort-asc": + query = fmt.Sprintf("%s order by `sort` ASC", query) + case "sort-desc": + query = fmt.Sprintf("%s order by `sort` DESC", query) + } + err = m.conn.QueryRowsCtx(ctx, &resp, query, strings.Join(ids, ","), 1) + if err != nil { + return nil, err + } + return +} diff --git a/model/fstagsmodel.go b/model/fstagsmodel.go index 97612541..ab53637c 100755 --- a/model/fstagsmodel.go +++ b/model/fstagsmodel.go @@ -4,6 +4,7 @@ import ( "context" "fmt" "github.com/zeromicro/go-zero/core/stores/sqlx" + "strings" ) var _ FsTagsModel = (*customFsTagsModel)(nil) @@ -13,7 +14,8 @@ type ( // and implement the added methods in customFsTagsModel. FsTagsModel interface { fsTagsModel - ListAllByLevelStatus(ctx context.Context, level int, status int) (resp []FsTags, err error) + GetAllByLevel(ctx context.Context, level int) (resp []FsTags, err error) + GetAllByIds(ctx context.Context, ids []string) (resp []FsTags, err error) } customFsTagsModel struct { @@ -28,9 +30,17 @@ func NewFsTagsModel(conn sqlx.SqlConn) FsTagsModel { } } -func (m *defaultFsTagsModel) ListAllByLevelStatus(ctx context.Context, level int, status int) (resp []FsTags, err error) { +func (m *defaultFsTagsModel) GetAllByLevel(ctx context.Context, level int) (resp []FsTags, err error) { query := fmt.Sprintf("select %s from %s where `level` = ? and `status` = ?", fsTagsRows, m.table) - err = m.conn.QueryRowsCtx(ctx, &resp, query, level, status) + err = m.conn.QueryRowsCtx(ctx, &resp, query, level, 1) + if err != nil { + return nil, err + } + return +} +func (m *defaultFsTagsModel) GetAllByIds(ctx context.Context, ids []string) (resp []FsTags, err error) { + query := fmt.Sprintf("select %s from %s where `id` in (?) and `status` = ?", fsTagsRows, m.table) + err = m.conn.QueryRowsCtx(ctx, &resp, query, strings.Join(ids, ","), 1) if err != nil { return nil, err } diff --git a/server/canteen/canteen.go b/server/canteen/canteen.go new file mode 100644 index 00000000..117183e1 --- /dev/null +++ b/server/canteen/canteen.go @@ -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() +} diff --git a/server/canteen/etc/canteen.yaml b/server/canteen/etc/canteen.yaml new file mode 100644 index 00000000..b8ce24d8 --- /dev/null +++ b/server/canteen/etc/canteen.yaml @@ -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 diff --git a/server/canteen/internal/config/config.go b/server/canteen/internal/config/config.go new file mode 100644 index 00000000..59d7e610 --- /dev/null +++ b/server/canteen/internal/config/config.go @@ -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 +} diff --git a/server/canteen/internal/handler/getcanteendetailhandler.go b/server/canteen/internal/handler/getcanteendetailhandler.go new file mode 100644 index 00000000..6333b150 --- /dev/null +++ b/server/canteen/internal/handler/getcanteendetailhandler.go @@ -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) + } + } +} diff --git a/server/canteen/internal/handler/routes.go b/server/canteen/internal/handler/routes.go new file mode 100644 index 00000000..b88da32f --- /dev/null +++ b/server/canteen/internal/handler/routes.go @@ -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), + ) +} diff --git a/server/canteen/internal/logic/getcanteendetaillogic.go b/server/canteen/internal/logic/getcanteendetaillogic.go new file mode 100644 index 00000000..7e655cfd --- /dev/null +++ b/server/canteen/internal/logic/getcanteendetaillogic.go @@ -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, + }) +} diff --git a/server/canteen/internal/svc/servicecontext.go b/server/canteen/internal/svc/servicecontext.go new file mode 100644 index 00000000..be0da7ad --- /dev/null +++ b/server/canteen/internal/svc/servicecontext.go @@ -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), + } +} diff --git a/server/canteen/internal/types/types.go b/server/canteen/internal/types/types.go new file mode 100644 index 00000000..b67a28a5 --- /dev/null +++ b/server/canteen/internal/types/types.go @@ -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 +} diff --git a/server/data-transfer/internal/config/config.go b/server/data-transfer/internal/config/config.go index cd024e85..ee387fda 100644 --- a/server/data-transfer/internal/config/config.go +++ b/server/data-transfer/internal/config/config.go @@ -1,12 +1,12 @@ package config import ( - types2 "fusenapi/server/data-transfer/internal/types" + types "fusenapi/server/data-transfer/internal/types" "github.com/zeromicro/go-zero/rest" ) type Config struct { rest.RestConf SourceMysql string - Auth types2.Auth + Auth types.Auth } diff --git a/server/product/internal/logic/getsizebyproductlogic.go b/server/product/internal/logic/getsizebyproductlogic.go index 8cb2d767..d61f7cfd 100644 --- a/server/product/internal/logic/getsizebyproductlogic.go +++ b/server/product/internal/logic/getsizebyproductlogic.go @@ -58,7 +58,7 @@ func (l *GetSizeByProductLogic) GetSizeByProduct() (resp *types.Response) { productIds = append(productIds, fmt.Sprintf("%d", v.Id)) } productSizeModel := model.NewFsProductSizeModel(l.svcCtx.MysqlConn) - productSizeList, err := productSizeModel.FindAllByProductIds(l.ctx, productIds, 2) + productSizeList, err := productSizeModel.GetAllByProductIds(l.ctx, productIds, "sort-desc") if err != nil { logx.Error(err) return resp.SetStatusWithMessage(basic.CodeServiceErr, "failed to get product size list") diff --git a/server_api/canteen.api b/server_api/canteen.api new file mode 100644 index 00000000..9eef053f --- /dev/null +++ b/server_api/canteen.api @@ -0,0 +1,34 @@ +syntax = "v1" + +info ( + title: "餐厅服务"// TODO: add title + desc: // TODO: add description + author: "daming" + email: "" +) +import "basic.api" +//验证登录 +@server( + jwt: Auth +) +service canteen { + //获取餐厅详情 + @handler GetCanteenDetailHandler + post /canteen-type/detail(GetCanteenDetailReq) returns (response); +} + +//获取餐厅详情 +type GetCanteenDetailReq { + Id int64 `json:"id"` +} +type GetCanteenDetailRsp { + Id int64 `json:"id"` + Name string `json:"name"` + ProductList []CanteenProduct `json:"product_list"` +} +type CanteenProduct { + Id int64 `json:"id"` + Name string `json:"name"` + SizeId int64 `json:"size_id"` + SId string `json:"s_id"` +} \ No newline at end of file