system.go 1.5 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162
  1. package services
  2. import (
  3. "context"
  4. "encoding/json"
  5. "eta/eta_forum_task/models"
  6. "eta/eta_forum_task/services/alarm_msg"
  7. "eta/eta_forum_task/services/eta_forum"
  8. "eta/eta_forum_task/utils"
  9. "fmt"
  10. "time"
  11. )
  12. type AdminBatchSaveReq struct {
  13. List []*models.Admin
  14. }
  15. func AdminBatchSaveTask(cont context.Context) (err error) {
  16. deleteCache := true
  17. cacheKey := "eta_forum_task:EtaForumAdminUpdate"
  18. defer func() {
  19. if deleteCache {
  20. _ = utils.Rc.Delete(cacheKey)
  21. }
  22. if err != nil {
  23. go alarm_msg.SendAlarmMsg(utils.APP_NAME_CN+"同步管理员数据失败"+time.Now().Format("2006-01-02 15:04:05")+"<br/>"+err.Error(), 3)
  24. }
  25. }()
  26. if !utils.Rc.SetNX(cacheKey, 1, 30*time.Second) {
  27. deleteCache = false
  28. err = fmt.Errorf("系统处理中,请稍后重试!")
  29. return
  30. }
  31. // 查询需要更新的图表信息总数
  32. adminList, err := models.GetAllAdminList()
  33. if err != nil {
  34. return
  35. }
  36. if len(adminList) == 0 {
  37. return
  38. }
  39. // 更新管理员数据
  40. //查询用户信息
  41. var req AdminBatchSaveReq
  42. req.List = adminList
  43. reqJson, err := json.Marshal(req)
  44. if err != nil {
  45. err = fmt.Errorf("参数解析异常,Err:" + err.Error())
  46. return
  47. }
  48. respItem, err := eta_forum.AdminBatchSaveLib(string(reqJson))
  49. if err != nil {
  50. err = fmt.Errorf("上传管理员信息失败,Err:" + err.Error())
  51. return
  52. }
  53. if respItem.Ret != 200 {
  54. err = fmt.Errorf("上传管理员信息失败,Err:%v,errMsg:%v", respItem.Msg, respItem.ErrMsg)
  55. return
  56. }
  57. fmt.Println("同步管理员数据完成, 同步管理员数据完成:", len(adminList), "条")
  58. return
  59. }