report_push.go 27 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818
  1. package services
  2. import (
  3. "context"
  4. "encoding/json"
  5. "errors"
  6. "fmt"
  7. "github.com/wenzhenxi/gorsa"
  8. "hongze/hongze_task/models"
  9. "hongze/hongze_task/models/yb"
  10. "hongze/hongze_task/services/alarm_msg"
  11. "hongze/hongze_task/utils"
  12. "io/ioutil"
  13. "net/http"
  14. "net/url"
  15. "strconv"
  16. "strings"
  17. "time"
  18. )
  19. //func init() {
  20. // report, _ := models.GetReportById(836)
  21. // SendReportToThs(report)
  22. //}
  23. // SendWaitReport 定时向同花顺推送报告(定时任务)
  24. func SendWaitReport(cont context.Context) (err error) {
  25. defer func() {
  26. if err != nil {
  27. //fmt.Println(utils.APPNAME+"【"+utils.RunMode+"】"+"失败提醒", "发送消息至同花顺失败 ErrMsg:"+err.Error(), utils.EmailSendToUsers)
  28. //go utils.SendEmail(utils.APPNAME+"【"+utils.RunMode+"】"+"失败提醒", "延时任务发送报告至同花顺失败 ErrMsg:"+err.Error(), utils.EmailSendToUsers)
  29. go alarm_msg.SendAlarmMsg("延时任务发送报告至同花顺失败 ErrMsg:"+err.Error(), 3)
  30. }
  31. }()
  32. list, err := models.GetWaitReportSendThsDetailList()
  33. if err != nil {
  34. return
  35. }
  36. for _, detail := range list {
  37. //先标记为
  38. _ = models.ModifyReportSendThsDetailStatus(detail.SendId, 0, "")
  39. go SendToThs(detail.SendId, detail.ReportId, detail.ReportType)
  40. }
  41. return
  42. }
  43. // SyncWxGroupEveryDay 每日定时向同花顺同步客户时间
  44. func SyncWxGroupEveryDay(cont context.Context) (err error) {
  45. defer func() {
  46. if err != nil {
  47. fmt.Println(utils.APPNAME+"【"+utils.RunMode+"】"+"失败提醒", "同步微信群组信息至同花顺失败 ErrMsg:"+err.Error(), utils.EmailSendToUsers)
  48. //go utils.SendEmail(utils.APPNAME+"【"+utils.RunMode+"】"+"失败提醒", "同步微信群组信息至同花顺失败 ErrMsg:"+err.Error(), utils.EmailSendToUsers)
  49. }
  50. }()
  51. //查询校验当天数据是否已经生成
  52. _, err = models.GetCompanyEndDate(time.Now().Format(utils.FormatDate))
  53. if err == nil || err.Error() != utils.ErrNoRow() {
  54. err = errors.New(fmt.Sprint(time.Now().Format(utils.FormatDate), "当天数据已经同步"))
  55. return
  56. }
  57. //永续、正式、试用、冻结
  58. list, err := models.GetAllCompanyProduct()
  59. if err != nil {
  60. return
  61. }
  62. timeLoc, _ := time.LoadLocation("Asia/Shanghai")
  63. //需要入库的数据
  64. companyEndDateMap := make(map[string]*models.CompanyEndDate)
  65. //实际需要推送的数据
  66. companyEndDatePushMap := make(map[string]*models.CompanyEndDate)
  67. //微信群组开放编号code
  68. companyProductOpenCode := make(map[string]string)
  69. for _, companyProduct := range list {
  70. key := fmt.Sprint(companyProduct.CompanyId, "_", companyProduct.ProductId)
  71. endDate, _ := time.ParseInLocation(utils.FormatDate, companyProduct.EndDate, timeLoc)
  72. if companyProduct.Status == "冻结" {
  73. endDate = time.Now().AddDate(0, 0, -1)
  74. }
  75. tmpCompanyEndDate := &models.CompanyEndDate{
  76. CompanyId: companyProduct.CompanyId,
  77. ProductId: companyProduct.ProductId,
  78. Status: companyProduct.Status,
  79. EndDate: endDate,
  80. CreateTime: time.Now(),
  81. }
  82. companyEndDateMap[key] = tmpCompanyEndDate
  83. companyEndDatePushMap[key] = tmpCompanyEndDate
  84. companyProductOpenCode[key] = companyProduct.OpenCode
  85. }
  86. startDate := time.Now().AddDate(0, 0, -1).Format(utils.FormatDate)
  87. endDate := time.Now().Format(utils.FormatDate)
  88. endDateList, err := models.GetAllCompanyProductEndDate(startDate, endDate)
  89. for _, endDate := range endDateList {
  90. key := fmt.Sprint(endDate.CompanyId, "_", endDate.ProductId)
  91. if companyEndDate, ok := companyEndDatePushMap[key]; ok {
  92. if companyEndDate.Status == "冻结" {
  93. //获取当天的零点时间
  94. endDateStr := time.Now().AddDate(0, 0, -1).Format(utils.FormatDate)
  95. companyEndDate.EndDate, _ = time.ParseInLocation(utils.FormatDate, endDateStr, timeLoc)
  96. }
  97. if companyEndDate.EndDate.Equal(endDate.EndDate) {
  98. delete(companyEndDatePushMap, key)
  99. }
  100. }
  101. }
  102. for _, companyEndDate := range companyEndDatePushMap {
  103. fmt.Println("待推送数据:", companyEndDate)
  104. key := fmt.Sprint(companyEndDate.CompanyId, "_", companyEndDate.ProductId)
  105. //同步至同花顺
  106. if openCode, ok := companyProductOpenCode[key]; ok {
  107. SyncWxGroup(openCode, companyEndDate.EndDate.Format(utils.FormatDate))
  108. }
  109. }
  110. for _, companyEndDate := range companyEndDateMap {
  111. models.AddCompanyEndDate(companyEndDate)
  112. }
  113. return
  114. }
  115. var permissionMap map[string]string = map[string]string{
  116. "化里化外日评": "原油,PTA,MEG,织造终端,甲醇,聚烯烃,沥青,橡胶,苯乙烯,玻璃纯碱",
  117. "股债日评": "宏观,利率债,原油,PTA,MEG,织造终端,甲醇,聚烯烃,沥青,橡胶,苯乙烯,玻璃纯碱,钢材,铁矿,双焦(焦煤、焦炭),有色(铜、铝),有色(锌、铅),镍+不锈钢",
  118. "贵金属复盘": "宏观,利率债,原油,PTA,MEG,织造终端,甲醇,聚烯烃,沥青,橡胶,苯乙烯,玻璃纯碱,钢材,铁矿,双焦(焦煤、焦炭),有色(铜、铝),有色(锌、铅),镍+不锈钢",
  119. "每日经济数据备忘录": "宏观,利率债,原油,PTA,MEG,织造终端,甲醇,聚烯烃,沥青,橡胶,苯乙烯,玻璃纯碱,钢材,铁矿,双焦(焦煤、焦炭),有色(铜、铝),有色(锌、铅),镍+不锈钢",
  120. "宏观商品复盘": "宏观,利率债,原油,PTA,MEG,织造终端,甲醇,聚烯烃,沥青,橡胶,苯乙烯,玻璃纯碱,钢材,铁矿,双焦(焦煤、焦炭),有色(铜、铝),有色(锌、铅),镍+不锈钢",
  121. "知白守黑日评": "钢材,铁矿,双焦(焦煤、焦炭)",
  122. "有声有色日度闲篇": "有色(铜、铝),有色(锌、铅),镍+不锈钢",
  123. "EIA原油库存点评": "原油",
  124. "苯乙烯数据点评": "苯乙烯",
  125. "API原油库存点评": "原油",
  126. "铁矿航运数据点评": "铁矿",
  127. "中观需求点评": "宏观,利率债,原油,PTA,MEG,织造终端,甲醇,聚烯烃,沥青,橡胶,苯乙烯,玻璃纯碱,钢材,铁矿,双焦(焦煤、焦炭),有色(铜、铝),有色(锌、铅),镍+不锈钢",
  128. "聚酯数据点评": "PTA,MEG",
  129. "钢材周度数据点评": "钢材",
  130. "寻根知本": "宏观,利率债,原油,PTA,MEG,织造终端,甲醇,聚烯烃,沥青,橡胶,苯乙烯,玻璃纯碱,钢材,铁矿,双焦(焦煤、焦炭),有色(铜、铝),有色(锌、铅),镍+不锈钢",
  131. "国际宏观": "宏观,利率债,原油,PTA,MEG,织造终端,甲醇,聚烯烃,沥青,橡胶,苯乙烯,玻璃纯碱,钢材,铁矿,双焦(焦煤、焦炭),有色(铜、铝),有色(锌、铅),镍+不锈钢",
  132. "能化百家谈": "原油,PTA,MEG,织造终端,甲醇,聚烯烃,沥青,橡胶,苯乙烯,玻璃纯碱",
  133. "有色百家谈": "有色(铜、铝),有色(锌、铅),镍+不锈钢",
  134. "黑色百家谈": "钢材,铁矿,双焦(焦煤、焦炭)",
  135. }
  136. // permissionLabelMap 品种与同花顺标签的映射关系
  137. var permissionLabelMap = map[string]string{
  138. "宏观经济": "宏观",
  139. "利率债": "利率债",
  140. "原油": "原油",
  141. "PTA": "PTA",
  142. "MEG": "MEG",
  143. "织造终端": "织造终端",
  144. "甲醇": "甲醇",
  145. "聚烯烃": "聚烯烃",
  146. "沥青": "沥青",
  147. "纯苯+苯乙烯": "纯苯+苯乙烯",
  148. "玻璃纯碱": "玻璃纯碱",
  149. "钢材": "钢材",
  150. "铁矿": "铁矿",
  151. "双焦(焦煤、焦炭)": "双焦(焦煤、焦炭)",
  152. "有色(锌)": "有色(锌)",
  153. "有色(铜、铝)": "有色(铜、铝)",
  154. "镍+不锈钢": "镍+不锈钢",
  155. "PVC": "PVC",
  156. "聚酯": "聚酯",
  157. "钴锂": "钴锂",
  158. "策略": "策略",
  159. "苯乙烯": "纯苯+苯乙烯",
  160. "锌": "有色(锌)",
  161. "双焦": "双焦(焦煤、焦炭)",
  162. "铜/铝": "有色(铜、铝)",
  163. "镍/不锈钢": "镍+不锈钢",
  164. "成品油": "成品油",
  165. "油品": "成品油",
  166. "纺服": "织造终端",
  167. }
  168. //TshResult 同花顺返回信息
  169. type TshResult struct {
  170. ErrorCode int `json:"error" description:"错误状态码"`
  171. Message string `json:"message" description:"提示信息"`
  172. }
  173. //
  174. func SendToThs(sendDetailId, reportId int, reportType string) (err error) {
  175. switch reportType {
  176. case "日度点评":
  177. tmpErr := SendReportToThs(reportId)
  178. if tmpErr != nil {
  179. err = tmpErr
  180. }
  181. case "研报小程序", "研报":
  182. tmpErr := SendReportMiniToThs(reportId)
  183. if tmpErr != nil {
  184. err = tmpErr
  185. }
  186. case "研报价格驱动", "价格驱动":
  187. tmpErr := SendYbPriceDrivenToThs(reportId)
  188. if tmpErr != nil {
  189. err = tmpErr
  190. }
  191. case "研报视频社区", "视频社区":
  192. tmpErr := SendYbCommunityVideoToThs(reportId)
  193. if tmpErr != nil {
  194. err = tmpErr
  195. }
  196. case "研报线上路演", "线上路演":
  197. tmpErr := SendYbRoadVideoToThs(reportId)
  198. if tmpErr != nil {
  199. err = tmpErr
  200. }
  201. case "语音播报":
  202. tmpErr := SendYbVoiceBroadcastToThs(reportId)
  203. if tmpErr != nil {
  204. err = tmpErr
  205. }
  206. default:
  207. err = errors.New("异常类型")
  208. }
  209. if err != nil {
  210. _ = models.ModifyReportSendThsDetailStatus(sendDetailId, -1, err.Error())
  211. err = nil
  212. return
  213. }
  214. _ = models.ModifyReportSendThsDetailStatus(sendDetailId, 1, "")
  215. return
  216. }
  217. // SendReportToThs 发送报告到同花顺
  218. func SendReportToThs(reportId int) (err error) {
  219. defer func() {
  220. if err != nil {
  221. //fmt.Println(utils.APPNAME+"【"+utils.RunMode+"】"+"失败提醒", "发送消息至同花顺失败 ErrMsg:"+err.Error(), utils.EmailSendToUsers)
  222. //go utils.SendEmail(utils.APPNAME+"【"+utils.RunMode+"】"+"失败提醒", "发送报告至同花顺失败 ErrMsg:"+err.Error(), utils.EmailSendToUsers)
  223. go alarm_msg.SendAlarmMsg("发送报告至同花顺失败 ErrMsg:"+err.Error(), 3)
  224. }
  225. }()
  226. report, tmpErr := models.GetReportById(reportId)
  227. if tmpErr != nil {
  228. err = tmpErr
  229. return
  230. }
  231. //获取分类信息(标签)
  232. permissionName := report.ClassifyNameSecond
  233. classifyItem, err := models.GetClassifyById(report.ClassifyIdSecond)
  234. if err != nil {
  235. err = errors.New(fmt.Sprint("获取分类失败:", permissionName))
  236. }
  237. //获取权限标签名称
  238. var permissionStr string
  239. if classifyItem != nil {
  240. permissionStr = classifyItem.ClassifyLabel
  241. if permissionStr == "" {
  242. var isOk bool
  243. permissionStr, isOk = permissionMap[permissionName]
  244. if !isOk {
  245. err = errors.New(fmt.Sprint("没有该权限的标签,权限名:", permissionName))
  246. return
  247. }
  248. }
  249. } else {
  250. var isOk bool
  251. permissionStr, isOk = permissionMap[permissionName]
  252. if !isOk {
  253. err = errors.New(fmt.Sprint("没有该权限的标签,权限名:", permissionName))
  254. return
  255. }
  256. }
  257. if permissionStr == "" {
  258. err = errors.New(fmt.Sprint("没有该权限的标签,权限名:", permissionName))
  259. return
  260. }
  261. //fmt.Println("sendDetailId:", sendDetailId)
  262. stageStr := fmt.Sprintf("%v", report.Stage)
  263. createDate, err := time.Parse(utils.FormatDateTime, report.CreateTime)
  264. createDateFrom := createDate.Format("0102")
  265. title := `【第` + stageStr + `期|FICC】` + report.Title + `(` + createDateFrom + ")"
  266. // 跳转地址
  267. jumpBaseUrl := `http://rddpweb.brilliantstart.cn/reportdtl?id=`
  268. //生产环境地址
  269. if utils.RunMode == "release" {
  270. jumpBaseUrl = `https://ficc.hzinsights.com/reportdtl?id=`
  271. }
  272. jumpUrl := fmt.Sprint(jumpBaseUrl, report.Id)
  273. // logo 地址
  274. logoUrl := `https://hongze.oss-cn-shanghai.aliyuncs.com/hzyj.png`
  275. // 简介
  276. abstract := report.Abstract
  277. // 实际推送到同花顺
  278. err = SendThs(title, permissionStr, abstract, jumpUrl, logoUrl, "1")
  279. return
  280. }
  281. // SendThs 发送消息到同花顺
  282. func SendThs(title, labelStr, abstract, jumpBaseUrl, logoUrl, dataType string) (err error) {
  283. defer func() {
  284. if err != nil {
  285. //fmt.Println(utils.APPNAME+"【"+utils.RunMode+"】"+"失败提醒", "发送消息至同花顺失败 ErrMsg:"+err.Error(), utils.EmailSendToUsers)
  286. //go utils.SendEmail(utils.APPNAME+"【"+utils.RunMode+"】"+"失败提醒", "发送消息至同花顺失败 ErrMsg:"+err.Error(), utils.EmailSendToUsers)
  287. go alarm_msg.SendAlarmMsg("发送消息至同花顺失败 ErrMsg:"+err.Error(), 3)
  288. }
  289. }()
  290. pubKey := utils.THS_PubKey
  291. sendUrl := utils.THS_SendUrl
  292. //fmt.Println("sendUrl:", sendUrl)
  293. //标题字符长度截取,最多50位字符
  294. title = utils.SubStr(title, 50)
  295. utils.FileLog.Info(fmt.Sprintf("title:%s", title))
  296. title, err = gorsa.PublicEncrypt(title, pubKey)
  297. if err != nil {
  298. return
  299. }
  300. //简介字符长度截取,最多50位字符
  301. abstract = utils.SubStr(abstract, 50)
  302. utils.FileLog.Info(fmt.Sprintf("abstract:%s", abstract))
  303. abstract, err = gorsa.PublicEncrypt(abstract, pubKey)
  304. if err != nil {
  305. return
  306. }
  307. // 关联后的标签数据
  308. newLabelList := make([]string, 0)
  309. labelList := strings.Split(labelStr, ",")
  310. for _, v := range labelList {
  311. tmpLabel, ok := permissionLabelMap[v] //判断是否在关联标签里面
  312. if !ok { //如果不在关联标签里面,那么就把原始的值赋值给
  313. tmpLabel = v
  314. }
  315. newLabelList = append(newLabelList, tmpLabel)
  316. }
  317. labelStr = strings.Join(newLabelList, ",")
  318. utils.FileLog.Info(fmt.Sprintf("labelStr:%s", labelStr))
  319. label, err := gorsa.PublicEncrypt(labelStr, pubKey)
  320. if err != nil {
  321. return
  322. }
  323. jumpUrl, err := gorsa.PublicEncrypt(jumpBaseUrl, pubKey)
  324. if err != nil {
  325. return
  326. }
  327. picUrl, err := gorsa.PublicEncrypt(logoUrl, pubKey)
  328. if err != nil {
  329. return
  330. }
  331. dataTypeEncript, err := gorsa.PublicEncrypt(dataType, pubKey)
  332. if err != nil {
  333. return
  334. }
  335. //开始发送
  336. client := http.Client{}
  337. form := url.Values{}
  338. form.Add("title", title)
  339. form.Add("description", abstract)
  340. form.Add("label", label)
  341. form.Add("url", jumpUrl)
  342. form.Add("icon", picUrl)
  343. form.Add("dataType", dataTypeEncript)
  344. utils.FileLog.Info(fmt.Sprintf("SendThs parms:%s", form.Encode()))
  345. resp, err := client.PostForm(sendUrl, form)
  346. if err != nil {
  347. return
  348. }
  349. defer resp.Body.Close()
  350. body, _ := ioutil.ReadAll(resp.Body)
  351. //fmt.Println(string(body))
  352. utils.FileLog.Info(fmt.Sprintf("ThsResult parms:%s", string(body)))
  353. //同花顺接口返回数据
  354. var tshResult TshResult
  355. err = json.Unmarshal(body, &tshResult)
  356. if err != nil {
  357. err = errors.New(fmt.Sprint("同花顺接口返回数据转换成结构体异常,Err:", err))
  358. return
  359. }
  360. if tshResult.ErrorCode != 1 {
  361. err = errors.New(fmt.Sprint("发送数据到同花顺接口异常,result:", string(body)))
  362. return
  363. }
  364. return
  365. }
  366. // SyncWxGroup 同步同花顺 微信群信息
  367. func SyncWxGroup(openCompanyCode, deadline string) (err error) {
  368. defer func() {
  369. if err != nil {
  370. //fmt.Println(utils.APPNAME+"【"+utils.RunMode+"】"+"失败提醒", "同步同花顺微信群信息失败 ErrMsg:"+err.Error(), utils.EmailSendToUsers)
  371. //go utils.SendEmail(utils.APPNAME+"【"+utils.RunMode+"】"+"失败提醒", "同步同花顺微信群信息失败 ErrMsg:"+err.Error(), utils.EmailSendToUsers)
  372. go alarm_msg.SendAlarmMsg("同步同花顺微信群信息失败 ErrMsg:"+err.Error(), 3)
  373. }
  374. }()
  375. pubKey := utils.THS_PubKey
  376. sendUrl := utils.THS_SyncWxGroupUrl
  377. openCompanyCode, err = gorsa.PublicEncrypt(openCompanyCode, pubKey)
  378. if err != nil {
  379. return
  380. }
  381. deadline, err = gorsa.PublicEncrypt(deadline, pubKey)
  382. if err != nil {
  383. return
  384. }
  385. status := `1`
  386. status, err = gorsa.PublicEncrypt(status, pubKey)
  387. if err != nil {
  388. return
  389. }
  390. //开始发送
  391. client := http.Client{}
  392. form := url.Values{}
  393. form.Add("thirdWechatGroupId", openCompanyCode)
  394. form.Add("deadline", deadline)
  395. form.Add("status", status)
  396. utils.FileLog.Info(fmt.Sprintf("SendThs SyncWxGroup parms:%s", form.Encode()))
  397. resp, err := client.PostForm(sendUrl, form)
  398. if err != nil {
  399. return
  400. }
  401. defer resp.Body.Close()
  402. body, _ := ioutil.ReadAll(resp.Body)
  403. fmt.Println(string(body))
  404. utils.FileLog.Info(fmt.Sprintf("SyncWxGroup ThsResult parms:%s", string(body)))
  405. //同花顺接口返回数据
  406. var tshResult TshResult
  407. err = json.Unmarshal(body, &tshResult)
  408. if err != nil {
  409. err = errors.New(fmt.Sprint("同花顺接口返回数据转换成结构体异常,Err:", err))
  410. return
  411. }
  412. if tshResult.ErrorCode != 1 {
  413. err = errors.New(fmt.Sprint("发送数据到同花顺接口异常,result:", string(body)))
  414. return
  415. }
  416. return
  417. }
  418. // SendReportMiniToThs 发送报告小程序到同花顺
  419. func SendReportMiniToThs(reportId int) (err error) {
  420. defer func() {
  421. if err != nil {
  422. //fmt.Println(utils.APPNAME+"【"+utils.RunMode+"】"+"失败提醒", "发送消息至同花顺失败 ErrMsg:"+err.Error(), utils.EmailSendToUsers)
  423. //go utils.SendEmail(utils.APPNAME+"【"+utils.RunMode+"】"+"失败提醒", "SendReportMiniToThs延时任务发送报告至同花顺失败, ReportId:"+strconv.Itoa(reportId)+", ErrMsg:"+err.Error(), utils.EmailSendToUsers)
  424. go alarm_msg.SendAlarmMsg("SendReportMiniToThs延时任务发送报告至同花顺失败, ReportId:"+strconv.Itoa(reportId)+", ErrMsg:"+err.Error(), 3)
  425. }
  426. }()
  427. report, tmpErr := models.GetReportById(reportId)
  428. if tmpErr != nil {
  429. err = tmpErr
  430. return
  431. }
  432. // 获取研报小程序配置
  433. ybConf, err := GetYbAppIdInfo()
  434. if err != nil {
  435. return
  436. }
  437. //小程序跳转地址
  438. jumpBaseUrl := ybConf.AppId + `/pages-report/reportDetail?reportId=`
  439. jumpUrl := fmt.Sprint(jumpBaseUrl, report.Id)
  440. logoUrl := `https://hongze.oss-cn-shanghai.aliyuncs.com/hzyj.png`
  441. var permissionStr string
  442. if report.HasChapter == 0 {
  443. // 获取分类信息(标签)
  444. permissionName := report.ClassifyNameSecond
  445. classifyItem, tmpErr := models.GetClassifyById(report.ClassifyIdSecond)
  446. if tmpErr != nil {
  447. err = errors.New(fmt.Sprint("获取分类失败:", permissionName))
  448. return
  449. }
  450. // 获取权限标签名称
  451. if classifyItem != nil {
  452. permissionStr = classifyItem.ClassifyLabel
  453. if permissionStr == "" {
  454. var isOk bool
  455. permissionStr, isOk = permissionMap[permissionName]
  456. if !isOk {
  457. err = errors.New(fmt.Sprint("没有该权限的标签,权限名:", permissionName))
  458. return
  459. }
  460. }
  461. } else {
  462. var isOk bool
  463. permissionStr, isOk = permissionMap[permissionName]
  464. if !isOk {
  465. err = errors.New(fmt.Sprint("没有该权限的标签,权限名:", permissionName))
  466. return
  467. }
  468. }
  469. if permissionStr == "" {
  470. err = errors.New(fmt.Sprint("没有该权限的标签,权限名:", permissionName))
  471. return
  472. }
  473. } else {
  474. // 同php的
  475. permissionStr = "宏观,利率债,原油,PTA,MEG,织造终端,甲醇,聚烯烃,沥青,橡胶,苯乙烯,玻璃纯碱,钢材,铁矿,双焦(焦煤、焦炭),有色(铜、铝),有色(锌、铅),镍+不锈钢"
  476. }
  477. // 实际推送到同花顺
  478. err = SendThs(report.Title, permissionStr, report.Abstract, jumpUrl, logoUrl, "2")
  479. return
  480. }
  481. // SendYbPriceDrivenToThs 推送研报小程序价格驱动客群消息
  482. func SendYbPriceDrivenToThs(priceDrivenId int) (err error) {
  483. defer func() {
  484. if err != nil {
  485. go alarm_msg.SendAlarmMsg("SendYbPriceDrivenToThs-延时任务发送报告至同花顺失败, PriceDrivenId:"+strconv.Itoa(priceDrivenId)+", ErrMsg:"+err.Error(), 3)
  486. }
  487. }()
  488. priceDriven, e := yb.GetPriceDrivenById(priceDrivenId)
  489. if e != nil {
  490. err = errors.New("获取价格驱动信息失败, Err:" + e.Error())
  491. return
  492. }
  493. // 标签信息
  494. if priceDriven.VarietyTagId <= 0 {
  495. return
  496. }
  497. varietyTag, err := models.GetVarietyTagById(priceDriven.VarietyTagId)
  498. if err != nil {
  499. return
  500. }
  501. permissionName := varietyTag.ChartPermissionName
  502. if permissionName == "" {
  503. err = errors.New("客群标签为空, 不可推送")
  504. return
  505. }
  506. if permissionName == "宏观经济" {
  507. permissionName = "宏观"
  508. }
  509. // 获取研报小程序配置
  510. ybConf, err := GetYbAppIdInfo()
  511. if err != nil {
  512. return
  513. }
  514. title := fmt.Sprintf("%s价格驱动", permissionName)
  515. jumpUrl := fmt.Sprintf(`%s/pages/pricedriven/pricedriven?default_classify_first=%d&default_classify_sub=%d`, ybConf.AppId, varietyTag.VarietyClassifyId, varietyTag.VarietyTagId)
  516. logoUrl := `https://hongze.oss-cn-shanghai.aliyuncs.com/hzyj.png`
  517. // 推送至同花顺
  518. dataType := "2"
  519. err = SendThs(title, permissionName, title, jumpUrl, logoUrl, dataType)
  520. return
  521. }
  522. // SendYbCommunityVideoToThs 推送研报小程序视频社区客群消息
  523. func SendYbCommunityVideoToThs(videoId int) (err error) {
  524. defer func() {
  525. if err != nil {
  526. go alarm_msg.SendAlarmMsg("SendYbCommunityVideoToThs-延时任务发送报告至同花顺失败, CommunityVideoId:"+strconv.Itoa(videoId)+", ErrMsg:"+err.Error(), 3)
  527. }
  528. }()
  529. video, e := yb.GetVideoById(videoId)
  530. if e != nil {
  531. err = errors.New("获取视频信息失败, Err:" + e.Error())
  532. return
  533. }
  534. // 获取研报小程序配置
  535. ybConf, err := GetYbAppIdInfo()
  536. if err != nil {
  537. return
  538. }
  539. permissionName := "宏观" // 所有客群都推
  540. title := video.Title
  541. jumpUrl := fmt.Sprint(ybConf.AppId+`/pages/video/videoList?videoId=`, videoId)
  542. logoUrl := `https://hongze.oss-cn-shanghai.aliyuncs.com/hzyj.png`
  543. // 推送至同花顺
  544. dataType := "2"
  545. err = SendThs(title, permissionName, title, jumpUrl, logoUrl, dataType)
  546. return
  547. }
  548. // SendYbRoadVideoToThs 推送研报路演视频客群消息
  549. func SendYbRoadVideoToThs(videoId int) (err error) {
  550. defer func() {
  551. if err != nil {
  552. go alarm_msg.SendAlarmMsg("SendYbRoadVideoToThs-延时任务发送报告至同花顺失败, RoadVideoId:"+strconv.Itoa(videoId)+", ErrMsg:"+err.Error(), 3)
  553. }
  554. }()
  555. video, e := yb.GetRoadVideoById(videoId)
  556. if e != nil {
  557. err = errors.New("获取视频信息失败, Err:" + e.Error())
  558. return
  559. }
  560. chartPermissionIds := video.ChartPermissionIds
  561. // 标签信息
  562. if chartPermissionIds == "" {
  563. return
  564. }
  565. chartPermissionIdSlice := strings.Split(chartPermissionIds, ",")
  566. chartList, e := models.GetChartPermissionByIds(chartPermissionIdSlice)
  567. if e != nil {
  568. err = errors.New("获取品种信息失败, Err:" + e.Error())
  569. return
  570. }
  571. permissionName := ""
  572. for _, v := range chartList {
  573. if v.PermissionName == "宏观经济" {
  574. v.PermissionName = "宏观"
  575. }
  576. permissionName += v.PermissionName + ","
  577. }
  578. if permissionName == "" {
  579. err = errors.New("客群标签为空, 不可推送")
  580. return
  581. }
  582. permissionName = strings.Trim(permissionName, ",")
  583. //permissionName := "宏观" //写死宏观,默认所有群都推
  584. // 获取研报小程序配置
  585. ybConf, err := GetYbAppIdInfo()
  586. if err != nil {
  587. return
  588. }
  589. title := video.Title
  590. jumpUrl := fmt.Sprint(ybConf.AppId+`/pages/roadShow/video/list?videoId=`, videoId)
  591. logoUrl := `https://hongze.oss-cn-shanghai.aliyuncs.com/hzyj.png`
  592. // 推送至同花顺
  593. dataType := "2"
  594. err = SendThs(title, permissionName, title, jumpUrl, logoUrl, dataType)
  595. return
  596. }
  597. // SendYbVoiceBroadcastToThs 推送研报小程序语音播报客群消息
  598. func SendYbVoiceBroadcastToThs(voiceId int) (err error) {
  599. defer func() {
  600. if err != nil {
  601. go alarm_msg.SendAlarmMsg("SendYbCommunityVideoToThs-延时任务发送报告至同花顺失败, voiceBroadcastId:"+strconv.Itoa(voiceId)+", ErrMsg:"+err.Error(), 3)
  602. }
  603. }()
  604. voice, e := yb.GetBroadcastById(voiceId)
  605. if e != nil {
  606. err = errors.New("获取语音播报信息失败, Err:" + e.Error())
  607. return
  608. }
  609. // 获取研报小程序配置
  610. ybConf, err := GetYbAppIdInfo()
  611. if err != nil {
  612. return
  613. }
  614. permissionName := "宏观" // 所有客群都推
  615. title := voice.BroadcastName
  616. jumpUrl := fmt.Sprint(ybConf.AppId+`/pages-voice/voiceDetail?voiceId=`, voiceId)
  617. logoUrl := `https://hongze.oss-cn-shanghai.aliyuncs.com/hzyj.png`
  618. // 推送至同花顺
  619. dataType := "2"
  620. err = SendThs(title, permissionName, title, jumpUrl, logoUrl, dataType)
  621. return
  622. }
  623. //TshListResult 同花顺返回信息
  624. type TshListResult struct {
  625. Error int64 `json:"error"`
  626. Message string `json:"message"`
  627. Rows TshListRowsResult `json:"rows"`
  628. }
  629. // TshListRowsResult 同花顺列表数据
  630. type TshListRowsResult struct {
  631. BeginPageIndex int64 `json:"beginPageIndex"`
  632. CurrentPage int64 `json:"currentPage"`
  633. EndPageIndex int64 `json:"endPageIndex"`
  634. HasNext bool `json:"hasNext"`
  635. HasPre bool `json:"hasPre"`
  636. NextPage int64 `json:"nextPage"`
  637. NumPerPage int64 `json:"numPerPage"`
  638. PageCount int64 `json:"pageCount"`
  639. PrePage int64 `json:"prePage"`
  640. RecordList []struct {
  641. Ctime int64 `json:"ctime" description:"创建时间"`
  642. DataType int64 `json:"dataType" description:"数据类型;1-文章,2-小程序"`
  643. Description string `json:"description" description:"描述"`
  644. Icon string `json:"icon" description:"图标"`
  645. ID int64 `json:"id" description:""`
  646. Label string `json:"label" description:"标签"`
  647. OrgCode string `json:"orgCode" description:""`
  648. PushTime string `json:"pushTime" description:""`
  649. Pushed int64 `json:"pushed" description:"已推微信群数量"`
  650. QueryRowCount int64 `json:"queryRowCount" description:""`
  651. Title string `json:"title" description:"标题"`
  652. Total int64 `json:"total" description:"推送微信群总数量"`
  653. URL string `json:"url" description:"跳转地址"`
  654. } `json:"recordList"`
  655. TotalCount int64 `json:"totalCount"`
  656. }
  657. // CheckThsReportList 定时检测同花顺客群推送状态信息
  658. func CheckThsReportList(cont context.Context) (err error) {
  659. errMsgList := make([]string, 0)
  660. defer func() {
  661. if len(errMsgList) > 0 {
  662. emailStr := `317699326@qq.com;984198890@qq.com;pdzhao@hzinsights.com`
  663. go alarm_msg.SendAlarmMsgWithEmail("定时检测同花顺客群推送状态:<br/>"+strings.Join(errMsgList, "<br/>"), emailStr, 3)
  664. //fmt.Println("定时检测同花顺客群推送状态:\n"+strings.Join(errMsgList, "\n"), 3)
  665. }
  666. }()
  667. // 暂时关停检测
  668. return
  669. tshListRowsResult, err := thsReportList(`50`)
  670. if err != nil {
  671. return
  672. }
  673. for _, v := range tshListRowsResult.RecordList {
  674. if v.Pushed == 0 { //一个群都没有推送的情况
  675. tmpTime := time.Unix(v.Ctime/1000, 0) //入库时间
  676. //超时五分钟还未推送,那么就要去通知了
  677. if time.Now().Sub(tmpTime).Minutes() > 5 {
  678. errMsg := fmt.Sprintf("报告名称:《%s》超时未推送客群,接口通知同花顺时间:%s;报告跳转地址:%s", v.Title, tmpTime.Format(utils.FormatDateTime), v.URL)
  679. errMsgList = append(errMsgList, errMsg)
  680. }
  681. }
  682. }
  683. return
  684. }
  685. // thsReportList 同花顺列表接口
  686. func thsReportList(numPerPageStr string) (tshListRowsResult TshListRowsResult, err error) {
  687. defer func() {
  688. if err != nil {
  689. //fmt.Println(utils.APPNAME+"【"+utils.RunMode+"】"+"失败提醒", "发送消息至同花顺失败 ErrMsg:"+err.Error(), utils.EmailSendToUsers)
  690. //go utils.SendEmail(utils.APPNAME+"【"+utils.RunMode+"】"+"失败提醒", "发送消息至同花顺失败 ErrMsg:"+err.Error(), utils.EmailSendToUsers)
  691. go alarm_msg.SendAlarmMsg("定时获取同花顺列表数据接口失败 ErrMsg:"+err.Error(), 3)
  692. }
  693. }()
  694. sendUrl := utils.THS_List_Url
  695. //sendUrl := `https://board.10jqka.com.cn/gateway/ps/syncNews/listPage`
  696. pageNumStr := `1` // 页码;默认1
  697. utils.FileLog.Info(fmt.Sprintf("pageNum:%s", pageNumStr))
  698. if err != nil {
  699. return
  700. }
  701. //numPerPageStr := "10" // 每页数量;默认10 否
  702. utils.FileLog.Info(fmt.Sprintf("numPerPage:%s", numPerPageStr))
  703. if err != nil {
  704. return
  705. }
  706. //pageNum int 页码;默认1 否
  707. //numPerPage int 每页数量;默认10 否
  708. //title String 标题 否
  709. //startDate String 开始日期;如2022-08-12 否
  710. //endDate String 结束日期;如2022-08-12 否
  711. //开始发送
  712. client := http.Client{}
  713. form := url.Values{}
  714. form.Add("pageNum", pageNumStr)
  715. form.Add("numPerPage", numPerPageStr)
  716. //utils.FileLog.Info(fmt.Sprintf("Ths List parms:%s", form.Encode()))
  717. resp, err := client.PostForm(sendUrl, form)
  718. if err != nil {
  719. return
  720. }
  721. defer resp.Body.Close()
  722. body, _ := ioutil.ReadAll(resp.Body)
  723. //fmt.Println(string(body))
  724. //utils.FileLog.Info(fmt.Sprintf("Ths List Result :%s", string(body)))
  725. //同花顺接口返回数据
  726. var tshResult TshListResult
  727. err = json.Unmarshal(body, &tshResult)
  728. if err != nil {
  729. err = errors.New(fmt.Sprint("同花顺接口返回数据转换成结构体异常,Err:", err))
  730. return
  731. }
  732. if tshResult.Error != 1 {
  733. err = errors.New(fmt.Sprint("发送数据到同花顺接口异常,result:", string(body)))
  734. return
  735. }
  736. tshListRowsResult = tshResult.Rows
  737. return
  738. }