article_history_record_newpv.go 12 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397
  1. package models
  2. import (
  3. "fmt"
  4. "github.com/beego/beego/v2/client/orm"
  5. "hongze/hongze_mfyx/models/company"
  6. "hongze/hongze_mfyx/utils"
  7. "strconv"
  8. "time"
  9. )
  10. type CygxArticleHistoryRecordNewpv struct {
  11. Id int `orm:"column(id);pk"`
  12. ArticleId int
  13. UserId int
  14. CreateTime time.Time
  15. ModifyTime time.Time
  16. Mobile string `description:"手机号"`
  17. Email string `description:"邮箱"`
  18. CompanyId int `description:"公司id"`
  19. CompanyName string `description:"公司名称"`
  20. StopTime int `description:"停留时间"`
  21. OutType int `description:"退出方式,1正常退出,2强制关闭"`
  22. Source string `description:"来源,MOBILE:手机端,PC:电脑端"`
  23. RegisterPlatform int `description:"来源 1小程序,2:网页"`
  24. }
  25. // 添加阅读记录信息
  26. func AddCygxArticleViewRecordNewpv(item *CygxArticleHistoryRecordNewpv) (lastId int64, err error) {
  27. o, err := orm.NewOrm().Begin()
  28. if err != nil {
  29. return
  30. }
  31. defer func() {
  32. fmt.Println(err)
  33. if err == nil {
  34. o.Commit()
  35. } else {
  36. o.Rollback()
  37. }
  38. }()
  39. userSeller, err := company.GetCompanyProductListByUserId(item.UserId)
  40. if err != nil {
  41. return
  42. }
  43. lastId, err = o.Insert(item)
  44. if err != nil {
  45. return
  46. }
  47. //写入记录到总的统计表
  48. record := new(CygxArticleHistoryRecordAll)
  49. record.UserId = item.UserId
  50. record.ArticleId = item.ArticleId
  51. record.CreateTime = item.CreateTime.Format(utils.FormatDateTime)
  52. record.ModifyTime = item.ModifyTime
  53. record.Mobile = item.Mobile
  54. record.Email = item.Email
  55. record.CompanyId = item.CompanyId
  56. record.CompanyName = item.CompanyName
  57. record.StopTime = item.StopTime
  58. record.OutType = item.OutType
  59. record.Source = item.Source
  60. record.CompanyStatus = userSeller.Status
  61. record.SellerName = userSeller.SellerName
  62. record.RegisterPlatform = utils.REGISTER_PLATFORM
  63. record.Platfor = 1
  64. lastId, err = o.Insert(record)
  65. if err != nil {
  66. return
  67. }
  68. // 软删除当天策略平台的文章阅读记录
  69. if item.Mobile != "" {
  70. sql := `UPDATE cygx_article_history_record_all
  71. SET is_del = 1
  72. WHERE
  73. article_id = ?
  74. AND mobile = ?
  75. AND platfor = 2
  76. AND create_time >= date(NOW()) `
  77. _, err = o.Raw(sql, record.ArticleId, record.Mobile).Exec()
  78. }
  79. return
  80. }
  81. // 获取最新的一条阅读记录
  82. func GetNewArticleHistoryRecordNewpv(uid, articleId int, modifytime string) (item *AddStopTimeNewRep, err error) {
  83. o := orm.NewOrm()
  84. sql := `SELECT * FROM cygx_article_history_record_newpv WHERE user_id = ? AND article_id = ? AND modify_time <='` + modifytime + `' ORDER BY id DESC LIMIT 1;`
  85. err = o.Raw(sql, uid, articleId).QueryRow(&item)
  86. return
  87. }
  88. // 把十分钟之内的阅读记录进行累加
  89. func UpdateCygxArticleViewRecordNewpv(itemRep *CygxArticleHistoryRecordNewpv, stopTime int) (err error) {
  90. o, err := orm.NewOrm().Begin()
  91. if err != nil {
  92. return
  93. }
  94. defer func() {
  95. fmt.Println(err)
  96. if err == nil {
  97. o.Commit()
  98. } else {
  99. o.Rollback()
  100. }
  101. }()
  102. sql := `UPDATE cygx_article_history_record_newpv
  103. SET modify_time = NOW(), stop_time = stop_time + ` + strconv.Itoa(stopTime) + `
  104. WHERE
  105. article_id = ?
  106. AND user_id = ?
  107. AND out_type = 2
  108. AND timestampdiff(MINUTE,modify_time,NOW()) < 10`
  109. _, err = o.Raw(sql, itemRep.ArticleId, itemRep.UserId).Exec()
  110. // 修改总表的停留时间
  111. sql = `UPDATE cygx_article_history_record_all
  112. SET modify_time = NOW(), stop_time = stop_time + ` + strconv.Itoa(stopTime) + `
  113. WHERE
  114. article_id = ?
  115. AND user_id = ?
  116. AND out_type = 2
  117. AND timestampdiff(MINUTE,modify_time,NOW()) < 10`
  118. _, err = o.Raw(sql, itemRep.ArticleId, itemRep.UserId).Exec()
  119. return
  120. }
  121. // 把十分钟之内的阅读记录进行累加
  122. func UpdateCygxArticleViewRecordNewpvList(itemRep *CygxArticleHistoryRecordNewpv, stopTime int) (err error) {
  123. o := orm.NewOrm()
  124. sql := `UPDATE cygx_article_history_record_newpv
  125. SET stop_time = stop_time + ` + strconv.Itoa(stopTime) + `
  126. WHERE
  127. article_id = ?
  128. AND user_id = ?
  129. AND modify_time = ?
  130. AND id = ?`
  131. _, err = o.Raw(sql, itemRep.ArticleId, itemRep.UserId, itemRep.ModifyTime, itemRep.Id).Exec()
  132. return
  133. }
  134. // 获取当天总表的阅读记录
  135. func GetArticleHistoryRecordAllList() (items []*CygxArticleHistoryRecordNewpv, err error) {
  136. o := orm.NewOrm()
  137. sql := ` SELECT * FROM cygx_article_history_record_all WHERE create_time >= date(NOW())
  138. AND mobile <> ''
  139. AND platfor = 1
  140. GROUP BY mobile,article_id `
  141. _, err = o.Raw(sql).QueryRows(&items)
  142. return
  143. }
  144. // 获取列表信息根据手机号分组
  145. func GetArticleHistoryRecordAllByMobileList(condition string) (items []*CygxArticleHistoryRecordAll, err error) {
  146. o := orm.NewOrm()
  147. sql := `SELECT * FROM cygx_article_history_record_all WHERE 1 = 1 ` + condition + ` GROUP BY mobile `
  148. _, err = o.Raw(sql).QueryRows(&items)
  149. return
  150. }
  151. // 修改用户阅读的相关信息
  152. func UpdateCygxArticleHistoryRecordAll(wxUser *WxUserItem) (err error) {
  153. o := orm.NewOrm()
  154. var sql string
  155. if wxUser.Mobile != "" {
  156. sql = `UPDATE cygx_article_history_record_all SET email=?,company_id=?,company_name=?,user_id=?,real_name=? WHERE mobile=? `
  157. _, err = o.Raw(sql, wxUser.Email, wxUser.CompanyId, wxUser.CompanyName, wxUser.UserId, wxUser.RealName, wxUser.Mobile).Exec()
  158. } else if wxUser.Email != "" {
  159. sql = `UPDATE cygx_article_history_record_all SET mobile=?,company_id=?,company_name=?,user_id=?,real_name=? WHERE email=? `
  160. _, err = o.Raw(sql, wxUser.Mobile, wxUser.CompanyId, wxUser.CompanyName, wxUser.UserId, wxUser.RealName, wxUser.Email).Exec()
  161. }
  162. return
  163. }
  164. type EsUserInteraction struct {
  165. Id int `description:"主键ID"`
  166. ArticleId int `description:"文章id"`
  167. ArticleType int `description:"文章类型 1:查研观向, 2:策略平台"`
  168. Title string `description:"标题"`
  169. PublishDate string `description:"发布时间"`
  170. CreateTime string `description:"创建时间"`
  171. StopTime string `description:"阅读停留时间"`
  172. RealName string `description:"姓名"`
  173. CompanyName string `description:"公司名称"`
  174. CompanyId int `description:"公司ID"`
  175. SellerName string `description:"所属销售"`
  176. SellerId int `description:"所属销售ID"`
  177. Mobile string `description:"手机号"`
  178. Email string `description:"邮箱"`
  179. UserId int `description:"用户ID"`
  180. UserArticleHistoryNum int `description:"用户阅读数量"`
  181. CompanyArticleHistoryNum int `description:"机构阅读数量"`
  182. }
  183. // 机构阅读记录列表
  184. func GetCygxArticleHistoryRecordByCompanyList(condition string, startSize, pageSize int) (items []*EsUserInteraction, err error) {
  185. o := orm.NewOrm()
  186. sql := ` SELECT
  187. r.id,
  188. art.title,
  189. art.article_id,
  190. art.article_id_md5,
  191. art.publish_date,
  192. art.category_name,
  193. r.create_time,
  194. r.mobile,
  195. r.user_id,
  196. r.company_name,
  197. cp.seller_name,
  198. cp.seller_id,
  199. cp.company_id,
  200. r.real_name,
  201. r.stop_time,
  202. ci.article_history_num AS company_article_history_num,
  203. ui.article_history_num AS user_article_history_num
  204. FROM
  205. cygx_article_history_record_all AS r
  206. INNER JOIN cygx_article AS art ON art.article_id = r.article_id
  207. INNER JOIN company_product AS cp ON cp.company_id = r.company_id
  208. AND cp.product_id = 2
  209. INNER JOIN cygx_company_interaction_num AS ci ON ci.company_id = r.company_id
  210. INNER JOIN cygx_user_interaction_num AS ui ON ui.user_id = r.user_id
  211. WHERE
  212. 1 = 1
  213. AND r.is_del = 0 ` + condition + ` GROUP BY r.id `
  214. if startSize > 0 || pageSize > 0 {
  215. sql += ` LIMIT ` + strconv.Itoa(startSize) + "," + strconv.Itoa(pageSize)
  216. }
  217. _, err = o.Raw(sql).QueryRows(&items)
  218. return
  219. }
  220. // 机构阅读记录列表
  221. func GetCygxArticleHistoryRecordByCompanyListNew(condition string, pars interface{}, startSize, pageSize int) (items []*EsUserInteraction, err error) {
  222. o := orm.NewOrm()
  223. sql := ` SELECT
  224. r.id,
  225. art.title,
  226. art.article_id,
  227. art.article_id_md5,
  228. art.publish_date,
  229. art.category_name,
  230. r.create_time,
  231. r.mobile,
  232. r.user_id,
  233. r.company_name,
  234. r.company_id,
  235. r.real_name,
  236. r.stop_time,
  237. ci.article_history_num AS company_article_history_num,
  238. ui.article_history_num AS user_article_history_num
  239. FROM
  240. cygx_article_history_record_all AS r
  241. INNER JOIN cygx_article AS art ON art.article_id = r.article_id
  242. LEFT JOIN cygx_company_interaction_num AS ci ON ci.company_id = r.company_id
  243. LEFT JOIN cygx_user_interaction_num AS ui ON ui.user_id = r.user_id
  244. WHERE
  245. 1 = 1
  246. AND r.is_del = 0 ` + condition + ` GROUP BY r.id `
  247. if startSize > 0 || pageSize > 0 {
  248. sql += ` LIMIT ` + strconv.Itoa(startSize) + "," + strconv.Itoa(pageSize)
  249. }
  250. _, err = o.Raw(sql, pars).QueryRows(&items)
  251. return
  252. }
  253. // 获取阅读记录数量
  254. func GetCygxArticleHistoryCountByCompany(condition string) (count int, err error) {
  255. o := orm.NewOrm()
  256. sqlCount := `SELECT
  257. COUNT( 1 ) AS count
  258. FROM
  259. (
  260. SELECT
  261. COUNT( 1 )
  262. FROM
  263. cygx_article_history_record_all AS r
  264. INNER JOIN cygx_article AS art ON art.article_id = r.article_id
  265. INNER JOIN company_product AS cp ON cp.company_id = r.company_id
  266. AND cp.product_id = 2
  267. INNER JOIN cygx_company_interaction_num AS ci ON ci.company_id = r.company_id
  268. INNER JOIN cygx_user_interaction_num AS ui ON ui.user_id = r.user_id
  269. WHERE
  270. r.is_del = 0 ` + condition + `
  271. GROUP BY
  272. r.id
  273. ) AS count `
  274. err = o.Raw(sqlCount).QueryRow(&count)
  275. return
  276. }
  277. type CygxArticleHistoryAllTopResp struct {
  278. Pv int `description:"阅读PV"`
  279. ArticleId int `description:"文章id"`
  280. Num int `description:"数量"`
  281. }
  282. // 获取近15天之内的阅读数据最多的15报告
  283. func GetCygxArticleHistoryAllTop(pars []interface{}, condition string) (items []*CygxArticleHistoryAllTopResp, err error) {
  284. o := orm.NewOrm()
  285. sql := ` SELECT
  286. COUNT( 1 ) AS pv,
  287. a.article_id
  288. FROM
  289. cygx_article_history_record_all AS l
  290. INNER JOIN cygx_article AS a ON a.article_id = l.article_id
  291. INNER JOIN cygx_report_mapping AS m ON m.category_id = a.category_id
  292. WHERE
  293. 1 = 1
  294. AND l.platfor = 1` + condition + `
  295. GROUP BY
  296. l.article_id
  297. ORDER BY
  298. pv DESC ,a.publish_date DESC
  299. LIMIT 15 `
  300. _, err = o.Raw(sql, pars).QueryRows(&items)
  301. return
  302. }
  303. // 列表
  304. func GetCygxArticleHistoryRecordNewpvList(condition string, pars []interface{}) (items []*CygxArticleHistoryRecordNewpv, err error) {
  305. o := orm.NewOrm()
  306. sql := `SELECT * FROM cygx_article_history_record_newpv as art WHERE 1= 1 `
  307. if condition != "" {
  308. sql += condition
  309. }
  310. _, err = o.Raw(sql, pars).QueryRows(&items)
  311. return
  312. }
  313. type ListPvUvResp struct {
  314. ArticleId int `description:"文章ID"`
  315. Pv int `description:"pv"`
  316. Uv int `description:"pv"`
  317. }
  318. // 列表
  319. func GetCygxArticleHistoryRecordNewpvListPv(condition string, pars []interface{}) (items []*ListPvUvResp, err error) {
  320. o := orm.NewOrm()
  321. sql := `SELECT
  322. COUNT( 1 ) AS pv,
  323. article_id
  324. FROM
  325. cygx_article_history_record_newpv WHERE 1 = 1 `
  326. if condition != "" {
  327. sql += condition
  328. }
  329. sql += ` GROUP BY article_id `
  330. _, err = o.Raw(sql, pars).QueryRows(&items)
  331. return
  332. }
  333. // 查研PV列表
  334. func GetCygxArticleHistoryRecordNewpvListPvCy(condition string, pars []interface{}) (items []*ListPvUvResp, err error) {
  335. o := orm.NewOrm()
  336. sql := `SELECT
  337. COUNT( 1 ) AS pv,
  338. article_id
  339. FROM
  340. cygx_article_history_record_all WHERE 1 = 1 AND platfor = 1 `
  341. if condition != "" {
  342. sql += condition
  343. }
  344. sql += ` GROUP BY article_id `
  345. _, err = o.Raw(sql, pars).QueryRows(&items)
  346. return
  347. }
  348. // 策略PV列表
  349. func GetCygxArticleHistoryRecordNewpvListPvCl(condition string, pars []interface{}) (items []*ListPvUvResp, err error) {
  350. o := orm.NewOrm()
  351. sql := `SELECT
  352. COUNT( 1 ) AS pv,
  353. article_id
  354. FROM
  355. cygx_article_history_record_all WHERE 1 = 1 AND platfor = 2 AND is_del = 0 `
  356. if condition != "" {
  357. sql += condition
  358. }
  359. sql += ` GROUP BY article_id `
  360. _, err = o.Raw(sql, pars).QueryRows(&items)
  361. return
  362. }
  363. // 获取数量
  364. func GetCygxArticleHistoryRecordAllCount(condition string, pars []interface{}) (count int, err error) {
  365. sqlCount := ` SELECT COUNT(1) AS count FROM cygx_article_history_record_all WHERE 1= 1 `
  366. if condition != "" {
  367. sqlCount += condition
  368. }
  369. o := orm.NewOrm()
  370. err = o.Raw(sqlCount, pars).QueryRow(&count)
  371. return
  372. }