system.go 4.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177
  1. package services
  2. import (
  3. "context"
  4. "encoding/json"
  5. "eta/eta_forum_task/models"
  6. "eta/eta_forum_task/models/system"
  7. "eta/eta_forum_task/services/alarm_msg"
  8. "eta/eta_forum_task/services/eta_forum"
  9. "eta/eta_forum_task/utils"
  10. "fmt"
  11. "time"
  12. )
  13. type AdminBatchSaveReq struct {
  14. List []*models.Admin
  15. }
  16. func AdminBatchSaveTask(cont context.Context) (err error) {
  17. deleteCache := true
  18. cacheKey := "eta_forum_task:EtaForumAdminUpdate"
  19. defer func() {
  20. if deleteCache {
  21. _ = utils.Rc.Delete(cacheKey)
  22. }
  23. if err != nil {
  24. go alarm_msg.SendAlarmMsg(utils.APP_NAME_CN+"同步管理员数据失败"+time.Now().Format("2006-01-02 15:04:05")+"<br/>"+err.Error(), 3)
  25. }
  26. }()
  27. if !utils.Rc.SetNX(cacheKey, 1, 30*time.Second) {
  28. deleteCache = false
  29. err = fmt.Errorf("系统处理中,请稍后重试!")
  30. return
  31. }
  32. // 查询需要更新的图表信息总数
  33. adminList, err := models.GetAllAdminList()
  34. if err != nil {
  35. return
  36. }
  37. if len(adminList) == 0 {
  38. return
  39. }
  40. // 更新管理员数据
  41. //查询用户信息
  42. var req AdminBatchSaveReq
  43. req.List = adminList
  44. reqJson, err := json.Marshal(req)
  45. if err != nil {
  46. err = fmt.Errorf("参数解析异常,Err:" + err.Error())
  47. return
  48. }
  49. respItem, err := eta_forum.AdminBatchSaveLib(string(reqJson))
  50. if err != nil {
  51. err = fmt.Errorf("上传管理员信息失败,Err:" + err.Error())
  52. return
  53. }
  54. if respItem.Ret != 200 {
  55. err = fmt.Errorf("上传管理员信息失败,Err:%v,errMsg:%v", respItem.Msg, respItem.ErrMsg)
  56. return
  57. }
  58. fmt.Println("同步管理员数据完成, 同步管理员数据完成:", len(adminList), "条")
  59. go GroupSyncTask()
  60. go RoleSyncTask()
  61. go DepartmentSyncTask()
  62. return
  63. }
  64. type SysGroupSyncReq struct {
  65. List []*system.SysGroup
  66. }
  67. // 同步分组信息
  68. func GroupSyncTask() (err error) {
  69. defer func() {
  70. if err != nil {
  71. go alarm_msg.SendAlarmMsg(utils.APP_NAME_CN+"同步部门分组数据失败"+time.Now().Format("2006-01-02 15:04:05")+"<br/>"+err.Error(), 3)
  72. }
  73. }()
  74. //查询用户信息
  75. list, err := system.GetAllSysGroup()
  76. if err != nil {
  77. err = fmt.Errorf("获取分组信息失败,Err:" + err.Error())
  78. return
  79. }
  80. var req SysGroupSyncReq
  81. req.List = list
  82. reqJson, err := json.Marshal(req)
  83. if err != nil {
  84. err = fmt.Errorf("参数解析异常,Err:" + err.Error())
  85. return
  86. }
  87. respItem, err := eta_forum.PostLib("/v1/system/group/sync", string(reqJson))
  88. if err != nil {
  89. err = fmt.Errorf("上传角色信息失败,Err:" + err.Error())
  90. return
  91. }
  92. if respItem.Ret != 200 {
  93. err = fmt.Errorf("上传角色信息失败,Err:%v,errMsg:%v", respItem.Msg, respItem.ErrMsg)
  94. return
  95. }
  96. fmt.Println("同步部门分组数据成功:", len(list), "条")
  97. return
  98. }
  99. type DepartmentSyncReq struct {
  100. List []*system.SysDepartment
  101. }
  102. // 同步部门信息
  103. func DepartmentSyncTask() (err error) {
  104. defer func() {
  105. if err != nil {
  106. go alarm_msg.SendAlarmMsg(utils.APP_NAME_CN+"同步部门数据失败"+time.Now().Format("2006-01-02 15:04:05")+"<br/>"+err.Error(), 3)
  107. }
  108. }()
  109. //查询用户信息
  110. list, err := system.GetAllSysDepartment()
  111. if err != nil {
  112. err = fmt.Errorf("获取部门信息失败,Err:" + err.Error())
  113. return
  114. }
  115. var req DepartmentSyncReq
  116. req.List = list
  117. reqJson, err := json.Marshal(req)
  118. if err != nil {
  119. err = fmt.Errorf("参数解析异常,Err:" + err.Error())
  120. return
  121. }
  122. respItem, err := eta_forum.PostLib("/v1/system/department/sync", string(reqJson))
  123. if err != nil {
  124. err = fmt.Errorf("同步部门信息失败,Err:" + err.Error())
  125. return
  126. }
  127. if respItem.Ret != 200 {
  128. err = fmt.Errorf("同步部门信息失败,Err:%v,errMsg:%v", respItem.Msg, respItem.ErrMsg)
  129. return
  130. }
  131. fmt.Println("同步部门数据成功:", len(list), "条")
  132. return
  133. }
  134. type SysRoleSyncReq struct {
  135. List []*system.SysRole
  136. }
  137. // 同步角色信息
  138. func RoleSyncTask() (err error) {
  139. defer func() {
  140. if err != nil {
  141. go alarm_msg.SendAlarmMsg(utils.APP_NAME_CN+"同步角色信息失败"+time.Now().Format("2006-01-02 15:04:05")+"<br/>"+err.Error(), 3)
  142. }
  143. }()
  144. //查询用户信息
  145. list, err := system.GetAllSysRole()
  146. if err != nil {
  147. err = fmt.Errorf("获取角色信息失败,Err:" + err.Error())
  148. return
  149. }
  150. var req SysRoleSyncReq
  151. req.List = list
  152. reqJson, err := json.Marshal(req)
  153. if err != nil {
  154. err = fmt.Errorf("参数解析异常,Err:" + err.Error())
  155. return
  156. }
  157. respItem, err := eta_forum.PostLib("/v1/system/role/sync", string(reqJson))
  158. if err != nil {
  159. err = fmt.Errorf("同步角色信息失败,Err:" + err.Error())
  160. return
  161. }
  162. if respItem.Ret != 200 {
  163. err = fmt.Errorf("同步角色信息失败,Err:%v,errMsg:%v", respItem.Msg, respItem.ErrMsg)
  164. return
  165. }
  166. fmt.Println("同步角色数据成功:", len(list), "条")
  167. return
  168. }