scrm-go

member_service.go 6.8KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202
  1. package member_service
  2. import (
  3. "SCRM/models"
  4. "SCRM/service"
  5. "strings"
  6. "time"
  7. "github.com/jinzhu/gorm"
  8. "fmt"
  9. )
  10. func GetMemberList(orgID, page, limit, level, source, tag, startTime, endTime int64, isStartTime, isEndTime bool, searchKey string) (members []*Members, total int64, err error) {
  11. db := service.UserReadDB().Table("sgj_user_customer as c").Where("c.status=1")
  12. if orgID > 0 {
  13. db = db.Where("c.user_org_id=?", orgID)
  14. }
  15. if source > 0 {
  16. db = db.Where("c.sources=?", source)
  17. }
  18. if isStartTime {
  19. db = db.Where("c.created_time>=?", startTime)
  20. }
  21. if isEndTime {
  22. db = db.Where("c.created_time<=?", endTime)
  23. }
  24. if len(searchKey) > 0 {
  25. searchKey = "%" + searchKey + "%"
  26. db = db.Where("c.name LIKE ? OR c.mobile LIKE ? ", searchKey, searchKey)
  27. }
  28. if level > 0 {
  29. db = db.Joins("JOIN sgj_user_user_card as uc ON uc.customer_id = c.id and uc.user_org_id = ? and uc.status=1 and uc.card_id=? ", orgID, level)
  30. }
  31. if tag > 0 {
  32. db = db.Joins("JOIN sgj_user_tag_links as ut ON ut.user_link_id = c.id and ut.user_org_id = ? and ut.status=1 and ut.tag_id=? ", orgID, tag)
  33. }
  34. offset := (page - 1) * limit
  35. err = db.Count(&total).Order("c.created_time desc").Offset(offset).Limit(limit).
  36. Preload("Tags", func(db *gorm.DB) *gorm.DB {
  37. return db.Where("sgj_user_tag_links.user_org_id=? and sgj_user_tags.user_org_id=? and sgj_user_tags.status=1 and sgj_user_tag_links.status=1", orgID, orgID).Order("sgj_user_tag_links.id desc")
  38. }).
  39. Preload("UserCard", func(db *gorm.DB) *gorm.DB {
  40. return db.Where("user_org_id=? and status=1", orgID)
  41. }).
  42. Preload("UserCard.Card", "user_org_id=? and status=1", orgID).
  43. Preload("Illness", "user_org_id=? and status=1", orgID).
  44. Select("c.id, c.user_org_id, c.user_id, c.mobile, c.name, c.gender, c.province_id, c.city_id, c.address, c.birthday, c.treat_type, c.relationship, c.illness_id, c.wechat_openid, c.membership, c.sources, c.status, c.created_time, c.updated_time, c.avatar, c.wechat_unionid, c.remark, c.medical_diagnose, c.yz_uid, c.ill_date, c.district_id").Find(&members).Error
  45. if err != nil {
  46. return
  47. }
  48. if len(members) > 0 {
  49. for _, member := range members {
  50. member.Avatar = strings.Replace(member.Avatar, "http://7xkofe.com1.z0.glb.clouddn.com", "https://images.shengws.com", 1)
  51. }
  52. }
  53. return
  54. }
  55. func GetCustomerByMobile(orgID int64, mobile string) (*models.UserCustomer, error) {
  56. var customer models.UserCustomer
  57. var err error
  58. err = service.UserReadDB().Where("user_org_id=? and mobile=? and status=1", orgID, mobile).Find(&customer).Error
  59. if err == gorm.ErrRecordNotFound {
  60. return nil, nil
  61. }
  62. if err != nil {
  63. return nil, err
  64. }
  65. return &customer, nil
  66. }
  67. func GetUserByMobile(mobile string) (*models.User, error) {
  68. var user models.User
  69. var err error
  70. err = service.UserReadDB().Where("mobile=?", mobile).Find(&user).Error
  71. if err == gorm.ErrRecordNotFound {
  72. return nil, nil
  73. }
  74. if err != nil {
  75. return nil, err
  76. }
  77. return &user, nil
  78. }
  79. func GetOnlyMemberByID(orgID, id int64) (*Members, error) {
  80. var member Members
  81. var err error
  82. err = service.UserReadDB().Where("id =? and user_org_id=? and status=1", id, orgID).Find(&member).Error
  83. if err == gorm.ErrRecordNotFound {
  84. return nil, nil
  85. }
  86. if err != nil {
  87. return nil, err
  88. }
  89. return &member, nil
  90. }
  91. func CreateMember(member *Members) (err error) {
  92. err = service.UserWriteDB().Create(member).Error
  93. return
  94. }
  95. func EditMember(member *Members) (err error) {
  96. err = service.UserWriteDB().Save(member).Error
  97. return
  98. }
  99. func CreateMemberWithUser(member *Members, user *models.User) (err error) {
  100. tx := service.UserWriteDB().Begin()
  101. err = tx.Create(user).Error
  102. if err != nil {
  103. tx.Rollback()
  104. return
  105. }
  106. member.UserId = user.ID
  107. err = tx.Create(member).Error
  108. if err != nil {
  109. tx.Rollback()
  110. return
  111. }
  112. tx.Commit()
  113. return
  114. }
  115. func DeleteMembers(orgID int64, ids []int64) (err error) {
  116. if len(ids) == 1 {
  117. err = service.UserWriteDB().Model(&models.UserCustomer{}).Where("id =? and user_org_id =?", ids[0], orgID).Update(map[string]interface{}{"Status": 0, "UpdatedTime": time.Now().Unix()}).Error
  118. } else {
  119. err = service.UserWriteDB().Model(&models.UserCustomer{}).Where("id IN (?) and user_org_id =?", ids, orgID).Update(map[string]interface{}{"Status": 0, "UpdatedTime": time.Now().Unix()}).Error
  120. }
  121. return
  122. }
  123. func FindCustomerInfoByOpenid(orgID int64, openid string) (*models.UserCustomer, error) {
  124. var cusotmer models.UserCustomer
  125. err := service.UserReadDB().Where("user_org_id = ? and wechat_openid =? and status=1", orgID, openid).First(&cusotmer).Error
  126. if err == gorm.ErrRecordNotFound {
  127. return nil, nil
  128. }
  129. if err != nil {
  130. return nil, err
  131. }
  132. return &cusotmer, nil
  133. }
  134. // 获取所有有手机号的客户(仅取部分信息!)
  135. func GetAllCustomersWithMobile(orgUserID int64) ([]*models.UserCustomer, error) {
  136. var customer []*models.UserCustomer
  137. err := service.UserReadDB().Table("sgj_user_customer AS uc").
  138. Where("uc.user_org_id = ? AND uc.status=1 and uc.mobile <>'' ", orgUserID).
  139. Select("uc.id, uc.name, uc.mobile").
  140. Scan(&customer).
  141. Error
  142. return customer, err
  143. }
  144. // 获取指定标签下有手机号的客户(仅取部分信息!)
  145. func GetTagCustomersWithMobileByTagIDs(orgID int64, tagIDs []string) ([]*models.UserCustomer, error) {
  146. var customer []*models.UserCustomer
  147. err := service.UserReadDB().Table("sgj_user_customer AS uc").
  148. Joins("JOIN sgj_user_tag_links AS tl on uc.id = tl.user_link_id").
  149. Where("tl.tag_id IN (?) and tl.status = 1 and uc.user_org_id = ? and uc.status=1 and uc.mobile <>'' ", tagIDs, orgID).
  150. Group("uc.id").
  151. Select("uc.id, uc.name, uc.mobile").
  152. Scan(&customer).
  153. Error
  154. return customer, err
  155. }
  156. // 获取指定客户ID的且有手机号的客户(仅取部分信息!)
  157. func GetSpecificCustomersWithMobile(orgID int64, customerIDs []string) ([]*models.UserCustomer, error) {
  158. var customer []*models.UserCustomer
  159. err := service.UserReadDB().Table("sgj_user_customer AS uc").
  160. Where("uc.user_org_id = ? AND uc.status=1 and uc.mobile <>'' and id in (?)", orgID, customerIDs).
  161. Select("uc.id, uc.name, uc.mobile").
  162. Scan(&customer).
  163. Error
  164. return customer, err
  165. }
  166. func GetAllMemberList(orgID int64) (members []*Members, err error) {
  167. db := service.UserReadDB().Table("sgj_user_customer as c").Where("c.user_org_id=? and c.status=1", orgID)
  168. err = db.Order("c.created_time desc").
  169. Select("c.id, c.user_org_id, c.user_id, c.mobile, c.name, c.gender, c.province_id, c.city_id, c.address, c.birthday, c.treat_type, c.relationship, c.illness_id, c.wechat_openid, c.membership, c.sources, c.status, c.created_time, c.updated_time, c.avatar, c.wechat_unionid, c.remark, c.medical_diagnose, c.yz_uid, c.ill_date, c.district_id").Find(&members).Error
  170. fmt.Println("错误是什么",err)
  171. if err != nil {
  172. return
  173. }
  174. if len(members) > 0 {
  175. for _, member := range members {
  176. member.Avatar = strings.Replace(member.Avatar, "http://7xkofe.com1.z0.glb.clouddn.com", "https://images.shengws.com", 1)
  177. }
  178. }
  179. return
  180. }