123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234 |
- package services
- import (
- "errors"
- "fmt"
- "hongze/hongze_api/models"
- "hongze/hongze_api/utils"
- "strconv"
- "strings"
- )
- func CheckUserPermission(userId int) (status int, err error) {
- if userId > 0 {
- //wxUser, err := models.GetWxUserItemByUserId(userId)
- wxUser, err := GetWxUserItemByUserId(userId, utils.WxPlatform)
- if err != nil {
- if err.Error() == utils.ErrNoRow() {
- status = 40001
- err = errors.New("用户信息不存在:userId:" + strconv.Itoa(userId))
- return status, err
- }
- status = 40001
- err = errors.New("获取用户信息失败:userId:" + strconv.Itoa(userId) + ";Err:" + err.Error())
- return status, err
- }
- if wxUser == nil {
- status = 40001
- err = errors.New("获取用户信息失败:userId:" + strconv.Itoa(userId))
- return status, err
- }
- companyId := wxUser.CompanyId
- if companyId <= 1 {
- status = 40001
- return status, err
- }
- fmt.Println("companyId", companyId)
- companyProduct, err := models.GetCompanyById(companyId)
- if err != nil {
- status = 40001
- err = errors.New("获取客户信息失败:userId:" + strconv.Itoa(userId) + ";Err:" + err.Error())
- return status, err
- }
- if len(companyProduct) == 0 {
- status = 40001
- err = errors.New("客户信息不存在:userId:" + strconv.Itoa(userId))
- return status, err
- }
- for _, v := range companyProduct {
- if v.Status == utils.COMPANY_STATUS_FORMAL || //正式
- v.Status == utils.COMPANY_STATUS_TRY_OUT || //试用
- v.Status == utils.COMPANY_STATUS_FOREVER { //永续
- status = 0
- break
- } else {
- status = 40002
- err = errors.New("非付费用户" + strconv.Itoa(userId))
- }
- }
- } else {
- status = 40001
- err = errors.New("用户id错误")
- }
- return
- }
- // CheckUserReportPermission 判断用户查看当前报告时是需要发起申请还是联系销售
- func CheckUserReportPermission(user *models.WxUserItem, productId int, report *models.ReportDetail, maxPermissionCount int) (status int, checkInfo models.PermissionCheckInfoResp, company *models.CompanyProduct, msg, brMsg, brErrMsg string) {
- reportId := report.Id
- company, err := models.GetCompanyProductById(user.CompanyId, productId)
- if err != nil {
- if err.Error() != utils.ErrNoRow() {
- brMsg = "获取报告详情失败"
- brErrMsg = "获取用户管理企业信息失败,Err:" + err.Error()
- return
- }
- }
- sellerName := ""
- if company == nil {
- status = 2
- msg = "您还未开通权限,立即申请免费试用"
- checkInfo.Type = "apply"
- }else if company.CompanyId == 1 {
- status = 2
- msg = "您还未开通权限,立即申请免费试用"
- checkInfo.Type = "apply"
- }else {
- checkInfo.Type = "contact"
- checkInfo.Status = company.Status
- // 判断用户状态
- if company.Status == utils.COMPANY_STATUS_LOSE {
- status = 2
- msg = "您还未开通权限,立即申请免费试用"
- checkInfo.Type = "apply"
- }else if company.Status == utils.COMPANY_STATUS_FREEZE {
- status = 2
- msg = "您暂无权限查看报告 请联系对口销售——"
- }else if company.IsSuspend > 0 && company.Status == "试用" { //如果客户产品被禁用了,只有在试用状态下,才不允许查看报告,那么没有权限
- status = 2
- msg = "您暂无权限查看报告 请联系对口销售——"
- checkInfo.Status = "暂停试用"
- }
- checkInfo.CompanyName = company.CompanyName
- checkInfo.RealName = user.RealName
- //查找对应客户的销售信息
- if company.SellerId > 0 {
- adminInfo, tmpErr := models.GetAdminByAdminId(company.SellerId)
- if tmpErr != nil && tmpErr.Error() != utils.ErrNoRow() {
- brMsg = "获取销售信息失败"
- brErrMsg = "获取销售信息失败,Err:" + tmpErr.Error()
- return
- }
- if adminInfo != nil {
- sellerName = adminInfo.RealName
- checkInfo.Name = adminInfo.RealName
- checkInfo.Mobile = adminInfo.Mobile
- if status == 2 && checkInfo.Type == "contact" {
- msg = "您暂无权限查看报告 请联系对口销售——"+sellerName
- }
- }
- }
- }
- if productId == 1 {
- reportType := "rddp"
- reportPermissionList, err := models.GetReportVarietyListByUserIdExt(user.UserId, reportType) //获取客户拥有权限的报告id
- if err != nil {
- if err.Error() != utils.ErrNoRow() {
- brMsg = "获取报告详情失败"
- brErrMsg = "获取客户信息失败,Err:" + err.Error()
- return
- }
- }
- if len(reportPermissionList) >= 0 {
- permissionMap := make(map[int]int)
- for _, v := range reportPermissionList {
- if _, ok := permissionMap[v.ReportChapterTypeId]; !ok {
- permissionMap[v.ReportChapterTypeId] = v.ReportChapterTypeId
- }
- }
- if _, ok := permissionMap[reportId]; !ok {
- tips := ""
- status = 2
- classifyNameSecond := report.ClassifyNameSecond
- if strings.Contains(classifyNameSecond, "宏观商品复盘") ||
- strings.Contains(classifyNameSecond, "股债日评") ||
- strings.Contains(classifyNameSecond, "每日经济数据备忘录") {
- tips = "宏观"
- } else if strings.Contains(classifyNameSecond, "知白守黑日评") ||
- strings.Contains(classifyNameSecond, "动力煤数据点评") {
- tips = "黑色"
- } else if strings.Contains(classifyNameSecond, "化里化外日评") ||
- strings.Contains(classifyNameSecond, "EIA原油库存点评") ||
- strings.Contains(classifyNameSecond, "苯乙烯数据点评") ||
- strings.Contains(classifyNameSecond, "聚酯数据点评") {
- tips = "化工"
- } else if strings.Contains(classifyNameSecond, "有声有色日度闲篇") {
- tips = "有色"
- }
- if checkInfo.Type == "contact" {
- msg = "您还未开通" + tips + "权限,如有需要请联系对口销售——"+sellerName
- }
- }
- }else{
- status = 2
- if checkInfo.Type == "contact" {
- msg = "您暂无权限查看报告 请联系对口销售——" + sellerName
- }
- }
- }
- //判断大小权限
- {
- if status == 2 {
- if company != nil && !strings.Contains(report.ClassifyNameFirst, "权益研报") {
- permissionCount, err := models.GetCompanyProductPermissionCount(company.CompanyId, productId)
- if err != nil {
- brMsg = "获取信息失败"
- brErrMsg = "获取权限信息失败,Err:" + err.Error()
- return
- }
- if permissionCount >= maxPermissionCount {
- status = 0
- msg = ""
- checkInfo.Type = ""
- }
- }
- // 判断用户是否有权益研报,权益产品中的策略权限
- if strings.Contains(report.ClassifyNameFirst, "权益研报") {
- productId = 2
- qyCompany, tmpErr := models.GetCompanyProductById(user.CompanyId, 2)
- if tmpErr != nil && tmpErr.Error() != utils.ErrNoRow() {
- brMsg = "获取客户产品信息失败"
- brErrMsg = "获取客户产品信息失败,Err:" + tmpErr.Error()
- return
- }
- if qyCompany != nil {
- if strings.Contains("正式,永续",qyCompany.Status) || (qyCompany.Status == "试用" && qyCompany.IsSuspend != 1) {
- tacticsCount, tmpErr2 := models.GetTacticsPermissionIdByCompanyId(qyCompany.CompanyId, 2)
- if tmpErr2 != nil && tmpErr2.Error() != utils.ErrNoRow() {
- brMsg = "获取信息失败"
- brErrMsg = "获取权限信息失败,Err:" + tmpErr2.Error()
- return
- }
- if tacticsCount > 0 {
- status = 0
- msg = ""
- checkInfo.Type = ""
- company = qyCompany
- }
- }
- }
- }
- }
- }
- if status == 2 {
- //查询是否有过申请记录
- record, err := models.GetLastApplyRecordNotOpRecordByUserId(user.UserId) // 从来源我的/活动申请的记录
- if err != nil && err.Error() != utils.ErrNoRow() {
- brMsg = "获取申请记录失败"
- brErrMsg = "获取申请记录失败,Err:" + err.Error()
- return
- }
- //查询是否有申请过,如果有申请过的话,那么err是nil
- if record != nil {
- checkInfo.HasApply = true
- }
- }
- return
- }
|