package services import ( "errors" "fmt" "github.com/PuerkitoBio/goquery" "golang.org/x/net/context" "hongze/hongze_task/models" "hongze/hongze_task/services/alarm_msg" "hongze/hongze_task/utils" "html" "os" "strconv" "strings" "time" ) func FixReportContentSub() { items, err := models.GetReport() if err != nil { fmt.Println("err:", err.Error()) return } for k, v := range items { fmt.Println(k, v.Id, v.ContentSub) content := html.UnescapeString(v.Content) utils.FileLog.Info("%s", content) doc, err := goquery.NewDocumentFromReader(strings.NewReader(content)) if err != nil { fmt.Println("create doc err:", err.Error()) return } n := 0 contentSub := "" doc.Find("p").Each(func(i int, s *goquery.Selection) { if n >= 5 { return } n++ phtml, err := s.Html() if err != nil { fmt.Println("get html err", err.Error()) return } if s.Text() != "" || strings.Contains(phtml, "src") { contentSub = contentSub + "
" + phtml + "
" utils.FileLog.Info("i:%d n:%d %s", i, n, contentSub) } }) err = models.ModifyReportContentSub(v.Id, contentSub) if err != nil { fmt.Println("ModifyReportContentSub Err:" + err.Error()) } } } func FixReportCount() { var err error defer func() { if err != nil { fmt.Println("err:", err.Error()) } }() list, err := models.GetReportLimit() if err != nil { return } for _, v := range list { content := v.Content contentHtml := html.UnescapeString(content) doc, err := goquery.NewDocumentFromReader(strings.NewReader(contentHtml)) if err != nil { fmt.Println("line 70:" + err.Error()) return } isModify := false doc.Find("img").Each(func(i int, img *goquery.Selection) { imgContent, srcExist := img.Attr("src") if srcExist { if strings.Contains(imgContent, "data:image/png;base64,") { contentBase64 := imgContent contentBase64 = strings.Replace(contentBase64, "data:image/png;base64,", "", -1) dateDir := time.Now().Format("20060102") uploadDir := "./" + dateDir err = os.MkdirAll(uploadDir, 777) if err != nil { return } randStr := utils.GetRandStringNoSpecialChar(28) fileName := randStr + ".png" fpath := uploadDir + "/" + fileName utils.SaveBase64ToFile(contentBase64, fpath) if err != nil { return } //上传到阿里云 resourceUrl, err := UploadAliyun(fileName, fpath) if err != nil { return } img.SetAttr("src", resourceUrl) isModify = true } } }) contentHtml, _ = doc.Html() fmt.Println(contentHtml) fmt.Println("isModify", isModify) err = models.EditReportContentHtml(v.Id, contentHtml) } } func DeleteReportSaveLog() (err error) { fmt.Println("start") models.DeleteReportSaveLog() fmt.Println("end") return } // UpdateReportEs 更新报告/章节Es func UpdateReportEs(reportId int, publishState int) (err error) { if reportId <= 0 { return } reportInfo, err := models.GetReportByReportId(reportId) if err != nil { return } categories := "" if reportInfo.HasChapter == 1 { // 晨周报 chapterList, tmpErr := models.GetPublishedChapterListByReportId(reportInfo.Id) if tmpErr != nil { return } if len(chapterList) > 0 { for i := 0; i < len(chapterList); i++ { // 章节对应的品种 permissionList, tmpErr := models.GetChapterTypePermissionByTypeIdAndResearchType(chapterList[i].TypeId, chapterList[i].ReportType) if tmpErr != nil { return } categoryArr := make([]string, 0) if len(permissionList) > 0 { for ii := 0; ii < len(permissionList); ii++ { categoryArr = append(categoryArr, permissionList[ii].PermissionName) } } aliasArr, _ := addCategoryAliasToArr(categoryArr) chapterCategories := strings.Join(aliasArr, ",") esChapter := &models.ElasticReportDetail{ ReportId: chapterList[i].ReportId, ReportChapterId: chapterList[i].ReportChapterId, Title: chapterList[i].Title, Abstract: chapterList[i].Abstract, BodyContent: utils.TrimHtml(html.UnescapeString(chapterList[i].Content)), PublishTime: chapterList[i].PublishTime.Format(utils.FormatDateTime), PublishState: chapterList[i].PublishState, Author: chapterList[i].Author, ClassifyIdFirst: chapterList[i].ClassifyIdFirst, ClassifyNameFirst: chapterList[i].ClassifyNameFirst, ClassifyIdSecond: 0, ClassifyNameSecond: "", Categories: chapterCategories, StageStr: strconv.Itoa(chapterList[i].Stage), } chapterDocId := fmt.Sprintf("%d-%d", reportInfo.Id, chapterList[i].ReportChapterId) if err = EsAddOrEditReport(utils.EsReportIndexName, chapterDocId, esChapter); err != nil { return } } } } else { permissionList, tmpErr := models.GetChartPermissionNameFromMappingByKeyword(reportInfo.ClassifyNameSecond, "rddp") if tmpErr != nil { return } categoryArr := make([]string, 0) for i := 0; i < len(permissionList); i++ { categoryArr = append(categoryArr, permissionList[i].PermissionName) } aliasArr, _ := addCategoryAliasToArr(categoryArr) categories = strings.Join(aliasArr, ",") } // 新增报告ES esReport := &models.ElasticReportDetail{ ReportId: reportInfo.Id, ReportChapterId: 0, Title: reportInfo.Title, Abstract: reportInfo.Abstract, BodyContent: utils.TrimHtml(html.UnescapeString(reportInfo.Content)), PublishTime: reportInfo.PublishTime.Format(utils.FormatDateTime), PublishState: publishState, Author: reportInfo.Author, ClassifyIdFirst: reportInfo.ClassifyIdFirst, ClassifyNameFirst: reportInfo.ClassifyNameFirst, ClassifyIdSecond: reportInfo.ClassifyIdSecond, ClassifyNameSecond: reportInfo.ClassifyNameSecond, Categories: categories, StageStr: strconv.Itoa(reportInfo.Stage), } docId := fmt.Sprintf("%d-%d", reportInfo.Id, 0) if err = EsAddOrEditReport(utils.EsReportIndexName, docId, esReport); err != nil { return } return } // addCategoryAliasToArr 品种别名 func addCategoryAliasToArr(categoryArr []string) (aliasArr []string, err error) { aliasArr = categoryArr if len(categoryArr) > 0 { for i := 0; i < len(categoryArr); i++ { if strings.Contains(categoryArr[i], "沥青") { aliasArr = append(aliasArr, "BU") } if strings.Contains(categoryArr[i], "MEG") { aliasArr = append(aliasArr, "EG", "乙二醇") } if strings.Contains(categoryArr[i], "聚酯") { aliasArr = append(aliasArr, "长丝", "短纤", "瓶片") } if strings.Contains(categoryArr[i], "纯苯+苯乙烯") { aliasArr = append(aliasArr, "EB") } if strings.Contains(categoryArr[i], "聚乙烯") { aliasArr = append(aliasArr, "PP", "PE") } if strings.Contains(categoryArr[i], "玻璃纯碱") { aliasArr = append(aliasArr, "玻璃", "纯碱", "FG", "SA") } if strings.Contains(categoryArr[i], "甲醇") { aliasArr = append(aliasArr, "甲醇", "MA") } if strings.Contains(categoryArr[i], "橡胶") { aliasArr = append(aliasArr, "橡胶", "RU") } } } return } // PublishReport 定时发布研报-每秒 func PublishReport(cont context.Context) (err error) { defer func() { if err != nil { go alarm_msg.SendAlarmMsg("PublishReport-定时发布研报失败, ErrMsg:\n"+err.Error(), 3) } }() now := time.Now().Format(utils.FormatDateTimeMinute) startTime := now + ":00" endTime := now + ":59" afterDate := time.Now().AddDate(0, -1, 0).Format(utils.FormatDate) //限制一下,只查询最近一个月的 list, err := models.GetPrePublishedReports(startTime, endTime, afterDate) if err != nil { return } listLen := len(list) if listLen == 0 { return } // 比对时间(分钟),时间相等则发布并推送 for i := 0; i < listLen; i++ { item := list[i] var publishTime time.Time if item.MsgIsSend == 1 && !item.PublishTime.IsZero() { //如果报告曾经发布过,并且已经发送过模版消息,则章节的发布时间为报告的发布时间 publishTime = item.PublishTime } else { publishTime = time.Now() } if item.HasChapter == 1 && (item.ChapterType == utils.REPORT_TYPE_DAY || item.ChapterType == utils.REPORT_TYPE_WEEK) { continue } if err = models.PublishReportById(item.Id, publishTime); err != nil { return } go func() { // 生成音频 if item.VideoUrl == "" { _ = CreateVideo(item) } //// 推送找钢网 //if utils.RunMode == "release" && (report.ClassifyNameSecond == "知白守黑日评" || report.ClassifyNameSecond == "股债日评") { // _ = services.ZhaoGangSend(report) //} // 更新报告Es _ = UpdateReportEs(item.Id, 2) // 判断是否未发送模版消息,并且配置了立即推送模版消息的报告需要推送 if item.MsgIsSend == 0 && item.PreMsgSend == 1 { _ = ReportSendTemplateMsg(item.Id) } }() } return } // PublishReportTest 定时发布研报-每秒 /*func PublishReportTest() (err error) { defer func() { if err != nil { fmt.Println(err.Error()) } }() item, err := models.GetReportById(3331) if err != nil { return } // 判断是否未发送模版消息,并且配置了立即推送模版消息的报告需要推送 if item.MsgIsSend == 0 && item.PreMsgSend == 1 { err = ReportSendTemplateMsg(item.Id) if err != nil { return } } return }*/ func ReportSendTemplateMsg(reportId int) (err error) { defer func() { if err != nil { msg := fmt.Sprintf("ReportSendTemplateMsg, 发送报告模版消息失败, ReportId:%s, Err:%s", reportId, err.Error()) utils.FileLog.Error(msg) go alarm_msg.SendAlarmMsg(msg, 3) } }() report, err := models.GetReportById(reportId) if err != nil { err = errors.New("查询报告失败 Err:" + err.Error()) return } if report.MsgIsSend == 1 { err = errors.New("模板消息已推送,请勿重复操作") return } videoNameDate := `(` + time.Now().Format("0102") + `)` err = models.UpdateReportPublishTime(reportId, videoNameDate) if err != nil { err = errors.New("修改发布时间失败,Err:" + err.Error()) return } if report.HasChapter > 0 { err = models.UpdateReportChapterPublishTime(reportId, videoNameDate) if err != nil { err = errors.New("修改发布时间失败,Err:" + err.Error()) return } } err = sendMiniProgramReportWxMsg(report) if err != nil { err = errors.New("发送失败,Err:" + err.Error()) return } err = models.ModifyReportMsgIsSend(reportId) if err != nil { err = errors.New("发送失败,Err:" + err.Error()) return } return } // sendMiniProgramReportWxMsg 推送报告微信模板消息-小程序链接 func sendMiniProgramReportWxMsg(report *models.ReportDetail) (err error) { reportId := report.Id var msg string reportIdStr := strconv.Itoa(reportId) defer func() { if err != nil { fmt.Println("msg:", msg) utils.FileLog.Error(fmt.Sprintf("SendMiniProgramReportWxMsg, 发送报告模版消息失败, ReportId:%s, Err:%s", reportIdStr, err.Error())) go alarm_msg.SendAlarmMsg("SendMiniProgramReportWxMsg发送报告模版消息失败;"+"ReportId:"+reportIdStr+",Err:"+err.Error()+";msg:"+msg, 3) //go utils.SendEmail("SendMiniProgramReportWxMsg发送报告模版消息失败"+"【"+utils.APPNAME+"】"+"【"+utils.RunMode+"】"+time.Now().Format("2006-01-02 15:04:05"), "ReportId:"+reportIdStr+";"+msg+";Err:"+err.Error(), toUser) } }() utils.FileLog.Info("%s", "services SendMsg") if report == nil { utils.FileLog.Info("报告信息不存在") return } var openIdArr []string if report.ClassifyIdSecond <= 0 { openIdArr, err = models.GetOpenIdArr() if err != nil { msg = "get GetOpenIdArr err:" + err.Error() return } } else { classify, err := models.GetClassifyById(report.ClassifyIdSecond) if err != nil { msg = "获取报告分类失败 err:" + err.Error() return err } if classify.IsMassSend == 1 { openIdArr, err = models.GetOpenIdArr() if err != nil { msg = "get GetOpenIdArr err:" + err.Error() return err } } else { openIdArr, err = models.GetOpenIdArrByClassifyNameSecond(report.ClassifyNameSecond) if err != nil { msg = "GetOpenIdArrByClassifyNameSecond err:" + err.Error() return err } } } title := fmt.Sprintf("弘则%s", report.ClassifyNameFirst) if CheckTwoWeekOrMonthReport(report.ClassifyIdFirst, report.ClassifyNameFirst) { title = fmt.Sprintf("弘则%s", report.ClassifyNameSecond) } //redirectUrl := utils.TemplateRedirectUrl + strconv.Itoa(reportId) first := fmt.Sprintf("Hi,最新一期%s已上线,欢迎查看", report.ClassifyNameFirst) keyword1 := title keyword2 := report.Title keyword3 := report.PublishTime keyword4 := report.Abstract var wxAppPath string if report.ChapterType == utils.REPORT_TYPE_WEEK { wxAppPath = fmt.Sprintf("pages-report/chapterList?reportId=%s", reportIdStr) } else { wxAppPath = fmt.Sprintf("pages-report/reportDetail?reportId=%s", reportIdStr) } sendInfo := new(SendWxTemplate) sendInfo.First = first sendInfo.Keyword1 = keyword1 sendInfo.Keyword2 = keyword2 sendInfo.Keyword3 = keyword3 sendInfo.Keyword4 = keyword4 sendInfo.TemplateId = utils.TemplateIdByProduct sendInfo.RedirectUrl = wxAppPath sendInfo.Resource = wxAppPath sendInfo.SendType = utils.TEMPLATE_MSG_REPORT sendInfo.OpenIdArr = openIdArr sendInfo.RedirectTarget = 1 err = SendTemplateMsgV2(sendInfo) return } // CheckTwoWeekOrMonthReport 校验推送报告是否为双周报或者月报 func CheckTwoWeekOrMonthReport(classifyId int, classifyName string) (ok bool) { if utils.RunMode == "debug" { miniStrArr := []string{ "双周报", "月报", } if utils.InArrayByStr(miniStrArr, classifyName) { ok = true } } else { // 此处生产环境用ID主要是担心分类改了名字... IdArr := []int{ 96, 112, } if utils.InArrayByInt(IdArr, classifyId) { ok = true } } return }