company_permission.go 13 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421
  1. package services
  2. import (
  3. "errors"
  4. "fmt"
  5. "hongze/hz_crm_api/models"
  6. "hongze/hz_crm_api/models/company"
  7. "hongze/hz_crm_api/utils"
  8. "strconv"
  9. "strings"
  10. "time"
  11. )
  12. // RaiCompanyPermissionAndCheckList
  13. // 权益客户-根据客户状态及所选产品线合并/拆分展示权限列表
  14. // 场景:查看客户权限、客户详情、编辑客户、增开试用
  15. // 非权益客户时, 返回权益权限列表, 但是checkList为空[]int
  16. // unify: 是否需要统一合并/拆分
  17. // filterPermissionName: 需要隐藏的权限名称
  18. func RaiCompanyPermissionAndCheckList(companyId int, unify bool, filterPermissionName []string) (resp *company.PermissionLookList, err error) {
  19. resp = new(company.PermissionLookList)
  20. productId := utils.COMPANY_PRODUCT_RAI_ID
  21. productName := utils.COMPANY_PRODUCT_RAI_NAME
  22. checkList := make([]int, 0) // 客户拥有的权限, 用于前端匹配权限列表回显
  23. // 获取权益权限列表
  24. permissionItems, e := company.GetPermissionLookItems(productId, productName)
  25. if e != nil {
  26. err = errors.New("获取权益权限列表失败, Err: " + e.Error())
  27. return
  28. }
  29. if len(permissionItems) == 0 {
  30. err = errors.New("权益权限信息有误")
  31. return
  32. }
  33. // 获取客户权益报告权限
  34. reportPermissions, e := company.GetCompanyReportPermission(companyId, productId)
  35. if e != nil {
  36. err = errors.New("获取客户权益报告权限列表失败, Err: " + e.Error())
  37. return
  38. }
  39. reportPermissionMap := make(map[int]*company.CompanyReportPermission, 0)
  40. for i := range reportPermissions {
  41. reportPermissionMap[reportPermissions[i].ChartPermissionId] = reportPermissions[i]
  42. }
  43. // 遍历权益权限, 赋予报告权限时长、状态等信息
  44. subjectMap := make(map[string]*company.PermissionLookItem, 0)
  45. objectMap := make(map[string]*company.PermissionLookItem, 0)
  46. for i := range permissionItems {
  47. v := permissionItems[i]
  48. p := reportPermissionMap[v.ChartPermissionId]
  49. if p != nil {
  50. checkList = append(checkList, v.ChartPermissionId)
  51. v.StartDate = p.StartDate
  52. v.EndDate = p.EndDate
  53. v.Status = p.Status
  54. v.IsUpgrade = p.IsUpgrade
  55. // 计算到期天数
  56. endDateTime, _ := time.Parse(utils.FormatDate, v.EndDate)
  57. endDateTime = endDateTime.AddDate(0, 0, 1)
  58. sub := endDateTime.Sub(time.Now())
  59. if sub < 0 {
  60. sub = 0
  61. }
  62. expireDay := fmt.Sprintf("%v", int(sub.Hours()/24))
  63. v.ExpireDay = expireDay
  64. // 正式/永续-买方研选划分为3w和5w两种
  65. //if v.PermissionName == utils.CHART_PERMISSION_NAME_MF_YANXUAN && strings.Contains("正式,永续", v.Status) {
  66. // if p.ExpensiveYx == 1 {
  67. // v.PermissionName = fmt.Sprint(v.PermissionName, "(5w)")
  68. // } else if p.ExpensiveYx == 2 {
  69. // v.PermissionName = fmt.Sprint(v.PermissionName, "(10w)")
  70. // } else {
  71. // v.PermissionName = fmt.Sprint(v.PermissionName, "(3w)")
  72. // }
  73. //}
  74. }
  75. if v.IsUpgrade == 1 && strings.Contains("正式,永续", v.Status) {
  76. v.RaiBothHas = true
  77. v.Remark = v.PermissionName
  78. v.PermissionName += "(升级)"
  79. if !unify {
  80. v.PermissionType = 0
  81. }
  82. v.PermissionTypeName = ""
  83. v.IsMerge = true
  84. }
  85. // 主客观
  86. if v.PermissionType == 1 {
  87. v.PermissionTypeName = "主观"
  88. if v.StartDate != "" {
  89. subjectMap[v.PermissionName] = v
  90. }
  91. }
  92. if v.PermissionType == 2 {
  93. v.PermissionTypeName = "客观"
  94. if v.StartDate != "" {
  95. objectMap[v.PermissionName] = v
  96. }
  97. }
  98. // 默认都为合并, 后续再判断不合并的, 方便前端处理
  99. if v.StartDate != "" {
  100. v.IsMerge = true
  101. }
  102. v.ClassifyName = productName
  103. }
  104. // 二次遍历, 通过权限时长状态以及客户状态判断是否需要合并主客观
  105. // 客户状态为试用且是在编辑时, 判断所有行业是否均为主客观都有, 都有的话且时长状态一致则均需要合并, 否则存在一个被拆出来展示的所有都要拆出来展示
  106. disperse := false
  107. for i := range permissionItems {
  108. v := permissionItems[i]
  109. if v.PermissionType == 0 || v.StartDate == "" {
  110. continue
  111. }
  112. // 判断是否主客观都有
  113. sub := subjectMap[v.PermissionName]
  114. ob := objectMap[v.PermissionName]
  115. if sub != nil && ob != nil {
  116. v.RaiBothHas = true
  117. }
  118. // 判断是否需要合并
  119. another := new(company.PermissionLookItem)
  120. if v.PermissionType == 1 {
  121. another = ob
  122. } else {
  123. another = sub
  124. }
  125. if another != nil {
  126. if v.StartDate == another.StartDate && v.EndDate == another.EndDate && v.Status == another.Status {
  127. v.IsMerge = true
  128. } else {
  129. v.IsMerge = false
  130. }
  131. } else {
  132. v.IsMerge = false
  133. }
  134. // 是否需要全部拆分
  135. if unify && !v.IsMerge {
  136. disperse = true
  137. }
  138. }
  139. // 全部合并/拆分(正式客户增开试用/试用客户编辑使用)
  140. if unify {
  141. if disperse {
  142. resp.RaiMerge = 2
  143. } else {
  144. if len(subjectMap) == len(objectMap) {
  145. resp.RaiMerge = 1
  146. }
  147. }
  148. }
  149. //有升级权限时只展示一个
  150. newpPermissionItems := make([]*company.PermissionLookItem, 0)
  151. if unify {
  152. //newpPermissionItems = permissionItems
  153. for _, v := range permissionItems {
  154. if utils.InArrayByStr(filterPermissionName, v.PermissionName) {
  155. continue
  156. }
  157. newpPermissionItems = append(newpPermissionItems, v)
  158. }
  159. } else {
  160. upgradeMap := make(map[string]int)
  161. for _, v := range permissionItems {
  162. //研选扣点包不展示
  163. //if v.ChartPermissionId == utils.YAN_XUAN_KOU_DIAN_BAO_ID {
  164. // continue
  165. //}
  166. if utils.InArrayByStr(filterPermissionName, v.PermissionName) {
  167. continue
  168. }
  169. if _, ok := upgradeMap[v.PermissionName]; !ok {
  170. newpPermissionItems = append(newpPermissionItems, v)
  171. if strings.Contains(v.PermissionName, "升级") {
  172. upgradeMap[v.PermissionName] = v.ChartPermissionId
  173. }
  174. }
  175. }
  176. }
  177. resp.Items = newpPermissionItems
  178. resp.CheckList = checkList
  179. resp.ClassifyName = productName
  180. return
  181. }
  182. // 处理哪些权选择了升级
  183. func PermissionIdUpgrade(permissionNames string) (mapPermissionIdUpgrade map[int]int, err error) {
  184. permissionArr := strings.Split(permissionNames, ",")
  185. if len(permissionArr) == 0 {
  186. return
  187. }
  188. var permissionNameUpgrade []string
  189. for _, v := range permissionArr {
  190. if strings.Contains(v, "升级") {
  191. permissionNameUpgrade = append(permissionNameUpgrade, "'"+strings.Replace(v, "(升级)", "", -1)+"'")
  192. }
  193. }
  194. if len(permissionNameUpgrade) == 0 {
  195. return
  196. }
  197. chartList, e := models.GetChartPermissionByNames(permissionNameUpgrade)
  198. if e != nil {
  199. err = errors.New("GetChartPermissionByNames 获取品种信息失败, Err:" + e.Error())
  200. return
  201. }
  202. mapPermissionId := make(map[int]int)
  203. for _, v := range chartList {
  204. mapPermissionId[v.ChartPermissionId] = v.ChartPermissionId
  205. }
  206. mapPermissionIdUpgrade = mapPermissionId
  207. return
  208. }
  209. // 处理申请服务更新的权益客户的升级权限,主客观权限的修改
  210. func CheckCompanyUpgrade(permissionIds string) (permissionIdsNew string, mapPermissionIdUpgrade map[int]int, err error) {
  211. //给升级的产业ID加一个极大值判断处理
  212. mapPermissionIdUpgrade = make(map[int]int)
  213. var permissionSliceNew []int
  214. permissionSlice := strings.Split(permissionIds, ",")
  215. mapPermissionId := make(map[int]int)
  216. //var permissionIdsNew string //新的权限id x
  217. var permissionIdsUpgrade string //升级的权限ID
  218. var permissionIdsNoUpgrade string // 没有升级的权限ID
  219. var permissionIdOnly int //处理普通权限与升级权限同时勾选的时候的拦截
  220. for _, v := range permissionSlice {
  221. permissionId, _ := strconv.Atoi(v)
  222. if permissionId > utils.PERMISSION_ID_UPGRADE {
  223. permissionIdsUpgrade += strconv.Itoa(permissionId-utils.PERMISSION_ID_UPGRADE) + ","
  224. permissionIdOnly = permissionId - utils.PERMISSION_ID_UPGRADE
  225. } else {
  226. permissionIdsNoUpgrade += strconv.Itoa(permissionId) + ","
  227. permissionIdOnly = permissionId
  228. }
  229. if _, ok := mapPermissionId[permissionIdOnly]; ok {
  230. err = errors.New("升级权限与普通权限重复选择!" + permissionIds)
  231. return
  232. }
  233. mapPermissionId[permissionIdOnly] = permissionIdOnly
  234. }
  235. if permissionIdsUpgrade != "" {
  236. permissionIdsUpgrade = strings.TrimRight(permissionIdsUpgrade, ",")
  237. permissionStrUpgrade, e := company.GetPermissionIdById(permissionIdsUpgrade)
  238. if e != nil {
  239. err = errors.New("获取升级主观客观权限信息失败!" + e.Error())
  240. return
  241. }
  242. permissionSliceUpgrade := strings.Split(permissionStrUpgrade, ",")
  243. for _, v := range permissionSliceUpgrade {
  244. permissionId, _ := strconv.Atoi(v)
  245. mapPermissionIdUpgrade[permissionId] = 1
  246. permissionSliceNew = append(permissionSliceNew, permissionId)
  247. }
  248. }
  249. if permissionIdsNoUpgrade != "" {
  250. permissionIdsNoUpgrade = strings.TrimRight(permissionIdsNoUpgrade, ",")
  251. permissionStrNoUpgrade, e := company.GetPermissionIdById(permissionIdsNoUpgrade)
  252. if e != nil {
  253. err = errors.New("获取主观客观权限信息失败!" + e.Error())
  254. return
  255. }
  256. permissionSliceNoUpgrade := strings.Split(permissionStrNoUpgrade, ",")
  257. for _, v := range permissionSliceNoUpgrade {
  258. permissionId, _ := strconv.Atoi(v)
  259. permissionSliceNew = append(permissionSliceNew, permissionId)
  260. }
  261. }
  262. for _, v := range permissionSliceNew {
  263. permissionIdsNew += strconv.Itoa(v) + ","
  264. }
  265. permissionIdsNew = strings.TrimRight(permissionIdsNew, ",")
  266. return
  267. }
  268. // CheckCompanyUpgradeV2 处理申请服务更新的权益客户的升级权限,主客观权限的修改
  269. func CheckCompanyUpgradeV2(strPermissionIds string, isRai bool) (newPermissions string, upgradeMap map[int]int, expensiveMap map[int]int, err error) {
  270. upgradeMap = make(map[int]int)
  271. expensiveMap = make(map[int]int)
  272. permissionArr := strings.Split(strPermissionIds, ",")
  273. if len(permissionArr) == 0 {
  274. return
  275. }
  276. // 非内部人员获取主观对应的客观权限
  277. subObjMap := make(map[int]int, 0)
  278. if !isRai {
  279. m, e := GetRaiSubObjPermissionIdMap()
  280. if e != nil {
  281. err = fmt.Errorf("GetRaiSubObjPermissionIdMap Err: %s", e.Error())
  282. return
  283. }
  284. subObjMap = m
  285. }
  286. existsId := make(map[int]bool) // 升级权限与普通权限冲突
  287. newPermissionArr := make([]string, 0)
  288. for _, v := range permissionArr {
  289. p, _ := strconv.Atoi(v)
  290. if existsId[p] {
  291. continue
  292. }
  293. // 升级权限
  294. if p > utils.PERMISSION_ID_UPGRADE {
  295. o := p - utils.PERMISSION_ID_UPGRADE
  296. upgradeMap[o] = 1
  297. existsId[o] = true
  298. newPermissionArr = append(newPermissionArr, strconv.Itoa(o))
  299. continue
  300. }
  301. // 买方研选5w
  302. if p > utils.PERMISSION_ID_YANXUAN_DIFF && p < utils.PERMISSION_ID_YANXUAN_10W_DIFF {
  303. o := p - utils.PERMISSION_ID_YANXUAN_DIFF
  304. expensiveMap[o] = 1
  305. existsId[o] = true
  306. newPermissionArr = append(newPermissionArr, strconv.Itoa(o))
  307. continue
  308. }
  309. // 买方研选10w
  310. if p == utils.PERMISSION_ID_YANXUAN_10W_DIFF {
  311. o := utils.PERMISSION_ID_YANXUAN_10W_DIFF
  312. expensiveMap[o] = 1
  313. existsId[o] = true
  314. newPermissionArr = append(newPermissionArr, strconv.Itoa(o))
  315. continue
  316. }
  317. // 非升级权限
  318. existsId[p] = true
  319. newPermissionArr = append(newPermissionArr, strconv.Itoa(p))
  320. // 非内部人员选择主观时需要加上客观的权限
  321. if !isRai && subObjMap[p] > 0 {
  322. newPermissionArr = append(newPermissionArr, strconv.Itoa(subObjMap[p]))
  323. }
  324. }
  325. newPermissions = strings.Join(newPermissionArr, ",")
  326. return
  327. }
  328. // GetRaiSubObjPermissionIdMap 获取权益行业主观权限对应的客观权限
  329. func GetRaiSubObjPermissionIdMap() (permissionMap map[int]int, err error) {
  330. permissionMap = make(map[int]int, 0)
  331. permissions, e := models.GetChartPermissionList()
  332. if e != nil {
  333. err = fmt.Errorf("GetChartPermissionList, Err: %s", e.Error())
  334. return
  335. }
  336. subMap, objMap := make(map[string]int), make(map[string]int)
  337. for _, v := range permissions {
  338. if !strings.Contains("医药消费科技智造", v.PermissionName) {
  339. continue
  340. }
  341. if strings.Contains(v.Remark, "主观") {
  342. subMap[v.PermissionName] = v.ChartPermissionId
  343. continue
  344. }
  345. objMap[v.PermissionName] = v.ChartPermissionId
  346. }
  347. nameArr := []string{utils.YI_YAO_NAME, utils.XIAO_FEI_NAME, utils.KE_JI_NAME, utils.ZHI_ZAO_NAME}
  348. for _, n := range nameArr {
  349. permissionMap[subMap[n]] = objMap[n]
  350. }
  351. return
  352. }
  353. // GetPermissionNameMap 权限名称与权限ID的map
  354. func GetPermissionNameMap() (mapItem map[int]string, err error) {
  355. list, e := models.GetChartPermissionListRai()
  356. if e != nil {
  357. err = e
  358. return
  359. }
  360. mapPermissionName := make(map[int]string)
  361. for _, v := range list {
  362. mapPermissionName[v.ChartPermissionId] = v.PermissionName
  363. }
  364. mapItem = mapPermissionName
  365. return
  366. }
  367. func GetBasePermissionLookItem(productId int) (allPermissions []*company.PermissionLookItem, permissionMap map[int][]*company.PermissionLookItem, err error) {
  368. //子权限切片集合
  369. allPermissions, err = company.GetPermissionLookItemsByProductId(productId)
  370. if err != nil {
  371. err = fmt.Errorf("获取权限失败,Err:%s", err.Error())
  372. return
  373. }
  374. permissionMap = make(map[int][]*company.PermissionLookItem, 0)
  375. for _, v := range allPermissions {
  376. if v.ParentId > 0 {
  377. permissionMap[v.ParentId] = append(permissionMap[v.ParentId], v)
  378. }
  379. }
  380. return
  381. }
  382. func GetBaseFiccPermissionSetItem() (allPermissions []*company.PermissionSetItem, permissionMap map[int][]*company.PermissionSetItem, err error) {
  383. //子权限切片集合
  384. allPermissions, err = company.GetPermissionByProductId(utils.COMPANY_PRODUCT_FICC_ID)
  385. if err != nil {
  386. err = fmt.Errorf("获取权限失败,Err:%s", err.Error())
  387. return
  388. }
  389. permissionMap = make(map[int][]*company.PermissionSetItem, 0)
  390. for _, v := range allPermissions {
  391. if v.ParentId > 0 {
  392. permissionMap[v.ParentId] = append(permissionMap[v.ParentId], v)
  393. }
  394. }
  395. return
  396. }
  397. func GetPermissionFirstArr(productId int) (list []*models.ChartPermission, err error) {
  398. if productId == 0 {
  399. list, err = models.GetPermissionFirst()
  400. } else {
  401. list, err = models.GetPermissionFirstByProductId(productId)
  402. }
  403. return
  404. }