english_classify.go 13 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481
  1. package english_report
  2. import (
  3. "encoding/json"
  4. "fmt"
  5. "github.com/rdlucklib/rdluck_tools/paging"
  6. "hongze/hz_eta_api/controllers"
  7. "hongze/hz_eta_api/models"
  8. "hongze/hz_eta_api/services"
  9. "hongze/hz_eta_api/utils"
  10. "sort"
  11. "time"
  12. )
  13. // EnglishClassifyController 英文分类
  14. type EnglishClassifyController struct {
  15. controllers.BaseAuthController
  16. }
  17. // ListClassify
  18. // @Title 获取分类列表
  19. // @Description 获取分类列表
  20. // @Param PageSize query int true "每页数据条数"
  21. // @Param CurrentIndex query int true "当前页页码,从1开始"
  22. // @Param KeyWord query string true "检索关键词"
  23. // @Param CompanyType query string false "产品类型,枚举值:'ficc','权益';不传默认返回全部"
  24. // @Success 200 {object} models.EnglishClassifyListResp
  25. // @router /classify/list [get]
  26. func (this *EnglishReportController) ListClassify() {
  27. br := new(models.BaseResponse).Init()
  28. defer func() {
  29. this.Data["json"] = br
  30. this.ServeJSON()
  31. }()
  32. pageSize, _ := this.GetInt("PageSize")
  33. currentIndex, _ := this.GetInt("CurrentIndex")
  34. keyWord := this.GetString("KeyWord")
  35. classifyType, _ := this.GetInt("ClassifyType", 0)
  36. var startSize int
  37. if pageSize <= 0 {
  38. pageSize = utils.PageSize20
  39. }
  40. if currentIndex <= 0 {
  41. currentIndex = 1
  42. }
  43. startSize = utils.StartIndex(currentIndex, pageSize)
  44. page := paging.GetPaging(currentIndex, pageSize, 0)
  45. resp := new(models.EnglishClassifyListResp)
  46. list, err := models.GetEnglishClassifyList(startSize, pageSize, keyWord, classifyType)
  47. if err != nil {
  48. br.Msg = "获取失败"
  49. br.ErrMsg = "获取失败,Err:" + err.Error()
  50. return
  51. }
  52. total, err := models.GetEnglishClassifyListCount(keyWord, classifyType)
  53. if err != nil {
  54. br.Msg = "获取失败"
  55. br.ErrMsg = "获取失败,Err:" + err.Error()
  56. return
  57. }
  58. if total == 0 {
  59. resp.List = make([]*models.EnglishClassifyList, 0)
  60. resp.Paging = page
  61. br.Data = resp
  62. br.Ret = 200
  63. br.Success = true
  64. br.Msg = "获取成功"
  65. return
  66. }
  67. var parentIds []int
  68. for _, v := range list {
  69. parentIds = append(parentIds, v.Id)
  70. }
  71. childMap := make(map[int][]*models.EnglishClassifyList)
  72. tmpList, err := models.GetEnglishClassifyChildByParentIds(parentIds, keyWord, classifyType)
  73. if err != nil {
  74. br.Msg = "获取二级分类失败"
  75. br.ErrMsg = "获取二级分类失败,Err:" + err.Error()
  76. return
  77. }
  78. // 二级分类-品种权限
  79. permissionMap := make(map[int][]int)
  80. {
  81. classifyIds := make([]int, 0)
  82. for _, v := range tmpList {
  83. classifyIds = append(classifyIds, v.Id)
  84. }
  85. if len(classifyIds) > 0 {
  86. cond := fmt.Sprintf(` AND %s IN (%s)`, models.EnClassifyPermissionColumns.EnClassifyId, utils.GetOrmInReplace(len(classifyIds)))
  87. pars := make([]interface{}, 0)
  88. pars = append(pars, classifyIds)
  89. ob := new(models.EnClassifyPermission)
  90. items, e := ob.GetItemsByCondition(cond, pars, []string{}, "")
  91. if e != nil {
  92. br.Msg = "获取失败"
  93. br.ErrMsg = "获取客户权限列表失败, Err: " + e.Error()
  94. return
  95. }
  96. for _, v := range items {
  97. if permissionMap[v.EnClassifyId] == nil {
  98. permissionMap[v.EnClassifyId] = make([]int, 0)
  99. }
  100. permissionMap[v.EnClassifyId] = append(permissionMap[v.EnClassifyId], v.EnPermissionId)
  101. }
  102. }
  103. }
  104. for _, v := range tmpList {
  105. v.EnPermissions = permissionMap[v.Id]
  106. childMap[v.ParentId] = append(childMap[v.ParentId], v)
  107. }
  108. for _, v := range list {
  109. if child, ok := childMap[v.Id]; ok {
  110. v.Child = child
  111. }
  112. }
  113. var sortList models.RSClassifyList
  114. sortList = list
  115. sort.Sort(sortList)
  116. for _, item := range sortList {
  117. var sortChildList models.RSChildClassifyList
  118. sortChildList = item.Child
  119. sort.Sort(sortChildList)
  120. item.Child = sortChildList
  121. }
  122. resp.List = sortList
  123. resp.Paging = page
  124. br.Data = resp
  125. br.Ret = 200
  126. br.Success = true
  127. br.Msg = "获取成功"
  128. }
  129. // @Title 新增分类
  130. // @Description 新增分类接口
  131. // @Param ClassifyName int true "分类名称"
  132. // @Param ParentId query int true "父级Id 添加父级时为0"
  133. // @Param Sort query string false "排序"
  134. // @Success 200 新增成功
  135. // @router /classify/add [get]
  136. func (this *EnglishReportController) AddClassify() {
  137. br := new(models.BaseResponse).Init()
  138. defer func() {
  139. this.Data["json"] = br
  140. this.ServeJSON()
  141. }()
  142. classifyName := this.GetString("ClassifyName")
  143. parentId, _ := this.GetInt("ParentId")
  144. classifyType, _ := this.GetInt("ClassifyType", 0)
  145. sort, _ := this.GetInt("Sort")
  146. item := &models.EnglishClassify{
  147. ClassifyName: classifyName,
  148. Sort: sort,
  149. ParentId: parentId,
  150. CreateTime: time.Now(),
  151. ModifyTime: time.Now(),
  152. ClassifyType: classifyType,
  153. IsShow: 1,
  154. }
  155. counts, err := models.GetEnglishClassifyCountsByName(classifyName, parentId, classifyType)
  156. if err != nil {
  157. br.Msg = "查询失败"
  158. br.ErrMsg = "查询失败,Err:" + err.Error()
  159. return
  160. }
  161. if counts > 0 {
  162. br.Msg = "该名称已存在"
  163. br.ErrMsg = "该名称已存在!"
  164. return
  165. }
  166. _, err = models.AddEnglishClassify(item)
  167. if err != nil {
  168. br.Msg = "保存失败"
  169. br.ErrMsg = "保存失败,Err:" + err.Error()
  170. return
  171. }
  172. br.Ret = 200
  173. br.Success = true
  174. br.Msg = "新增成功"
  175. }
  176. // @Title 编辑分类
  177. // @Description 编辑分类接口
  178. // @Param ClassifyId int true "分类Id"
  179. // @Param ClassifyName string true "分类名称"
  180. // @Param ParentId query int true "父级Id 添加父级时为0"
  181. // @Param Sort query string false "排序"
  182. // @Success 200 保存成功
  183. // @router /classify/edit [get]
  184. func (this *EnglishReportController) EditClassify() {
  185. br := new(models.BaseResponse).Init()
  186. defer func() {
  187. this.Data["json"] = br
  188. this.ServeJSON()
  189. }()
  190. classifyId, _ := this.GetInt("ClassifyId")
  191. classifyName := this.GetString("ClassifyName")
  192. parentId, _ := this.GetInt("ParentId")
  193. sort, _ := this.GetInt("Sort")
  194. if parentId == classifyId {
  195. br.Msg = "上级分类不能选择自己"
  196. return
  197. }
  198. if classifyId <= 0 {
  199. br.Msg = "参数错误"
  200. return
  201. }
  202. if classifyName == "" {
  203. br.Msg = "分类名称不可为空"
  204. return
  205. }
  206. oldItem, err := models.GetEnglishReportClassifyById(classifyId)
  207. if err != nil && err.Error() != utils.ErrNoRow() {
  208. br.Msg = "获取信息失败"
  209. br.ErrMsg = "获取信息失败,Err:" + err.Error()
  210. return
  211. }
  212. parent, err := models.GetEnglishReportClassifyById(parentId)
  213. if err != nil && err.Error() != utils.ErrNoRow() {
  214. br.Msg = "获取信息失败"
  215. br.ErrMsg = "获取信息失败,Err:" + err.Error()
  216. return
  217. }
  218. if oldItem.ParentId == 0 && parentId > 0 {
  219. // 一级分类变更为二级分类,禁止
  220. br.Msg = "一级分类不允许更改为二级分类"
  221. return
  222. }
  223. //判断是否已存在同个名称的分类
  224. existItem, e := models.GetEnglishClassifyByClassifyNameParentId(classifyName, parentId, oldItem.ClassifyType)
  225. if e != nil {
  226. if e.Error() != utils.ErrNoRow() {
  227. br.Msg = "查询已存在的分类出错"
  228. br.ErrMsg = "查询已存在的分类出错, Err:" + e.Error()
  229. return
  230. }
  231. }
  232. if e == nil && existItem.Id != oldItem.Id {
  233. br.Msg = "分类名称:" + classifyName + "已存在"
  234. return
  235. }
  236. if err != nil && err.Error() != utils.ErrNoRow() {
  237. br.Msg = "获取信息失败"
  238. br.ErrMsg = "获取信息失败,Err:" + err.Error()
  239. return
  240. }
  241. //oldParentId := item.ParentId
  242. item := &models.EnglishClassify{
  243. Id: oldItem.Id,
  244. ClassifyName: classifyName,
  245. Sort: sort,
  246. ParentId: parentId,
  247. ModifyTime: time.Now(),
  248. ClassifyType: oldItem.ClassifyType,
  249. IsShow: 1,
  250. }
  251. if oldItem.ClassifyType == 1 {
  252. err = services.UpdateEnglishVideoClassifyId(oldItem, item, parent, classifyId)
  253. if err != nil {
  254. br.Msg = err.Error()
  255. return
  256. }
  257. } else {
  258. // 更新研报里的分类名称
  259. err = services.UpdateEnglishReportClassifyId(oldItem, item, parent, classifyId)
  260. if err != nil {
  261. br.Msg = err.Error()
  262. return
  263. }
  264. }
  265. err = models.ModifyEnglishClassify(item)
  266. if err != nil {
  267. br.Msg = "保存失败"
  268. br.ErrMsg = "保存失败,Err:" + err.Error()
  269. return
  270. }
  271. br.Ret = 200
  272. br.Success = true
  273. br.Msg = "保存成功"
  274. }
  275. // @Title 删除分类列表
  276. // @Description 删除分类列表
  277. // @Param ClassifyId int true "分类名称"
  278. // @Param ParentId query int true "父级Id"
  279. // @Success 200 保存成功
  280. // @router /classify/delete [get]
  281. func (this *EnglishReportController) DelClassify() {
  282. br := new(models.BaseResponse).Init()
  283. defer func() {
  284. this.Data["json"] = br
  285. this.ServeJSON()
  286. }()
  287. classifyId, _ := this.GetInt("ClassifyId")
  288. //parentId, _ := this.GetInt("ParentId")
  289. classifyInfo, err := models.GetEnglishReportClassifyById(classifyId)
  290. if err != nil {
  291. if err.Error() == utils.ErrNoRow() {
  292. br.Msg = "当前分类不存在"
  293. br.ErrMsg = "当前分类不存在"
  294. return
  295. }
  296. br.Msg = "获取分类信息失败"
  297. br.ErrMsg = "获取分类信息失败,Err:" + err.Error()
  298. return
  299. }
  300. count, err := models.GetEnglishClassifyChildCounts(classifyId)
  301. if err != nil && err.Error() != utils.ErrNoRow() {
  302. br.Msg = "获取信息失败"
  303. br.ErrMsg = "获取失败,Err:" + err.Error()
  304. return
  305. }
  306. if count > 0 {
  307. br.Msg = "请先删除该分类下关联分类"
  308. br.Ret = 403
  309. return
  310. }
  311. if classifyInfo.ClassifyType == 0 {
  312. reportCount, e := models.GetEnglishReportCounts(classifyId, classifyInfo.ParentId)
  313. if e != nil && e.Error() != utils.ErrNoRow() {
  314. br.Msg = "获取信息失败"
  315. br.ErrMsg = "获取失败,Err:" + e.Error()
  316. return
  317. }
  318. if reportCount > 0 {
  319. br.Msg = "该分类有关联报告,不允许删除"
  320. br.Ret = 403
  321. return
  322. }
  323. } else {
  324. videoCount, e := models.GetEnglishVideoCounts(classifyId, classifyInfo.ParentId)
  325. if e != nil && e.Error() != utils.ErrNoRow() {
  326. br.Msg = "获取信息失败"
  327. br.ErrMsg = "获取失败,Err:" + e.Error()
  328. return
  329. }
  330. if videoCount > 0 {
  331. br.Msg = "该分类有关联的路演视频,不允许删除"
  332. br.Ret = 403
  333. return
  334. }
  335. }
  336. if err = models.DeleteEnglishClassify(classifyId); err != nil {
  337. br.Msg = "删除失败"
  338. br.ErrMsg = "删除报告失败, Err: " + err.Error()
  339. return
  340. }
  341. br.Ret = 200
  342. br.Success = true
  343. br.Msg = "删除成功"
  344. }
  345. // @Title 获取一级分类列表
  346. // @Description 获取一级分类列表
  347. // @Param PageSize query int true "每页数据条数"
  348. // @Param CurrentIndex query int true "当前页页码,从1开始"
  349. // @Param CompanyType query string false "产品类型,枚举值:'ficc','权益';不传默认返回全部"
  350. // @Success 200 {object} models.EnglishClassifyListResp
  351. // @router /classify/first/list [get]
  352. func (this *EnglishReportController) FistListClassify() {
  353. br := new(models.BaseResponse).Init()
  354. defer func() {
  355. this.Data["json"] = br
  356. this.ServeJSON()
  357. }()
  358. pageSize, _ := this.GetInt("PageSize")
  359. currentIndex, _ := this.GetInt("CurrentIndex")
  360. classifyType, _ := this.GetInt("ClassifyType", 0)
  361. var startSize int
  362. if pageSize <= 0 {
  363. pageSize = utils.PageSize20
  364. }
  365. if currentIndex <= 0 {
  366. currentIndex = 1
  367. }
  368. startSize = utils.StartIndex(currentIndex, pageSize)
  369. list, err := models.GetEnglishFirstClassifyList(classifyType, startSize, pageSize)
  370. if err != nil {
  371. br.Msg = "获取失败"
  372. br.ErrMsg = "获取失败,Err:" + err.Error()
  373. return
  374. }
  375. total, err := models.GetEnglishFirstClassifyListCount(classifyType)
  376. if err != nil {
  377. br.Msg = "获取失败"
  378. br.ErrMsg = "获取失败,Err:" + err.Error()
  379. return
  380. }
  381. page := paging.GetPaging(currentIndex, pageSize, total)
  382. resp := new(models.EnglishClassifyListResp)
  383. resp.List = list
  384. resp.Paging = page
  385. br.Data = resp
  386. br.Ret = 200
  387. br.Success = true
  388. br.Msg = "获取成功"
  389. }
  390. // PermissionEdit
  391. // @Title 编辑品种权限
  392. // @Description 编辑品种权限
  393. // @Param request body models.EnPermissionAddReq true "type json string"
  394. // @Success 200 Ret=200 操作成功
  395. // @router /permission/edit [post]
  396. func (this *EnglishClassifyController) PermissionEdit() {
  397. br := new(models.BaseResponse).Init()
  398. defer func() {
  399. if br.ErrMsg == "" {
  400. br.IsSendEmail = false
  401. }
  402. this.Data["json"] = br
  403. this.ServeJSON()
  404. }()
  405. sysUser := this.SysUser
  406. if sysUser == nil {
  407. br.Msg = "请登录"
  408. br.ErrMsg = "请登录,SysUser Is Empty"
  409. br.Ret = 408
  410. return
  411. }
  412. var req models.EnClassifyPermissionEditReq
  413. if e := json.Unmarshal(this.Ctx.Input.RequestBody, &req); e != nil {
  414. br.Msg = "参数解析异常!"
  415. br.ErrMsg = "参数解析失败,Err:" + e.Error()
  416. return
  417. }
  418. if req.ClassifyId <= 0 {
  419. br.Msg = "参数有误"
  420. return
  421. }
  422. // 更新品种权限
  423. nowTime := time.Now().Local()
  424. permissions := make([]*models.EnClassifyPermission, 0)
  425. for _, p := range req.EnPermissions {
  426. v := new(models.EnClassifyPermission)
  427. v.EnPermissionId = p
  428. v.EnClassifyId = req.ClassifyId
  429. v.CreateTime = nowTime
  430. permissions = append(permissions, v)
  431. }
  432. if e := models.ClearAndCreateEnClassifyPermissions(req.ClassifyId, permissions); e != nil {
  433. br.Msg = "保存失败"
  434. br.ErrMsg = "更新分类品种权限失败, Err: " + e.Error()
  435. return
  436. }
  437. br.Ret = 200
  438. br.Success = true
  439. br.Msg = "操作成功"
  440. }