report.go 14 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471
  1. package services
  2. import (
  3. "errors"
  4. "fmt"
  5. "github.com/PuerkitoBio/goquery"
  6. "golang.org/x/net/context"
  7. "hongze/hongze_task/models"
  8. "hongze/hongze_task/services/alarm_msg"
  9. "hongze/hongze_task/utils"
  10. "html"
  11. "os"
  12. "strconv"
  13. "strings"
  14. "time"
  15. )
  16. func FixReportContentSub() {
  17. items, err := models.GetReport()
  18. if err != nil {
  19. fmt.Println("err:", err.Error())
  20. return
  21. }
  22. for k, v := range items {
  23. fmt.Println(k, v.Id, v.ContentSub)
  24. content := html.UnescapeString(v.Content)
  25. utils.FileLog.Info("%s", content)
  26. doc, err := goquery.NewDocumentFromReader(strings.NewReader(content))
  27. if err != nil {
  28. fmt.Println("create doc err:", err.Error())
  29. return
  30. }
  31. n := 0
  32. contentSub := ""
  33. doc.Find("p").Each(func(i int, s *goquery.Selection) {
  34. if n >= 5 {
  35. return
  36. }
  37. n++
  38. phtml, err := s.Html()
  39. if err != nil {
  40. fmt.Println("get html err", err.Error())
  41. return
  42. }
  43. if s.Text() != "" || strings.Contains(phtml, "src") {
  44. contentSub = contentSub + "<p>" + phtml + "</p>"
  45. utils.FileLog.Info("i:%d n:%d %s", i, n, contentSub)
  46. }
  47. })
  48. err = models.ModifyReportContentSub(v.Id, contentSub)
  49. if err != nil {
  50. fmt.Println("ModifyReportContentSub Err:" + err.Error())
  51. }
  52. }
  53. }
  54. func FixReportCount() {
  55. var err error
  56. defer func() {
  57. if err != nil {
  58. fmt.Println("err:", err.Error())
  59. }
  60. }()
  61. list, err := models.GetReportLimit()
  62. if err != nil {
  63. return
  64. }
  65. for _, v := range list {
  66. content := v.Content
  67. contentHtml := html.UnescapeString(content)
  68. doc, err := goquery.NewDocumentFromReader(strings.NewReader(contentHtml))
  69. if err != nil {
  70. fmt.Println("line 70:" + err.Error())
  71. return
  72. }
  73. isModify := false
  74. doc.Find("img").Each(func(i int, img *goquery.Selection) {
  75. imgContent, srcExist := img.Attr("src")
  76. if srcExist {
  77. if strings.Contains(imgContent, "data:image/png;base64,") {
  78. contentBase64 := imgContent
  79. contentBase64 = strings.Replace(contentBase64, "data:image/png;base64,", "", -1)
  80. dateDir := time.Now().Format("20060102")
  81. uploadDir := "./" + dateDir
  82. err = os.MkdirAll(uploadDir, 777)
  83. if err != nil {
  84. return
  85. }
  86. randStr := utils.GetRandStringNoSpecialChar(28)
  87. fileName := randStr + ".png"
  88. fpath := uploadDir + "/" + fileName
  89. utils.SaveBase64ToFile(contentBase64, fpath)
  90. if err != nil {
  91. return
  92. }
  93. //上传到阿里云
  94. resourceUrl, err := UploadAliyun(fileName, fpath)
  95. if err != nil {
  96. return
  97. }
  98. img.SetAttr("src", resourceUrl)
  99. isModify = true
  100. }
  101. }
  102. })
  103. contentHtml, _ = doc.Html()
  104. fmt.Println(contentHtml)
  105. fmt.Println("isModify", isModify)
  106. err = models.EditReportContentHtml(v.Id, contentHtml)
  107. }
  108. }
  109. func DeleteReportSaveLog() (err error) {
  110. fmt.Println("start")
  111. models.DeleteReportSaveLog()
  112. fmt.Println("end")
  113. return
  114. }
  115. // UpdateReportEs 更新报告/章节Es
  116. func UpdateReportEs(reportId int, publishState int) (err error) {
  117. if reportId <= 0 {
  118. return
  119. }
  120. reportInfo, err := models.GetReportByReportId(reportId)
  121. if err != nil {
  122. return
  123. }
  124. categories := ""
  125. if reportInfo.HasChapter == 1 {
  126. // 晨周报
  127. chapterList, tmpErr := models.GetPublishedChapterListByReportId(reportInfo.Id)
  128. if tmpErr != nil {
  129. return
  130. }
  131. if len(chapterList) > 0 {
  132. for i := 0; i < len(chapterList); i++ {
  133. // 章节对应的品种
  134. permissionList, tmpErr := models.GetChapterTypePermissionByTypeIdAndResearchType(chapterList[i].TypeId, chapterList[i].ReportType)
  135. if tmpErr != nil {
  136. return
  137. }
  138. categoryArr := make([]string, 0)
  139. if len(permissionList) > 0 {
  140. for ii := 0; ii < len(permissionList); ii++ {
  141. categoryArr = append(categoryArr, permissionList[ii].PermissionName)
  142. }
  143. }
  144. aliasArr, _ := addCategoryAliasToArr(categoryArr)
  145. chapterCategories := strings.Join(aliasArr, ",")
  146. esChapter := &models.ElasticReportDetail{
  147. ReportId: chapterList[i].ReportId,
  148. ReportChapterId: chapterList[i].ReportChapterId,
  149. Title: chapterList[i].Title,
  150. Abstract: chapterList[i].Abstract,
  151. BodyContent: utils.TrimHtml(html.UnescapeString(chapterList[i].Content)),
  152. PublishTime: chapterList[i].PublishTime.Format(utils.FormatDateTime),
  153. PublishState: chapterList[i].PublishState,
  154. Author: chapterList[i].Author,
  155. ClassifyIdFirst: chapterList[i].ClassifyIdFirst,
  156. ClassifyNameFirst: chapterList[i].ClassifyNameFirst,
  157. ClassifyIdSecond: 0,
  158. ClassifyNameSecond: "",
  159. Categories: chapterCategories,
  160. StageStr: strconv.Itoa(chapterList[i].Stage),
  161. }
  162. chapterDocId := fmt.Sprintf("%d-%d", reportInfo.Id, chapterList[i].ReportChapterId)
  163. if err = EsAddOrEditReport(utils.EsReportIndexName, chapterDocId, esChapter); err != nil {
  164. return
  165. }
  166. }
  167. }
  168. } else {
  169. permissionList, tmpErr := models.GetChartPermissionNameFromMappingByKeyword(reportInfo.ClassifyNameSecond, "rddp")
  170. if tmpErr != nil {
  171. return
  172. }
  173. categoryArr := make([]string, 0)
  174. for i := 0; i < len(permissionList); i++ {
  175. categoryArr = append(categoryArr, permissionList[i].PermissionName)
  176. }
  177. aliasArr, _ := addCategoryAliasToArr(categoryArr)
  178. categories = strings.Join(aliasArr, ",")
  179. }
  180. // 新增报告ES
  181. esReport := &models.ElasticReportDetail{
  182. ReportId: reportInfo.Id,
  183. ReportChapterId: 0,
  184. Title: reportInfo.Title,
  185. Abstract: reportInfo.Abstract,
  186. BodyContent: utils.TrimHtml(html.UnescapeString(reportInfo.Content)),
  187. PublishTime: reportInfo.PublishTime.Format(utils.FormatDateTime),
  188. PublishState: publishState,
  189. Author: reportInfo.Author,
  190. ClassifyIdFirst: reportInfo.ClassifyIdFirst,
  191. ClassifyNameFirst: reportInfo.ClassifyNameFirst,
  192. ClassifyIdSecond: reportInfo.ClassifyIdSecond,
  193. ClassifyNameSecond: reportInfo.ClassifyNameSecond,
  194. Categories: categories,
  195. StageStr: strconv.Itoa(reportInfo.Stage),
  196. }
  197. docId := fmt.Sprintf("%d-%d", reportInfo.Id, 0)
  198. if err = EsAddOrEditReport(utils.EsReportIndexName, docId, esReport); err != nil {
  199. return
  200. }
  201. return
  202. }
  203. // addCategoryAliasToArr 品种别名
  204. func addCategoryAliasToArr(categoryArr []string) (aliasArr []string, err error) {
  205. aliasArr = categoryArr
  206. if len(categoryArr) > 0 {
  207. for i := 0; i < len(categoryArr); i++ {
  208. if strings.Contains(categoryArr[i], "沥青") {
  209. aliasArr = append(aliasArr, "BU")
  210. }
  211. if strings.Contains(categoryArr[i], "MEG") {
  212. aliasArr = append(aliasArr, "EG", "乙二醇")
  213. }
  214. if strings.Contains(categoryArr[i], "聚酯") {
  215. aliasArr = append(aliasArr, "长丝", "短纤", "瓶片")
  216. }
  217. if strings.Contains(categoryArr[i], "纯苯+苯乙烯") {
  218. aliasArr = append(aliasArr, "EB")
  219. }
  220. if strings.Contains(categoryArr[i], "聚乙烯") {
  221. aliasArr = append(aliasArr, "PP", "PE")
  222. }
  223. if strings.Contains(categoryArr[i], "玻璃纯碱") {
  224. aliasArr = append(aliasArr, "玻璃", "纯碱", "FG", "SA")
  225. }
  226. if strings.Contains(categoryArr[i], "甲醇") {
  227. aliasArr = append(aliasArr, "甲醇", "MA")
  228. }
  229. if strings.Contains(categoryArr[i], "橡胶") {
  230. aliasArr = append(aliasArr, "橡胶", "RU")
  231. }
  232. }
  233. }
  234. return
  235. }
  236. // PublishReport 定时发布研报-每秒
  237. func PublishReport(cont context.Context) (err error) {
  238. defer func() {
  239. if err != nil {
  240. go alarm_msg.SendAlarmMsg("PublishReport-定时发布研报失败, ErrMsg:\n"+err.Error(), 3)
  241. }
  242. }()
  243. now := time.Now().Format(utils.FormatDateTime)
  244. list, e := models.GetPrePublishedReports(now)
  245. if e != nil {
  246. return
  247. }
  248. listLen := len(list)
  249. if listLen == 0 {
  250. return
  251. }
  252. // 比对时间(分钟),时间相等则发布并推送
  253. for i := 0; i < listLen; i++ {
  254. item := list[i]
  255. var publishTime time.Time
  256. if item.MsgIsSend == 1 && !item.PublishTime.IsZero() { //如果报告曾经发布过,并且已经发送过模版消息,则章节的发布时间为报告的发布时间
  257. publishTime = item.PublishTime
  258. } else {
  259. publishTime = time.Now()
  260. }
  261. if item.HasChapter == 1 && (item.ChapterType == utils.REPORT_TYPE_DAY || item.ChapterType == utils.REPORT_TYPE_WEEK) {
  262. continue
  263. }
  264. if err = models.PublishReportById(item.Id, publishTime); err != nil {
  265. err = e
  266. return
  267. }
  268. go func() {
  269. // 生成音频
  270. if item.VideoUrl == "" {
  271. _ = CreateVideo(item)
  272. }
  273. //// 推送找钢网
  274. //if utils.RunMode == "release" && (report.ClassifyNameSecond == "知白守黑日评" || report.ClassifyNameSecond == "股债日评") {
  275. // _ = services.ZhaoGangSend(report)
  276. //}
  277. // 更新报告Es
  278. _ = UpdateReportEs(item.Id, 2)
  279. // 判断是否未发送模版消息,并且配置了立即推送模版消息的报告需要推送
  280. if item.MsgIsSend == 0 && item.PreMsgSend == 1 {
  281. _ = ReportSendTemplateMsg(item.Id)
  282. }
  283. }()
  284. }
  285. return
  286. }
  287. // PublishReportTest 定时发布研报-每秒
  288. /*func PublishReportTest() (err error) {
  289. defer func() {
  290. if err != nil {
  291. fmt.Println(err.Error())
  292. }
  293. }()
  294. item, err := models.GetReportById(3331)
  295. if err != nil {
  296. return
  297. }
  298. // 判断是否未发送模版消息,并且配置了立即推送模版消息的报告需要推送
  299. if item.MsgIsSend == 0 && item.PreMsgSend == 1 {
  300. err = ReportSendTemplateMsg(item.Id)
  301. if err != nil {
  302. return
  303. }
  304. }
  305. return
  306. }*/
  307. func ReportSendTemplateMsg(reportId int) (err error) {
  308. defer func() {
  309. if err != nil {
  310. msg := fmt.Sprintf("ReportSendTemplateMsg, 发送报告模版消息失败, ReportId:%s, Err:%s", reportId, err.Error())
  311. utils.FileLog.Error(msg)
  312. go alarm_msg.SendAlarmMsg(msg, 3)
  313. }
  314. }()
  315. report, err := models.GetReportById(reportId)
  316. if err != nil {
  317. err = errors.New("查询报告失败 Err:" + err.Error())
  318. return
  319. }
  320. if report.MsgIsSend == 1 {
  321. err = errors.New("模板消息已推送,请勿重复操作")
  322. return
  323. }
  324. videoNameDate := `(` + time.Now().Format("0102") + `)`
  325. err = models.UpdateReportPublishTime(reportId, videoNameDate)
  326. if err != nil {
  327. err = errors.New("修改发布时间失败,Err:" + err.Error())
  328. return
  329. }
  330. if report.HasChapter > 0 {
  331. err = models.UpdateReportChapterPublishTime(reportId, videoNameDate)
  332. if err != nil {
  333. err = errors.New("修改发布时间失败,Err:" + err.Error())
  334. return
  335. }
  336. }
  337. err = sendMiniProgramReportWxMsg(report)
  338. if err != nil {
  339. err = errors.New("发送失败,Err:" + err.Error())
  340. return
  341. }
  342. err = models.ModifyReportMsgIsSend(reportId)
  343. if err != nil {
  344. err = errors.New("发送失败,Err:" + err.Error())
  345. return
  346. }
  347. return
  348. }
  349. // sendMiniProgramReportWxMsg 推送报告微信模板消息-小程序链接
  350. func sendMiniProgramReportWxMsg(report *models.ReportDetail) (err error) {
  351. reportId := report.Id
  352. var msg string
  353. reportIdStr := strconv.Itoa(reportId)
  354. defer func() {
  355. if err != nil {
  356. fmt.Println("msg:", msg)
  357. utils.FileLog.Error(fmt.Sprintf("SendMiniProgramReportWxMsg, 发送报告模版消息失败, ReportId:%s, Err:%s", reportIdStr, err.Error()))
  358. go alarm_msg.SendAlarmMsg("SendMiniProgramReportWxMsg发送报告模版消息失败;"+"ReportId:"+reportIdStr+",Err:"+err.Error()+";msg:"+msg, 3)
  359. //go utils.SendEmail("SendMiniProgramReportWxMsg发送报告模版消息失败"+"【"+utils.APPNAME+"】"+"【"+utils.RunMode+"】"+time.Now().Format("2006-01-02 15:04:05"), "ReportId:"+reportIdStr+";"+msg+";Err:"+err.Error(), toUser)
  360. }
  361. }()
  362. utils.FileLog.Info("%s", "services SendMsg")
  363. if report == nil {
  364. utils.FileLog.Info("报告信息不存在")
  365. return
  366. }
  367. var openIdArr []string
  368. if report.ClassifyIdSecond <= 0 {
  369. openIdArr, err = models.GetOpenIdArr()
  370. if err != nil {
  371. msg = "get GetOpenIdArr err:" + err.Error()
  372. return
  373. }
  374. } else {
  375. classify, err := models.GetClassifyById(report.ClassifyIdSecond)
  376. if err != nil {
  377. msg = "获取报告分类失败 err:" + err.Error()
  378. return err
  379. }
  380. if classify.IsMassSend == 1 {
  381. openIdArr, err = models.GetOpenIdArr()
  382. if err != nil {
  383. msg = "get GetOpenIdArr err:" + err.Error()
  384. return err
  385. }
  386. } else {
  387. openIdArr, err = models.GetOpenIdArrByClassifyNameSecond(report.ClassifyNameSecond)
  388. if err != nil {
  389. msg = "GetOpenIdArrByClassifyNameSecond err:" + err.Error()
  390. return err
  391. }
  392. }
  393. }
  394. title := fmt.Sprintf("弘则%s", report.ClassifyNameFirst)
  395. if CheckTwoWeekOrMonthReport(report.ClassifyIdFirst, report.ClassifyNameFirst) {
  396. title = fmt.Sprintf("弘则%s", report.ClassifyNameSecond)
  397. }
  398. //redirectUrl := utils.TemplateRedirectUrl + strconv.Itoa(reportId)
  399. first := fmt.Sprintf("Hi,最新一期%s已上线,欢迎查看", report.ClassifyNameFirst)
  400. keyword1 := title
  401. keyword2 := report.Title
  402. keyword3 := report.PublishTime
  403. keyword4 := report.Abstract
  404. var wxAppPath string
  405. if report.ChapterType == utils.REPORT_TYPE_WEEK {
  406. wxAppPath = fmt.Sprintf("pages-report/chapterList?reportId=%s", reportIdStr)
  407. } else {
  408. wxAppPath = fmt.Sprintf("pages-report/reportDetail?reportId=%s", reportIdStr)
  409. }
  410. sendInfo := new(SendWxTemplate)
  411. sendInfo.First = first
  412. sendInfo.Keyword1 = keyword1
  413. sendInfo.Keyword2 = keyword2
  414. sendInfo.Keyword3 = keyword3
  415. sendInfo.Keyword4 = keyword4
  416. sendInfo.TemplateId = utils.TemplateIdByProduct
  417. sendInfo.RedirectUrl = wxAppPath
  418. sendInfo.Resource = wxAppPath
  419. sendInfo.SendType = utils.TEMPLATE_MSG_REPORT
  420. sendInfo.OpenIdArr = openIdArr
  421. sendInfo.RedirectTarget = 1
  422. err = SendTemplateMsgV2(sendInfo)
  423. return
  424. }
  425. // CheckTwoWeekOrMonthReport 校验推送报告是否为双周报或者月报
  426. func CheckTwoWeekOrMonthReport(classifyId int, classifyName string) (ok bool) {
  427. if utils.RunMode == "debug" {
  428. miniStrArr := []string{
  429. "双周报", "月报",
  430. }
  431. if utils.InArrayByStr(miniStrArr, classifyName) {
  432. ok = true
  433. }
  434. } else {
  435. // 此处生产环境用ID主要是担心分类改了名字...
  436. IdArr := []int{
  437. 96, 112,
  438. }
  439. if utils.InArrayByInt(IdArr, classifyId) {
  440. ok = true
  441. }
  442. }
  443. return
  444. }