123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142 |
- package member_service
-
- import (
- "SCRM/models"
- "SCRM/service"
- "time"
-
- "github.com/jinzhu/gorm"
- )
-
- func GetCardList(orgID int64) (cards []*models.UserMembershipCard, err error) {
- err = service.UserReadDB().Model(&models.UserMembershipCard{}).Where("status=1 and user_org_id=?", orgID).Find(&cards).Error
- return
- }
-
- func GetCardByID(orgID, cardID int64) (*models.UserMembershipCard, error) {
- var card models.UserMembershipCard
- var err error
- err = service.UserReadDB().Model(&models.UserMembershipCard{}).Where("id=? and user_org_id=? and status=1", cardID, orgID).First(&card).Error
- if err == gorm.ErrRecordNotFound {
- return nil, nil
- }
- if err != nil {
- return nil, err
- }
- return &card, nil
- }
-
- func GetUserCardByID(orgID, customerID int64) (*models.UserCard, error) {
- var card models.UserCard
- var err error
- err = service.UserReadDB().Model(&models.UserCard{}).Where("user_org_id=? and customer_id=? and status=1", orgID, customerID).First(&card).Error
- if err == gorm.ErrRecordNotFound {
- return nil, nil
- }
- if err != nil {
- return nil, err
- }
- return &card, nil
- }
-
- func DeleteUserCard(orgID, customerID int64) (err error) {
- 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
- return
- }
-
- func SaveUserCard(card *models.UserCard, member *Members) (err error) {
- if member.Membership == 1 {
- err = service.UserWriteDB().Save(card).Error
- } else {
- tx := service.UserWriteDB().Begin()
- err = service.UserWriteDB().Save(card).Error
- if err != nil {
- tx.Rollback()
- return
- }
-
- 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
- if err != nil {
- tx.Rollback()
- return
- }
- tx.Commit()
- }
-
- return
- }
-
- func FindMemberCardByName(orgID int64, name string) (*models.UserMembershipCard, error) {
- var card models.UserMembershipCard
- var err error
- err = service.UserReadDB().Model(&models.UserMembershipCard{}).Where("card_name = ? AND user_org_id=? AND status=1", name, orgID).First(&card).Error
- if err == gorm.ErrRecordNotFound {
- return nil, nil
- }
- if err != nil {
- return nil, err
- }
- return &card, nil
- }
-
- func SaveMemberCard(card *models.UserMembershipCard) (err error) {
- err = service.UserWriteDB().Save(card).Error
- return
- }
-
- func FindOnlyMemberCardByID(orgID, id int64) (*models.UserMembershipCard, error) {
- var card models.UserMembershipCard
- var err error
- err = service.UserReadDB().Model(&models.UserMembershipCard{}).Where("id = ? AND user_org_id=? AND status=1", id, orgID).First(&card).Error
- if err == gorm.ErrRecordNotFound {
- return nil, nil
- }
- if err != nil {
- return nil, err
- }
- return &card, nil
- }
-
- func GetMemebrCardCount(orgID int64) (count int64, err error) {
- err = service.UserReadDB().Model(&models.UserMembershipCard{}).Where("user_org_id=? AND status=1", orgID).Count(&count).Error
- return
- }
-
- func GetCardUserCount(orgID, id int64) (count int64, err error) {
- 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
-
- return
- }
-
- func GetCardsUserCount(orgID int64, ids []int64) (counts []*CardsMemberCount, err error) {
- 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
-
- return
- }
-
- func DeleteMemberShipCards(orgID int64, ids []int64) (err error) {
- if len(ids) == 1 {
- 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
- } else {
- 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
- }
- return
- }
-
- func GetMembershipRights(orgID int64) (*models.MembershipRights, error) {
- var right models.MembershipRights
- err := service.UserReadDB().Model(&models.MembershipRights{}).Where("user_org_id=?", orgID).First(&right).Error
- if err == gorm.ErrRecordNotFound {
- return nil, nil
- }
- if err != nil {
- return nil, err
- }
- return &right, nil
- }
-
- func SaveMembershipRight(right *models.MembershipRights) (err error) {
- err = service.UserWriteDB().Save(right).Error
- return
- }
|