package services import ( "errors" "hongze/hongze_clpt/models" "hongze/hongze_clpt/utils" "strings" "time" ) var ERR_NO_USER_RECORD = errors.New("用户关系没有入库") var ERR_USER_NOT_BIND = errors.New("用户没有绑定") // 通过用户 关系表记录 和 用户记录 格式化返回 用户数据 func formatWxUserAndUserRecord(wxUser *models.WxUserItem, userRecord *models.UserRecord) { wxUser.OpenId = userRecord.OpenId wxUser.UnionId = userRecord.UnionId wxUser.NickName = userRecord.NickName //wxUser.RealName = userRecord.RealName //wxUser.BindAccount = userRecord.BindAccount wxUser.Headimgurl = userRecord.Headimgurl wxUser.SessionKey = userRecord.SessionKey } func GetWxUserItemByOpenId(unionId string) (item *models.WxUserItem, err error) { //通过openid获取用户关联信息 item = new(models.WxUserItem) item.UnionId = unionId // 先写入 unionId userRecord, userRecordErr := models.GetUserRecordByUnionId(unionId) if userRecordErr != nil && userRecordErr.Error() != utils.ErrNoRow() { err = userRecordErr return } //如果 userRecord 表中的手机号不为空,那么就通过手机号来获取详情 if userRecord != nil { if userRecord.BindAccount != "" { user, userErr := models.GetWxUserItemByUserMobile(userRecord.BindAccount) if userErr != nil && userErr.Error() != utils.ErrNoRow() { err = userErr return } if user != nil { item = user } } } return } // 获取 用户类型 //1、永续客户 //2、大套餐客户(4个行业全开通的正式客户) //3、分行业套餐客户(开通对应行业的正式客户) //4、仅开通专家套餐的正式客户 //5、开通对应行业套餐或专家套餐的试用客户;6、冻结客户;7、流失客户 func GetUserType(companyId int) (userType int, permissionStrnew string, err error) { var permissionStr, permissionZhengShiStr string if companyId <= 1 { userType = 0 } else { total, errs := models.GetCountCompanyDetailByIdGroup(companyId) if errs != nil { err = errs return } if total == 0 { userType = 0 } else { companyDetail, errs := models.GetCompanyDetailByIdGroup(companyId) if errs != nil { err = errs return } permissionStr, errs = models.GetCompanyPermission(companyId) if errs != nil { err = errs return } permissionStrnew = permissionStr //大套餐客户,数据库添加标识, companyUserTypeDetail, errs := models.GetCygxCompanyUserType(companyId) if errs != nil && errs.Error() != utils.ErrNoRow() { err = errs return } if companyUserTypeDetail != nil { if companyUserTypeDetail.CustomerTypeId != 0 { userType = companyUserTypeDetail.CustomerTypeId return } } permissionZhengShiStr, errs = models.GetCompanyPermissionByUserZhengShi(companyId) if errs != nil { err = errs return } //1、永续客户 //2、大套餐客户(4个行业全开通的正式客户) //3、分行业套餐客户(开通对应行业的正式客户) //4、仅开通专家套餐的正式客户 //5、开通对应行业套餐或专家套餐的试用客户、 10: 30W套餐客户 //大套餐客户定义:医药、消费、科技、智造、策略。5个行业中任意4个及以上是正式权限的,属于大套餐客户(医药、消费、科技、智造需要主客观都开) if companyDetail.Status == "永续" { userType = 1 } else if companyDetail.Status == "试用" { userType = 5 } else if companyDetail.Status == "冻结" { userType = 6 } else if companyDetail.Status == "流失" { userType = 7 } //大套餐客户定义:医药、消费、科技、智造、策略。5个行业中任意4个及以上是正式权限的,属于大套餐客户(医药、消费、科技、智造需要主客观都开) if userType == 0 && companyDetail.Status == "正式" { var permissionZhegnshiNum int if strings.Count(permissionZhengShiStr, "医药") == 2 { permissionZhegnshiNum++ } if strings.Count(permissionZhengShiStr, "消费") == 2 { permissionZhegnshiNum++ } if strings.Count(permissionZhengShiStr, "科技") == 2 { permissionZhegnshiNum++ } if strings.Count(permissionZhengShiStr, "智造") == 2 { permissionZhegnshiNum++ } if strings.Count(permissionZhengShiStr, "策略") == 1 { permissionZhegnshiNum++ } if strings.Count(permissionZhengShiStr, "路演服务") == 1 { permissionZhegnshiNum++ } //if permissionZhegnshiNum == 6 { // userType = 2 //} else //大套餐客户,数据库添加标识,条件大于等于四的都是 30W套餐客户 if permissionZhegnshiNum >= 4 { userType = 10 } else { userType = 3 } } } } permissionStrnew = permissionStr return } func GetUserTypeZhengShi(companyId int) (userType int, permissionStrnew, permissionStrZhengShinew string, err error) { var permissionStr, permissionZhengShiStr string if companyId <= 1 { userType = 0 } else { total, errs := models.GetCountCompanyDetailByIdGroup(companyId) if errs != nil { err = errs return } if total == 0 { userType = 0 } else { companyDetail, errs := models.GetCompanyDetailByIdGroup(companyId) if errs != nil { err = errs return } permissionStr, errs = models.GetCompanyPermission(companyId) if errs != nil { err = errs return } permissionStrnew = permissionStr permissionZhengShiStr, errs = models.GetCompanyPermissionByUserZhengShi(companyId) if errs != nil { err = errs return } permissionStrZhengShinew = permissionZhengShiStr //大套餐客户,数据库添加标识, companyUserTypeDetail, errs := models.GetCygxCompanyUserType(companyId) if errs != nil && errs.Error() != utils.ErrNoRow() { err = errs return } if companyUserTypeDetail != nil { if companyUserTypeDetail.CustomerTypeId != 0 { userType = companyUserTypeDetail.CustomerTypeId return } } //1、永续客户 //2、大套餐客户(4个行业全开通的正式客户) //3、分行业套餐客户(开通对应行业的正式客户) //4、仅开通专家套餐的正式客户 //5、开通对应行业套餐或专家套餐的试用客户、 10: 30W套餐客户 //大套餐客户定义:医药、消费、科技、智造、策略。5个行业中任意4个及以上是正式权限的,属于大套餐客户(医药、消费、科技、智造需要主客观都开) if companyDetail.Status == "永续" { userType = 1 } else if companyDetail.Status == "试用" { userType = 5 } else if companyDetail.Status == "冻结" { userType = 6 } else if companyDetail.Status == "流失" { userType = 7 } //大套餐客户定义:医药、消费、科技、智造、策略。5个行业中任意4个及以上是正式权限的,属于大套餐客户(医药、消费、科技、智造需要主客观都开) if userType == 0 && companyDetail.Status == "正式" { var permissionZhegnshiNum int if strings.Count(permissionZhengShiStr, "医药") == 2 { permissionZhegnshiNum++ } if strings.Count(permissionZhengShiStr, "消费") == 2 { permissionZhegnshiNum++ } if strings.Count(permissionZhengShiStr, "科技") == 2 { permissionZhegnshiNum++ } if strings.Count(permissionZhengShiStr, "智造") == 2 { permissionZhegnshiNum++ } if strings.Count(permissionZhengShiStr, "策略") == 1 { permissionZhegnshiNum++ } if strings.Count(permissionZhengShiStr, "路演服务") == 1 { permissionZhegnshiNum++ } //if permissionZhegnshiNum == 6 { // userType = 2 //} else //大套餐客户,数据库添加标识,条件大于等于四的都是 30W套餐客户 if permissionZhegnshiNum >= 4 { userType = 10 } else { userType = 3 } } } } permissionStrnew = permissionStr permissionStrZhengShinew = permissionZhengShiStr return } // 用户绑定 func BindWxUser(mobile, countryCode string) (wxUser *models.WxUserItem, err error) { if mobile == "" { err = errors.New("手机号或邮箱必填一个") return } //根据手机号获取用户信息 if mobile != "" { tmpWxUser, wxUserErr := models.GetWxUserItemByMobile(mobile) if wxUserErr != nil && wxUserErr.Error() != utils.ErrNoRow() { err = wxUserErr return } wxUser = tmpWxUser } var userId int //如果查询出来的用户是nil,那么需要新增用户 if wxUser == nil { user := &models.WxUser{ CompanyId: 1, CreatedTime: time.Now(), FirstLogin: 1, Enabled: 1, RegisterPlatform: 7, RegisterTime: time.Now(), Mobile: mobile, //Email: email, IsRegister: 1, Source: 3, CountryCode: countryCode, OutboundMobile: mobile, OutboundCountryCode: countryCode, } tmpUserId, addUserErr := models.AddWxUser(user) if addUserErr != nil { err = addUserErr return } user.UserId = int(tmpUserId) userId = int(tmpUserId) wxUser, err = models.GetWxUserItemByUserId(userId) } else { userId = wxUser.UserId err = models.BindUserOutboundMobile(mobile, countryCode, userId) if err != nil { return } if wxUser.IsRegister == 0 { models.ModifyWxUserRegisterStatus(userId) } } return } // 用户绑定用户手机号以及openid func BindWxUserMobileAndOpenid(mobile, openid, countryCode string) (wxUser *models.WxUserItem, err error) { if mobile == "" { err = errors.New("手机号或邮箱必填一个") return } var bindAccount string //根据手机号获取用户信息 if mobile != "" { tmpWxUser, wxUserErr := models.GetWxUserItemByMobile(mobile) if wxUserErr != nil && wxUserErr.Error() != utils.ErrNoRow() { err = wxUserErr return } wxUser = tmpWxUser bindAccount = mobile } //查询openid的第三方(微信)信息 userRecord, err := models.GetUserRecordByOpenId(openid) if err != nil { return } var userId int //如果查询出来的用户是nil,那么需要新增用户 if wxUser == nil { user := &models.WxUser{ CompanyId: 1, CreatedTime: time.Now(), FirstLogin: 1, Enabled: 1, RegisterPlatform: 7, RegisterTime: time.Now(), Mobile: mobile, //Email: email, IsRegister: 1, Source: 3, CountryCode: countryCode, OutboundMobile: mobile, OutboundCountryCode: countryCode, } tmpUserId, addUserErr := models.AddWxUser(user) if addUserErr != nil { err = addUserErr return } user.UserId = int(tmpUserId) userId = int(tmpUserId) wxUser, err = models.GetWxUserItemByUserId(userId) } else { userId = wxUser.UserId err = models.BindUserOutboundMobile(mobile, countryCode, userId) if err != nil { return } if wxUser.IsRegister == 0 { models.ModifyWxUserRegisterStatus(userId) } } //如果存在该手机号/邮箱,那么需要校验 if userRecord.UserId > 0 && userRecord.UserId != userId { err = errors.New("用户已绑定,不允许重复绑定") return } err = models.BindUserRecordByOpenid(userId, openid, bindAccount) if err != nil { return } userRecord.UserId = userId //如果当前该第三方用户信息的昵称为空串的话,那么需要去查询该用户的第一个绑定信息的数据作为来源做数据修复 if userRecord.NickName == "" { oldUserRecord, err := models.GetUserThirdRecordByUserId(userId) if err == nil && oldUserRecord != nil { //如果该用户绑定的第一条数据的头像信息不为空串,那么就去做新数据的修复 if oldUserRecord.NickName != "" { _ = models.ModifyUserRecordByDetail(userRecord.OpenId, userRecord.UnionId, oldUserRecord.NickName, oldUserRecord.Headimgurl, oldUserRecord.City, oldUserRecord.Province, oldUserRecord.Country, oldUserRecord.Sex, userId) } } } //格式化用户数据 formatWxUserAndUserRecord(wxUser, userRecord) return } // 添加第三方用户(微信)记录 func AddUserRecord(openId, unionId, nickName, realName, province, city, country, headimgurl, sessionKey string, platform, sex, subscribe int) (userRecord *models.UserRecord, err error) { find, err := models.GetUserRecordByOpenId(openId) if err != nil && err.Error() != utils.ErrNoRow() { return } if find != nil { userRecord = find return } userRecord = &models.UserRecord{ OpenId: openId, //用户open_id UnionId: unionId, //用户union_id Subscribe: subscribe, NickName: nickName, //用户昵称,最大长度:32 RealName: realName, //用户实际名称,最大长度:32 Sex: sex, //普通用户性别,1为男性,2为女性 Province: province, //普通用户个人资料填写的省份,最大长度:30 City: city, //普通用户个人资料填写的城市,最大长度:30 Country: country, //国家,如中国为CN,最大长度:30 Headimgurl: headimgurl, //用户第三方(微信)头像,最大长度:512 CreateTime: time.Now(), //创建时间,关系添加时间、用户授权时间 CreatePlatform: platform, //注册平台,1:日度点评公众号,2:管理后台,3:pc端网站,4:查研观向小程序;默认:1 SessionKey: sessionKey, //微信小程序会话密钥,最大长度:255 } //recordId, err := models.AddUserRecord(userRecord) if err != nil { return } //userRecord.UserRecordId = int(recordId) return } // 用户绑定 func BindSession(mobile, countryCode string) (wxUser *models.WxUserItem, err error) { if mobile == "" { err = errors.New("手机号或邮箱必填一个") return } //var bindAccount string //根据手机号获取用户信息 if mobile != "" { tmpWxUser, wxUserErr := models.GetWxUserItemByMobile(mobile) if wxUserErr != nil && wxUserErr.Error() != utils.ErrNoRow() { err = wxUserErr return } wxUser = tmpWxUser //bindAccount = mobile } var userId int //如果查询出来的用户是nil,那么需要新增用户 if wxUser == nil { user := &models.WxUser{ CompanyId: 1, CreatedTime: time.Now(), FirstLogin: 1, Enabled: 1, RegisterPlatform: 7, RegisterTime: time.Now(), Mobile: mobile, //Email: email, IsRegister: 1, Source: 3, CountryCode: countryCode, OutboundMobile: mobile, OutboundCountryCode: countryCode, } tmpUserId, addUserErr := models.AddWxUser(user) if addUserErr != nil { err = addUserErr return } user.UserId = int(tmpUserId) userId = int(tmpUserId) wxUser, err = models.GetWxUserItemByUserId(userId) } else { userId = wxUser.UserId err = models.BindUserOutboundMobile(mobile, countryCode, userId) if err != nil { return } if wxUser.IsRegister == 0 { models.ModifyWxUserRegisterStatus(userId) } } return } // 我的收藏 func GetMicroRoadShowMycollect(pageSize, currentIndex int, audioIds, videoIds, activityVideoIds string) (respList []*models.MicroRoadShowPageList, total int, err error) { var e error // 根据每页数据量获取音视频配比 startSize := utils.StartIndex(currentIndex, pageSize) videoList := make([]*models.MicroRoadShowPageList, 0) //音频的查询 var audioCond string var audioPars []interface{} // 如果筛选条件为指定视频ID或只看视频则不做音频查询 // 活动已发布且已结束 audioCond += ` AND b.publish_status = 1 AND b.active_state = 3` if audioIds != "" { sliceId := strings.Split(audioIds, ",") var idSqlStr string for _, v := range sliceId { idSqlStr += "'" + v + "'," } idSqlStr = strings.TrimRight(idSqlStr, ",") audioCond += ` AND a.activity_voice_id IN (` + idSqlStr + `)` } else { audioCond += ` AND a.activity_voice_id = 0 ` } //视频的处理 var videoCond string var videoCondAct string if activityVideoIds != "" { sliceId := strings.Split(activityVideoIds, ",") var idSqlStr string for _, v := range sliceId { idSqlStr += "'" + v + "'," } idSqlStr = strings.TrimRight(idSqlStr, ",") videoCondAct += ` AND v.video_id IN (` + idSqlStr + `)` } else { videoCondAct += ` AND v.video_id = 0 ` } var videoPars []interface{} var videoParsAct []interface{} if videoIds != "" { sliceId := strings.Split(videoIds, ",") var idSqlStr string for _, v := range sliceId { idSqlStr += "'" + v + "'," } idSqlStr = strings.TrimRight(idSqlStr, ",") videoCond += ` AND video_id IN (` + idSqlStr + `)` } else { videoCond += ` AND video_id = 0 ` } videoCond += ` AND publish_status = 1` total, videoList, e = models.GetMicroRoadShowVideoPageListV8(startSize, pageSize, videoCond, videoPars, videoCondAct, videoParsAct, audioCond, audioPars, 0, 0, 0, 0) if e != nil { err = errors.New("获取微路演音视频列表失败, Err: " + e.Error()) return } respList = videoList return } // 我的收藏 func GetMicroRoadShowMycollectV12(pageSize, currentIndex int, audioIds, activityVideoIds, roadshowIdsStr, askserieVideoIds string, user *models.WxUserItem) (respList []*models.MicroRoadShowPageList, total int, err error) { //Type int `description:"类型: 1-活动音频; 2-活动视频; 3-产业视频; 4-系列问答"` var e error // 根据每页数据量获取音视频配比 startSize := utils.StartIndex(currentIndex, pageSize) videoList := make([]*models.MicroRoadShowPageList, 0) var audioAct string // 活动音频 1 var audioActPars []interface{} var videoAct string // 活动视频 2 var videoActPars []interface{} //视频的处理 var videoMico string //产业视频 3 var videoMicoPars []interface{} var conditionAskserie string // 系列问答 4 var askseriePars []interface{} if audioIds != "" { sliceId := strings.Split(audioIds, ",") audioAct += ` AND a.activity_voice_id IN (` + strings.Join(sliceId, ",") + `)` } else { audioAct += ` AND a.activity_voice_id = 0 ` } if activityVideoIds != "" { sliceId := strings.Split(activityVideoIds, ",") videoAct += ` AND a.video_id IN (` + strings.Join(sliceId, ",") + `)` } else { videoAct += ` AND a.video_id = 0 ` } if roadshowIdsStr != "" { sliceId := strings.Split(roadshowIdsStr, ",") videoMico += ` AND a.video_id IN (` + strings.Join(sliceId, ",") + `)` } else { videoMico += ` AND a.video_id = 0 ` } if askserieVideoIds != "" { sliceId := strings.Split(askserieVideoIds, ",") conditionAskserie += ` AND a.askserie_video_id IN (` + strings.Join(sliceId, ",") + `)` } else { conditionAskserie += ` AND a.askserie_video_id = 0 ` } //发布状态 audioAct += ` AND b.publish_status = 1 ` videoAct += ` AND b.publish_status = 1 ` videoMico += ` AND a.publish_status = 1 ` conditionAskserie += ` AND a.publish_status = 1 ` total, videoList, e = models.GetMicroRoadShowVideoPageListV12(startSize, pageSize, audioAct, audioActPars, videoAct, videoActPars, videoMico, videoMicoPars, conditionAskserie, askseriePars) if e != nil { err = errors.New("获取微路演音视频列表失败, Err: " + e.Error()) return } userId := user.UserId listMycollect, e := models.GetUserMicroRoadshowCollectList(userId) if e != nil { err = errors.New("GetUserMicroRoadshowCollectList, Err: " + e.Error()) return } mapaudioIds := make(map[int]int) //活动音频 mapvideoIds := make(map[int]int) // 微路演视频 mapactivityVideoIds := make(map[int]int) // 活动视频 mapAskserieVideoIds := make(map[int]int) // 活动视频 for _, item := range listMycollect { if item.ActivityVoiceId > 0 { mapaudioIds[item.ActivityVoiceId] = item.ActivityVoiceId } else if item.VideoId > 0 { mapvideoIds[item.VideoId] = item.VideoId } else if item.ActivityVideoId > 0 { mapactivityVideoIds[item.ActivityVideoId] = item.ActivityVideoId } } listAskserieVideoCollect, e := models.GetUserCygxAskserieVideoCollectList(userId) if e != nil { err = errors.New("GetUserCygxAskserieVideoCollectList, Err: " + e.Error()) return } for _, v := range listAskserieVideoCollect { if v.AskserieVideoId > 0 { mapAskserieVideoIds[v.AskserieVideoId] = v.AskserieVideoId } } for _, item := range videoList { if item.Type == 1 { //音频 if mapaudioIds[item.Id] > 0 { item.IsCollect = true } } else if item.Type == 2 { //活动视频 if mapactivityVideoIds[item.Id] > 0 { item.IsCollect = true } } else if item.Type == 3 { //微路演视频 if mapvideoIds[item.Id] > 0 { item.IsCollect = true } } else if item.Type == 4 { //系列问答视频 if mapAskserieVideoIds[item.SourceId] > 0 { item.IsCollect = true } } } for _, v := range videoList { v.LabelType = v.Type if v.Type == 1 || v.Type == 2 { v.LabelType = v.ActivityFileType } } respList = videoList return } // 获取用户权限 func GetUserhasPermission(user *models.WxUserItem) (hasPermission int, err error) { //判断是否已经申请过 applyCount, err := models.GetApplyRecordCount(user.UserId) if err != nil && err.Error() != utils.ErrNoRow() { return } if applyCount > 0 { hasPermission = 3 } else { hasPermission = 5 } //HasPermission int `description:"1:有该行业权限,正常展示,2:无该行业权限,不存在权益客户下,3:无该品类权限,已提交过申请,4:无该行业权限,未提交过申请,5:潜在客户,未提交过申请,6:潜在客户,已提交过申请"` if user.CompanyId > 1 { companyPermission, errPer := models.GetCompanyPermission(user.CompanyId) if errPer != nil { err = errPer return } if companyPermission == "" { if applyCount > 0 { hasPermission = 3 } else { hasPermission = 4 } } else { if strings.Contains(companyPermission, "医药") || strings.Contains(companyPermission, "科技") || strings.Contains(companyPermission, "消费") || strings.Contains(companyPermission, "智造") || strings.Contains(companyPermission, "策略") { hasPermission = 1 } } } return }