chart_permission.go 12 KB

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