workDesktop.go 8.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224
  1. package service
  2. import (
  3. MC "app.yhyue.com/moapp/jybase/common"
  4. "app.yhyue.com/moapp/jybase/redis"
  5. "encoding/json"
  6. "errors"
  7. "fmt"
  8. "github.com/zeromicro/go-zero/core/logx"
  9. "math/rand"
  10. "strings"
  11. "time"
  12. "userCenter/entity"
  13. "userCenter/rpc/pb"
  14. . "userCenter/rpc/usercenter"
  15. )
  16. //工作桌面--菜单当前选择模式--全部:all/可用:usable
  17. //工作桌面--常用功能更新
  18. //工作桌面--常用功能列表
  19. func RenewWorkDesktopMenuModeOrCommonly(in *WorkDesktopComprehensiveReq) (r *WorkDesktopComprehensiveResp) {
  20. r = &WorkDesktopComprehensiveResp{}
  21. switch in.ActionMode {
  22. case "commonlyRenew": //常用功能更新
  23. //in.MenuIds 为空;删除
  24. if in.MenuIds == "" {
  25. count := entity.BaseMysql.CountBySql(`SELECT COUNT(id) FROM `+entity.WorkCommonly+` WHERE base_userid = ? AND appid = ? AND platform = ? AND field = ?`, in.NewUserId, in.AppId, in.Platform, in.ActionMode)
  26. if count > 0 {
  27. if count := entity.BaseMysql.UpdateOrDeleteBySql(`DELETE FROM `+entity.WorkCommonly+` WHERE base_userid = ? AND appid = ? AND platform = ? AND field = ?`, in.NewUserId, in.AppId, in.Platform, in.ActionMode); count <= 0 {
  28. r.ErrorCode = -1
  29. r.ErrorMsg = "常用功能-清空常用功能异常"
  30. }
  31. }
  32. } else {
  33. //in.MenuIds 不为空:更新
  34. if b, m := entity.CommonlyUpdate(in); !b {
  35. r.ErrorCode = -1
  36. r.ErrorMsg = m
  37. }
  38. }
  39. case "commonlyList":
  40. //查询常用功能列表
  41. in.ActionMode = "commonlyRenew"
  42. existingData := entity.BaseMysql.SelectBySql(`SELECT value FROM `+entity.WorkCommonly+` WHERE base_userid = ? AND appid = ? AND field = ? AND platform = ? ORDER BY id DESC `, in.NewUserId, in.AppId, in.ActionMode, in.Platform)
  43. if existingData != nil && len(*existingData) > 0 {
  44. eData := (*existingData)[0]
  45. if MC.ObjToString(eData["value"]) != "" {
  46. var (
  47. //params []string
  48. pIds []string
  49. )
  50. for _, pv := range strings.Split(MC.ObjToString(eData["value"]), ",") {
  51. //params = append(params, "?")
  52. pIds = append(pIds, pv)
  53. }
  54. //常用功能存储是三级菜单id,需要四级菜单信息也查出来
  55. //会出现 用户设置常用功能,此功能下线,用户设置依然保存的有此功能 查询不能加 AND status = 0
  56. menuData := entity.BaseMysql.SelectBySql(fmt.Sprintf(`SELECT * from %s WHERE ( id IN (%s) OR parentid IN (%s))`, entity.WorkMenu, strings.Join(pIds, ","), strings.Join(pIds, ",")))
  57. if menuData != nil && len(*menuData) > 0 {
  58. var m = &entity.WorkDesktopMenu{
  59. MenuTree: []*entity.JYMenu{},
  60. UserId: in.UserId,
  61. NewUserId: in.NewUserId,
  62. AppId: in.AppId,
  63. TimeOut: int(in.RedisOutTime),
  64. BigMemberOff: in.BigMemberOff,
  65. }
  66. for _, mv := range *menuData {
  67. menu := &entity.JYMenu{
  68. Id: MC.IntAll(mv["id"]),
  69. Name: MC.ObjToString(mv["name"]),
  70. OrderId: MC.IntAll(mv["orderid"]),
  71. ParentId: MC.IntAll(mv["parentid"]),
  72. PowerIds: MC.ObjToString(mv["powerids"]),
  73. CheckCode: MC.IntAll(mv["checkcode"]),
  74. Icon: MC.ObjToString(mv["icon"]),
  75. AppType: MC.ObjToString(mv["apptype"]),
  76. OpenType: MC.ObjToString(mv["opentype"]),
  77. }
  78. switch in.Platform {
  79. case "WX":
  80. menu.Url = MC.ObjToString(mv["wxurl"])
  81. case "APP":
  82. menu.Url = MC.ObjToString(mv["appurl"])
  83. default:
  84. menu.Url = MC.ObjToString(mv["pcurl"])
  85. }
  86. if additionalInfo := MC.ObjToString(mv["additionalinfo"]); additionalInfo != "" {
  87. additional := entity.Additional{}
  88. if json.Unmarshal([]byte(additionalInfo), &additional) == nil {
  89. menu.AdditionalInfo = additional
  90. }
  91. }
  92. if menu.Id > 0 {
  93. m.MenuTree = append(m.MenuTree, menu)
  94. }
  95. }
  96. //常用功能格式化
  97. r.Data, _ = m.CommonlyFormat()
  98. }
  99. }
  100. }
  101. case "menuMode": //菜单模式更新
  102. if in.MenuMode == "" || (in.MenuMode != "usable" && in.MenuMode != "all") {
  103. r.ErrorCode = -1
  104. r.ErrorMsg = "菜单模式-参数异常"
  105. } else {
  106. menuModes := entity.BaseMysql.SelectBySql(`SELECT id,value FROM `+entity.WorkCommonly+` WHERE appid=? AND base_userid =? AND field = ? AND platform = ? LIMIT 1`, in.AppId, in.NewUserId, in.ActionMode, in.Platform)
  107. if menuModes != nil && len(*menuModes) > 0 {
  108. menuMode := (*menuModes)[0]
  109. if MC.ObjToString(menuMode["value"]) != in.MenuMode {
  110. if entity.BaseMysql.UpdateOrDeleteBySql(`UPDATE `+entity.WorkCommonly+` SET value = ? WHERE id = ?`, in.MenuMode, MC.IntAll(menuMode["id"])) < 0 {
  111. r.ErrorCode = -1
  112. r.ErrorMsg = "菜单模式-更新异常"
  113. }
  114. }
  115. } else {
  116. if entity.BaseMysql.Insert(entity.WorkCommonly, map[string]interface{}{
  117. "appid": in.AppId,
  118. "base_userid": in.NewUserId,
  119. "field": in.ActionMode,
  120. "platform": in.Platform,
  121. "value": in.MenuMode,
  122. }) < 0 {
  123. r.ErrorCode = -1
  124. r.ErrorMsg = "菜单模式-插入异常"
  125. }
  126. }
  127. }
  128. }
  129. return
  130. }
  131. //
  132. func GetWorkDesktopMenuMode(in *WorkDesktopMenuInfoReq) (str string, err error) {
  133. if in.UserId == "" {
  134. return "", fmt.Errorf("参数异常")
  135. }
  136. str = "all"
  137. menuModes := entity.BaseMysql.SelectBySql(`SELECT value FROM `+entity.WorkCommonly+` WHERE appid=? AND base_userid=? AND field = 'menuMode' AND platform = ? LIMIT 1`, in.AppId, in.NewUserId, in.Platform)
  138. if menuModes != nil && len(*menuModes) > 0 {
  139. menuMode := (*menuModes)[0]
  140. if MC.ObjToString(menuMode["value"]) != "" {
  141. str = MC.ObjToString(menuMode["value"])
  142. }
  143. }
  144. return
  145. }
  146. //获取工作桌面菜单树
  147. func GetWordDesktopMenuTree(in *WorkDesktopMenuInfoReq) ([]*pb.MenuList, error) {
  148. t1 := time.Now()
  149. //redis缓存
  150. var menuList []*pb.MenuList
  151. RedisMenuKey := fmt.Sprintf(entity.RedisMenuKey, in.AppId, in.Platform, in.UserId)
  152. menuBytes, err := redis.GetBytes(entity.RedisCode, RedisMenuKey)
  153. if err == nil && len(*menuBytes) > 0 {
  154. if json.Unmarshal(*menuBytes, &menuList) == nil {
  155. return menuList, nil
  156. }
  157. }
  158. //tidb数据
  159. menuData := entity.BaseMysql.SelectBySql(`SELECT * FROM `+entity.WorkMenu+` WHERE status=0 AND appid=? ORDER BY id ASC`, in.AppId)
  160. if menuData == nil || len(*menuData) == 0 {
  161. return nil, errors.New("查询数据异常")
  162. }
  163. wdm := &entity.WorkDesktopMenu{
  164. MenuTree: []*entity.JYMenu{},
  165. UserId: in.UserId,
  166. NewUserId: in.NewUserId,
  167. AppId: in.AppId,
  168. TimeOut: int(in.RedisOutTime),
  169. BigMemberOff: in.BigMemberOff,
  170. }
  171. for _, v := range *menuData {
  172. menu := &entity.JYMenu{
  173. Id: MC.IntAll(v["id"]),
  174. Name: MC.ObjToString(v["name"]),
  175. OrderId: MC.IntAll(v["orderid"]),
  176. ParentId: MC.IntAll(v["parentid"]),
  177. PowerIds: MC.ObjToString(v["powerids"]),
  178. CheckCode: MC.IntAll(v["checkcode"]),
  179. Icon: MC.ObjToString(v["icon"]),
  180. AppType: MC.ObjToString(v["apptype"]),
  181. OpenType: MC.ObjToString(v["opentype"]),
  182. }
  183. switch in.Platform {
  184. case "WX":
  185. menu.Url = MC.ObjToString(v["wxurl"])
  186. case "APP":
  187. menu.Url = MC.ObjToString(v["appurl"])
  188. default:
  189. menu.Url = MC.ObjToString(v["pcurl"])
  190. }
  191. if additionalInfo := MC.ObjToString(v["additionalinfo"]); additionalInfo != "" {
  192. additional := entity.Additional{}
  193. if json.Unmarshal([]byte(additionalInfo), &additional) == nil {
  194. menu.AdditionalInfo = additional
  195. }
  196. }
  197. if menu.Id > 0 {
  198. wdm.MenuTree = append(wdm.MenuTree, menu)
  199. }
  200. }
  201. logx.Info("数据长度:", len(wdm.MenuTree), "-- 查询菜单数据耗时:", time.Since(t1))
  202. //jyMenu := wdm.WorkMenuTree(0)
  203. wdm.MenuTree = wdm.WorkMenuTree(0)
  204. logx.Info("菜单树生成耗时:", time.Since(t1))
  205. menuList, err = wdm.WorkMenuFormat()
  206. logx.Info("菜单格式化耗时:", time.Since(t1))
  207. if err == nil && len(menuList) > 0 {
  208. if menuBytes, err := json.Marshal(menuList); err == nil {
  209. redisOutTime := int(in.RedisOutTime) + rand.Intn(60*60)
  210. if redis.PutBytes(entity.RedisCode, RedisMenuKey, &menuBytes, redisOutTime) != nil {
  211. logx.Info("工作桌面菜单 redis缓存异常")
  212. }
  213. } else {
  214. logx.Info("菜单数据序列化异常")
  215. }
  216. }
  217. logx.Info("整体耗时:", time.Since(t1))
  218. return menuList, err
  219. }