user_permission.go 7.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234
  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. checkInfo.Status = company.Status
  86. // 判断用户状态
  87. if company.Status == utils.COMPANY_STATUS_LOSE {
  88. status = 2
  89. msg = "您还未开通权限,立即申请免费试用"
  90. checkInfo.Type = "apply"
  91. }else if company.Status == utils.COMPANY_STATUS_FREEZE {
  92. status = 2
  93. msg = "您暂无权限查看报告 请联系对口销售——"
  94. }else if company.IsSuspend > 0 && company.Status == "试用" { //如果客户产品被禁用了,只有在试用状态下,才不允许查看报告,那么没有权限
  95. status = 2
  96. msg = "您暂无权限查看报告 请联系对口销售——"
  97. checkInfo.Status = "暂停试用"
  98. }
  99. checkInfo.CompanyName = company.CompanyName
  100. checkInfo.RealName = user.RealName
  101. //查找对应客户的销售信息
  102. if company.SellerId > 0 {
  103. adminInfo, tmpErr := models.GetAdminByAdminId(company.SellerId)
  104. if tmpErr != nil && tmpErr.Error() != utils.ErrNoRow() {
  105. brMsg = "获取销售信息失败"
  106. brErrMsg = "获取销售信息失败,Err:" + tmpErr.Error()
  107. return
  108. }
  109. if adminInfo != nil {
  110. sellerName = adminInfo.RealName
  111. checkInfo.Name = adminInfo.RealName
  112. checkInfo.Mobile = adminInfo.Mobile
  113. if status == 2 && checkInfo.Type == "contact" {
  114. msg = "您暂无权限查看报告 请联系对口销售——"+sellerName
  115. }
  116. }
  117. }
  118. }
  119. if productId == 1 {
  120. reportType := "rddp"
  121. reportPermissionList, err := models.GetReportVarietyListByUserIdExt(user.UserId, reportType) //获取客户拥有权限的报告id
  122. if err != nil {
  123. if err.Error() != utils.ErrNoRow() {
  124. brMsg = "获取报告详情失败"
  125. brErrMsg = "获取客户信息失败,Err:" + err.Error()
  126. return
  127. }
  128. }
  129. if len(reportPermissionList) >= 0 {
  130. permissionMap := make(map[int]int)
  131. for _, v := range reportPermissionList {
  132. if _, ok := permissionMap[v.ReportChapterTypeId]; !ok {
  133. permissionMap[v.ReportChapterTypeId] = v.ReportChapterTypeId
  134. }
  135. }
  136. if _, ok := permissionMap[reportId]; !ok {
  137. tips := ""
  138. status = 2
  139. classifyNameSecond := report.ClassifyNameSecond
  140. if strings.Contains(classifyNameSecond, "宏观商品复盘") ||
  141. strings.Contains(classifyNameSecond, "股债日评") ||
  142. strings.Contains(classifyNameSecond, "每日经济数据备忘录") {
  143. tips = "宏观"
  144. } else if strings.Contains(classifyNameSecond, "知白守黑日评") ||
  145. strings.Contains(classifyNameSecond, "动力煤数据点评") {
  146. tips = "黑色"
  147. } else if strings.Contains(classifyNameSecond, "化里化外日评") ||
  148. strings.Contains(classifyNameSecond, "EIA原油库存点评") ||
  149. strings.Contains(classifyNameSecond, "苯乙烯数据点评") ||
  150. strings.Contains(classifyNameSecond, "聚酯数据点评") {
  151. tips = "化工"
  152. } else if strings.Contains(classifyNameSecond, "有声有色日度闲篇") {
  153. tips = "有色"
  154. }
  155. if checkInfo.Type == "contact" {
  156. msg = "您还未开通" + tips + "权限,如有需要请联系对口销售——"+sellerName
  157. }
  158. }
  159. }else{
  160. status = 2
  161. if checkInfo.Type == "contact" {
  162. msg = "您暂无权限查看报告 请联系对口销售——" + sellerName
  163. }
  164. }
  165. }
  166. //判断大小权限
  167. {
  168. if status == 2 {
  169. if company != nil && !strings.Contains(report.ClassifyNameFirst, "权益研报") {
  170. permissionCount, err := models.GetCompanyProductPermissionCount(company.CompanyId, productId)
  171. if err != nil {
  172. brMsg = "获取信息失败"
  173. brErrMsg = "获取权限信息失败,Err:" + err.Error()
  174. return
  175. }
  176. if permissionCount >= maxPermissionCount {
  177. status = 0
  178. msg = ""
  179. checkInfo.Type = ""
  180. }
  181. }
  182. // 判断用户是否有权益研报,权益产品中的策略权限
  183. if strings.Contains(report.ClassifyNameFirst, "权益研报") {
  184. productId = 2
  185. qyCompany, tmpErr := models.GetCompanyProductById(user.CompanyId, 2)
  186. if tmpErr != nil && tmpErr.Error() != utils.ErrNoRow() {
  187. brMsg = "获取客户产品信息失败"
  188. brErrMsg = "获取客户产品信息失败,Err:" + tmpErr.Error()
  189. return
  190. }
  191. if qyCompany != nil {
  192. if strings.Contains("正式,永续",qyCompany.Status) || (qyCompany.Status == "试用" && qyCompany.IsSuspend != 1) {
  193. tacticsCount, tmpErr2 := models.GetTacticsPermissionIdByCompanyId(qyCompany.CompanyId, 2)
  194. if tmpErr2 != nil && tmpErr2.Error() != utils.ErrNoRow() {
  195. brMsg = "获取信息失败"
  196. brErrMsg = "获取权限信息失败,Err:" + tmpErr2.Error()
  197. return
  198. }
  199. if tacticsCount > 0 {
  200. status = 0
  201. msg = ""
  202. checkInfo.Type = ""
  203. company = qyCompany
  204. }
  205. }
  206. }
  207. }
  208. }
  209. }
  210. if status == 2 {
  211. //查询是否有过申请记录
  212. record, err := models.GetLastApplyRecordNotOpRecordByUserId(user.UserId) // 从来源我的/活动申请的记录
  213. if err != nil && err.Error() != utils.ErrNoRow() {
  214. brMsg = "获取申请记录失败"
  215. brErrMsg = "获取申请记录失败,Err:" + err.Error()
  216. return
  217. }
  218. //查询是否有申请过,如果有申请过的话,那么err是nil
  219. if record != nil {
  220. checkInfo.HasApply = true
  221. }
  222. }
  223. return
  224. }