user_permission.go 6.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212
  1. package services
  2. import (
  3. "errors"
  4. "fmt"
  5. "hongze/hongze_api/models"
  6. "hongze/hongze_api/utils"
  7. "strconv"
  8. "strings"
  9. )
  10. func CheckUserPermission(userId int) (status int, err error) {
  11. if userId > 0 {
  12. //wxUser, err := models.GetWxUserItemByUserId(userId)
  13. wxUser, err := GetWxUserItemByUserId(userId, utils.WxPlatform)
  14. if err != nil {
  15. if err.Error() == utils.ErrNoRow() {
  16. status = 40001
  17. err = errors.New("用户信息不存在:userId:" + strconv.Itoa(userId))
  18. return status, err
  19. }
  20. status = 40001
  21. err = errors.New("获取用户信息失败:userId:" + strconv.Itoa(userId) + ";Err:" + err.Error())
  22. return status, err
  23. }
  24. if wxUser == nil {
  25. status = 40001
  26. err = errors.New("获取用户信息失败:userId:" + strconv.Itoa(userId))
  27. return status, err
  28. }
  29. companyId := wxUser.CompanyId
  30. if companyId <= 1 {
  31. status = 40001
  32. return status, err
  33. }
  34. fmt.Println("companyId", companyId)
  35. companyProduct, err := models.GetCompanyById(companyId)
  36. if err != nil {
  37. status = 40001
  38. err = errors.New("获取客户信息失败:userId:" + strconv.Itoa(userId) + ";Err:" + err.Error())
  39. return status, err
  40. }
  41. if len(companyProduct) == 0 {
  42. status = 40001
  43. err = errors.New("客户信息不存在:userId:" + strconv.Itoa(userId))
  44. return status, err
  45. }
  46. for _, v := range companyProduct {
  47. if v.Status == utils.COMPANY_STATUS_FORMAL || //正式
  48. v.Status == utils.COMPANY_STATUS_TRY_OUT || //试用
  49. v.Status == utils.COMPANY_STATUS_FOREVER { //永续
  50. status = 0
  51. break
  52. } else {
  53. status = 40002
  54. err = errors.New("非付费用户" + strconv.Itoa(userId))
  55. }
  56. }
  57. } else {
  58. status = 40001
  59. err = errors.New("用户id错误")
  60. }
  61. return
  62. }
  63. // CheckUserReportPermission 判断用户查看当前报告时是需要发起申请还是联系销售
  64. func CheckUserReportPermission(user *models.WxUserItem, productId int, report *models.ReportDetail, maxPermissionCount int) (status int, checkInfo models.PermissionCheckInfoResp, company *models.CompanyProduct, msg, brMsg, brErrMsg string) {
  65. reportId := report.Id
  66. company, err := models.GetCompanyProductById(user.CompanyId, productId)
  67. if err != nil {
  68. if err.Error() != utils.ErrNoRow() {
  69. brMsg = "获取报告详情失败"
  70. brErrMsg = "获取用户管理企业信息失败,Err:" + err.Error()
  71. return
  72. }
  73. }
  74. sellerName := ""
  75. if company == nil {
  76. status = 2
  77. msg = "您还未开通权限,立即申请免费试用"
  78. checkInfo.Type = "apply"
  79. }else if company.CompanyId == 1 {
  80. status = 2
  81. msg = "您还未开通权限,立即申请免费试用"
  82. checkInfo.Type = "apply"
  83. }else {
  84. checkInfo.Type = "contact"
  85. customerInfo := models.CustomerInfoResp{
  86. CompanyName: company.CompanyName,
  87. Status: company.Status,
  88. Name: user.RealName,
  89. IsSuspend: int8(company.IsSuspend),
  90. Mobile: user.Mobile,
  91. }
  92. checkInfo.CustomerInfo = customerInfo
  93. // 判断用户状态
  94. if company.Status == utils.COMPANY_STATUS_LOSE {
  95. status = 2
  96. msg = "您还未开通权限,立即申请免费试用"
  97. checkInfo.Type = "apply"
  98. }else if company.Status == utils.COMPANY_STATUS_FREEZE {
  99. status = 2
  100. msg = "您还未开通权限,如有需要请联系对口销售"
  101. }else if company.IsSuspend > 0 && company.Status == "试用" { //如果客户产品被禁用了,只有在试用状态下,才不允许查看报告,那么没有权限
  102. status = 2
  103. msg = "您还未开通权限,如有需要请联系对口销售"
  104. }
  105. //查找对应客户的销售信息
  106. if company.SellerId > 0 {
  107. adminInfo, tmpErr := models.GetAdminByAdminId(company.SellerId)
  108. if tmpErr != nil && tmpErr.Error() != utils.ErrNoRow() {
  109. brMsg = "获取销售信息失败"
  110. brErrMsg = "获取销售信息失败,Err:" + tmpErr.Error()
  111. return
  112. }
  113. if adminInfo != nil {
  114. sellerName = adminInfo.RealName
  115. checkInfo.Name = adminInfo.RealName
  116. checkInfo.Mobile = adminInfo.Mobile
  117. if status == 2 && checkInfo.Type == "contact" {
  118. msg = "您还未开通权限,如有需要请联系对口销售"+sellerName
  119. }
  120. }
  121. }
  122. }
  123. if productId == 1 {
  124. reportType := "rddp"
  125. reportPermissionList, err := models.GetReportVarietyListByUserIdExt(user.UserId, reportType) //获取客户拥有权限的报告id
  126. if err != nil {
  127. if err.Error() != utils.ErrNoRow() {
  128. brMsg = "获取报告详情失败"
  129. brErrMsg = "获取客户信息失败,Err:" + err.Error()
  130. return
  131. }
  132. }
  133. if len(reportPermissionList) >= 0 {
  134. permissionMap := make(map[int]int)
  135. for _, v := range reportPermissionList {
  136. if _, ok := permissionMap[v.ReportChapterTypeId]; !ok {
  137. permissionMap[v.ReportChapterTypeId] = v.ReportChapterTypeId
  138. }
  139. }
  140. if _, ok := permissionMap[reportId]; !ok {
  141. tips := ""
  142. status = 2
  143. classifyNameSecond := report.ClassifyNameSecond
  144. if strings.Contains(classifyNameSecond, "宏观商品复盘") ||
  145. strings.Contains(classifyNameSecond, "股债日评") ||
  146. strings.Contains(classifyNameSecond, "每日经济数据备忘录") {
  147. tips = "宏观"
  148. } else if strings.Contains(classifyNameSecond, "知白守黑日评") ||
  149. strings.Contains(classifyNameSecond, "动力煤数据点评") {
  150. tips = "黑色"
  151. } else if strings.Contains(classifyNameSecond, "化里化外日评") ||
  152. strings.Contains(classifyNameSecond, "EIA原油库存点评") ||
  153. strings.Contains(classifyNameSecond, "苯乙烯数据点评") ||
  154. strings.Contains(classifyNameSecond, "聚酯数据点评") {
  155. tips = "化工"
  156. } else if strings.Contains(classifyNameSecond, "有声有色日度闲篇") {
  157. tips = "有色"
  158. }
  159. if checkInfo.Type == "contact" {
  160. msg = "您还未开通" + tips + "权限,如有需要请联系对口销售 "+sellerName
  161. }
  162. }
  163. }else{
  164. status = 2
  165. if checkInfo.Type == "contact" {
  166. msg = "您还未开通权限,如有需要请联系对口销售 " + sellerName
  167. }
  168. }
  169. }
  170. //判断大小权限
  171. {
  172. if status == 2 {
  173. if company != nil {
  174. permissionCount, err := models.GetCompanyProductPermissionCount(company.CompanyId, productId)
  175. if err != nil {
  176. brMsg = "获取信息失败"
  177. brErrMsg = "获取权限信息失败,Err:" + err.Error()
  178. return
  179. }
  180. if permissionCount >= maxPermissionCount {
  181. status = 0
  182. msg = ""
  183. checkInfo.Type = ""
  184. }
  185. }
  186. }
  187. }
  188. if status == 2 {
  189. //查询是否有过申请记录
  190. record, err := models.GetLastApplyRecordNotOpRecordByUserId(user.UserId) // 从来源我的/活动申请的记录
  191. if err != nil && err.Error() != utils.ErrNoRow() {
  192. brMsg = "获取申请记录失败"
  193. brErrMsg = "获取申请记录失败,Err:" + err.Error()
  194. return
  195. }
  196. //查询是否有申请过,如果有申请过的话,那么err是nil
  197. if record != nil {
  198. checkInfo.CustomerInfo.HasApply = true
  199. }
  200. }
  201. return
  202. }