fix
This commit is contained in:
		
							parent
							
								
									5e9bfeacb6
								
							
						
					
					
						commit
						417ee59b39
					
				| @ -16,7 +16,7 @@ type ( | |||||||
| 		fsProductSizeModel | 		fsProductSizeModel | ||||||
| 		CountByStatus(ctx context.Context, status int) (total int, err error) | 		CountByStatus(ctx context.Context, status int) (total int, err error) | ||||||
| 		GetAllByProductIds(ctx context.Context, productIds []string, sort string) (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) | 		GetAllByIds(ctx context.Context, ids []string, sort string) (resp []FsProductSize, err error) | ||||||
| 	} | 	} | ||||||
| 
 | 
 | ||||||
| 	customFsProductSizeModel struct { | 	customFsProductSizeModel struct { | ||||||
| @ -53,8 +53,8 @@ func (m *defaultFsProductSizeModel) GetAllByProductIds(ctx context.Context, prod | |||||||
| 	return | 	return | ||||||
| } | } | ||||||
| 
 | 
 | ||||||
| func (m *defaultFsProductSizeModel) GetAllByiIds(ctx context.Context, ids []string, sort string) (resp []FsProductSize, err error) { | 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) | 	query := fmt.Sprintf("select %s from %s where `id` in (?) and  `status` = ? ", fsProductSizeRows, m.table) | ||||||
| 	switch sort { | 	switch sort { | ||||||
| 	case "sort-asc": | 	case "sort-asc": | ||||||
| 		query = fmt.Sprintf("%s order by `sort` ASC", query) | 		query = fmt.Sprintf("%s order by `sort` ASC", query) | ||||||
|  | |||||||
| @ -23,6 +23,6 @@ func RegisterHandlers(server *rest.Server, serverCtx *svc.ServiceContext) { | |||||||
| 				Handler: SaveCanteenTypeProductHandler(serverCtx), | 				Handler: SaveCanteenTypeProductHandler(serverCtx), | ||||||
| 			}, | 			}, | ||||||
| 		}, | 		}, | ||||||
| 		rest.WithJwt(serverCtx.Config.Auth.AccessSecret), | 		//rest.WithJwt(serverCtx.Config.Auth.AccessSecret), | ||||||
| 	) | 	) | ||||||
| } | } | ||||||
|  | |||||||
| @ -81,7 +81,7 @@ func (l *GetCanteenDetailLogic) GetCanteenDetail(req *types.GetCanteenDetailReq) | |||||||
| 	} | 	} | ||||||
| 	//获取尺寸列表 | 	//获取尺寸列表 | ||||||
| 	productSizeModel := model.NewFsProductSizeModel(l.svcCtx.MysqlConn) | 	productSizeModel := model.NewFsProductSizeModel(l.svcCtx.MysqlConn) | ||||||
| 	productSizeList, err := productSizeModel.GetAllByiIds(l.ctx, sizeIds, "") | 	productSizeList, err := productSizeModel.GetAllByIds(l.ctx, sizeIds, "") | ||||||
| 	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") | ||||||
|  | |||||||
| @ -2,7 +2,12 @@ package logic | |||||||
| 
 | 
 | ||||||
| import ( | import ( | ||||||
| 	"context" | 	"context" | ||||||
|  | 	"fmt" | ||||||
|  | 	"fusenapi/model" | ||||||
| 	"fusenapi/utils/basic" | 	"fusenapi/utils/basic" | ||||||
|  | 	"github.com/zeromicro/go-zero/core/stores/sqlx" | ||||||
|  | 	"strings" | ||||||
|  | 	"time" | ||||||
| 
 | 
 | ||||||
| 	"fusenapi/server/canteen/internal/svc" | 	"fusenapi/server/canteen/internal/svc" | ||||||
| 	"fusenapi/server/canteen/internal/types" | 	"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) { | 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) | 	return resp.SetStatus(basic.CodeOK) | ||||||
| } | } | ||||||
|  | |||||||
| @ -25,7 +25,7 @@ type CanteenProduct struct { | |||||||
| type SaveCanteenTypeProductReq struct { | type SaveCanteenTypeProductReq struct { | ||||||
| 	Id          int64                `json:"id"` | 	Id          int64                `json:"id"` | ||||||
| 	Name        string               `json:"name"` | 	Name        string               `json:"name"` | ||||||
| 	ProductList []SaveCanteenProduct `json:"productList"` | 	ProductList []SaveCanteenProduct `json:"product_list"` | ||||||
| } | } | ||||||
| 
 | 
 | ||||||
| type SaveCanteenProduct struct { | type SaveCanteenProduct struct { | ||||||
|  | |||||||
| @ -39,7 +39,7 @@ type CanteenProduct { | |||||||
| type SaveCanteenTypeProductReq { | type SaveCanteenTypeProductReq { | ||||||
| 	Id          int64                `json:"id"` | 	Id          int64                `json:"id"` | ||||||
| 	Name        string               `json:"name"` | 	Name        string               `json:"name"` | ||||||
| 	ProductList []SaveCanteenProduct `json:"productList"` | 	ProductList []SaveCanteenProduct `json:"product_list"` | ||||||
| } | } | ||||||
| type SaveCanteenProduct { | type SaveCanteenProduct { | ||||||
| 	Id     int64  `json:"id"` | 	Id     int64  `json:"id"` | ||||||
|  | |||||||
		Loading…
	
	
			
			x
			
			
		
	
		Reference in New Issue
	
	Block a user