permission.go 33 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973974975976977978979980981982983984985986987988989990991992993994995996997998999100010011002100310041005100610071008100910101011101210131014101510161017101810191020102110221023102410251026102710281029103010311032103310341035103610371038103910401041104210431044104510461047104810491050105110521053105410551056105710581059106010611062106310641065106610671068106910701071107210731074107510761077107810791080108110821083108410851086108710881089109010911092109310941095109610971098109911001101110211031104110511061107110811091110111111121113111411151116111711181119112011211122112311241125112611271128
  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/company"
  10. "hongze/hongze_yb/models/tables/company_product"
  11. "hongze/hongze_yb/models/tables/company_report_permission"
  12. "hongze/hongze_yb/models/tables/company_user_chart_classify_permission"
  13. chart_permission2 "hongze/hongze_yb/models/tables/rddp/chart_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_permission2.ChartPermission, err error) {
  29. list, err = chart_permission2.GetClassNameListByProductId(productId)
  30. return
  31. }
  32. // GetPermissionListByProductId 根据product_id获取所有的权限列表
  33. func GetPermissionListByProductId(productId int64) (list []*chart_permission2.ChartPermission, err error) {
  34. list, err = chart_permission2.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. // 潜在客户名下的
  312. if companyId <= 1 {
  313. permissionCheckInfo.Type = CheckTypeApply
  314. return
  315. }
  316. var productId int64
  317. productId = 1
  318. //查询是否 开通ficc的客户
  319. companyProductInfo, tmpErr := company_product.GetByCompany2ProductId(companyId, productId)
  320. if tmpErr != nil {
  321. // 没有开通ficc的客户
  322. if tmpErr == utils.ErrNoRow {
  323. permissionCheckInfo.Type = CheckTypeApply
  324. return
  325. }
  326. err = tmpErr
  327. return
  328. }
  329. wxUser, tmpErr := wx_user.GetByUserId(userId)
  330. if tmpErr != nil {
  331. permissionCheckInfo.Type = CheckTypeApply
  332. err = tmpErr
  333. return
  334. }
  335. //客户信息
  336. companyInfo, tmpErr := companyCache.GetByCompanyId(companyId)
  337. if tmpErr != nil {
  338. // 没有开通ficc的客户
  339. if tmpErr == utils.ErrNoRow {
  340. permissionCheckInfo.Type = CheckTypeApply
  341. return
  342. }
  343. err = tmpErr
  344. return
  345. }
  346. customerInfo := response.CustomerInfo{
  347. CompanyName: companyInfo.CompanyName,
  348. Status: companyProductInfo.Status,
  349. Name: wxUser.RealName,
  350. IsSuspend: companyProductInfo.IsSuspend,
  351. Mobile: wxUser.Mobile,
  352. }
  353. permissionCheckInfo.CustomerInfo = customerInfo
  354. // 如果客户ficc产品的状态是流失,那么也是让去申请
  355. if companyProductInfo.Status == "流失" || companyProductInfo.Status == "关闭" {
  356. permissionCheckInfo.Type = CheckTypeApply
  357. return
  358. }
  359. //查找对应客户的销售信息
  360. adminInfo, tmpErr := admin.GetByAdminId(companyProductInfo.SellerID)
  361. if tmpErr != nil {
  362. //if tmpErr ==
  363. err = tmpErr
  364. return
  365. }
  366. permissionCheckInfo.Name = adminInfo.RealName
  367. permissionCheckInfo.Mobile = adminInfo.Mobile
  368. if companyProductInfo.Status == "冻结" {
  369. permissionCheckInfo.Type = CheckTypeContact
  370. return
  371. }
  372. //客户状态是:试用暂停状态(联系销售)
  373. if companyProductInfo.Status == "试用" && companyProductInfo.IsSuspend == 1 {
  374. permissionCheckInfo.Type = CheckTypeContact
  375. return
  376. }
  377. // 获取有效的权限id列表
  378. validPermissionIdList, tmpErr := GetValidPermissionIdListByCompany2ProductId(companyId, productId)
  379. if tmpErr != nil {
  380. err = tmpErr
  381. return
  382. }
  383. // 校验在有效的权限id列表中是否存在该权限
  384. if len(permissionIdList) > 0 {
  385. for _, validPermissionId := range validPermissionIdList {
  386. //如果有该权限id,那么直接返回校验通过
  387. if _, validOk := permissionMap[validPermissionId]; validOk {
  388. finalValidPermissionIds = append(finalValidPermissionIds, validPermissionId)
  389. ok = true
  390. }
  391. }
  392. return
  393. }
  394. permissionCheckInfo.Type = CheckTypeContact
  395. return
  396. }
  397. // checkPermissionByPermissionIdList2Rai 根据权限id集合权限校验(权益)
  398. func checkPermissionByPermissionIdList2Rai(companyId int64, userId int, permissionIdList []int) (ok bool, err error) {
  399. //非潜在客户
  400. if len(permissionIdList) <= 0 {
  401. err = fmt.Errorf("权限异常,请传入需要校验的权限")
  402. return
  403. }
  404. // 查询用户是否为弘则研究已禁用的联系人
  405. wxUser, tmpErr := wx_user.GetByUserId(userId)
  406. if tmpErr != nil {
  407. err = fmt.Errorf("用户信息异常")
  408. return
  409. }
  410. if companyId == 16 {
  411. if wxUser.Enabled != 1 {
  412. return
  413. }
  414. }
  415. //市场策略(ficc权限id):33
  416. ficcPermissionId := 33
  417. //市场策略(权益权限id):23
  418. raiPermissionId := 23
  419. //是否往下校验
  420. var permissionOk bool
  421. for _, permissionId := range permissionIdList {
  422. if permissionId == ficcPermissionId {
  423. permissionOk = true
  424. }
  425. }
  426. var productId int64
  427. productId = 2
  428. if companyId > 1 && permissionOk {
  429. // 获取有效的权限id列表
  430. validPermissionIdList, tmpErr := GetValidPermissionIdListByCompany2ProductId(companyId, productId)
  431. if tmpErr != nil {
  432. err = tmpErr
  433. return
  434. }
  435. // 校验在有效的权限id列表中是否存在该权限
  436. for _, validPermissionId := range validPermissionIdList {
  437. if validPermissionId == raiPermissionId {
  438. ok = true
  439. return
  440. }
  441. }
  442. }
  443. return
  444. }
  445. // ChartPermissionCheckInfo 图表权限验证信息
  446. type ChartPermissionCheckInfo struct {
  447. Name string `json:"name" description:"销售名称"`
  448. Mobile string `json:"mobile" description:"手机号"`
  449. Type string `json:"type" description:"无权限,需要前端处理的类型,枚举值:expired, apply, contact"`
  450. CustomerInfo CustomerInfo `json:"customer_info" description:"客户信息"`
  451. Jump string `json:"jump" description:"需要跳转的页面,sandbox_list"`
  452. }
  453. // CheckUserChartPermission 验证用户/联系人的图库权限
  454. func CheckUserChartPermission(companyId int64, userId int) (ok bool, permissionCheckInfo ChartPermissionCheckInfo, err error) {
  455. defer func() {
  456. // 如果无权限,那么就去查询是否申请过
  457. if ok == false && permissionCheckInfo.Type == CheckTypeApply {
  458. _, err = yb_apply_record.GetLastNotOpRecordByUserId(userId) // 图库申请
  459. // 查询是否有申请过,如果有申请过的话,那么err是nil
  460. if err != nil {
  461. if err == utils.ErrNoRow {
  462. err = nil
  463. return
  464. }
  465. return
  466. }
  467. permissionCheckInfo.CustomerInfo.HasApply = true
  468. }
  469. }()
  470. var productId int64
  471. productId = 1
  472. if companyId > 1 {
  473. // 查询是否开通FICC
  474. companyProductInfo, tmpErr := company_product.GetByCompany2ProductId(companyId, productId)
  475. if tmpErr != nil {
  476. // 没有开通FICC
  477. if tmpErr == utils.ErrNoRow {
  478. permissionCheckInfo.Type = CheckTypeApply
  479. return
  480. }
  481. err = tmpErr
  482. return
  483. }
  484. wxUser, tmpErr := wx_user.GetByUserId(userId)
  485. if tmpErr != nil {
  486. permissionCheckInfo.Type = CheckTypeApply
  487. err = tmpErr
  488. return
  489. }
  490. // 查询用户是否为弘则研究已禁用的联系人
  491. if companyId == 16 {
  492. if wxUser.Enabled != 1 {
  493. permissionCheckInfo.Type = CheckTypeApply
  494. return
  495. }
  496. }
  497. // 客户信息
  498. companyInfo, tmpErr := company.GetByCompanyId(companyId)
  499. if tmpErr != nil {
  500. // 没有开通FICC
  501. if tmpErr == utils.ErrNoRow {
  502. permissionCheckInfo.Type = CheckTypeApply
  503. return
  504. }
  505. err = tmpErr
  506. return
  507. }
  508. customerInfo := CustomerInfo{
  509. CompanyName: companyInfo.CompanyName,
  510. Status: companyProductInfo.Status,
  511. Name: wxUser.RealName,
  512. IsSuspend: companyProductInfo.IsSuspend,
  513. Mobile: wxUser.Mobile,
  514. }
  515. permissionCheckInfo.CustomerInfo = customerInfo
  516. // 如果客户FICC产品的状态是流失-申请
  517. if companyProductInfo.Status == "流失" || companyProductInfo.Status == "关闭" {
  518. permissionCheckInfo.Type = CheckTypeApply
  519. return
  520. }
  521. // 查找对应客户的销售信息
  522. adminInfo, tmpErr := admin.GetByAdminId(companyProductInfo.SellerID)
  523. if tmpErr != nil {
  524. err = tmpErr
  525. return
  526. }
  527. // 客户状态为冻结/试用暂停-联系销售
  528. permissionCheckInfo.Name = adminInfo.RealName
  529. permissionCheckInfo.Mobile = adminInfo.Mobile
  530. if companyProductInfo.Status == "冻结" || (companyProductInfo.Status == "试用" && companyProductInfo.IsSuspend == 1) {
  531. permissionCheckInfo.Type = CheckTypeContact
  532. return
  533. }
  534. // 验证用户的图表权限
  535. permissionInfo, tmpErr := company_user_chart_classify_permission.GetPermissionByUserId(userId)
  536. if tmpErr != nil {
  537. if tmpErr == utils.ErrNoRow {
  538. // 无权限-联系销售
  539. permissionCheckInfo.Type = CheckTypeContact
  540. return
  541. }
  542. err = tmpErr
  543. return
  544. }
  545. nowTime := time.Now()
  546. if permissionInfo.Enabled != 1 {
  547. // 权限被禁用-联系销售
  548. permissionCheckInfo.Type = CheckTypeContact
  549. return
  550. }
  551. endTime := permissionInfo.EndTime.Add(23*time.Hour + 59*time.Minute + 59*time.Second)
  552. if nowTime.After(permissionInfo.StartTime) && nowTime.Before(endTime) {
  553. ok = true
  554. } else {
  555. // 权限已到期
  556. permissionCheckInfo.Type = CheckTypeExpire
  557. return
  558. }
  559. // 都不是默认联系销售
  560. permissionCheckInfo.Type = CheckTypeContact
  561. } else {
  562. permissionCheckInfo.Type = CheckTypeApply
  563. }
  564. return
  565. }
  566. type FiccPermissionList struct {
  567. ClassifyName string `description:"分类"`
  568. //HasPermission bool `description:"是否有权限"`
  569. Items []*PermissionItem `description:"子类"`
  570. }
  571. type PermissionItem struct {
  572. PermissionId int `description:"权限id"`
  573. PermissionName string `description:"权限名称"`
  574. //HasPermission bool `description:"是否有权限"`
  575. }
  576. // GetFiccPermissionList 获取FICC品种权限列表-不校验用户权限
  577. func GetFiccPermissionList() (list []*FiccPermissionList, err error) {
  578. productId := 1
  579. list = make([]*FiccPermissionList, 0)
  580. allFiccPermissions, e := chart_permission2.GetListByProductIdAndPermissionType(productId)
  581. if e != nil {
  582. err = errors.New("获取品种权限失败 Err:" + e.Error())
  583. return
  584. }
  585. permissionMap := make(map[int][]*chart_permission2.ChartPermission, 0)
  586. for _, v := range allFiccPermissions {
  587. if v.ParentId > 0 {
  588. permissionMap[v.ParentId] = append(permissionMap[v.ParentId], v)
  589. }
  590. }
  591. for _, v := range allFiccPermissions {
  592. if v.ParentId == 0 {
  593. classify := new(FiccPermissionList)
  594. classify.ClassifyName = v.PermissionName
  595. classify.Items = make([]*PermissionItem, 0)
  596. items, ok := permissionMap[int(v.ChartPermissionID)]
  597. if ok {
  598. for _, iv := range items {
  599. item := new(PermissionItem)
  600. item.PermissionId = int(iv.ChartPermissionID)
  601. item.PermissionName = iv.PermissionName
  602. classify.Items = append(classify.Items, item)
  603. }
  604. }
  605. list = append(list, classify)
  606. }
  607. }
  608. return
  609. }
  610. func GetHomeFiccPermissions(user user.UserInfo) (ret response.PermissionFiccResp, err error) {
  611. var errMsg string
  612. defer func() {
  613. if err != nil {
  614. global.LOG.Critical(fmt.Sprintf("GetHomeFiccPermissions: userId=%d, err:%s, errMsg:%s", user.UserID, err.Error(), errMsg))
  615. }
  616. }()
  617. validPermissionList, err := GetValidPermissionByCompany2ProductId(user.CompanyID, 1)
  618. if err != nil {
  619. return
  620. }
  621. var validPermissionIds []int
  622. for _, item := range validPermissionList {
  623. validPermissionIds = append(validPermissionIds, item.ChartPermissionID)
  624. }
  625. permissionList, err := chart_permission2.GetFiccListExceptTacticByProductId()
  626. if err != nil {
  627. return
  628. }
  629. permissionMap := make(map[uint64]bool)
  630. permissionFirstMap := make(map[int]*response.PermissionFirstItem)
  631. //查询首页展示的图标
  632. permissionFirstList, err := chart_permission2.GetFiccFirstListExceptTacticByProductId()
  633. if err != nil {
  634. return
  635. }
  636. for _, v := range permissionFirstList {
  637. permissionFirstMap[int(v.ChartPermissionID)] = &response.PermissionFirstItem{
  638. Id: int(v.ChartPermissionID),
  639. Sort: v.Sort + 1000,
  640. YbIndexName: v.PermissionName,
  641. YbIndexIcon: "",
  642. }
  643. }
  644. for k, v := range permissionList {
  645. if v.ParentId > 0 {
  646. permissionMap[v.ChartPermissionID] = false
  647. for _, myPerId := range validPermissionIds {
  648. if int(v.ChartPermissionID) == myPerId {
  649. permissionMap[v.ChartPermissionID] = true
  650. permissionList[k].Sort = v.Sort - 1000
  651. if _, ok := permissionFirstMap[v.ParentId]; ok && !permissionFirstMap[v.ParentId].IsCheck {
  652. permissionFirstMap[v.ParentId].AuthOk = true
  653. permissionFirstMap[v.ParentId].Sort -= 1000
  654. permissionFirstMap[v.ParentId].IsCheck = true
  655. }
  656. break
  657. }
  658. }
  659. }
  660. }
  661. for _, v := range permissionList {
  662. if v.ParentId > 0 {
  663. temp := new(response.PermissionFiccSecondItem)
  664. temp.Sort = v.Sort
  665. temp.ChartPermissionID = int(v.ChartPermissionID)
  666. temp.ChartPermissionName = v.PermissionName
  667. temp.AuthOk = permissionMap[v.ChartPermissionID]
  668. temp.PirceDrivenState = v.PriceDrivenState
  669. if _, ok := permissionFirstMap[v.ParentId]; ok {
  670. permissionFirstMap[v.ParentId].List = append(permissionFirstMap[v.ParentId].List, temp)
  671. } else {
  672. permissionFirstMap[v.ParentId] = &response.PermissionFirstItem{
  673. List: []*response.PermissionFiccSecondItem{temp},
  674. }
  675. }
  676. }
  677. }
  678. var list response.PermissionFiccList
  679. for _, v := range permissionFirstMap {
  680. temp := new(response.PermissionFiccItem)
  681. temp.Id = v.Id
  682. temp.Sort = v.Sort
  683. temp.ClassifyName = v.YbIndexName
  684. temp.IconUrl = v.YbIndexIcon
  685. if len(temp.IconUrl) > 4 {
  686. tmpIcon := temp.IconUrl[:len(temp.IconUrl)-4]
  687. temp.SelectIconUrl = tmpIcon + "_select.png"
  688. }
  689. if len(v.List) > 0 {
  690. temp.List = v.List
  691. sort.Sort(temp.List)
  692. list = append(list, temp)
  693. }
  694. }
  695. if len(list) > 0 {
  696. sort.Sort(list)
  697. }
  698. //判断是否是已购用户,如果是已购用户,需要返回对应的联系人方式
  699. companyProduct, err := company_product.GetByCompany2ProductId(user.CompanyID, 1)
  700. if err != nil {
  701. if err == utils.ErrNoRow {
  702. err = nil
  703. }
  704. }
  705. if err != nil {
  706. errMsg = err.Error()
  707. err = errors.New("查询用户状态失败")
  708. return
  709. }
  710. // 已购仅付费用户可见
  711. var contactInfo response.PermissionCheckContactInfo
  712. checkFlag := false
  713. if companyProduct.Status != "" && strings.Contains("永续,正式", companyProduct.Status) {
  714. //查询对应的销售
  715. //查找对应客户的销售信息
  716. adminInfo, tmpErr := admin.GetByAdminId(companyProduct.SellerID)
  717. if tmpErr != nil {
  718. errMsg = tmpErr.Error()
  719. err = errors.New("查询销售信息出错")
  720. return
  721. }
  722. contactInfo.Name = adminInfo.RealName
  723. contactInfo.Mobile = adminInfo.Mobile
  724. checkFlag = true
  725. }
  726. temp := new(response.PermissionFiccItem)
  727. temp.SelectIconUrl = utils.ALIYUN_YBIMG_HOST + "ficc_icon_idx_more_select.png"
  728. temp.IconUrl = utils.ALIYUN_YBIMG_HOST + "ficc_icon_idx_more.png"
  729. temp.ClassifyName = "更多报告"
  730. temp.Sort = 100000
  731. list = append(list, temp)
  732. ret.PermissionList = list
  733. ret.CheckFlag = checkFlag
  734. ret.ContactInfo = contactInfo
  735. return
  736. }
  737. // CheckUserSandboxPermission 验证用户/联系人的沙盘权限
  738. func CheckUserSandboxPermission(companyId int64, userId, permissionId int) (ok bool, companyPermissionIdList []int, permissionCheckInfo ChartPermissionCheckInfo, err error) {
  739. defer func() {
  740. // 如果无权限,那么就去查询是否申请过
  741. if ok == false && permissionCheckInfo.Type == CheckTypeApply {
  742. _, err = yb_apply_record.GetLastNotOpRecordByUserId(userId) // 图库申请
  743. // 查询是否有申请过,如果有申请过的话,那么err是nil
  744. if err != nil {
  745. if err == utils.ErrNoRow {
  746. err = nil
  747. return
  748. }
  749. return
  750. }
  751. permissionCheckInfo.CustomerInfo.HasApply = true
  752. }
  753. }()
  754. var productId int64
  755. productId = 1
  756. if companyId > 1 {
  757. // 查询是否开通FICC
  758. companyProductInfo, tmpErr := company_product.GetByCompany2ProductId(companyId, productId)
  759. if tmpErr != nil {
  760. // 没有开通FICC
  761. if tmpErr == utils.ErrNoRow {
  762. permissionCheckInfo.Type = CheckTypeApply
  763. return
  764. }
  765. err = tmpErr
  766. return
  767. }
  768. wxUser, tmpErr := wx_user.GetByUserId(userId)
  769. if tmpErr != nil {
  770. permissionCheckInfo.Type = CheckTypeApply
  771. err = tmpErr
  772. return
  773. }
  774. // 查询用户是否为弘则研究已禁用的联系人
  775. if companyId == 16 {
  776. if wxUser.Enabled != 1 {
  777. permissionCheckInfo.Type = CheckTypeApply
  778. return
  779. }
  780. }
  781. // 客户信息
  782. companyInfo, tmpErr := company.GetByCompanyId(companyId)
  783. if tmpErr != nil {
  784. // 没有开通FICC
  785. if tmpErr == utils.ErrNoRow {
  786. permissionCheckInfo.Type = CheckTypeApply
  787. return
  788. }
  789. err = tmpErr
  790. return
  791. }
  792. customerInfo := CustomerInfo{
  793. CompanyName: companyInfo.CompanyName,
  794. Status: companyProductInfo.Status,
  795. Name: wxUser.RealName,
  796. IsSuspend: companyProductInfo.IsSuspend,
  797. Mobile: wxUser.Mobile,
  798. }
  799. permissionCheckInfo.CustomerInfo = customerInfo
  800. // 如果客户FICC产品的状态是流失-申请
  801. if companyProductInfo.Status == "流失" || companyProductInfo.Status == "关闭" {
  802. permissionCheckInfo.Type = CheckTypeApply
  803. return
  804. }
  805. // 查找对应客户的销售信息
  806. adminInfo, tmpErr := admin.GetByAdminId(companyProductInfo.SellerID)
  807. if tmpErr != nil {
  808. err = tmpErr
  809. return
  810. }
  811. // 客户状态为冻结/试用暂停-联系销售
  812. permissionCheckInfo.Name = adminInfo.RealName
  813. permissionCheckInfo.Mobile = adminInfo.Mobile
  814. if companyProductInfo.Status == "冻结" || (companyProductInfo.Status == "试用" && companyProductInfo.IsSuspend == 1) {
  815. permissionCheckInfo.Type = CheckTypeContact
  816. return
  817. }
  818. // 验证用户的沙盘权限
  819. //获取所有的权限分类列表
  820. companyPermissionList, tmpErr := GetValidPermissionByCompany2ProductId(companyId, productId)
  821. if tmpErr != nil {
  822. err = tmpErr
  823. return
  824. }
  825. //客户品种权限赋值
  826. for _, chartPermission := range companyPermissionList {
  827. companyPermissionIdList = append(companyPermissionIdList, chartPermission.ChartPermissionID)
  828. }
  829. if permissionId > 0 {
  830. for _, chartPermission := range companyPermissionList {
  831. if chartPermission.ChartPermissionID == permissionId {
  832. ok = true
  833. }
  834. }
  835. if ok == false {
  836. permissionCheckInfo.Jump = `sandbox_list` //如果是没有单个品种的权限,那么提示后并跳转到沙盘列表页吧
  837. }
  838. } else {
  839. // 不校验品种权限的话,那么就是认为有权限的
  840. if len(companyPermissionList) > 0 {
  841. ok = true
  842. }
  843. }
  844. // 都不是默认联系销售
  845. permissionCheckInfo.Type = CheckTypeContact
  846. } else {
  847. permissionCheckInfo.Type = CheckTypeApply
  848. }
  849. return
  850. }
  851. func GetBindFiccPermissions(user user.UserInfo) (ret response.BindPermissionFiccResp, err error) {
  852. var errMsg string
  853. defer func() {
  854. if err != nil {
  855. global.LOG.Critical(fmt.Sprintf("GetHomeFiccPermissions: userId=%d, err:%s, errMsg:%s", user.UserID, err.Error(), errMsg))
  856. }
  857. }()
  858. ret.PermissionList = make([]*response.BindPermissionFiccItem, 0)
  859. // 判断用户状态是否是正常和永续
  860. var productAuthOk bool
  861. companyProduct, err := company_product.GetByCompany2ProductId(user.CompanyID, 1)
  862. if err == utils.ErrNoRow {
  863. err = nil
  864. return
  865. }
  866. if err != nil {
  867. errMsg = err.Error()
  868. err = errors.New("查询用户购买产品出错")
  869. return
  870. }
  871. if companyProduct != nil {
  872. // 无FICC权限的客户不可见
  873. if companyProduct.CompanyProductID > 0 {
  874. // 已购或者试用用户可见
  875. if strings.Contains("永续,正式", companyProduct.Status) || (companyProduct.Status == "试用" && companyProduct.IsSuspend != 1) {
  876. productAuthOk = true
  877. }
  878. }
  879. }
  880. if !productAuthOk {
  881. return
  882. }
  883. validPermissionList, err := GetValidPermissionByCompany2ProductId(user.CompanyID, 1)
  884. if err != nil {
  885. return
  886. }
  887. var validPermissionIds []int
  888. for _, item := range validPermissionList {
  889. validPermissionIds = append(validPermissionIds, item.ChartPermissionID)
  890. }
  891. permissionList, err := chart_permission2.GetFiccListExceptTacticByProductId()
  892. if err != nil {
  893. return
  894. }
  895. permissionMap := make(map[uint64]bool)
  896. permissionFirstMap := make(map[int]*response.BindPermissionFiccItem)
  897. //查询首页展示的图标
  898. permissionFirstList, err := chart_permission2.GetFiccFirstListExceptTacticByProductId()
  899. if err != nil {
  900. return
  901. }
  902. for _, v := range permissionFirstList {
  903. permissionFirstMap[int(v.ChartPermissionID)] = &response.BindPermissionFiccItem{
  904. Id: int(v.ChartPermissionID),
  905. ClassifyName: v.ChartPermissionName,
  906. }
  907. }
  908. for _, v := range permissionList {
  909. permissionMap[v.ChartPermissionID] = false
  910. for _, myPerId := range validPermissionIds {
  911. if int(v.ChartPermissionID) == myPerId {
  912. permissionMap[v.ChartPermissionID] = true
  913. break
  914. }
  915. }
  916. }
  917. for _, v := range permissionList {
  918. temp := new(response.BindPermissionFiccSecondItem)
  919. temp.ChartPermissionID = int(v.ChartPermissionID)
  920. temp.ChartPermissionName = v.PermissionName
  921. if _, ok := permissionFirstMap[v.ParentId]; ok && permissionMap[v.ChartPermissionID] {
  922. permissionFirstMap[v.ParentId].List = append(permissionFirstMap[v.ParentId].List, temp)
  923. }
  924. }
  925. var list []*response.BindPermissionFiccItem
  926. for _, v := range permissionFirstList {
  927. item := permissionFirstMap[int(v.ChartPermissionID)]
  928. if len(item.List) > 0 {
  929. temp := new(response.BindPermissionFiccItem)
  930. temp.Id = item.Id
  931. temp.ClassifyName = item.ClassifyName
  932. temp.List = item.List
  933. list = append(list, temp)
  934. }
  935. }
  936. ret.PermissionList = list
  937. return
  938. }
  939. func GetCommoditiesBindFiccPermissions(user user.UserInfo) (ret response.BindPermissionFiccResp, err error) {
  940. var errMsg string
  941. defer func() {
  942. if err != nil {
  943. global.LOG.Critical(fmt.Sprintf("GetHomeFiccPermissions: userId=%d, err:%s, errMsg:%s", user.UserID, err.Error(), errMsg))
  944. }
  945. }()
  946. ret.PermissionList = make([]*response.BindPermissionFiccItem, 0)
  947. // 判断用户状态是否是正常和永续
  948. var productAuthOk bool
  949. companyProduct, err := company_product.GetByCompany2ProductId(user.CompanyID, 1)
  950. if err == utils.ErrNoRow {
  951. err = nil
  952. return
  953. }
  954. if err != nil {
  955. errMsg = err.Error()
  956. err = errors.New("查询用户购买产品出错")
  957. return
  958. }
  959. if companyProduct != nil {
  960. // 无FICC权限的客户不可见
  961. if companyProduct.CompanyProductID > 0 {
  962. // 已购或者试用用户可见
  963. if strings.Contains("永续,正式", companyProduct.Status) || (companyProduct.Status == "试用" && companyProduct.IsSuspend != 1) {
  964. productAuthOk = true
  965. }
  966. }
  967. }
  968. if !productAuthOk {
  969. return
  970. }
  971. validPermissionList, err := GetValidPermissionByCompany2ProductId(user.CompanyID, 1)
  972. if err != nil {
  973. return
  974. }
  975. var validPermissionIds []int
  976. for _, item := range validPermissionList {
  977. validPermissionIds = append(validPermissionIds, item.ChartPermissionID)
  978. }
  979. permissionList, err := chart_permission2.GetFiccListExceptTacticByProductId()
  980. if err != nil {
  981. return
  982. }
  983. permissionMap := make(map[uint64]bool)
  984. permissionFirstMap := make(map[int]*response.BindPermissionFiccItem)
  985. //查询首页展示的图标
  986. permissionFirstList, err := chart_permission2.GetFiccFirstListExceptTacticByProductId()
  987. if err != nil {
  988. return
  989. }
  990. for _, v := range permissionFirstList {
  991. permissionFirstMap[int(v.ChartPermissionID)] = &response.BindPermissionFiccItem{
  992. Id: int(v.ChartPermissionID),
  993. ClassifyName: v.ChartPermissionName,
  994. }
  995. }
  996. for _, v := range permissionList {
  997. permissionMap[v.ChartPermissionID] = false
  998. for _, myPerId := range validPermissionIds {
  999. if int(v.ChartPermissionID) == myPerId {
  1000. permissionMap[v.ChartPermissionID] = true
  1001. break
  1002. }
  1003. }
  1004. }
  1005. for _, v := range permissionList {
  1006. temp := new(response.BindPermissionFiccSecondItem)
  1007. temp.ChartPermissionID = int(v.ChartPermissionID)
  1008. temp.ChartPermissionName = v.PermissionName
  1009. if _, ok := permissionFirstMap[v.ParentId]; ok && permissionMap[v.ChartPermissionID] {
  1010. permissionFirstMap[v.ParentId].List = append(permissionFirstMap[v.ParentId].List, temp)
  1011. }
  1012. }
  1013. var list []*response.BindPermissionFiccItem
  1014. LOOP:
  1015. for _, v := range permissionFirstList {
  1016. item := permissionFirstMap[int(v.ChartPermissionID)]
  1017. if len(item.List) > 0 {
  1018. temp := new(response.BindPermissionFiccItem)
  1019. temp.Id = item.Id
  1020. temp.ClassifyName = item.ClassifyName
  1021. temp.List = item.List
  1022. //大宗商品分类内合并PTA&MEG
  1023. if temp.ClassifyName == "化工产业" {
  1024. count := 0
  1025. for i, secondItem := range temp.List {
  1026. if secondItem.ChartPermissionName == "PTA" || secondItem.ChartPermissionName == "MEG" {
  1027. count++
  1028. secondItem.ChartPermissionName = "PTA/MEG"
  1029. if count > 1 {
  1030. temp.List = append(temp.List[:i], temp.List[i+1:]...)
  1031. }
  1032. }
  1033. }
  1034. }
  1035. if temp.ClassifyName == "宏观经济" {
  1036. continue LOOP
  1037. }
  1038. list = append(list, temp)
  1039. }
  1040. }
  1041. ret.PermissionList = list
  1042. return
  1043. }