chart_permission.go 13 KB

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