user_label.go 3.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115
  1. package models
  2. import (
  3. "github.com/beego/beego/v2/client/orm"
  4. "time"
  5. )
  6. type CygxUserLabelLogRedis struct {
  7. UserId int `description:"用户ID"`
  8. SourceId int `description:"资源ID"`
  9. SourceType int `description:"1:文章阅读、 2产业关注、3:活动到会、4系列关注、5专项调研活动到会。"`
  10. IsFllow int `description:"1关注、0取消关注"`
  11. CreateTime time.Time `description:"创建时间"`
  12. }
  13. type CygxUserLabel struct {
  14. Id int `orm:"column(id);pk"`
  15. UserId int `description:"用户ID"`
  16. CompanyId int `description:"公司id"`
  17. RealName string `description:"用户实际名称"`
  18. Mobile string `description:"手机号"`
  19. Email string `description:"邮箱"`
  20. Label string `description:"标签内容"`
  21. Weight int `description:"权重"`
  22. SourceId int `description:"来源ID(产业ID,系列ID)"`
  23. Source int `description:"来源1:产业、2:系列"`
  24. IsFollow int `description:"是否关注,1是,0否"`
  25. CreateTime time.Time `description:"创建时间"`
  26. ModifyTime time.Time `description:"更新时间"`
  27. }
  28. // 获取数量
  29. func GetCygxUserLabelCount(condition string, pars []interface{}) (count int, err error) {
  30. sqlCount := ` SELECT COUNT(1) AS count FROM cygx_user_label as art WHERE 1= 1 AND modify_time > DATE_SUB(CURDATE(), INTERVAL 3 MONTH) `
  31. if condition != "" {
  32. sqlCount += condition
  33. }
  34. o := orm.NewOrm()
  35. err = o.Raw(sqlCount, pars).QueryRow(&count)
  36. return
  37. }
  38. // 添加
  39. func AddCygxUserLabel(item *CygxUserLabel) (lastId int64, err error) {
  40. o := orm.NewOrm()
  41. lastId, err = o.Insert(item)
  42. return
  43. }
  44. // 批量添加
  45. func CygxUserLabelMulti(items []*CygxUserLabel) (err error) {
  46. o := orm.NewOrm()
  47. if len(items) > 0 {
  48. //批量添加新的记录
  49. _, err = o.InsertMulti(len(items), items)
  50. }
  51. return
  52. }
  53. // 修改是否关注
  54. func UpdateCygxUserLabelIsFollow(isFollow, sourceId, source, userId int, label string) (err error) {
  55. o := orm.NewOrm()
  56. sql := `UPDATE cygx_user_label SET is_follow=?,label = ? , modify_time=NOW() WHERE source_id=? AND source = ? AND user_id = ? `
  57. _, err = o.Raw(sql, isFollow, label, sourceId, source, userId).Exec()
  58. return
  59. }
  60. // 修改对应标签权重+1
  61. func UpdateCygxUserLabelWeight(sourceId, source, userId int, label string) (err error) {
  62. o := orm.NewOrm()
  63. sql := `UPDATE cygx_user_label SET weight = weight +1 ,label = ? , modify_time=NOW() WHERE source_id=? AND source = ? AND user_id = ? `
  64. _, err = o.Raw(sql, label, sourceId, source, userId).Exec()
  65. return
  66. }
  67. // 把所有用户标签权重设置为0
  68. func UpdateCygxUserLabelWeightAll() (err error) {
  69. o := orm.NewOrm()
  70. sql := `UPDATE cygx_user_label SET weight = 0 `
  71. _, err = o.Raw(sql).Exec()
  72. return
  73. }
  74. // 列表
  75. func GetCygxUserLabelList(condition string, pars []interface{}) (items []*CygxUserLabel, err error) {
  76. o := orm.NewOrm()
  77. sql := `SELECT * FROM cygx_user_label as art WHERE 1= 1 `
  78. if condition != "" {
  79. sql += condition
  80. }
  81. _, err = o.Raw(sql, pars).QueryRows(&items)
  82. return
  83. }
  84. // UpdateCygxUserLabelMulti 批量修改用户标签权重
  85. func UpdateCygxUserLabelMulti(items []*CygxUserLabel) (err error) {
  86. o := orm.NewOrm()
  87. p, err := o.Raw(` UPDATE cygx_user_label SET weight = ? WHERE user_id = ? AND label = ? `).Prepare()
  88. if err != nil {
  89. return
  90. }
  91. defer func() {
  92. _ = p.Close() // 别忘记关闭 statement
  93. }()
  94. for _, v := range items {
  95. _, err = p.Exec(
  96. v.Weight,
  97. v.UserId,
  98. v.Label)
  99. if err != nil {
  100. return
  101. }
  102. }
  103. return
  104. }