zhangqian
2024-02-28 fe5cd122cf94f83f97a1e71a391cefd62f219938
字段增删改查接口
3个文件已添加
3 文件已重命名
10个文件已修改
1226 ■■■■■ 已修改文件
conf/config.yaml 15 ●●●● 补丁 | 查看 | 原始文档 | blame | 历史
constvar/const.go 9 ●●●●● 补丁 | 查看 | 原始文档 | blame | 历史
controllers/department.go 4 ●●●● 补丁 | 查看 | 原始文档 | blame | 历史
controllers/dict.go 149 ●●●●● 补丁 | 查看 | 原始文档 | blame | 历史
controllers/request/common.go 补丁 | 查看 | 原始文档 | blame | 历史
controllers/request/department.go 补丁 | 查看 | 原始文档 | blame | 历史
controllers/request/dict.go 20 ●●●●● 补丁 | 查看 | 原始文档 | blame | 历史
controllers/response/common.go 补丁 | 查看 | 原始文档 | blame | 历史
docs/docs.go 279 ●●●●● 补丁 | 查看 | 原始文档 | blame | 历史
docs/swagger.json 279 ●●●●● 补丁 | 查看 | 原始文档 | blame | 历史
docs/swagger.yaml 184 ●●●●● 补丁 | 查看 | 原始文档 | blame | 历史
main.go 5 ●●●● 补丁 | 查看 | 原始文档 | blame | 历史
models/db.go 8 ●●●● 补丁 | 查看 | 原始文档 | blame | 历史
models/department.go 2 ●●● 补丁 | 查看 | 原始文档 | blame | 历史
models/dict.go 260 ●●●●● 补丁 | 查看 | 原始文档 | blame | 历史
router/router.go 12 ●●●●● 补丁 | 查看 | 原始文档 | blame | 历史
conf/config.yaml
@@ -1,21 +1,13 @@
web:
  port: 8005
  port: 8008
  host: 192.168.20.119
  apport: 9091
  alhost: 192.168.20.119
  alport: 9090
  nodeId: wangpengfei
  ossType: local
db:
  dsn: root:c++java123@tcp(192.168.20.119:3306)/aps_server2?charset=utf8&parseTime=True&loc=Local
  dsn: root:c++java123@tcp(192.168.20.119:3306)/silk?charset=utf8&parseTime=True&loc=Local
  logMode: true
  maxIdleCon: 20
  maxOpenCon: 100
  connMaxLifeTimeSecond: 120
  connMaxIdleTimeSecond: 1800
registry:
  etcdAddr: 192.168.20.247:2379
  keyPrefix: /aps/apsServer/node/
log:
  path: ./logs/server.log
  encoder: console
@@ -24,8 +16,5 @@
  MaxSize: 200   # 每个日志文件的最大大小(MB)
  MaxBackups: 2  # 保留的旧日志文件个数
  RotateDays: 5  # 日志文件的最大保留天数
nsq:
  nsqdAddr: 121.31.232.83:4150
  nsqlookupdAddr:
local:
  storePath: uploads/file
constvar/const.go
@@ -1 +1,10 @@
package constvar
type DictType int
const (
    DictTypeMarket   DictType = iota //庄口
    DictTypeWorkshop                 //车间
    DictTypeColor                    //颜色
    DictTypeSpec                     //规格
)
controllers/department.go
@@ -5,12 +5,12 @@
    "fmt"
    "github.com/gin-gonic/gin"
    "gorm.io/gorm"
    "jialian/controllers/request"
    "jialian/extend/code"
    "jialian/extend/util"
    "jialian/models"
    "jialian/pkg/convertx"
    "jialian/pkg/structx"
    "jialian/request"
)
type DepartmentController struct{}
@@ -150,7 +150,7 @@
    }
    treeMap := make(map[uint][]*models.Department)
    allMenus, err := models.NewDepartmentSearch().FindNotTotal()
    allMenus, err := models.NewDepartmentSearch().FindAll()
    for _, v := range allMenus {
        treeMap[v.ParentId] = append(treeMap[v.ParentId], v)
    }
controllers/dict.go
New file
@@ -0,0 +1,149 @@
package controllers
import (
    "errors"
    "fmt"
    "github.com/gin-gonic/gin"
    "gorm.io/gorm"
    "jialian/controllers/request"
    "jialian/extend/code"
    "jialian/extend/util"
    "jialian/models"
    "jialian/pkg/convertx"
    "jialian/pkg/structx"
)
type DictController struct{}
// Add
// @Tags      系统设置/字典
// @Summary   添加字典
// @Produce   application/json
// @Param     object  body  request.AddDict true  "字典信息"
// @Success   200 {object} util.Response "成功"
// @Router    /api-jl/v1/system/dict [post]
func (slf DictController) Add(c *gin.Context) {
    var reqParams request.AddDict
    var params models.Dict
    if err := c.BindJSON(&reqParams); err != nil {
        util.ResponseFormat(c, code.RequestParamError, "参数解析失败,数据类型错误")
        return
    }
    if err := structx.AssignTo(reqParams, &params); err != nil {
        util.ResponseFormat(c, code.RequestParamError, "数据转换错误")
        return
    }
    if err := slf.ParamsCheck(params); err != nil {
        util.ResponseFormat(c, code.RequestParamError, err.Error())
        return
    }
    if err := models.NewDictSearch().Create(&params); err != nil {
        util.ResponseFormat(c, code.SaveFail, "插入失败")
        return
    }
    util.ResponseFormat(c, code.Success, "添加成功")
}
// Update
// @Tags      系统设置/字典
// @Summary   编辑字典
// @Produce   application/json
// @Param     object  body request.UpdateDict true  "字典信息"
// @Success   200 {object} util.Response "成功"
// @Router    /api-jl/v1/system/dict [put]
func (slf DictController) Update(c *gin.Context) {
    var (
        reqParams request.UpdateDict
        params    models.Dict
    )
    if err := c.BindJSON(&reqParams); err != nil {
        util.ResponseFormat(c, code.RequestParamError, fmt.Sprintf("参数解析失败: %v"+err.Error()))
        return
    }
    if err := structx.AssignTo(reqParams, &params); err != nil {
        util.ResponseFormat(c, code.RequestParamError, fmt.Sprintf("数据转换错误: %v", err.Error()))
        return
    }
    if err := slf.ParamsCheck(params); err != nil {
        util.ResponseFormat(c, code.RequestParamError, err.Error())
        return
    }
    _, err := models.NewDictSearch().SetID(params.ID).First()
    if err != nil {
        util.ResponseFormat(c, code.RequestParamError, "ID不存在")
        return
    }
    err = models.NewDictSearch().SetID(params.ID).Save(&params)
    if err != nil {
        util.ResponseFormat(c, code.RequestParamError, "修改失败")
        return
    }
    util.ResponseFormat(c, code.UpdateSuccess, "更新成功")
}
func (slf DictController) ParamsCheck(params models.Dict) (err error) {
    dict, err := models.NewDictSearch().SetNumber(params.Number).First()
    if err != gorm.ErrRecordNotFound && dict != nil && dict.ID != params.ID {
        return errors.New("编号重复")
    }
    dict, err = models.NewDictSearch().SetName(params.Name).First()
    if err != gorm.ErrRecordNotFound && dict != nil && dict.ID != params.ID {
        return errors.New("名称重复")
    }
    return nil
}
// List
// @Tags      系统设置/字典
// @Summary   查询字典列表
// @Produce   application/json
// @Param     object  query    request.GetDictList true  "查询参数"
// @Success   200   {object}  util.ResponseList{data=[]models.Dict}  "成功"
// @Router    /api-jl/v1/system/dict [get]
func (slf DictController) List(c *gin.Context) {
    var params request.GetDictList
    if err := c.ShouldBindQuery(&params); err != nil {
        util.ResponseFormat(c, code.RequestParamError, err.Error())
        return
    }
    list, err := models.NewDictSearch().SetDictType(params.DictType).FindAll()
    if err != nil {
        util.ResponseFormat(c, code.RequestParamError, "查找失败")
        return
    }
    util.ResponseFormatList(c, code.Success, list, len(list))
}
// Delete
// @Tags      系统设置/字典
// @Summary   删除字典
// @Produce   application/json
// @Param     id  path string true  "字典信息"
// @Success   200 {object} util.Response "成功"
// @Router    /api-jl/v1/system/dict/{id} [delete]
func (slf DictController) Delete(c *gin.Context) {
    idStr := c.Param("id")
    if idStr == "0" || idStr == "" {
        util.ResponseFormat(c, code.RequestParamError, "空的记录id")
        return
    }
    id := convertx.StringToUInt(idStr)
    err := models.NewDictSearch().SetID(id).Delete()
    if err != nil {
        util.ResponseFormat(c, code.RequestParamError, "删除失败")
        return
    }
    util.ResponseFormat(c, code.UpdateSuccess, "删除成功")
}
controllers/request/common.go
controllers/request/department.go
controllers/request/dict.go
New file
@@ -0,0 +1,20 @@
package request
import "jialian/constvar"
type GetDictList struct {
    PageInfo
    DictType constvar.DictType `json:"dictType"` //字典类型
}
type AddDict struct {
    DictType constvar.DictType `json:"dictType"` //字典类型
    Number   string            `json:"number"`   //编码
    Name     string            `json:"name"`     //名称
    Remark   string            `json:"remark"`   //备注
}
type UpdateDict struct {
    ID uint `gorm:"comment:主键ID;primaryKey;" json:"id"`
    AddDict
}
controllers/response/common.go
docs/docs.go
@@ -16,6 +16,162 @@
    "host": "{{.Host}}",
    "basePath": "{{.BasePath}}",
    "paths": {
        "/api-jl/v1/system/dict": {
            "get": {
                "produces": [
                    "application/json"
                ],
                "tags": [
                    "系统设置/字典"
                ],
                "summary": "查询字典列表",
                "parameters": [
                    {
                        "enum": [
                            0,
                            1,
                            2,
                            3
                        ],
                        "type": "integer",
                        "x-enum-comments": {
                            "DictTypeColor": "颜色",
                            "DictTypeMarket": "庄口",
                            "DictTypeSpec": "规格",
                            "DictTypeWorkshop": "车间"
                        },
                        "x-enum-varnames": [
                            "DictTypeMarket",
                            "DictTypeWorkshop",
                            "DictTypeColor",
                            "DictTypeSpec"
                        ],
                        "description": "字典类型",
                        "name": "dictType",
                        "in": "query"
                    },
                    {
                        "type": "integer",
                        "description": "页码",
                        "name": "page",
                        "in": "query"
                    },
                    {
                        "type": "integer",
                        "description": "每页大小",
                        "name": "pageSize",
                        "in": "query"
                    }
                ],
                "responses": {
                    "200": {
                        "description": "成功",
                        "schema": {
                            "allOf": [
                                {
                                    "$ref": "#/definitions/util.ResponseList"
                                },
                                {
                                    "type": "object",
                                    "properties": {
                                        "data": {
                                            "type": "array",
                                            "items": {
                                                "$ref": "#/definitions/models.Dict"
                                            }
                                        }
                                    }
                                }
                            ]
                        }
                    }
                }
            },
            "put": {
                "produces": [
                    "application/json"
                ],
                "tags": [
                    "系统设置/字典"
                ],
                "summary": "编辑字典",
                "parameters": [
                    {
                        "description": "字典信息",
                        "name": "object",
                        "in": "body",
                        "required": true,
                        "schema": {
                            "$ref": "#/definitions/request.UpdateDict"
                        }
                    }
                ],
                "responses": {
                    "200": {
                        "description": "成功",
                        "schema": {
                            "$ref": "#/definitions/util.Response"
                        }
                    }
                }
            },
            "post": {
                "produces": [
                    "application/json"
                ],
                "tags": [
                    "系统设置/字典"
                ],
                "summary": "添加字典",
                "parameters": [
                    {
                        "description": "字典信息",
                        "name": "object",
                        "in": "body",
                        "required": true,
                        "schema": {
                            "$ref": "#/definitions/request.AddDict"
                        }
                    }
                ],
                "responses": {
                    "200": {
                        "description": "成功",
                        "schema": {
                            "$ref": "#/definitions/util.Response"
                        }
                    }
                }
            }
        },
        "/api-jl/v1/system/dict/{id}": {
            "delete": {
                "produces": [
                    "application/json"
                ],
                "tags": [
                    "系统设置/字典"
                ],
                "summary": "删除字典",
                "parameters": [
                    {
                        "type": "string",
                        "description": "字典信息",
                        "name": "id",
                        "in": "path",
                        "required": true
                    }
                ],
                "responses": {
                    "200": {
                        "description": "成功",
                        "schema": {
                            "$ref": "#/definitions/util.Response"
                        }
                    }
                }
            }
        },
        "/api-s/v1/organize/department": {
            "get": {
                "produces": [
@@ -152,6 +308,39 @@
        }
    },
    "definitions": {
        "constvar.DictType": {
            "type": "integer",
            "enum": [
                0,
                1,
                2,
                3
            ],
            "x-enum-comments": {
                "DictTypeColor": "颜色",
                "DictTypeMarket": "庄口",
                "DictTypeSpec": "规格",
                "DictTypeWorkshop": "车间"
            },
            "x-enum-varnames": [
                "DictTypeMarket",
                "DictTypeWorkshop",
                "DictTypeColor",
                "DictTypeSpec"
            ]
        },
        "gorm.DeletedAt": {
            "type": "object",
            "properties": {
                "time": {
                    "type": "string"
                },
                "valid": {
                    "description": "Valid is true if Time is not NULL",
                    "type": "boolean"
                }
            }
        },
        "models.Department": {
            "type": "object",
            "properties": {
@@ -190,6 +379,43 @@
                }
            }
        },
        "models.Dict": {
            "type": "object",
            "properties": {
                "createdAt": {
                    "type": "string"
                },
                "deletedAt": {
                    "$ref": "#/definitions/gorm.DeletedAt"
                },
                "dictType": {
                    "description": "字典类型",
                    "allOf": [
                        {
                            "$ref": "#/definitions/constvar.DictType"
                        }
                    ]
                },
                "id": {
                    "type": "integer"
                },
                "name": {
                    "description": "名称",
                    "type": "string"
                },
                "number": {
                    "description": "编号",
                    "type": "string"
                },
                "remark": {
                    "description": "备注",
                    "type": "string"
                },
                "updatedAt": {
                    "type": "string"
                }
            }
        },
        "request.AddDepartment": {
            "type": "object",
            "properties": {
@@ -204,6 +430,31 @@
                "parentId": {
                    "description": "上级部门ID 一级部门传0",
                    "type": "integer"
                },
                "remark": {
                    "description": "备注",
                    "type": "string"
                }
            }
        },
        "request.AddDict": {
            "type": "object",
            "properties": {
                "dictType": {
                    "description": "字典类型",
                    "allOf": [
                        {
                            "$ref": "#/definitions/constvar.DictType"
                        }
                    ]
                },
                "name": {
                    "description": "名称",
                    "type": "string"
                },
                "number": {
                    "description": "编码",
                    "type": "string"
                },
                "remark": {
                    "description": "备注",
@@ -235,6 +486,34 @@
                }
            }
        },
        "request.UpdateDict": {
            "type": "object",
            "properties": {
                "dictType": {
                    "description": "字典类型",
                    "allOf": [
                        {
                            "$ref": "#/definitions/constvar.DictType"
                        }
                    ]
                },
                "id": {
                    "type": "integer"
                },
                "name": {
                    "description": "名称",
                    "type": "string"
                },
                "number": {
                    "description": "编码",
                    "type": "string"
                },
                "remark": {
                    "description": "备注",
                    "type": "string"
                }
            }
        },
        "util.Response": {
            "type": "object",
            "properties": {
docs/swagger.json
@@ -4,6 +4,162 @@
        "contact": {}
    },
    "paths": {
        "/api-jl/v1/system/dict": {
            "get": {
                "produces": [
                    "application/json"
                ],
                "tags": [
                    "系统设置/字典"
                ],
                "summary": "查询字典列表",
                "parameters": [
                    {
                        "enum": [
                            0,
                            1,
                            2,
                            3
                        ],
                        "type": "integer",
                        "x-enum-comments": {
                            "DictTypeColor": "颜色",
                            "DictTypeMarket": "庄口",
                            "DictTypeSpec": "规格",
                            "DictTypeWorkshop": "车间"
                        },
                        "x-enum-varnames": [
                            "DictTypeMarket",
                            "DictTypeWorkshop",
                            "DictTypeColor",
                            "DictTypeSpec"
                        ],
                        "description": "字典类型",
                        "name": "dictType",
                        "in": "query"
                    },
                    {
                        "type": "integer",
                        "description": "页码",
                        "name": "page",
                        "in": "query"
                    },
                    {
                        "type": "integer",
                        "description": "每页大小",
                        "name": "pageSize",
                        "in": "query"
                    }
                ],
                "responses": {
                    "200": {
                        "description": "成功",
                        "schema": {
                            "allOf": [
                                {
                                    "$ref": "#/definitions/util.ResponseList"
                                },
                                {
                                    "type": "object",
                                    "properties": {
                                        "data": {
                                            "type": "array",
                                            "items": {
                                                "$ref": "#/definitions/models.Dict"
                                            }
                                        }
                                    }
                                }
                            ]
                        }
                    }
                }
            },
            "put": {
                "produces": [
                    "application/json"
                ],
                "tags": [
                    "系统设置/字典"
                ],
                "summary": "编辑字典",
                "parameters": [
                    {
                        "description": "字典信息",
                        "name": "object",
                        "in": "body",
                        "required": true,
                        "schema": {
                            "$ref": "#/definitions/request.UpdateDict"
                        }
                    }
                ],
                "responses": {
                    "200": {
                        "description": "成功",
                        "schema": {
                            "$ref": "#/definitions/util.Response"
                        }
                    }
                }
            },
            "post": {
                "produces": [
                    "application/json"
                ],
                "tags": [
                    "系统设置/字典"
                ],
                "summary": "添加字典",
                "parameters": [
                    {
                        "description": "字典信息",
                        "name": "object",
                        "in": "body",
                        "required": true,
                        "schema": {
                            "$ref": "#/definitions/request.AddDict"
                        }
                    }
                ],
                "responses": {
                    "200": {
                        "description": "成功",
                        "schema": {
                            "$ref": "#/definitions/util.Response"
                        }
                    }
                }
            }
        },
        "/api-jl/v1/system/dict/{id}": {
            "delete": {
                "produces": [
                    "application/json"
                ],
                "tags": [
                    "系统设置/字典"
                ],
                "summary": "删除字典",
                "parameters": [
                    {
                        "type": "string",
                        "description": "字典信息",
                        "name": "id",
                        "in": "path",
                        "required": true
                    }
                ],
                "responses": {
                    "200": {
                        "description": "成功",
                        "schema": {
                            "$ref": "#/definitions/util.Response"
                        }
                    }
                }
            }
        },
        "/api-s/v1/organize/department": {
            "get": {
                "produces": [
@@ -140,6 +296,39 @@
        }
    },
    "definitions": {
        "constvar.DictType": {
            "type": "integer",
            "enum": [
                0,
                1,
                2,
                3
            ],
            "x-enum-comments": {
                "DictTypeColor": "颜色",
                "DictTypeMarket": "庄口",
                "DictTypeSpec": "规格",
                "DictTypeWorkshop": "车间"
            },
            "x-enum-varnames": [
                "DictTypeMarket",
                "DictTypeWorkshop",
                "DictTypeColor",
                "DictTypeSpec"
            ]
        },
        "gorm.DeletedAt": {
            "type": "object",
            "properties": {
                "time": {
                    "type": "string"
                },
                "valid": {
                    "description": "Valid is true if Time is not NULL",
                    "type": "boolean"
                }
            }
        },
        "models.Department": {
            "type": "object",
            "properties": {
@@ -178,6 +367,43 @@
                }
            }
        },
        "models.Dict": {
            "type": "object",
            "properties": {
                "createdAt": {
                    "type": "string"
                },
                "deletedAt": {
                    "$ref": "#/definitions/gorm.DeletedAt"
                },
                "dictType": {
                    "description": "字典类型",
                    "allOf": [
                        {
                            "$ref": "#/definitions/constvar.DictType"
                        }
                    ]
                },
                "id": {
                    "type": "integer"
                },
                "name": {
                    "description": "名称",
                    "type": "string"
                },
                "number": {
                    "description": "编号",
                    "type": "string"
                },
                "remark": {
                    "description": "备注",
                    "type": "string"
                },
                "updatedAt": {
                    "type": "string"
                }
            }
        },
        "request.AddDepartment": {
            "type": "object",
            "properties": {
@@ -192,6 +418,31 @@
                "parentId": {
                    "description": "上级部门ID 一级部门传0",
                    "type": "integer"
                },
                "remark": {
                    "description": "备注",
                    "type": "string"
                }
            }
        },
        "request.AddDict": {
            "type": "object",
            "properties": {
                "dictType": {
                    "description": "字典类型",
                    "allOf": [
                        {
                            "$ref": "#/definitions/constvar.DictType"
                        }
                    ]
                },
                "name": {
                    "description": "名称",
                    "type": "string"
                },
                "number": {
                    "description": "编码",
                    "type": "string"
                },
                "remark": {
                    "description": "备注",
@@ -223,6 +474,34 @@
                }
            }
        },
        "request.UpdateDict": {
            "type": "object",
            "properties": {
                "dictType": {
                    "description": "字典类型",
                    "allOf": [
                        {
                            "$ref": "#/definitions/constvar.DictType"
                        }
                    ]
                },
                "id": {
                    "type": "integer"
                },
                "name": {
                    "description": "名称",
                    "type": "string"
                },
                "number": {
                    "description": "编码",
                    "type": "string"
                },
                "remark": {
                    "description": "备注",
                    "type": "string"
                }
            }
        },
        "util.Response": {
            "type": "object",
            "properties": {
docs/swagger.yaml
@@ -1,4 +1,29 @@
definitions:
  constvar.DictType:
    enum:
    - 0
    - 1
    - 2
    - 3
    type: integer
    x-enum-comments:
      DictTypeColor: 颜色
      DictTypeMarket: 庄口
      DictTypeSpec: 规格
      DictTypeWorkshop: 车间
    x-enum-varnames:
    - DictTypeMarket
    - DictTypeWorkshop
    - DictTypeColor
    - DictTypeSpec
  gorm.DeletedAt:
    properties:
      time:
        type: string
      valid:
        description: Valid is true if Time is not NULL
        type: boolean
    type: object
  models.Department:
    properties:
      children:
@@ -26,6 +51,30 @@
        description: 排序
        type: integer
    type: object
  models.Dict:
    properties:
      createdAt:
        type: string
      deletedAt:
        $ref: '#/definitions/gorm.DeletedAt'
      dictType:
        allOf:
        - $ref: '#/definitions/constvar.DictType'
        description: 字典类型
      id:
        type: integer
      name:
        description: 名称
        type: string
      number:
        description: 编号
        type: string
      remark:
        description: 备注
        type: string
      updatedAt:
        type: string
    type: object
  request.AddDepartment:
    properties:
      name:
@@ -37,6 +86,22 @@
      parentId:
        description: 上级部门ID 一级部门传0
        type: integer
      remark:
        description: 备注
        type: string
    type: object
  request.AddDict:
    properties:
      dictType:
        allOf:
        - $ref: '#/definitions/constvar.DictType'
        description: 字典类型
      name:
        description: 名称
        type: string
      number:
        description: 编码
        type: string
      remark:
        description: 备注
        type: string
@@ -54,6 +119,24 @@
      parentId:
        description: 上级部门ID 一级部门传0
        type: integer
      remark:
        description: 备注
        type: string
    type: object
  request.UpdateDict:
    properties:
      dictType:
        allOf:
        - $ref: '#/definitions/constvar.DictType'
        description: 字典类型
      id:
        type: integer
      name:
        description: 名称
        type: string
      number:
        description: 编码
        type: string
      remark:
        description: 备注
        type: string
@@ -83,6 +166,107 @@
info:
  contact: {}
paths:
  /api-jl/v1/system/dict:
    get:
      parameters:
      - description: 字典类型
        enum:
        - 0
        - 1
        - 2
        - 3
        in: query
        name: dictType
        type: integer
        x-enum-comments:
          DictTypeColor: 颜色
          DictTypeMarket: 庄口
          DictTypeSpec: 规格
          DictTypeWorkshop: 车间
        x-enum-varnames:
        - DictTypeMarket
        - DictTypeWorkshop
        - DictTypeColor
        - DictTypeSpec
      - description: 页码
        in: query
        name: page
        type: integer
      - description: 每页大小
        in: query
        name: pageSize
        type: integer
      produces:
      - application/json
      responses:
        "200":
          description: 成功
          schema:
            allOf:
            - $ref: '#/definitions/util.ResponseList'
            - properties:
                data:
                  items:
                    $ref: '#/definitions/models.Dict'
                  type: array
              type: object
      summary: 查询字典列表
      tags:
      - 系统设置/字典
    post:
      parameters:
      - description: 字典信息
        in: body
        name: object
        required: true
        schema:
          $ref: '#/definitions/request.AddDict'
      produces:
      - application/json
      responses:
        "200":
          description: 成功
          schema:
            $ref: '#/definitions/util.Response'
      summary: 添加字典
      tags:
      - 系统设置/字典
    put:
      parameters:
      - description: 字典信息
        in: body
        name: object
        required: true
        schema:
          $ref: '#/definitions/request.UpdateDict'
      produces:
      - application/json
      responses:
        "200":
          description: 成功
          schema:
            $ref: '#/definitions/util.Response'
      summary: 编辑字典
      tags:
      - 系统设置/字典
  /api-jl/v1/system/dict/{id}:
    delete:
      parameters:
      - description: 字典信息
        in: path
        name: id
        required: true
        type: string
      produces:
      - application/json
      responses:
        "200":
          description: 成功
          schema:
            $ref: '#/definitions/util.Response'
      summary: 删除字典
      tags:
      - 系统设置/字典
  /api-s/v1/organize/department:
    get:
      parameters:
main.go
@@ -1,4 +1,4 @@
package jialian
package main
import (
    "jialian/conf"
@@ -29,9 +29,6 @@
        logx.Errorf("db init error! ", err.Error())
        return
    }
    // 启动APS RPC服务
    //safe.Go(service.StartAPServer)
    go shutdown()
    logx.Infof("server start serve...")
models/db.go
@@ -72,7 +72,9 @@
func RegisterTables() error {
    db := mysqlx.GetDB()
    err := db.AutoMigrate()
    err := db.AutoMigrate(
        Dict{},
    )
    return err
}
@@ -81,9 +83,7 @@
}
func InsertDefaultData() {
    models := []interface{}{
        NewDepartmentSearch(),
    }
    models := []interface{}{}
    for _, model := range models {
        if id, ok := model.(InitDefaultData); ok {
            if err := id.InitDefaultData(); err != nil {
models/department.go
@@ -196,7 +196,7 @@
    return records, total, nil
}
func (slf *DepartmentSearch) FindNotTotal() ([]*Department, error) {
func (slf *DepartmentSearch) FindAll() ([]*Department, error) {
    var (
        records = make([]*Department, 0)
        db      = slf.build()
models/dict.go
New file
@@ -0,0 +1,260 @@
package models
import (
    "fmt"
    "gorm.io/gorm"
    "jialian/constvar"
    "jialian/pkg/mysqlx"
)
type (
    // Dict 词典
    Dict struct {
        gorm.Model
        DictType constvar.DictType `gorm:"index;type:tinyint(3);not null;comment:字典类型"`         //字典类型
        Number   string            `gorm:"type:varchar(255);not null;comment:编号" json:"number"` //编号
        Name     string            `gorm:"type:varchar(255);not null;comment:名称" json:"name"`   //名称
        Remark   string            `gorm:"type:varchar(255);not null;comment:备注" json:"remark"` //备注
    }
    DictSearch struct {
        Dict
        Order    string
        PageNum  int
        PageSize int
        Orm      *gorm.DB
    }
)
func (slf *Dict) TableName() string {
    return "dict"
}
func NewDictSearch() *DictSearch {
    return &DictSearch{Orm: mysqlx.GetDB()}
}
func (slf *DictSearch) SetOrm(tx *gorm.DB) *DictSearch {
    slf.Orm = tx
    return slf
}
func (slf *DictSearch) SetPage(page, size int) *DictSearch {
    slf.PageNum, slf.PageSize = page, size
    return slf
}
func (slf *DictSearch) SetOrder(order string) *DictSearch {
    slf.Order = order
    return slf
}
func (slf *DictSearch) SetID(id uint) *DictSearch {
    slf.ID = id
    return slf
}
func (slf *DictSearch) SetNumber(number string) *DictSearch {
    slf.Number = number
    return slf
}
func (slf *DictSearch) SetName(name string) *DictSearch {
    slf.Name = name
    return slf
}
func (slf *DictSearch) SetDictType(dt constvar.DictType) *DictSearch {
    slf.DictType = dt
    return slf
}
func (slf *DictSearch) build() *gorm.DB {
    var db = slf.Orm.Table(slf.TableName())
    if slf.ID != 0 {
        db = db.Where("id = ?", slf.ID)
    }
    if slf.Order != "" {
        db = db.Order(slf.Order)
    }
    if slf.Number != "" {
        db = db.Where("number = ?", slf.Number)
    }
    if slf.DictType != 0 {
        db = db.Where("dict_type = ?", slf.DictType)
    }
    return db
}
// Create 单条插入
func (slf *DictSearch) Create(record *Dict) error {
    var db = slf.build()
    if err := db.Create(record).Error; err != nil {
        return fmt.Errorf("create err: %v, record: %+v", err, record)
    }
    return nil
}
// CreateBatch 批量插入
func (slf *DictSearch) CreateBatch(records []*Dict) error {
    var db = slf.build()
    if err := db.Create(&records).Error; err != nil {
        return fmt.Errorf("create batch err: %v, records: %+v", err, records)
    }
    return nil
}
func (slf *DictSearch) Save(record *Dict) error {
    var db = slf.build()
    if err := db.Omit("CreatedAt").Save(record).Error; err != nil {
        return fmt.Errorf("save err: %v, record: %+v", err, record)
    }
    return nil
}
func (slf *DictSearch) Update(record *Dict) error {
    var db = slf.build()
    if err := db.Updates(record).Error; err != nil {
        return fmt.Errorf("save err: %v, record: %+v", err, record)
    }
    return nil
}
func (slf *DictSearch) UpdateByMap(upMap map[string]interface{}) error {
    var (
        db = slf.build()
    )
    if err := db.Updates(upMap).Error; err != nil {
        return fmt.Errorf("update by map err: %v, upMap: %+v", err, upMap)
    }
    return nil
}
func (slf *DictSearch) UpdateByQuery(query string, args []interface{}, upMap map[string]interface{}) error {
    var (
        db = slf.Orm.Table(slf.TableName()).Where(query, args...)
    )
    if err := db.Updates(upMap).Error; err != nil {
        return fmt.Errorf("update by query err: %v, query: %s, args: %+v, upMap: %+v", err, query, args, upMap)
    }
    return nil
}
func (slf *DictSearch) Delete() error {
    var db = slf.build()
    if err := db.Unscoped().Delete(&Dict{}).Error; err != nil {
        return err
    }
    return nil
}
func (slf *DictSearch) First() (*Dict, error) {
    var (
        record = new(Dict)
        db     = slf.build()
    )
    if err := db.First(record).Error; err != nil {
        return record, err
    }
    return record, nil
}
func (slf *DictSearch) Find() ([]*Dict, int64, error) {
    var (
        records = make([]*Dict, 0)
        total   int64
        db      = slf.build()
    )
    if err := db.Count(&total).Error; err != nil {
        return records, total, fmt.Errorf("find count err: %v", err)
    }
    if slf.PageNum*slf.PageSize > 0 {
        db = db.Offset((slf.PageNum - 1) * slf.PageSize).Limit(slf.PageSize)
    }
    if err := db.Find(&records).Error; err != nil {
        return records, total, fmt.Errorf("find records err: %v", err)
    }
    return records, total, nil
}
func (slf *DictSearch) FindAll() ([]*Dict, error) {
    var (
        records = make([]*Dict, 0)
        db      = slf.build()
    )
    if slf.PageNum*slf.PageSize > 0 {
        db = db.Offset((slf.PageNum - 1) * slf.PageSize).Limit(slf.PageSize)
    }
    if err := db.Find(&records).Error; err != nil {
        return records, fmt.Errorf("find records err: %v", err)
    }
    return records, nil
}
// FindByQuery 指定条件查询.
func (slf *DictSearch) FindByQuery(query string, args []interface{}) ([]*Dict, int64, error) {
    var (
        records = make([]*Dict, 0)
        total   int64
        db      = slf.Orm.Table(slf.TableName()).Where(query, args...)
    )
    if err := db.Count(&total).Error; err != nil {
        return records, total, fmt.Errorf("find by query count err: %v", err)
    }
    if slf.PageNum*slf.PageSize > 0 {
        db = db.Offset((slf.PageNum - 1) * slf.PageSize).Limit(slf.PageSize)
    }
    if err := db.Find(&records).Error; err != nil {
        return records, total, fmt.Errorf("find by query records err: %v, query: %s, args: %+v", err, query, args)
    }
    return records, total, nil
}
// FindByQueryNotTotal 指定条件查询&不查询总条数.
func (slf *DictSearch) FindByQueryNotTotal(query string, args []interface{}) ([]*Dict, error) {
    var (
        records = make([]*Dict, 0)
        db      = slf.Orm.Table(slf.TableName()).Where(query, args...)
    )
    if slf.PageNum*slf.PageSize > 0 {
        db = db.Offset((slf.PageNum - 1) * slf.PageSize).Limit(slf.PageSize)
    }
    if err := db.Find(&records).Error; err != nil {
        return records, fmt.Errorf("find by query records err: %v, query: %s, args: %+v", err, query, args)
    }
    return records, nil
}
// InitDefaultData 初始化数据
func (slf *DictSearch) InitDefaultData() error {
    return nil
}
router/router.go
@@ -19,7 +19,7 @@
    r.StaticFS(conf.LocalConf.StorePath, http.Dir(conf.LocalConf.StorePath)) // 为用户头像和文件提供静态地址
    r.GET("/swagger/*any", ginSwagger.WrapHandler(swaggerFiles.Handler))
    urlPrefix := "/api-s/v1"
    urlPrefix := "/api-jl/v1"
    // 组织管理
    departmentController := new(controllers.DepartmentController)
@@ -31,5 +31,15 @@
        organizeAPI.DELETE("department/:id", departmentController.Delete) // 删除部门
    }
    // 词典管理
    DictController := new(controllers.DictController)
    systemApi := r.Group(urlPrefix + "/system")
    {
        systemApi.GET("dict", DictController.List)          // 获取词典列表
        systemApi.POST("dict", DictController.Add)          // 新增词典
        systemApi.PUT("dict", DictController.Update)        // 修改词典
        systemApi.DELETE("dict/:id", DictController.Delete) // 删除词典
    }
    return r
}