fix
This commit is contained in:
parent
78e5b2cc59
commit
2a18f0c21a
14
ddl/fs_canteen_product.sql
Normal file
14
ddl/fs_canteen_product.sql
Normal file
|
@ -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='餐厅类别产品对应表';
|
38
model/fscanteenproductmodel.go
Executable file
38
model/fscanteenproductmodel.go
Executable file
|
@ -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
|
||||||
|
}
|
90
model/fscanteenproductmodel_gen.go
Executable file
90
model/fscanteenproductmodel_gen.go
Executable file
|
@ -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
|
||||||
|
}
|
|
@ -1,3 +1,5 @@
|
||||||
|
// Code generated by goctl. DO NOT EDIT.
|
||||||
|
|
||||||
package model
|
package model
|
||||||
|
|
||||||
import (
|
import (
|
||||||
|
|
|
@ -16,6 +16,7 @@ type (
|
||||||
fsProductModel
|
fsProductModel
|
||||||
GetProductListByConditions(ctx context.Context, productTypes []string, sort string) ([]FsProduct, error)
|
GetProductListByConditions(ctx context.Context, productTypes []string, sort string) ([]FsProduct, error)
|
||||||
GetRandomProductList(ctx context.Context, limit int) (resp []FsProduct, err 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 {
|
customFsProductModel struct {
|
||||||
|
@ -55,3 +56,20 @@ func (m *defaultFsProductModel) GetRandomProductList(ctx context.Context, limit
|
||||||
}
|
}
|
||||||
return
|
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
|
||||||
|
}
|
||||||
|
|
|
@ -15,7 +15,8 @@ type (
|
||||||
FsProductSizeModel interface {
|
FsProductSizeModel interface {
|
||||||
fsProductSizeModel
|
fsProductSizeModel
|
||||||
CountByStatus(ctx context.Context, status int) (total int, err error)
|
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 {
|
customFsProductSizeModel struct {
|
||||||
|
@ -37,12 +38,12 @@ func (m *defaultFsProductSizeModel) CountByStatus(ctx context.Context, status in
|
||||||
}
|
}
|
||||||
return
|
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)
|
query := fmt.Sprintf("select %s from %s where `product_id` in(?) and `status` = ? ", fsProductSizeRows, m.table)
|
||||||
switch sort {
|
switch sort {
|
||||||
case 1:
|
case "sort-asc":
|
||||||
query = fmt.Sprintf("%s order by `sort` ASC", query)
|
query = fmt.Sprintf("%s order by `sort` ASC", query)
|
||||||
case 2:
|
case "sort-desc":
|
||||||
query = fmt.Sprintf("%s order by `sort` DESC", query)
|
query = fmt.Sprintf("%s order by `sort` DESC", query)
|
||||||
}
|
}
|
||||||
err = m.conn.QueryRowsCtx(ctx, &resp, query, strings.Join(productIds, ","), 1)
|
err = m.conn.QueryRowsCtx(ctx, &resp, query, strings.Join(productIds, ","), 1)
|
||||||
|
@ -51,3 +52,18 @@ func (m *defaultFsProductSizeModel) FindAllByProductIds(ctx context.Context, pro
|
||||||
}
|
}
|
||||||
return
|
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
|
||||||
|
}
|
||||||
|
|
|
@ -4,6 +4,7 @@ import (
|
||||||
"context"
|
"context"
|
||||||
"fmt"
|
"fmt"
|
||||||
"github.com/zeromicro/go-zero/core/stores/sqlx"
|
"github.com/zeromicro/go-zero/core/stores/sqlx"
|
||||||
|
"strings"
|
||||||
)
|
)
|
||||||
|
|
||||||
var _ FsTagsModel = (*customFsTagsModel)(nil)
|
var _ FsTagsModel = (*customFsTagsModel)(nil)
|
||||||
|
@ -13,7 +14,8 @@ type (
|
||||||
// and implement the added methods in customFsTagsModel.
|
// and implement the added methods in customFsTagsModel.
|
||||||
FsTagsModel interface {
|
FsTagsModel interface {
|
||||||
fsTagsModel
|
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 {
|
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)
|
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 {
|
if err != nil {
|
||||||
return nil, err
|
return nil, err
|
||||||
}
|
}
|
||||||
|
|
31
server/canteen/canteen.go
Normal file
31
server/canteen/canteen.go
Normal 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()
|
||||||
|
}
|
8
server/canteen/etc/canteen.yaml
Normal file
8
server/canteen/etc/canteen.yaml
Normal 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
|
12
server/canteen/internal/config/config.go
Normal file
12
server/canteen/internal/config/config.go
Normal 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
|
||||||
|
}
|
38
server/canteen/internal/handler/getcanteendetailhandler.go
Normal file
38
server/canteen/internal/handler/getcanteendetailhandler.go
Normal 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)
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
23
server/canteen/internal/handler/routes.go
Normal file
23
server/canteen/internal/handler/routes.go
Normal 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),
|
||||||
|
)
|
||||||
|
}
|
121
server/canteen/internal/logic/getcanteendetaillogic.go
Normal file
121
server/canteen/internal/logic/getcanteendetaillogic.go
Normal 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,
|
||||||
|
})
|
||||||
|
}
|
19
server/canteen/internal/svc/servicecontext.go
Normal file
19
server/canteen/internal/svc/servicecontext.go
Normal 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),
|
||||||
|
}
|
||||||
|
}
|
83
server/canteen/internal/types/types.go
Normal file
83
server/canteen/internal/types/types.go
Normal 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
|
||||||
|
}
|
|
@ -1,12 +1,12 @@
|
||||||
package config
|
package config
|
||||||
|
|
||||||
import (
|
import (
|
||||||
types2 "fusenapi/server/data-transfer/internal/types"
|
types "fusenapi/server/data-transfer/internal/types"
|
||||||
"github.com/zeromicro/go-zero/rest"
|
"github.com/zeromicro/go-zero/rest"
|
||||||
)
|
)
|
||||||
|
|
||||||
type Config struct {
|
type Config struct {
|
||||||
rest.RestConf
|
rest.RestConf
|
||||||
SourceMysql string
|
SourceMysql string
|
||||||
Auth types2.Auth
|
Auth types.Auth
|
||||||
}
|
}
|
||||||
|
|
|
@ -58,7 +58,7 @@ func (l *GetSizeByProductLogic) GetSizeByProduct() (resp *types.Response) {
|
||||||
productIds = append(productIds, fmt.Sprintf("%d", v.Id))
|
productIds = append(productIds, fmt.Sprintf("%d", v.Id))
|
||||||
}
|
}
|
||||||
productSizeModel := model.NewFsProductSizeModel(l.svcCtx.MysqlConn)
|
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 {
|
if err != nil {
|
||||||
logx.Error(err)
|
logx.Error(err)
|
||||||
return resp.SetStatusWithMessage(basic.CodeServiceErr, "failed to get product size list")
|
return resp.SetStatusWithMessage(basic.CodeServiceErr, "failed to get product size list")
|
||||||
|
|
34
server_api/canteen.api
Normal file
34
server_api/canteen.api
Normal file
|
@ -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"`
|
||||||
|
}
|
Loading…
Reference in New Issue
Block a user