cygx_yanxuan_special.go 15 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506
  1. package services
  2. import (
  3. "errors"
  4. "fmt"
  5. "hongze/hongze_cygx/models"
  6. "hongze/hongze_cygx/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(fmt.Sprint("处理试用申请给王芳,汪洋发消息失败, specialId:", specialId, "ErrMsg", err.Error()), 2)
  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("更新研选专栏失败sourceId: ", sourceId, "ErrMsg", err.Error()), 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 := detail.PublishTime
  250. item := new(models.CygxResourceData)
  251. item.SourceId = sourceId
  252. item.Source = source
  253. item.PublishDate = publishDate
  254. item.CreateTime = time.Now()
  255. if totalData == 0 {
  256. _, e := models.AddCygxResourceData(item)
  257. if e != nil {
  258. err = errors.New("AddCygxResourceData, Err: " + e.Error())
  259. return
  260. }
  261. } else {
  262. e = models.UpdateResourceDataByItem(item)
  263. if e != nil {
  264. err = errors.New("UpdateResourceDataByItem, Err: " + e.Error())
  265. return
  266. }
  267. }
  268. }
  269. return
  270. }
  271. // 获取研选专栏用户信息
  272. func GetYanxuanSpecialAuthorInfo(user *models.WxUserItem) (isAuthor, isImproveInformation bool) {
  273. var err error
  274. defer func() {
  275. if err != nil {
  276. go utils.SendAlarmMsg(fmt.Sprint("获取研选专栏用户信息失败,GetYanxuanSpecialAuthorInfo Err ", err, "userId", user.UserId), 2)
  277. }
  278. }()
  279. var condition string
  280. condition += ` AND a.status = 1 `
  281. specialUser, e := models.GetYanxuanSpecialAuthor(user.UserId, user.UserId, condition)
  282. if e != nil && e.Error() != utils.ErrNoRow() {
  283. err = errors.New("GetYanxuanSpecialAuthor, Err: " + e.Error())
  284. return
  285. }
  286. if specialUser != nil {
  287. isAuthor = true
  288. //如果昵称 、专栏名称、简介 都不为空就表示信息完善
  289. if specialUser.SpecialName != "" && specialUser.Introduction != "" && specialUser.NickName != "" {
  290. isImproveInformation = true
  291. }
  292. }
  293. return
  294. }
  295. // 获取专栏用户最新的一篇文章信息
  296. func GetBestNewYanxuanSpecialByUserId(userIds []int) (mapResp map[int]*models.CygxYanxuanSpecialCenterAuthorResp) {
  297. lenArr := len(userIds)
  298. if lenArr == 0 {
  299. return
  300. }
  301. var err error
  302. defer func() {
  303. if err != nil {
  304. fmt.Println(err)
  305. go utils.SendAlarmMsg(fmt.Sprint("获取研选专栏用户信息失败,GetBestNewYanxuanSpecialByUserId Err ", err, "userIds", userIds), 2)
  306. }
  307. }()
  308. var condition string
  309. var pars []interface{}
  310. 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`
  311. pars = append(pars, userIds)
  312. list, e := models.GetYanxuanSpecialListBycondition(condition, pars, 0, lenArr)
  313. if e != nil && e.Error() != utils.ErrNoRow() {
  314. err = errors.New("GetYanxuanSpecialListBycondition, Err: " + e.Error())
  315. return
  316. }
  317. mapResp = make(map[int]*models.CygxYanxuanSpecialCenterAuthorResp, 0)
  318. for _, v := range list {
  319. item := new(models.CygxYanxuanSpecialCenterAuthorResp)
  320. item.UserId = v.UserId
  321. item.Id = v.Id
  322. item.Title = v.Title
  323. item.PublishTime = v.PublishTime
  324. mapResp[v.UserId] = item
  325. }
  326. return
  327. }
  328. // 研选专栏审批记录
  329. func AddAddCygxYanxuanSpecialApprovalLog(user *models.WxUserItem, specialId, status int, reason string) {
  330. var err error
  331. defer func() {
  332. if err != nil {
  333. go utils.SendAlarmMsg(fmt.Sprint("获取研选专栏用户信息失败,GetYanxuanSpecialAuthorInfo Err ", err, "userId", user.UserId), 2)
  334. }
  335. }()
  336. detail, e := models.GetYanxuanSpecialBySpecialId(specialId)
  337. if e != nil {
  338. err = errors.New("GetYanxuanSpecialBySpecialId, Err: " + e.Error())
  339. return
  340. }
  341. specialAuthor, e := models.GetCygxYanxuanSpecialAuthorByUserId(detail.UserId)
  342. if e != nil {
  343. err = errors.New("GetCygxYanxuanSpecialAuthorByUserId, Err: " + e.Error())
  344. return
  345. }
  346. item := new(models.CygxYanxuanSpecialApprovalLog)
  347. item.UserId = detail.UserId
  348. item.Content = detail.Content
  349. item.Tags = detail.Tags
  350. item.ApprovalStatus = status
  351. item.ImgUrl = detail.ImgUrl
  352. item.DocUrl = detail.DocUrl
  353. item.Reason = reason
  354. item.Title = detail.Title
  355. item.Type = detail.Type
  356. item.CompanyTags = detail.CompanyTags
  357. item.IndustryTags = detail.IndustryTags
  358. item.YanxuanSpecialId = specialId
  359. item.AdminName = user.RealName
  360. item.AdminUserId = user.UserId
  361. item.SpecialName = specialAuthor.SpecialName
  362. item.NickName = specialAuthor.NickName
  363. item.CreateTime = time.Now()
  364. item.ModifyTime = time.Now()
  365. e = models.AddCygxYanxuanSpecialApprovalLog(item)
  366. if e != nil {
  367. err = errors.New("AddCygxYanxuanSpecialApprovalLog, Err: " + e.Error())
  368. return
  369. }
  370. return
  371. }
  372. // 更新文章收藏数量
  373. func UdpateYanxuanSpecialCollect(specialId int) {
  374. var err error
  375. defer func() {
  376. if err != nil {
  377. fmt.Println(err)
  378. go utils.SendAlarmMsg(fmt.Sprint("更新文章收藏数量失败,UdpateYanxuanSpecialCollect Err ", err, "specialId:", specialId), 2)
  379. }
  380. }()
  381. detail, e := models.GetYanxuanSpecialBySpecialId(specialId)
  382. if e != nil {
  383. err = errors.New("GetYanxuanSpecialBySpecialId, Err: " + e.Error())
  384. return
  385. }
  386. specialAuthor, e := models.GetCygxYanxuanSpecialAuthorByUserId(detail.UserId)
  387. if e != nil {
  388. err = errors.New("GetCygxYanxuanSpecialAuthorByUserId, Err: " + e.Error())
  389. return
  390. }
  391. var condition string
  392. var pars []interface{}
  393. condition = " AND yanxuan_special_id = ? "
  394. pars = append(pars, specialId)
  395. //专栏被收藏的数量
  396. totalSpecial, e := models.GetCygxYanxuanSpecialCollectCount(condition, pars)
  397. if e != nil {
  398. err = errors.New("GetCygxYanxuanSpecialCollectCount, Err: " + e.Error())
  399. return
  400. }
  401. pars = make([]interface{}, 0)
  402. condition = " AND yanxuan_special_id IN (SELECT id FROM cygx_yanxuan_special AS a WHERE user_id = ?) "
  403. pars = append(pars, specialAuthor.UserId)
  404. //作者被收藏的数量
  405. totalAuthor, e := models.GetCygxYanxuanSpecialCollectCount(condition, pars)
  406. if e != nil {
  407. err = errors.New("GetCygxYanxuanSpecialCollectCount, Err: " + e.Error())
  408. return
  409. }
  410. //更新文章被收藏数量
  411. e = models.UpdateYanxuanSpecialarticleCollectNum(totalSpecial, specialId)
  412. if e != nil {
  413. err = errors.New("UpdateYanxuanSpecialarticleCollectNum, Err: " + e.Error())
  414. return
  415. }
  416. //更新作者文章被收藏数量
  417. e = models.UpdateYanxuanSpecialAuthorArticleCollectNum(totalAuthor, specialAuthor.UserId)
  418. if e != nil {
  419. err = errors.New("UpdateYanxuanSpecialAuthorArticleCollectNum, Err: " + e.Error())
  420. return
  421. }
  422. return
  423. }
  424. // 更新作者粉丝数量
  425. func UdpateYanxuanSpecialFansNum(specialUserIdId int) {
  426. var err error
  427. defer func() {
  428. if err != nil {
  429. fmt.Println(err)
  430. go utils.SendAlarmMsg(fmt.Sprint("更新作者粉丝数量失败,UdpateYanxuanSpecialFansNum Err ", err, "specialUserIdId", specialUserIdId), 2)
  431. }
  432. }()
  433. var condition string
  434. var pars []interface{}
  435. condition = " AND follow_user_id = ? "
  436. pars = append(pars, specialUserIdId)
  437. //作者粉丝数量
  438. total, e := models.GetCygxYanxuanSpecialFollowCount(condition, pars)
  439. if e != nil {
  440. err = errors.New("GetCygxYanxuanSpecialFollowCount, Err: " + e.Error())
  441. return
  442. }
  443. e = models.UpdateYanxuanSpecialAuthorFansNum(total, specialUserIdId)
  444. if e != nil {
  445. err = errors.New("UpdateYanxuanSpecialAuthorFansNum, Err: " + e.Error())
  446. return
  447. }
  448. return
  449. }
  450. // UdpateYanxuanSpecialauthorArticleNum 更新作者发布文章的数量
  451. func UdpateYanxuanSpecialauthorArticleNum(authoruserId int) {
  452. var err error
  453. defer func() {
  454. if err != nil {
  455. fmt.Println(err)
  456. go utils.SendAlarmMsg(fmt.Sprint("更新作者发布文章的数量失败,UdpateYanxuanSpecialauthorArticleNum Err ", err, "userId", authoruserId), 2)
  457. }
  458. }()
  459. var condition string
  460. var pars []interface{}
  461. condition = ` AND status = 3 AND user_id = ? `
  462. pars = append(pars, authoruserId)
  463. total, e := models.GetCygxYanxuanSpecialCount(condition, pars)
  464. if e != nil {
  465. err = errors.New("GetCygxYanxuanSpecialCount, Err: " + e.Error())
  466. return
  467. }
  468. e = models.UdpateYanxuanSpecialauthorArticleNum(total, authoruserId)
  469. if e != nil {
  470. err = errors.New("UdpateYanxuanSpecialauthorArticleNum, Err: " + e.Error())
  471. return
  472. }
  473. return
  474. }