123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202 |
- package system
- import (
- "github.com/gin-gonic/gin"
- "github.com/go-playground/validator/v10"
- "hongze/fms_api/controller/resp"
- "hongze/fms_api/global"
- "hongze/fms_api/models/base"
- "hongze/fms_api/models/system"
- systemService "hongze/fms_api/services/system"
- "hongze/fms_api/utils"
- )
- type SysDeptController struct {
- }
- // List 部门列表
- func (s *SysDeptController) List(c *gin.Context) {
- req := new(system.DeptListReq)
- err := c.BindQuery(&req)
- if err != nil {
- resp.FailData("参数解析失败", "Err:"+err.Error(), c)
- return
- }
- page := new(base.Page)
- page.SetPageSize(req.PageSize)
- page.SetCurrent(req.Current)
- count, list, err, _ := systemService.DeptTreeList(page, req)
- if err != nil {
- resp.FailMsg("获取失败", "获取失败,Err:"+err.Error(), c)
- return
- }
- page.SetTotal(count)
- resultPage := &base.BaseData{
- Page: page,
- List: list,
- }
- resp.OkData("获取成功", resultPage, c)
- }
- // Add 新增部门
- func (s *SysDeptController) Add(c *gin.Context) {
- req := new(system.SysDeptAddReq)
- err := c.ShouldBind(&req)
- if err != nil {
- errs, ok := err.(validator.ValidationErrors)
- if !ok {
- resp.FailData("参数解析失败", "Err:"+err.Error(), c)
- return
- }
- resp.FailData("参数解析失败", errs.Translate(global.Trans), c)
- return
- }
- item := new(system.SysDept)
- item.ParentId = req.ParentId
- item.DeptName = req.DeptName
- item.Sort = req.Sort
- if req.ParentId > 0 {
- parentInfo, tErr := item.GetDeptByDeptId(req.ParentId)
- if tErr != nil {
- if tErr == utils.ErrNoRow {
- resp.Fail("上级部门不存在", c)
- return
- }
- resp.FailMsg("查询上级部门出错", tErr.Error(), c)
- return
- }
- if parentInfo.RootId == 0 {
- item.RootId = parentInfo.DeptId
- } else {
- item.RootId = parentInfo.RootId
- }
- item.Level = parentInfo.Level + 1
- }
- // 判断是否存在相同的部门名称
- cond := "parent_id = ? and dept_name = ?"
- pars := make([]interface{}, 0)
- pars = append(pars, item.ParentId, item.DeptName)
- exitList, err := item.GetDeptListByCondition(cond, pars, "")
- if err != nil {
- resp.FailMsg("查询部门出错", err.Error(), c)
- return
- }
- if len(exitList) > 0 {
- resp.Fail("该部门名称已存在", c)
- return
- }
- err = item.Add()
- if err != nil {
- resp.FailMsg("保存失败", "保存失败,Err:"+err.Error(), c)
- return
- }
- resp.Ok("保存成功", c)
- }
- // Edit 编辑部门
- func (s *SysDeptController) Edit(c *gin.Context) {
- req := new(system.SysDeptEditReq)
- err := c.ShouldBind(&req)
- if err != nil {
- resp.FailData("参数解析失败", "Err:"+err.Error(), c)
- return
- }
- item := new(system.SysDept)
- item.DeptId = req.DeptId
- item.ParentId = req.ParentId
- item.DeptName = req.DeptName
- item.Sort = req.Sort
- //查询当前部门是否已存在
- deptInfo, err := item.GetDeptByDeptId(req.DeptId)
- if err != nil {
- if err == utils.ErrNoRow {
- resp.Fail("该部门不存在", c)
- return
- }
- resp.FailMsg("查询部门出错", err.Error(), c)
- return
- }
- updateCols := make([]string, 0)
- if deptInfo.ParentId > 0 && deptInfo.ParentId != item.ParentId {
- parentInfo, tErr := item.GetDeptByDeptId(req.ParentId)
- if tErr != nil {
- if tErr == utils.ErrNoRow {
- resp.Fail("上级部门不存在", c)
- return
- }
- resp.FailMsg("查询上级部门出错", tErr.Error(), c)
- return
- }
- if parentInfo.RootId == 0 {
- item.RootId = parentInfo.DeptId
- } else {
- item.RootId = parentInfo.RootId
- }
- item.Level = parentInfo.Level + 1
- updateCols = append(updateCols, "parent_id", "root_id", "level")
- } else if req.ParentId == 0 && deptInfo.ParentId != item.ParentId {
- item.RootId = 0
- item.Level = 0
- updateCols = append(updateCols, "parent_id", "root_id", "level")
- }
- if deptInfo.Sort != item.Sort {
- updateCols = append(updateCols, "sort")
- }
- if deptInfo.DeptName != item.DeptName {
- cond := "parent_id = ? and dept_name = ?"
- pars := make([]interface{}, 0)
- pars = append(pars, item.ParentId, item.DeptName)
- exitList, err := item.GetDeptListByCondition(cond, pars, "")
- if err != nil {
- resp.FailMsg("查询部门出错", err.Error(), c)
- return
- }
- if len(exitList) > 0 {
- resp.Fail("该部门名称已存在", c)
- return
- }
- updateCols = append(updateCols, "dept_name")
- }
- if len(updateCols) == 0 {
- resp.Ok("保存成功", c)
- return
- }
- err = item.Update(updateCols)
- if err != nil {
- resp.FailMsg("保存失败", "保存失败,Err:"+err.Error(), c)
- return
- }
- resp.Ok("保存成功", c)
- }
- // Delete 删除部门
- func (s *SysDeptController) Delete(c *gin.Context) {
- req := new(system.DelSysDeptReq)
- err := c.ShouldBind(&req)
- if err != nil {
- resp.FailData("参数解析失败", "Err:"+err.Error(), c)
- return
- }
- err, errMsg := systemService.DeleteDept(req)
- if err != nil {
- resp.FailMsg(errMsg, err.Error(), c)
- return
- }
- resp.Ok("删除成功", c)
- }
- // AdminList 部门列表
- func (s *SysDeptController) AdminList(c *gin.Context) {
- list, err, errMsg := systemService.DeptAdminTreeList()
- if err != nil {
- resp.FailMsg(errMsg, err.Error(), c)
- return
- }
- resultPage := &base.BaseData{
- List: list,
- }
- resp.OkData("获取成功", resultPage, c)
- }
|