123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202 |
- package member_service
-
- import (
- "SCRM/models"
- "SCRM/service"
- "strings"
-
- "time"
-
- "fmt"
- "github.com/jinzhu/gorm"
- )
-
- func GetMemberList(orgID, page, limit, level, source, tag, startTime, endTime int64, isStartTime, isEndTime bool, searchKey string) (members []*Members, total int64, err error) {
- db := service.UserReadDB().Table("sgj_user_customer as c").Where("c.status=1")
- if orgID > 0 {
- db = db.Where("c.user_org_id=?", orgID)
- }
- if source > 0 {
- db = db.Where("c.sources=?", source)
- }
- if isStartTime {
- db = db.Where("c.created_time>=?", startTime)
- }
- if isEndTime {
- db = db.Where("c.created_time<=?", endTime)
- }
- if len(searchKey) > 0 {
- searchKey = "%" + searchKey + "%"
- db = db.Where("c.name LIKE ? OR c.mobile LIKE ? ", searchKey, searchKey)
- }
- if level > 0 {
- 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)
- }
- if tag > 0 {
- 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)
- }
-
- offset := (page - 1) * limit
- err = db.Count(&total).Order("c.created_time desc").Offset(offset).Limit(limit).
- Preload("Tags", func(db *gorm.DB) *gorm.DB {
- 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")
- }).
- Preload("UserCard", func(db *gorm.DB) *gorm.DB {
- return db.Where("user_org_id=? and status=1", orgID)
- }).
- Preload("UserCard.Card", "user_org_id=? and status=1", orgID).
- Preload("Illness", "user_org_id=? and status=1", orgID).
- 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
- if err != nil {
- return
- }
- if len(members) > 0 {
- for _, member := range members {
- member.Avatar = strings.Replace(member.Avatar, "http://7xkofe.com1.z0.glb.clouddn.com", "https://images.shengws.com", 1)
- }
- }
-
- return
- }
-
- func GetCustomerByMobile(orgID int64, mobile string) (*models.UserCustomer, error) {
- var customer models.UserCustomer
- var err error
- err = service.UserReadDB().Where("user_org_id=? and mobile=? and status=1", orgID, mobile).Find(&customer).Error
- if err == gorm.ErrRecordNotFound {
- return nil, nil
- }
- if err != nil {
- return nil, err
- }
- return &customer, nil
- }
-
- func GetUserByMobile(mobile string) (*models.User, error) {
- var user models.User
- var err error
- err = service.UserReadDB().Where("mobile=?", mobile).Find(&user).Error
- if err == gorm.ErrRecordNotFound {
- return nil, nil
- }
- if err != nil {
- return nil, err
- }
- return &user, nil
- }
-
- func GetOnlyMemberByID(orgID, id int64) (*Members, error) {
- var member Members
- var err error
- err = service.UserReadDB().Where("id =? and user_org_id=? and status=1", id, orgID).Find(&member).Error
- if err == gorm.ErrRecordNotFound {
- return nil, nil
- }
- if err != nil {
- return nil, err
- }
- return &member, nil
- }
-
- func CreateMember(member *Members) (err error) {
- err = service.UserWriteDB().Create(member).Error
- return
- }
-
- func EditMember(member *Members) (err error) {
- err = service.UserWriteDB().Save(member).Error
- return
- }
-
- func CreateMemberWithUser(member *Members, user *models.User) (err error) {
- tx := service.UserWriteDB().Begin()
- err = tx.Create(user).Error
- if err != nil {
- tx.Rollback()
- return
- }
- member.UserId = user.ID
- err = tx.Create(member).Error
- if err != nil {
- tx.Rollback()
- return
- }
-
- tx.Commit()
-
- return
- }
-
- func DeleteMembers(orgID int64, ids []int64) (err error) {
- if len(ids) == 1 {
- 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
- } else {
- 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
- }
- return
- }
-
- func FindCustomerInfoByOpenid(orgID int64, openid string) (*models.UserCustomer, error) {
- var cusotmer models.UserCustomer
- err := service.UserReadDB().Where("user_org_id = ? and wechat_openid =? and status=1", orgID, openid).First(&cusotmer).Error
- if err == gorm.ErrRecordNotFound {
- return nil, nil
- }
- if err != nil {
- return nil, err
- }
- return &cusotmer, nil
- }
-
- // 获取所有有手机号的客户(仅取部分信息!)
- func GetAllCustomersWithMobile(orgUserID int64) ([]*models.UserCustomer, error) {
- var customer []*models.UserCustomer
- err := service.UserReadDB().Table("sgj_user_customer AS uc").
- Where("uc.user_org_id = ? AND uc.status=1 and uc.mobile <>'' ", orgUserID).
- Select("uc.id, uc.name, uc.mobile").
- Scan(&customer).
- Error
- return customer, err
- }
-
- // 获取指定标签下有手机号的客户(仅取部分信息!)
- func GetTagCustomersWithMobileByTagIDs(orgID int64, tagIDs []string) ([]*models.UserCustomer, error) {
- var customer []*models.UserCustomer
- err := service.UserReadDB().Table("sgj_user_customer AS uc").
- Joins("JOIN sgj_user_tag_links AS tl on uc.id = tl.user_link_id").
- Where("tl.tag_id IN (?) and tl.status = 1 and uc.user_org_id = ? and uc.status=1 and uc.mobile <>'' ", tagIDs, orgID).
- Group("uc.id").
- Select("uc.id, uc.name, uc.mobile").
- Scan(&customer).
- Error
- return customer, err
- }
-
- // 获取指定客户ID的且有手机号的客户(仅取部分信息!)
- func GetSpecificCustomersWithMobile(orgID int64, customerIDs []string) ([]*models.UserCustomer, error) {
- var customer []*models.UserCustomer
- err := service.UserReadDB().Table("sgj_user_customer AS uc").
- Where("uc.user_org_id = ? AND uc.status=1 and uc.mobile <>'' and id in (?)", orgID, customerIDs).
- Select("uc.id, uc.name, uc.mobile").
- Scan(&customer).
- Error
- return customer, err
- }
-
- func GetAllMemberList(orgID int64) (members []*Members, err error) {
- db := service.UserReadDB().Table("sgj_user_customer as c").Where("c.user_org_id=? and c.status=1", orgID)
-
- err = db.Order("c.created_time desc").
- 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
- fmt.Println("错误是什么", err)
- if err != nil {
- return
- }
- if len(members) > 0 {
- for _, member := range members {
- member.Avatar = strings.Replace(member.Avatar, "http://7xkofe.com1.z0.glb.clouddn.com", "https://images.shengws.com", 1)
- }
- }
- return
- }
|