Skip to content

Commit

Permalink
增加数据字典
Browse files Browse the repository at this point in the history
  • Loading branch information
xufqing committed Nov 24, 2020
1 parent bd5966d commit 565f477
Show file tree
Hide file tree
Showing 41 changed files with 627 additions and 232 deletions.
10 changes: 0 additions & 10 deletions README.en.md
Original file line number Diff line number Diff line change
Expand Up @@ -24,13 +24,3 @@ Software architecture description
2. Create Feat_xxx branch
3. Commit your code
4. Create Pull Request


#### Gitee Feature

1. You can use Readme\_XXX.md to support different languages, such as Readme\_en.md, Readme\_zh.md
2. Gitee blog [blog.gitee.com](https://blog.gitee.com)
3. Explore open source project [https://gitee.com/explore](https://gitee.com/explore)
4. The most valuable open source project [GVP](https://gitee.com/gvp)
5. The manual of Gitee [https://gitee.com/help](https://gitee.com/help)
6. The most popular members [https://gitee.com/gitee-stars/](https://gitee.com/gitee-stars/)
19 changes: 1 addition & 18 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -18,21 +18,4 @@ golang 运维平台

1. xxxx
2. xxxx
3. xxxx

#### 参与贡献

1. Fork 本仓库
2. 新建 Feat_xxx 分支
3. 提交代码
4. 新建 Pull Request


#### 码云特技

1. 使用 Readme\_XXX.md 来支持不同的语言,例如 Readme\_en.md, Readme\_zh.md
2. 码云官方博客 [blog.gitee.com](https://blog.gitee.com)
3. 你可以 [https://gitee.com/explore](https://gitee.com/explore) 这个地址来了解码云上的优秀开源项目
4. [GVP](https://gitee.com/gvp) 全称是码云最有价值开源项目,是码云综合评定出的优秀开源项目
5. 码云官方提供的使用手册 [https://gitee.com/help](https://gitee.com/help)
6. 码云封面人物是一档用来展示码云会员风采的栏目 [https://gitee.com/gitee-stars/](https://gitee.com/gitee-stars/)
3. xxxx
4 changes: 3 additions & 1 deletion api/v1/system/sys_dept.go
Original file line number Diff line number Diff line change
Expand Up @@ -22,7 +22,9 @@ func GetDepts(c *gin.Context) {
s := service.New()
depts := s.GetDepts(&req)
if (req.Name != "" || req.Status != nil){
response.SuccessWithData(depts)
var newResp []response.DictTreeResp
utils.Struct2StructByJson(depts, &newResp)
response.SuccessWithData(newResp)
} else {
var resp []response.DeptTreeResp
resp = service.GenDeptTree(nil,depts)
Expand Down
107 changes: 107 additions & 0 deletions api/v1/system/sys_dict.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,107 @@
package system

import (
"anew-server/dto/request"
"anew-server/dto/response"
"anew-server/dto/service"
"anew-server/pkg/common"
"anew-server/pkg/utils"
"github.com/gin-gonic/gin"
)

// 查询所有字典
func GetDicts(c *gin.Context) {
// 绑定参数
var req request.DictListReq
err := c.Bind(&req)
if err != nil {
response.FailWithCode(response.ParmError)
return
}
// 创建服务
s := service.New()
dicts := s.GetDicts(&req)
if req.Key != "" || req.Value != "" || req.Status != nil || req.TypeKey != "" {
var newResp []response.DictTreeResp
utils.Struct2StructByJson(dicts, &newResp)
response.SuccessWithData(newResp)
} else {
var resp []response.DictTreeResp
resp = service.GenDictTree(nil, dicts)
response.SuccessWithData(resp)
}
}

// 创建字典
func CreateDict(c *gin.Context) {
user := GetCurrentUser(c)
// 绑定参数
var req request.CreateDictReq
err := c.Bind(&req)
if err != nil {
response.FailWithCode(response.ParmError)
return
}

// 参数校验
err = common.NewValidatorError(common.Validate.Struct(req), req.FieldTrans())
if err != nil {
response.FailWithMsg(err.Error())
return
}
// 记录当前创建人信息
req.Creator = user.Name
// 创建服务
s := service.New()
err = s.CreateDict(&req)
if err != nil {
response.FailWithMsg(err.Error())
return
}
response.Success()
}

// 更新字典
func UpdateDictById(c *gin.Context) {
// 绑定参数
var req request.UpdateDictReq
err := c.Bind(&req)
if err != nil {
response.FailWithCode(response.ParmError)
return
}
DictId := utils.Str2Uint(c.Param("DictId"))
if DictId == 0 {
response.FailWithMsg("字典编号不正确")
return
}
// 创建服务
s := service.New()
// 更新数据
err = s.UpdateDictById(DictId, req)
if err != nil {
response.FailWithMsg(err.Error())
return
}
response.Success()
}

// 批量删除字典
func BatchDeleteDictByIds(c *gin.Context) {
var req request.IdsReq
err := c.Bind(&req)
if err != nil {
response.FailWithCode(response.ParmError)
return
}

// 创建服务
s := service.New()
// 删除数据
err = s.DeleteDictByIds(req.Ids)
if err != nil {
response.FailWithMsg(err.Error())
return
}
response.Success()
}
2 changes: 1 addition & 1 deletion api/v1/system/sys_menu.go
Original file line number Diff line number Diff line change
Expand Up @@ -67,7 +67,7 @@ func CreateMenu(c *gin.Context) {
// 更新菜单
func UpdateMenuById(c *gin.Context) {
// 绑定参数
var req gin.H
var req request.UpdateMenuReq
err := c.Bind(&req)
if err != nil {
response.FailWithCode(response.ParmError)
Expand Down
6 changes: 3 additions & 3 deletions api/v1/system/sys_user.go
Original file line number Diff line number Diff line change
Expand Up @@ -4,17 +4,17 @@ import (
"anew-server/dto/request"
"anew-server/dto/response"
"anew-server/dto/service"
"anew-server/models"
"anew-server/models/system"
"anew-server/pkg/common"
"anew-server/pkg/utils"
"github.com/gin-gonic/gin"
"path"
)

// 获取当前请求用户信息
func GetCurrentUser(c *gin.Context) (models.SysUser) {
func GetCurrentUser(c *gin.Context) (system.SysUser) {
user, exists := c.Get("user")
var newUser models.SysUser
var newUser system.SysUser
if !exists {
return newUser
}
Expand Down
3 changes: 1 addition & 2 deletions dto/request/sys_dept.go
Original file line number Diff line number Diff line change
Expand Up @@ -4,12 +4,11 @@ package request
type CreateDeptReq struct {
Name string `json:"name" validate:"required"`
Sort int `json:"sort"`
Status *bool `json:"status"`
ParentId uint `json:"parent_id"`
Creator string `json:"creator"`
}

// 修改用户结构体
// 修改部门
type UpdateDeptReq struct {
Name string `json:"name" validate:"required"`
Status *bool `json:"status"`
Expand Down
44 changes: 44 additions & 0 deletions dto/request/sys_dict.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,44 @@
package request

// 创建字典结构体
type CreateDictReq struct {
Key string `json:"key" validate:"required"`
Value string `json:"value" validate:"required"`
Desc string `json:"desc"`
ParentId uint `json:"parent_id"`
Creator string `json:"creator"`
}

// 修改字典
type UpdateDictReq struct {
Key string `json:"key" validate:"required"`
Value string `json:"value" validate:"required"`
Desc string `json:"desc"`
ParentId uint `json:"parent_id"`
Status *bool `json:"status"`
}

type DictListReq struct {
Key string `json:"key" form:"key"`
Value string `json:"value" form:"value"`
Desc string `json:"desc" form:"desc"`
Creator string `json:"creator" form:"creator"`
Status *bool `json:"status" form:"status"`
TypeKey string `json:"type_key" form:"type_key"`
}

// 翻译需要校验的字段名称
func (s CreateDictReq) FieldTrans() map[string]string {
m := make(map[string]string, 0)
m["Key"] = "字典Key"
m["Value"] = "字典Value"
return m
}

// 翻译需要校验的字段名称
func (s UpdateDictReq) FieldTrans() map[string]string {
m := make(map[string]string, 0)
m["Key"] = "字典Key"
m["Value"] = "字典Value"
return m
}
11 changes: 10 additions & 1 deletion dto/request/sys_menu.go
Original file line number Diff line number Diff line change
Expand Up @@ -17,11 +17,20 @@ type CreateMenuReq struct {
Icon string `json:"icon"`
Path string `json:"path"`
Sort int `json:"sort"`
Status *bool `json:"status"`
ParentId uint `json:"parent_id"`
Creator string `json:"creator"`
}

// 修改菜单
type UpdateMenuReq struct {
Name string `json:"name" validate:"required"`
Icon string `json:"icon"`
Path string `json:"path"`
Sort int `json:"sort"`
Status *bool `json:"status"`
ParentId uint `json:"parent_id"`
}

// 翻译需要校验的字段名称
func (s CreateMenuReq) FieldTrans() map[string]string {
m := make(map[string]string, 0)
Expand Down
1 change: 0 additions & 1 deletion dto/request/sys_role.go
Original file line number Diff line number Diff line change
Expand Up @@ -22,7 +22,6 @@ type CreateRoleReq struct {
Name string `json:"name" validate:"required"`
Keyword string `json:"keyword" validate:"required"`
Desc string `json:"desc"`
Status *bool `json:"status"`
Creator string `json:"creator"`
}

Expand Down
1 change: 0 additions & 1 deletion dto/request/sys_user.go
Original file line number Diff line number Diff line change
Expand Up @@ -35,7 +35,6 @@ type CreateUserReq struct {
Avatar string `json:"avatar"`
Name string `json:"name" validate:"required"`
Email string `json:"email"`
Status *bool `json:"status"`
DeptId uint `json:"dept_id"`
RoleId uint `json:"role_id" validate:"required"`
Creator string `json:"creator"`
Expand Down
14 changes: 14 additions & 0 deletions dto/response/sys_dict.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,14 @@
package response

// 字典树信息响应,

type DictTreeResp struct {
Id uint `json:"id"`
ParentId uint `json:"parent_id"`
Key string `json:"key"`
Value string `json:"value"`
Desc string `json:"desc"`
Creator string `json:"creator"`
Status bool `json:"status"`
Children []DictTreeResp `json:"children,omitempty"` //tag:omitempty 为空的值不显示
}
18 changes: 9 additions & 9 deletions dto/service/sys_api.go
Original file line number Diff line number Diff line change
Expand Up @@ -2,20 +2,20 @@ package service

import (
"anew-server/dto/request"
"anew-server/models"
"anew-server/models/system"
"anew-server/pkg/utils"
"gorm.io/gorm"
"errors"
"fmt"
"github.com/gin-gonic/gin"
"gorm.io/gorm"
"strings"
)


func (s *MysqlService) GetApis(req *request.ApiListReq) ([]models.SysApi, error) {
func (s *MysqlService) GetApis(req *request.ApiListReq) ([]system.SysApi, error) {
var err error
list := make([]models.SysApi, 0)
query := s.db.Table(new(models.SysApi).TableName())
list := make([]system.SysApi, 0)
query := s.db.Table(new(system.SysApi).TableName())
name := strings.TrimSpace(req.Name)
if name != "" {
query = query.Where("name LIKE ?", fmt.Sprintf("%%%s%%", name))
Expand Down Expand Up @@ -51,7 +51,7 @@ func (s *MysqlService) GetApis(req *request.ApiListReq) ([]models.SysApi, error)

// 创建接口
func (s *MysqlService) CreateApi(req *request.CreateApiReq) (err error) {
var api models.SysApi
var api system.SysApi
utils.Struct2StructByJson(req, &api)
// 创建数据
err = s.db.Create(&api).Error
Expand All @@ -60,14 +60,14 @@ func (s *MysqlService) CreateApi(req *request.CreateApiReq) (err error) {

// 更新接口
func (s *MysqlService) UpdateApiById(id uint, req gin.H) (err error) {
var oldApi models.SysApi
var oldApi system.SysApi
query := s.db.Table(oldApi.TableName()).Where("id = ?", id).First(&oldApi)
if query.Error == gorm.ErrRecordNotFound {
return errors.New("记录不存在")
}

// 比对增量字段
var m models.SysApi
var m system.SysApi
utils.CompareDifferenceStructByJson(oldApi, req, &m)
// 更新指定列
err = query.Updates(m).Error
Expand All @@ -77,5 +77,5 @@ func (s *MysqlService) UpdateApiById(id uint, req gin.H) (err error) {
// 批量删除接口
func (s *MysqlService) DeleteApiByIds(ids []uint) (err error) {

return s.db.Where("id IN (?)", ids).Delete(models.SysApi{}).Error
return s.db.Where("id IN (?)", ids).Delete(system.SysApi{}).Error
}
Loading

0 comments on commit 565f477

Please sign in to comment.