| 
									
										
										
										
											2023-06-21 16:35:43 +08:00
										 |  |  | package logic | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | import ( | 
					
						
							|  |  |  | 	"encoding/json" | 
					
						
							|  |  |  | 	"fusenapi/model/gmodel" | 
					
						
							| 
									
										
										
										
											2023-06-25 14:11:47 +08:00
										 |  |  | 	"fusenapi/server/product-template/internal/types" | 
					
						
							| 
									
										
										
										
											2023-06-26 18:27:38 +08:00
										 |  |  | 	"fusenapi/utils/auth" | 
					
						
							| 
									
										
										
										
											2023-06-21 16:35:43 +08:00
										 |  |  | 	"fusenapi/utils/basic" | 
					
						
							|  |  |  | 	"io/ioutil" | 
					
						
							|  |  |  | 	"net/http" | 
					
						
							|  |  |  | 	"time" | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | 	"context" | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2023-06-25 14:11:47 +08:00
										 |  |  | 	"fusenapi/server/product-template/internal/svc" | 
					
						
							| 
									
										
										
										
											2023-07-21 15:20:18 +08:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2023-06-21 16:35:43 +08:00
										 |  |  | 	"github.com/zeromicro/go-zero/core/logx" | 
					
						
							|  |  |  | ) | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | type SaveBaseMapLogic struct { | 
					
						
							|  |  |  | 	logx.Logger | 
					
						
							| 
									
										
										
										
											2023-07-21 15:20:18 +08:00
										 |  |  | 	ctx       context.Context | 
					
						
							|  |  |  | 	svcCtx    *svc.ServiceContext | 
					
						
							|  |  |  | 	bodyBytes []byte | 
					
						
							| 
									
										
										
										
											2023-06-21 16:35:43 +08:00
										 |  |  | } | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | func NewSaveBaseMapLogic(ctx context.Context, svcCtx *svc.ServiceContext) *SaveBaseMapLogic { | 
					
						
							|  |  |  | 	return &SaveBaseMapLogic{ | 
					
						
							|  |  |  | 		Logger: logx.WithContext(ctx), | 
					
						
							|  |  |  | 		ctx:    ctx, | 
					
						
							|  |  |  | 		svcCtx: svcCtx, | 
					
						
							|  |  |  | 	} | 
					
						
							|  |  |  | } | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2023-07-21 15:20:18 +08:00
										 |  |  | // 处理进入前逻辑w,r | 
					
						
							|  |  |  | func (l *SaveBaseMapLogic) BeforeLogic(w http.ResponseWriter, r *http.Request) { | 
					
						
							| 
									
										
										
										
											2023-06-21 16:35:43 +08:00
										 |  |  | 	bodyBytes, err := ioutil.ReadAll(r.Body) | 
					
						
							|  |  |  | 	defer r.Body.Close() | 
					
						
							| 
									
										
										
										
											2023-07-21 15:20:18 +08:00
										 |  |  | 	if err != nil { | 
					
						
							|  |  |  | 		logx.Error(err) | 
					
						
							|  |  |  | 	} | 
					
						
							|  |  |  | 	l.bodyBytes = bodyBytes | 
					
						
							|  |  |  | } | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | func (l *SaveBaseMapLogic) SaveBaseMap(req *types.Request, userInfo *auth.BackendUserInfo) (resp *basic.Response) { | 
					
						
							|  |  |  | 	var err error | 
					
						
							| 
									
										
										
										
											2023-06-21 16:35:43 +08:00
										 |  |  | 	var postData []types.SaveBaseMapReq | 
					
						
							| 
									
										
										
										
											2023-07-21 15:20:18 +08:00
										 |  |  | 	if err = json.Unmarshal(l.bodyBytes, &postData); err != nil { | 
					
						
							| 
									
										
										
										
											2023-06-21 16:35:43 +08:00
										 |  |  | 		logx.Error(err) | 
					
						
							|  |  |  | 		return resp.SetStatusWithMessage(basic.CodeRequestParamsErr, "invalid request param") | 
					
						
							|  |  |  | 	} | 
					
						
							| 
									
										
										
										
											2023-07-21 15:20:18 +08:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2023-06-21 16:35:43 +08:00
										 |  |  | 	//空数组 | 
					
						
							|  |  |  | 	if len(postData) == 0 { | 
					
						
							|  |  |  | 		return resp.SetStatusWithMessage(basic.CodeRequestParamsErr, "param can`t be empty array") | 
					
						
							|  |  |  | 	} | 
					
						
							|  |  |  | 	updDataArr := make([]gmodel.FsProductTemplateBasemap, 0, len(postData)) | 
					
						
							|  |  |  | 	for _, v := range postData { | 
					
						
							| 
									
										
										
										
											2023-06-21 18:06:39 +08:00
										 |  |  | 		val := v | 
					
						
							| 
									
										
										
										
											2023-06-21 16:35:43 +08:00
										 |  |  | 		ctimeT, err := time.ParseInLocation("2006-01-02 15:04:05", v.Ctime, time.Local) | 
					
						
							|  |  |  | 		if err != nil { | 
					
						
							|  |  |  | 			logx.Error(err) | 
					
						
							|  |  |  | 			return resp.SetStatusWithMessage(basic.CodeRequestParamsErr, "err create time format") | 
					
						
							|  |  |  | 		} | 
					
						
							|  |  |  | 		ctime := ctimeT.Unix() | 
					
						
							|  |  |  | 		updDataArr = append(updDataArr, gmodel.FsProductTemplateBasemap{ | 
					
						
							| 
									
										
										
										
											2023-06-21 18:06:39 +08:00
										 |  |  | 			Id:    val.Id, | 
					
						
							|  |  |  | 			Name:  &val.Name, | 
					
						
							|  |  |  | 			Url:   &val.Url, | 
					
						
							| 
									
										
										
										
											2023-06-21 16:35:43 +08:00
										 |  |  | 			Ctime: &ctime, | 
					
						
							|  |  |  | 		}) | 
					
						
							|  |  |  | 	} | 
					
						
							|  |  |  | 	//更新 | 
					
						
							| 
									
										
										
										
											2023-06-21 18:06:39 +08:00
										 |  |  | 	err = l.svcCtx.AllModels.FsProductTemplateBasemap.UpdateBaseMapWithTransaction(l.ctx, updDataArr) | 
					
						
							| 
									
										
										
										
											2023-06-21 16:35:43 +08:00
										 |  |  | 	if err != nil { | 
					
						
							|  |  |  | 		logx.Error(err) | 
					
						
							|  |  |  | 		return resp.SetStatusWithMessage(basic.CodeDbSqlErr, "failed to update base map") | 
					
						
							|  |  |  | 	} | 
					
						
							|  |  |  | 	return resp.SetStatusWithMessage(basic.CodeOK, "success") | 
					
						
							|  |  |  | } |