diff --git a/app/admin/apis/system/dept.go b/app/admin/apis/system/dept.go index fb33bfae..d3c2f10c 100644 --- a/app/admin/apis/system/dept.go +++ b/app/admin/apis/system/dept.go @@ -20,7 +20,7 @@ import ( // @Router /api/v1/deptList [get] // @Security Bearer func GetDeptList(c *gin.Context) { - var Dept models.SysDept + var Dept models.SysDepts Dept.DeptName = c.Request.FormValue("deptName") Dept.Status = c.Request.FormValue("status") Dept.DeptId, _ = tools.StringToInt(c.Request.FormValue("deptId")) @@ -31,7 +31,7 @@ func GetDeptList(c *gin.Context) { } func GetDeptTree(c *gin.Context) { - var Dept models.SysDept + var Dept models.SysDepts Dept.DeptName = c.Request.FormValue("deptName") Dept.Status = c.Request.FormValue("status") Dept.DeptId, _ = tools.StringToInt(c.Request.FormValue("deptId")) @@ -49,7 +49,7 @@ func GetDeptTree(c *gin.Context) { // @Router /api/v1/dept/{deptId} [get] // @Security Bearer func GetDept(c *gin.Context) { - var Dept models.SysDept + var Dept models.SysDepts Dept.DeptId, _ = tools.StringToInt(c.Param("deptId")) Dept.DataScope = tools.GetUserIdStr(c) result, err := Dept.Get() @@ -68,7 +68,7 @@ func GetDept(c *gin.Context) { // @Router /api/v1/dept [post] // @Security Bearer func InsertDept(c *gin.Context) { - var data models.SysDept + var data models.SysDepts err := c.BindWith(&data, binding.JSON) tools.HasError(err, "", 500) data.CreateBy = tools.GetUserIdStr(c) @@ -89,7 +89,7 @@ func InsertDept(c *gin.Context) { // @Router /api/v1/dept [put] // @Security Bearer func UpdateDept(c *gin.Context) { - var data models.SysDept + var data models.SysDepts err := c.BindJSON(&data) tools.HasError(err, "", -1) data.UpdateBy = tools.GetUserIdStr(c) @@ -106,7 +106,7 @@ func UpdateDept(c *gin.Context) { // @Success 200 {string} string "{"code": -1, "message": "删除失败"}" // @Router /api/v1/dept/{id} [delete] func DeleteDept(c *gin.Context) { - var data models.SysDept + var data models.SysDepts id, err := tools.StringToInt(c.Param("id")) _, err = data.Delete(id) tools.HasError(err, "删除失败", 500) @@ -114,7 +114,7 @@ func DeleteDept(c *gin.Context) { } func GetDeptTreeRoleselect(c *gin.Context) { - var Dept models.SysDept + var Dept models.SysDepts var SysRole models.SysRole id, err := tools.StringToInt(c.Param("roleId")) SysRole.RoleId = id diff --git a/app/admin/apis/system/sys_dept/sys_dept.go b/app/admin/apis/system/sys_dept/sys_dept.go new file mode 100644 index 00000000..2e2b3602 --- /dev/null +++ b/app/admin/apis/system/sys_dept/sys_dept.go @@ -0,0 +1,174 @@ +package sys_dept + +import ( + "github.com/gin-gonic/gin" + "go-admin/app/admin/models" + + "go-admin/app/admin/service" + "go-admin/app/admin/service/dto" + "go-admin/common/apis" + "go-admin/common/log" + "go-admin/tools" + + "net/http" +) + +type SysDept struct { + apis.Api +} + +func (e *SysDept) GetSysDeptList(c *gin.Context) { + msgID := tools.GenerateMsgIDFromContext(c) + d := new(dto.SysDeptSearch) + db, err := tools.GetOrm(c) + if err != nil { + log.Error(err) + return + } + + //查询列表 + err = d.Bind(c) + if err != nil { + e.Error(c, http.StatusUnprocessableEntity, err, "参数验证失败") + return + } + + list := make([]models.SysDept, 0) + var count int64 + serviceStudent := service.SysDept{} + serviceStudent.MsgID = msgID + serviceStudent.Orm = db + err = serviceStudent.GetSysDeptPage(d, &list, &count) + if err != nil { + e.Error(c, http.StatusUnprocessableEntity, err, "查询失败") + return + } + + e.PageOK(c, list, int(count), d.GetPageIndex(), d.GetPageSize(), "查询成功") +} + +func (e *SysDept) GetSysDept(c *gin.Context) { + control := new(dto.SysDeptById) + db, err := tools.GetOrm(c) + if err != nil { + log.Error(err) + return + } + + msgID := tools.GenerateMsgIDFromContext(c) + //查看详情 + err = control.Bind(c) + if err != nil { + e.Error(c, http.StatusUnprocessableEntity, err, "参数验证失败") + return + } + var object models.SysDept + + serviceSysOperlog := service.SysDept{} + serviceSysOperlog.MsgID = msgID + serviceSysOperlog.Orm = db + err = serviceSysOperlog.GetSysDept(control, &object) + if err != nil { + e.Error(c, http.StatusUnprocessableEntity, err, "查询失败") + return + } + + e.OK(c, object, "查看成功") +} + +func (e *SysDept) InsertSysDept(c *gin.Context) { + control := new(dto.SysDeptControl) + db, err := tools.GetOrm(c) + if err != nil { + log.Error(err) + return + } + + msgID := tools.GenerateMsgIDFromContext(c) + //新增操作 + err = control.Bind(c) + if err != nil { + e.Error(c, http.StatusUnprocessableEntity, err, "参数验证失败") + return + } + object, err := control.Generate() + if err != nil { + e.Error(c, http.StatusInternalServerError, err, "模型生成失败") + return + } + // 设置创建人 + object.SetCreateBy(tools.GetUserId(c)) + + serviceSysDept := service.SysDept{} + serviceSysDept.Orm = db + serviceSysDept.MsgID = msgID + err = serviceSysDept.InsertSysDept(object) + if err != nil { + log.Error(err) + e.Error(c, http.StatusInternalServerError, err, "创建失败") + return + } + + e.OK(c, object.GetId(), "创建成功") +} + +func (e *SysDept) UpdateSysDept(c *gin.Context) { + control := new(dto.SysDeptControl) + db, err := tools.GetOrm(c) + if err != nil { + log.Error(err) + return + } + + msgID := tools.GenerateMsgIDFromContext(c) + //更新操作 + err = control.Bind(c) + if err != nil { + e.Error(c, http.StatusUnprocessableEntity, err, "参数验证失败") + return + } + object, err := control.Generate() + if err != nil { + e.Error(c, http.StatusInternalServerError, err, "模型生成失败") + return + } + object.SetUpdateBy(tools.GetUserId(c)) + + serviceSysDept := service.SysDept{} + serviceSysDept.Orm = db + serviceSysDept.MsgID = msgID + err = serviceSysDept.UpdateSysDept(object) + if err != nil { + log.Error(err) + return + } + e.OK(c, object.GetId(), "更新成功") +} + +func (e *SysDept) DeleteSysDept(c *gin.Context) { + control := new(dto.SysDeptById) + db, err := tools.GetOrm(c) + if err != nil { + log.Error(err) + return + } + + msgID := tools.GenerateMsgIDFromContext(c) + //删除操作 + err = control.Bind(c) + if err != nil { + log.Errorf("MsgID[%s] Bind error: %s", msgID, err) + e.Error(c, http.StatusUnprocessableEntity, err, "参数验证失败") + return + } + + serviceSysDept := service.SysDept{} + serviceSysDept.Orm = db + serviceSysDept.MsgID = msgID + err = serviceSysDept.RemoveSysDept(control) + if err != nil { + log.Error(err) + return + } + e.OK(c, control.GetId(), "删除成功") +} diff --git a/app/admin/apis/system/sysuser.go b/app/admin/apis/system/sysuser.go index 9748ef5b..c9cf2298 100644 --- a/app/admin/apis/system/sysuser.go +++ b/app/admin/apis/system/sysuser.go @@ -98,7 +98,7 @@ func GetSysUserProfile(c *gin.Context) { tools.HasError(err, "抱歉未找到相关信息", -1) var SysRole models.SysRole var Post models.Post - var Dept models.SysDept + var Dept models.SysDepts //获取角色列表 roles, err := SysRole.GetList() //获取职位列表 diff --git a/app/admin/models/dept.go b/app/admin/models/dept.go index cb756989..8e31d7ca 100644 --- a/app/admin/models/dept.go +++ b/app/admin/models/dept.go @@ -8,7 +8,7 @@ import ( "go-admin/tools" ) -type SysDept struct { +type SysDepts struct { DeptId int `json:"deptId" gorm:"primary_key;auto_increment;"` //部门编码 ParentId int `json:"parentId" gorm:""` //上级部门 DeptPath string `json:"deptPath" gorm:"size:255;"` // @@ -24,10 +24,10 @@ type SysDept struct { DataScope string `json:"dataScope" gorm:"-"` Params string `json:"params" gorm:"-"` - Children []SysDept `json:"children" gorm:"-"` + Children []SysDepts `json:"children" gorm:"-"` } -func (SysDept) TableName() string { +func (SysDepts) TableName() string { return "sys_dept" } @@ -37,8 +37,8 @@ type DeptLable struct { Children []DeptLable `gorm:"-" json:"children"` } -func (e *SysDept) Create() (SysDept, error) { - var doc SysDept +func (e *SysDepts) Create() (SysDepts, error) { + var doc SysDepts result := orm.Eloquent.Table(e.TableName()).Create(&e) if result.Error != nil { err := result.Error @@ -46,7 +46,7 @@ func (e *SysDept) Create() (SysDept, error) { } deptPath := "/" + tools.IntToString(e.DeptId) if int(e.ParentId) != 0 { - var deptP SysDept + var deptP SysDepts orm.Eloquent.Table(e.TableName()).Where("dept_id = ?", e.ParentId).First(&deptP) deptPath = deptP.DeptPath + deptPath } else { @@ -63,8 +63,8 @@ func (e *SysDept) Create() (SysDept, error) { return doc, nil } -func (e *SysDept) Get() (SysDept, error) { - var doc SysDept +func (e *SysDepts) Get() (SysDepts, error) { + var doc SysDepts table := orm.Eloquent.Table(e.TableName()) if e.DeptId != 0 { @@ -80,8 +80,8 @@ func (e *SysDept) Get() (SysDept, error) { return doc, nil } -func (e *SysDept) GetList() ([]SysDept, error) { - var doc []SysDept +func (e *SysDepts) GetList() ([]SysDepts, error) { + var doc []SysDepts table := orm.Eloquent.Table(e.TableName()) if e.DeptId != 0 { @@ -100,8 +100,8 @@ func (e *SysDept) GetList() ([]SysDept, error) { return doc, nil } -func (e *SysDept) GetPage(bl bool) ([]SysDept, error) { - var doc []SysDept +func (e *SysDepts) GetPage(bl bool) ([]SysDepts, error) { + var doc []SysDepts table := orm.Eloquent.Table(e.TableName()) if e.DeptId != 0 { @@ -133,10 +133,10 @@ func (e *SysDept) GetPage(bl bool) ([]SysDept, error) { return doc, nil } -func (e *SysDept) SetDept(bl bool) ([]SysDept, error) { +func (e *SysDepts) SetDept(bl bool) ([]SysDepts, error) { list, err := e.GetPage(bl) - m := make([]SysDept, 0) + m := make([]SysDepts, 0) for i := 0; i < len(list); i++ { if list[i].ParentId != 0 { continue @@ -148,16 +148,16 @@ func (e *SysDept) SetDept(bl bool) ([]SysDept, error) { return m, err } -func Digui(deptlist *[]SysDept, menu SysDept) SysDept { +func Digui(deptlist *[]SysDepts, menu SysDepts) SysDepts { list := *deptlist - min := make([]SysDept, 0) + min := make([]SysDepts, 0) for j := 0; j < len(list); j++ { if menu.DeptId != list[j].ParentId { continue } - mi := SysDept{} + mi := SysDepts{} mi.DeptId = list[j].DeptId mi.ParentId = list[j].ParentId mi.DeptPath = list[j].DeptPath @@ -168,7 +168,7 @@ func Digui(deptlist *[]SysDept, menu SysDept) SysDept { mi.Email = list[j].Email mi.Status = list[j].Status mi.CreatedAt = list[j].CreatedAt - mi.Children = []SysDept{} + mi.Children = []SysDepts{} ms := Digui(deptlist, mi) min = append(min, ms) @@ -177,14 +177,14 @@ func Digui(deptlist *[]SysDept, menu SysDept) SysDept { return menu } -func (e *SysDept) Update(id int) (update SysDept, err error) { +func (e *SysDepts) Update(id int) (update SysDepts, err error) { if err = orm.Eloquent.Table(e.TableName()).Where("dept_id = ?", id).First(&update).Error; err != nil { return } deptPath := "/" + tools.IntToString(e.DeptId) if int(e.ParentId) != 0 { - var deptP SysDept + var deptP SysDepts orm.Eloquent.Table(e.TableName()).Where("dept_id = ?", e.ParentId).First(&deptP) deptPath = deptP.DeptPath + deptPath } else { @@ -206,7 +206,7 @@ func (e *SysDept) Update(id int) (update SysDept, err error) { return } -func (e *SysDept) Delete(id int) (success bool, err error) { +func (e *SysDepts) Delete(id int) (success bool, err error) { user := SysUser{} user.DeptId = id @@ -226,7 +226,7 @@ func (e *SysDept) Delete(id int) (success bool, err error) { return } - if err = tx.Table(e.TableName()).Where("dept_id = ?", id).Delete(&SysDept{}).Error; err != nil { + if err = tx.Table(e.TableName()).Where("dept_id = ?", id).Delete(&SysDepts{}).Error; err != nil { success = false tx.Rollback() return @@ -240,7 +240,7 @@ func (e *SysDept) Delete(id int) (success bool, err error) { return } -func (dept *SysDept) SetDeptLable() (m []DeptLable, err error) { +func (dept *SysDepts) SetDeptLable() (m []DeptLable, err error) { deptlist, err := dept.GetList() m = make([]DeptLable, 0) @@ -258,7 +258,7 @@ func (dept *SysDept) SetDeptLable() (m []DeptLable, err error) { return } -func DiguiDeptLable(deptlist *[]SysDept, dept DeptLable) DeptLable { +func DiguiDeptLable(deptlist *[]SysDepts, dept DeptLable) DeptLable { list := *deptlist min := make([]DeptLable, 0) diff --git a/app/admin/models/sys_dept.go b/app/admin/models/sys_dept.go new file mode 100644 index 00000000..fbd2f2d3 --- /dev/null +++ b/app/admin/models/sys_dept.go @@ -0,0 +1,33 @@ +package models + +import "go-admin/common/models" + +type SysDept struct { + DeptId int `json:"deptId" gorm:"primary_key;auto_increment;"` //部门编码 + ParentId int `json:"parentId" gorm:""` //上级部门 + DeptPath string `json:"deptPath" gorm:"size:255;"` // + DeptName string `json:"deptName" gorm:"size:128;"` //部门名称 + Sort int `json:"sort" gorm:""` //排序 + Leader string `json:"leader" gorm:"size:128;"` //负责人 + Phone string `json:"phone" gorm:"size:11;"` //手机 + Email string `json:"email" gorm:"size:64;"` //邮箱 + Status string `json:"status" gorm:"size:4;"` //状态 + models.ControlBy + models.ModelTime + DataScope string `json:"dataScope" gorm:"-"` + Params string `json:"params" gorm:"-"` + Children []SysDept `json:"children" gorm:"-"` +} + +func (SysDept) TableName() string { + return "sys_dept" +} + +func (e *SysDept) Generate() models.ActiveRecord { + o := *e + return &o +} + +func (e *SysDept) GetId() interface{} { + return e.DeptId +} diff --git a/app/admin/models/sys_meun.go b/app/admin/models/sys_meun.go new file mode 100644 index 00000000..8adeca4c --- /dev/null +++ b/app/admin/models/sys_meun.go @@ -0,0 +1,64 @@ +package models + +import "go-admin/common/models" + +type SysMenu struct { + MenuId int `json:"menuId" gorm:"primary_key;AUTO_INCREMENT"` + MenuName string `json:"menuName" gorm:"size:128;"` + Title string `json:"title" gorm:"size:128;"` + Icon string `json:"icon" gorm:"size:128;"` + Path string `json:"path" gorm:"size:128;"` + Paths string `json:"paths" gorm:"size:128;"` + MenuType string `json:"menuType" gorm:"size:1;"` + Action string `json:"action" gorm:"size:16;"` + Permission string `json:"permission" gorm:"size:255;"` + ParentId int `json:"parentId" gorm:"size:11;"` + NoCache bool `json:"noCache" gorm:"size:8;"` + Breadcrumb string `json:"breadcrumb" gorm:"size:255;"` + Component string `json:"component" gorm:"size:255;"` + Sort int `json:"sort" gorm:"size:4;"` + Visible string `json:"visible" gorm:"size:1;"` + IsFrame string `json:"isFrame" gorm:"size:1;DEFAULT:0;"` + models.ControlBy + models.ModelTime + DataScope string `json:"dataScope" gorm:"-"` + Params string `json:"params" gorm:"-"` + RoleId int `gorm:"-"` + Children []SysMenu `json:"children" gorm:"-"` + IsSelect bool `json:"is_select" gorm:"-"` +} + +type SysMenus struct { + MenuId int `json:"menuId" gorm:"column:menu_id;primary_key;"` + MenuName string `json:"menuName" gorm:"column:menu_name"` + Title string `json:"title" gorm:"column:title"` + Icon string `json:"icon" gorm:"column:icon"` + Path string `json:"path" gorm:"column:path"` + MenuType string `json:"menuType" gorm:"column:menu_type"` + Action string `json:"action" gorm:"column:action"` + Permission string `json:"permission" gorm:"column:permission"` + ParentId int `json:"parentId" gorm:"column:parent_id"` + NoCache bool `json:"noCache" gorm:"column:no_cache"` + Breadcrumb string `json:"breadcrumb" gorm:"column:breadcrumb"` + Component string `json:"component" gorm:"column:component"` + Sort int `json:"sort" gorm:"column:sort"` + Visible string `json:"visible" gorm:"column:visible"` + Children []SysMenu `json:"children" gorm:"-"` + models.ControlBy + models.ModelTime + DataScope string `json:"dataScope" gorm:"-"` + Params string `json:"params" gorm:"-"` +} + +func (SysMenu) TableName() string { + return "sys_menu" +} + +func (e *SysMenu) Generate() models.ActiveRecord { + o := *e + return &o +} + +func (e *SysMenu) GetId() interface{} { + return e.MenuId +} diff --git a/app/admin/service/dto/sys_dept.go b/app/admin/service/dto/sys_dept.go new file mode 100644 index 00000000..1d48de80 --- /dev/null +++ b/app/admin/service/dto/sys_dept.go @@ -0,0 +1,131 @@ +package dto + +import ( + "encoding/json" + "github.com/gin-gonic/gin" + "github.com/gin-gonic/gin/binding" + "go-admin/app/admin/models" + "go-admin/common/dto" + "go-admin/common/log" + "go-admin/tools" +) + +// SysConfigSearch 列表或者搜索使用结构体 +type SysDeptSearch struct { + dto.Pagination `search:"-"` + ParentId int `form:"parentId" search:"type:exact;column:parent_id;table:sys_dept" comment:"上级部门"` //上级部门 + DeptPath string `form:"deptPath" search:"type:exact;column:dept_path;table:sys_dept" comment:""` //路径 + DeptName string `form:"deptName" search:"type:exact;column:dept_name;table:sys_dept" comment:"部门名称"` //部门名称 + Sort int `form:"sort" search:"type:exact;column:sort;table:sys_dept" comment:"排序"` //排序 + Leader string `form:"leader" search:"type:exact;column:leader;table:sys_dept" comment:"负责人"` //负责人 + Phone string `form:"phone" search:"type:exact;column:phone;table:sys_dept" comment:"手机"` //手机 + Email string `form:"email" search:"type:exact;column:email;table:sys_dept" comment:"邮箱"` //邮箱 + Status string `form:"status" search:"type:exact;column:status;table:sys_dept" comment:"状态"` //状态 +} + +func (m *SysDeptSearch) GetNeedSearch() interface{} { + return *m +} + +// Bind 映射上下文中的结构体数据 +func (m *SysDeptSearch) Bind(ctx *gin.Context) error { + msgID := tools.GenerateMsgIDFromContext(ctx) + err := ctx.ShouldBind(m) + if err != nil { + log.Debugf("MsgID[%s] ShouldBind error: %s", msgID, err.Error()) + } + return err +} + +// SysConfigControl 增、改使用的结构体 +type SysDeptControl struct { + DeptId int `uri:"id" comment:"编码"` // 编码 + ParentId int `form:"parentId" comment:"上级部门"` //上级部门 + DeptPath string `form:"deptPath" comment:""` //路径 + DeptName string `form:"deptName" comment:"部门名称"` //部门名称 + Sort int `form:"sort" comment:"排序"` //排序 + Leader string `form:"leader" comment:"负责人"` //负责人 + Phone string `form:"phone" comment:"手机"` //手机 + Email string `form:"email" comment:"邮箱"` //邮箱 + Status string `form:"status" comment:"状态"` //状态 +} + +// Bind 映射上下文中的结构体数据 +func (s *SysDeptControl) Bind(ctx *gin.Context) error { + msgID := tools.GenerateMsgIDFromContext(ctx) + err := ctx.ShouldBindUri(s) + if err != nil { + log.Debugf("MsgID[%s] ShouldBindUri error: %s", msgID, err.Error()) + return err + } + err = ctx.ShouldBindBodyWith(s, binding.JSON) + if err != nil { + log.Debugf("MsgID[%s] ShouldBind error: %#v", msgID, err.Error()) + } + var jsonStr []byte + jsonStr, err = json.Marshal(s) + if err != nil { + log.Debugf("MsgID[%s] ShouldBind error: %#v", msgID, err.Error()) + } + ctx.Set("body", string(jsonStr)) + return err +} + +// Generate 结构体数据转化 从 SysConfigControl 至 system.SysConfig 对应的模型 +func (s *SysDeptControl) Generate() (*models.SysDept, error) { + return &models.SysDept{ + DeptId: s.DeptId, + DeptName: s.DeptName, + ParentId: s.ParentId, + DeptPath: s.DeptPath, + Sort: s.Sort, + Leader: s.Leader, + Phone: s.Phone, + Email: s.Email, + Status: s.Status, + }, nil +} + +// GetId 获取数据对应的ID +func (s *SysDeptControl) GetId() interface{} { + return s.DeptId +} + +// SysConfigById 获取单个或者删除的结构体 +type SysDeptById struct { + Id int `uri:"id"` + Ids []int `json:"ids"` +} + +func (s *SysDeptById) Generate() *SysDeptById { + cp := *s + return &cp +} + +func (s *SysDeptById) GetId() interface{} { + return s.Id +} + +func (s *SysDeptById) Bind(ctx *gin.Context) error { + msgID := tools.GenerateMsgIDFromContext(ctx) + err := ctx.ShouldBindUri(s) + if err != nil { + log.Debugf("MsgID[%s] ShouldBindUri error: %s", msgID, err.Error()) + return err + } + err = ctx.ShouldBind(s) + if err != nil { + log.Debugf("MsgID[%s] ShouldBind error: %#v", msgID, err.Error()) + } + return err +} + +func (s *SysDeptById) GenerateM() (*models.SysDept, error) { + return &models.SysDept{}, nil +} + +type DeptLabel struct { + Id int `gorm:"-" json:"id"` + Label string `gorm:"-" json:"label"` + Children []DeptLabel `gorm:"-" json:"children"` +} \ No newline at end of file diff --git a/app/admin/service/dto/sys_menu.go b/app/admin/service/dto/sys_menu.go new file mode 100644 index 00000000..46e5cd89 --- /dev/null +++ b/app/admin/service/dto/sys_menu.go @@ -0,0 +1,150 @@ +package dto + +import ( + "encoding/json" + "github.com/gin-gonic/gin" + "github.com/gin-gonic/gin/binding" + "go-admin/app/admin/models" + "go-admin/common/dto" + "go-admin/common/log" + "go-admin/tools" +) + +// SysMenuSearch 列表或者搜索使用结构体 +type SysMenuSearch struct { + dto.Pagination `search:"-"` + ParentId int `form:"parentId" search:"type:exact;column:parent_id;table:sys_dept" comment:"上级部门"` //上级部门 + DeptPath string `form:"deptPath" search:"type:exact;column:dept_path;table:sys_dept" comment:""` //路径 + DeptName string `form:"deptName" search:"type:exact;column:dept_name;table:sys_dept" comment:"部门名称"` //部门名称 + Sort int `form:"sort" search:"type:exact;column:sort;table:sys_dept" comment:"排序"` //排序 + Leader string `form:"leader" search:"type:exact;column:leader;table:sys_dept" comment:"负责人"` //负责人 + Phone string `form:"phone" search:"type:exact;column:phone;table:sys_dept" comment:"手机"` //手机 + Email string `form:"email" search:"type:exact;column:email;table:sys_dept" comment:"邮箱"` //邮箱 + Status string `form:"status" search:"type:exact;column:status;table:sys_dept" comment:"状态"` //状态 +} + +func (m *SysMenuSearch) GetNeedSearch() interface{} { + return *m +} + +// Bind 映射上下文中的结构体数据 +func (m *SysMenuSearch) Bind(ctx *gin.Context) error { + msgID := tools.GenerateMsgIDFromContext(ctx) + err := ctx.ShouldBind(m) + if err != nil { + log.Debugf("MsgID[%s] ShouldBind error: %s", msgID, err.Error()) + } + return err +} + +// SysConfigControl 增、改使用的结构体 +type SysMenuControl struct { + MenuId int `uri:"id" comment:"编码"` // 编码 + MenuName string `form:"menuName" comment:"菜单name"` //菜单name + Title string `form:"title" comment:"显示名称"` //显示名称 + Icon string `form:"icon" comment:"图标"` //图标 + Path string `form:"path" comment:"路径"` //路径 + Paths string `form:"paths" comment:"id路径"` //id路径 + MenuType string `form:"menuType" comment:"菜单类型"` //菜单类型 + Action string `form:"action" comment:"请求方式"` //请求方式 + Permission string `form:"permission" comment:"权限编码"` //权限编码 + ParentId int `form:"parentId" comment:"上级菜单"` //上级菜单 + NoCache bool `form:"noCache" comment:"是否缓存"` //是否缓存 + Breadcrumb string `form:"breadcrumb" comment:"是否面包屑"` //是否面包屑 + Component string `form:"component" comment:"组件"` //组件 + Sort int `form:"sort" comment:"排序"` //排序 + Visible string `form:"visible" comment:"是否显示"` //是否显示 + IsFrame string `form:"isFrame" comment:"是否frame"` //是否frame +} + +// Bind 映射上下文中的结构体数据 +func (s *SysMenuControl) Bind(ctx *gin.Context) error { + msgID := tools.GenerateMsgIDFromContext(ctx) + err := ctx.ShouldBindUri(s) + if err != nil { + log.Debugf("MsgID[%s] ShouldBindUri error: %s", msgID, err.Error()) + return err + } + err = ctx.ShouldBindBodyWith(s, binding.JSON) + if err != nil { + log.Debugf("MsgID[%s] ShouldBind error: %#v", msgID, err.Error()) + } + var jsonStr []byte + jsonStr, err = json.Marshal(s) + if err != nil { + log.Debugf("MsgID[%s] ShouldBind error: %#v", msgID, err.Error()) + } + ctx.Set("body", string(jsonStr)) + return err +} + +// Generate 结构体数据转化 从 SysConfigControl 至 system.SysConfig 对应的模型 +func (s *SysMenuControl) Generate() (*models.SysMenu, error) { + return &models.SysMenu{ + MenuId: s.MenuId, + MenuName: s.MenuName, + Title: s.Title, + Icon: s.Icon, + Path: s.Path, + Paths: s.Paths, + MenuType: s.MenuType, + Action: s.Action, + Permission: s.Permission, + ParentId: s.ParentId, + NoCache: s.NoCache, + Breadcrumb: s.Breadcrumb, + Component: s.Component, + Sort: s.Sort, + Visible: s.Visible, + IsFrame: s.IsFrame, + }, nil +} + +// GetId 获取数据对应的ID +func (s *SysMenuControl) GetId() interface{} { + return s.MenuId +} + +// SysConfigById 获取单个或者删除的结构体 +type SysMenuById struct { + Id int `uri:"id"` + Ids []int `json:"ids"` +} + +func (s *SysMenuById) Generate() *SysMenuById { + cp := *s + return &cp +} + +func (s *SysMenuById) GetId() interface{} { + return s.Id +} + +func (s *SysMenuById) Bind(ctx *gin.Context) error { + msgID := tools.GenerateMsgIDFromContext(ctx) + err := ctx.ShouldBindUri(s) + if err != nil { + log.Debugf("MsgID[%s] ShouldBindUri error: %s", msgID, err.Error()) + return err + } + err = ctx.ShouldBind(s) + if err != nil { + log.Debugf("MsgID[%s] ShouldBind error: %#v", msgID, err.Error()) + } + return err +} + +func (s *SysMenuById) GenerateM() (*models.SysMenu, error) { + return &models.SysMenu{}, nil +} + +type MenuLabel struct { + Id int `json:"id" gorm:"-"` + Label string `json:"label" gorm:"-"` + Children []MenuLabel `json:"children" gorm:"-"` +} + +type MenuRole struct { + models.SysMenus + IsSelect bool `json:"is_select" gorm:"-"` +} diff --git a/app/admin/service/sys_dept.go b/app/admin/service/sys_dept.go new file mode 100644 index 00000000..7cf85b16 --- /dev/null +++ b/app/admin/service/sys_dept.go @@ -0,0 +1,164 @@ +package service + +import ( + "errors" + "go-admin/app/admin/models" + "go-admin/app/admin/service/dto" + cDto "go-admin/common/dto" + "go-admin/common/log" + "go-admin/common/service" + "gorm.io/gorm" +) + +type SysDept struct { + service.Service +} + +// GetSysDeptPage 获取SysDept列表 +func (e *SysDept) GetSysDeptPage(c *dto.SysDeptSearch, list *[]models.SysDept, count *int64) error { + var err error + var data models.SysDept + msgID := e.MsgID + + err = e.Orm.Model(&data). + Scopes( + cDto.MakeCondition(c.GetNeedSearch()), + cDto.Paginate(c.GetPageSize(), c.GetPageIndex()), + ). + Find(list).Limit(-1).Offset(-1). + Count(count).Error + if err != nil { + log.Errorf("msgID[%s] db error:%s", msgID, err) + return err + } + return nil +} + +// GetSysDept 获取SysDept对象 +func (e *SysDept) GetSysDept(d *dto.SysDeptById, model *models.SysDept) error { + var err error + var data models.SysDept + msgID := e.MsgID + + db := e.Orm.Model(&data). + First(model, d.GetId()) + err = db.Error + if err != nil && errors.Is(err, gorm.ErrRecordNotFound) { + err = errors.New("查看对象不存在或无权查看") + log.Errorf("msgID[%s] db error:%s", msgID, err) + return err + } + if db.Error != nil { + log.Errorf("msgID[%s] db error:%s", msgID, err) + return err + } + return nil +} + +// InsertSysDept 创建SysDept对象 +func (e *SysDept) InsertSysDept(model *models.SysDept) error { + var err error + var data models.SysDept + msgID := e.MsgID + + err = e.Orm.Model(&data). + Create(model).Error + if err != nil { + log.Errorf("msgID[%s] db error:%s", msgID, err) + return err + } + return nil +} + +// UpdateSysDept 修改SysDept对象 +func (e *SysDept) UpdateSysDept(c *models.SysDept) error { + var err error + var data models.SysDept + msgID := e.MsgID + + db := e.Orm.Model(&data). + Where(c.GetId()).Updates(c) + if db.Error != nil { + log.Errorf("msgID[%s] db error:%s", msgID, err) + return err + } + if db.RowsAffected == 0 { + return errors.New("无权更新该数据") + + } + return nil +} + +// RemoveSysDept 删除SysDept +func (e *SysDept) RemoveSysDept(d *dto.SysDeptById) error { + var err error + var data models.SysDept + msgID := e.MsgID + + db := e.Orm.Model(&data). + Where(d.GetId()).Delete(&data) + if db.Error != nil { + err = db.Error + log.Errorf("MsgID[%s] Delete error: %s", msgID, err) + return err + } + if db.RowsAffected == 0 { + err = errors.New("无权删除该数据") + return err + } + return nil +} + +// GetSysDeptList 获取组织数据 +func (e *SysDept) GetSysDeptList(c *dto.SysDeptSearch, list *[]models.SysDept) error { + var err error + var data models.SysDept + msgID := e.MsgID + + err = e.Orm.Model(&data). + Scopes( + cDto.MakeCondition(c.GetNeedSearch()), + ). + Find(list).Error + if err != nil { + log.Errorf("msgID[%s] db error:%s", msgID, err) + return err + } + return nil +} + +// SetDeptTree 设置组织数据 +func (e *SysDept) SetDeptTree() (c *dto.SysDeptSearch, m []dto.DeptLabel, err error) { + var list []models.SysDept + err = e.GetSysDeptList(c, &list) + + m = make([]dto.DeptLabel, 0) + for i := 0; i < len(list); i++ { + if list[i].ParentId != 0 { + continue + } + e := dto.DeptLabel{} + e.Id = list[i].DeptId + e.Label = list[i].DeptName + deptsInfo := DeptCall(&list, e) + + m = append(m, deptsInfo) + } + return +} + +// Call 递归构造组织数据 +func DeptCall(deptlist *[]models.SysDept, dept dto.DeptLabel) dto.DeptLabel { + list := *deptlist + min := make([]dto.DeptLabel, 0) + for j := 0; j < len(list); j++ { + if dept.Id != list[j].ParentId { + continue + } + mi := dto.DeptLabel{Id: list[j].DeptId, Label: list[j].DeptName, Children: []dto.DeptLabel{}} + ms := DeptCall(deptlist, mi) + min = append(min, ms) + } + dept.Children = min + return dept +} diff --git a/app/admin/service/sys_menu.go b/app/admin/service/sys_menu.go new file mode 100644 index 00000000..f833bd41 --- /dev/null +++ b/app/admin/service/sys_menu.go @@ -0,0 +1,249 @@ +package service + +import ( + "errors" + "go-admin/app/admin/models" + "go-admin/app/admin/service/dto" + cDto "go-admin/common/dto" + "go-admin/common/log" + "go-admin/common/service" + "go-admin/tools" + "gorm.io/gorm" +) + +type SysMenu struct { + service.Service +} + +// GetSysMenuPage 获取SysMenu列表 +func (e *SysMenu) getSysMenuPage(c *dto.SysMenuSearch, list *[]models.SysMenu) error { + var err error + var data models.SysMenu + msgID := e.MsgID + + err = e.Orm.Model(&data). + Scopes( + cDto.MakeCondition(c.GetNeedSearch()), + ). + Find(list).Error + if err != nil { + log.Errorf("msgID[%s] db error:%s", msgID, err) + return err + } + return nil +} + +// GetSysMenu 获取SysMenu对象 +func (e *SysMenu) GetSysMenu(d *dto.SysMenuById, model *models.SysMenu) error { + var err error + var data models.SysMenu + msgID := e.MsgID + + db := e.Orm.Model(&data). + First(model, d.GetId()) + err = db.Error + if err != nil && errors.Is(err, gorm.ErrRecordNotFound) { + err = errors.New("查看对象不存在或无权查看") + log.Errorf("msgID[%s] db error:%s", msgID, err) + return err + } + if db.Error != nil { + log.Errorf("msgID[%s] db error:%s", msgID, err) + return err + } + return nil +} + +// InsertSysMenu 创建SysMenu对象 +func (e *SysMenu) InsertSysMenu(model *models.SysMenu) error { + var err error + var data models.SysMenu + msgID := e.MsgID + + err = e.Orm.Model(&data). + Create(model).Error + if err != nil { + log.Errorf("msgID[%s] db error:%s", msgID, err) + return err + } + return nil +} + +func (e *SysMenu) initPaths(menu *models.SysMenu) error { + var err error + var data models.SysMenu + parentMenu := new(models.SysMenu) + if int(menu.ParentId) != 0 { + e.Orm.Model(&data).First(parentMenu, menu.ParentId) + if parentMenu.Paths == "" { + err = errors.New("父级paths异常,请尝试对当前节点父级菜单进行更新操作!") + return err + } + menu.Paths = parentMenu.Paths + "/" + tools.IntToString(menu.MenuId) + } else { + menu.Paths = "/0/" + tools.IntToString(menu.MenuId) + } + e.Orm.Model(&data).Where("menu_id = ?", menu.MenuId).Update("paths", menu.Paths) + return err +} + +// UpdateSysMenu 修改SysMenu对象 +func (e *SysMenu) UpdateSysMenu(c *models.SysMenu) error { + var err error + var data models.SysMenu + msgID := e.MsgID + + db := e.Orm.Model(&data). + Where(c.GetId()).Updates(c) + if db.Error != nil { + log.Errorf("msgID[%s] db error:%s", msgID, err) + return err + } + if db.RowsAffected == 0 { + return errors.New("无权更新该数据") + + } + return nil +} + +// RemoveSysMenu 删除SysMenu +func (e *SysMenu) RemoveSysMenu(d *dto.SysMenuById) error { + var err error + var data models.SysMenu + msgID := e.MsgID + + db := e.Orm.Model(&data). + Where(d.GetId()).Delete(&data) + if db.Error != nil { + err = db.Error + log.Errorf("MsgID[%s] Delete error: %s", msgID, err) + return err + } + if db.RowsAffected == 0 { + err = errors.New("无权删除该数据") + return err + } + return nil +} + +// GetSysMenuList 获取菜单数据 +func (e *SysMenu) GetSysMenuList(c *dto.SysMenuSearch, list *[]models.SysMenu) error { + var err error + var data models.SysMenu + msgID := e.MsgID + + err = e.Orm.Model(&data). + Scopes( + cDto.MakeCondition(c.GetNeedSearch()), + ). + Find(list).Error + if err != nil { + log.Errorf("msgID[%s] db error:%s", msgID, err) + return err + } + return nil +} + +// SetSysMenuTree 设置菜单数据 +func (e *SysMenu) SetSysMenuTree() (c *dto.SysMenuSearch, m []dto.MenuLabel, err error) { + var list []models.SysMenu + err = e.GetSysMenuList(c, &list) + + m = make([]dto.MenuLabel, 0) + for i := 0; i < len(list); i++ { + if list[i].ParentId != 0 { + continue + } + e := dto.MenuLabel{} + e.Id = list[i].MenuId + e.Label = list[i].Title + deptsInfo := menuLabelCall(&list, e) + + m = append(m, deptsInfo) + } + return +} + +// menuLabelCall 递归构造组织数据 +func menuLabelCall(elist *[]models.SysMenu, dept dto.MenuLabel) dto.MenuLabel { + list := *elist + + min := make([]dto.MenuLabel, 0) + for j := 0; j < len(list); j++ { + + if dept.Id != list[j].ParentId { + continue + } + mi := dto.MenuLabel{} + mi.Id = list[j].MenuId + mi.Label = list[j].Title + mi.Children = []dto.MenuLabel{} + if list[j].MenuType != "F" { + ms := menuLabelCall(elist, mi) + min = append(min, ms) + } else { + min = append(min, mi) + } + } + if len(min) > 0 { + dept.Children = min + } else { + dept.Children = nil + } + return dept +} + +// GetMenuList 菜单列表 +func (e *SysMenu) GetSysMenuPage() (c *dto.SysMenuSearch, m []models.SysMenu, err error) { + err = e.getSysMenuPage(c,&m) + m = make([]models.SysMenu, 0) + for i := 0; i < len(m); i++ { + if m[i].ParentId != 0 { + continue + } + menusInfo := menuCall(&m, m[i]) + + m = append(m, menusInfo) + } + return +} + +func menuCall(menulist *[]models.SysMenu, menu models.SysMenu) models.SysMenu { + list := *menulist + + min := make([]models.SysMenu, 0) + for j := 0; j < len(list); j++ { + + if menu.MenuId != list[j].ParentId { + continue + } + mi := models.SysMenu{} + mi.MenuId = list[j].MenuId + mi.MenuName = list[j].MenuName + mi.Title = list[j].Title + mi.Icon = list[j].Icon + mi.Path = list[j].Path + mi.MenuType = list[j].MenuType + mi.Action = list[j].Action + mi.Permission = list[j].Permission + mi.ParentId = list[j].ParentId + mi.NoCache = list[j].NoCache + mi.Breadcrumb = list[j].Breadcrumb + mi.Component = list[j].Component + mi.Sort = list[j].Sort + mi.Visible = list[j].Visible + mi.CreatedAt = list[j].CreatedAt + mi.Children = []models.SysMenu{} + + if mi.MenuType != "F" { + ms := menuCall(menulist, mi) + min = append(min, ms) + + } else { + min = append(min, mi) + } + + } + menu.Children = min + return menu +} \ No newline at end of file diff --git a/cmd/migrate/migration/version/1599190683670_migrate.go b/cmd/migrate/migration/version/1599190683670_migrate.go index 91aa1e68..9763e5c4 100644 --- a/cmd/migrate/migration/version/1599190683670_migrate.go +++ b/cmd/migrate/migration/version/1599190683670_migrate.go @@ -21,11 +21,11 @@ func _1599190683670Test(db *gorm.DB, version string) error { return db.Transaction(func(tx *gorm.DB) error { list3 := []models.SysDept{ - {DeptId: 1, ParentId: 0, DeptPath: "/0/1", DeptName: "爱拓科技", Sort: 0, Leader: "aituo", Phone: "13782218188", Email: "atuo@aituo.com", Status: "2", CreateBy: "1", UpdateBy: "1", BaseModel: models.BaseModel{CreatedAt: time.Now(), UpdatedAt: time.Now()}}, - {DeptId: 7, ParentId: 1, DeptPath: "/0/1/7", DeptName: "研发部", Sort: 1, Leader: "aituo", Phone: "13782218188", Email: "atuo@aituo.com", Status: "2", CreateBy: "1", UpdateBy: "1", BaseModel: models.BaseModel{CreatedAt: time.Now(), UpdatedAt: time.Now()}}, - {DeptId: 8, ParentId: 1, DeptPath: "/0/1/8", DeptName: "运维部", Sort: 0, Leader: "aituo", Phone: "13782218188", Email: "atuo@aituo.com", Status: "2", CreateBy: "1", UpdateBy: "1", BaseModel: models.BaseModel{CreatedAt: time.Now(), UpdatedAt: time.Now()}}, - {DeptId: 9, ParentId: 1, DeptPath: "/0/1/9", DeptName: "客服部", Sort: 0, Leader: "aituo", Phone: "13782218188", Email: "atuo@aituo.com", Status: "2", CreateBy: "1", UpdateBy: "1", BaseModel: models.BaseModel{CreatedAt: time.Now(), UpdatedAt: time.Now()}}, - {DeptId: 10, ParentId: 1, DeptPath: "/0/1/10", DeptName: "人力资源", Sort: 3, Leader: "aituo", Phone: "13782218188", Email: "atuo@aituo.com", Status: "1", CreateBy: "1", UpdateBy: "1", BaseModel: models.BaseModel{CreatedAt: time.Now(), UpdatedAt: time.Now()}}, + {DeptId: 1, ParentId: 0, DeptPath: "/0/1", DeptName: "爱拓科技", Sort: 0, Leader: "aituo", Phone: "13782218188", Email: "atuo@aituo.com", Status: "2", ControlBy:common.ControlBy{ CreateBy: 1, UpdateBy: 1}, ModelTime: common.ModelTime{CreatedAt: time.Now(), UpdatedAt: time.Now()}}, + {DeptId: 7, ParentId: 1, DeptPath: "/0/1/7", DeptName: "研发部", Sort: 1, Leader: "aituo", Phone: "13782218188", Email: "atuo@aituo.com", Status: "2", ControlBy:common.ControlBy{ CreateBy: 1, UpdateBy: 1}, ModelTime: common.ModelTime{CreatedAt: time.Now(), UpdatedAt: time.Now()}}, + {DeptId: 8, ParentId: 1, DeptPath: "/0/1/8", DeptName: "运维部", Sort: 0, Leader: "aituo", Phone: "13782218188", Email: "atuo@aituo.com", Status: "2", ControlBy:common.ControlBy{ CreateBy: 1, UpdateBy: 1}, ModelTime: common.ModelTime{CreatedAt: time.Now(), UpdatedAt: time.Now()}}, + {DeptId: 9, ParentId: 1, DeptPath: "/0/1/9", DeptName: "客服部", Sort: 0, Leader: "aituo", Phone: "13782218188", Email: "atuo@aituo.com", Status: "2", ControlBy:common.ControlBy{ CreateBy: 1, UpdateBy: 1}, ModelTime: common.ModelTime{CreatedAt: time.Now(), UpdatedAt: time.Now()}}, + {DeptId: 10, ParentId: 1, DeptPath: "/0/1/10", DeptName: "人力资源", Sort: 3, Leader: "aituo", Phone: "13782218188", Email: "atuo@aituo.com", Status: "1", ControlBy:common.ControlBy{ CreateBy: 1, UpdateBy: 1}, ModelTime: common.ModelTime{CreatedAt: time.Now(), UpdatedAt: time.Now()}}, } list4 := []system.SysConfig{