cygx_yanxuan_special.go 15 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508
  1. package services
  2. import (
  3. "context"
  4. "errors"
  5. "fmt"
  6. "hongze/hongze_cygx/models"
  7. "hongze/hongze_cygx/utils"
  8. "strconv"
  9. "strings"
  10. "time"
  11. )
  12. // 研选专栏有新内容审核通过时,给关注此专栏的客户发送模板消息
  13. func SendWxMsgSpecialFollow(specialId int) (err error) {
  14. defer func() {
  15. if err != nil {
  16. go utils.SendAlarmMsg(fmt.Sprint("研选专栏有新内容审核通过时,给关注此专栏的客户发送模板消息失败", specialId, ", specialId", err.Error()), 2)
  17. }
  18. }()
  19. var first string
  20. var keyword1 string
  21. var keyword2 string
  22. var keyword3 string
  23. var keyword4 string
  24. var remark string
  25. followers, e := models.GetYanxuanSpecialFollowUserById(specialId)
  26. if e != nil {
  27. err = errors.New("GetYanxuanSpecialFollowUserById, Err: " + e.Error())
  28. return
  29. }
  30. if len(followers) == 0 {
  31. return
  32. }
  33. specialItem, e := models.GetYanxuanSpecialItemById(specialId)
  34. if e != nil {
  35. err = errors.New("GetYanxuanSpecialFollowUserById, Err: " + e.Error())
  36. return
  37. }
  38. var allInUserId string
  39. for _, v := range followers {
  40. allInUserId += strconv.Itoa(v) + ","
  41. }
  42. allInUserId = strings.TrimRight(allInUserId, ",")
  43. userList, err := models.GetWxUserListByUserIds(allInUserId)
  44. if err != nil && err.Error() != utils.ErrNoRow() {
  45. return err
  46. }
  47. mobile := ``
  48. for _, v := range userList {
  49. mobile += v.Mobile + ","
  50. }
  51. mobile = strings.TrimRight(mobile, ",")
  52. openIdList, e := models.GetWxOpenIdByMobileList(mobile)
  53. if e != nil {
  54. err = errors.New("GetSellerByAdminId, Err: " + e.Error())
  55. return
  56. }
  57. if len(openIdList) == 0 {
  58. return
  59. }
  60. //first =
  61. keyword1 = "研选专栏:" + specialItem.SpecialName
  62. keyword2 = "发布了新内容,点击查看详情"
  63. keyword3 = "-"
  64. //keyword4 = "【" + activityInfo.ResearchTheme + "】已有10人预报名"
  65. openIdArr := make([]string, 0)
  66. for _, v := range openIdList {
  67. openIdArr = append(openIdArr, v.OpenId)
  68. }
  69. redirectUrl := ""
  70. redirectUrl = utils.WX_MSG_PATH_YX_SPECIAL_DETAIL + strconv.Itoa(specialId)
  71. sendInfo := new(SendWxTemplate)
  72. sendInfo.First = first
  73. sendInfo.Keyword1 = keyword1
  74. sendInfo.Keyword2 = keyword2
  75. sendInfo.Keyword3 = keyword3
  76. sendInfo.Keyword4 = keyword4
  77. sendInfo.Remark = remark
  78. sendInfo.TemplateId = utils.WxMsgTemplateIdArticleUserRemind
  79. sendInfo.RedirectUrl = redirectUrl
  80. sendInfo.RedirectTarget = 3
  81. sendInfo.Resource = strconv.Itoa(specialId)
  82. sendInfo.SendType = utils.TEMPLATE_MSG_CYGX_ARTICLE_ADD
  83. sendInfo.OpenIdArr = openIdArr
  84. err = PublicSendTemplateMsg(sendInfo)
  85. if err != nil {
  86. return
  87. }
  88. return
  89. }
  90. // SendReviewTemplateMsgAdmin 提交审核时给王芳,葛琳发消息
  91. func SendReviewTemplateMsgAdmin(specialId int) (err error) {
  92. defer func() {
  93. if err != nil {
  94. go utils.SendAlarmMsg(fmt.Sprint("处理试用申请给王芳,汪洋发消息失败, specialId:", specialId, "ErrMsg", err.Error()), 2)
  95. }
  96. }()
  97. var configCode string
  98. //研选专栏修改之后给这些手机号推送审核模版消息
  99. configCode = utils.TPL_MSG_YAN_XUAN_SPECIAL_APPROVAL
  100. cnf, e := models.GetConfigByCode(configCode)
  101. if e != nil {
  102. err = errors.New("GetConfigByCode, Err: " + e.Error() + configCode)
  103. return
  104. }
  105. openIdList, e := models.GetUserRecordListByMobile(4, cnf.ConfigValue)
  106. if e != nil && e.Error() != utils.ErrNoRow() {
  107. err = errors.New("GetUserRecordListByMobile, Err: " + e.Error() + cnf.ConfigValue)
  108. return err
  109. }
  110. specialItem, e := models.GetYanxuanSpecialItemById(specialId)
  111. if e != nil {
  112. err = errors.New("GetYanxuanSpecialFollowUserById, Err: " + e.Error())
  113. return
  114. }
  115. user, e := models.GetWxUserItemByUserId(specialItem.UserId)
  116. if e != nil {
  117. err = errors.New("GetWxUserItemByUserId, Err: " + e.Error())
  118. return err
  119. }
  120. var keyword1 string
  121. var keyword2 string
  122. var keyword3 string
  123. var keyword4 string
  124. var remark string
  125. keyword1 = specialItem.RealName + "【" + user.CompanyName + "】"
  126. keyword2 = user.Mobile
  127. keyword3 = time.Now().Format(utils.FormatDateTime)
  128. keyword4 = "研选专栏提交了内容待审核"
  129. openIdArr := make([]string, 0)
  130. for _, v := range openIdList {
  131. openIdArr = append(openIdArr, v.OpenId)
  132. }
  133. redirectUrl := ""
  134. redirectUrl = utils.WX_MSG_PATH_YX_SPECIAL_ENABLE_DETAIL + strconv.Itoa(specialId)
  135. sendInfo := new(SendWxTemplate)
  136. sendInfo.Keyword1 = keyword1
  137. sendInfo.Keyword2 = keyword2
  138. sendInfo.Keyword3 = keyword3
  139. sendInfo.Keyword4 = keyword4
  140. sendInfo.Remark = remark
  141. sendInfo.TemplateId = utils.WxMsgTemplateIdAskMsgXzs
  142. sendInfo.RedirectUrl = redirectUrl
  143. sendInfo.RedirectTarget = 3
  144. sendInfo.Resource = strconv.Itoa(specialId)
  145. sendInfo.SendType = utils.TEMPLATE_MSG_CYGX_ARTICLE_ADD
  146. sendInfo.OpenIdArr = openIdArr
  147. err = PublicSendTemplateMsg(sendInfo)
  148. if err != nil {
  149. return
  150. }
  151. return
  152. }
  153. // 研选专栏审核完成时,给提交人发送模板消息
  154. func SendWxMsgSpecialAuthor(specialId, status int) (err error) {
  155. defer func() {
  156. if err != nil {
  157. go utils.SendAlarmMsg(fmt.Sprint("研选专栏审核完成时,给提交人发送模板消息", specialId, ", specialId", err.Error()), 2)
  158. }
  159. }()
  160. var first string
  161. var keyword1 string
  162. var keyword2 string
  163. var keyword3 string
  164. var keyword4 string
  165. var remark string
  166. var redirectUrl string
  167. specialItem, e := models.GetYanxuanSpecialItemById(specialId)
  168. if e != nil {
  169. err = errors.New("GetYanxuanSpecialFollowUserById, Err: " + e.Error())
  170. return
  171. }
  172. user, e := models.GetWxUserItemByUserId(specialItem.UserId)
  173. if e != nil {
  174. err = errors.New("GetWxUserItemByUserId, Err: " + e.Error())
  175. return err
  176. }
  177. openIdList, err := models.GetUserRecordListByMobile(4, user.Mobile)
  178. if err != nil && err.Error() != utils.ErrNoRow() {
  179. return err
  180. }
  181. if len(openIdList) == 0 {
  182. err = nil
  183. return
  184. }
  185. keyword1 = "研选专栏内容审核"
  186. if status == 1 {
  187. keyword2 = "已通过审核,点击查看详情"
  188. redirectUrl = utils.WX_MSG_PATH_YX_SPECIAL_DETAIL + strconv.Itoa(specialId)
  189. } else {
  190. keyword2 = "未通过审核,点击查看驳回原因"
  191. redirectUrl = utils.WX_MSG_PATH_YX_SPECIAL_CENTER
  192. }
  193. keyword3 = "-"
  194. openIdArr := make([]string, 0)
  195. for _, v := range openIdList {
  196. openIdArr = append(openIdArr, v.OpenId)
  197. }
  198. sendInfo := new(SendWxTemplate)
  199. sendInfo.First = first
  200. sendInfo.Keyword1 = keyword1
  201. sendInfo.Keyword2 = keyword2
  202. sendInfo.Keyword3 = keyword3
  203. sendInfo.Keyword4 = keyword4
  204. sendInfo.Remark = remark
  205. sendInfo.TemplateId = utils.WxMsgTemplateIdArticleUserRemind
  206. sendInfo.RedirectUrl = redirectUrl
  207. sendInfo.RedirectTarget = 3
  208. sendInfo.Resource = strconv.Itoa(specialId)
  209. sendInfo.SendType = utils.TEMPLATE_MSG_CYGX_ARTICLE_ADD
  210. sendInfo.OpenIdArr = openIdArr
  211. err = PublicSendTemplateMsg(sendInfo)
  212. if err != nil {
  213. return
  214. }
  215. return
  216. }
  217. //func init() {
  218. // UpdateYanxuanSpecialResourceData(229)
  219. //}
  220. // 获取研选专栏用户信息
  221. func GetYanxuanSpecialAuthorInfo(user *models.WxUserItem) (isAuthor, isImproveInformation bool) {
  222. var err error
  223. defer func() {
  224. if err != nil {
  225. go utils.SendAlarmMsg(fmt.Sprint("获取研选专栏用户信息失败,GetYanxuanSpecialAuthorInfo Err ", err, "userId", user.UserId), 2)
  226. }
  227. }()
  228. var condition string
  229. condition += ` AND a.status = 1 `
  230. specialUser, e := models.GetYanxuanSpecialAuthor(user.UserId, user.UserId, condition)
  231. if e != nil && e.Error() != utils.ErrNoRow() {
  232. err = errors.New("GetYanxuanSpecialAuthor, Err: " + e.Error())
  233. return
  234. }
  235. if specialUser != nil {
  236. isAuthor = true
  237. //如果昵称 、专栏名称、简介 都不为空就表示信息完善
  238. if specialUser.SpecialName != "" && specialUser.Introduction != "" && specialUser.NickName != "" {
  239. isImproveInformation = true
  240. }
  241. }
  242. return
  243. }
  244. // 获取专栏用户最新的一篇文章信息
  245. func GetBestNewYanxuanSpecialByUserId(userIds []int) (mapResp map[int]*models.CygxYanxuanSpecialCenterAuthorResp) {
  246. lenArr := len(userIds)
  247. if lenArr == 0 {
  248. return
  249. }
  250. var err error
  251. defer func() {
  252. if err != nil {
  253. fmt.Println(err)
  254. go utils.SendAlarmMsg(fmt.Sprint("获取研选专栏用户信息失败,GetBestNewYanxuanSpecialByUserId Err ", err, "userIds", userIds), 2)
  255. }
  256. }()
  257. var condition string
  258. var pars []interface{}
  259. condition += ` AND a.publish_time = ( SELECT max( b.publish_time ) FROM cygx_yanxuan_special b WHERE a.user_id = b.user_id AND b.STATUS = 3 ) AND user_id IN (` + utils.GetOrmInReplace(lenArr) + `) AND a.status = 3 GROUP BY user_id ORDER BY publish_time DESC`
  260. pars = append(pars, userIds)
  261. list, e := models.GetYanxuanSpecialListBycondition(condition, pars, 0, lenArr)
  262. if e != nil && e.Error() != utils.ErrNoRow() {
  263. err = errors.New("GetYanxuanSpecialListBycondition, Err: " + e.Error())
  264. return
  265. }
  266. mapResp = make(map[int]*models.CygxYanxuanSpecialCenterAuthorResp, 0)
  267. for _, v := range list {
  268. item := new(models.CygxYanxuanSpecialCenterAuthorResp)
  269. item.UserId = v.UserId
  270. item.Id = v.Id
  271. item.Title = v.Title
  272. item.PublishTime = v.PublishTime
  273. mapResp[v.UserId] = item
  274. }
  275. return
  276. }
  277. // 研选专栏审批记录
  278. func AddAddCygxYanxuanSpecialApprovalLog(user *models.WxUserItem, specialId, status int, reason string) {
  279. var err error
  280. defer func() {
  281. if err != nil {
  282. go utils.SendAlarmMsg(fmt.Sprint("获取研选专栏用户信息失败,GetYanxuanSpecialAuthorInfo Err ", err, "userId", user.UserId), 2)
  283. }
  284. }()
  285. detail, e := models.GetYanxuanSpecialBySpecialId(specialId)
  286. if e != nil {
  287. err = errors.New("GetYanxuanSpecialBySpecialId, Err: " + e.Error())
  288. return
  289. }
  290. specialAuthor, e := models.GetCygxYanxuanSpecialAuthorByUserId(detail.UserId)
  291. if e != nil {
  292. err = errors.New("GetCygxYanxuanSpecialAuthorByUserId, Err: " + e.Error())
  293. return
  294. }
  295. item := new(models.CygxYanxuanSpecialApprovalLog)
  296. item.UserId = detail.UserId
  297. item.Content = detail.Content
  298. item.Tags = detail.Tags
  299. item.ApprovalStatus = status
  300. item.ImgUrl = detail.ImgUrl
  301. item.DocUrl = detail.DocUrl
  302. item.Reason = reason
  303. item.Title = detail.Title
  304. item.Type = detail.Type
  305. item.CompanyTags = detail.CompanyTags
  306. item.IndustryTags = detail.IndustryTags
  307. item.YanxuanSpecialId = specialId
  308. item.AdminName = user.RealName
  309. item.AdminUserId = user.UserId
  310. item.SpecialName = specialAuthor.SpecialName
  311. item.NickName = specialAuthor.NickName
  312. item.CreateTime = time.Now()
  313. item.ModifyTime = time.Now()
  314. e = models.AddCygxYanxuanSpecialApprovalLog(item)
  315. if e != nil {
  316. err = errors.New("AddCygxYanxuanSpecialApprovalLog, Err: " + e.Error())
  317. return
  318. }
  319. return
  320. }
  321. // 更新文章收藏数量
  322. func UdpateYanxuanSpecialCollect(specialId int) {
  323. var err error
  324. defer func() {
  325. if err != nil {
  326. fmt.Println(err)
  327. go utils.SendAlarmMsg(fmt.Sprint("更新文章收藏数量失败,UdpateYanxuanSpecialCollect Err ", err, "specialId:", specialId), 2)
  328. }
  329. }()
  330. detail, e := models.GetYanxuanSpecialBySpecialId(specialId)
  331. if e != nil {
  332. err = errors.New("GetYanxuanSpecialBySpecialId, Err: " + e.Error())
  333. return
  334. }
  335. specialAuthor, e := models.GetCygxYanxuanSpecialAuthorByUserId(detail.UserId)
  336. if e != nil {
  337. err = errors.New("GetCygxYanxuanSpecialAuthorByUserId, Err: " + e.Error())
  338. return
  339. }
  340. var condition string
  341. var pars []interface{}
  342. condition = " AND yanxuan_special_id = ? "
  343. pars = append(pars, specialId)
  344. //专栏被收藏的数量
  345. totalSpecial, e := models.GetCygxYanxuanSpecialCollectCount(condition, pars)
  346. if e != nil {
  347. err = errors.New("GetCygxYanxuanSpecialCollectCount, Err: " + e.Error())
  348. return
  349. }
  350. pars = make([]interface{}, 0)
  351. condition = " AND yanxuan_special_id IN (SELECT id FROM cygx_yanxuan_special AS a WHERE user_id = ?) "
  352. pars = append(pars, specialAuthor.UserId)
  353. //作者被收藏的数量
  354. totalAuthor, e := models.GetCygxYanxuanSpecialCollectCount(condition, pars)
  355. if e != nil {
  356. err = errors.New("GetCygxYanxuanSpecialCollectCount, Err: " + e.Error())
  357. return
  358. }
  359. //更新文章被收藏数量
  360. e = models.UpdateYanxuanSpecialarticleCollectNum(totalSpecial, specialId)
  361. if e != nil {
  362. err = errors.New("UpdateYanxuanSpecialarticleCollectNum, Err: " + e.Error())
  363. return
  364. }
  365. //更新作者文章被收藏数量
  366. e = models.UpdateYanxuanSpecialAuthorArticleCollectNum(totalAuthor, specialAuthor.UserId)
  367. if e != nil {
  368. err = errors.New("UpdateYanxuanSpecialAuthorArticleCollectNum, Err: " + e.Error())
  369. return
  370. }
  371. return
  372. }
  373. // 更新作者粉丝数量
  374. func UdpateYanxuanSpecialFansNum(specialUserIdId int) {
  375. var err error
  376. defer func() {
  377. if err != nil {
  378. fmt.Println(err)
  379. go utils.SendAlarmMsg(fmt.Sprint("更新作者粉丝数量失败,UdpateYanxuanSpecialFansNum Err ", err, "specialUserIdId", specialUserIdId), 2)
  380. }
  381. }()
  382. var condition string
  383. var pars []interface{}
  384. condition = " AND follow_user_id = ? "
  385. pars = append(pars, specialUserIdId)
  386. //作者粉丝数量
  387. total, e := models.GetCygxYanxuanSpecialFollowCount(condition, pars)
  388. if e != nil {
  389. err = errors.New("GetCygxYanxuanSpecialFollowCount, Err: " + e.Error())
  390. return
  391. }
  392. e = models.UpdateYanxuanSpecialAuthorFansNum(total, specialUserIdId)
  393. if e != nil {
  394. err = errors.New("UpdateYanxuanSpecialAuthorFansNum, Err: " + e.Error())
  395. return
  396. }
  397. return
  398. }
  399. // UdpateYanxuanSpecialauthorArticleNum 更新作者发布文章的数量
  400. func UdpateYanxuanSpecialauthorArticleNum(authoruserId int) {
  401. var err error
  402. defer func() {
  403. if err != nil {
  404. fmt.Println(err)
  405. go utils.SendAlarmMsg(fmt.Sprint("更新作者发布文章的数量失败,UdpateYanxuanSpecialauthorArticleNum Err ", err, "userId", authoruserId), 2)
  406. }
  407. }()
  408. var condition string
  409. var pars []interface{}
  410. condition = ` AND status = 3 AND user_id = ? `
  411. pars = append(pars, authoruserId)
  412. total, e := models.GetCygxYanxuanSpecialCount(condition, pars)
  413. if e != nil {
  414. err = errors.New("GetCygxYanxuanSpecialCount, Err: " + e.Error())
  415. return
  416. }
  417. e = models.UdpateYanxuanSpecialauthorArticleNum(total, authoruserId)
  418. if e != nil {
  419. err = errors.New("UdpateYanxuanSpecialauthorArticleNum, Err: " + e.Error())
  420. return
  421. }
  422. return
  423. }
  424. // GetYanxuanSpecialCollectMap 根据用户ID获取所有研选专栏的收藏
  425. func GetYanxuanSpecialCollectMap(userId int) (mapResp map[int]bool, err error) {
  426. defer func() {
  427. if err != nil {
  428. go utils.SendAlarmMsg("GetYanxuanSpecialCollectMap 根据用户ID获取所有研选专栏的收藏失败 ErrMsg:"+err.Error(), 2)
  429. }
  430. }()
  431. list, e := models.GetCygxYanxuanSpecialCollectByUser(userId)
  432. if e != nil && e.Error() != utils.ErrNoRow() {
  433. err = errors.New("根据用户ID获取所有文章收藏,GetCygxYanxuanSpecialCollectByUser " + e.Error())
  434. return
  435. }
  436. mapResp = make(map[int]bool, 0)
  437. if len(list) > 0 {
  438. for _, v := range list {
  439. mapResp[v.YanxuanSpecialId] = true
  440. }
  441. }
  442. return
  443. }
  444. //func init() {
  445. // UpdateYanxuanSpecialAuthoMomentsImg()
  446. //}
  447. // 更新研选专栏作者朋友圈封面图片,并删除老的图片
  448. // func UpdateYanxuanSpecialAuthoMomentsImg() {
  449. func UpdateYanxuanSpecialAuthoMomentsImg(cont context.Context) (err error) {
  450. //var err error
  451. //time.Sleep(3*time.Second) // 有时候同时添加多个活动,延迟三秒
  452. defer func() {
  453. if err != nil {
  454. fmt.Println("err:", err)
  455. go utils.SendAlarmMsg("生成研选专栏分享到朋友圈的图片,MakeArticleMomentsImg Err:"+err.Error(), 3)
  456. }
  457. }()
  458. var condition string
  459. var pars []interface{}
  460. condition += ` AND a.nick_name <> '' `
  461. condition += ` ORDER BY latest_publish_time DESC`
  462. list, e := models.GetYanxuanSpecialAuthorList(condition, pars, 0, 999)
  463. if e != nil {
  464. err = errors.New("GetYanxuanSpecialAuthorList, Err: " + e.Error())
  465. return
  466. }
  467. for _, v := range list {
  468. time.Sleep(500 * time.Millisecond) // 延迟0.5秒
  469. GetYanxuanSpecialAuthoMomentsImg(v.UserId)
  470. }
  471. return
  472. }