chart_permission.go 15 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478
  1. package services
  2. import (
  3. "eta/eta_api/models"
  4. "eta/eta_api/models/company"
  5. "eta/eta_api/utils"
  6. "fmt"
  7. "time"
  8. )
  9. // 获取权限列表
  10. func GetChartPermissionList(cond string, pars []interface{}) (permissionList []*models.ChartPermission, err error) {
  11. ob := new(models.ChartPermission)
  12. permissionList, err = ob.GetItemsByCondition(cond, pars)
  13. if err != nil {
  14. err = fmt.Errorf("获取权限列表失败, Err: %s", err.Error())
  15. return
  16. }
  17. return
  18. }
  19. // 新增权限
  20. func AddChartPermission(req models.PermissionAddReq) (err error, errMsg string) {
  21. // 重名校验
  22. ob := new(models.ChartPermission)
  23. existCond := ` AND permission_name = ? AND parent_id = ?`
  24. existPars := make([]interface{}, 0)
  25. existPars = append(existPars, req.PermissionName, req.ParentId)
  26. exist, e := ob.GetItemByCondition(existCond, existPars)
  27. if e != nil && e.Error() != utils.ErrNoRow() {
  28. errMsg = "操作失败"
  29. err = fmt.Errorf("获取重名品种权限失败, Err: " + e.Error())
  30. return
  31. }
  32. if exist != nil && exist.ChartPermissionId > 0 {
  33. errMsg = "品种名称已存在"
  34. err = fmt.Errorf("品种名称已存在")
  35. return
  36. }
  37. // 获取最大的排序值
  38. maxSort, e := ob.GetMaxSort()
  39. if e != nil {
  40. errMsg = "查询品种排序失败"
  41. err = fmt.Errorf("查询品种排序失败, Err: " + e.Error())
  42. return
  43. }
  44. parentPermissionName := ""
  45. if req.ParentId > 0 {
  46. //查询父级是否存在
  47. parent, e := ob.GetItemById(req.ParentId)
  48. if e != nil {
  49. errMsg = "查询父级品种失败"
  50. err = fmt.Errorf("查询父级品种失败, Err: " + e.Error())
  51. return
  52. }
  53. if parent.ChartPermissionId == 0 {
  54. errMsg = "请选择正确的父级品种"
  55. err = fmt.Errorf("请选择正确的父级品种")
  56. return
  57. }
  58. if parent.ParentId != 0 {
  59. errMsg = "只能选择一级品种作为父级品种"
  60. err = fmt.Errorf("只能选择一级品种作为父级品种")
  61. return
  62. }
  63. parentPermissionName = parent.PermissionName
  64. } else {
  65. parentPermissionName = req.PermissionName
  66. }
  67. // 新增
  68. ob.ChartPermissionName = req.PermissionName
  69. ob.PermissionName = req.PermissionName
  70. ob.Remark = req.PermissionName
  71. ob.ParentId = req.ParentId
  72. ob.ProductId = 1
  73. ob.IsPublic = req.IsPublic
  74. ob.Enabled = req.Enabled
  75. ob.Sort = maxSort + 1
  76. ob.CreatedTime = time.Now()
  77. ob.LastUpdatedTime = time.Now()
  78. if ob.ProductId == 1 {
  79. ob.ProductName = "ficc"
  80. } else {
  81. ob.ProductName = "权益"
  82. }
  83. ob.ProductPermissionName = ob.ProductName + ob.PermissionName
  84. ob.ClassifyName = parentPermissionName
  85. if e = ob.Create(); e != nil {
  86. errMsg = "操作失败"
  87. err = fmt.Errorf("新增品种权限失败, Err: " + e.Error())
  88. return
  89. }
  90. if req.ParentId > 0 && req.Enabled == 1 {
  91. // 更新父级品种的启用禁用
  92. err = ob.SetEnabledByChartPermissionId(req.ParentId, req.Enabled)
  93. if err != nil {
  94. errMsg = "更新品种启用禁用状态失败"
  95. err = fmt.Errorf("更新品种启用禁用状态失败, Err: " + err.Error())
  96. return
  97. }
  98. }
  99. // 同步本次变更的数据
  100. go func() {
  101. _, _ = ChartFiccPermissionSync()
  102. }()
  103. return
  104. }
  105. func EditChartPermission(req models.PermissionEditReq) (err error, errMsg string) {
  106. //查询是否存在品种
  107. ob := new(models.ChartPermission)
  108. item, e := ob.GetItemById(req.PermissionId)
  109. if e != nil {
  110. errMsg = "查询品种信息失败"
  111. err = fmt.Errorf("查询品种信息失败, Err: " + e.Error())
  112. return
  113. }
  114. if item.ChartPermissionId == 0 {
  115. errMsg = "请选择正确的品种"
  116. err = fmt.Errorf("请选择正确的品种")
  117. return
  118. }
  119. // 查询品种名称是否修改
  120. if item.PermissionName != req.PermissionName {
  121. existCond := ` AND permission_name = ? AND parent_id = ? AND chart_permission_id !=?`
  122. existPars := make([]interface{}, 0)
  123. existPars = append(existPars, req.PermissionName, item.ParentId, req.PermissionId)
  124. exist, e := ob.GetItemByCondition(existCond, existPars)
  125. if e != nil && e.Error() != utils.ErrNoRow() {
  126. errMsg = "操作失败"
  127. err = fmt.Errorf("获取重名品种权限失败, Err: " + e.Error())
  128. return
  129. }
  130. if exist != nil && exist.ChartPermissionId > 0 {
  131. errMsg = "品种名称已存在"
  132. err = fmt.Errorf("品种名称已存在")
  133. return
  134. }
  135. }
  136. //判断父级品种是否存在
  137. parentPermissionName := ""
  138. if req.ParentId > 0 {
  139. //查询父级是否存在
  140. parent, e := ob.GetItemById(req.ParentId)
  141. if e != nil {
  142. errMsg = "查询父级品种失败"
  143. err = fmt.Errorf("查询父级品种失败, Err: " + e.Error())
  144. return
  145. }
  146. if parent.ChartPermissionId == 0 {
  147. errMsg = "请选择正确的父级品种"
  148. err = fmt.Errorf("请选择正确的父级品种")
  149. return
  150. }
  151. if parent.ParentId != 0 {
  152. errMsg = "只能选择一级品种作为父级品种"
  153. err = fmt.Errorf("只能选择一级品种作为父级品种")
  154. return
  155. }
  156. parentPermissionName = parent.PermissionName
  157. } else {
  158. parentPermissionName = req.PermissionName
  159. }
  160. updateCol := []string{"PermissionName", "IsPublic", "ParentId", "ClassifyName", "LastUpdatedTime", "ProductPermissionName"}
  161. if item.ProductId == 1 {
  162. item.Remark = req.PermissionName //remark 在权益品种中用来区分主客观
  163. updateCol = append(updateCol, "Remark")
  164. }
  165. item.PermissionName = req.PermissionName
  166. item.IsPublic = req.IsPublic
  167. item.ParentId = req.ParentId
  168. item.ClassifyName = parentPermissionName
  169. item.ProductPermissionName = item.ProductName + item.PermissionName
  170. item.LastUpdatedTime = time.Now()
  171. err = item.Update(updateCol)
  172. if err != nil {
  173. errMsg = "更新失败"
  174. err = fmt.Errorf("更新品种失败, Err: " + err.Error())
  175. return
  176. }
  177. //判断是否是一级品种
  178. if item.ParentId == 0 {
  179. // 判断是否设置公有的二级品种
  180. if len(req.PublicPermissionIds) > 0 {
  181. // 更新二级品种的公有权限
  182. err = ob.SetIsPublic(req.PublicPermissionIds, item.ChartPermissionId, 1)
  183. if err != nil {
  184. errMsg = "更新品种公有权限失败"
  185. err = fmt.Errorf("更新品种公有权限失败, Err: " + err.Error())
  186. return
  187. }
  188. }
  189. // 更新二级的启动禁用
  190. err = ob.UpdateClassifyNameByParentId(item.ChartPermissionId, req.PermissionName)
  191. if err != nil {
  192. errMsg = "更新品种启用禁用状态失败"
  193. err = fmt.Errorf("更新品种启用禁用状态失败, Err: " + err.Error())
  194. return
  195. }
  196. } else if item.ParentId > 0 {
  197. if item.Enabled == 1 {
  198. // 更新父级品种的启用禁用
  199. err = ob.SetEnabledByChartPermissionId(item.ParentId, item.Enabled)
  200. if err != nil {
  201. errMsg = "更新品种启用禁用状态失败"
  202. err = fmt.Errorf("更新品种启用禁用状态失败, Err: " + err.Error())
  203. return
  204. }
  205. }
  206. }
  207. // 同步本次变更的数据
  208. go func() {
  209. _, _ = ChartFiccPermissionSync()
  210. }()
  211. return
  212. }
  213. // SetEnabledChartPermission 设置启用禁用
  214. func SetEnabledChartPermission(req models.PermissionEnabledReq) (err error, errMsg string) {
  215. //查询是否存在品种
  216. ob := new(models.ChartPermission)
  217. item, e := ob.GetItemById(req.PermissionId)
  218. if e != nil {
  219. errMsg = "查询品种信息失败"
  220. err = fmt.Errorf("查询品种信息失败, Err: " + e.Error())
  221. return
  222. }
  223. if item.ChartPermissionId == 0 {
  224. errMsg = "请选择正确的品种"
  225. err = fmt.Errorf("请选择正确的品种")
  226. return
  227. }
  228. // 查询品种名称是否修改
  229. item.Enabled = req.Enabled
  230. item.LastUpdatedTime = time.Now()
  231. err = item.Update([]string{"Enabled", "LastUpdatedTime"})
  232. if err != nil {
  233. errMsg = "更新失败"
  234. err = fmt.Errorf("更新品种失败, Err: " + err.Error())
  235. return
  236. }
  237. //判断是否是一级品种
  238. if item.ParentId == 0 {
  239. // 判断是否设置公有的二级品种
  240. // 更新二级的启动禁用
  241. err = ob.SetEnabledByParentId(item.ChartPermissionId, req.Enabled)
  242. if err != nil {
  243. errMsg = "更新品种启用禁用状态失败"
  244. err = fmt.Errorf("更新品种启用禁用状态失败, Err: " + err.Error())
  245. return
  246. }
  247. //
  248. } else if item.ParentId > 0 {
  249. if req.Enabled == 1 {
  250. // 更新父级品种的启用禁用
  251. err = ob.SetEnabledByChartPermissionId(item.ParentId, req.Enabled)
  252. if err != nil {
  253. errMsg = "更新品种启用禁用状态失败"
  254. err = fmt.Errorf("更新品种启用禁用状态失败, Err: " + err.Error())
  255. return
  256. }
  257. }
  258. }
  259. // 同步本次变更的数据
  260. go func() {
  261. _, _ = ChartFiccPermissionSync()
  262. }()
  263. return
  264. }
  265. // MoveChartPermission 移动品种
  266. func MoveChartPermission(req models.PermissionMoveReq) (err error, errMsg string) {
  267. ob := new(models.ChartPermission)
  268. chartPermissionId := req.PermissionId
  269. prevChartPermissionId := req.PrevPermissionId
  270. nextChartPermissionId := req.NextPermissionId
  271. //如果有传入 上一个兄弟节点分类id
  272. var (
  273. permissionInfo *models.ChartPermission
  274. prevPermission *models.ChartPermission
  275. nextPermission *models.ChartPermission
  276. prevSort int
  277. nextSort int
  278. )
  279. // 移动对象为分类, 判断权限
  280. permissionInfo, err = ob.GetItemById(chartPermissionId)
  281. if err != nil {
  282. if err.Error() == utils.ErrNoRow() {
  283. errMsg = "当前品种不存在"
  284. err = fmt.Errorf("获取品种信息失败,Err:" + err.Error())
  285. return
  286. }
  287. errMsg = "移动失败"
  288. err = fmt.Errorf("获取分类信息失败,Err:" + err.Error())
  289. return
  290. } else if permissionInfo.ChartPermissionId == 0 {
  291. errMsg = "当前品种不存在"
  292. err = fmt.Errorf("获取品种信息失败,Err:" + err.Error())
  293. return
  294. }
  295. parentChartPermissionId := permissionInfo.ParentId
  296. productId := permissionInfo.ProductId
  297. if prevChartPermissionId > 0 {
  298. prevPermission, err = ob.GetItemById(prevChartPermissionId)
  299. if err != nil {
  300. errMsg = "移动失败"
  301. err = fmt.Errorf("获取上一个兄弟节点分类信息失败,Err:" + err.Error())
  302. return
  303. }
  304. prevSort = prevPermission.Sort
  305. }
  306. if nextChartPermissionId > 0 {
  307. //下一个兄弟节点
  308. nextPermission, err = ob.GetItemById(nextChartPermissionId)
  309. if err != nil {
  310. errMsg = "移动失败"
  311. err = fmt.Errorf("获取下一个兄弟节点分类信息失败,Err:" + err.Error())
  312. return
  313. }
  314. nextSort = nextPermission.Sort
  315. }
  316. err, errMsg = moveChartPermission(permissionInfo, prevPermission, nextPermission, parentChartPermissionId, prevSort, nextSort, productId)
  317. return
  318. }
  319. // moveChartPermission 移动指标分类
  320. func moveChartPermission(permissionInfo, prevPermission, nextPermission *models.ChartPermission, parentId, prevSort, nextSort, productId int) (err error, errMsg string) {
  321. ob := new(models.ChartPermission)
  322. updateCol := make([]string, 0)
  323. //判断上级id是否一致,如果不一致的话,那么需要移动该分类层级
  324. if permissionInfo.ParentId != parentId {
  325. errMsg = "移动失败"
  326. err = fmt.Errorf("不支持目录层级变更")
  327. return
  328. }
  329. if prevSort > 0 {
  330. //如果是移动在两个兄弟节点之间
  331. if nextSort > 0 {
  332. //下一个兄弟节点
  333. //如果上一个兄弟与下一个兄弟的排序权重是一致的,那么需要将下一个兄弟(以及下个兄弟的同样排序权重)的排序权重+2,自己变成上一个兄弟的排序权重+1
  334. if prevSort == nextSort || prevSort == permissionInfo.Sort {
  335. //变更兄弟节点的排序
  336. updateSortStr := `sort + 2`
  337. //变更分类
  338. if prevPermission != nil {
  339. _ = models.UpdateChartPermissionSortByParentId(parentId, prevPermission.ChartPermissionId, prevPermission.Sort, updateSortStr, productId)
  340. } else {
  341. _ = models.UpdateChartPermissionSortByParentId(parentId, 0, prevSort, updateSortStr, productId)
  342. }
  343. } else {
  344. //如果下一个兄弟的排序权重正好是上个兄弟节点的下一层,那么需要再加一层了
  345. if nextSort-prevSort == 1 {
  346. //变更兄弟节点的排序
  347. updateSortStr := `sort + 1`
  348. //变更分类
  349. if prevPermission != nil {
  350. _ = models.UpdateChartPermissionSortByParentId(parentId, prevPermission.ChartPermissionId, prevSort, updateSortStr, productId)
  351. } else {
  352. _ = models.UpdateChartPermissionSortByParentId(parentId, 0, prevSort, updateSortStr, productId)
  353. }
  354. }
  355. }
  356. }
  357. permissionInfo.Sort = prevSort + 1
  358. permissionInfo.LastUpdatedTime = time.Now()
  359. updateCol = append(updateCol, "Sort", "LastUpdatedTime")
  360. } else if prevPermission == nil && nextPermission == nil && parentId > 0 {
  361. //处理只拖动到目录里,默认放到目录底部的情况
  362. var maxSort int
  363. maxSort, err = ob.GetMaxSortByParentId(parentId)
  364. if err != nil {
  365. errMsg = "移动失败"
  366. err = fmt.Errorf("查询组内排序信息失败,Err:" + err.Error())
  367. return
  368. }
  369. permissionInfo.Sort = maxSort + 1 //那就是排在组内最后一位
  370. permissionInfo.LastUpdatedTime = time.Now()
  371. updateCol = append(updateCol, "Sort", "LastUpdatedTime")
  372. } else {
  373. // 拖动到父级分类的第一位
  374. firstPermission, tmpErr := ob.GetFirstChartPermissionByParentId(parentId)
  375. if tmpErr != nil && tmpErr.Error() != utils.ErrNoRow() {
  376. errMsg = "移动失败"
  377. err = fmt.Errorf("获取获取当前父级分类下的排序第一条的分类信息失败,Err:" + tmpErr.Error())
  378. return
  379. }
  380. //如果该分类下存在其他分类,且第一个其他分类的排序等于0,那么需要调整排序
  381. if firstPermission != nil && firstPermission.ChartPermissionId != 0 && firstPermission.Sort == 0 {
  382. updateSortStr := ` sort + 1 `
  383. _ = models.UpdateChartPermissionSortByParentId(parentId, firstPermission.ChartPermissionId-1, 0, updateSortStr, productId)
  384. }
  385. permissionInfo.Sort = 0 //那就是排在第一位
  386. permissionInfo.LastUpdatedTime = time.Now()
  387. updateCol = append(updateCol, "Sort", "LastUpdatedTime")
  388. }
  389. //更新
  390. if len(updateCol) > 0 {
  391. err = permissionInfo.Update(updateCol)
  392. if err != nil {
  393. errMsg = "移动失败"
  394. err = fmt.Errorf("修改失败,Err:" + err.Error())
  395. return
  396. }
  397. }
  398. // 同步本次变更的数据
  399. go func() {
  400. _, _ = ChartFiccPermissionSync()
  401. }()
  402. return
  403. }
  404. // GetChartPermissionEnabledList 查询已启用的FICC权限列表
  405. func GetChartPermissionEnabledList() (list []*company.PermissionSetList, err error) {
  406. dataList, err := GetChartPermissionList(" AND enabled = 1", []interface{}{})
  407. if err != nil {
  408. return
  409. }
  410. //enabledList := make([]*models.ChartPermission, 0)
  411. parentList := make([]*models.ChartPermission, 0)
  412. enabledMap := make(map[int][]*company.PermissionSetItem)
  413. listMap := make(map[int]*models.ChartPermission)
  414. for _, v := range dataList {
  415. listMap[v.ChartPermissionId] = v
  416. if v.ParentId == 0 {
  417. parentList = append(parentList, v)
  418. }
  419. if v.Enabled == 1 && v.ParentId > 0 && v.PermissionType == 0 {
  420. //enabledList = append(enabledList, v)
  421. item := &company.PermissionSetItem{
  422. ChartPermissionId: v.ChartPermissionId,
  423. PermissionName: v.PermissionName,
  424. PermissionType: v.PermissionType,
  425. }
  426. enabledMap[v.ParentId] = append(enabledMap[v.ParentId], item)
  427. }
  428. }
  429. for _, v := range parentList {
  430. items, ok := enabledMap[v.ChartPermissionId]
  431. if !ok {
  432. continue
  433. }
  434. checkList := make([]int, 0)
  435. p := new(company.PermissionSetList)
  436. p.PermissionName = v.PermissionName
  437. p.Child = items
  438. if v.PermissionName == "宏观经济" {
  439. checkList = append(checkList, 1)
  440. }
  441. p.CheckList = checkList
  442. list = append(list, p)
  443. }
  444. return
  445. }