user_permission.go 12 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393
  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("判断用户是否开通了个人研选权限失败 GetCygxGoodsList, 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("判断用户是否开通了个人研选权限失败 GetCygxGoodsList, err:", err.Error()), 2)
  228. }
  229. }()
  230. if companyIdPay >= 1 {
  231. return
  232. }
  233. // 客户申请
  234. applyCount, e := models.GetApplyRecordCountByCompanyIdPay(companyIdPay)
  235. if e != nil && e.Error() != utils.ErrNoRow() {
  236. err = errors.New("获取用户申请信息失败, Err: " + e.Error())
  237. return
  238. }
  239. if applyCount > 0 {
  240. isCompanyApply = true
  241. }
  242. return
  243. }
  244. // 获取用户权限状态 https://hzstatic.hzinsights.com/static/images/202402/20240205/LpE6dspJCLzfQoCoE8SFMDiLuxXk.png(状态码说明)
  245. func GetUserPermissionCode(userId, companyId int) (permission int, err error) {
  246. // 用户申请记录
  247. applyCount, e := models.GetApplyRecordCount(userId)
  248. if e != nil && e.Error() != utils.ErrNoRow() {
  249. err = errors.New("获取用户申请信息失败, Err: " + e.Error())
  250. return
  251. }
  252. if companyId == 1 {
  253. // 潜在用户
  254. if applyCount > 0 {
  255. permission = 6
  256. } else {
  257. permission = 7
  258. }
  259. } else {
  260. //权益客户
  261. raiCount, e := models.GetCompanyProductCount(companyId, utils.COMPANY_PRODUCT_RAI_ID)
  262. if e != nil {
  263. err = errors.New("获取用户申请信息失败, Err: " + e.Error())
  264. return
  265. }
  266. if raiCount == 1 {
  267. if applyCount > 0 {
  268. permission = 2
  269. } else {
  270. permission = 3
  271. }
  272. } else {
  273. //ficc 客户
  274. ficcCount, e := models.GetCompanyProductCount(companyId, utils.COMPANY_PRODUCT_FICC_ID)
  275. if e != nil {
  276. err = errors.New("获取用户申请信息失败, Err: " + e.Error())
  277. return
  278. }
  279. if ficcCount == 1 {
  280. if applyCount > 0 {
  281. permission = 4
  282. } else {
  283. permission = 5
  284. }
  285. }
  286. }
  287. }
  288. return
  289. }
  290. // 用户详情页获取用户权限状态 https://hzstatic.hzinsights.com/static/images/202402/20240205/LpE6dspJCLzfQoCoE8SFMDiLuxXk.png(状态码说明)
  291. func GetUserDetailPermissionCode(userId, companyId int) (permission int, err error) {
  292. // 用户申请记录
  293. applyCount, e := models.GetApplyRecordCount(userId)
  294. if e != nil && e.Error() != utils.ErrNoRow() {
  295. err = errors.New("获取用户申请信息失败, Err: " + e.Error())
  296. return
  297. }
  298. fmt.Println("companyId", companyId)
  299. if companyId == 1 {
  300. // 潜在用户
  301. if applyCount > 0 {
  302. permission = 6
  303. } else {
  304. permission = 7
  305. }
  306. } else {
  307. //权益客户
  308. raiCount, e := models.GetCompanyProductCount(companyId, utils.COMPANY_PRODUCT_RAI_ID)
  309. if e != nil {
  310. err = errors.New("获取用户申请信息失败, Err: " + e.Error())
  311. return
  312. }
  313. if raiCount == 1 {
  314. if applyCount > 0 {
  315. permission = 2
  316. } else {
  317. permission = 3
  318. }
  319. } else {
  320. //ficc 客户
  321. ficcCount, e := models.GetCompanyProductCount(companyId, utils.COMPANY_PRODUCT_FICC_ID)
  322. if e != nil {
  323. err = errors.New("获取用户申请信息失败, Err: " + e.Error())
  324. return
  325. }
  326. if ficcCount == 1 {
  327. if applyCount > 0 {
  328. permission = 4
  329. } else {
  330. permission = 5
  331. }
  332. }
  333. }
  334. }
  335. fmt.Println("permission", permission)
  336. return
  337. }
  338. // 获取权益销售姓名
  339. func GetSellerName(user *models.WxUserItem) (sellerName string, sellerId int) {
  340. var err error
  341. defer func() {
  342. if err != nil {
  343. fmt.Println(err)
  344. go utils.SendAlarmMsg(fmt.Sprint("获取权益销售姓名失败 GetSellerName, err:", err.Error()), 2)
  345. }
  346. }()
  347. companyId := user.CompanyId
  348. //潜在客户没有销售
  349. if user.CompanyId <= 1 {
  350. return
  351. }
  352. //权益客户
  353. raiCount, e := models.GetCompanyProductCount(companyId, utils.COMPANY_PRODUCT_RAI_ID)
  354. if e != nil {
  355. err = errors.New("GetCompanyProductCount, Err: " + e.Error())
  356. return
  357. }
  358. //仅开通FICC的客户不展示销售姓名
  359. if raiCount == 0 {
  360. return
  361. }
  362. sealldetail, e := models.GetRaiSellerByCompanyId(companyId)
  363. if e != nil {
  364. err = errors.New("GetRaiSellerByCompanyId, Err: " + e.Error())
  365. return
  366. }
  367. sellerName = sealldetail.RealName
  368. sellerId = sealldetail.AdminId
  369. return
  370. }