permission.go 33 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798991001011021031041051061071081091101111121131141151161171181191201211221231241251261271281291301311321331341351361371381391401411421431441451461471481491501511521531541551561571581591601611621631641651661671681691701711721731741751761771781791801811821831841851861871881891901911921931941951961971981992002012022032042052062072082092102112122132142152162172182192202212222232242252262272282292302312322332342352362372382392402412422432442452462472482492502512522532542552562572582592602612622632642652662672682692702712722732742752762772782792802812822832842852862872882892902912922932942952962972982993003013023033043053063073083093103113123133143153163173183193203213223233243253263273283293303313323333343353363373383393403413423433443453463473483493503513523533543553563573583593603613623633643653663673683693703713723733743753763773783793803813823833843853863873883893903913923933943953963973983994004014024034044054064074084094104114124134144154164174184194204214224234244254264274284294304314324334344354364374384394404414424434444454464474484494504514524534544554564574584594604614624634644654664674684694704714724734744754764774784794804814824834844854864874884894904914924934944954964974984995005015025035045055065075085095105115125135145155165175185195205215225235245255265275285295305315325335345355365375385395405415425435445455465475485495505515525535545555565575585595605615625635645655665675685695705715725735745755765775785795805815825835845855865875885895905915925935945955965975985996006016026036046056066076086096106116126136146156166176186196206216226236246256266276286296306316326336346356366376386396406416426436446456466476486496506516526536546556566576586596606616626636646656666676686696706716726736746756766776786796806816826836846856866876886896906916926936946956966976986997007017027037047057067077087097107117127137147157167177187197207217227237247257267277287297307317327337347357367377387397407417427437447457467477487497507517527537547557567577587597607617627637647657667677687697707717727737747757767777787797807817827837847857867877887897907917927937947957967977987998008018028038048058068078088098108118128138148158168178188198208218228238248258268278288298308318328338348358368378388398408418428438448458468478488498508518528538548558568578588598608618628638648658668678688698708718728738748758768778788798808818828838848858868878888898908918928938948958968978988999009019029039049059069079089099109119129139149159169179189199209219229239249259269279289299309319329339349359369379389399409419429439449459469479489499509519529539549559569579589599609619629639649659669679689699709719729739749759769779789799809819829839849859869879889899909919929939949959969979989991000100110021003100410051006100710081009101010111012101310141015101610171018101910201021102210231024102510261027102810291030103110321033103410351036103710381039104010411042104310441045104610471048104910501051105210531054105510561057105810591060106110621063106410651066106710681069107010711072107310741075107610771078107910801081108210831084108510861087108810891090109110921093109410951096109710981099110011011102110311041105110611071108110911101111111211131114111511161117111811191120112111221123112411251126
  1. package company
  2. import (
  3. "errors"
  4. "fmt"
  5. companyCache "hongze/hongze_yb/cache/company"
  6. "hongze/hongze_yb/global"
  7. "hongze/hongze_yb/models/response"
  8. "hongze/hongze_yb/models/tables/admin"
  9. "hongze/hongze_yb/models/tables/chart_permission"
  10. "hongze/hongze_yb/models/tables/company"
  11. "hongze/hongze_yb/models/tables/company_product"
  12. "hongze/hongze_yb/models/tables/company_report_permission"
  13. "hongze/hongze_yb/models/tables/company_user_chart_classify_permission"
  14. "hongze/hongze_yb/models/tables/wx_user"
  15. "hongze/hongze_yb/models/tables/yb_apply_record"
  16. "hongze/hongze_yb/services/user"
  17. "hongze/hongze_yb/utils"
  18. "sort"
  19. "strings"
  20. "time"
  21. )
  22. var (
  23. CheckTypeApply = "apply"
  24. CheckTypeContact = "contact"
  25. CheckTypeExpire = "expired"
  26. )
  27. // GetClassNameListByProductId 根据权限id获取权限分类
  28. func GetClassNameListByProductId(productId int64) (list []*chart_permission.ChartPermission, err error) {
  29. list, err = chart_permission.GetClassNameListByProductId(productId)
  30. return
  31. }
  32. // GetPermissionListByProductId 根据product_id获取所有的权限列表
  33. func GetPermissionListByProductId(productId int64) (list []*chart_permission.ChartPermission, err error) {
  34. list, err = chart_permission.GetListByProductId(productId)
  35. return
  36. }
  37. // GetValidPermissionByCompany2ProductId 根据客户id和产品id获取有效的权限列表
  38. func GetValidPermissionByCompany2ProductId(companyId, productId int64) (list []*company_report_permission.CompanyReportPermission, err error) {
  39. where := make(map[string]interface{})
  40. where["company_id ="] = companyId
  41. where["product_id ="] = productId
  42. where["status in"] = []string{"正式", "试用", "永续"}
  43. list, err = company_report_permission.GetByWhereMap(where)
  44. return
  45. }
  46. // GetPurchasePermissionIdsByCompany2ProductId 根据客户id和产品id获取有效的权限id集合
  47. func GetPurchasePermissionIdsByCompany2ProductId(companyId, productId int64) (permissionIds []int, err error) {
  48. where := make(map[string]interface{})
  49. where["company_id ="] = companyId
  50. where["product_id ="] = productId
  51. where["status in"] = []string{"正式", "永续"}
  52. list, err := company_report_permission.GetByWhereMap(where)
  53. if err != nil {
  54. return
  55. }
  56. for _, v := range list {
  57. permissionIds = append(permissionIds, v.ChartPermissionID)
  58. }
  59. return
  60. }
  61. // GetValidPermissionIdListByCompany2ProductId 根据客户id和产品id获取有效的权限id列表
  62. func GetValidPermissionIdListByCompany2ProductId(companyId, productId int64) (list []int, err error) {
  63. companyReportPermissionList, err := GetValidPermissionByCompany2ProductId(companyId, productId)
  64. if err != nil {
  65. return
  66. }
  67. for _, v := range companyReportPermissionList {
  68. list = append(list, v.ChartPermissionID)
  69. }
  70. return
  71. }
  72. // PermissionCheckInfo 权限校验完成后的结果
  73. type PermissionCheckInfo struct {
  74. Name string `json:"name" description:"销售名称"`
  75. Mobile string `json:"mobile" description:"手机号"`
  76. Type string `json:"type" description:"校验失败,没有权限,需要让前端处理的类型,枚举值:apply,contact"`
  77. CustomerInfo CustomerInfo `json:"customer_info" description:"客户信息"`
  78. }
  79. // CustomerInfo 客户信息
  80. type CustomerInfo struct {
  81. CompanyName string `json:"company_name" description:"客户(公司)名称"`
  82. Name string `json:"name" description:"联系人名称"`
  83. Mobile string `json:"mobile" description:"手机号"`
  84. Status string `json:"status" description:"状态"`
  85. IsSuspend int8 `json:"is_suspend" description:"启用与否字段:1:暂停,0:启用"`
  86. HasApply bool `json:"has_apply" description:"是否有申请过"`
  87. }
  88. // CheckBaseFiccPermission 校验用户基本的FICC权限
  89. func CheckBaseFiccPermission(companyId int64, userId int) (ok bool, checkInfo response.PermissionCheckInfo, permissionIdArr []int, err error) {
  90. defer func() {
  91. // 无权限查询是否有申请记录
  92. if !ok {
  93. if checkInfo.Type == CheckTypeApply {
  94. if _, e := yb_apply_record.GetLastNotOpRecordByUserId(userId); e == nil {
  95. checkInfo.CustomerInfo.HasApply = true
  96. }
  97. }
  98. }
  99. }()
  100. // 潜在用户
  101. if companyId == 1 {
  102. checkInfo.Type = CheckTypeApply
  103. return
  104. }
  105. // 是否开通FICC
  106. productId := int64(1)
  107. productInfo, e := company_product.GetByCompany2ProductId(companyId, productId)
  108. if e != nil {
  109. if e == utils.ErrNoRow {
  110. checkInfo.Type = CheckTypeApply
  111. return
  112. }
  113. err = errors.New("获取用户FICC权限失败 Err:" + e.Error())
  114. return
  115. }
  116. wxUser, e := wx_user.GetByUserId(userId)
  117. if e != nil {
  118. err = errors.New("不存在当前用户, Err:" + e.Error())
  119. return
  120. }
  121. // 是否为弘则研究已禁用的联系人
  122. if companyId == 16 {
  123. userInfo, e := wx_user.GetByUserId(userId)
  124. if e != nil {
  125. err = errors.New("用户信息异常, Err:" + e.Error())
  126. return
  127. }
  128. if userInfo.Enabled != 1 {
  129. checkInfo.Type = CheckTypeApply
  130. return
  131. }
  132. }
  133. customerInfo := response.CustomerInfo{
  134. CompanyName: productInfo.CompanyName,
  135. Status: productInfo.Status,
  136. Name: wxUser.RealName,
  137. IsSuspend: productInfo.IsSuspend,
  138. Mobile: wxUser.Mobile,
  139. }
  140. checkInfo.CustomerInfo = customerInfo
  141. // 客户状态是否为流失
  142. if productInfo.Status == "流失" || productInfo.Status == "关闭" {
  143. checkInfo.Type = CheckTypeApply
  144. return
  145. }
  146. // 客户对应的销售信息
  147. sellerInfo, e := admin.GetByAdminId(productInfo.SellerID)
  148. if e != nil {
  149. err = errors.New("获取用户销售信息失败 Err:" + e.Error())
  150. return
  151. }
  152. checkInfo.Name = sellerInfo.RealName
  153. checkInfo.Mobile = sellerInfo.Mobile
  154. if productInfo.Status == "冻结" || (productInfo.Status == "试用" && productInfo.IsSuspend == 1) {
  155. checkInfo.Type = CheckTypeContact
  156. return
  157. }
  158. // 正常用户, 获取有效权限
  159. permissionIdArr, e = GetValidPermissionIdListByCompany2ProductId(companyId, productId)
  160. if e != nil {
  161. err = errors.New("获取用户有效权限列表失败 Err:" + e.Error())
  162. return
  163. }
  164. ok = true
  165. return
  166. }
  167. // CheckPermissionByPermissionIdList2Ficc 根据权限id集合权限校验(ficc)
  168. func CheckPermissionByPermissionIdList2Ficc(companyId int64, userId int, permissionIdList []int) (ok bool, permissionCheckInfo PermissionCheckInfo, err error) {
  169. defer func() {
  170. // 如果无权限,那么就去查询是否申请过
  171. if ok == false {
  172. ok, _ = checkPermissionByPermissionIdList2Rai(companyId, userId, permissionIdList)
  173. }
  174. if ok == false && permissionCheckInfo.Type == CheckTypeApply {
  175. _, err = yb_apply_record.GetLastNotOpRecordByUserId(userId) // 从来源我的/活动申请的记录
  176. if err != nil && err != utils.ErrNoRow {
  177. return
  178. }
  179. //查询是否有申请过,如果有申请过的话,那么err是nil
  180. if err == nil {
  181. permissionCheckInfo.CustomerInfo.HasApply = true
  182. }
  183. }
  184. }()
  185. //非潜在客户
  186. if len(permissionIdList) <= 0 {
  187. err = fmt.Errorf("权限异常,请传入需要校验的权限")
  188. return
  189. }
  190. permissionMap := make(map[int]bool)
  191. for _, permissionId := range permissionIdList {
  192. permissionMap[permissionId] = true
  193. }
  194. var productId int64
  195. productId = 1
  196. if companyId > 1 {
  197. //查询是否 开通ficc的客户
  198. companyProductInfo, tmpErr := company_product.GetByCompany2ProductId(companyId, productId)
  199. if tmpErr != nil {
  200. // 没有开通ficc的客户
  201. if tmpErr == utils.ErrNoRow {
  202. permissionCheckInfo.Type = CheckTypeApply
  203. return
  204. }
  205. err = tmpErr
  206. return
  207. }
  208. wxUser, tmpErr := wx_user.GetByUserId(userId)
  209. if tmpErr != nil {
  210. permissionCheckInfo.Type = CheckTypeApply
  211. err = tmpErr
  212. return
  213. }
  214. // 查询用户是否为弘则研究已禁用的联系人
  215. if companyId == 16 {
  216. if wxUser.Enabled != 1 {
  217. permissionCheckInfo.Type = CheckTypeApply
  218. return
  219. }
  220. }
  221. //客户信息
  222. companyInfo, tmpErr := company.GetByCompanyId(companyId)
  223. if tmpErr != nil {
  224. // 没有开通ficc的客户
  225. if tmpErr == utils.ErrNoRow {
  226. permissionCheckInfo.Type = CheckTypeApply
  227. return
  228. }
  229. err = tmpErr
  230. return
  231. }
  232. customerInfo := CustomerInfo{
  233. CompanyName: companyInfo.CompanyName,
  234. Status: companyProductInfo.Status,
  235. Name: wxUser.RealName,
  236. IsSuspend: companyProductInfo.IsSuspend,
  237. Mobile: wxUser.Mobile,
  238. }
  239. permissionCheckInfo.CustomerInfo = customerInfo
  240. // 如果客户ficc产品的状态是流失,那么也是让去申请
  241. if companyProductInfo.Status == "流失" || companyProductInfo.Status == "关闭" {
  242. permissionCheckInfo.Type = CheckTypeApply
  243. return
  244. }
  245. //查找对应客户的销售信息
  246. adminInfo, tmpErr := admin.GetByAdminId(companyProductInfo.SellerID)
  247. if tmpErr != nil {
  248. //if tmpErr ==
  249. err = tmpErr
  250. return
  251. }
  252. permissionCheckInfo.Name = adminInfo.RealName
  253. permissionCheckInfo.Mobile = adminInfo.Mobile
  254. if companyProductInfo.Status == "冻结" {
  255. permissionCheckInfo.Type = CheckTypeContact
  256. return
  257. }
  258. //客户状态是:试用暂停状态(联系销售)
  259. if companyProductInfo.Status == "试用" && companyProductInfo.IsSuspend == 1 {
  260. permissionCheckInfo.Type = CheckTypeContact
  261. return
  262. }
  263. // 获取有效的权限id列表
  264. validPermissionIdList, tmpErr := GetValidPermissionIdListByCompany2ProductId(companyId, productId)
  265. if tmpErr != nil {
  266. err = tmpErr
  267. return
  268. }
  269. // 校验在有效的权限id列表中是否存在该权限
  270. for _, validPermissionId := range validPermissionIdList {
  271. //如果有该权限id,那么直接返回校验通过
  272. if _, validOk := permissionMap[validPermissionId]; validOk {
  273. ok = true
  274. return
  275. }
  276. }
  277. permissionCheckInfo.Type = CheckTypeContact
  278. } else {
  279. permissionCheckInfo.Type = CheckTypeApply
  280. }
  281. return
  282. }
  283. func GetCheckPermission(companyId int64, userId int, permissionIdList []int) (ok bool, permissionCheckInfo response.PermissionCheckInfo, finalValidPermissionIds []int, err error) {
  284. defer func() {
  285. // 如果无权限,那么就去查询是否申请过
  286. if ok == false {
  287. ok, _ = checkPermissionByPermissionIdList2Rai(companyId, userId, permissionIdList)
  288. }
  289. if ok == false && permissionCheckInfo.Type == CheckTypeApply {
  290. _, err = yb_apply_record.GetLastNotOpRecordByUserId(userId) // 从来源我的/活动申请的记录
  291. if err != nil {
  292. if err == utils.ErrNoRow {
  293. err = nil
  294. }
  295. return
  296. }
  297. //查询是否有申请过,如果有申请过的话,那么err是nil
  298. if err == nil {
  299. permissionCheckInfo.CustomerInfo.HasApply = true
  300. }
  301. }
  302. }()
  303. permissionCheckInfo.HzPhone = utils.HZPHONE
  304. //非潜在客户
  305. permissionMap := make(map[int]bool)
  306. if len(permissionIdList) > 0 {
  307. for _, permissionId := range permissionIdList {
  308. permissionMap[permissionId] = true
  309. }
  310. }
  311. var productId int64
  312. productId = 1
  313. if companyId > 1 {
  314. //查询是否 开通ficc的客户
  315. companyProductInfo, tmpErr := company_product.GetByCompany2ProductId(companyId, productId)
  316. if tmpErr != nil {
  317. // 没有开通ficc的客户
  318. if tmpErr == utils.ErrNoRow {
  319. permissionCheckInfo.Type = CheckTypeApply
  320. return
  321. }
  322. err = tmpErr
  323. return
  324. }
  325. wxUser, tmpErr := wx_user.GetByUserId(userId)
  326. if tmpErr != nil {
  327. permissionCheckInfo.Type = CheckTypeApply
  328. err = tmpErr
  329. return
  330. }
  331. //客户信息
  332. companyInfo, tmpErr := companyCache.GetByCompanyId(companyId)
  333. if tmpErr != nil {
  334. // 没有开通ficc的客户
  335. if tmpErr == utils.ErrNoRow {
  336. permissionCheckInfo.Type = CheckTypeApply
  337. return
  338. }
  339. err = tmpErr
  340. return
  341. }
  342. customerInfo := response.CustomerInfo{
  343. CompanyName: companyInfo.CompanyName,
  344. Status: companyProductInfo.Status,
  345. Name: wxUser.RealName,
  346. IsSuspend: companyProductInfo.IsSuspend,
  347. Mobile: wxUser.Mobile,
  348. }
  349. permissionCheckInfo.CustomerInfo = customerInfo
  350. // 如果客户ficc产品的状态是流失,那么也是让去申请
  351. if companyProductInfo.Status == "流失" || companyProductInfo.Status == "关闭" {
  352. permissionCheckInfo.Type = CheckTypeApply
  353. return
  354. }
  355. //查找对应客户的销售信息
  356. adminInfo, tmpErr := admin.GetByAdminId(companyProductInfo.SellerID)
  357. if tmpErr != nil {
  358. //if tmpErr ==
  359. err = tmpErr
  360. return
  361. }
  362. permissionCheckInfo.Name = adminInfo.RealName
  363. permissionCheckInfo.Mobile = adminInfo.Mobile
  364. if companyProductInfo.Status == "冻结" {
  365. permissionCheckInfo.Type = CheckTypeContact
  366. return
  367. }
  368. //客户状态是:试用暂停状态(联系销售)
  369. if companyProductInfo.Status == "试用" && companyProductInfo.IsSuspend == 1 {
  370. permissionCheckInfo.Type = CheckTypeContact
  371. return
  372. }
  373. // 获取有效的权限id列表
  374. validPermissionIdList, tmpErr := GetValidPermissionIdListByCompany2ProductId(companyId, productId)
  375. if tmpErr != nil {
  376. err = tmpErr
  377. return
  378. }
  379. // 校验在有效的权限id列表中是否存在该权限
  380. if len(permissionIdList) > 0 {
  381. for _, validPermissionId := range validPermissionIdList {
  382. //如果有该权限id,那么直接返回校验通过
  383. if _, validOk := permissionMap[validPermissionId]; validOk {
  384. finalValidPermissionIds = append(finalValidPermissionIds, validPermissionId)
  385. ok = true
  386. }
  387. }
  388. return
  389. }
  390. permissionCheckInfo.Type = CheckTypeContact
  391. } else {
  392. permissionCheckInfo.Type = CheckTypeApply
  393. }
  394. return
  395. }
  396. // checkPermissionByPermissionIdList2Rai 根据权限id集合权限校验(权益)
  397. func checkPermissionByPermissionIdList2Rai(companyId int64, userId int, permissionIdList []int) (ok bool, err error) {
  398. //非潜在客户
  399. if len(permissionIdList) <= 0 {
  400. err = fmt.Errorf("权限异常,请传入需要校验的权限")
  401. return
  402. }
  403. // 查询用户是否为弘则研究已禁用的联系人
  404. wxUser, tmpErr := wx_user.GetByUserId(userId)
  405. if tmpErr != nil {
  406. err = fmt.Errorf("用户信息异常")
  407. return
  408. }
  409. if companyId == 16 {
  410. if wxUser.Enabled != 1 {
  411. return
  412. }
  413. }
  414. //市场策略(ficc权限id):33
  415. ficcPermissionId := 33
  416. //市场策略(权益权限id):23
  417. raiPermissionId := 23
  418. //是否往下校验
  419. var permissionOk bool
  420. for _, permissionId := range permissionIdList {
  421. if permissionId == ficcPermissionId {
  422. permissionOk = true
  423. }
  424. }
  425. var productId int64
  426. productId = 2
  427. if companyId > 1 && permissionOk {
  428. // 获取有效的权限id列表
  429. validPermissionIdList, tmpErr := GetValidPermissionIdListByCompany2ProductId(companyId, productId)
  430. if tmpErr != nil {
  431. err = tmpErr
  432. return
  433. }
  434. // 校验在有效的权限id列表中是否存在该权限
  435. for _, validPermissionId := range validPermissionIdList {
  436. if validPermissionId == raiPermissionId {
  437. ok = true
  438. return
  439. }
  440. }
  441. }
  442. return
  443. }
  444. // ChartPermissionCheckInfo 图表权限验证信息
  445. type ChartPermissionCheckInfo struct {
  446. Name string `json:"name" description:"销售名称"`
  447. Mobile string `json:"mobile" description:"手机号"`
  448. Type string `json:"type" description:"无权限,需要前端处理的类型,枚举值:expired, apply, contact"`
  449. CustomerInfo CustomerInfo `json:"customer_info" description:"客户信息"`
  450. Jump string `json:"jump" description:"需要跳转的页面,sandbox_list"`
  451. }
  452. // CheckUserChartPermission 验证用户/联系人的图库权限
  453. func CheckUserChartPermission(companyId int64, userId int) (ok bool, permissionCheckInfo ChartPermissionCheckInfo, err error) {
  454. defer func() {
  455. // 如果无权限,那么就去查询是否申请过
  456. if ok == false && permissionCheckInfo.Type == CheckTypeApply {
  457. _, err = yb_apply_record.GetLastNotOpRecordByUserId(userId) // 图库申请
  458. // 查询是否有申请过,如果有申请过的话,那么err是nil
  459. if err != nil {
  460. if err == utils.ErrNoRow {
  461. err = nil
  462. return
  463. }
  464. return
  465. }
  466. permissionCheckInfo.CustomerInfo.HasApply = true
  467. }
  468. }()
  469. var productId int64
  470. productId = 1
  471. if companyId > 1 {
  472. // 查询是否开通FICC
  473. companyProductInfo, tmpErr := company_product.GetByCompany2ProductId(companyId, productId)
  474. if tmpErr != nil {
  475. // 没有开通FICC
  476. if tmpErr == utils.ErrNoRow {
  477. permissionCheckInfo.Type = CheckTypeApply
  478. return
  479. }
  480. err = tmpErr
  481. return
  482. }
  483. wxUser, tmpErr := wx_user.GetByUserId(userId)
  484. if tmpErr != nil {
  485. permissionCheckInfo.Type = CheckTypeApply
  486. err = tmpErr
  487. return
  488. }
  489. // 查询用户是否为弘则研究已禁用的联系人
  490. if companyId == 16 {
  491. if wxUser.Enabled != 1 {
  492. permissionCheckInfo.Type = CheckTypeApply
  493. return
  494. }
  495. }
  496. // 客户信息
  497. companyInfo, tmpErr := company.GetByCompanyId(companyId)
  498. if tmpErr != nil {
  499. // 没有开通FICC
  500. if tmpErr == utils.ErrNoRow {
  501. permissionCheckInfo.Type = CheckTypeApply
  502. return
  503. }
  504. err = tmpErr
  505. return
  506. }
  507. customerInfo := CustomerInfo{
  508. CompanyName: companyInfo.CompanyName,
  509. Status: companyProductInfo.Status,
  510. Name: wxUser.RealName,
  511. IsSuspend: companyProductInfo.IsSuspend,
  512. Mobile: wxUser.Mobile,
  513. }
  514. permissionCheckInfo.CustomerInfo = customerInfo
  515. // 如果客户FICC产品的状态是流失-申请
  516. if companyProductInfo.Status == "流失" || companyProductInfo.Status == "关闭" {
  517. permissionCheckInfo.Type = CheckTypeApply
  518. return
  519. }
  520. // 查找对应客户的销售信息
  521. adminInfo, tmpErr := admin.GetByAdminId(companyProductInfo.SellerID)
  522. if tmpErr != nil {
  523. err = tmpErr
  524. return
  525. }
  526. // 客户状态为冻结/试用暂停-联系销售
  527. permissionCheckInfo.Name = adminInfo.RealName
  528. permissionCheckInfo.Mobile = adminInfo.Mobile
  529. if companyProductInfo.Status == "冻结" || (companyProductInfo.Status == "试用" && companyProductInfo.IsSuspend == 1) {
  530. permissionCheckInfo.Type = CheckTypeContact
  531. return
  532. }
  533. // 验证用户的图表权限
  534. permissionInfo, tmpErr := company_user_chart_classify_permission.GetPermissionByUserId(userId)
  535. if tmpErr != nil {
  536. if tmpErr == utils.ErrNoRow {
  537. // 无权限-联系销售
  538. permissionCheckInfo.Type = CheckTypeContact
  539. return
  540. }
  541. err = tmpErr
  542. return
  543. }
  544. nowTime := time.Now()
  545. if permissionInfo.Enabled != 1 {
  546. // 权限被禁用-联系销售
  547. permissionCheckInfo.Type = CheckTypeContact
  548. return
  549. }
  550. endTime := permissionInfo.EndTime.Add(23*time.Hour + 59*time.Minute + 59*time.Second)
  551. if nowTime.After(permissionInfo.StartTime) && nowTime.Before(endTime) {
  552. ok = true
  553. } else {
  554. // 权限已到期
  555. permissionCheckInfo.Type = CheckTypeExpire
  556. return
  557. }
  558. // 都不是默认联系销售
  559. permissionCheckInfo.Type = CheckTypeContact
  560. } else {
  561. permissionCheckInfo.Type = CheckTypeApply
  562. }
  563. return
  564. }
  565. type FiccPermissionList struct {
  566. ClassifyName string `description:"分类"`
  567. //HasPermission bool `description:"是否有权限"`
  568. Items []*PermissionItem `description:"子类"`
  569. }
  570. type PermissionItem struct {
  571. PermissionId int `description:"权限id"`
  572. PermissionName string `description:"权限名称"`
  573. //HasPermission bool `description:"是否有权限"`
  574. }
  575. // GetFiccPermissionList 获取FICC品种权限列表-不校验用户权限
  576. func GetFiccPermissionList() (list []*FiccPermissionList, err error) {
  577. productId := 1
  578. list = make([]*FiccPermissionList, 0)
  579. allFiccPermissions, e := chart_permission.GetListByProductIdAndPermissionType(productId)
  580. if e != nil {
  581. err = errors.New("获取品种权限失败 Err:" + e.Error())
  582. return
  583. }
  584. permissionMap := make(map[int][]*chart_permission.ChartPermission, 0)
  585. for _, v := range allFiccPermissions {
  586. if v.ParentId > 0 {
  587. permissionMap[v.ParentId] = append(permissionMap[v.ParentId], v)
  588. }
  589. }
  590. for _, v := range allFiccPermissions {
  591. if v.ParentId == 0 {
  592. classify := new(FiccPermissionList)
  593. classify.ClassifyName = v.PermissionName
  594. classify.Items = make([]*PermissionItem, 0)
  595. items, ok := permissionMap[int(v.ChartPermissionID)]
  596. if ok {
  597. for _, iv := range items {
  598. item := new(PermissionItem)
  599. item.PermissionId = int(iv.ChartPermissionID)
  600. item.PermissionName = iv.PermissionName
  601. classify.Items = append(classify.Items, item)
  602. }
  603. }
  604. list = append(list, classify)
  605. }
  606. }
  607. return
  608. }
  609. func GetHomeFiccPermissions(user user.UserInfo) (ret response.PermissionFiccResp, err error) {
  610. var errMsg string
  611. defer func() {
  612. if err != nil {
  613. global.LOG.Critical(fmt.Sprintf("GetHomeFiccPermissions: userId=%d, err:%s, errMsg:%s", user.UserID, err.Error(), errMsg))
  614. }
  615. }()
  616. validPermissionList, err := GetValidPermissionByCompany2ProductId(user.CompanyID, 1)
  617. if err != nil {
  618. return
  619. }
  620. var validPermissionIds []int
  621. for _, item := range validPermissionList {
  622. validPermissionIds = append(validPermissionIds, item.ChartPermissionID)
  623. }
  624. permissionList, err := chart_permission.GetFiccListExceptTacticByProductId()
  625. if err != nil {
  626. return
  627. }
  628. permissionMap := make(map[uint64]bool)
  629. permissionFirstMap := make(map[int]*response.PermissionFirstItem)
  630. //查询首页展示的图标
  631. permissionFirstList, err := chart_permission.GetFiccFirstListExceptTacticByProductId()
  632. if err != nil {
  633. return
  634. }
  635. for _, v := range permissionFirstList {
  636. permissionFirstMap[int(v.ChartPermissionID)] = &response.PermissionFirstItem{
  637. Id: int(v.ChartPermissionID),
  638. Sort: v.Sort + 1000,
  639. YbIndexName: v.PermissionName,
  640. YbIndexIcon: "",
  641. }
  642. }
  643. for k, v := range permissionList {
  644. if v.ParentId > 0 {
  645. permissionMap[v.ChartPermissionID] = false
  646. for _, myPerId := range validPermissionIds {
  647. if int(v.ChartPermissionID) == myPerId {
  648. permissionMap[v.ChartPermissionID] = true
  649. permissionList[k].Sort = v.Sort - 1000
  650. if _, ok := permissionFirstMap[v.ParentId]; ok && !permissionFirstMap[v.ParentId].IsCheck {
  651. permissionFirstMap[v.ParentId].AuthOk = true
  652. permissionFirstMap[v.ParentId].Sort -= 1000
  653. permissionFirstMap[v.ParentId].IsCheck = true
  654. }
  655. break
  656. }
  657. }
  658. }
  659. }
  660. for _, v := range permissionList {
  661. if v.ParentId > 0 {
  662. temp := new(response.PermissionFiccSecondItem)
  663. temp.Sort = v.Sort
  664. temp.ChartPermissionID = int(v.ChartPermissionID)
  665. temp.ChartPermissionName = v.PermissionName
  666. temp.AuthOk = permissionMap[v.ChartPermissionID]
  667. temp.PirceDrivenState = v.PriceDrivenState
  668. if _, ok := permissionFirstMap[v.ParentId]; ok {
  669. permissionFirstMap[v.ParentId].List = append(permissionFirstMap[v.ParentId].List, temp)
  670. } else {
  671. permissionFirstMap[v.ParentId] = &response.PermissionFirstItem{
  672. List: []*response.PermissionFiccSecondItem{temp},
  673. }
  674. }
  675. }
  676. }
  677. var list response.PermissionFiccList
  678. for _, v := range permissionFirstMap {
  679. temp := new(response.PermissionFiccItem)
  680. temp.Id = v.Id
  681. temp.Sort = v.Sort
  682. temp.ClassifyName = v.YbIndexName
  683. temp.IconUrl = v.YbIndexIcon
  684. if len(temp.IconUrl) > 4 {
  685. tmpIcon := temp.IconUrl[:len(temp.IconUrl)-4]
  686. temp.SelectIconUrl = tmpIcon + "_select.png"
  687. }
  688. if len(v.List) > 0 {
  689. temp.List = v.List
  690. sort.Sort(temp.List)
  691. }
  692. list = append(list, temp)
  693. }
  694. if len(list) > 0 {
  695. sort.Sort(list)
  696. }
  697. //判断是否是已购用户,如果是已购用户,需要返回对应的联系人方式
  698. companyProduct, err := company_product.GetByCompany2ProductId(user.CompanyID, 1)
  699. if err != nil {
  700. if err == utils.ErrNoRow {
  701. err = nil
  702. }
  703. }
  704. if err != nil {
  705. errMsg = err.Error()
  706. err = errors.New("查询用户状态失败")
  707. return
  708. }
  709. // 已购仅付费用户可见
  710. var contactInfo response.PermissionCheckContactInfo
  711. checkFlag := false
  712. if companyProduct.Status != "" && strings.Contains("永续,正式", companyProduct.Status) {
  713. //查询对应的销售
  714. //查找对应客户的销售信息
  715. adminInfo, tmpErr := admin.GetByAdminId(companyProduct.SellerID)
  716. if tmpErr != nil {
  717. errMsg = tmpErr.Error()
  718. err = errors.New("查询销售信息出错")
  719. return
  720. }
  721. contactInfo.Name = adminInfo.RealName
  722. contactInfo.Mobile = adminInfo.Mobile
  723. checkFlag = true
  724. }
  725. temp := new(response.PermissionFiccItem)
  726. temp.SelectIconUrl = utils.ALIYUN_YBIMG_HOST + "ficc_icon_idx_more_select.png"
  727. temp.IconUrl = utils.ALIYUN_YBIMG_HOST + "ficc_icon_idx_more.png"
  728. temp.ClassifyName = "更多报告"
  729. temp.Sort = 100000
  730. list = append(list, temp)
  731. ret.PermissionList = list
  732. ret.CheckFlag = checkFlag
  733. ret.ContactInfo = contactInfo
  734. return
  735. }
  736. // CheckUserSandboxPermission 验证用户/联系人的沙盘权限
  737. func CheckUserSandboxPermission(companyId int64, userId, permissionId int) (ok bool, companyPermissionIdList []int, permissionCheckInfo ChartPermissionCheckInfo, err error) {
  738. defer func() {
  739. // 如果无权限,那么就去查询是否申请过
  740. if ok == false && permissionCheckInfo.Type == CheckTypeApply {
  741. _, err = yb_apply_record.GetLastNotOpRecordByUserId(userId) // 图库申请
  742. // 查询是否有申请过,如果有申请过的话,那么err是nil
  743. if err != nil {
  744. if err == utils.ErrNoRow {
  745. err = nil
  746. return
  747. }
  748. return
  749. }
  750. permissionCheckInfo.CustomerInfo.HasApply = true
  751. }
  752. }()
  753. var productId int64
  754. productId = 1
  755. if companyId > 1 {
  756. // 查询是否开通FICC
  757. companyProductInfo, tmpErr := company_product.GetByCompany2ProductId(companyId, productId)
  758. if tmpErr != nil {
  759. // 没有开通FICC
  760. if tmpErr == utils.ErrNoRow {
  761. permissionCheckInfo.Type = CheckTypeApply
  762. return
  763. }
  764. err = tmpErr
  765. return
  766. }
  767. wxUser, tmpErr := wx_user.GetByUserId(userId)
  768. if tmpErr != nil {
  769. permissionCheckInfo.Type = CheckTypeApply
  770. err = tmpErr
  771. return
  772. }
  773. // 查询用户是否为弘则研究已禁用的联系人
  774. if companyId == 16 {
  775. if wxUser.Enabled != 1 {
  776. permissionCheckInfo.Type = CheckTypeApply
  777. return
  778. }
  779. }
  780. // 客户信息
  781. companyInfo, tmpErr := company.GetByCompanyId(companyId)
  782. if tmpErr != nil {
  783. // 没有开通FICC
  784. if tmpErr == utils.ErrNoRow {
  785. permissionCheckInfo.Type = CheckTypeApply
  786. return
  787. }
  788. err = tmpErr
  789. return
  790. }
  791. customerInfo := CustomerInfo{
  792. CompanyName: companyInfo.CompanyName,
  793. Status: companyProductInfo.Status,
  794. Name: wxUser.RealName,
  795. IsSuspend: companyProductInfo.IsSuspend,
  796. Mobile: wxUser.Mobile,
  797. }
  798. permissionCheckInfo.CustomerInfo = customerInfo
  799. // 如果客户FICC产品的状态是流失-申请
  800. if companyProductInfo.Status == "流失" || companyProductInfo.Status == "关闭" {
  801. permissionCheckInfo.Type = CheckTypeApply
  802. return
  803. }
  804. // 查找对应客户的销售信息
  805. adminInfo, tmpErr := admin.GetByAdminId(companyProductInfo.SellerID)
  806. if tmpErr != nil {
  807. err = tmpErr
  808. return
  809. }
  810. // 客户状态为冻结/试用暂停-联系销售
  811. permissionCheckInfo.Name = adminInfo.RealName
  812. permissionCheckInfo.Mobile = adminInfo.Mobile
  813. if companyProductInfo.Status == "冻结" || (companyProductInfo.Status == "试用" && companyProductInfo.IsSuspend == 1) {
  814. permissionCheckInfo.Type = CheckTypeContact
  815. return
  816. }
  817. // 验证用户的沙盘权限
  818. //获取所有的权限分类列表
  819. companyPermissionList, tmpErr := GetValidPermissionByCompany2ProductId(companyId, productId)
  820. if tmpErr != nil {
  821. err = tmpErr
  822. return
  823. }
  824. //客户品种权限赋值
  825. for _, chartPermission := range companyPermissionList {
  826. companyPermissionIdList = append(companyPermissionIdList, chartPermission.ChartPermissionID)
  827. }
  828. if permissionId > 0 {
  829. for _, chartPermission := range companyPermissionList {
  830. if chartPermission.ChartPermissionID == permissionId {
  831. ok = true
  832. }
  833. }
  834. if ok == false {
  835. permissionCheckInfo.Jump = `sandbox_list` //如果是没有单个品种的权限,那么提示后并跳转到沙盘列表页吧
  836. }
  837. } else {
  838. // 不校验品种权限的话,那么就是认为有权限的
  839. if len(companyPermissionList) > 0 {
  840. ok = true
  841. }
  842. }
  843. // 都不是默认联系销售
  844. permissionCheckInfo.Type = CheckTypeContact
  845. } else {
  846. permissionCheckInfo.Type = CheckTypeApply
  847. }
  848. return
  849. }
  850. func GetBindFiccPermissions(user user.UserInfo) (ret response.BindPermissionFiccResp, err error) {
  851. var errMsg string
  852. defer func() {
  853. if err != nil {
  854. global.LOG.Critical(fmt.Sprintf("GetHomeFiccPermissions: userId=%d, err:%s, errMsg:%s", user.UserID, err.Error(), errMsg))
  855. }
  856. }()
  857. ret.PermissionList = make([]*response.BindPermissionFiccItem, 0)
  858. // 判断用户状态是否是正常和永续
  859. var productAuthOk bool
  860. companyProduct, err := company_product.GetByCompany2ProductId(user.CompanyID, 1)
  861. if err == utils.ErrNoRow {
  862. err = nil
  863. return
  864. }
  865. if err != nil {
  866. errMsg = err.Error()
  867. err = errors.New("查询用户购买产品出错")
  868. return
  869. }
  870. if companyProduct != nil {
  871. // 无FICC权限的客户不可见
  872. if companyProduct.CompanyProductID > 0 {
  873. // 已购或者试用用户可见
  874. if strings.Contains("永续,正式", companyProduct.Status) || (companyProduct.Status == "试用" && companyProduct.IsSuspend != 1) {
  875. productAuthOk = true
  876. }
  877. }
  878. }
  879. if !productAuthOk {
  880. return
  881. }
  882. validPermissionList, err := GetValidPermissionByCompany2ProductId(user.CompanyID, 1)
  883. if err != nil {
  884. return
  885. }
  886. var validPermissionIds []int
  887. for _, item := range validPermissionList {
  888. validPermissionIds = append(validPermissionIds, item.ChartPermissionID)
  889. }
  890. permissionList, err := chart_permission.GetFiccListExceptTacticByProductId()
  891. if err != nil {
  892. return
  893. }
  894. permissionMap := make(map[uint64]bool)
  895. permissionFirstMap := make(map[int]*response.BindPermissionFiccItem)
  896. //查询首页展示的图标
  897. permissionFirstList, err := chart_permission.GetFiccFirstListExceptTacticByProductId()
  898. if err != nil {
  899. return
  900. }
  901. for _, v := range permissionFirstList {
  902. permissionFirstMap[int(v.ChartPermissionID)] = &response.BindPermissionFiccItem{
  903. Id: int(v.ChartPermissionID),
  904. ClassifyName: v.ChartPermissionName,
  905. }
  906. }
  907. for _, v := range permissionList {
  908. permissionMap[v.ChartPermissionID] = false
  909. for _, myPerId := range validPermissionIds {
  910. if int(v.ChartPermissionID) == myPerId {
  911. permissionMap[v.ChartPermissionID] = true
  912. break
  913. }
  914. }
  915. }
  916. for _, v := range permissionList {
  917. temp := new(response.BindPermissionFiccSecondItem)
  918. temp.ChartPermissionID = int(v.ChartPermissionID)
  919. temp.ChartPermissionName = v.PermissionName
  920. if _, ok := permissionFirstMap[v.ParentId]; ok && permissionMap[v.ChartPermissionID] {
  921. permissionFirstMap[v.ParentId].List = append(permissionFirstMap[v.ParentId].List, temp)
  922. }
  923. }
  924. var list []*response.BindPermissionFiccItem
  925. for _, v := range permissionFirstList {
  926. item := permissionFirstMap[int(v.ChartPermissionID)]
  927. if len(item.List) > 0 {
  928. temp := new(response.BindPermissionFiccItem)
  929. temp.Id = item.Id
  930. temp.ClassifyName = item.ClassifyName
  931. temp.List = item.List
  932. list = append(list, temp)
  933. }
  934. }
  935. ret.PermissionList = list
  936. return
  937. }
  938. func GetCommoditiesBindFiccPermissions(user user.UserInfo) (ret response.BindPermissionFiccResp, err error) {
  939. var errMsg string
  940. defer func() {
  941. if err != nil {
  942. global.LOG.Critical(fmt.Sprintf("GetHomeFiccPermissions: userId=%d, err:%s, errMsg:%s", user.UserID, err.Error(), errMsg))
  943. }
  944. }()
  945. ret.PermissionList = make([]*response.BindPermissionFiccItem, 0)
  946. // 判断用户状态是否是正常和永续
  947. var productAuthOk bool
  948. companyProduct, err := company_product.GetByCompany2ProductId(user.CompanyID, 1)
  949. if err == utils.ErrNoRow {
  950. err = nil
  951. return
  952. }
  953. if err != nil {
  954. errMsg = err.Error()
  955. err = errors.New("查询用户购买产品出错")
  956. return
  957. }
  958. if companyProduct != nil {
  959. // 无FICC权限的客户不可见
  960. if companyProduct.CompanyProductID > 0 {
  961. // 已购或者试用用户可见
  962. if strings.Contains("永续,正式", companyProduct.Status) || (companyProduct.Status == "试用" && companyProduct.IsSuspend != 1) {
  963. productAuthOk = true
  964. }
  965. }
  966. }
  967. if !productAuthOk {
  968. return
  969. }
  970. validPermissionList, err := GetValidPermissionByCompany2ProductId(user.CompanyID, 1)
  971. if err != nil {
  972. return
  973. }
  974. var validPermissionIds []int
  975. for _, item := range validPermissionList {
  976. validPermissionIds = append(validPermissionIds, item.ChartPermissionID)
  977. }
  978. permissionList, err := chart_permission.GetFiccListExceptTacticByProductId()
  979. if err != nil {
  980. return
  981. }
  982. permissionMap := make(map[uint64]bool)
  983. permissionFirstMap := make(map[int]*response.BindPermissionFiccItem)
  984. //查询首页展示的图标
  985. permissionFirstList, err := chart_permission.GetFiccFirstListExceptTacticByProductId()
  986. if err != nil {
  987. return
  988. }
  989. for _, v := range permissionFirstList {
  990. permissionFirstMap[int(v.ChartPermissionID)] = &response.BindPermissionFiccItem{
  991. Id: int(v.ChartPermissionID),
  992. ClassifyName: v.ChartPermissionName,
  993. }
  994. }
  995. for _, v := range permissionList {
  996. permissionMap[v.ChartPermissionID] = false
  997. for _, myPerId := range validPermissionIds {
  998. if int(v.ChartPermissionID) == myPerId {
  999. permissionMap[v.ChartPermissionID] = true
  1000. break
  1001. }
  1002. }
  1003. }
  1004. for _, v := range permissionList {
  1005. temp := new(response.BindPermissionFiccSecondItem)
  1006. temp.ChartPermissionID = int(v.ChartPermissionID)
  1007. temp.ChartPermissionName = v.PermissionName
  1008. if _, ok := permissionFirstMap[v.ParentId]; ok && permissionMap[v.ChartPermissionID] {
  1009. permissionFirstMap[v.ParentId].List = append(permissionFirstMap[v.ParentId].List, temp)
  1010. }
  1011. }
  1012. var list []*response.BindPermissionFiccItem
  1013. LOOP:
  1014. for _, v := range permissionFirstList {
  1015. item := permissionFirstMap[int(v.ChartPermissionID)]
  1016. if len(item.List) > 0 {
  1017. temp := new(response.BindPermissionFiccItem)
  1018. temp.Id = item.Id
  1019. temp.ClassifyName = item.ClassifyName
  1020. temp.List = item.List
  1021. //大宗商品分类内合并PTA&MEG
  1022. if temp.ClassifyName == "化工产业" {
  1023. count := 0
  1024. for i, secondItem := range temp.List {
  1025. if secondItem.ChartPermissionName == "PTA" || secondItem.ChartPermissionName == "MEG" {
  1026. count++
  1027. secondItem.ChartPermissionName = "PTA/MEG"
  1028. if count > 1 {
  1029. temp.List = append(temp.List[:i], temp.List[i+1:]...)
  1030. }
  1031. }
  1032. }
  1033. }
  1034. if temp.ClassifyName == "宏观经济" {
  1035. continue LOOP
  1036. }
  1037. list = append(list, temp)
  1038. }
  1039. }
  1040. ret.PermissionList = list
  1041. return
  1042. }