cygx_yanxuan_special.go 16 KB

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