classify.go 26 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973974975976977978979980981982983984985986987988989990991992
  1. package controllers
  2. import (
  3. "encoding/json"
  4. "eta_gn/eta_api/models"
  5. "eta_gn/eta_api/services"
  6. "eta_gn/eta_api/utils"
  7. "fmt"
  8. "strconv"
  9. "strings"
  10. "time"
  11. )
  12. // 分类
  13. type ClassifyController struct {
  14. BaseAuthController
  15. }
  16. // Add
  17. // @Title 新增分类接口
  18. // @Description 新增分类
  19. // @Param request body models.ClassifyAddReq true "type json string"
  20. // @Success 200 新增成功
  21. // @router /add [post]
  22. func (this *ClassifyController) 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. var req models.ClassifyAddReq
  32. err := json.Unmarshal(this.Ctx.Input.RequestBody, &req)
  33. if err != nil {
  34. br.Msg = "参数解析异常!"
  35. br.ErrMsg = "参数解析失败,Err:" + err.Error()
  36. return
  37. }
  38. // 获取系统菜单, 如果没有对应的字段的特殊处理项, 则忽略必填
  39. /*menus, e := system.GetSysMenuItemsByCondition(` AND hidden = 0`, make([]interface{}, 0), []string{}, ``)
  40. if e != nil {
  41. br.Msg = "保存失败"
  42. br.ErrMsg = "获取菜单列表失败, Err: " + e.Error()
  43. return
  44. }
  45. menuMap := make(map[string]bool)
  46. for _, m := range menus {
  47. if m.ButtonCode != "" {
  48. menuMap[m.ButtonCode] = true
  49. }
  50. }*/
  51. if req.ClassifyName == "" {
  52. br.Msg = "分类名称不可为空"
  53. return
  54. }
  55. if req.ClassifyType != utils.ReportTypeDefault && req.ClassifyType != utils.ReportTypePPT {
  56. br.Msg = "分类类型有误"
  57. br.ErrMsg = fmt.Sprintf("分类类型有误, %d", req.ClassifyType)
  58. return
  59. }
  60. if req.IsRemind == 1 {
  61. if req.RemindTime == "" {
  62. br.Msg = "请选择报告提醒时间"
  63. return
  64. }
  65. _, e := time.Parse("15:04", req.RemindTime)
  66. if e != nil {
  67. br.Msg = "提醒时间格式有误"
  68. br.ErrMsg = fmt.Sprintf("提醒时间格式有误, %s", req.RemindTime)
  69. return
  70. }
  71. }
  72. // 新增分类
  73. err, errMsg, isSentEmail := services.AddReportClassify(req.ClassifyName, req.ParentId, req.ClassifyType, req.IsRemind, req.RemindTime)
  74. if err != nil {
  75. br.Msg = "添加失败"
  76. if errMsg != "" {
  77. br.Msg = errMsg
  78. }
  79. br.ErrMsg = "添加失败,Err:" + err.Error()
  80. br.IsSendEmail = isSentEmail
  81. return
  82. }
  83. br.Ret = 200
  84. br.Success = true
  85. br.Msg = "新增成功"
  86. }
  87. // CheckDeleteClassify
  88. // @Title 删除分类-检测接口
  89. // @Description 删除分类-信息检测,是否符合删除条件
  90. // @Param ClassifyId query int true "分类ID"
  91. // @Success 200 {object} models.CheckDeleteClassifyResp
  92. // @router /checkDeleteClassify [get]
  93. func (this *ClassifyController) CheckDeleteClassify() {
  94. br := new(models.BaseResponse).Init()
  95. defer func() {
  96. this.Data["json"] = br
  97. this.ServeJSON()
  98. }()
  99. classifyId, err := this.GetInt("ClassifyId")
  100. if classifyId <= 0 {
  101. br.Msg = "参数错误"
  102. return
  103. }
  104. classify, e := models.GetClassifyById(classifyId)
  105. if e != nil {
  106. if utils.IsErrNoRow(e) {
  107. br.Msg = "分类不存在,请刷新页面"
  108. return
  109. }
  110. br.Msg = "获取信息失败"
  111. br.ErrMsg = "获取信息失败,Err:" + err.Error()
  112. return
  113. }
  114. resp := new(models.CheckDeleteClassifyResp)
  115. if classify == nil {
  116. br.Msg = "分类不存在"
  117. resp.Code = 1
  118. resp.Msg = "分类不存在"
  119. br.Data = resp
  120. br.Ret = 200
  121. br.Success = true
  122. return
  123. }
  124. if classify.ReportNum > 0 {
  125. resp.Code = 2
  126. resp.Msg = "该分类或子分类下有关联报告,不允许删除"
  127. br.Data = resp
  128. br.Ret = 200
  129. br.Msg = "该分类或子分类下有关联报告,不允许删除"
  130. br.Success = true
  131. return
  132. }
  133. //判断分类是否关联了报告
  134. //if classify.ParentId > 0 {
  135. // count, err := models.GetReportCountByClassifyId(classifyId)
  136. // if err != nil {
  137. // br.Msg = "获取信息失败"
  138. // br.ErrMsg = "获取信息失败,Err:" + err.Error()
  139. // return
  140. // }
  141. // if count > 0 {
  142. // resp.Code = 2
  143. // resp.Msg = "该分类有关联报告,不允许删除"
  144. // br.Data = resp
  145. // br.Ret = 200
  146. // br.Msg = "该分类有关联报告,不允许删除"
  147. // br.Success = true
  148. // return
  149. // }
  150. //} else {
  151. // subCount, err := models.GetClassifySubCountByClassifyId(classifyId)
  152. // if err != nil {
  153. // br.Msg = "获取信息失败"
  154. // br.ErrMsg = "获取信息失败,Err:" + err.Error()
  155. // return
  156. // }
  157. // if subCount > 0 {
  158. // resp.Code = 3
  159. // resp.Msg = "二级分类有关联报告,不允许删除"
  160. // br.Data = resp
  161. // br.Ret = 200
  162. // br.Msg = "二级分类有关联报告,不允许删除"
  163. // br.Success = true
  164. // return
  165. // }
  166. // subTotal, err := models.GetClassifySubCountByParentId(classifyId)
  167. // if err != nil {
  168. // br.Msg = "获取信息失败"
  169. // br.ErrMsg = "获取信息失败,Err:" + err.Error()
  170. // return
  171. // }
  172. // if subTotal > 0 {
  173. // resp.Code = 4
  174. // resp.Msg = "请先删除该分类下关联分类"
  175. // br.Data = resp
  176. // br.Ret = 200
  177. // br.Msg = "请先删除该分类下关联分类"
  178. // br.Success = true
  179. // return
  180. // }
  181. //}
  182. // 查询该分类是否关联了审批流
  183. //flowOb := new(report_approve.ReportApproveFlow)
  184. //flowCond := fmt.Sprintf(` AND %s = ? AND (%s = ? OR %s = ?)`, report_approve.ReportApproveFlowCols.ReportType, report_approve.ReportApproveFlowCols.ClassifyFirstId, report_approve.ReportApproveFlowCols.ClassifySecondId)
  185. //flowPars := make([]interface{}, 0)
  186. //flowPars = append(flowPars, report_approve.FlowReportTypeChinese, classifyId, classifyId)
  187. //flowCount, e := flowOb.GetCountByCondition(flowCond, flowPars)
  188. //if e != nil {
  189. // br.Msg = "检测失败"
  190. // br.ErrMsg = "获取关联审批流失败, Err: " + e.Error()
  191. // return
  192. //}
  193. //if flowCount > 0 {
  194. // resp.Code = 5
  195. // resp.Msg = "该分类关联审批流,不允许删除"
  196. // br.Data = resp
  197. // br.Ret = 200
  198. // br.Msg = resp.Msg
  199. // br.Success = true
  200. // return
  201. //}
  202. resp.Code = 0
  203. resp.Msg = "检测完成,可进行删除操作"
  204. br.Ret = 200
  205. br.Data = resp
  206. br.Success = true
  207. br.Msg = "检测成功"
  208. }
  209. // Delete
  210. // @Title 删除分类接口
  211. // @Description 删除分类
  212. // @Param request body models.DeleteClassifyReq true "type json string"
  213. // @Success 200 Ret=200,删除成功
  214. // @router /delete [post]
  215. func (this *ClassifyController) Delete() {
  216. br := new(models.BaseResponse).Init()
  217. defer func() {
  218. this.Data["json"] = br
  219. this.ServeJSON()
  220. }()
  221. var req models.DeleteClassifyReq
  222. err := json.Unmarshal(this.Ctx.Input.RequestBody, &req)
  223. if err != nil {
  224. br.Msg = "参数解析异常!"
  225. br.ErrMsg = "参数解析失败,Err:" + err.Error()
  226. return
  227. }
  228. if req.ClassifyId <= 0 {
  229. br.Msg = "参数错误"
  230. return
  231. }
  232. item, e := models.GetClassifyById(req.ClassifyId)
  233. if e != nil {
  234. if utils.IsErrNoRow(e) {
  235. br.Ret = 200
  236. br.Success = true
  237. br.Msg = "删除成功"
  238. return
  239. }
  240. br.Msg = "删除失败"
  241. br.ErrMsg = fmt.Sprintf("获取分类失败, %v", e)
  242. return
  243. }
  244. // 删除校验(上级分类报告数为子分类总和)
  245. if item.ReportNum > 0 {
  246. br.Msg = "分类下或子分类下有关联报告, 不允许删除"
  247. return
  248. }
  249. var removeIds []int
  250. {
  251. list, e := models.GetAllClassify()
  252. if e != nil {
  253. br.Msg = "删除失败"
  254. br.ErrMsg = fmt.Sprintf("获取所有分类失败, %v", e)
  255. return
  256. }
  257. removeIds = services.GetClassifyChildIdsTreeRecursive(list, item.Id)
  258. }
  259. removeIds = append(removeIds, item.Id)
  260. if e = models.DeleteClassify(removeIds); e != nil {
  261. br.Msg = "删除失败"
  262. br.ErrMsg = fmt.Sprintf("删除分类及子分类失败, %v", e)
  263. return
  264. }
  265. br.Ret = 200
  266. br.Success = true
  267. br.Msg = "删除成功"
  268. }
  269. // Edit
  270. // @Title 修改分类接口
  271. // @Description 修改分类
  272. // @Param request body models.EditClassifyReq true "type json string"
  273. // @Success 200 Ret=200,修改成功
  274. // @router /edit [post]
  275. func (this *ClassifyController) Edit() {
  276. br := new(models.BaseResponse).Init()
  277. defer func() {
  278. this.Data["json"] = br
  279. this.ServeJSON()
  280. }()
  281. var req models.EditClassifyReq
  282. err := json.Unmarshal(this.Ctx.Input.RequestBody, &req)
  283. if err != nil {
  284. br.Msg = "参数解析异常!"
  285. br.ErrMsg = "参数解析失败,Err:" + err.Error()
  286. return
  287. }
  288. if req.ClassifyId <= 0 {
  289. br.Msg = "参数错误"
  290. return
  291. }
  292. if req.ClassifyName == "" {
  293. br.Msg = "分类名称不可为空"
  294. return
  295. }
  296. if req.IsRemind == 1 {
  297. if req.RemindTime == "" {
  298. br.Msg = "请选择报告提醒时间"
  299. return
  300. }
  301. _, e := time.Parse("15:04", req.RemindTime)
  302. if e != nil {
  303. br.Msg = "提醒时间格式有误"
  304. br.ErrMsg = fmt.Sprintf("提醒时间格式有误, %s", req.RemindTime)
  305. return
  306. }
  307. }
  308. // 修改分类
  309. err, errMsg, isSentEmail := services.EditReportClassify(req.ClassifyId, req.ClassifyName, req.IsRemind, req.RemindTime)
  310. if err != nil {
  311. br.Msg = "修改失败"
  312. if errMsg != "" {
  313. br.Msg = errMsg
  314. }
  315. br.ErrMsg = "修改失败,Err:" + err.Error()
  316. br.IsSendEmail = isSentEmail
  317. return
  318. }
  319. br.Ret = 200
  320. br.Success = true
  321. br.Msg = "修改成功"
  322. }
  323. // ParentClassify
  324. // @Title 获取父级分类接口
  325. // @Description 获取父级分类
  326. // @Success 200 {object} models.Classify
  327. // @router /parent [get]
  328. func (this *ClassifyController) ParentClassify() {
  329. br := new(models.BaseResponse).Init()
  330. defer func() {
  331. this.Data["json"] = br
  332. this.ServeJSON()
  333. }()
  334. items, err := models.GetAllClassify()
  335. if err != nil {
  336. br.Msg = "获取失败"
  337. br.ErrMsg = "获取失败,Err:" + err.Error()
  338. return
  339. }
  340. classifyIds := make([]int, 0)
  341. for i := range items {
  342. classifyIds = append(classifyIds, items[i].Id)
  343. }
  344. classifyLen := len(classifyIds)
  345. if classifyLen == 0 {
  346. br.Ret = 200
  347. br.Success = true
  348. br.Msg = "获取成功"
  349. return
  350. }
  351. // 获取一级分类-子目录列表
  352. menuListMap := make(map[int][]*models.ClassifyMenu, 0)
  353. var menuCond string
  354. var menuPars []interface{}
  355. menuCond += ` AND classify_id IN (` + utils.GetOrmInReplace(classifyLen) + `)`
  356. menuPars = append(menuPars, classifyIds)
  357. parentMenus, e := models.GetClassifyMenuList(menuCond, menuPars)
  358. if e != nil {
  359. br.Msg = "获取失败"
  360. br.ErrMsg = "获取一级分类子目录列表失败"
  361. return
  362. }
  363. for i := range parentMenus {
  364. if menuListMap[parentMenus[i].ClassifyId] == nil {
  365. menuListMap[parentMenus[i].ClassifyId] = make([]*models.ClassifyMenu, 0)
  366. }
  367. menuListMap[parentMenus[i].ClassifyId] = append(menuListMap[parentMenus[i].ClassifyId], parentMenus[i])
  368. }
  369. resp := make([]*models.ClassifyItem, 0)
  370. for i := range items {
  371. resp = append(resp, &models.ClassifyItem{
  372. Classify: *items[i],
  373. ClassifyMenuList: menuListMap[items[i].Id],
  374. })
  375. }
  376. resp = services.GetClassifyTreeRecursive(resp, 0)
  377. br.Data = resp
  378. br.Ret = 200
  379. br.Success = true
  380. br.Msg = "获取成功"
  381. }
  382. // @Title 根据id获取分类详情接口
  383. // @Description 根据id获取分类详情
  384. // @Param request body models.FindByIdClassifyReq true "type json string"
  385. // @Success 200 {object} models.Classify
  386. // @router /findById [get]
  387. func (this *ClassifyController) FindByIdClassify() {
  388. br := new(models.BaseResponse).Init()
  389. defer func() {
  390. this.Data["json"] = br
  391. this.ServeJSON()
  392. }()
  393. var req models.FindByIdClassifyReq
  394. err := json.Unmarshal(this.Ctx.Input.RequestBody, &req)
  395. if err != nil {
  396. br.Msg = "参数解析异常!"
  397. br.ErrMsg = "参数解析失败,Err:" + err.Error()
  398. return
  399. }
  400. items, err := models.FindByIdClassify(req.ClassifyId)
  401. if err != nil {
  402. br.Msg = "获取失败"
  403. br.ErrMsg = "获取失败,Err:" + err.Error()
  404. return
  405. }
  406. br.Data = items
  407. br.Ret = 200
  408. br.Success = true
  409. br.Msg = "获取成功"
  410. }
  411. // ListClassify
  412. // @Title 获取分类列表
  413. // @Description 获取分类列表
  414. // @Param KeyWord query string true "检索关键词"
  415. // @Param ClassifyType query int false "分类类型:0-全部(默认);1-研报;2-PPT"
  416. // @Param Enabled query int false "启用状态:-1-全部(默认);0-禁用;1-启用"
  417. // @Success 200 {object} models.Classify
  418. // @router /list [get]
  419. func (this *ClassifyController) ListClassify() {
  420. br := new(models.BaseResponse).Init()
  421. defer func() {
  422. this.Data["json"] = br
  423. this.ServeJSON()
  424. }()
  425. classifyType, _ := this.GetInt("ClassifyType", 0)
  426. keyWord := this.GetString("KeyWord")
  427. reqEnabled, _ := this.GetInt("Enabled", -1)
  428. enabled := -1
  429. if reqEnabled == 1 {
  430. enabled = reqEnabled
  431. }
  432. //list, err := models.GetClassifyListByKeyword(keyWord, enabled, classifyType)
  433. //if err != nil {
  434. // br.Msg = "获取失败"
  435. // br.ErrMsg = "获取失败,Err:" + err.Error()
  436. // return
  437. //}
  438. //
  439. //if keyWord != `` {
  440. // idMap := make(map[int]bool)
  441. //
  442. // currParentClassifyIdList := make([]int, 0)
  443. // for _, v := range list {
  444. // idMap[v.Id] = true
  445. // if v.ParentId > 0 {
  446. // currParentClassifyIdList = append(currParentClassifyIdList, v.ParentId)
  447. // }
  448. // }
  449. //
  450. // findList := list
  451. // list = make([]*models.ClassifyList, 0)
  452. //
  453. // tmpList, tmpErr := services.GetParentClassifyListByParentIdList(currParentClassifyIdList)
  454. // if tmpErr != nil {
  455. // br.Msg = "获取失败"
  456. // br.ErrMsg = "获取上级分类信息失败,Err:" + tmpErr.Error()
  457. // return
  458. // }
  459. // for _, v := range tmpList {
  460. // if _, ok := idMap[v.Id]; !ok {
  461. // list = append(list, v)
  462. // }
  463. // }
  464. //
  465. // list = append(list, findList...)
  466. //}
  467. //
  468. //classifyIdList := make([]int, 0)
  469. //for i := range list {
  470. // classifyIdList = append(classifyIdList, list[i].Id)
  471. //}
  472. //parentIdLen := len(classifyIdList)
  473. //if parentIdLen == 0 {
  474. // resp := &models.ClassifyListResp{
  475. // List: list,
  476. // }
  477. // br.Data = resp
  478. // br.Ret = 200
  479. // br.Success = true
  480. // br.Msg = "获取成功"
  481. // return
  482. //}
  483. //
  484. //// 获取子目录列表
  485. //menuListMap := make(map[int][]*models.ClassifyMenu, 0)
  486. //var menuCond string
  487. //var menuPars []interface{}
  488. //menuCond += ` AND classify_id IN (` + utils.GetOrmInReplace(parentIdLen) + `)`
  489. //menuPars = append(menuPars, classifyIdList)
  490. //parentMenus, e := models.GetClassifyMenuList(menuCond, menuPars)
  491. //if e != nil {
  492. // br.Msg = "获取失败"
  493. // br.ErrMsg = "获取一级分类子目录列表失败"
  494. // return
  495. //}
  496. //for i := range parentMenus {
  497. // if menuListMap[parentMenus[i].ClassifyId] == nil {
  498. // menuListMap[parentMenus[i].ClassifyId] = make([]*models.ClassifyMenu, 0)
  499. // }
  500. // menuListMap[parentMenus[i].ClassifyId] = append(menuListMap[parentMenus[i].ClassifyId], parentMenus[i])
  501. //}
  502. //
  503. //// 分类与子目录关联
  504. //relateMap := make(map[int]int, 0)
  505. //{
  506. // var relateCond string
  507. // var relatePars []interface{}
  508. // relateCond += ` AND classify_id IN (` + utils.GetOrmInReplace(parentIdLen) + `)`
  509. // relatePars = append(relatePars, classifyIdList)
  510. // relates, e := models.GetClassifyMenuRelationList(relateCond, relatePars)
  511. // if e != nil {
  512. // br.Msg = "获取失败"
  513. // br.ErrMsg = "获取二级分类子目录关联失败, Err: " + e.Error()
  514. // return
  515. // }
  516. // for i := range relates {
  517. // relateMap[relates[i].ClassifyId] = relates[i].MenuId
  518. // }
  519. //}
  520. //
  521. //// 查询分类绑定的权限
  522. //permissionList, _ := models.GetAllPermissionMapping()
  523. //classifyPermissionMap := make(map[int][]int)
  524. //if len(permissionList) > 0 {
  525. // for _, v := range permissionList {
  526. // classifyPermissionMap[v.ClassifyId] = append(classifyPermissionMap[v.ClassifyId], v.ChartPermissionId)
  527. // }
  528. //}
  529. //// 遍历分类并绑定子目录和权限
  530. //for i, v := range list {
  531. // list[i].ClassifyMenuList = menuListMap[v.Id]
  532. //
  533. // list[i].ClassifyMenuId = relateMap[v.Id]
  534. // if permissionIds, ok := classifyPermissionMap[v.Id]; ok {
  535. // list[i].ChartPermissionIdList = permissionIds
  536. // }
  537. //}
  538. // 获取所有分类
  539. originList, err := models.GetClassifyListByKeyword("", enabled, 0)
  540. if err != nil {
  541. br.Msg = "获取失败"
  542. br.ErrMsg = "获取失败,Err:" + err.Error()
  543. return
  544. }
  545. list := originList
  546. // 指定分类类型(上级中的分类类型可能与最下层的不一致,但是要把上级也一起取出来, 这需求...=_=!)
  547. if classifyType > 0 {
  548. list = make([]*models.ClassifyList, 0)
  549. // 先取出指定分类以及他对应的LevelPath中的所有ID
  550. classifyIds := make([]int, 0)
  551. for _, v := range originList {
  552. if v.ClassifyType != classifyType {
  553. continue
  554. }
  555. pathArr := strings.Split(v.LevelPath, ",")
  556. for _, p := range pathArr {
  557. id, _ := strconv.Atoi(p)
  558. if id <= 0 {
  559. continue
  560. }
  561. if utils.InArrayByInt(classifyIds, id) {
  562. continue
  563. }
  564. classifyIds = append(classifyIds, id)
  565. }
  566. }
  567. // 过滤掉不在ID中的
  568. for _, v := range originList {
  569. if !utils.InArrayByInt(classifyIds, v.Id) {
  570. continue
  571. }
  572. list = append(list, v)
  573. }
  574. }
  575. // 有关键词,那么按照上面的方法再筛一遍
  576. keyWord = strings.TrimSpace(keyWord)
  577. if keyWord != `` {
  578. classifyIds := make([]int, 0)
  579. for _, v := range list {
  580. if !strings.Contains(v.ClassifyName, keyWord) {
  581. continue
  582. }
  583. pathArr := strings.Split(v.LevelPath, ",")
  584. for _, p := range pathArr {
  585. id, _ := strconv.Atoi(p)
  586. if id <= 0 {
  587. continue
  588. }
  589. if utils.InArrayByInt(classifyIds, id) {
  590. continue
  591. }
  592. classifyIds = append(classifyIds, id)
  593. }
  594. }
  595. for _, v := range originList {
  596. if !utils.InArrayByInt(classifyIds, v.Id) {
  597. continue
  598. }
  599. list = append(list, v)
  600. }
  601. }
  602. // 先将分类列表排序
  603. services.SortClassifyListBySortAndCreateTime(list)
  604. // 接着转换结构
  605. list = services.GetClassifyListTreeRecursive(list, 0)
  606. resp := new(models.ClassifyListResp)
  607. resp.List = list
  608. br.Data = resp
  609. br.Ret = 200
  610. br.Success = true
  611. br.Msg = "获取成功"
  612. }
  613. // ClassifyPermission
  614. // @Title 获取分类权限列表
  615. // @Description 获取分类权限列表
  616. // @Param ClassifyId query int true "分类id"
  617. // @Success 200 {object} []models.ChartPermissionSearchKeyWordMapping
  618. // @router /permission_list [get]
  619. func (this *ClassifyController) ClassifyPermission() {
  620. br := new(models.BaseResponse).Init()
  621. defer func() {
  622. this.Data["json"] = br
  623. this.ServeJSON()
  624. }()
  625. classifyId, _ := this.GetInt("ClassifyId")
  626. if classifyId <= 0 {
  627. br.Msg = "参数错误"
  628. return
  629. }
  630. //获取报告分类详情
  631. classifyInfo, err := models.GetClassifyById(classifyId)
  632. if err != nil {
  633. br.Msg = "获取分类信息失败"
  634. br.ErrMsg = "获取失败,Err:" + err.Error()
  635. return
  636. }
  637. //获取报告分类权限列表
  638. list, err := models.GetPermissionByClassifyId(classifyInfo.Id)
  639. if err != nil {
  640. br.Msg = "获取分类信息失败"
  641. br.ErrMsg = "获取失败,Err:" + err.Error()
  642. return
  643. }
  644. br.Data = list
  645. br.Ret = 200
  646. br.Success = true
  647. br.Msg = "获取成功"
  648. }
  649. // EditClassifyPermission
  650. // @Title 编辑分类权限
  651. // @Description 编辑分类权限
  652. // @Param ClassifyId query int true "分类id"
  653. // @Success 200 {object} models.EditClassifyPermissionReq
  654. // @router /permission/edit [post]
  655. func (this *ClassifyController) EditClassifyPermission() {
  656. br := new(models.BaseResponse).Init()
  657. defer func() {
  658. this.Data["json"] = br
  659. this.ServeJSON()
  660. }()
  661. var req models.EditClassifyPermissionReq
  662. err := json.Unmarshal(this.Ctx.Input.RequestBody, &req)
  663. if err != nil {
  664. br.Msg = "参数解析异常!"
  665. br.ErrMsg = "参数解析失败,Err:" + err.Error()
  666. return
  667. }
  668. if req.ClassifyId <= 0 {
  669. br.Msg = "参数错误"
  670. return
  671. }
  672. if len(req.ChartPermissionIdList) <= 0 {
  673. br.Msg = "请选择权限"
  674. return
  675. }
  676. //获取报告分类详情
  677. classifyInfo, err := models.GetClassifyById(req.ClassifyId)
  678. if err != nil {
  679. br.Msg = "获取分类信息失败"
  680. br.ErrMsg = "获取失败,Err:" + err.Error()
  681. return
  682. }
  683. //获取报告分类权限列表
  684. err = models.EditChartPermissionSearchKeyWordMappingMulti(classifyInfo.ClassifyName, req.ChartPermissionIdList, req.ClassifyId)
  685. if err != nil {
  686. br.Msg = "修改分类权限失败"
  687. br.ErrMsg = "修改分类权限失败,Err:" + err.Error()
  688. return
  689. }
  690. go func() {
  691. _ = services.EditClassifyChartPermissionSync(classifyInfo.ClassifyName, req.ClassifyId)
  692. }()
  693. // 是关联电话会二级分类修改了权限, 同步关联报告电话会的分类
  694. //if classifyInfo.ParentId > 0 && classifyInfo.RelateTel == 1 {
  695. // go func() {
  696. // _ = yb.SyncClassifyAndFiccActivityType()
  697. // }()
  698. //}
  699. br.Ret = 200
  700. br.Success = true
  701. br.Msg = "修改成功"
  702. }
  703. // @Title 含电话会分类列表
  704. // @Description 含电话会分类列表
  705. // @Success 200 {object} models.Classify
  706. // @router /tel_list [get]
  707. func (this *ClassifyController) TelListClassify() {
  708. br := new(models.BaseResponse).Init()
  709. defer func() {
  710. this.Data["json"] = br
  711. this.ServeJSON()
  712. }()
  713. resp := make([]*models.SimpleClassifyList, 0)
  714. var condition, orderRule string
  715. var pars []interface{}
  716. telList, e := models.GetClassifyByCondition(condition, orderRule, pars)
  717. if e != nil {
  718. return
  719. }
  720. firstList := make([]*models.SimpleClassifyList, 0)
  721. secondList := make([]*models.SimpleClassifyList, 0)
  722. secondMap := make(map[int][]*models.SimpleClassifyList, 0)
  723. for i := range telList {
  724. if telList[i].ParentId == 0 {
  725. firstList = append(firstList, telList[i])
  726. } else {
  727. secondList = append(secondList, telList[i])
  728. secondMap[telList[i].ParentId] = append(secondMap[telList[i].ParentId], telList[i])
  729. }
  730. }
  731. for i := range firstList {
  732. firstList[i].Child = secondMap[firstList[i].Id]
  733. }
  734. resp = firstList
  735. br.Data = resp
  736. br.Ret = 200
  737. br.Success = true
  738. br.Msg = "获取成功"
  739. }
  740. // Move
  741. // @Title 移动分类接口
  742. // @Description 移动分类
  743. // @Param request body models.ClassifyMoveReq true "type json string"
  744. // @Success 200 新增成功
  745. // @router /move [post]
  746. func (this *ClassifyController) Move() {
  747. br := new(models.BaseResponse).Init()
  748. defer func() {
  749. this.Data["json"] = br
  750. this.ServeJSON()
  751. }()
  752. var req models.ClassifyMoveReq
  753. err := json.Unmarshal(this.Ctx.Input.RequestBody, &req)
  754. if err != nil {
  755. br.Msg = "参数解析异常!"
  756. br.ErrMsg = "参数解析失败,Err:" + err.Error()
  757. return
  758. }
  759. if req.ClassifyId <= 0 {
  760. br.Msg = "请选择分类"
  761. return
  762. }
  763. e, msg := services.MoveReportClassify(req)
  764. if e != nil {
  765. br.Msg = msg
  766. br.ErrMsg = "移动分类失败, Err: " + e.Error()
  767. return
  768. }
  769. br.Ret = 200
  770. br.Success = true
  771. br.Msg = "操作成功"
  772. }
  773. // SetEnabled
  774. // @Title 启用/禁用分类接口
  775. // @Description 启用/禁用分类
  776. // @Param request body models.ClassifyMoveReq true "type json string"
  777. // @Success 200 新增成功
  778. // @router /enabled/set [post]
  779. func (this *ClassifyController) SetEnabled() {
  780. br := new(models.BaseResponse).Init()
  781. defer func() {
  782. this.Data["json"] = br
  783. this.ServeJSON()
  784. }()
  785. var req models.ClassifySetEnabledReq
  786. err := json.Unmarshal(this.Ctx.Input.RequestBody, &req)
  787. if err != nil {
  788. br.Msg = "参数解析异常!"
  789. br.ErrMsg = "参数解析失败,Err:" + err.Error()
  790. return
  791. }
  792. if req.ClassifyId <= 0 {
  793. br.Msg = "请选择分类"
  794. return
  795. }
  796. if req.Enabled != 0 && req.Enabled != 1 {
  797. br.Msg = "请选择正确的启用禁用状态"
  798. return
  799. }
  800. item, err := models.GetClassifyById(req.ClassifyId)
  801. if err != nil {
  802. if utils.IsErrNoRow(err) {
  803. br.Msg = "分类不存在"
  804. return
  805. }
  806. br.Msg = "获取信息失败"
  807. br.ErrMsg = "获取信息失败,Err:" + err.Error()
  808. return
  809. }
  810. if item == nil {
  811. br.Msg = "分类不存在"
  812. return
  813. }
  814. ob := new(models.Classify)
  815. //设置分类启用、禁用状态
  816. err = ob.SetEnabled(req.ClassifyId, req.Enabled)
  817. if err != nil {
  818. br.Msg = "操作失败"
  819. br.ErrMsg = "操作失败,Err:" + err.Error()
  820. return
  821. }
  822. br.Ret = 200
  823. br.Success = true
  824. br.Msg = "操作成功"
  825. }
  826. // ClassifyPermissionV2
  827. // @Title 获取权限设置基础信息
  828. // @Description 获取权限设置基础信息接口
  829. // @Param CompanyType query string true "客户类型:传空字符串或者不传为全部,'ficc','权益'"
  830. // @Param NoUpgrade query bool false "是否不展示升级权限,默认为 false"
  831. // @Success 200 {object} company.PermissionSetResp
  832. // @router /permission/list [get]
  833. func (this *ClassifyController) ClassifyPermissionV2() {
  834. br := new(models.BaseResponse).Init()
  835. defer func() {
  836. this.Data["json"] = br
  837. this.ServeJSON()
  838. }()
  839. sysUser := this.SysUser
  840. if sysUser == nil {
  841. br.Msg = "请登录"
  842. br.ErrMsg = "请登录,SysUser Is Empty"
  843. br.Ret = 408
  844. return
  845. }
  846. classifyId, _ := this.GetInt("ClassifyId")
  847. if classifyId <= 0 {
  848. br.Msg = "参数错误"
  849. return
  850. }
  851. resp := make([]*models.ChartPermissionItem, 0)
  852. // 拥有的品种ID列表
  853. permissionIdMap := make(map[int]bool)
  854. {
  855. //获取报告分类详情
  856. classifyInfo, err := models.GetClassifyById(classifyId)
  857. if err != nil {
  858. br.Msg = "获取分类信息失败"
  859. br.ErrMsg = "获取失败,Err:" + err.Error()
  860. return
  861. }
  862. //获取报告分类权限列表
  863. list, err := models.GetPermission(classifyInfo.Id)
  864. if err != nil {
  865. br.Msg = "获取分类信息失败"
  866. br.ErrMsg = "获取失败,Err:" + err.Error()
  867. return
  868. }
  869. for _, v := range list {
  870. permissionIdMap[v.ChartPermissionId] = true
  871. }
  872. }
  873. // 没有关联品种时,直接返回
  874. num := len(permissionIdMap)
  875. if num <= 0 {
  876. br.Ret = 200
  877. br.Success = true
  878. br.Msg = "获取成功"
  879. br.Data = resp
  880. return
  881. }
  882. cond := ` and product_id = ?`
  883. pars := make([]interface{}, 0)
  884. pars = append(pars, 1)
  885. list, e := services.GetChartPermissionList(cond, pars)
  886. if e != nil {
  887. br.Msg = "获取失败"
  888. br.ErrMsg = "获取品种列表失败, Err: " + e.Error()
  889. return
  890. }
  891. // 品种树
  892. childMap := make(map[int][]*models.ChartPermissionItem)
  893. tmpResp := make([]*models.ChartPermissionItem, 0)
  894. for _, v := range list {
  895. t := new(models.ChartPermissionItem)
  896. t.PermissionId = v.ChartPermissionId
  897. t.PermissionName = v.PermissionName
  898. t.ParentId = v.ParentId
  899. t.IsPublic = v.IsPublic
  900. t.Enabled = v.Enabled
  901. t.Sort = v.Sort
  902. t.CreateTime = v.CreatedTime.Format(utils.FormatDateTime)
  903. t.Child = make([]*models.ChartPermissionItem, 0)
  904. if v.ParentId == 0 {
  905. tmpResp = append(tmpResp, t)
  906. continue
  907. }
  908. if v.ParentId > 0 {
  909. if _, ok := permissionIdMap[v.ChartPermissionId]; ok {
  910. if childMap[v.ParentId] == nil {
  911. childMap[v.ParentId] = make([]*models.ChartPermissionItem, 0)
  912. }
  913. childMap[v.ParentId] = append(childMap[v.ParentId], t)
  914. }
  915. }
  916. }
  917. for _, r := range tmpResp {
  918. childList, ok := childMap[r.PermissionId]
  919. if !ok {
  920. continue
  921. }
  922. r.Child = childList
  923. resp = append(resp, r)
  924. }
  925. br.Ret = 200
  926. br.Success = true
  927. br.Msg = "获取成功"
  928. br.Data = resp
  929. }