This commit is contained in:
laodaming 2023-06-09 16:45:29 +08:00
parent 5e9bfeacb6
commit 417ee59b39
6 changed files with 82 additions and 8 deletions

View File

@ -16,7 +16,7 @@ type (
fsProductSizeModel
CountByStatus(ctx context.Context, status int) (total int, 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)
GetAllByIds(ctx context.Context, ids []string, sort string) (resp []FsProductSize, err error)
}
customFsProductSizeModel struct {
@ -53,8 +53,8 @@ func (m *defaultFsProductSizeModel) GetAllByProductIds(ctx context.Context, prod
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)
func (m *defaultFsProductSizeModel) GetAllByIds(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)

View File

@ -23,6 +23,6 @@ func RegisterHandlers(server *rest.Server, serverCtx *svc.ServiceContext) {
Handler: SaveCanteenTypeProductHandler(serverCtx),
},
},
rest.WithJwt(serverCtx.Config.Auth.AccessSecret),
//rest.WithJwt(serverCtx.Config.Auth.AccessSecret),
)
}

View File

@ -81,7 +81,7 @@ func (l *GetCanteenDetailLogic) GetCanteenDetail(req *types.GetCanteenDetailReq)
}
//获取尺寸列表
productSizeModel := model.NewFsProductSizeModel(l.svcCtx.MysqlConn)
productSizeList, err := productSizeModel.GetAllByiIds(l.ctx, sizeIds, "")
productSizeList, err := productSizeModel.GetAllByIds(l.ctx, sizeIds, "")
if err != nil {
logx.Error(err)
return resp.SetStatusWithMessage(basic.CodeServiceErr, "failed to get product size list")

View File

@ -2,7 +2,12 @@ package logic
import (
"context"
"fmt"
"fusenapi/model"
"fusenapi/utils/basic"
"github.com/zeromicro/go-zero/core/stores/sqlx"
"strings"
"time"
"fusenapi/server/canteen/internal/svc"
"fusenapi/server/canteen/internal/types"
@ -26,6 +31,75 @@ func NewSaveCanteenTypeProductLogic(ctx context.Context, svcCtx *svc.ServiceCont
// 保存餐厅类型的关联产品
func (l *SaveCanteenTypeProductLogic) SaveCanteenTypeProduct(req *types.SaveCanteenTypeProductReq) (resp *types.Response) {
if len(req.ProductList) == 0 {
return resp.SetStatusWithMessage(basic.CodeServiceErr, "product list can`t be empty")
}
canteenProductModel := model.NewFsCanteenProductModel(l.svcCtx.MysqlConn)
//获取原有餐厅类型的所有产品
oldCanteenProductList, err := canteenProductModel.GetAllByCanteenTypeId(l.ctx, req.Id)
if err != nil {
logx.Error(err)
return resp.SetStatusWithMessage(basic.CodeServiceErr, "failed to get canteen product list")
}
sizeIds := make([]string, 0, len(req.ProductList))
for _, v := range req.ProductList {
sizeIds = append(sizeIds, fmt.Sprintf("%d", v.SizeId))
}
productSizeModel := model.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")
}
fmt.Println(len(productSizeList))
mapProductSize := make(map[int64]model.FsProductSize)
for _, v := range productSizeList {
mapProductSize[v.Id] = v
}
//开启事务
err = l.svcCtx.MysqlConn.TransactCtx(l.ctx, func(ctx context.Context, session sqlx.Session) error {
sort := int64(0)
//新的变更记录
mapUpdateCanteenPid := make(map[int64]struct{})
for _, v := range req.ProductList {
sort++
sizeInfo, ok := mapProductSize[v.SizeId]
if !ok {
continue
}
if v.Id > 0 { //更新
mapUpdateCanteenPid[v.Id] = struct{}{}
if _, err = session.ExecCtx(l.ctx, "update `fs_canteen_product` set "+
"`size_id` = ?,`sid` = ?,`sort` = ?,`product_id` = ? where `id` = ? ",
v.SizeId, v.SId, sort, sizeInfo.ProductId, v.Id,
); err != nil {
return err
}
continue
}
//新增
if _, err = session.ExecCtx(l.ctx, "insert into `fs_canteen_product` "+
"(`size_id`,`sid`,`sort`,`status`,`ctime`,`canteen_type`,`product_id`) "+
"values (?, ?, ?, ?, ?, ?, ?)", sizeInfo.Id, v.SId, sort, 1, time.Now().Unix(), req.Id, sizeInfo.ProductId); err != nil {
return err
}
}
diffCanteenProductId := make([]string, 0, len(oldCanteenProductList))
//旧的中不包含在更新的里面则去掉
for _, v := range oldCanteenProductList {
if _, ok := mapUpdateCanteenPid[v.Id]; !ok {
diffCanteenProductId = append(diffCanteenProductId, fmt.Sprintf("%d", v.Id))
}
}
if len(diffCanteenProductId) == 0 {
return nil
}
_, err = session.ExecCtx(l.ctx, "update `fs_canteen_product` set `status` = ? where `id` in (?)", 0, strings.Join(diffCanteenProductId, ","))
return err
})
if err != nil {
logx.Error(err)
return resp.SetStatusWithMessage(basic.CodeServiceErr, "failed to save canteen product")
}
return resp.SetStatus(basic.CodeOK)
}

View File

@ -25,7 +25,7 @@ type CanteenProduct struct {
type SaveCanteenTypeProductReq struct {
Id int64 `json:"id"`
Name string `json:"name"`
ProductList []SaveCanteenProduct `json:"productList"`
ProductList []SaveCanteenProduct `json:"product_list"`
}
type SaveCanteenProduct struct {

View File

@ -39,7 +39,7 @@ type CanteenProduct {
type SaveCanteenTypeProductReq {
Id int64 `json:"id"`
Name string `json:"name"`
ProductList []SaveCanteenProduct `json:"productList"`
ProductList []SaveCanteenProduct `json:"product_list"`
}
type SaveCanteenProduct {
Id int64 `json:"id"`