en_permission.go 11 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420
  1. package english_report
  2. import (
  3. "encoding/json"
  4. "fmt"
  5. "eta/eta_api/controllers"
  6. "eta/eta_api/models"
  7. "eta/eta_api/utils"
  8. "strings"
  9. "time"
  10. )
  11. // EnPermissionController 英文权限
  12. type EnPermissionController struct {
  13. controllers.BaseAuthController
  14. }
  15. // Add
  16. // @Title 新增品种权限
  17. // @Description 新增品种权限
  18. // @Param request body models.EnPermissionAddReq true "type json string"
  19. // @Success 200 Ret=200 操作成功
  20. // @router /add [post]
  21. func (this *EnPermissionController) Add() {
  22. br := new(models.BaseResponse).Init()
  23. defer func() {
  24. if br.ErrMsg == "" {
  25. br.IsSendEmail = false
  26. }
  27. this.Data["json"] = br
  28. this.ServeJSON()
  29. }()
  30. sysUser := this.SysUser
  31. if sysUser == nil {
  32. br.Msg = "请登录"
  33. br.ErrMsg = "请登录,SysUser Is Empty"
  34. br.Ret = 408
  35. return
  36. }
  37. var req models.EnPermissionAddReq
  38. if e := json.Unmarshal(this.Ctx.Input.RequestBody, &req); e != nil {
  39. br.Msg = "参数解析异常!"
  40. br.ErrMsg = "参数解析失败,Err:" + e.Error()
  41. return
  42. }
  43. req.EnPermissionName = strings.TrimSpace(req.EnPermissionName)
  44. if req.EnPermissionName == "" {
  45. br.Msg = "请输入品种名称"
  46. return
  47. }
  48. // 重名校验
  49. existOB := new(models.EnPermission)
  50. existCond := fmt.Sprintf(` AND %s = ? AND %s = ?`, models.EnPermissionColumns.EnPermissionName, models.EnPermissionColumns.ParentId)
  51. existPars := make([]interface{}, 0)
  52. existPars = append(existPars, req.EnPermissionName, req.ParentId)
  53. exist, e := existOB.GetItemByCondition(existCond, existPars)
  54. if e != nil && e.Error() != utils.ErrNoRow() {
  55. br.Msg = "操作失败"
  56. br.ErrMsg = "获取重名品种权限失败, Err: " + e.Error()
  57. return
  58. }
  59. if exist != nil && exist.EnPermissionId > 0 {
  60. br.Msg = "品种名称已存在"
  61. return
  62. }
  63. // 新增
  64. item := new(models.EnPermission)
  65. item.EnPermissionName = req.EnPermissionName
  66. item.ParentId = req.ParentId
  67. item.Sort = req.Sort
  68. item.CreateTime = time.Now().Local()
  69. item.ModifyTime = time.Now().Local()
  70. if e = item.Create(); e != nil {
  71. br.Msg = "操作失败"
  72. br.ErrMsg = "新增品种权限失败, Err: " + e.Error()
  73. return
  74. }
  75. br.Ret = 200
  76. br.Success = true
  77. br.Msg = "操作成功"
  78. br.Data = item.EnPermissionId
  79. }
  80. // Edit
  81. // @Title 编辑品种权限
  82. // @Description 编辑品种权限
  83. // @Param request body models.EnPermissionAddReq true "type json string"
  84. // @Success 200 Ret=200 操作成功
  85. // @router /edit [post]
  86. func (this *EnPermissionController) Edit() {
  87. br := new(models.BaseResponse).Init()
  88. defer func() {
  89. if br.ErrMsg == "" {
  90. br.IsSendEmail = false
  91. }
  92. this.Data["json"] = br
  93. this.ServeJSON()
  94. }()
  95. sysUser := this.SysUser
  96. if sysUser == nil {
  97. br.Msg = "请登录"
  98. br.ErrMsg = "请登录,SysUser Is Empty"
  99. br.Ret = 408
  100. return
  101. }
  102. var req models.EnPermissionEditReq
  103. if e := json.Unmarshal(this.Ctx.Input.RequestBody, &req); e != nil {
  104. br.Msg = "参数解析异常!"
  105. br.ErrMsg = "参数解析失败,Err:" + e.Error()
  106. return
  107. }
  108. if req.EnPermissionId <= 0 {
  109. br.Msg = "参数有误"
  110. return
  111. }
  112. req.EnPermissionName = strings.TrimSpace(req.EnPermissionName)
  113. if req.EnPermissionName == "" {
  114. br.Msg = "请输入品种名称"
  115. return
  116. }
  117. // 重名校验
  118. ob := new(models.EnPermission)
  119. existCond := fmt.Sprintf(` AND %s = ? AND %s = ? AND %s <> ?`, models.EnPermissionColumns.EnPermissionName, models.EnPermissionColumns.ParentId, models.EnPermissionColumns.EnPermissionId)
  120. existPars := make([]interface{}, 0)
  121. existPars = append(existPars, req.EnPermissionName, req.ParentId, req.EnPermissionId)
  122. exist, e := ob.GetItemByCondition(existCond, existPars)
  123. if e != nil && e.Error() != utils.ErrNoRow() {
  124. br.Msg = "操作失败"
  125. br.ErrMsg = "获取重名品种权限失败, Err: " + e.Error()
  126. return
  127. }
  128. if exist != nil && exist.EnPermissionId > 0 {
  129. br.Msg = "品种名称已存在"
  130. return
  131. }
  132. item, e := ob.GetItemById(req.EnPermissionId)
  133. if e != nil {
  134. if e.Error() == utils.ErrNoRow() {
  135. br.Msg = "品种不存在, 请刷新页面"
  136. return
  137. }
  138. br.Msg = "操作失败"
  139. br.ErrMsg = "获取品种权限失败, Err: " + e.Error()
  140. return
  141. }
  142. originPid := item.ParentId
  143. item.EnPermissionName = req.EnPermissionName
  144. item.ParentId = req.ParentId
  145. item.Sort = req.Sort
  146. item.ModifyTime = time.Now().Local()
  147. if e = item.Update([]string{"EnPermissionName", "ParentId", "Sort", "ModifyTime"}); e != nil {
  148. br.Msg = "操作失败"
  149. br.ErrMsg = "更新品种权限失败, Err: " + e.Error()
  150. return
  151. }
  152. // 若二级分类转为一级分类, 则清空对应客户、分类权限
  153. if originPid > 0 && req.ParentId == 0 {
  154. if e = models.ClearEnPermissionsByPermissionId(item.EnPermissionId); e != nil {
  155. br.Msg = "操作失败"
  156. br.ErrMsg = "清空英文分类权限失败, Err: " + e.Error()
  157. return
  158. }
  159. }
  160. br.Ret = 200
  161. br.Success = true
  162. br.Msg = "操作成功"
  163. br.Data = item.EnPermissionId
  164. }
  165. // ParentList
  166. // @Title 父级品种列表
  167. // @Description 父级品种列表
  168. // @Success 200 Ret=200 操作成功
  169. // @router /parent/list [get]
  170. func (this *EnPermissionController) ParentList() {
  171. br := new(models.BaseResponse).Init()
  172. defer func() {
  173. if br.ErrMsg == "" {
  174. br.IsSendEmail = false
  175. }
  176. this.Data["json"] = br
  177. this.ServeJSON()
  178. }()
  179. sysUser := this.SysUser
  180. if sysUser == nil {
  181. br.Msg = "请登录"
  182. br.ErrMsg = "请登录,SysUser Is Empty"
  183. br.Ret = 408
  184. return
  185. }
  186. cond := fmt.Sprintf(` AND %s = ?`, models.EnPermissionColumns.ParentId)
  187. pars := make([]interface{}, 0)
  188. pars = append(pars, 0)
  189. ob := new(models.EnPermission)
  190. list, e := ob.GetItemsByCondition(cond, pars, []string{}, "sort ASC, create_time ASC")
  191. if e != nil {
  192. br.Msg = "获取失败"
  193. br.ErrMsg = "获取父级品种列表失败, Err: " + e.Error()
  194. return
  195. }
  196. resp := make([]*models.EnPermissionItem, 0)
  197. for _, v := range list {
  198. t := new(models.EnPermissionItem)
  199. t.EnPermissionId = v.EnPermissionId
  200. t.EnPermissionName = v.EnPermissionName
  201. t.CnPermissionName = v.CnPermissionName
  202. t.ParentId = v.ParentId
  203. t.Sort = v.Sort
  204. t.CreateTime = v.CreateTime.Format(utils.FormatDateTime)
  205. t.Child = make([]*models.EnPermissionItem, 0)
  206. resp = append(resp, t)
  207. }
  208. br.Ret = 200
  209. br.Success = true
  210. br.Msg = "获取成功"
  211. br.Data = resp
  212. }
  213. // List
  214. // @Title 品种列表
  215. // @Description 品种列表
  216. // @Param Keyword query string false "关键词"
  217. // @Param ReportId query int false "报告ID-用于邮件推送时过滤掉无权限的品种"
  218. // @Param VideoId query int false "路演视频ID-用于邮件推送时过滤掉无权限的品种"
  219. // @Success 200 Ret=200 操作成功
  220. // @router /list [get]
  221. func (this *EnPermissionController) List() {
  222. br := new(models.BaseResponse).Init()
  223. defer func() {
  224. if br.ErrMsg == "" {
  225. br.IsSendEmail = false
  226. }
  227. this.Data["json"] = br
  228. this.ServeJSON()
  229. }()
  230. sysUser := this.SysUser
  231. if sysUser == nil {
  232. br.Msg = "请登录"
  233. br.ErrMsg = "请登录,SysUser Is Empty"
  234. br.Ret = 408
  235. return
  236. }
  237. keyword := this.GetString("Keyword", "")
  238. keyword = strings.TrimSpace(keyword)
  239. // 禁用指定报告、指定路演无权限的品种
  240. reportId, _ := this.GetInt("ReportId", 0)
  241. videoId, _ := this.GetInt("VideoId", 0)
  242. limitIds := make([]int, 0)
  243. if reportId > 0 {
  244. ps, e := models.GetEnPermissionIdsByEnglishReportId(reportId)
  245. if e != nil {
  246. br.Msg = "获取失败"
  247. br.ErrMsg = "获取报告权限品种失败, Err: " + e.Error()
  248. return
  249. }
  250. limitIds = ps
  251. }
  252. if videoId > 0 {
  253. ps, e := models.GetEnPermissionIdsByEnglishVideoId(videoId)
  254. if e != nil {
  255. br.Msg = "获取失败"
  256. br.ErrMsg = "获取视频权限品种失败, Err: " + e.Error()
  257. return
  258. }
  259. limitIds = ps
  260. }
  261. cond := ``
  262. pars := make([]interface{}, 0)
  263. if keyword != "" {
  264. k := fmt.Sprint("%", keyword, "%")
  265. cond += fmt.Sprintf(` AND %s LIKE ?`, models.EnPermissionColumns.EnPermissionName)
  266. pars = append(pars, k)
  267. }
  268. list, e := models.GetEnPermissionUnionList(cond, pars)
  269. if e != nil {
  270. br.Msg = "获取失败"
  271. br.ErrMsg = "获取品种Union列表失败, Err: " + e.Error()
  272. return
  273. }
  274. // 品种树
  275. resp := make([]*models.EnPermissionItem, 0)
  276. childMap := make(map[int][]*models.EnPermissionItem)
  277. for _, v := range list {
  278. t := new(models.EnPermissionItem)
  279. t.EnPermissionId = v.EnPermissionId
  280. t.EnPermissionName = v.EnPermissionName
  281. t.CnPermissionName = v.CnPermissionName
  282. t.ParentId = v.ParentId
  283. t.Sort = v.Sort
  284. t.CreateTime = v.CreateTime.Format(utils.FormatDateTime)
  285. t.Child = make([]*models.EnPermissionItem, 0)
  286. if v.ParentId == 0 {
  287. resp = append(resp, t)
  288. continue
  289. }
  290. if v.ParentId > 0 {
  291. if childMap[v.ParentId] == nil {
  292. childMap[v.ParentId] = make([]*models.EnPermissionItem, 0)
  293. }
  294. // 无权限则隐藏
  295. if (reportId > 0 || videoId > 0) && !utils.InArrayByInt(limitIds, t.EnPermissionId) {
  296. continue
  297. }
  298. childMap[v.ParentId] = append(childMap[v.ParentId], t)
  299. }
  300. }
  301. for _, r := range resp {
  302. r.Child = childMap[r.EnPermissionId]
  303. }
  304. br.Ret = 200
  305. br.Success = true
  306. br.Msg = "获取成功"
  307. br.Data = resp
  308. }
  309. // Remove
  310. // @Title 删除品种权限
  311. // @Description 删除品种权限
  312. // @Param request body models.EnPermissionRemoveReq true "type json string"
  313. // @Success 200 Ret=200 操作成功
  314. // @router /remove [post]
  315. func (this *EnPermissionController) Remove() {
  316. br := new(models.BaseResponse).Init()
  317. defer func() {
  318. if br.ErrMsg == "" {
  319. br.IsSendEmail = false
  320. }
  321. this.Data["json"] = br
  322. this.ServeJSON()
  323. }()
  324. sysUser := this.SysUser
  325. if sysUser == nil {
  326. br.Msg = "请登录"
  327. br.ErrMsg = "请登录,SysUser Is Empty"
  328. br.Ret = 408
  329. return
  330. }
  331. var req models.EnPermissionRemoveReq
  332. if e := json.Unmarshal(this.Ctx.Input.RequestBody, &req); e != nil {
  333. br.Msg = "参数解析异常!"
  334. br.ErrMsg = "参数解析失败,Err:" + e.Error()
  335. return
  336. }
  337. if req.EnPermissionId <= 0 {
  338. br.Msg = "参数有误"
  339. return
  340. }
  341. ob := new(models.EnPermission)
  342. item, e := ob.GetItemById(req.EnPermissionId)
  343. if e != nil {
  344. if e.Error() == utils.ErrNoRow() {
  345. br.Msg = "品种不存在, 请刷新页面"
  346. return
  347. }
  348. br.Msg = "操作失败"
  349. br.ErrMsg = "获取品种权限失败, Err: " + e.Error()
  350. return
  351. }
  352. // 校验是否有子品种
  353. childCond := fmt.Sprintf(` AND %s = ?`, models.EnPermissionColumns.ParentId)
  354. childPars := make([]interface{}, 0)
  355. childPars = append(childPars, req.EnPermissionId)
  356. num, e := ob.GetCountByCondition(childCond, childPars)
  357. if e != nil {
  358. br.Msg = "操作失败"
  359. br.ErrMsg = "获取品种子权限数量失败, Err: " + e.Error()
  360. return
  361. }
  362. if num > 0 {
  363. br.Msg = "请先删除该品种下关联品种"
  364. return
  365. }
  366. // 校验是否关联客户
  367. {
  368. cond := fmt.Sprintf(` AND %s = ?`, models.EnCompanyPermissionColumns.EnPermissionId)
  369. pars := make([]interface{}, 0)
  370. pars = append(pars, item.EnPermissionId)
  371. ob := new(models.EnCompanyPermission)
  372. num, e := ob.GetCountByCondition(cond, pars)
  373. if e != nil {
  374. br.Msg = "操作失败"
  375. br.ErrMsg = "获取品种关联客户数量失败, Err: " + e.Error()
  376. return
  377. }
  378. if num > 0 {
  379. br.Msg = "该品种有关联客户, 不允许删除"
  380. return
  381. }
  382. }
  383. // 删除品种
  384. if e = item.Del(); e != nil {
  385. br.Msg = "操作失败"
  386. br.ErrMsg = "删除品种失败, Err: " + e.Error()
  387. return
  388. }
  389. br.Ret = 200
  390. br.Success = true
  391. br.Msg = "操作成功"
  392. }