user_label.go 3.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116
  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. sqlCount := ` SELECT COUNT(1) AS count FROM cygx_user_label as art WHERE 1= 1 `
  32. if condition != "" {
  33. sqlCount += condition
  34. }
  35. o := orm.NewOrm()
  36. err = o.Raw(sqlCount, pars).QueryRow(&count)
  37. return
  38. }
  39. // 添加
  40. func AddCygxUserLabel(item *CygxUserLabel) (lastId int64, err error) {
  41. o := orm.NewOrm()
  42. lastId, err = o.Insert(item)
  43. return
  44. }
  45. // 批量添加
  46. func CygxUserLabelMulti(items []*CygxUserLabel) (err error) {
  47. o := orm.NewOrm()
  48. if len(items) > 0 {
  49. //批量添加新的记录
  50. _, err = o.InsertMulti(len(items), items)
  51. }
  52. return
  53. }
  54. // 修改是否关注
  55. func UpdateCygxUserLabelIsFollow(isFollow, sourceId, source, userId int, label string) (err error) {
  56. o := orm.NewOrm()
  57. sql := `UPDATE cygx_user_label SET is_follow=?,label = ? , modify_time=NOW() WHERE source_id=? AND source = ? AND user_id = ? `
  58. _, err = o.Raw(sql, isFollow, label, sourceId, source, userId).Exec()
  59. return
  60. }
  61. // 修改对应标签权重+1
  62. func UpdateCygxUserLabelWeight(sourceId, source, userId int, label string) (err error) {
  63. o := orm.NewOrm()
  64. sql := `UPDATE cygx_user_label SET weight = weight +1 ,label = ? , modify_time=NOW() WHERE source_id=? AND source = ? AND user_id = ? `
  65. _, err = o.Raw(sql, label, sourceId, source, userId).Exec()
  66. return
  67. }
  68. // 把所有用户标签权重设置为0
  69. func UpdateCygxUserLabelWeightAll() (err error) {
  70. o := orm.NewOrm()
  71. sql := `UPDATE cygx_user_label SET weight = 0 `
  72. _, err = o.Raw(sql).Exec()
  73. return
  74. }
  75. // 列表
  76. func GetCygxUserLabelList(condition string, pars []interface{}) (items []*CygxUserLabel, err error) {
  77. o := orm.NewOrm()
  78. sql := `SELECT * FROM cygx_user_label as art WHERE 1= 1 `
  79. if condition != "" {
  80. sql += condition
  81. }
  82. _, err = o.Raw(sql, pars).QueryRows(&items)
  83. return
  84. }
  85. // UpdateCygxUserLabelMulti 批量修改用户标签权重
  86. func UpdateCygxUserLabelMulti(items []*CygxUserLabel) (err error) {
  87. o := orm.NewOrm()
  88. p, err := o.Raw(` UPDATE cygx_user_label SET weight = ? WHERE user_id = ? AND label = ? `).Prepare()
  89. if err != nil {
  90. return
  91. }
  92. defer func() {
  93. _ = p.Close() // 别忘记关闭 statement
  94. }()
  95. for _, v := range items {
  96. _, err = p.Exec(
  97. v.Weight,
  98. v.UserId,
  99. v.Label)
  100. if err != nil {
  101. return
  102. }
  103. }
  104. return
  105. }