report_push.go 27 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783
  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 := SendYbCommunityVideoToThs(reportId)
  188. if tmpErr != nil {
  189. err = tmpErr
  190. }
  191. case "研报线上路演", "线上路演":
  192. tmpErr := SendYbRoadVideoToThs(reportId)
  193. if tmpErr != nil {
  194. err = tmpErr
  195. }
  196. case "研报价格驱动", "价格驱动":
  197. tmpErr := SendYbPriceDrivenToThs(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. jumpBaseUrl := utils.WxYbAppId + `/pages-report/reportDetail?reportId=`
  434. jumpUrl := fmt.Sprint(jumpBaseUrl, report.Id)
  435. logoUrl := `https://hongze.oss-cn-shanghai.aliyuncs.com/hzyj.png`
  436. var permissionStr string
  437. if report.HasChapter == 0 {
  438. // 获取分类信息(标签)
  439. permissionName := report.ClassifyNameSecond
  440. classifyItem, tmpErr := models.GetClassifyById(report.ClassifyIdSecond)
  441. if tmpErr != nil {
  442. err = errors.New(fmt.Sprint("获取分类失败:", permissionName))
  443. return
  444. }
  445. // 获取权限标签名称
  446. if classifyItem != nil {
  447. permissionStr = classifyItem.ClassifyLabel
  448. if permissionStr == "" {
  449. var isOk bool
  450. permissionStr, isOk = permissionMap[permissionName]
  451. if !isOk {
  452. err = errors.New(fmt.Sprint("没有该权限的标签,权限名:", permissionName))
  453. return
  454. }
  455. }
  456. } else {
  457. var isOk bool
  458. permissionStr, isOk = permissionMap[permissionName]
  459. if !isOk {
  460. err = errors.New(fmt.Sprint("没有该权限的标签,权限名:", permissionName))
  461. return
  462. }
  463. }
  464. if permissionStr == "" {
  465. err = errors.New(fmt.Sprint("没有该权限的标签,权限名:", permissionName))
  466. return
  467. }
  468. } else {
  469. // 同php的
  470. permissionStr = "宏观,利率债,原油,PTA,MEG,织造终端,甲醇,聚烯烃,沥青,橡胶,苯乙烯,玻璃纯碱,钢材,铁矿,双焦(焦煤、焦炭),有色(铜、铝),有色(锌、铅),镍+不锈钢"
  471. }
  472. // 实际推送到同花顺
  473. err = SendThs(report.Title, permissionStr, report.Abstract, jumpUrl, logoUrl, "2")
  474. return
  475. }
  476. // SendYbPriceDrivenToThs 推送研报小程序价格驱动客群消息
  477. func SendYbPriceDrivenToThs(priceDrivenId int) (err error) {
  478. defer func() {
  479. if err != nil {
  480. go alarm_msg.SendAlarmMsg("SendYbPriceDrivenToThs-延时任务发送报告至同花顺失败, PriceDrivenId:"+strconv.Itoa(priceDrivenId)+", ErrMsg:"+err.Error(), 3)
  481. }
  482. }()
  483. priceDriven, e := yb.GetPriceDrivenById(priceDrivenId)
  484. if e != nil {
  485. err = errors.New("获取价格驱动信息失败, Err:" + e.Error())
  486. return
  487. }
  488. // 标签信息
  489. if priceDriven.VarietyTagId <= 0 {
  490. return
  491. }
  492. varietyTag, err := models.GetVarietyTagById(priceDriven.VarietyTagId)
  493. if err != nil {
  494. return
  495. }
  496. permissionName := varietyTag.ChartPermissionName
  497. if permissionName == "" {
  498. err = errors.New("客群标签为空, 不可推送")
  499. return
  500. }
  501. if permissionName == "宏观经济" {
  502. permissionName = "宏观"
  503. }
  504. title := fmt.Sprintf("%s价格驱动", permissionName)
  505. jumpUrl := fmt.Sprintf(`%s/pages/pricedriven/pricedriven?default_classify_first=%d&default_classify_sub=%d`, utils.WxYbAppId, varietyTag.VarietyClassifyId, varietyTag.VarietyTagId)
  506. logoUrl := `https://hongze.oss-cn-shanghai.aliyuncs.com/hzyj.png`
  507. // 推送至同花顺
  508. dataType := "2"
  509. err = SendThs(title, permissionName, title, jumpUrl, logoUrl, dataType)
  510. return
  511. }
  512. // SendYbCommunityVideoToThs 推送研报小程序视频社区客群消息
  513. func SendYbCommunityVideoToThs(videoId int) (err error) {
  514. defer func() {
  515. if err != nil {
  516. go alarm_msg.SendAlarmMsg("SendYbCommunityVideoToThs-延时任务发送报告至同花顺失败, CommunityVideoId:"+strconv.Itoa(videoId)+", ErrMsg:"+err.Error(), 3)
  517. }
  518. }()
  519. video, e := yb.GetVideoById(videoId)
  520. if e != nil {
  521. err = errors.New("获取视频信息失败, Err:" + e.Error())
  522. return
  523. }
  524. permissionName := "宏观" // 所有客群都推
  525. title := video.Title
  526. jumpUrl := fmt.Sprint(utils.WxYbAppId+`/pages/video/videoList?videoId=`, videoId)
  527. logoUrl := `https://hongze.oss-cn-shanghai.aliyuncs.com/hzyj.png`
  528. // 推送至同花顺
  529. dataType := "2"
  530. err = SendThs(title, permissionName, title, jumpUrl, logoUrl, dataType)
  531. return
  532. }
  533. // SendYbRoadVideoToThs 推送研报路演视频客群消息
  534. func SendYbRoadVideoToThs(videoId int) (err error) {
  535. defer func() {
  536. if err != nil {
  537. go alarm_msg.SendAlarmMsg("SendYbRoadVideoToThs-延时任务发送报告至同花顺失败, RoadVideoId:"+strconv.Itoa(videoId)+", ErrMsg:"+err.Error(), 3)
  538. }
  539. }()
  540. video, e := yb.GetRoadVideoById(videoId)
  541. if e != nil {
  542. err = errors.New("获取视频信息失败, Err:" + e.Error())
  543. return
  544. }
  545. chartPermissionIds := video.ChartPermissionIds
  546. // 标签信息
  547. if chartPermissionIds == "" {
  548. return
  549. }
  550. chartPermissionIdSlice := strings.Split(chartPermissionIds, ",")
  551. chartList, e := models.GetChartPermissionByIds(chartPermissionIdSlice)
  552. if e != nil {
  553. err = errors.New("获取品种信息失败, Err:" + e.Error())
  554. return
  555. }
  556. permissionName := ""
  557. for _, v := range chartList {
  558. if v.PermissionName == "宏观经济" {
  559. v.PermissionName = "宏观"
  560. }
  561. permissionName += v.PermissionName + ","
  562. }
  563. if permissionName == "" {
  564. err = errors.New("客群标签为空, 不可推送")
  565. return
  566. }
  567. permissionName = strings.Trim(permissionName, ",")
  568. //permissionName := "宏观" //写死宏观,默认所有群都推
  569. title := video.Title
  570. jumpUrl := fmt.Sprint(utils.WxYbAppId+`/pages/roadShow/video/list?videoId=`, videoId)
  571. logoUrl := `https://hongze.oss-cn-shanghai.aliyuncs.com/hzyj.png`
  572. // 推送至同花顺
  573. dataType := "2"
  574. err = SendThs(title, permissionName, title, jumpUrl, logoUrl, dataType)
  575. return
  576. }
  577. // SendYbVoiceBroadcastToThs 推送研报小程序语音播报客群消息
  578. func SendYbVoiceBroadcastToThs(voiceId int) (err error) {
  579. defer func() {
  580. if err != nil {
  581. go alarm_msg.SendAlarmMsg("SendYbCommunityVideoToThs-延时任务发送报告至同花顺失败, voiceBroadcastId:"+strconv.Itoa(voiceId)+", ErrMsg:"+err.Error(), 3)
  582. }
  583. }()
  584. voice, e := yb.GetBroadcastById(voiceId)
  585. if e != nil {
  586. err = errors.New("获取语音播报信息失败, Err:" + e.Error())
  587. return
  588. }
  589. permissionName := "宏观" // 所有客群都推
  590. title := voice.BroadcastName
  591. jumpUrl := fmt.Sprint(utils.WxYbAppId+`/pages-voice/voiceDetail?voiceId=`, voiceId)
  592. logoUrl := `https://hongze.oss-cn-shanghai.aliyuncs.com/hzyj.png`
  593. // 推送至同花顺
  594. dataType := "2"
  595. err = SendThs(title, permissionName, title, jumpUrl, logoUrl, dataType)
  596. return
  597. }
  598. //TshListResult 同花顺返回信息
  599. type TshListResult struct {
  600. Error int64 `json:"error"`
  601. Message string `json:"message"`
  602. Rows TshListRowsResult `json:"rows"`
  603. }
  604. // TshListRowsResult 同花顺列表数据
  605. type TshListRowsResult struct {
  606. BeginPageIndex int64 `json:"beginPageIndex"`
  607. CurrentPage int64 `json:"currentPage"`
  608. EndPageIndex int64 `json:"endPageIndex"`
  609. HasNext bool `json:"hasNext"`
  610. HasPre bool `json:"hasPre"`
  611. NextPage int64 `json:"nextPage"`
  612. NumPerPage int64 `json:"numPerPage"`
  613. PageCount int64 `json:"pageCount"`
  614. PrePage int64 `json:"prePage"`
  615. RecordList []struct {
  616. Ctime int64 `json:"ctime" description:"创建时间"`
  617. DataType int64 `json:"dataType" description:"数据类型;1-文章,2-小程序"`
  618. Description string `json:"description" description:"描述"`
  619. Icon string `json:"icon" description:"图标"`
  620. ID int64 `json:"id" description:""`
  621. Label string `json:"label" description:"标签"`
  622. OrgCode string `json:"orgCode" description:""`
  623. PushTime string `json:"pushTime" description:""`
  624. Pushed int64 `json:"pushed" description:"已推微信群数量"`
  625. QueryRowCount int64 `json:"queryRowCount" description:""`
  626. Title string `json:"title" description:"标题"`
  627. Total int64 `json:"total" description:"推送微信群总数量"`
  628. URL string `json:"url" description:"跳转地址"`
  629. } `json:"recordList"`
  630. TotalCount int64 `json:"totalCount"`
  631. }
  632. // CheckThsReportList 定时检测同花顺客群推送状态信息
  633. func CheckThsReportList(cont context.Context) (err error) {
  634. errMsgList := make([]string, 0)
  635. defer func() {
  636. if len(errMsgList) > 0 {
  637. emailStr := `317699326@qq.com;984198890@qq.com;pdzhao@hzinsights.com`
  638. go alarm_msg.SendAlarmMsgWithEmail("定时检测同花顺客群推送状态:<br/>"+strings.Join(errMsgList, "<br/>"), emailStr, 3)
  639. //fmt.Println("定时检测同花顺客群推送状态:\n"+strings.Join(errMsgList, "\n"), 3)
  640. }
  641. }()
  642. tshListRowsResult, err := thsReportList(`50`)
  643. if err != nil {
  644. return
  645. }
  646. for _, v := range tshListRowsResult.RecordList {
  647. if v.Pushed == 0 { //一个群都没有推送的情况
  648. tmpTime := time.Unix(v.Ctime/1000, 0) //入库时间
  649. //超时五分钟还未推送,那么就要去通知了
  650. if time.Now().Sub(tmpTime).Minutes() > 5 {
  651. errMsg := fmt.Sprintf("报告名称:《%s》超时未推送客群,接口通知同花顺时间:%s;报告跳转地址:%s", v.Title, tmpTime.Format(utils.FormatDateTime), v.URL)
  652. errMsgList = append(errMsgList, errMsg)
  653. }
  654. }
  655. }
  656. return
  657. }
  658. // thsReportList 同花顺列表接口
  659. func thsReportList(numPerPageStr string) (tshListRowsResult TshListRowsResult, err error) {
  660. defer func() {
  661. if err != nil {
  662. //fmt.Println(utils.APPNAME+"【"+utils.RunMode+"】"+"失败提醒", "发送消息至同花顺失败 ErrMsg:"+err.Error(), utils.EmailSendToUsers)
  663. //go utils.SendEmail(utils.APPNAME+"【"+utils.RunMode+"】"+"失败提醒", "发送消息至同花顺失败 ErrMsg:"+err.Error(), utils.EmailSendToUsers)
  664. go alarm_msg.SendAlarmMsg("定时获取同花顺列表数据接口失败 ErrMsg:"+err.Error(), 3)
  665. }
  666. }()
  667. sendUrl := utils.THS_List_Url
  668. //sendUrl := `https://board.10jqka.com.cn/gateway/ps/syncNews/listPage`
  669. pageNumStr := `1` // 页码;默认1
  670. utils.FileLog.Info(fmt.Sprintf("pageNum:%s", pageNumStr))
  671. if err != nil {
  672. return
  673. }
  674. //numPerPageStr := "10" // 每页数量;默认10 否
  675. utils.FileLog.Info(fmt.Sprintf("numPerPage:%s", numPerPageStr))
  676. if err != nil {
  677. return
  678. }
  679. //pageNum int 页码;默认1 否
  680. //numPerPage int 每页数量;默认10 否
  681. //title String 标题 否
  682. //startDate String 开始日期;如2022-08-12 否
  683. //endDate String 结束日期;如2022-08-12 否
  684. //开始发送
  685. client := http.Client{}
  686. form := url.Values{}
  687. form.Add("pageNum", pageNumStr)
  688. form.Add("numPerPage", numPerPageStr)
  689. //utils.FileLog.Info(fmt.Sprintf("Ths List parms:%s", form.Encode()))
  690. resp, err := client.PostForm(sendUrl, form)
  691. if err != nil {
  692. return
  693. }
  694. defer resp.Body.Close()
  695. body, _ := ioutil.ReadAll(resp.Body)
  696. //fmt.Println(string(body))
  697. //utils.FileLog.Info(fmt.Sprintf("Ths List Result :%s", string(body)))
  698. //同花顺接口返回数据
  699. var tshResult TshListResult
  700. err = json.Unmarshal(body, &tshResult)
  701. if err != nil {
  702. err = errors.New(fmt.Sprint("同花顺接口返回数据转换成结构体异常,Err:", err))
  703. return
  704. }
  705. if tshResult.Error != 1 {
  706. err = errors.New(fmt.Sprint("发送数据到同花顺接口异常,result:", string(body)))
  707. return
  708. }
  709. tshListRowsResult = tshResult.Rows
  710. return
  711. }