scrm-go

cards_service.go 4.7KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142
  1. package member_service
  2. import (
  3. "SCRM/models"
  4. "SCRM/service"
  5. "time"
  6. "github.com/jinzhu/gorm"
  7. )
  8. func GetCardList(orgID int64) (cards []*models.UserMembershipCard, err error) {
  9. err = service.UserReadDB().Model(&models.UserMembershipCard{}).Where("status=1 and user_org_id=?", orgID).Find(&cards).Error
  10. return
  11. }
  12. func GetCardByID(orgID, cardID int64) (*models.UserMembershipCard, error) {
  13. var card models.UserMembershipCard
  14. var err error
  15. err = service.UserReadDB().Model(&models.UserMembershipCard{}).Where("id=? and user_org_id=? and status=1", cardID, orgID).First(&card).Error
  16. if err == gorm.ErrRecordNotFound {
  17. return nil, nil
  18. }
  19. if err != nil {
  20. return nil, err
  21. }
  22. return &card, nil
  23. }
  24. func GetUserCardByID(orgID, customerID int64) (*models.UserCard, error) {
  25. var card models.UserCard
  26. var err error
  27. err = service.UserReadDB().Model(&models.UserCard{}).Where("user_org_id=? and customer_id=? and status=1", orgID, customerID).First(&card).Error
  28. if err == gorm.ErrRecordNotFound {
  29. return nil, nil
  30. }
  31. if err != nil {
  32. return nil, err
  33. }
  34. return &card, nil
  35. }
  36. func DeleteUserCard(orgID, customerID int64) (err error) {
  37. err = service.UserWriteDB().Model(&models.UserCard{}).Where("user_org_id=? and customer_id=? ", orgID, customerID).Update(map[string]interface{}{"Status": 0, "UpdatedTime": time.Now().Unix()}).Error
  38. return
  39. }
  40. func SaveUserCard(card *models.UserCard, member *Members) (err error) {
  41. if member.Membership == 1 {
  42. err = service.UserWriteDB().Save(card).Error
  43. } else {
  44. tx := service.UserWriteDB().Begin()
  45. err = service.UserWriteDB().Save(card).Error
  46. if err != nil {
  47. tx.Rollback()
  48. return
  49. }
  50. err = service.UserWriteDB().Model(&models.UserCustomer{}).Where("id=? and user_org_id=? ", member.ID, member.UserOrgId).Update(map[string]interface{}{"Membership": 1, "UpdatedTime": time.Now().Unix()}).Error
  51. if err != nil {
  52. tx.Rollback()
  53. return
  54. }
  55. tx.Commit()
  56. }
  57. return
  58. }
  59. func FindMemberCardByName(orgID int64, name string) (*models.UserMembershipCard, error) {
  60. var card models.UserMembershipCard
  61. var err error
  62. err = service.UserReadDB().Model(&models.UserMembershipCard{}).Where("card_name = ? AND user_org_id=? AND status=1", name, orgID).First(&card).Error
  63. if err == gorm.ErrRecordNotFound {
  64. return nil, nil
  65. }
  66. if err != nil {
  67. return nil, err
  68. }
  69. return &card, nil
  70. }
  71. func SaveMemberCard(card *models.UserMembershipCard) (err error) {
  72. err = service.UserWriteDB().Save(card).Error
  73. return
  74. }
  75. func FindOnlyMemberCardByID(orgID, id int64) (*models.UserMembershipCard, error) {
  76. var card models.UserMembershipCard
  77. var err error
  78. err = service.UserReadDB().Model(&models.UserMembershipCard{}).Where("id = ? AND user_org_id=? AND status=1", id, orgID).First(&card).Error
  79. if err == gorm.ErrRecordNotFound {
  80. return nil, nil
  81. }
  82. if err != nil {
  83. return nil, err
  84. }
  85. return &card, nil
  86. }
  87. func GetMemebrCardCount(orgID int64) (count int64, err error) {
  88. err = service.UserReadDB().Model(&models.UserMembershipCard{}).Where("user_org_id=? AND status=1", orgID).Count(&count).Error
  89. return
  90. }
  91. func GetCardUserCount(orgID, id int64) (count int64, err error) {
  92. err = service.UserReadDB().Table("sgj_user_user_card as uc").Joins("JOIN sgj_user_customer as c ON c.id=uc.customer_id AND uc.user_org_id=? AND c.user_org_id=? AND uc.card_id=?", orgID, orgID, id).Where("uc.card_id=? AND uc.status=1 AND c.status=1", id).Count(&count).Error
  93. return
  94. }
  95. func GetCardsUserCount(orgID int64, ids []int64) (counts []*CardsMemberCount, err error) {
  96. err = service.UserReadDB().Table("sgj_user_user_card as uc").Joins("JOIN sgj_user_customer as c ON c.id=uc.customer_id AND uc.user_org_id=? AND c.user_org_id=?", orgID, orgID).Where("uc.card_id IN (?) AND uc.status=1 AND c.status=1", ids).Select("uc.card_id, COUNT(uc.card_id) AS member_count").Group("uc.card_id").Scan(&counts).Error
  97. return
  98. }
  99. func DeleteMemberShipCards(orgID int64, ids []int64) (err error) {
  100. if len(ids) == 1 {
  101. err = service.UserWriteDB().Model(&models.UserMembershipCard{}).Where("id =? and user_org_id =?", ids[0], orgID).Update(map[string]interface{}{"Status": 0, "UpdatedTime": time.Now().Unix()}).Error
  102. } else {
  103. err = service.UserWriteDB().Model(&models.UserMembershipCard{}).Where("id IN (?) and user_org_id =?", ids, orgID).Update(map[string]interface{}{"Status": 0, "UpdatedTime": time.Now().Unix()}).Error
  104. }
  105. return
  106. }
  107. func GetMembershipRights(orgID int64) (*models.MembershipRights, error) {
  108. var right models.MembershipRights
  109. err := service.UserReadDB().Model(&models.MembershipRights{}).Where("user_org_id=?", orgID).First(&right).Error
  110. if err == gorm.ErrRecordNotFound {
  111. return nil, nil
  112. }
  113. if err != nil {
  114. return nil, err
  115. }
  116. return &right, nil
  117. }
  118. func SaveMembershipRight(right *models.MembershipRights) (err error) {
  119. err = service.UserWriteDB().Save(right).Error
  120. return
  121. }