user_permission.go 12 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399
  1. package services
  2. import (
  3. "errors"
  4. "fmt"
  5. "hongze/hongze_web_mfyx/models"
  6. "hongze/hongze_web_mfyx/models/order"
  7. "hongze/hongze_web_mfyx/utils"
  8. "strings"
  9. )
  10. var (
  11. UserPermissionOperationModeApply = "Apply"
  12. UserPermissionOperationModeCall = "Call"
  13. UserPermissionPopupMsgApplyActivity = "您暂无权限参加此活动,若想参加可以申请开通对应的试用权限"
  14. UserPermissionPopupMsgCallActivity = "您暂无权限参加此活动,若想参加可以联系对口销售"
  15. UserPermissionPopupMsgApplyMicroVideo = "您暂无权限查看此视频,若想查看可以申请开通对应的试用权限"
  16. UserPermissionPopupMsgApplyMicroVoice = "您暂无权限查看此音频,若想查看可以申请开通对应的试用权限"
  17. UserPermissionPopupMsgCallMicroVoice = "您暂无权限查看此音频,若想参加可以联系对口销售"
  18. UserPermissionPopupMsgCallMicroVideo = "您暂无权限查看此视频,若想查看可以联系对口销售"
  19. )
  20. // GetUserRaiPermissionInfo 获取权限类型及信息
  21. // HasPermission int `description:"1:有该行业权限,正常展示,2:无该行业权限,不存在权益客户下(ficc),3:无该品类权限,已提交过申请,4:无该品类权限,未提交过申请,5:潜在客户,未提交过申请,6:潜在客户,已提交过申请"`
  22. func GetUserRaiPermissionInfo1(userId, companyId int) (authInfo models.UserPermissionAuthInfo, permissionArr []string, err error) {
  23. // 用户申请记录
  24. applyCount, e := models.GetApplyRecordCount(userId)
  25. if e != nil && e.Error() != utils.ErrNoRow() {
  26. err = errors.New("获取用户申请信息失败, Err: " + e.Error())
  27. return
  28. }
  29. // 潜在用户
  30. if companyId <= 1 {
  31. authInfo.HasPermission = 5
  32. if applyCount > 0 {
  33. authInfo.HasPermission = 6
  34. }
  35. authInfo.OperationMode = UserPermissionOperationModeApply
  36. authInfo.PopupMsg = UserPermissionPopupMsgApplyActivity
  37. return
  38. }
  39. // 销售信息
  40. ficcSeller, e := models.GetSellerByCompanyIdCheckFicc(companyId, utils.COMPANY_PRODUCT_FICC_ID)
  41. if e != nil && e.Error() != utils.ErrNoRow() {
  42. err = errors.New("获取FICC销售信息失败, Err: " + e.Error())
  43. return
  44. }
  45. raiSeller, e := models.GetSellerByCompanyIdCheckFicc(companyId, utils.COMPANY_PRODUCT_RAI_ID)
  46. if e != nil && e.Error() != utils.ErrNoRow() {
  47. err = errors.New("获取权益销售信息失败, Err: " + e.Error())
  48. return
  49. }
  50. if raiSeller != nil {
  51. authInfo.SellerMobile = raiSeller.Mobile
  52. authInfo.SellerName = raiSeller.RealName
  53. authInfo.HasPermission = 4
  54. if applyCount > 0 {
  55. authInfo.HasPermission = 3
  56. }
  57. authInfo.OperationMode = UserPermissionOperationModeApply
  58. authInfo.PopupMsg = UserPermissionPopupMsgApplyActivity
  59. return
  60. }
  61. // 仅有FICC权限
  62. if ficcSeller != nil {
  63. authInfo.HasPermission = 2
  64. authInfo.OperationMode = UserPermissionOperationModeApply
  65. authInfo.PopupMsg = UserPermissionPopupMsgApplyActivity
  66. return
  67. }
  68. // permissions示例: 医药,消费,科技,智造,策略,专家,买方研选
  69. permissions, e := models.GetCompanyPermissionName(companyId)
  70. if e != nil {
  71. err = errors.New("获取客户权限失败, Err: " + e.Error())
  72. return
  73. }
  74. if permissions == "" {
  75. // 无权益权限
  76. // 有销售信息
  77. authInfo.HasPermission = 2
  78. authInfo.OperationMode = UserPermissionOperationModeCall
  79. authInfo.PopupMsg = UserPermissionPopupMsgCallActivity
  80. return
  81. // 无销售信息, 无申请, 视作潜在用户
  82. //authInfo.HasPermission = 3
  83. //authInfo.OperationMode = UserPermissionOperationModeApply
  84. //return
  85. }
  86. // 有基本的权益权限
  87. authInfo.HasPermission = 1
  88. permissionArr = strings.Split(permissions, ",")
  89. return
  90. }
  91. func GetUserRaiPermissionInfo(userId, companyId int) (authInfo models.UserPermissionAuthInfo, permissionArr []string, err error) {
  92. // 用户申请记录
  93. applyCount, e := models.GetApplyRecordCount(userId)
  94. if e != nil && e.Error() != utils.ErrNoRow() {
  95. err = errors.New("获取用户申请信息失败, Err: " + e.Error())
  96. return
  97. }
  98. // 潜在用户
  99. if companyId <= 1 {
  100. authInfo.HasPermission = 6
  101. if applyCount > 0 {
  102. authInfo.HasPermission = 7
  103. }
  104. authInfo.OperationMode = UserPermissionOperationModeApply
  105. authInfo.PopupMsg = UserPermissionPopupMsgApplyActivity
  106. return
  107. }
  108. // 销售信息
  109. ficcSeller, e := models.GetSellerByCompanyIdCheckFicc(companyId, utils.COMPANY_PRODUCT_FICC_ID)
  110. if e != nil && e.Error() != utils.ErrNoRow() {
  111. err = errors.New("获取FICC销售信息失败, Err: " + e.Error())
  112. return
  113. }
  114. raiSeller, e := models.GetSellerByCompanyIdCheckFicc(companyId, utils.COMPANY_PRODUCT_RAI_ID)
  115. if e != nil && e.Error() != utils.ErrNoRow() {
  116. err = errors.New("获取权益销售信息失败, Err: " + e.Error())
  117. return
  118. }
  119. if raiSeller != nil {
  120. authInfo.SellerMobile = raiSeller.Mobile
  121. authInfo.SellerName = raiSeller.RealName
  122. }
  123. // 仅有FICC权限
  124. if ficcSeller != nil && raiSeller == nil {
  125. authInfo.HasPermission = 2
  126. authInfo.OperationMode = UserPermissionOperationModeApply
  127. authInfo.PopupMsg = UserPermissionPopupMsgApplyActivity
  128. return
  129. }
  130. // permissions示例: 医药,消费,科技,智造,策略,专家,买方研选
  131. permissions, e := models.GetCompanyPermissionName(companyId)
  132. if e != nil {
  133. err = errors.New("获取客户权限失败, Err: " + e.Error())
  134. return
  135. }
  136. if permissions == "" {
  137. // 无权益权限
  138. if raiSeller != nil {
  139. // 有销售信息
  140. authInfo.HasPermission = 2
  141. authInfo.OperationMode = UserPermissionOperationModeCall
  142. authInfo.PopupMsg = UserPermissionPopupMsgCallActivity
  143. return
  144. }
  145. // 无销售信息, 无申请, 视作潜在用户
  146. authInfo.HasPermission = 3
  147. authInfo.OperationMode = UserPermissionOperationModeApply
  148. return
  149. }
  150. // 有基本的权益权限
  151. authInfo.HasPermission = 1
  152. permissionArr = strings.Split(permissions, ",")
  153. //判断用户是否开通了个人研选权限,如果有权限后缀拼接权限名称
  154. mfyxUserPermissionTotal := GetMfyxUserPermissionTotal(userId)
  155. if mfyxUserPermissionTotal == 1 {
  156. permissionArr = append(permissionArr, utils.CHART_PERMISSION_NAME_MF_YANXUAN)
  157. }
  158. return
  159. }
  160. // 查询研选的权限状态
  161. func GetUserRaiPermissionYanXuanInfo(user *models.WxUserItem) (hasPermission int, err error) {
  162. //判断用户是否开通了个人研选权限
  163. mfyxUserPermissionTotal := GetMfyxUserPermissionTotal(user.UserId)
  164. if mfyxUserPermissionTotal == 1 {
  165. hasPermission = 1
  166. return
  167. }
  168. var condition string
  169. var pars []interface{}
  170. condition = " AND company_id = ? AND status IN ('正式','试用') AND chart_permission_id = ? ORDER BY company_report_permission_id DESC LIMIT 1 "
  171. pars = append(pars, user.CompanyId, utils.CHART_PERMISSION_ID_YANXUAN)
  172. companyReportPermissionDetail, e := models.GetCompanyReportPermissionDetailByCondition(condition, pars)
  173. if e != nil && e.Error() != utils.ErrNoRow() {
  174. err = errors.New("GetCompanyReportPermissionDetailByCondition, Err: " + e.Error())
  175. return
  176. }
  177. //如果用户没有研选权限,那么就获取他对应的状态码
  178. if companyReportPermissionDetail == nil {
  179. hasPermission, _, _, _, e = GetUserHasPermissionArticle(user)
  180. if e != nil {
  181. err = errors.New("GetUserHasPermissionArticle, Err: " + e.Error())
  182. return
  183. }
  184. } else {
  185. hasPermission = 1
  186. }
  187. return
  188. }
  189. func GetUserApplyRecordCount(userId int) (applyCount int, err error) {
  190. // 用户申请记录
  191. applyCount, e := models.GetApplyRecordCount(userId)
  192. if e != nil && e.Error() != utils.ErrNoRow() {
  193. err = errors.New("获取用户申请信息失败, Err: " + e.Error())
  194. return
  195. }
  196. return
  197. }
  198. // 是否需要上传名片
  199. func GetCygxUserBusinessCardCount(userId, companyId int) (isNeedBusinessCard bool) {
  200. if companyId > 1 {
  201. return
  202. }
  203. var err error
  204. defer func() {
  205. if err != nil {
  206. fmt.Println(err)
  207. go utils.SendAlarmMsg(fmt.Sprint("是否需要上传名片失败 GetCygxUserBusinessCardCount, err:", err.Error()), 2)
  208. }
  209. }()
  210. businessCardCount, e := order.GetCygxUserBusinessCardCount(userId)
  211. if e != nil && e.Error() != utils.ErrNoRow() {
  212. err = errors.New("GetCygxUserBusinessCardCount, Err: " + e.Error())
  213. return
  214. }
  215. if businessCardCount > 0 {
  216. return
  217. }
  218. isNeedBusinessCard = true
  219. return
  220. }
  221. // 获取客户是否有过历史申请记录
  222. func GetUserApplyRecordCountByCompanyIdPay(companyIdPay int) (isCompanyApply bool) {
  223. var err error
  224. defer func() {
  225. if err != nil {
  226. fmt.Println(err)
  227. go utils.SendAlarmMsg(fmt.Sprint("获取客户是否有过历史申请记录失败 GetUserApplyRecordCountByCompanyIdPay, err:", err.Error()), 2)
  228. }
  229. }()
  230. //if companyIdPay >= 1 {
  231. // return
  232. //}
  233. // 客户申请
  234. //applyCount, e := models.GetApplyRecordCountByCompanyIdPay(companyIdPay)
  235. //if e != nil {
  236. // err = errors.New("GetApplyRecordCountByCompanyIdPay, Err: " + e.Error())
  237. // return
  238. //}
  239. //有研选订阅的权限的也不开放申请按钮
  240. permissionCount, e := models.GetCompanyPermissionCheck(companyIdPay, utils.CHART_PERMISSION_ID_YANXUAN)
  241. if e != nil {
  242. err = errors.New("GetCompanyPermissionCheck, Err: " + e.Error())
  243. return
  244. }
  245. //if applyCount > 0 || permissionCount > 0 {
  246. if permissionCount > 0 {
  247. isCompanyApply = true
  248. }
  249. return
  250. }
  251. // 获取用户权限状态 https://hzstatic.hzinsights.com/static/images/202402/20240205/LpE6dspJCLzfQoCoE8SFMDiLuxXk.png(状态码说明)
  252. func GetUserPermissionCode(userId, companyId int) (permission int, err error) {
  253. // 用户申请记录
  254. applyCount, e := models.GetApplyRecordCount(userId)
  255. if e != nil && e.Error() != utils.ErrNoRow() {
  256. err = errors.New("获取用户申请信息失败, Err: " + e.Error())
  257. return
  258. }
  259. if companyId == 1 {
  260. // 潜在用户
  261. if applyCount > 0 {
  262. permission = 6
  263. } else {
  264. permission = 7
  265. }
  266. } else {
  267. //权益客户
  268. raiCount, e := models.GetCompanyProductCount(companyId, utils.COMPANY_PRODUCT_RAI_ID)
  269. if e != nil {
  270. err = errors.New("获取用户申请信息失败, Err: " + e.Error())
  271. return
  272. }
  273. if raiCount == 1 {
  274. if applyCount > 0 {
  275. permission = 2
  276. } else {
  277. permission = 3
  278. }
  279. } else {
  280. //ficc 客户
  281. ficcCount, e := models.GetCompanyProductCount(companyId, utils.COMPANY_PRODUCT_FICC_ID)
  282. if e != nil {
  283. err = errors.New("获取用户申请信息失败, Err: " + e.Error())
  284. return
  285. }
  286. if ficcCount == 1 {
  287. if applyCount > 0 {
  288. permission = 4
  289. } else {
  290. permission = 5
  291. }
  292. }
  293. }
  294. }
  295. return
  296. }
  297. // 用户详情页获取用户权限状态 https://hzstatic.hzinsights.com/static/images/202402/20240205/LpE6dspJCLzfQoCoE8SFMDiLuxXk.png(状态码说明)
  298. func GetUserDetailPermissionCode(userId, companyId int) (permission int, err error) {
  299. // 用户申请记录
  300. applyCount, e := models.GetApplyRecordCount(userId)
  301. if e != nil && e.Error() != utils.ErrNoRow() {
  302. err = errors.New("获取用户申请信息失败, Err: " + e.Error())
  303. return
  304. }
  305. if companyId == 1 {
  306. // 潜在用户
  307. if applyCount > 0 {
  308. permission = 6
  309. } else {
  310. permission = 7
  311. }
  312. } else {
  313. //权益客户
  314. raiCount, e := models.GetCompanyProductCount(companyId, utils.COMPANY_PRODUCT_RAI_ID)
  315. if e != nil {
  316. err = errors.New("获取用户申请信息失败, Err: " + e.Error())
  317. return
  318. }
  319. if raiCount == 1 {
  320. if applyCount > 0 {
  321. permission = 2
  322. } else {
  323. permission = 3
  324. }
  325. } else {
  326. //ficc 客户
  327. ficcCount, e := models.GetCompanyProductCount(companyId, utils.COMPANY_PRODUCT_FICC_ID)
  328. if e != nil {
  329. err = errors.New("获取用户申请信息失败, Err: " + e.Error())
  330. return
  331. }
  332. if ficcCount == 1 {
  333. if applyCount > 0 {
  334. permission = 4
  335. } else {
  336. permission = 5
  337. }
  338. }
  339. }
  340. }
  341. return
  342. }
  343. // 获取权益销售姓名
  344. func GetSellerName(user *models.WxUserItem) (sellerName string, sellerId int) {
  345. var err error
  346. defer func() {
  347. if err != nil {
  348. fmt.Println(err)
  349. go utils.SendAlarmMsg(fmt.Sprint("获取权益销售姓名失败 GetSellerName, err:", err.Error()), 2)
  350. }
  351. }()
  352. companyId := user.CompanyId
  353. //潜在客户没有销售
  354. if user.CompanyId <= 1 {
  355. return
  356. }
  357. //权益客户
  358. raiCount, e := models.GetCompanyProductCount(companyId, utils.COMPANY_PRODUCT_RAI_ID)
  359. if e != nil {
  360. err = errors.New("GetCompanyProductCount, Err: " + e.Error())
  361. return
  362. }
  363. //仅开通FICC的客户不展示销售姓名
  364. if raiCount == 0 {
  365. return
  366. }
  367. sealldetail, e := models.GetRaiSellerByCompanyId(companyId)
  368. if e != nil {
  369. err = errors.New("GetRaiSellerByCompanyId, Err: " + e.Error())
  370. return
  371. }
  372. sellerName = sealldetail.RealName
  373. sellerId = sealldetail.AdminId
  374. return
  375. }