article_history.go 8.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287
  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. utils.Rc.Put(key, 1, 2*time.Second)
  150. }
  151. go ArticleHistoryUserLabelLogAdd(articleId, uid)
  152. return
  153. }
  154. //func init() {
  155. // AddAllArticleAndYanxuanHistory()
  156. //}
  157. // 定时任务更新文章和研选专栏记录
  158. func AddAllArticleAndYanxuanHistory(cont context.Context) (err error) {
  159. //func AddAllArticleAndYanxuanHistory() (err error) {
  160. defer func() {
  161. if err != nil {
  162. fmt.Println(err)
  163. go utils.SendAlarmMsg("同步阅读记录到es失败;AddAllArticleHistory Err:"+err.Error(), 2)
  164. }
  165. }()
  166. var pars []interface{}
  167. var condition string
  168. var companyIds []int
  169. condition = " AND product_id = 2 AND STATUS IN ( '正式', '试用', '冻结' ) "
  170. list, e := models.GetCompanyProductList(condition, pars)
  171. if e != nil {
  172. err = errors.New("GetCompanyProductList, Err: " + e.Error())
  173. return
  174. }
  175. for _, v := range list {
  176. companyIds = append(companyIds, v.CompanyId)
  177. }
  178. //添加文章记录
  179. {
  180. condition = ""
  181. pars = make([]interface{}, 0)
  182. condition = " AND art.company_id IN (" + utils.GetOrmInReplace(len(companyIds)) + ") AND art.is_del = 0 AND art.create_time >= ? AND art.create_time <= ? "
  183. pars = append(pars, companyIds, time.Now().AddDate(0, 0, -1).Format(utils.FormatDate), time.Now().Format(utils.FormatDate))
  184. total, e := models.GetCygxArticleHistoryRecordAllCountBycondition(condition, pars)
  185. if e != nil {
  186. err = errors.New("GetCygxArticleHistoryRecordAllCount, Err: " + e.Error())
  187. return
  188. }
  189. for i := 0; i <= total/2000; i++ {
  190. allList, e := models.GetCygxArticleHistoryRecordAllList(condition, pars, 2000*i, 2000)
  191. if e != nil {
  192. err = errors.New("GetCygxArticleHistoryRecordAllList, Err: " + e.Error())
  193. return
  194. }
  195. var items []*models.CygxArticleAndYanxuanRecord
  196. for _, v := range allList {
  197. item := new(models.CygxArticleAndYanxuanRecord)
  198. item.SourceId = v.ArticleId
  199. item.Source = utils.CYGX_OBJ_ARTICLE
  200. item.UserId = v.UserId
  201. item.RealName = v.RealName
  202. item.Mobile = v.Mobile
  203. item.Email = v.Email
  204. item.CompanyId = v.CompanyId
  205. item.CompanyName = v.CompanyName
  206. item.CompanyId = v.CompanyId
  207. item.CreateTime = v.CreateTime
  208. item.ModifyTime = v.ModifyTime
  209. item.StopTime = v.StopTime
  210. if v.Source == "CELUE" {
  211. item.RegisterPlatform = 3
  212. } else if v.Source == "WEB" {
  213. item.RegisterPlatform = 2
  214. } else {
  215. item.RegisterPlatform = 1
  216. }
  217. items = append(items, item)
  218. }
  219. e = models.AddCygxArticleAndYanxuanRecordMulti(items)
  220. if e != nil {
  221. err = errors.New("AddCygxArticleAndYanxuanRecordMulti, Err: " + e.Error())
  222. return
  223. }
  224. }
  225. }
  226. //添加研选专栏记录
  227. {
  228. condition = ""
  229. pars = make([]interface{}, 0)
  230. condition = " AND art.create_time >= ? AND art.create_time <= ? AND user_id > 0 "
  231. pars = append(pars, time.Now().AddDate(0, 0, -1).Format(utils.FormatDate), time.Now().Format(utils.FormatDate))
  232. totalYanxuanSpecial, e := models.GetCygxYanxuanSpecialRecordCount(condition, pars)
  233. if e != nil {
  234. err = errors.New("GetCygxYanxuanSpecialRecordCount, Err: " + e.Error())
  235. return
  236. }
  237. for i := 0; i <= totalYanxuanSpecial/2000; i++ {
  238. listYanxuanSpecialRecord, e := models.GetCygxYanxuanSpecialRecordRespList(condition, pars, 2000*i, 2000)
  239. if e != nil {
  240. err = errors.New("GetCygxYanxuanSpecialRecordRespList, Err: " + e.Error())
  241. return
  242. }
  243. var items []*models.CygxArticleAndYanxuanRecord
  244. for _, v := range listYanxuanSpecialRecord {
  245. item := new(models.CygxArticleAndYanxuanRecord)
  246. item.SourceId = v.YanxuanSpecialId
  247. item.Source = utils.CYGX_OBJ_YANXUANSPECIAL
  248. item.UserId = v.UserId
  249. item.RealName = v.RealName
  250. item.Mobile = v.Mobile
  251. item.Email = v.Email
  252. item.CompanyId = v.CompanyId
  253. item.CompanyName = v.CompanyName
  254. item.CompanyId = v.CompanyId
  255. item.CreateTime = v.CreateTime
  256. item.ModifyTime = v.ModifyTime
  257. item.StopTime = v.StopTime
  258. item.PermissionCode = v.PermissionCode
  259. item.RegisterPlatform = v.RegisterPlatform
  260. items = append(items, item)
  261. }
  262. e = models.AddCygxArticleAndYanxuanRecordMulti(items)
  263. if e != nil {
  264. err = errors.New("AddCygxArticleAndYanxuanRecordMulti, Err: " + e.Error())
  265. return
  266. }
  267. }
  268. }
  269. return
  270. }