eta_permission_task.go 2.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687
  1. package permission
  2. import (
  3. "encoding/json"
  4. logger "eta/eta_mini_ht_api/common/component/log"
  5. "eta/eta_mini_ht_api/common/contants"
  6. configService "eta/eta_mini_ht_api/domian/config"
  7. "eta/eta_mini_ht_api/models/eta"
  8. "eta/eta_mini_ht_api/task/base"
  9. )
  10. var (
  11. taskName base.TaskType = "ETAPermissionSyncTask"
  12. cron = "0/10 * * * * *"
  13. )
  14. // Execute Task ETA取研报的数据
  15. func (pr *PermissionTask) Execute(taskDetail *base.TaskDetail) error {
  16. logger.Info(contants.TaskFormat, "同步ETA品种开始")
  17. classifyList, err := eta.GetSyncClassifyList()
  18. if err != nil {
  19. logger.Error("获取ETA分类列表失败:%v", err)
  20. return err
  21. }
  22. if len(classifyList) > 0 {
  23. var list []byte
  24. list, err = json.Marshal(classifyList)
  25. if err == nil {
  26. taskDetail.Content = string(list)
  27. }
  28. var permissionClassifyMappingDTOList []configService.PermissionClassifyMappingDTO
  29. for _, classify := range classifyList {
  30. permissionClassifyMappingDTOList = append(permissionClassifyMappingDTOList, convertMapping(classify))
  31. }
  32. err = configService.SyncPermissionClassifyMapping(permissionClassifyMappingDTOList)
  33. if err != nil {
  34. logger.Error("同步ETA分类列表失败:%v", err)
  35. return err
  36. }
  37. }
  38. permissionList, err := eta.GetSyncPermissionList()
  39. if err != nil {
  40. logger.Error("获取ETA品种列表失败:%v", err)
  41. return err
  42. }
  43. if len(permissionList) > 0 {
  44. var list []byte
  45. list, err = json.Marshal(permissionList)
  46. if err == nil {
  47. taskDetail.Content = string(list)
  48. }
  49. var permissionDTOList []configService.PermissionDTO
  50. for _, permisson := range permissionList {
  51. permissionDTOList = append(permissionDTOList, convert(permisson))
  52. }
  53. err = configService.SyncPermission(permissionDTOList)
  54. if err != nil {
  55. logger.Error("同步ETA品种列表失败:%v", err)
  56. return err
  57. }
  58. }
  59. logger.Info(contants.TaskFormat, "同步ETA品种结束")
  60. return nil
  61. }
  62. type PermissionTask struct {
  63. }
  64. func convert(permission eta.ChartPermission) configService.PermissionDTO {
  65. return configService.PermissionDTO{
  66. PermissionId: permission.ChartPermissionID,
  67. PermissionName: permission.PermissionName,
  68. ParentId: permission.ParentID,
  69. Sort: permission.Sort,
  70. }
  71. }
  72. func convertMapping(permission eta.ChartPermissionSearchKeyWordMapping) configService.PermissionClassifyMappingDTO {
  73. return configService.PermissionClassifyMappingDTO{
  74. PermissionId: permission.ChartPermissionID,
  75. ClassifyId: permission.ClassifyID,
  76. }
  77. }
  78. func init() {
  79. permissionTask := base.NewTask(taskName, cron, new(PermissionTask), base.PROD)
  80. base.RegisterTask(&permissionTask)
  81. }