article_history.go 8.1 KB

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