task.go 6.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219
  1. package services
  2. import (
  3. "eta/eta_mini_crm/models"
  4. "eta/eta_mini_crm/services/elastic"
  5. "eta/eta_mini_crm/utils"
  6. "fmt"
  7. "time"
  8. )
  9. func InitTask() {
  10. fmt.Println("start task")
  11. // 创建Es索引
  12. CreateIndex()
  13. // 初始化研报推送状态
  14. InitEtaReportPushStatus()
  15. // 初始化pdf报告推送状态
  16. InitPdfReportPushStatus()
  17. // 初始化系统用户权限
  18. InitSystemUserPermission()
  19. fmt.Println("end task!")
  20. }
  21. func CreateIndex() {
  22. var mappingJson = `{
  23. "mappings": {
  24. "properties": {
  25. "ReportPdfId": {"type": "integer"},
  26. "PdfUrl": {"type": "text"},
  27. "PdfName": {"type": "text"},
  28. "Title": {"type": "text"},
  29. "Author": {"type": "text"},
  30. "Abstract": {"type": "text"},
  31. "ClassifyIdFirst": {"type": "integer"},
  32. "ClassifyNameFirst": {"type": "text"},
  33. "ClassifyIdSecond": {"type": "integer"},
  34. "ClassifyNameSecond": {"type": "text"},
  35. "Stage": {"type": "integer"},
  36. "PublishTime": {
  37. "type": "text",
  38. "fields": {
  39. "keyword": {
  40. "type": "keyword",
  41. "ignore_above": 256
  42. }
  43. }
  44. },
  45. "ModifyTime": {"type": "date"},
  46. "Pv": {"type": "integer"},
  47. "Uv": {"type": "integer"},
  48. "SysUserId": {"type": "integer"},
  49. "SysRealName": {"type": "text"},
  50. "State": {"type": "integer"}
  51. }
  52. }
  53. }`
  54. err := elastic.EsCreateIndex(utils.MINI_REPORT_INDEX_NAME, mappingJson)
  55. if err != nil {
  56. fmt.Println(err)
  57. }
  58. }
  59. func InitEtaReportPushStatus() {
  60. for {
  61. maxId, err := models.GetMaxSyncIdReportPush(1)
  62. fmt.Println("同步研报开始, maxId:", maxId)
  63. if err != nil {
  64. fmt.Println("同步研报失败, Err:", err)
  65. }
  66. reportList, err := models.GetBatchReport(maxId, 100)
  67. if err != nil {
  68. fmt.Println("同步研报失败, Err:", err)
  69. }
  70. var reportIds []int
  71. for _, v := range reportList {
  72. reportIds = append(reportIds, v.Id)
  73. }
  74. reportPushList, err := models.GetReportPushStatusByReportIds(utils.ReportTypeEta, reportIds)
  75. if err != nil {
  76. return
  77. }
  78. reportPushMap := make(map[int]struct{})
  79. for _, v := range reportPushList {
  80. reportPushMap[v.ReportId] = struct{}{}
  81. }
  82. var insertReportPushList []*models.ReportPushStatus
  83. for _, v := range reportList {
  84. if _, ok := reportPushMap[v.Id]; !ok {
  85. insertReportPushList = append(insertReportPushList, &models.ReportPushStatus{
  86. ReportId: v.Id,
  87. State: 0,
  88. Title: v.Title,
  89. Abstract: v.Abstract,
  90. Stage: v.Stage,
  91. ClassifyIdFirst: v.ClassifyIdFirst,
  92. ClassifyNameFirst: v.ClassifyNameFirst,
  93. ClassifyIdSecond: v.ClassifyIdSecond,
  94. ClassifyNameSecond: v.ClassifyNameSecond,
  95. ClassifyIdThird: v.ClassifyIdThird,
  96. ClassifyNameThird: v.ClassifyNameThird,
  97. Author: v.Author,
  98. ReportType: utils.ReportTypeEta,
  99. PublishTime: v.PublishTime,
  100. CreateTime: time.Now(),
  101. ModifyTime: time.Now(),
  102. })
  103. }
  104. }
  105. err = models.BatchAddReportPushStatus(insertReportPushList)
  106. if err != nil {
  107. return
  108. }
  109. if len(reportList) != 100 {
  110. fmt.Println("同步研报完成")
  111. return
  112. }
  113. }
  114. }
  115. // 初始化pdf报告的推送状态
  116. func InitPdfReportPushStatus() {
  117. for {
  118. maxId, err := models.GetMaxSyncIdReportPush(2)
  119. fmt.Println("同步pdf研报开始, maxId:", maxId)
  120. if err != nil {
  121. fmt.Println("同步研pdf报失败, Err:", err)
  122. }
  123. reportList, err := models.GetBatchPdfReport(maxId, 100)
  124. if err != nil {
  125. fmt.Println("同步pdf研报失败, Err:", err)
  126. }
  127. var reportIds []int
  128. for _, v := range reportList {
  129. reportIds = append(reportIds, v.ReportPdfId)
  130. }
  131. reportPushList, err := models.GetReportPushStatusByReportIds(utils.ReportTypePdf, reportIds)
  132. if err != nil {
  133. return
  134. }
  135. reportPushMap := make(map[int]struct{})
  136. for _, v := range reportPushList {
  137. reportPushMap[v.ReportId] = struct{}{}
  138. }
  139. var insertReportPushList []*models.ReportPushStatus
  140. for _, v := range reportList {
  141. if _, ok := reportPushMap[v.ReportPdfId]; !ok {
  142. insertReportPushList = append(insertReportPushList, &models.ReportPushStatus{
  143. ReportId: v.ReportPdfId,
  144. State: 0,
  145. Title: v.Title,
  146. Abstract: v.Abstract,
  147. Stage: v.Stage,
  148. ClassifyIdFirst: v.ClassifyIdFirst,
  149. ClassifyNameFirst: v.ClassifyNameFirst,
  150. ClassifyIdSecond: v.ClassifyIdSecond,
  151. ClassifyNameSecond: v.ClassifyNameSecond,
  152. ClassifyIdThird: v.ClassifyIdThird,
  153. ClassifyNameThird: v.ClassifyNameThird,
  154. Author: v.Author,
  155. ReportType: utils.ReportTypePdf,
  156. PublishTime: v.PublishTime,
  157. CreateTime: time.Now(),
  158. ModifyTime: time.Now(),
  159. })
  160. }
  161. }
  162. err = models.BatchAddReportPushStatus(insertReportPushList)
  163. if err != nil {
  164. return
  165. }
  166. if len(reportList) != 100 {
  167. fmt.Println("同步pdf研报完成")
  168. return
  169. }
  170. }
  171. }
  172. func InitSystemUserPermission() {
  173. menuList, err := models.GetSysMenuItemsByCondition("", []interface{}{}, []string{"sys_menu_id"}, "")
  174. if err != nil {
  175. fmt.Println("获取系统菜单失败, Err:", err)
  176. utils.FileLog.Info("获取系统菜单失败, Err:", err)
  177. return
  178. }
  179. roleMenuList, err := models.GetSysRoleMenuByRoleId(1)
  180. if err != nil {
  181. if err.Error() == utils.ErrNoRow() {
  182. fmt.Println("系统角色不存在, 请先创建系统角色")
  183. utils.FileLog.Info("系统角色不存在, 请先创建系统角色")
  184. }
  185. fmt.Println("获取系统角色菜单失败, Err:", err)
  186. utils.FileLog.Info("获取系统角色菜单失败, Err:", err)
  187. return
  188. }
  189. menuMap := make(map[int]struct{})
  190. for _, v := range menuList {
  191. menuMap[v.SysMenuId] = struct{}{}
  192. }
  193. for _, v := range roleMenuList {
  194. delete(menuMap, v.SysMenuId)
  195. }
  196. insertMenu := make([]*models.SysRoleMenuMapping, 0)
  197. for k, _ := range menuMap {
  198. t := new(models.SysRoleMenuMapping)
  199. t.SysMenuId = k
  200. t.SysRoleId = 1
  201. t.Type = 0
  202. insertMenu = append(insertMenu, t)
  203. }
  204. if len(insertMenu) > 0 {
  205. err = models.BatchAddSysRoleMenu(insertMenu)
  206. if err != nil {
  207. fmt.Println("初始化系统用户权限失败, Err:", err)
  208. utils.FileLog.Info("初始化系统用户权限失败, Err:", err)
  209. return
  210. }
  211. }
  212. fmt.Println("初始化系统用户权限完成")
  213. }