user_seller_relation.go 6.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183
  1. package models
  2. import (
  3. "github.com/beego/beego/v2/client/orm"
  4. "time"
  5. )
  6. type UserSellerRelation struct {
  7. RelationId int64 `orm:"column(relation_id);pk"`
  8. UserId int `description:"用户id"`
  9. CompanyId int `description:"企业用户id"`
  10. SellerId int `description:"销售id"`
  11. Seller string `description:"销售员名称"`
  12. ProductId int `description:"产品id"`
  13. Mobile string `description:"手机号"`
  14. Email string `description:"邮箱"`
  15. ModifyTime time.Time `description:"修改时间"`
  16. CreateTime time.Time `description:"创建时间"`
  17. }
  18. // 添加销售员与员工的关系
  19. func AddUserSellerRelation(userId int64, companyId, sellerId, productId int, seller, mobile, email string) (lastId int64, err error) {
  20. o := orm.NewOrm()
  21. relation := UserSellerRelation{
  22. UserId: int(userId),
  23. SellerId: sellerId,
  24. CompanyId: companyId,
  25. Seller: seller,
  26. ProductId: productId,
  27. Mobile: mobile,
  28. Email: email,
  29. CreateTime: time.Now(),
  30. ModifyTime: time.Now(),
  31. }
  32. lastId, err = o.Insert(&relation)
  33. return
  34. }
  35. // 根据企业用户id修改所属销售
  36. func UpdateUserSellerRelationByCompanyId(companyId, productId, sellerId int, seller string) (err error) {
  37. o := orm.NewOrm()
  38. sql := `UPDATE user_seller_relation SET seller_id=?,seller = ? ,modify_time=NOW() WHERE company_id = ? AND product_id=?`
  39. _, err = o.Raw(sql, sellerId, seller, companyId, productId).Exec()
  40. return
  41. }
  42. // 删除销售员与员工的关系
  43. func DeleteUserSellerRelation(userId, sellerId int) (err error) {
  44. o := orm.NewOrm()
  45. sql := ` DELETE FROM user_seller_relation WHERE user_id=? and seller_id = ?`
  46. _, err = o.Raw(sql, userId, sellerId).Exec()
  47. return
  48. }
  49. // 根据产品id删除销售员与员工的关系
  50. func DeleteUserSellerRelationByProductId(userId, productId int) (err error) {
  51. o := orm.NewOrm()
  52. sql := ` DELETE FROM user_seller_relation WHERE user_id=? and product_id = ?`
  53. _, err = o.Raw(sql, userId, productId).Exec()
  54. return
  55. }
  56. // 根据联系人id删除所有销售员与该联系人的关系
  57. func DeleteUserSellerRelationByUserId(userId int) (err error) {
  58. o := orm.NewOrm()
  59. sql := ` DELETE FROM user_seller_relation WHERE user_id=? `
  60. _, err = o.Raw(sql, userId).Exec()
  61. return
  62. }
  63. // 根据用户id和销售员id获取关系
  64. func GetUserSellerRelation(userId int64, sellerId int) (item *UserSellerRelation, err error) {
  65. o := orm.NewOrm()
  66. sql := `SELECT * FROM user_seller_relation WHERE user_id = ? and seller_id= ?`
  67. err = o.Raw(sql, userId, sellerId).QueryRow(&item)
  68. return
  69. }
  70. // 获取用户的销售员(产品)的关系数量
  71. func GetUserSellerRelationCount(userId int) (count int, err error) {
  72. o := orm.NewOrm()
  73. sql := `SELECT COUNT(*) AS count
  74. FROM user_seller_relation WHERE user_id = ? `
  75. err = o.Raw(sql, userId).QueryRow(&count)
  76. return
  77. }
  78. // 通过用户id获取用户的销售员(产品)的关系
  79. func GetUserSellerRelationList(userId int) (list []*UserSellerRelation, err error) {
  80. o := orm.NewOrm()
  81. sql := `SELECT *
  82. FROM user_seller_relation WHERE user_id = ? `
  83. _, err = o.Raw(sql, userId).QueryRows(&list)
  84. return
  85. }
  86. // 用户与销售员的关系数量切片
  87. type UserSellerRelationSlice struct {
  88. Total int `description:"总阅读数"`
  89. UserId int `description:"用户id"`
  90. ProductIds string `description:"所属权限id"`
  91. }
  92. // 根据用户id字符串,获取用户与销售员的关系数量
  93. func GetUserSellerRelationCountByUserIds(userIds string) (items []*UserSellerRelationSlice, err error) {
  94. o := orm.NewOrm()
  95. sql := `select *,COUNT(1) AS total,GROUP_CONCAT( DISTINCT product_id SEPARATOR ',' ) AS product_ids from (SELECT user_id, product_id
  96. FROM user_seller_relation WHERE user_id in (` + userIds + `) group by user_id,product_id) b group by user_id`
  97. _, err = o.Raw(sql).QueryRows(&items)
  98. return
  99. }
  100. // 根据用户产品权限来获取名片数
  101. func GetCompanyUserBusinessCardCountByProductId(companyId, productId int) (count int, err error) {
  102. o := orm.NewOrm()
  103. sql := ` SELECT COUNT(1) AS count FROM user_seller_relation a
  104. left join wx_user b on a.user_id=b.user_id WHERE a.company_id =? and a.product_id=? AND b.business_card_url<>'' `
  105. err = o.Raw(sql, companyId, productId).QueryRow(&count)
  106. return
  107. }
  108. // GetCompanyUserSellerRelationByProductId 根据企业id和产品id获取所有用户
  109. func GetCompanyUserSellerRelationByProductId(companyId, productId int) (items []*UserSellerRelation, err error) {
  110. o := orm.NewOrm()
  111. sql := `SELECT * FROM user_seller_relation WHERE company_id = ? AND product_id=?`
  112. _, err = o.Raw(sql, companyId, productId).QueryRows(&items)
  113. return
  114. }
  115. // 根据企业id和产品id获取所有不是该产品id的所有用户
  116. func GetNotCompanyUserSellerRelationByProductId(companyId, productId int) (items []*UserSellerRelation, err error) {
  117. o := orm.NewOrm()
  118. sql := `SELECT user_id FROM user_seller_relation WHERE company_id = ? AND product_id != ?`
  119. _, err = o.Raw(sql, companyId, productId).QueryRows(&items)
  120. return
  121. }
  122. // 根据产品id删除某个客户下 销售员与员工的关系
  123. func DelCompanyUserSellerRelationByProductId(companyId, productId int) (err error) {
  124. o := orm.NewOrm()
  125. sql := ` DELETE FROM user_seller_relation WHERE company_id = ? AND product_id = ? `
  126. _, err = o.Raw(sql, companyId, productId).Exec()
  127. return
  128. }
  129. // 获取联系人分组信息
  130. type UserSellerRelationGroup struct {
  131. RelationId int64 `orm:"column(relation_id);pk"`
  132. UserId int `description:"用户id"`
  133. CompanyId int `description:"企业用户id"`
  134. SellerId int `description:"销售id"`
  135. Seller string `description:"销售员名称"`
  136. SellerRealName string `description:"销售员名称"`
  137. Status string `description:"产品权限状态"`
  138. ProductId int `description:"产品id"`
  139. Mobile string `description:"手机号"`
  140. Email string `description:"邮箱"`
  141. ModifyTime time.Time `description:"修改时间"`
  142. CreateTime time.Time `description:"创建时间"`
  143. }
  144. func GetUserGroupSellerByUserId(userId int) (item *UserSellerRelationGroup, err error) {
  145. o := orm.NewOrm()
  146. sql := ` SELECT a.*,GROUP_CONCAT(DISTINCT b.real_name SEPARATOR '/') AS seller_real_name,GROUP_CONCAT(DISTINCT c.status SEPARATOR '/') AS status
  147. FROM user_seller_relation AS a
  148. LEFT JOIN admin AS b ON a.seller_id=b.admin_id
  149. LEFT JOIN company_product AS c ON a.company_id=c.company_id
  150. WHERE a.user_id=? GROUP BY a.company_id`
  151. err = o.Raw(sql, userId).QueryRow(&item)
  152. return
  153. }
  154. // 根据联系人id获所有的分组信息
  155. func GetUserGroupSellersByUserId(userId int) (list []*UserSellerRelationGroup, err error) {
  156. o := orm.NewOrm()
  157. sql := ` SELECT a.*,b.real_name AS seller_real_name,c.status
  158. FROM user_seller_relation AS a
  159. LEFT JOIN admin AS b ON a.seller_id=b.admin_id
  160. LEFT JOIN company_product AS c ON a.company_id=c.company_id
  161. WHERE a.user_id=? `
  162. _, err = o.Raw(sql, userId).QueryRows(&list)
  163. return
  164. }