en_permission.go 15 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561
  1. package english_report
  2. import (
  3. "encoding/json"
  4. "eta/eta_api/controllers"
  5. "eta/eta_api/models"
  6. "eta/eta_api/services"
  7. "eta/eta_api/utils"
  8. "fmt"
  9. "strings"
  10. "time"
  11. )
  12. // EnPermissionController 英文权限
  13. type EnPermissionController struct {
  14. controllers.BaseAuthController
  15. }
  16. // Add
  17. // @Title 新增品种权限
  18. // @Description 新增品种权限
  19. // @Param request body models.EnPermissionAddReq true "type json string"
  20. // @Success 200 Ret=200 操作成功
  21. // @router /add [post]
  22. func (this *EnPermissionController) Add() {
  23. br := new(models.BaseResponse).Init()
  24. defer func() {
  25. if br.ErrMsg == "" {
  26. br.IsSendEmail = false
  27. }
  28. this.Data["json"] = br
  29. this.ServeJSON()
  30. }()
  31. sysUser := this.SysUser
  32. if sysUser == nil {
  33. br.Msg = "请登录"
  34. br.ErrMsg = "请登录,SysUser Is Empty"
  35. br.Ret = 408
  36. return
  37. }
  38. var req models.EnPermissionAddReq
  39. if e := json.Unmarshal(this.Ctx.Input.RequestBody, &req); e != nil {
  40. br.Msg = "参数解析异常!"
  41. br.ErrMsg = "参数解析失败,Err:" + e.Error()
  42. return
  43. }
  44. req.PermissionName = strings.TrimSpace(req.PermissionName)
  45. if req.PermissionName == "" {
  46. br.Msg = "请输入品种名称"
  47. return
  48. }
  49. if req.Enabled != 1 && req.Enabled != 0 {
  50. br.Msg = "请设置正确的状态"
  51. return
  52. }
  53. // 重名校验
  54. existOB := new(models.EnPermission)
  55. existCond := fmt.Sprintf(` AND %s = ? AND %s = ?`, models.EnPermissionColumns.EnPermissionName, models.EnPermissionColumns.ParentId)
  56. existPars := make([]interface{}, 0)
  57. existPars = append(existPars, req.PermissionName, req.ParentId)
  58. exist, e := existOB.GetItemByCondition(existCond, existPars)
  59. if e != nil && e.Error() != utils.ErrNoRow() {
  60. br.Msg = "操作失败"
  61. br.ErrMsg = "获取重名品种权限失败, Err: " + e.Error()
  62. return
  63. }
  64. if exist != nil && exist.EnPermissionId > 0 {
  65. br.Msg = "品种名称已存在"
  66. return
  67. }
  68. // 获取最大的排序值
  69. item := new(models.EnPermission)
  70. maxSort, e := item.GetMaxSort()
  71. if e != nil {
  72. br.Msg = "操作失败"
  73. br.ErrMsg = "查询品种排序失败, Err: " + e.Error()
  74. return
  75. }
  76. // 新增
  77. item.EnPermissionName = req.PermissionName
  78. item.ParentId = req.ParentId
  79. item.Sort = maxSort + 1
  80. item.Enabled = req.Enabled
  81. item.CreateTime = time.Now().Local()
  82. item.ModifyTime = time.Now().Local()
  83. if e = item.Create(); e != nil {
  84. br.Msg = "操作失败"
  85. br.ErrMsg = "新增品种权限失败, Err: " + e.Error()
  86. return
  87. }
  88. br.Ret = 200
  89. br.Success = true
  90. br.Msg = "操作成功"
  91. br.Data = item.EnPermissionId
  92. }
  93. // Edit
  94. // @Title 编辑品种权限
  95. // @Description 编辑品种权限
  96. // @Param request body models.EnPermissionAddReq true "type json string"
  97. // @Success 200 Ret=200 操作成功
  98. // @router /edit [post]
  99. func (this *EnPermissionController) Edit() {
  100. br := new(models.BaseResponse).Init()
  101. defer func() {
  102. if br.ErrMsg == "" {
  103. br.IsSendEmail = false
  104. }
  105. this.Data["json"] = br
  106. this.ServeJSON()
  107. }()
  108. sysUser := this.SysUser
  109. if sysUser == nil {
  110. br.Msg = "请登录"
  111. br.ErrMsg = "请登录,SysUser Is Empty"
  112. br.Ret = 408
  113. return
  114. }
  115. var req models.EnPermissionEditReq
  116. if e := json.Unmarshal(this.Ctx.Input.RequestBody, &req); e != nil {
  117. br.Msg = "参数解析异常!"
  118. br.ErrMsg = "参数解析失败,Err:" + e.Error()
  119. return
  120. }
  121. if req.PermissionId <= 0 {
  122. br.Msg = "参数有误"
  123. return
  124. }
  125. req.PermissionName = strings.TrimSpace(req.PermissionName)
  126. if req.PermissionName == "" {
  127. br.Msg = "请输入品种名称"
  128. return
  129. }
  130. // 重名校验
  131. ob := new(models.EnPermission)
  132. existCond := fmt.Sprintf(` AND %s = ? AND %s = ? AND %s <> ?`, models.EnPermissionColumns.EnPermissionName, models.EnPermissionColumns.ParentId, models.EnPermissionColumns.EnPermissionId)
  133. existPars := make([]interface{}, 0)
  134. existPars = append(existPars, req.PermissionName, req.ParentId, req.PermissionId)
  135. exist, e := ob.GetItemByCondition(existCond, existPars)
  136. if e != nil && e.Error() != utils.ErrNoRow() {
  137. br.Msg = "操作失败"
  138. br.ErrMsg = "获取重名品种权限失败, Err: " + e.Error()
  139. return
  140. }
  141. if exist != nil && exist.EnPermissionId > 0 {
  142. br.Msg = "品种名称已存在"
  143. return
  144. }
  145. item, e := ob.GetItemById(req.PermissionId)
  146. if e != nil {
  147. if e.Error() == utils.ErrNoRow() {
  148. br.Msg = "品种不存在, 请刷新页面"
  149. return
  150. }
  151. br.Msg = "操作失败"
  152. br.ErrMsg = "获取品种权限失败, Err: " + e.Error()
  153. return
  154. }
  155. originPid := item.ParentId
  156. item.EnPermissionName = req.PermissionName
  157. item.ParentId = req.ParentId
  158. item.ModifyTime = time.Now().Local()
  159. if e = item.Update([]string{"EnPermissionName", "ParentId", "ModifyTime"}); e != nil {
  160. br.Msg = "操作失败"
  161. br.ErrMsg = "更新品种权限失败, Err: " + e.Error()
  162. return
  163. }
  164. // 若二级分类转为一级分类, 则清空对应客户、分类权限
  165. if originPid > 0 && req.ParentId == 0 {
  166. if e = models.ClearEnPermissionsByPermissionId(item.EnPermissionId); e != nil {
  167. br.Msg = "操作失败"
  168. br.ErrMsg = "清空英文分类权限失败, Err: " + e.Error()
  169. return
  170. }
  171. }
  172. br.Ret = 200
  173. br.Success = true
  174. br.Msg = "操作成功"
  175. br.Data = item.EnPermissionId
  176. }
  177. // ParentList
  178. // @Title 父级品种列表
  179. // @Description 父级品种列表
  180. // @Success 200 Ret=200 操作成功
  181. // @router /parent/list [get]
  182. func (this *EnPermissionController) ParentList() {
  183. br := new(models.BaseResponse).Init()
  184. defer func() {
  185. if br.ErrMsg == "" {
  186. br.IsSendEmail = false
  187. }
  188. this.Data["json"] = br
  189. this.ServeJSON()
  190. }()
  191. sysUser := this.SysUser
  192. if sysUser == nil {
  193. br.Msg = "请登录"
  194. br.ErrMsg = "请登录,SysUser Is Empty"
  195. br.Ret = 408
  196. return
  197. }
  198. cond := fmt.Sprintf(` AND %s = ?`, models.EnPermissionColumns.ParentId)
  199. pars := make([]interface{}, 0)
  200. pars = append(pars, 0)
  201. ob := new(models.EnPermission)
  202. list, e := ob.GetItemsByCondition(cond, pars, []string{}, "sort ASC, create_time ASC")
  203. if e != nil {
  204. br.Msg = "获取失败"
  205. br.ErrMsg = "获取父级品种列表失败, Err: " + e.Error()
  206. return
  207. }
  208. resp := make([]*models.EnPermissionItem, 0)
  209. for _, v := range list {
  210. t := new(models.EnPermissionItem)
  211. t.PermissionId = v.EnPermissionId
  212. t.PermissionName = v.EnPermissionName
  213. t.CnPermissionName = v.CnPermissionName
  214. t.ParentId = v.ParentId
  215. t.Sort = v.Sort
  216. t.CreateTime = v.CreateTime.Format(utils.FormatDateTime)
  217. t.Child = make([]*models.EnPermissionItem, 0)
  218. resp = append(resp, t)
  219. }
  220. br.Ret = 200
  221. br.Success = true
  222. br.Msg = "获取成功"
  223. br.Data = resp
  224. }
  225. // List
  226. // @Title 品种列表
  227. // @Description 品种列表
  228. // @Param Keyword query string false "关键词"
  229. // @Param ReportId query int false "报告ID-用于邮件推送时过滤掉无权限的品种"
  230. // @Param VideoId query int false "路演视频ID-用于邮件推送时过滤掉无权限的品种"
  231. // @Success 200 Ret=200 操作成功
  232. // @router /list [get]
  233. func (this *EnPermissionController) List() {
  234. br := new(models.BaseResponse).Init()
  235. defer func() {
  236. if br.ErrMsg == "" {
  237. br.IsSendEmail = false
  238. }
  239. this.Data["json"] = br
  240. this.ServeJSON()
  241. }()
  242. sysUser := this.SysUser
  243. if sysUser == nil {
  244. br.Msg = "请登录"
  245. br.ErrMsg = "请登录,SysUser Is Empty"
  246. br.Ret = 408
  247. return
  248. }
  249. keyword := this.GetString("Keyword", "")
  250. keyword = strings.TrimSpace(keyword)
  251. enabled, _ := this.GetInt("Enabled", -1)
  252. // 禁用指定报告、指定路演无权限的品种
  253. reportId, _ := this.GetInt("ReportId", 0)
  254. videoId, _ := this.GetInt("VideoId", 0)
  255. limitIds := make([]int, 0)
  256. if reportId > 0 {
  257. ps, e := models.GetEnPermissionIdsByEnglishReportId(reportId)
  258. if e != nil {
  259. br.Msg = "获取失败"
  260. br.ErrMsg = "获取报告权限品种失败, Err: " + e.Error()
  261. return
  262. }
  263. limitIds = ps
  264. }
  265. if videoId > 0 {
  266. ps, e := models.GetEnPermissionIdsByEnglishVideoId(videoId)
  267. if e != nil {
  268. br.Msg = "获取失败"
  269. br.ErrMsg = "获取视频权限品种失败, Err: " + e.Error()
  270. return
  271. }
  272. limitIds = ps
  273. }
  274. cond := ``
  275. pars := make([]interface{}, 0)
  276. if keyword != "" {
  277. k := fmt.Sprint("%", keyword, "%")
  278. cond += fmt.Sprintf(` AND %s LIKE ?`, models.EnPermissionColumns.EnPermissionName)
  279. pars = append(pars, k)
  280. }
  281. list, e := models.GetEnPermissionUnionList(cond, pars)
  282. if e != nil {
  283. br.Msg = "获取失败"
  284. br.ErrMsg = "获取品种Union列表失败, Err: " + e.Error()
  285. return
  286. }
  287. // 品种树
  288. resp := make([]*models.EnPermissionItem, 0)
  289. childMap := make(map[int][]*models.EnPermissionItem)
  290. for _, v := range list {
  291. t := new(models.EnPermissionItem)
  292. t.PermissionId = v.EnPermissionId
  293. t.PermissionName = v.EnPermissionName
  294. t.CnPermissionName = v.CnPermissionName
  295. t.Enabled = v.Enabled
  296. t.ParentId = v.ParentId
  297. t.Sort = v.Sort
  298. t.CreateTime = v.CreateTime.Format(utils.FormatDateTime)
  299. t.Child = make([]*models.EnPermissionItem, 0)
  300. if v.ParentId == 0 {
  301. resp = append(resp, t)
  302. continue
  303. }
  304. if v.ParentId > 0 {
  305. if v.Enabled == 0 && enabled == 1 {
  306. continue
  307. }
  308. if childMap[v.ParentId] == nil {
  309. childMap[v.ParentId] = make([]*models.EnPermissionItem, 0)
  310. }
  311. // 无权限则隐藏
  312. if (reportId > 0 || videoId > 0) && !utils.InArrayByInt(limitIds, t.PermissionId) {
  313. continue
  314. }
  315. childMap[v.ParentId] = append(childMap[v.ParentId], t)
  316. }
  317. }
  318. for _, r := range resp {
  319. r.Child = childMap[r.PermissionId]
  320. }
  321. // todo 过滤禁用品种的一级分类,如果一级分类没有二级分类
  322. br.Ret = 200
  323. br.Success = true
  324. br.Msg = "获取成功"
  325. br.Data = resp
  326. }
  327. // Remove
  328. // @Title 删除品种权限
  329. // @Description 删除品种权限
  330. // @Param request body models.EnPermissionRemoveReq true "type json string"
  331. // @Success 200 Ret=200 操作成功
  332. // @router /remove [post]
  333. func (this *EnPermissionController) Remove() {
  334. br := new(models.BaseResponse).Init()
  335. defer func() {
  336. if br.ErrMsg == "" {
  337. br.IsSendEmail = false
  338. }
  339. this.Data["json"] = br
  340. this.ServeJSON()
  341. }()
  342. sysUser := this.SysUser
  343. if sysUser == nil {
  344. br.Msg = "请登录"
  345. br.ErrMsg = "请登录,SysUser Is Empty"
  346. br.Ret = 408
  347. return
  348. }
  349. var req models.EnPermissionRemoveReq
  350. if e := json.Unmarshal(this.Ctx.Input.RequestBody, &req); e != nil {
  351. br.Msg = "参数解析异常!"
  352. br.ErrMsg = "参数解析失败,Err:" + e.Error()
  353. return
  354. }
  355. if req.PermissionId <= 0 {
  356. br.Msg = "参数有误"
  357. return
  358. }
  359. ob := new(models.EnPermission)
  360. item, e := ob.GetItemById(req.PermissionId)
  361. if e != nil {
  362. if e.Error() == utils.ErrNoRow() {
  363. br.Msg = "品种不存在, 请刷新页面"
  364. return
  365. }
  366. br.Msg = "操作失败"
  367. br.ErrMsg = "获取品种权限失败, Err: " + e.Error()
  368. return
  369. }
  370. // 校验是否有子品种
  371. childCond := fmt.Sprintf(` AND %s = ?`, models.EnPermissionColumns.ParentId)
  372. childPars := make([]interface{}, 0)
  373. childPars = append(childPars, req.PermissionId)
  374. num, e := ob.GetCountByCondition(childCond, childPars)
  375. if e != nil {
  376. br.Msg = "操作失败"
  377. br.ErrMsg = "获取品种子权限数量失败, Err: " + e.Error()
  378. return
  379. }
  380. if num > 0 {
  381. br.Msg = "请先删除该品种下关联品种"
  382. return
  383. }
  384. // 校验是否关联客户
  385. {
  386. cond := fmt.Sprintf(` AND %s = ?`, models.EnCompanyPermissionColumns.EnPermissionId)
  387. pars := make([]interface{}, 0)
  388. pars = append(pars, item.EnPermissionId)
  389. ob := new(models.EnCompanyPermission)
  390. num, e := ob.GetCountByCondition(cond, pars)
  391. if e != nil {
  392. br.Msg = "操作失败"
  393. br.ErrMsg = "获取品种关联客户数量失败, Err: " + e.Error()
  394. return
  395. }
  396. if num > 0 {
  397. br.Msg = "该品种有关联客户, 不允许删除"
  398. return
  399. }
  400. }
  401. // 删除品种
  402. if e = item.Del(); e != nil {
  403. br.Msg = "操作失败"
  404. br.ErrMsg = "删除品种失败, Err: " + e.Error()
  405. return
  406. }
  407. br.Ret = 200
  408. br.Success = true
  409. br.Msg = "操作成功"
  410. }
  411. // Move
  412. // @Title 移动品种权限
  413. // @Description 移动品种权限
  414. // @Param request body models.EnPermissionMoveReq true "type json string"
  415. // @Success 200 Ret=200 操作成功
  416. // @router /move [post]
  417. func (this *EnPermissionController) Move() {
  418. br := new(models.BaseResponse).Init()
  419. defer func() {
  420. if br.ErrMsg == "" {
  421. br.IsSendEmail = false
  422. }
  423. this.Data["json"] = br
  424. this.ServeJSON()
  425. }()
  426. sysUser := this.SysUser
  427. if sysUser == nil {
  428. br.Msg = "请登录"
  429. br.ErrMsg = "请登录,SysUser Is Empty"
  430. br.Ret = 408
  431. return
  432. }
  433. var req models.EnPermissionMoveReq
  434. if e := json.Unmarshal(this.Ctx.Input.RequestBody, &req); e != nil {
  435. br.Msg = "参数解析异常!"
  436. br.ErrMsg = "参数解析失败,Err:" + e.Error()
  437. return
  438. }
  439. if req.PermissionId <= 0 {
  440. br.Msg = "参数有误"
  441. return
  442. }
  443. e, msg := services.MoveEnPermission(req)
  444. if e != nil {
  445. br.Msg = msg
  446. br.ErrMsg = "移动品种失败, Err: " + e.Error()
  447. return
  448. }
  449. br.Ret = 200
  450. br.Success = true
  451. br.Msg = "操作成功"
  452. }
  453. // SetEnabled
  454. // @Title 启用/禁用品种
  455. // @Description 启用/禁用品种
  456. // @Param request body models.EnPermissionEnabledReq true "type json string"
  457. // @Success 200 Ret=200 操作成功
  458. // @router /enabled/set [post]
  459. func (this *EnPermissionController) SetEnabled() {
  460. br := new(models.BaseResponse).Init()
  461. defer func() {
  462. if br.ErrMsg == "" {
  463. br.IsSendEmail = false
  464. }
  465. this.Data["json"] = br
  466. this.ServeJSON()
  467. }()
  468. sysUser := this.SysUser
  469. if sysUser == nil {
  470. br.Msg = "请登录"
  471. br.ErrMsg = "请登录,SysUser Is Empty"
  472. br.Ret = 408
  473. return
  474. }
  475. var req models.EnPermissionEnabledReq
  476. if e := json.Unmarshal(this.Ctx.Input.RequestBody, &req); e != nil {
  477. br.Msg = "参数解析异常!"
  478. br.ErrMsg = "参数解析失败,Err:" + e.Error()
  479. return
  480. }
  481. if req.PermissionId <= 0 {
  482. br.Msg = "参数有误"
  483. return
  484. }
  485. // 重名校验
  486. ob := new(models.EnPermission)
  487. item, e := ob.GetItemById(req.PermissionId)
  488. if e != nil {
  489. if e.Error() == utils.ErrNoRow() {
  490. br.Msg = "品种不存在, 请刷新页面"
  491. return
  492. }
  493. br.Msg = "操作失败"
  494. br.ErrMsg = "获取品种权限失败, Err: " + e.Error()
  495. return
  496. }
  497. item.Enabled = req.Enabled
  498. item.ModifyTime = time.Now().Local()
  499. if e = item.Update([]string{"Enabled", "ModifyTime"}); e != nil {
  500. br.Msg = "操作失败"
  501. br.ErrMsg = "更新品种权限失败, Err: " + e.Error()
  502. return
  503. }
  504. // 如果是一级品种被启用,则所有二级设置成启用, 如果一级品种被禁用,则所有二级设置成禁用
  505. if item.ParentId == 0 {
  506. if e = item.SetEnabled(item.EnPermissionId, req.Enabled); e != nil {
  507. br.Msg = "操作失败"
  508. br.ErrMsg = "更新英文分类权限失败, Err: " + e.Error()
  509. return
  510. }
  511. } else if item.ParentId > 0 && req.Enabled == 1 {
  512. // 如果二级品种被启用,则他的上级品种设置成启用
  513. if e = item.SetEnabledByPermissionId(item.ParentId, req.Enabled); e != nil {
  514. br.Msg = "操作失败"
  515. br.ErrMsg = "更新英文分类权限失败, Err: " + e.Error()
  516. return
  517. }
  518. }
  519. br.Ret = 200
  520. br.Success = true
  521. br.Msg = "操作成功"
  522. br.Data = item.EnPermissionId
  523. }