cygx_yanxuan_special.go 14 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471
  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. //func init() {
  213. // UpdateYanxuanSpecialResourceData(229)
  214. //}
  215. // 获取研选专栏用户信息
  216. func GetYanxuanSpecialAuthorInfo(user *models.WxUserItem) (isAuthor, isImproveInformation bool) {
  217. var err error
  218. defer func() {
  219. if err != nil {
  220. go utils.SendAlarmMsg(fmt.Sprint("获取研选专栏用户信息失败,GetYanxuanSpecialAuthorInfo Err ", err, "userId", user.UserId), 2)
  221. }
  222. }()
  223. var condition string
  224. condition += ` AND a.status = 1 `
  225. specialUser, e := models.GetYanxuanSpecialAuthor(user.UserId, user.UserId, condition)
  226. if e != nil && e.Error() != utils.ErrNoRow() {
  227. err = errors.New("GetYanxuanSpecialAuthor, Err: " + e.Error())
  228. return
  229. }
  230. if specialUser != nil {
  231. isAuthor = true
  232. //如果昵称 、专栏名称、简介 都不为空就表示信息完善
  233. if specialUser.SpecialName != "" && specialUser.Introduction != "" && specialUser.NickName != "" {
  234. isImproveInformation = true
  235. }
  236. }
  237. return
  238. }
  239. // 获取专栏用户最新的一篇文章信息
  240. func GetBestNewYanxuanSpecialByUserId(userIds []int) (mapResp map[int]*models.CygxYanxuanSpecialCenterAuthorResp) {
  241. lenArr := len(userIds)
  242. if lenArr == 0 {
  243. return
  244. }
  245. var err error
  246. defer func() {
  247. if err != nil {
  248. fmt.Println(err)
  249. go utils.SendAlarmMsg(fmt.Sprint("获取研选专栏用户信息失败,GetBestNewYanxuanSpecialByUserId Err ", err, "userIds", userIds), 2)
  250. }
  251. }()
  252. var condition string
  253. var pars []interface{}
  254. 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`
  255. pars = append(pars, userIds)
  256. list, e := models.GetYanxuanSpecialListBycondition(condition, pars, 0, lenArr)
  257. if e != nil && e.Error() != utils.ErrNoRow() {
  258. err = errors.New("GetYanxuanSpecialListBycondition, Err: " + e.Error())
  259. return
  260. }
  261. mapResp = make(map[int]*models.CygxYanxuanSpecialCenterAuthorResp, 0)
  262. for _, v := range list {
  263. item := new(models.CygxYanxuanSpecialCenterAuthorResp)
  264. item.UserId = v.UserId
  265. item.Id = v.Id
  266. item.Title = v.Title
  267. item.PublishTime = v.PublishTime
  268. mapResp[v.UserId] = item
  269. }
  270. return
  271. }
  272. // 研选专栏审批记录
  273. func AddAddCygxYanxuanSpecialApprovalLog(user *models.WxUserItem, specialId, status int, reason string) {
  274. var err error
  275. defer func() {
  276. if err != nil {
  277. go utils.SendAlarmMsg(fmt.Sprint("获取研选专栏用户信息失败,GetYanxuanSpecialAuthorInfo Err ", err, "userId", user.UserId), 2)
  278. }
  279. }()
  280. detail, e := models.GetYanxuanSpecialBySpecialId(specialId)
  281. if e != nil {
  282. err = errors.New("GetYanxuanSpecialBySpecialId, Err: " + e.Error())
  283. return
  284. }
  285. specialAuthor, e := models.GetCygxYanxuanSpecialAuthorByUserId(detail.UserId)
  286. if e != nil {
  287. err = errors.New("GetCygxYanxuanSpecialAuthorByUserId, Err: " + e.Error())
  288. return
  289. }
  290. item := new(models.CygxYanxuanSpecialApprovalLog)
  291. item.UserId = detail.UserId
  292. item.Content = detail.Content
  293. item.Tags = detail.Tags
  294. item.ApprovalStatus = status
  295. item.ImgUrl = detail.ImgUrl
  296. item.DocUrl = detail.DocUrl
  297. item.Reason = reason
  298. item.Title = detail.Title
  299. item.Type = detail.Type
  300. item.CompanyTags = detail.CompanyTags
  301. item.IndustryTags = detail.IndustryTags
  302. item.YanxuanSpecialId = specialId
  303. item.AdminName = user.RealName
  304. item.AdminUserId = user.UserId
  305. item.SpecialName = specialAuthor.SpecialName
  306. item.NickName = specialAuthor.NickName
  307. item.CreateTime = time.Now()
  308. item.ModifyTime = time.Now()
  309. e = models.AddCygxYanxuanSpecialApprovalLog(item)
  310. if e != nil {
  311. err = errors.New("AddCygxYanxuanSpecialApprovalLog, Err: " + e.Error())
  312. return
  313. }
  314. return
  315. }
  316. // 更新文章收藏数量
  317. func UdpateYanxuanSpecialCollect(specialId int) {
  318. var err error
  319. defer func() {
  320. if err != nil {
  321. fmt.Println(err)
  322. go utils.SendAlarmMsg(fmt.Sprint("更新文章收藏数量失败,UdpateYanxuanSpecialCollect Err ", err, "specialId:", specialId), 2)
  323. }
  324. }()
  325. detail, e := models.GetYanxuanSpecialBySpecialId(specialId)
  326. if e != nil {
  327. err = errors.New("GetYanxuanSpecialBySpecialId, Err: " + e.Error())
  328. return
  329. }
  330. specialAuthor, e := models.GetCygxYanxuanSpecialAuthorByUserId(detail.UserId)
  331. if e != nil {
  332. err = errors.New("GetCygxYanxuanSpecialAuthorByUserId, Err: " + e.Error())
  333. return
  334. }
  335. var condition string
  336. var pars []interface{}
  337. condition = " AND yanxuan_special_id = ? "
  338. pars = append(pars, specialId)
  339. //专栏被收藏的数量
  340. totalSpecial, e := models.GetCygxYanxuanSpecialCollectCount(condition, pars)
  341. if e != nil {
  342. err = errors.New("GetCygxYanxuanSpecialCollectCount, Err: " + e.Error())
  343. return
  344. }
  345. pars = make([]interface{}, 0)
  346. condition = " AND yanxuan_special_id IN (SELECT id FROM cygx_yanxuan_special AS a WHERE user_id = ?) "
  347. pars = append(pars, specialAuthor.UserId)
  348. //作者被收藏的数量
  349. totalAuthor, e := models.GetCygxYanxuanSpecialCollectCount(condition, pars)
  350. if e != nil {
  351. err = errors.New("GetCygxYanxuanSpecialCollectCount, Err: " + e.Error())
  352. return
  353. }
  354. //更新文章被收藏数量
  355. e = models.UpdateYanxuanSpecialarticleCollectNum(totalSpecial, specialId)
  356. if e != nil {
  357. err = errors.New("UpdateYanxuanSpecialarticleCollectNum, Err: " + e.Error())
  358. return
  359. }
  360. //更新作者文章被收藏数量
  361. e = models.UpdateYanxuanSpecialAuthorArticleCollectNum(totalAuthor, specialAuthor.UserId)
  362. if e != nil {
  363. err = errors.New("UpdateYanxuanSpecialAuthorArticleCollectNum, Err: " + e.Error())
  364. return
  365. }
  366. return
  367. }
  368. // 更新作者粉丝数量
  369. func UdpateYanxuanSpecialFansNum(specialUserIdId int) {
  370. var err error
  371. defer func() {
  372. if err != nil {
  373. fmt.Println(err)
  374. go utils.SendAlarmMsg(fmt.Sprint("更新作者粉丝数量失败,UdpateYanxuanSpecialFansNum Err ", err, "specialUserIdId", specialUserIdId), 2)
  375. }
  376. }()
  377. var condition string
  378. var pars []interface{}
  379. condition = " AND follow_user_id = ? "
  380. pars = append(pars, specialUserIdId)
  381. //作者粉丝数量
  382. total, e := models.GetCygxYanxuanSpecialFollowCount(condition, pars)
  383. if e != nil {
  384. err = errors.New("GetCygxYanxuanSpecialFollowCount, Err: " + e.Error())
  385. return
  386. }
  387. e = models.UpdateYanxuanSpecialAuthorFansNum(total, specialUserIdId)
  388. if e != nil {
  389. err = errors.New("UpdateYanxuanSpecialAuthorFansNum, Err: " + e.Error())
  390. return
  391. }
  392. return
  393. }
  394. // UdpateYanxuanSpecialauthorArticleNum 更新作者发布文章的数量
  395. func UdpateYanxuanSpecialauthorArticleNum(authoruserId int) {
  396. var err error
  397. defer func() {
  398. if err != nil {
  399. fmt.Println(err)
  400. go utils.SendAlarmMsg(fmt.Sprint("更新作者发布文章的数量失败,UdpateYanxuanSpecialauthorArticleNum Err ", err, "userId", authoruserId), 2)
  401. }
  402. }()
  403. var condition string
  404. var pars []interface{}
  405. condition = ` AND status = 3 AND user_id = ? `
  406. pars = append(pars, authoruserId)
  407. total, e := models.GetCygxYanxuanSpecialCount(condition, pars)
  408. if e != nil {
  409. err = errors.New("GetCygxYanxuanSpecialCount, Err: " + e.Error())
  410. return
  411. }
  412. e = models.UdpateYanxuanSpecialauthorArticleNum(total, authoruserId)
  413. if e != nil {
  414. err = errors.New("UdpateYanxuanSpecialauthorArticleNum, Err: " + e.Error())
  415. return
  416. }
  417. return
  418. }
  419. // GetYanxuanSpecialCollectMap 根据用户ID获取所有研选专栏的收藏
  420. func GetYanxuanSpecialCollectMap(userId int) (mapResp map[int]bool, err error) {
  421. defer func() {
  422. if err != nil {
  423. go utils.SendAlarmMsg("GetYanxuanSpecialCollectMap 根据用户ID获取所有研选专栏的收藏失败 ErrMsg:"+err.Error(), 2)
  424. }
  425. }()
  426. list, e := models.GetCygxYanxuanSpecialCollectByUser(userId)
  427. if e != nil && e.Error() != utils.ErrNoRow() {
  428. err = errors.New("根据用户ID获取所有文章收藏,GetCygxYanxuanSpecialCollectByUser " + e.Error())
  429. return
  430. }
  431. mapResp = make(map[int]bool, 0)
  432. if len(list) > 0 {
  433. for _, v := range list {
  434. mapResp[v.YanxuanSpecialId] = true
  435. }
  436. }
  437. return
  438. }