english_policy_report.go 7.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239
  1. package services
  2. import (
  3. "encoding/json"
  4. "eta/eta_api/models"
  5. "eta/eta_api/services/alarm_msg"
  6. "eta/eta_api/utils"
  7. "fmt"
  8. "github.com/rdlucklib/rdluck_tools/http"
  9. "time"
  10. )
  11. // PullPolicyReport 策略报告拉取
  12. func PullPolicyReport() (ret models.PullEnglishPolicyDataResp, err error, msg string) {
  13. //调用列表接口,批量插入数据
  14. originData, err := getOriginPolicyReportList()
  15. if err != nil {
  16. msg = "调用英文策略报告列表接口失败"
  17. return
  18. }
  19. condition := ""
  20. var pars []interface{}
  21. startDate := time.Now().AddDate(0, -1, 0).Format(utils.FormatDate)
  22. condition += ` AND publish_time >= ? `
  23. pars = append(pars, startDate)
  24. //获取已插入的报告ID,避免重复插入
  25. existData, err := models.GetEnglishPolicyReportByCondition(condition, pars)
  26. if err != nil {
  27. msg = "查询已存在的英文策略报告列表接口失败"
  28. return
  29. }
  30. existIdsMap := make(map[int]struct{}, 0)
  31. for _, v := range existData {
  32. existIdsMap[v.SourceReportId] = struct{}{}
  33. }
  34. //组装数据,插入到policy_report表中
  35. addItems := make([]*models.EnglishPolicyReport, 0)
  36. addIds := make([]int, 0)
  37. for _, v := range originData {
  38. if _, ok := existIdsMap[v.Id]; ok {
  39. continue
  40. }
  41. addIds = append(addIds, v.Id)
  42. tmp := &models.EnglishPolicyReport{
  43. ClassifyIdFirst: v.Industry.Id,
  44. ClassifyNameFirst: v.Industry.Description,
  45. ClassifyIdSecond: v.FieldId,
  46. ClassifyNameSecond: v.Field.Description,
  47. Title: v.TitleEn,
  48. Abstract: v.Content.Abstract,
  49. Author: v.Author.Name,
  50. Frequency: v.Frequency,
  51. CreateTime: time.Now(),
  52. ModifyTime: time.Now(),
  53. State: 1,
  54. PublishStatus: v.PublishStatus,
  55. PublishTime: v.PublishDate,
  56. KeyTakeaways: v.Content.Annotation,
  57. AuthorMobile: v.Author.PhoneNumber,
  58. SourceReportId: v.Id,
  59. ReportCoverUrl: v.Cover,
  60. }
  61. addItems = append(addItems, tmp)
  62. }
  63. if len(addItems) > 0 {
  64. err = models.AddEnglishPolicyReportMulti(addItems)
  65. if err != nil {
  66. msg = "批量新增英文策略报告信息失败"
  67. return
  68. }
  69. }
  70. //根据原始ID调用详情接口,更新数据
  71. go setPolicyReportContentAndSync(addIds)
  72. ret = models.PullEnglishPolicyDataResp{
  73. Num: len(addItems),
  74. }
  75. return
  76. }
  77. type EnPolicyReportDataListResp struct {
  78. Code int `json:"code"`
  79. Msg string `json:"msg"`
  80. Data []EnPolicyReportDataListItem `json:"data"`
  81. Pagination EnPolicyReportDataPage `json:"pagination"`
  82. }
  83. type EnPolicyReportDataDetailResp struct {
  84. Code int `json:"code"`
  85. Msg string `json:"msg"`
  86. Data EnPolicyReportDataListItem `json:"data"`
  87. }
  88. type EnPolicyReportDataPage struct {
  89. Total int `json:"total"`
  90. Page int `json:"page"`
  91. PageSize int `json:"page_size"`
  92. PageTotal int `json:"page_total"`
  93. }
  94. type EnPolicyReportDataListItem struct {
  95. Id int `json:"id"`
  96. Title string `json:"title"`
  97. TitleEn string `json:"title_en"`
  98. Frequency string `json:"frequency"`
  99. CreateDate time.Time `json:"create_date"`
  100. UpdateDate time.Time `json:"update_date"`
  101. PublishDate time.Time `json:"publish_date"`
  102. PublishStatus int `json:"publish_status"`
  103. VerifyStatus int `json:"verify_status"`
  104. PublishArea string `json:"publish_area"`
  105. AccessLevel int `json:"access_level"`
  106. IsActive bool `json:"is_active"`
  107. AuthorPhoneNumber string `json:"author_phone_number"`
  108. Cover string `json:"cover"`
  109. IndustryId int `json:"industry_id"`
  110. ContentId int `json:"content_id"`
  111. TypeId int `json:"type_id"`
  112. FieldId int `json:"field_id"`
  113. SeriesId int `json:"series_id"`
  114. File interface{} `json:"file"`
  115. Stock []string `json:"stock"`
  116. IsFocused int `json:"is_focused"`
  117. Content EnPolicyReportDataListItemContent `json:"content"`
  118. Industry EnPolicyReportDataListItemInfo `json:"industry"`
  119. Type EnPolicyReportDataListItemInfo `json:"type"`
  120. Field EnPolicyReportDataListItemInfoMore `json:"field"`
  121. Series EnPolicyReportDataListItemInfoMore `json:"series"`
  122. Author EnPolicyReportDataListItemAuthor `json:"author"`
  123. }
  124. type EnPolicyReportDataListItemContent struct {
  125. Id int `json:"id"`
  126. Body string `json:"body"`
  127. Abstract string `json:"abstract"`
  128. Annotation string `json:"annotation"`
  129. }
  130. type EnPolicyReportDataListItemInfo struct {
  131. Id int `json:"id"`
  132. Name string `json:"name"`
  133. Description string `json:"description"`
  134. }
  135. type EnPolicyReportDataListItemInfoMore struct {
  136. Id int `json:"id"`
  137. Name string `json:"name"`
  138. Description string `json:"description"`
  139. IndustryId int `json:"industry_id"`
  140. }
  141. type EnPolicyReportDataListItemAuthor struct {
  142. PhoneNumber string `json:"phone_number"`
  143. Name string `json:"name"`
  144. }
  145. func getOriginPolicyReportList() (list []EnPolicyReportDataListItem, err error) {
  146. //组装请求字段
  147. typeId := 9
  148. //field_id=1 表示 Daily Check-in
  149. fieldId := 1
  150. take := 1000
  151. skip := 0
  152. publishStatus := "1,2"
  153. mode := "all"
  154. startDt := ""
  155. // 获取当天的报告
  156. startDt = time.Now().AddDate(0, 0, -7).Format(utils.FormatDate)
  157. //设置接口地址
  158. //处理返回值
  159. url := utils.EnPolicyReportUrl + `articles/index?type_id=%d&field_id=%d&take=%d&skip=%d&publish_status=%s&mode=%s&start_dt=%s`
  160. url = fmt.Sprintf(url, typeId, fieldId, take, skip, publishStatus, mode, startDt)
  161. utils.FileLog.Info("url:%s", url)
  162. body, err := http.Get(url)
  163. fmt.Println("getOriginPolicyReportList body:")
  164. fmt.Println(string(body))
  165. item := new(EnPolicyReportDataListResp)
  166. err = json.Unmarshal(body, &item)
  167. if err != nil {
  168. return
  169. }
  170. if item.Code != 0 {
  171. err = fmt.Errorf("getOriginPolicyReportDetail ErrCode: %d, ErrMsg: %s", item.Code, item.Msg)
  172. return
  173. }
  174. list = item.Data
  175. return
  176. }
  177. func getOriginPolicyReportDetail(id int) (detail EnPolicyReportDataListItem, err error) {
  178. //设置接口地址
  179. //处理返回值
  180. url := utils.EnPolicyReportUrl + `articles/en/%d`
  181. url = fmt.Sprintf(url, id)
  182. utils.FileLog.Info("url:%s", url)
  183. body, err := http.Get(url)
  184. fmt.Println("getOriginPolicyReportDetail body:")
  185. fmt.Println(string(body))
  186. item := new(EnPolicyReportDataDetailResp)
  187. err = json.Unmarshal(body, &item)
  188. if err != nil {
  189. return
  190. }
  191. if item.Code != 0 {
  192. err = fmt.Errorf("getOriginPolicyReportDetail ErrCode: %d, ErrMsg: %s", item.Code, item.Msg)
  193. return
  194. }
  195. detail = item.Data
  196. return
  197. }
  198. func setPolicyReportContentAndSync(addIds []int) (err error, msg string) {
  199. defer func() {
  200. if err != nil {
  201. go alarm_msg.SendAlarmMsg("设置英文策略报告内容失败,Err:"+err.Error()+";msg:"+msg, 3)
  202. utils.FileLog.Info(fmt.Sprintf("设置英文策略报告内容失败 setPolicyReportContentAndSync,Err:%s,%s", err.Error(), msg))
  203. }
  204. }()
  205. if len(addIds) > 0 {
  206. for _, v := range addIds {
  207. originDetail, e := getOriginPolicyReportDetail(v)
  208. if e != nil {
  209. msg = "拉取英文策略报告详情失败"
  210. err = e
  211. return
  212. }
  213. err = models.SetEnglishPolicyReportContentBySourceReportId(v, originDetail.Content.Body)
  214. if err != nil {
  215. msg = "更新英文策略报告内容失败"
  216. return
  217. }
  218. }
  219. }
  220. //同步到英文研报
  221. err, msg = MultiEnglishPolicyReportSync()
  222. return
  223. }