report.go 14 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473
  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.FormatDateTimeMinute)
  244. startTime := now + ":00"
  245. endTime := now + ":59"
  246. afterDate := time.Now().AddDate(0, -1, 0).Format(utils.FormatDate) //限制一下,只查询最近一个月的
  247. list, err := models.GetPrePublishedReports(startTime, endTime, afterDate)
  248. if err != nil {
  249. return
  250. }
  251. listLen := len(list)
  252. if listLen == 0 {
  253. return
  254. }
  255. // 比对时间(分钟),时间相等则发布并推送
  256. for i := 0; i < listLen; i++ {
  257. item := list[i]
  258. var publishTime time.Time
  259. if item.MsgIsSend == 1 && !item.PublishTime.IsZero() { //如果报告曾经发布过,并且已经发送过模版消息,则章节的发布时间为报告的发布时间
  260. publishTime = item.PublishTime
  261. } else {
  262. publishTime = time.Now()
  263. }
  264. if item.HasChapter == 1 && (item.ChapterType == utils.REPORT_TYPE_DAY || item.ChapterType == utils.REPORT_TYPE_WEEK) {
  265. continue
  266. }
  267. if err = models.PublishReportById(item.Id, publishTime); err != nil {
  268. return
  269. }
  270. go func() {
  271. // 生成音频
  272. if item.VideoUrl == "" {
  273. _ = CreateVideo(item)
  274. }
  275. //// 推送找钢网
  276. //if utils.RunMode == "release" && (report.ClassifyNameSecond == "知白守黑日评" || report.ClassifyNameSecond == "股债日评") {
  277. // _ = services.ZhaoGangSend(report)
  278. //}
  279. // 更新报告Es
  280. _ = UpdateReportEs(item.Id, 2)
  281. // 判断是否未发送模版消息,并且配置了立即推送模版消息的报告需要推送
  282. if item.MsgIsSend == 0 && item.PreMsgSend == 1 {
  283. _ = ReportSendTemplateMsg(item.Id)
  284. }
  285. }()
  286. }
  287. return
  288. }
  289. // PublishReportTest 定时发布研报-每秒
  290. /*func PublishReportTest() (err error) {
  291. defer func() {
  292. if err != nil {
  293. fmt.Println(err.Error())
  294. }
  295. }()
  296. item, err := models.GetReportById(3331)
  297. if err != nil {
  298. return
  299. }
  300. // 判断是否未发送模版消息,并且配置了立即推送模版消息的报告需要推送
  301. if item.MsgIsSend == 0 && item.PreMsgSend == 1 {
  302. err = ReportSendTemplateMsg(item.Id)
  303. if err != nil {
  304. return
  305. }
  306. }
  307. return
  308. }*/
  309. func ReportSendTemplateMsg(reportId int) (err error) {
  310. defer func() {
  311. if err != nil {
  312. msg := fmt.Sprintf("ReportSendTemplateMsg, 发送报告模版消息失败, ReportId:%s, Err:%s", reportId, err.Error())
  313. utils.FileLog.Error(msg)
  314. go alarm_msg.SendAlarmMsg(msg, 3)
  315. }
  316. }()
  317. report, err := models.GetReportById(reportId)
  318. if err != nil {
  319. err = errors.New("查询报告失败 Err:" + err.Error())
  320. return
  321. }
  322. if report.MsgIsSend == 1 {
  323. err = errors.New("模板消息已推送,请勿重复操作")
  324. return
  325. }
  326. videoNameDate := `(` + time.Now().Format("0102") + `)`
  327. err = models.UpdateReportPublishTime(reportId, videoNameDate)
  328. if err != nil {
  329. err = errors.New("修改发布时间失败,Err:" + err.Error())
  330. return
  331. }
  332. if report.HasChapter > 0 {
  333. err = models.UpdateReportChapterPublishTime(reportId, videoNameDate)
  334. if err != nil {
  335. err = errors.New("修改发布时间失败,Err:" + err.Error())
  336. return
  337. }
  338. }
  339. err = sendMiniProgramReportWxMsg(report)
  340. if err != nil {
  341. err = errors.New("发送失败,Err:" + err.Error())
  342. return
  343. }
  344. err = models.ModifyReportMsgIsSend(reportId)
  345. if err != nil {
  346. err = errors.New("发送失败,Err:" + err.Error())
  347. return
  348. }
  349. return
  350. }
  351. // sendMiniProgramReportWxMsg 推送报告微信模板消息-小程序链接
  352. func sendMiniProgramReportWxMsg(report *models.ReportDetail) (err error) {
  353. reportId := report.Id
  354. var msg string
  355. reportIdStr := strconv.Itoa(reportId)
  356. defer func() {
  357. if err != nil {
  358. fmt.Println("msg:", msg)
  359. utils.FileLog.Error(fmt.Sprintf("SendMiniProgramReportWxMsg, 发送报告模版消息失败, ReportId:%s, Err:%s", reportIdStr, err.Error()))
  360. go alarm_msg.SendAlarmMsg("SendMiniProgramReportWxMsg发送报告模版消息失败;"+"ReportId:"+reportIdStr+",Err:"+err.Error()+";msg:"+msg, 3)
  361. //go utils.SendEmail("SendMiniProgramReportWxMsg发送报告模版消息失败"+"【"+utils.APPNAME+"】"+"【"+utils.RunMode+"】"+time.Now().Format("2006-01-02 15:04:05"), "ReportId:"+reportIdStr+";"+msg+";Err:"+err.Error(), toUser)
  362. }
  363. }()
  364. utils.FileLog.Info("%s", "services SendMsg")
  365. if report == nil {
  366. utils.FileLog.Info("报告信息不存在")
  367. return
  368. }
  369. var openIdArr []string
  370. if report.ClassifyIdSecond <= 0 {
  371. openIdArr, err = models.GetOpenIdArr()
  372. if err != nil {
  373. msg = "get GetOpenIdArr err:" + err.Error()
  374. return
  375. }
  376. } else {
  377. classify, err := models.GetClassifyById(report.ClassifyIdSecond)
  378. if err != nil {
  379. msg = "获取报告分类失败 err:" + err.Error()
  380. return err
  381. }
  382. if classify.IsMassSend == 1 {
  383. openIdArr, err = models.GetOpenIdArr()
  384. if err != nil {
  385. msg = "get GetOpenIdArr err:" + err.Error()
  386. return err
  387. }
  388. } else {
  389. openIdArr, err = models.GetOpenIdArrByClassifyNameSecond(report.ClassifyNameSecond)
  390. if err != nil {
  391. msg = "GetOpenIdArrByClassifyNameSecond err:" + err.Error()
  392. return err
  393. }
  394. }
  395. }
  396. title := fmt.Sprintf("弘则%s", report.ClassifyNameFirst)
  397. if CheckTwoWeekOrMonthReport(report.ClassifyIdFirst, report.ClassifyNameFirst) {
  398. title = fmt.Sprintf("弘则%s", report.ClassifyNameSecond)
  399. }
  400. //redirectUrl := utils.TemplateRedirectUrl + strconv.Itoa(reportId)
  401. first := fmt.Sprintf("Hi,最新一期%s已上线,欢迎查看", report.ClassifyNameFirst)
  402. keyword1 := title
  403. keyword2 := report.Title
  404. keyword3 := report.PublishTime
  405. keyword4 := report.Abstract
  406. var wxAppPath string
  407. if report.ChapterType == utils.REPORT_TYPE_WEEK {
  408. wxAppPath = fmt.Sprintf("pages-report/chapterList?reportId=%s", reportIdStr)
  409. } else {
  410. wxAppPath = fmt.Sprintf("pages-report/reportDetail?reportId=%s", reportIdStr)
  411. }
  412. sendInfo := new(SendWxTemplate)
  413. sendInfo.First = first
  414. sendInfo.Keyword1 = keyword1
  415. sendInfo.Keyword2 = keyword2
  416. sendInfo.Keyword3 = keyword3
  417. sendInfo.Keyword4 = keyword4
  418. sendInfo.TemplateId = utils.TemplateIdByProduct
  419. sendInfo.RedirectUrl = wxAppPath
  420. sendInfo.Resource = wxAppPath
  421. sendInfo.SendType = utils.TEMPLATE_MSG_REPORT
  422. sendInfo.OpenIdArr = openIdArr
  423. sendInfo.RedirectTarget = 1
  424. err = SendTemplateMsgV2(sendInfo)
  425. return
  426. }
  427. // CheckTwoWeekOrMonthReport 校验推送报告是否为双周报或者月报
  428. func CheckTwoWeekOrMonthReport(classifyId int, classifyName string) (ok bool) {
  429. if utils.RunMode == "debug" {
  430. miniStrArr := []string{
  431. "双周报", "月报",
  432. }
  433. if utils.InArrayByStr(miniStrArr, classifyName) {
  434. ok = true
  435. }
  436. } else {
  437. // 此处生产环境用ID主要是担心分类改了名字...
  438. IdArr := []int{
  439. 96, 112,
  440. }
  441. if utils.InArrayByInt(IdArr, classifyId) {
  442. ok = true
  443. }
  444. }
  445. return
  446. }