classify.go 9.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354
  1. package controllers
  2. import (
  3. "encoding/json"
  4. "hongze/hongze_admin/models"
  5. "hongze/hongze_admin/utils"
  6. "time"
  7. )
  8. //分类
  9. type ClassifyController struct {
  10. BaseAuthController
  11. }
  12. // @Title 新增分类接口
  13. // @Description 新增分类
  14. // @Param request body models.ClassifyAddReq true "type json string"
  15. // @Success 200 新增成功
  16. // @router /add [post]
  17. func (this *ClassifyController) Add() {
  18. br := new(models.BaseResponse).Init()
  19. defer func() {
  20. this.Data["json"] = br
  21. this.ServeJSON()
  22. }()
  23. var req models.ClassifyAddReq
  24. err := json.Unmarshal(this.Ctx.Input.RequestBody, &req)
  25. if err != nil {
  26. br.Msg = "参数解析异常!"
  27. br.ErrMsg = "参数解析失败,Err:" + err.Error()
  28. return
  29. }
  30. if req.ClassifyName == "" {
  31. br.Msg = "分类名称不可为空"
  32. return
  33. }
  34. item, err := models.GetClassifyByName(req.ClassifyName, req.ParentId)
  35. if err != nil && err.Error() != utils.ErrNoRow() {
  36. br.Msg = "获取分类信息失败"
  37. br.ErrMsg = "获取分类信息失败,Err:" + err.Error()
  38. return
  39. }
  40. if item != nil {
  41. br.Msg = "分类名称:" + req.ClassifyName + "已存在"
  42. return
  43. }
  44. classify := new(models.Classify)
  45. classify.ClassifyName = req.ClassifyName
  46. classify.ParentId = req.ParentId
  47. classify.CreateTime = time.Now()
  48. classify.ModifyTime = time.Now()
  49. classify.Abstract = req.Abstract
  50. classify.Descript = req.Descript
  51. err = models.AddClassify(classify)
  52. if err != nil {
  53. br.Msg = "新增失败"
  54. br.ErrMsg = "新增失败,Err:" + err.Error()
  55. return
  56. }
  57. br.Ret = 200
  58. br.Success = true
  59. br.Msg = "新增成功"
  60. }
  61. // @Title 删除分类-检测接口
  62. // @Description 删除分类-信息检测,是否符合删除条件
  63. // @Param request body models.CheckDeleteClassifyReq true "type json string"
  64. // @Success 200 {object} models.CheckDeleteClassifyResp
  65. // @router /checkDeleteClassify [get]
  66. func (this *ClassifyController) CheckDeleteClassify() {
  67. br := new(models.BaseResponse).Init()
  68. defer func() {
  69. this.Data["json"] = br
  70. this.ServeJSON()
  71. }()
  72. var req models.CheckDeleteClassifyReq
  73. err := json.Unmarshal(this.Ctx.Input.RequestBody, &req)
  74. if err != nil {
  75. br.Msg = "参数解析异常!"
  76. br.ErrMsg = "参数解析失败,Err:" + err.Error()
  77. return
  78. }
  79. if req.ClassifyId <= 0 {
  80. br.Msg = "参数错误"
  81. return
  82. }
  83. classify, err := models.GetClassifyById(req.ClassifyId)
  84. if err != nil {
  85. br.Msg = "获取信息失败"
  86. br.ErrMsg = "获取信息失败,Err:" + err.Error()
  87. return
  88. }
  89. resp := new(models.CheckDeleteClassifyResp)
  90. if classify == nil {
  91. br.Msg = "分类不存在"
  92. resp.Code = 1
  93. resp.Msg = "分类不存在"
  94. br.Data = resp
  95. br.Ret = 200
  96. br.Success = true
  97. return
  98. }
  99. //判断分类是否关联了报告
  100. if classify.ParentId > 0 {
  101. count, err := models.GetReportCountByClassifyId(req.ClassifyId)
  102. if err != nil {
  103. br.Msg = "获取信息失败"
  104. br.ErrMsg = "获取信息失败,Err:" + err.Error()
  105. return
  106. }
  107. if count > 0 {
  108. resp.Code = 2
  109. resp.Msg = "该分类有关联报告,不允许删除"
  110. br.Data = resp
  111. br.Ret = 200
  112. br.Msg = "该分类有关联报告,不允许删除"
  113. br.Success = true
  114. return
  115. }
  116. } else {
  117. subCount, err := models.GetClassifySubCountByClassifyId(req.ClassifyId)
  118. if err != nil {
  119. br.Msg = "获取信息失败"
  120. br.ErrMsg = "获取信息失败,Err:" + err.Error()
  121. return
  122. }
  123. if subCount > 0 {
  124. resp.Code = 3
  125. resp.Msg = "二级分类有关联报告,不允许删除"
  126. br.Data = resp
  127. br.Ret = 200
  128. br.Msg = "二级分类有关联报告,不允许删除"
  129. br.Success = true
  130. return
  131. }
  132. subTotal, err := models.GetClassifySubCountByParentId(req.ClassifyId)
  133. if err != nil {
  134. br.Msg = "获取信息失败"
  135. br.ErrMsg = "获取信息失败,Err:" + err.Error()
  136. return
  137. }
  138. if subTotal > 0 {
  139. resp.Code = 4
  140. resp.Msg = "该分类下有关联分类,是否确认全部删除"
  141. br.Data = resp
  142. br.Ret = 200
  143. br.Msg = "该分类下有关联分类,是否确认全部删除"
  144. br.Success = true
  145. return
  146. }
  147. }
  148. resp.Code = 0
  149. resp.Msg = "检测完成,可进行删除操作"
  150. br.Ret = 200
  151. br.Data = resp
  152. br.Success = true
  153. br.Msg = "检测成功"
  154. }
  155. // @Title 删除分类接口
  156. // @Description 删除分类
  157. // @Param request body models.DeleteClassifyReq true "type json string"
  158. // @Success 200 Ret=200,删除成功
  159. // @router /delete [post]
  160. func (this *ClassifyController) Delete() {
  161. br := new(models.BaseResponse).Init()
  162. defer func() {
  163. this.Data["json"] = br
  164. this.ServeJSON()
  165. }()
  166. var req models.DeleteClassifyReq
  167. err := json.Unmarshal(this.Ctx.Input.RequestBody, &req)
  168. if err != nil {
  169. br.Msg = "参数解析异常!"
  170. br.ErrMsg = "参数解析失败,Err:" + err.Error()
  171. return
  172. }
  173. if req.ClassifyId <= 0 {
  174. br.Msg = "参数错误"
  175. return
  176. }
  177. item, err := models.GetClassifyById(req.ClassifyId)
  178. if err != nil {
  179. br.Msg = "获取信息失败"
  180. br.ErrMsg = "获取信息失败,Err:" + err.Error()
  181. return
  182. }
  183. if item == nil {
  184. br.Msg = "分类不存在"
  185. return
  186. }
  187. err = models.DeleteClassify(req.ClassifyId)
  188. if err != nil {
  189. br.Msg = "删除失败"
  190. br.ErrMsg = "删除失败,Err:" + err.Error()
  191. return
  192. }
  193. br.Ret = 200
  194. br.Success = true
  195. br.Msg = "删除成功"
  196. }
  197. // @Title 修改分类接口
  198. // @Description 修改分类
  199. // @Param request body models.EditClassifyReq true "type json string"
  200. // @Success 200 Ret=200,修改成功
  201. // @router /edit [post]
  202. func (this *ClassifyController) Edit() {
  203. br := new(models.BaseResponse).Init()
  204. defer func() {
  205. this.Data["json"] = br
  206. this.ServeJSON()
  207. }()
  208. var req models.EditClassifyReq
  209. err := json.Unmarshal(this.Ctx.Input.RequestBody, &req)
  210. if err != nil {
  211. br.Msg = "参数解析异常!"
  212. br.ErrMsg = "参数解析失败,Err:" + err.Error()
  213. return
  214. }
  215. if req.ClassifyId <= 0 {
  216. br.Msg = "参数错误"
  217. return
  218. }
  219. item, err := models.GetClassifyByName(req.ClassifyName, req.ParentId)
  220. if err != nil && err.Error() != utils.ErrNoRow() {
  221. br.Msg = "获取信息失败"
  222. br.ErrMsg = "获取信息失败,Err:" + err.Error()
  223. return
  224. }
  225. if item != nil && item.Id != req.ClassifyId {
  226. br.Msg = "分类名称:" + req.ClassifyName + "已存在"
  227. return
  228. }
  229. err = models.EditClassify(req.ClassifyName, req.Abstract, req.Descript, req.ParentId, req.ClassifyId)
  230. if err != nil {
  231. br.Msg = "修改失败"
  232. br.ErrMsg = "修改失败,Err:" + err.Error()
  233. return
  234. }
  235. br.Ret = 200
  236. br.Success = true
  237. br.Msg = "修改成功"
  238. }
  239. // @Title 获取父级分类接口
  240. // @Description 获取父级分类
  241. // @Success 200 {object} models.Classify
  242. // @router /parent [get]
  243. func (this *ClassifyController) ParentClassify() {
  244. br := new(models.BaseResponse).Init()
  245. defer func() {
  246. this.Data["json"] = br
  247. this.ServeJSON()
  248. }()
  249. items, err := models.ParentClassify()
  250. if err != nil {
  251. br.Msg = "获取失败"
  252. br.ErrMsg = "获取失败,Err:" + err.Error()
  253. return
  254. }
  255. br.Data = items
  256. br.Ret = 200
  257. br.Success = true
  258. br.Msg = "获取成功"
  259. }
  260. // @Title 根据id获取分类详情接口
  261. // @Description 根据id获取分类详情
  262. // @Param request body models.FindByIdClassifyReq true "type json string"
  263. // @Success 200 {object} models.Classify
  264. // @router /findById [get]
  265. func (this *ClassifyController) FindByIdClassify() {
  266. br := new(models.BaseResponse).Init()
  267. defer func() {
  268. this.Data["json"] = br
  269. this.ServeJSON()
  270. }()
  271. var req models.FindByIdClassifyReq
  272. err := json.Unmarshal(this.Ctx.Input.RequestBody, &req)
  273. if err != nil {
  274. br.Msg = "参数解析异常!"
  275. br.ErrMsg = "参数解析失败,Err:" + err.Error()
  276. return
  277. }
  278. items, err := models.FindByIdClassify(req.ClassifyId)
  279. if err != nil {
  280. br.Msg = "获取失败"
  281. br.ErrMsg = "获取失败,Err:" + err.Error()
  282. return
  283. }
  284. br.Data = items
  285. br.Ret = 200
  286. br.Success = true
  287. br.Msg = "获取成功"
  288. }
  289. // @Title 获取分类列表
  290. // @Description 获取分类列表
  291. // @Param PageSize query int true "每页数据条数"
  292. // @Param CurrentIndex query int true "当前页页码,从1开始"
  293. // @Param KeyWord query string true "检索关键词"
  294. // @Success 200 {object} models.Classify
  295. // @router /list [get]
  296. func (this *ClassifyController) ListClassify() {
  297. br := new(models.BaseResponse).Init()
  298. defer func() {
  299. this.Data["json"] = br
  300. this.ServeJSON()
  301. }()
  302. pageSize, _ := this.GetInt("PageSize")
  303. currentIndex, _ := this.GetInt("CurrentIndex")
  304. keyWord := this.GetString("KeyWord")
  305. var startSize int
  306. if pageSize <= 0 {
  307. pageSize = utils.PageSize20
  308. }
  309. if currentIndex <= 0 {
  310. currentIndex = 1
  311. }
  312. startSize = utils.StartIndex(currentIndex, pageSize)
  313. list, err := models.GetClassifyList(startSize, pageSize, keyWord)
  314. if err != nil {
  315. br.Msg = "获取失败"
  316. br.ErrMsg = "获取失败,Err:" + err.Error()
  317. return
  318. }
  319. total, err := models.GetClassifyListCount(keyWord)
  320. if err != nil {
  321. br.Msg = "获取失败"
  322. br.ErrMsg = "获取失败,Err:" + err.Error()
  323. return
  324. }
  325. for _, v := range list {
  326. child, err := models.GetClassifyChild(v.Id, keyWord)
  327. if err != nil {
  328. br.Msg = "获取信息失败"
  329. br.ErrMsg = "获取失败,Err:" + err.Error()
  330. return
  331. }
  332. v.Child = child
  333. }
  334. page := models.GetPaging(currentIndex, pageSize, total)
  335. resp := new(models.ClassifyListResp)
  336. resp.List = list
  337. resp.Paging = page
  338. br.Data = resp
  339. br.Ret = 200
  340. br.Success = true
  341. br.Msg = "获取成功"
  342. }