package controllers import ( "encoding/json" "hongze/hongze_admin/models" "hongze/hongze_admin/utils" "time" ) //分类 type ClassifyController struct { BaseAuthController } // @Title 新增分类接口 // @Description 新增分类 // @Param request body models.ClassifyAddReq true "type json string" // @Success 200 新增成功 // @router /add [post] func (this *ClassifyController) Add() { br := new(models.BaseResponse).Init() defer func() { this.Data["json"] = br this.ServeJSON() }() var req models.ClassifyAddReq err := json.Unmarshal(this.Ctx.Input.RequestBody, &req) if err != nil { br.Msg = "参数解析异常!" br.ErrMsg = "参数解析失败,Err:" + err.Error() return } if req.ClassifyName == "" { br.Msg = "分类名称不可为空" return } item, err := models.GetClassifyByName(req.ClassifyName, req.ParentId) if err != nil && err.Error() != utils.ErrNoRow() { br.Msg = "获取分类信息失败" br.ErrMsg = "获取分类信息失败,Err:" + err.Error() return } if item != nil { br.Msg = "分类名称:" + req.ClassifyName + "已存在" return } classify := new(models.Classify) classify.ClassifyName = req.ClassifyName classify.ParentId = req.ParentId classify.CreateTime = time.Now() classify.ModifyTime = time.Now() classify.Abstract = req.Abstract classify.Descript = req.Descript err = models.AddClassify(classify) if err != nil { br.Msg = "新增失败" br.ErrMsg = "新增失败,Err:" + err.Error() return } br.Ret = 200 br.Success = true br.Msg = "新增成功" } // @Title 删除分类-检测接口 // @Description 删除分类-信息检测,是否符合删除条件 // @Param request body models.CheckDeleteClassifyReq true "type json string" // @Success 200 {object} models.CheckDeleteClassifyResp // @router /checkDeleteClassify [get] func (this *ClassifyController) CheckDeleteClassify() { br := new(models.BaseResponse).Init() defer func() { this.Data["json"] = br this.ServeJSON() }() var req models.CheckDeleteClassifyReq err := json.Unmarshal(this.Ctx.Input.RequestBody, &req) if err != nil { br.Msg = "参数解析异常!" br.ErrMsg = "参数解析失败,Err:" + err.Error() return } if req.ClassifyId <= 0 { br.Msg = "参数错误" return } classify, err := models.GetClassifyById(req.ClassifyId) if err != nil { br.Msg = "获取信息失败" br.ErrMsg = "获取信息失败,Err:" + err.Error() return } resp := new(models.CheckDeleteClassifyResp) if classify == nil { br.Msg = "分类不存在" resp.Code = 1 resp.Msg = "分类不存在" br.Data = resp br.Ret = 200 br.Success = true return } //判断分类是否关联了报告 if classify.ParentId > 0 { count, err := models.GetReportCountByClassifyId(req.ClassifyId) if err != nil { br.Msg = "获取信息失败" br.ErrMsg = "获取信息失败,Err:" + err.Error() return } if count > 0 { resp.Code = 2 resp.Msg = "该分类有关联报告,不允许删除" br.Data = resp br.Ret = 200 br.Msg = "该分类有关联报告,不允许删除" br.Success = true return } } else { subCount, err := models.GetClassifySubCountByClassifyId(req.ClassifyId) if err != nil { br.Msg = "获取信息失败" br.ErrMsg = "获取信息失败,Err:" + err.Error() return } if subCount > 0 { resp.Code = 3 resp.Msg = "二级分类有关联报告,不允许删除" br.Data = resp br.Ret = 200 br.Msg = "二级分类有关联报告,不允许删除" br.Success = true return } subTotal, err := models.GetClassifySubCountByParentId(req.ClassifyId) if err != nil { br.Msg = "获取信息失败" br.ErrMsg = "获取信息失败,Err:" + err.Error() return } if subTotal > 0 { resp.Code = 4 resp.Msg = "该分类下有关联分类,是否确认全部删除" br.Data = resp br.Ret = 200 br.Msg = "该分类下有关联分类,是否确认全部删除" br.Success = true return } } resp.Code = 0 resp.Msg = "检测完成,可进行删除操作" br.Ret = 200 br.Data = resp br.Success = true br.Msg = "检测成功" } // @Title 删除分类接口 // @Description 删除分类 // @Param request body models.DeleteClassifyReq true "type json string" // @Success 200 Ret=200,删除成功 // @router /delete [post] func (this *ClassifyController) Delete() { br := new(models.BaseResponse).Init() defer func() { this.Data["json"] = br this.ServeJSON() }() var req models.DeleteClassifyReq err := json.Unmarshal(this.Ctx.Input.RequestBody, &req) if err != nil { br.Msg = "参数解析异常!" br.ErrMsg = "参数解析失败,Err:" + err.Error() return } if req.ClassifyId <= 0 { br.Msg = "参数错误" return } item, err := models.GetClassifyById(req.ClassifyId) if err != nil { br.Msg = "获取信息失败" br.ErrMsg = "获取信息失败,Err:" + err.Error() return } if item == nil { br.Msg = "分类不存在" return } err = models.DeleteClassify(req.ClassifyId) if err != nil { br.Msg = "删除失败" br.ErrMsg = "删除失败,Err:" + err.Error() return } br.Ret = 200 br.Success = true br.Msg = "删除成功" } // @Title 修改分类接口 // @Description 修改分类 // @Param request body models.EditClassifyReq true "type json string" // @Success 200 Ret=200,修改成功 // @router /edit [post] func (this *ClassifyController) Edit() { br := new(models.BaseResponse).Init() defer func() { this.Data["json"] = br this.ServeJSON() }() var req models.EditClassifyReq err := json.Unmarshal(this.Ctx.Input.RequestBody, &req) if err != nil { br.Msg = "参数解析异常!" br.ErrMsg = "参数解析失败,Err:" + err.Error() return } if req.ClassifyId <= 0 { br.Msg = "参数错误" return } item, err := models.GetClassifyByName(req.ClassifyName, req.ParentId) if err != nil && err.Error() != utils.ErrNoRow() { br.Msg = "获取信息失败" br.ErrMsg = "获取信息失败,Err:" + err.Error() return } if item != nil && item.Id != req.ClassifyId { br.Msg = "分类名称:" + req.ClassifyName + "已存在" return } err = models.EditClassify(req.ClassifyName, req.Abstract, req.Descript, req.ParentId, req.ClassifyId) if err != nil { br.Msg = "修改失败" br.ErrMsg = "修改失败,Err:" + err.Error() return } br.Ret = 200 br.Success = true br.Msg = "修改成功" } // @Title 获取父级分类接口 // @Description 获取父级分类 // @Success 200 {object} models.Classify // @router /parent [get] func (this *ClassifyController) ParentClassify() { br := new(models.BaseResponse).Init() defer func() { this.Data["json"] = br this.ServeJSON() }() items, err := models.ParentClassify() if err != nil { br.Msg = "获取失败" br.ErrMsg = "获取失败,Err:" + err.Error() return } br.Data = items br.Ret = 200 br.Success = true br.Msg = "获取成功" } // @Title 根据id获取分类详情接口 // @Description 根据id获取分类详情 // @Param request body models.FindByIdClassifyReq true "type json string" // @Success 200 {object} models.Classify // @router /findById [get] func (this *ClassifyController) FindByIdClassify() { br := new(models.BaseResponse).Init() defer func() { this.Data["json"] = br this.ServeJSON() }() var req models.FindByIdClassifyReq err := json.Unmarshal(this.Ctx.Input.RequestBody, &req) if err != nil { br.Msg = "参数解析异常!" br.ErrMsg = "参数解析失败,Err:" + err.Error() return } items, err := models.FindByIdClassify(req.ClassifyId) if err != nil { br.Msg = "获取失败" br.ErrMsg = "获取失败,Err:" + err.Error() return } br.Data = items br.Ret = 200 br.Success = true br.Msg = "获取成功" } // @Title 获取分类列表 // @Description 获取分类列表 // @Param PageSize query int true "每页数据条数" // @Param CurrentIndex query int true "当前页页码,从1开始" // @Param KeyWord query string true "检索关键词" // @Success 200 {object} models.Classify // @router /list [get] func (this *ClassifyController) ListClassify() { br := new(models.BaseResponse).Init() defer func() { this.Data["json"] = br this.ServeJSON() }() pageSize, _ := this.GetInt("PageSize") currentIndex, _ := this.GetInt("CurrentIndex") keyWord := this.GetString("KeyWord") var startSize int if pageSize <= 0 { pageSize = utils.PageSize20 } if currentIndex <= 0 { currentIndex = 1 } startSize = utils.StartIndex(currentIndex, pageSize) list, err := models.GetClassifyList(startSize, pageSize, keyWord) if err != nil { br.Msg = "获取失败" br.ErrMsg = "获取失败,Err:" + err.Error() return } total, err := models.GetClassifyListCount(keyWord) if err != nil { br.Msg = "获取失败" br.ErrMsg = "获取失败,Err:" + err.Error() return } for _, v := range list { child, err := models.GetClassifyChild(v.Id, keyWord) if err != nil { br.Msg = "获取信息失败" br.ErrMsg = "获取失败,Err:" + err.Error() return } v.Child = child } page := models.GetPaging(currentIndex, pageSize, total) resp := new(models.ClassifyListResp) resp.List = list resp.Paging = page br.Data = resp br.Ret = 200 br.Success = true br.Msg = "获取成功" }