article.go 11 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342
  1. package cygx
  2. import (
  3. "errors"
  4. "fmt"
  5. "github.com/PuerkitoBio/goquery"
  6. "hongze/hz_crm_api/models/company"
  7. "hongze/hz_crm_api/models/cygx"
  8. "hongze/hz_crm_api/services"
  9. "hongze/hz_crm_api/services/alarm_msg"
  10. "hongze/hz_crm_api/utils"
  11. "html"
  12. "strconv"
  13. "strings"
  14. )
  15. func ArticlePublishDate(publishDate string) (newPublishDate string) {
  16. newPublishDate = strings.Replace(publishDate, "00:00:00", "", -1)
  17. newPublishDate = strings.Replace(newPublishDate, " ", "", -1)
  18. return
  19. }
  20. func GetReportContentTextSubNew(content string) (contentSub string, err error) {
  21. content = html.UnescapeString(content)
  22. doc, errdoc := goquery.NewDocumentFromReader(strings.NewReader(content))
  23. if errdoc != nil {
  24. err = errdoc
  25. return
  26. }
  27. docText := doc.Text()
  28. bodyRune := []rune(docText)
  29. bodyRuneLen := len(bodyRune)
  30. body := string(bodyRune[:bodyRuneLen])
  31. contentSub = body
  32. contentSub = strings.Replace(contentSub, "Powered by Froala Editor", "", -1)
  33. contentSub = strings.Replace(contentSub, " ", "", -1)
  34. contentSub = strings.Replace(contentSub, "<p data-f-id=\"pbf\" style=\"text-align: center; font-size: 14px; margin-top: 30px; opacity: 0.65; font-family: sanered by <a href=\"https://www.froala.com/wysiwyg-editor?pb=1\" title=\"Froala Editor\">Froala Editor</a></p>", "", -1)
  35. return
  36. }
  37. func ArticleComPanyLabelToStr(companyLabel []string) (label string) {
  38. for _, v := range companyLabel {
  39. label += v + "{|}"
  40. }
  41. label = strings.TrimRight(label, "{|}")
  42. return
  43. }
  44. // GetArticleStockMap 获取个股标签所对应的文章ID
  45. func GetArticleStockMap() (mapResp map[string]int, err error) {
  46. defer func() {
  47. if err != nil {
  48. go alarm_msg.SendAlarmMsg("获取个股标签所对应的文章ID失败"+err.Error(), 2)
  49. }
  50. }()
  51. list, err := cygx.GetArticleStock()
  52. if err != nil && err.Error() != utils.ErrNoRow() {
  53. return
  54. }
  55. mapResp = make(map[string]int, 0)
  56. if len(list) > 0 {
  57. //一对一精准匹配
  58. for _, v := range list {
  59. sliceSubjects := strings.Split(v.Stock, "/")
  60. if len(sliceSubjects) > 0 {
  61. for _, vSubject := range sliceSubjects {
  62. sliceKuohao := strings.Split(vSubject, "(") //过滤括号
  63. sliceXiahuaxian := strings.Split(sliceKuohao[0], "-") //过滤下划线
  64. subject := sliceXiahuaxian[0]
  65. mapResp[subject] = v.ArticleId
  66. }
  67. }
  68. }
  69. }
  70. return
  71. }
  72. // 处理文章来源类型
  73. func GetArticleSourcePlatform(SourcePlatform string) (sourcePlatformResp int) {
  74. //SourcePlatform string `description:"来源 'MOBILE:移动端小程序','PC:PC端小程序','CELUE:上海策略平台','WEB:查研观向网页版'"`
  75. if SourcePlatform == "MOBILE" || SourcePlatform == "PC" {
  76. sourcePlatformResp = 1
  77. } else if SourcePlatform == "WEB" {
  78. sourcePlatformResp = 2
  79. } else if SourcePlatform == "CELUE" {
  80. sourcePlatformResp = 3
  81. } else if SourcePlatform == "1" {
  82. sourcePlatformResp = 1
  83. } else if SourcePlatform == "2" {
  84. sourcePlatformResp = 2
  85. }
  86. return
  87. }
  88. //func init() {
  89. // GetArticleSubjectLabelByArticleId([]int{9551, 9082})
  90. //}
  91. // 根据文章ID获取文章关联的产业名称
  92. func GetArticleIndustrialLabelByArticleId(articleIds []int) (respMap map[int][]string) {
  93. var err error
  94. defer func() {
  95. if err != nil {
  96. go alarm_msg.SendAlarmMsg(fmt.Sprint("根据文章ID获取文章关联的产业名称失败 GetArticleIndustrialLabelByArticleId rticleIds: ", articleIds, err.Error()), 2)
  97. }
  98. }()
  99. list, e := cygx.GetIndustrialArticleGroupListByarticleIdsArr(articleIds)
  100. if e != nil && e.Error() != utils.ErrNoRow() {
  101. err = errors.New("GetIndustrialArticleGroupListByarticleIdsArr, Err: " + e.Error())
  102. return
  103. }
  104. respMap = make(map[int][]string, 0)
  105. for _, v := range list {
  106. respMap[v.ArticleId] = append(respMap[v.ArticleId], v.IndustryName)
  107. }
  108. return
  109. }
  110. // 根据文章ID获取文章关联的标的名称
  111. func GetArticleSubjectLabelByArticleId(articleIds []int) (respMap map[int][]string) {
  112. var err error
  113. defer func() {
  114. if err != nil {
  115. go alarm_msg.SendAlarmMsg(fmt.Sprint("根据文章ID获取文章关联的产业名称 失败 GetArticleSubjectLabelByArticleId rticleIds: ", articleIds, err.Error()), 2)
  116. }
  117. }()
  118. list, e := cygx.GetSubjectArticleGroupListByarticleIdsArr(articleIds)
  119. if e != nil && e.Error() != utils.ErrNoRow() {
  120. err = errors.New("GetIndustrialArticleGroupListByarticleIdsArr, Err: " + e.Error())
  121. return
  122. }
  123. respMap = make(map[int][]string, 0)
  124. for _, v := range list {
  125. respMap[v.ArticleId] = append(respMap[v.ArticleId], v.SubjectName)
  126. }
  127. return
  128. }
  129. // 处理文章、研选专栏的查询信息回显
  130. func HandleArticleAndYanxuanRecordList(items []*cygx.CygxArticleAndYanxuanRecordResp) (itemsResp []*cygx.UserInteraction, err error) {
  131. itemsResp = make([]*cygx.UserInteraction, 0)
  132. if len(items) > 0 {
  133. var articleIds []int // 文章ID
  134. var yanxuanSpecialIds []int // 研选专栏ID
  135. for _, v := range items {
  136. if v.Source == utils.CYGX_OBJ_ARTICLE {
  137. articleIds = append(articleIds, v.SourceId)
  138. }
  139. if v.Source == utils.CYGX_OBJ_YANXUANSPECIAL {
  140. yanxuanSpecialIds = append(yanxuanSpecialIds, v.SourceId)
  141. }
  142. }
  143. mapIndustrialLabel := GetArticleIndustrialLabelByArticleId(articleIds) // 关联产业
  144. mapSubjectLabel := GetArticleSubjectLabelByArticleId(articleIds) // 关联标的
  145. var condition string
  146. var pars []interface{}
  147. //获取文章map
  148. mapArticle := make(map[int]*cygx.CygxReportArticle)
  149. lenarticleIds := len(articleIds)
  150. if lenarticleIds > 0 {
  151. condition = " AND art.article_id IN (" + utils.GetOrmInReplace(lenarticleIds) + ")"
  152. pars = append(pars, articleIds)
  153. list, e := cygx.GetReportArticleList(condition, pars, 0, lenarticleIds, 1)
  154. if e != nil && e.Error() != utils.ErrNoRow() {
  155. err = e
  156. return
  157. }
  158. if len(list) > 0 {
  159. for _, v := range list {
  160. mapArticle[v.ArticleId] = v
  161. }
  162. }
  163. }
  164. //获取研选专栏map
  165. mapYanxuanSpecial := make(map[int]*cygx.CygxYanxuanSpeciaResplItem)
  166. lenyanxuanSpecialIds := len(yanxuanSpecialIds)
  167. if lenyanxuanSpecialIds > 0 {
  168. pars = make([]interface{}, 0)
  169. condition = " AND a.id IN (" + utils.GetOrmInReplace(lenyanxuanSpecialIds) + ")"
  170. pars = append(pars, yanxuanSpecialIds)
  171. list, e := cygx.GetYanxuanSpecialListByCondition(condition, pars, 0, lenyanxuanSpecialIds)
  172. if e != nil && e.Error() != utils.ErrNoRow() {
  173. err = e
  174. return
  175. }
  176. if len(list) > 0 {
  177. for _, v := range list {
  178. mapYanxuanSpecial[v.Id] = v
  179. }
  180. }
  181. }
  182. for _, v := range items {
  183. item := new(cygx.UserInteraction)
  184. if v.Source == utils.CYGX_OBJ_ARTICLE {
  185. item.IndustryName = strings.Join(mapIndustrialLabel[v.SourceId], ",")
  186. item.SubjectNameStr = strings.Join(mapSubjectLabel[v.SourceId], ",")
  187. if v.SourceId >= utils.SummaryArticleId {
  188. item.ArticleType = 1
  189. } else {
  190. item.ArticleType = 2
  191. }
  192. //如果对应的map不为空,就赋值
  193. if mapArticle[v.SourceId] != nil {
  194. item.Title = mapArticle[v.SourceId].Title
  195. item.PublishDate = mapArticle[v.SourceId].PublishDate
  196. item.PermissionName = mapArticle[v.SourceId].PermissionName
  197. item.ArticleIdMd5 = mapArticle[v.SourceId].ArticleIdMd5
  198. }
  199. }
  200. if v.Source == utils.CYGX_OBJ_YANXUANSPECIAL {
  201. item.PermissionName = utils.CHART_PERMISSION_NAME_MF_YANXUAN
  202. //如果对应的map不为空,就赋值
  203. if mapYanxuanSpecial[v.SourceId] != nil {
  204. item.Title = mapYanxuanSpecial[v.SourceId].Title
  205. item.PublishDate = mapYanxuanSpecial[v.SourceId].PublishTime
  206. item.SpecialType = mapYanxuanSpecial[v.SourceId].Type
  207. }
  208. }
  209. item.ArticleId = v.SourceId
  210. item.CreateTime = v.CreateTime
  211. item.RegisterPlatform = v.RegisterPlatform
  212. itemsResp = append(itemsResp, item)
  213. }
  214. }
  215. return
  216. }
  217. //
  218. //func init() {
  219. // SendWxMsgWithroadshowEssence(1000546)
  220. //}
  221. // 路演精华的文章做模板消息推送
  222. func SendWxMsgWithroadshowEssence(articleId int) (err error) {
  223. defer func() {
  224. if err != nil {
  225. fmt.Println(err)
  226. go alarm_msg.SendAlarmMsg("路演精华的文章做模板消息推送送,发送模版消息失败,Err:"+err.Error(), 3)
  227. utils.FileLog.Info(fmt.Sprintf("路演精华的文章做模板消息推送失败,Err:%s,%s", err.Error()))
  228. }
  229. }()
  230. detail, e := cygx.GetArticleRoadshowEssenceDetail(articleId)
  231. if e != nil {
  232. err = errors.New("获取详情失败" + e.Error())
  233. return
  234. }
  235. if detail == nil {
  236. err = errors.New("获取详情失败")
  237. return
  238. }
  239. //fmt.Println(detail)
  240. permissionStr, e := company.GetPermissionIdById(strconv.Itoa(detail.ChartPermissionId))
  241. if e != nil {
  242. err = errors.New("获取主客观权限失败" + e.Error())
  243. return
  244. }
  245. // 获取所有有权的用户的 openid
  246. openidPowerList, e := cygx.GetCygxUserRecordPower(permissionStr)
  247. if e != nil {
  248. err = errors.New("获取所有有权的用户的 openid失败" + e.Error())
  249. return
  250. }
  251. //获取关注对应产业的用户信息
  252. industryFllowList, e := cygx.GetCygxUserFllowOpenid(articleId)
  253. if e != nil {
  254. err = errors.New("获取关注对应产业的用户信息失败 " + e.Error())
  255. return
  256. }
  257. mapOpenidFllow := make(map[int]string)
  258. for _, v := range industryFllowList {
  259. mapOpenidFllow[v.UserId] = v.OpenId
  260. }
  261. //获取拒绝接收推送的的用户的 openid
  262. mapOpenidRefuset := make(map[int]string)
  263. openidRefusetList, e := cygx.GetCygxUserRefusetOpenid()
  264. if e != nil && e.Error() != utils.ErrNoRow() {
  265. err = errors.New("获取拒绝接收推送的的用户的 openid 失败" + e.Error())
  266. return
  267. }
  268. for _, v := range openidRefusetList {
  269. mapOpenidRefuset[v.UserId] = v.OpenId
  270. }
  271. //获取提交过推送规则的用户的 openid
  272. mapUserIdChooseSend := make(map[int]int)
  273. chooseSendtList, err := cygx.GetCygxXzsChooseSend("")
  274. if err != nil && err.Error() != utils.ErrNoRow() {
  275. return err
  276. }
  277. for _, v := range chooseSendtList {
  278. mapUserIdChooseSend[v.UserId] = v.UserId
  279. }
  280. openIdArr := make([]string, len(openidPowerList))
  281. for i, v := range openidPowerList {
  282. if _, ok := mapOpenidRefuset[v.UserId]; ok {
  283. continue //如果用户选择了拒绝推送消息,那么就不做模板消息推送
  284. }
  285. if _, ok := mapUserIdChooseSend[v.UserId]; ok {
  286. if _, ok := mapOpenidFllow[v.UserId]; !ok {
  287. continue //如果用户提交过关注信息,而且这个产业他没有关注,那么不做推送
  288. }
  289. }
  290. openIdArr[i] = v.OpenId
  291. }
  292. fmt.Println(openIdArr)
  293. first := "【路演精华】已发布,欢迎查看"
  294. keyword1 := detail.Title
  295. keyword2 := "已发布"
  296. keyword3 := detail.PublishDate
  297. keyword4 := detail.Abstract
  298. remark := "点击查看详情"
  299. redirectUrl := utils.WX_MSG_PATH_ROAD_ESSENCE + strconv.Itoa(detail.ArticleId) + "&IsSendWx=1"
  300. sendInfo := new(services.SendWxTemplate)
  301. sendInfo.First = first
  302. sendInfo.Keyword1 = keyword1
  303. sendInfo.Keyword2 = keyword2
  304. sendInfo.Keyword3 = keyword3
  305. sendInfo.Keyword4 = keyword4
  306. sendInfo.Remark = remark
  307. sendInfo.TemplateId = utils.WxMsgTemplateIdActivityChangeApplyXzs
  308. sendInfo.RedirectUrl = redirectUrl
  309. sendInfo.RedirectTarget = 3
  310. sendInfo.Resource = strconv.Itoa(detail.ArticleId)
  311. sendInfo.SendType = utils.TEMPLATE_MSG_CYGX_ROADSHOW_VIDEO
  312. sendInfo.OpenIdArr = openIdArr
  313. e = services.SendTemplateMsg(sendInfo)
  314. if e != nil {
  315. err = errors.New("推送模板消息失败" + e.Error())
  316. return
  317. }
  318. return
  319. }