article_history.go 8.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288
  1. package services
  2. import (
  3. "context"
  4. "errors"
  5. "fmt"
  6. "hongze/hongze_cygx/models"
  7. "hongze/hongze_cygx/utils"
  8. "strconv"
  9. "time"
  10. )
  11. func GetArticleHistoryByUser(articleIds []int, user *models.WxUserItem) (mapResp map[int]int) {
  12. var err error
  13. defer func() {
  14. if err != nil {
  15. fmt.Println(err)
  16. go utils.SendAlarmMsg("获取用户的阅读数据,信息失败,Err:"+err.Error(), 3)
  17. }
  18. }()
  19. lenIds := len(articleIds)
  20. if lenIds == 0 {
  21. return
  22. }
  23. var condition string
  24. var pars []interface{}
  25. condition = ` AND article_id IN (` + utils.GetOrmInReplace(lenIds) + `) AND user_id = ?`
  26. pars = append(pars, articleIds, user.UserId)
  27. list, err := models.GetCygxArticleHistoryRecordNewpvListPv(condition, pars)
  28. if err != nil {
  29. return
  30. }
  31. mapResp = make(map[int]int, 0)
  32. for _, v := range list {
  33. mapResp[v.ArticleId] = v.Pv
  34. }
  35. return
  36. }
  37. func GetArticleHistoryByArticleId(articleIds []int) (mapResp map[int]int) {
  38. var err error
  39. defer func() {
  40. if err != nil {
  41. fmt.Println(err)
  42. go utils.SendAlarmMsg("获取用户的阅读数据,信息失败,Err:"+err.Error(), 3)
  43. }
  44. }()
  45. lenIds := len(articleIds)
  46. if lenIds == 0 {
  47. return
  48. }
  49. var condition string
  50. var pars []interface{}
  51. condition = ` AND article_id IN (` + utils.GetOrmInReplace(lenIds) + `) `
  52. pars = append(pars, articleIds)
  53. listCy, e := models.GetCygxArticleHistoryRecordNewpvListPvCy(condition, pars)
  54. if e != nil {
  55. err = errors.New("GetCygxArticleHistoryRecordNewpvListPvCy, Err: " + e.Error())
  56. return
  57. }
  58. mapResp = make(map[int]int, 0)
  59. for _, v := range listCy {
  60. mapResp[v.ArticleId] = v.Pv
  61. }
  62. listCl, e := models.GetCygxArticleHistoryRecordNewpvListPvCl(condition, pars)
  63. if e != nil {
  64. err = errors.New("GetCygxArticleHistoryRecordNewpvListPvCy, Err: " + e.Error())
  65. return
  66. }
  67. for _, v := range listCl {
  68. mapResp[v.ArticleId] += v.Pv
  69. }
  70. return
  71. }
  72. // 记录用户文章浏览记录
  73. func ArticleHistory(articleId int, user *models.WxUserItem) (err error) {
  74. defer func() {
  75. if err != nil {
  76. go utils.SendAlarmMsg("记录用户文章浏览记录,失败"+err.Error(), 2)
  77. }
  78. }()
  79. uid := user.UserId
  80. key := "CYGX_ARTICLE_" + strconv.Itoa(articleId) + "_" + strconv.Itoa(uid)
  81. if !utils.Rc.IsExist(key) {
  82. //新增浏览记录
  83. //这个表貌似没怎么用了,暂时保留记录
  84. record := new(models.CygxArticleViewRecord)
  85. record.UserId = uid
  86. record.ArticleId = articleId
  87. record.CreateTime = time.Now()
  88. record.Mobile = user.Mobile
  89. record.Email = user.Email
  90. record.CompanyId = user.CompanyId
  91. record.CompanyName = user.CompanyName
  92. _, e := models.AddCygxArticleViewRecord(record)
  93. if e != nil {
  94. err = errors.New("AddCygxArticleViewRecord, Err: " + e.Error())
  95. return
  96. }
  97. e = models.ModifyReportLastViewTime(uid)
  98. if e != nil {
  99. err = errors.New("ModifyReportLastViewTime, Err: " + e.Error())
  100. return
  101. }
  102. utils.Rc.Put(key, 1, 2*time.Second)
  103. }
  104. return
  105. }
  106. // 记录用户文章浏览记录带时长
  107. func ArticleHistoryStopTime(articleId, stopTime, outType int, user *models.WxUserItem) (err error) {
  108. defer func() {
  109. if err != nil {
  110. go utils.SendAlarmMsg("记录用户文章浏览记录带时长,失败"+err.Error(), 2)
  111. }
  112. }()
  113. if stopTime < 3 {
  114. return
  115. }
  116. uid := user.UserId
  117. key := "CYGX_ARTICLE_PV" + strconv.Itoa(articleId) + "_" + strconv.Itoa(uid) + "_" + strconv.Itoa(user.CompanyId) + "_" + strconv.Itoa(outType)
  118. if !utils.Rc.IsExist(key) {
  119. record := new(models.CygxArticleHistoryRecordNewpv)
  120. record.UserId = uid
  121. record.ArticleId = articleId
  122. record.CreateTime = time.Now().Add(-time.Second * time.Duration(stopTime))
  123. record.ModifyTime = time.Now()
  124. record.Mobile = user.Mobile
  125. record.Email = user.Email
  126. record.CompanyId = user.CompanyId
  127. record.CompanyName = user.CompanyName
  128. record.StopTime = stopTime
  129. record.OutType = outType
  130. record.RegisterPlatform = utils.REGISTER_PLATFORM
  131. record.Source = "MOBILE"
  132. newId, e := models.AddCygxArticleViewRecordNewpv(record)
  133. if e != nil {
  134. err = errors.New("AddCygxArticleViewRecordNewpv, Err: " + e.Error())
  135. return
  136. }
  137. recordRedis := new(ReportViewRecord)
  138. recordRedis.UserId = user.UserId
  139. recordRedis.ReportId = articleId
  140. recordRedis.Mobile = user.Mobile
  141. recordRedis.Email = user.Email
  142. recordRedis.RealName = user.RealName
  143. recordRedis.CompanyName = user.CompanyName
  144. recordRedis.StopTime = stopTime
  145. recordRedis.ViewTime = record.CreateTime.Format(utils.FormatDateTime)
  146. recordRedis.OutId = int(newId)
  147. //recordRedis.CreateTime = time.Now()
  148. go PushViewRecordNewRedisData(recordRedis, user.CompanyId)
  149. go ArticleWxUserRaiLabelRedisAdd(articleId, user.UserId, record.CreateTime)
  150. utils.Rc.Put(key, 1, 2*time.Second)
  151. }
  152. go ArticleHistoryUserLabelLogAdd(articleId, uid)
  153. return
  154. }
  155. //func init() {
  156. // AddAllArticleAndYanxuanHistory()
  157. //}
  158. // 定时任务更新文章和研选专栏记录
  159. func AddAllArticleAndYanxuanHistory(cont context.Context) (err error) {
  160. //func AddAllArticleAndYanxuanHistory() (err error) {
  161. defer func() {
  162. if err != nil {
  163. fmt.Println(err)
  164. go utils.SendAlarmMsg("同步阅读记录到es失败;AddAllArticleHistory Err:"+err.Error(), 2)
  165. }
  166. }()
  167. var pars []interface{}
  168. var condition string
  169. var companyIds []int
  170. condition = " AND product_id = 2 AND STATUS IN ( '正式', '试用', '冻结' ) "
  171. list, e := models.GetCompanyProductList(condition, pars)
  172. if e != nil {
  173. err = errors.New("GetCompanyProductList, Err: " + e.Error())
  174. return
  175. }
  176. for _, v := range list {
  177. companyIds = append(companyIds, v.CompanyId)
  178. }
  179. //添加文章记录
  180. {
  181. condition = ""
  182. pars = make([]interface{}, 0)
  183. condition = " AND art.company_id IN (" + utils.GetOrmInReplace(len(companyIds)) + ") AND art.is_del = 0 AND art.create_time >= ? AND art.create_time <= ? "
  184. pars = append(pars, companyIds, time.Now().AddDate(0, 0, -1).Format(utils.FormatDate), time.Now().Format(utils.FormatDate))
  185. total, e := models.GetCygxArticleHistoryRecordAllCountBycondition(condition, pars)
  186. if e != nil {
  187. err = errors.New("GetCygxArticleHistoryRecordAllCount, Err: " + e.Error())
  188. return
  189. }
  190. for i := 0; i <= total/2000; i++ {
  191. allList, e := models.GetCygxArticleHistoryRecordAllList(condition, pars, 2000*i, 2000)
  192. if e != nil {
  193. err = errors.New("GetCygxArticleHistoryRecordAllList, Err: " + e.Error())
  194. return
  195. }
  196. var items []*models.CygxArticleAndYanxuanRecord
  197. for _, v := range allList {
  198. item := new(models.CygxArticleAndYanxuanRecord)
  199. item.SourceId = v.ArticleId
  200. item.Source = utils.CYGX_OBJ_ARTICLE
  201. item.UserId = v.UserId
  202. item.RealName = v.RealName
  203. item.Mobile = v.Mobile
  204. item.Email = v.Email
  205. item.CompanyId = v.CompanyId
  206. item.CompanyName = v.CompanyName
  207. item.CompanyId = v.CompanyId
  208. item.CreateTime = v.CreateTime
  209. item.ModifyTime = v.ModifyTime
  210. item.StopTime = v.StopTime
  211. if v.Source == "CELUE" {
  212. item.RegisterPlatform = 3
  213. } else if v.Source == "WEB" {
  214. item.RegisterPlatform = 2
  215. } else {
  216. item.RegisterPlatform = 1
  217. }
  218. items = append(items, item)
  219. }
  220. e = models.AddCygxArticleAndYanxuanRecordMulti(items)
  221. if e != nil {
  222. err = errors.New("AddCygxArticleAndYanxuanRecordMulti, Err: " + e.Error())
  223. return
  224. }
  225. }
  226. }
  227. //添加研选专栏记录
  228. {
  229. condition = ""
  230. pars = make([]interface{}, 0)
  231. condition = " AND art.create_time >= ? AND art.create_time <= ? AND user_id > 0 "
  232. pars = append(pars, time.Now().AddDate(0, 0, -1).Format(utils.FormatDate), time.Now().Format(utils.FormatDate))
  233. totalYanxuanSpecial, e := models.GetCygxYanxuanSpecialRecordCount(condition, pars)
  234. if e != nil {
  235. err = errors.New("GetCygxYanxuanSpecialRecordCount, Err: " + e.Error())
  236. return
  237. }
  238. for i := 0; i <= totalYanxuanSpecial/2000; i++ {
  239. listYanxuanSpecialRecord, e := models.GetCygxYanxuanSpecialRecordRespList(condition, pars, 2000*i, 2000)
  240. if e != nil {
  241. err = errors.New("GetCygxYanxuanSpecialRecordRespList, Err: " + e.Error())
  242. return
  243. }
  244. var items []*models.CygxArticleAndYanxuanRecord
  245. for _, v := range listYanxuanSpecialRecord {
  246. item := new(models.CygxArticleAndYanxuanRecord)
  247. item.SourceId = v.YanxuanSpecialId
  248. item.Source = utils.CYGX_OBJ_YANXUANSPECIAL
  249. item.UserId = v.UserId
  250. item.RealName = v.RealName
  251. item.Mobile = v.Mobile
  252. item.Email = v.Email
  253. item.CompanyId = v.CompanyId
  254. item.CompanyName = v.CompanyName
  255. item.CompanyId = v.CompanyId
  256. item.CreateTime = v.CreateTime
  257. item.ModifyTime = v.ModifyTime
  258. item.StopTime = v.StopTime
  259. item.PermissionCode = v.PermissionCode
  260. item.RegisterPlatform = v.RegisterPlatform
  261. items = append(items, item)
  262. }
  263. e = models.AddCygxArticleAndYanxuanRecordMulti(items)
  264. if e != nil {
  265. err = errors.New("AddCygxArticleAndYanxuanRecordMulti, Err: " + e.Error())
  266. return
  267. }
  268. }
  269. }
  270. return
  271. }