package service import ( "context" "finclip-app-manager/domain/entity" "finclip-app-manager/domain/entity/proto/apiproto" "finclip-app-manager/infrastructure/cache/redis" impl "finclip-app-manager/infrastructure/db/repo" "finclip-app-manager/infrastructure/utility" "github.com/gin-gonic/gin" "gopkg.in/mgo.v2/bson" "net/http" "time" ) type MenuInfoService struct { } func NewMenuInfoService() *MenuInfoService { var s MenuInfoService return &s } func (m *MenuInfoService) AddMenu(ctx context.Context, c *gin.Context, req *apiproto.AddMenuReq) { errRsp := make(map[string]interface{}) userId := c.GetHeader(entity.ACCOUNT_ID_HEADER_KEY) adminInfo, err := hCaller.GetAdminAccountInfo(ctx, userId) if err != nil { log.Errorf("get admin info err:%s", err.Error()) utility.MakeLocRsp(c, http.StatusInternalServerError, utility.FS_SYSTEM_CALL, gin.H{}) return } now := time.Now().UnixNano() / 1e6 menuInfo := entity.MenuInfo{ TraceId: bson.NewObjectId().Hex(), Name: req.Name, InfoId: req.ID, ImageUrl: req.Image, CreateTime: now, UpdateTime: now, UpdateOperator: adminInfo.Account, } repo := impl.InitMenuInfoRepo() err = repo.Add(ctx, &menuInfo) if err != nil { log.Errorf("add menu err:%s", err.Error()) if err == entity.MenuIdOrNameExiErr { utility.MakeLocRsp(c, http.StatusBadRequest, utility.FS_MENU_ID_EXITS_ERR, gin.H{}) return } utility.MakeLocRsp(c, http.StatusInternalServerError, utility.FS_SERVER_ERR, gin.H{}) return } //m.menuCache.UpdateMemMenuCreatedInfo(true) //m.menuCache.SetRedisMenuCreatedInfo(ctx) //m.menuCache.Update(ctx) redis.RedisSet(ctx, utility.MenuCreatedKey, utility.MenuCreatedValue, 0) errRsp["traceId"] = menuInfo.TraceId utility.MakeLocRsp(c, http.StatusOK, utility.OK, errRsp) return } func (m *MenuInfoService) DelMenu(ctx context.Context, c *gin.Context, traceId string) { userId := c.GetHeader(entity.ACCOUNT_ID_HEADER_KEY) _, err := hCaller.GetAdminAccountInfo(ctx, userId) if err != nil { log.Errorf("get admin info err:%s", err.Error()) utility.MakeLocRsp(c, http.StatusInternalServerError, utility.FS_SYSTEM_CALL, gin.H{}) return } repo := impl.InitMenuInfoRepo() err = repo.DelByTraceId(ctx, traceId) if err != nil { log.Errorf("del menu err:%s", err.Error()) utility.MakeLocRsp(c, http.StatusInternalServerError, utility.FS_SERVER_ERR, gin.H{}) return } //m.menuCache.Update(ctx) utility.MakeLocRsp(c, http.StatusOK, utility.OK, gin.H{}) return } func (m *MenuInfoService) UpdateMenu(ctx context.Context, c *gin.Context, traceId string, req *apiproto.UpdateMenuReq) { userId := c.GetHeader(entity.ACCOUNT_ID_HEADER_KEY) adminInfo, err := hCaller.GetAdminAccountInfo(ctx, userId) if err != nil { log.Errorf("get admin info err:%s", err.Error()) utility.MakeLocRsp(c, http.StatusInternalServerError, utility.FS_SYSTEM_CALL, gin.H{}) return } repo := impl.InitMenuInfoRepo() menuInfo, err := repo.GetInfoByTraceId(ctx, traceId) if err != nil { log.Errorf("get menu info by trace id err:%s", err.Error()) utility.MakeLocRsp(c, http.StatusInternalServerError, utility.FS_SERVER_ERR, gin.H{}) return } menuInfo.Name = req.Name menuInfo.ImageUrl = req.Image menuInfo.InfoId = req.ID menuInfo.UpdateOperator = adminInfo.Account err = repo.UpdateInfo(ctx, traceId, menuInfo) if err != nil { if err == entity.MenuIdOrNameExiErr { utility.MakeLocRsp(c, http.StatusBadRequest, utility.FS_MENU_ID_EXITS_ERR, gin.H{}) return } log.Errorf("update menu err:%s", err.Error()) utility.MakeLocRsp(c, http.StatusInternalServerError, utility.FS_SERVER_ERR, gin.H{}) return } //m.menuCache.Update(ctx) utility.MakeLocRsp(c, http.StatusOK, utility.OK, gin.H{}) return } func (m *MenuInfoService) GetAllMenu(ctx context.Context, c *gin.Context) { result := apiproto.GetAllMenuRsp{} repo := impl.InitMenuInfoRepo() infoList, total, err := repo.GetAllMenuList(ctx, []string{"sort_num"}) if err != nil { log.Errorf("get all menu err:%s", err.Error()) utility.MakeLocRsp(c, http.StatusInternalServerError, utility.FS_SYSTEM_CALL, result) return } result.List = make([]apiproto.GetAllMenuRspItem, 0) result.Total = total for _, v := range infoList { item := apiproto.GetAllMenuRspItem{} item.TraceId = v.TraceId item.Name = v.Name item.Image = v.ImageUrl item.ID = v.InfoId item.SortNum = v.SortNum item.CreateTime = v.CreateTime item.UpdateTime = v.UpdateTime item.UpdateOperator = v.UpdateOperator result.List = append(result.List, item) } utility.MakeLocRsp(c, http.StatusOK, utility.OK, result) return } func (m *MenuInfoService) SortMenu(ctx context.Context, c *gin.Context, req *apiproto.MenuSortReq) { userId := c.GetHeader(entity.ACCOUNT_ID_HEADER_KEY) _, err := hCaller.GetAdminAccountInfo(ctx, userId) if err != nil { log.Errorf("get admin info err:%s", err.Error()) utility.MakeLocRsp(c, http.StatusInternalServerError, utility.FS_SYSTEM_CALL, gin.H{}) return } repo := impl.InitMenuInfoRepo() err = repo.Sort(ctx, req) if err != nil { log.Errorf("sort menu err:%s", err.Error()) if err.Error() == "has deleted" { utility.MakeLocRsp(c, http.StatusBadRequest, utility.FS_DATA_HAS_DEL_ERR, gin.H{}) return } utility.MakeLocRsp(c, http.StatusInternalServerError, utility.FS_SERVER_ERR, gin.H{}) return } //m.menuCache.Update(ctx) utility.MakeLocRsp(c, http.StatusOK, utility.OK, gin.H{}) return }