|
|
|
|
@@ -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)
|
|
|
|
|
}
|
|
|
|
|
|