From fe5cd122cf94f83f97a1e71a391cefd62f219938 Mon Sep 17 00:00:00 2001 From: zhangqian <zhangqian@123.com> Date: 星期三, 28 二月 2024 20:26:32 +0800 Subject: [PATCH] 字段增删改查接口 --- controllers/department.go | 4 models/dict.go | 260 ++++++++++++ controllers/request/dict.go | 20 docs/swagger.yaml | 184 ++++++++ docs/docs.go | 279 +++++++++++++ conf/config.yaml | 15 docs/swagger.json | 279 +++++++++++++ router/router.go | 12 constvar/const.go | 9 controllers/dict.go | 149 +++++++ controllers/request/department.go | 0 models/department.go | 2 models/db.go | 8 main.go | 5 controllers/request/common.go | 0 controllers/response/common.go | 0 16 files changed, 1,201 insertions(+), 25 deletions(-) diff --git a/conf/config.yaml b/conf/config.yaml index d75d1b6..ede21b4 100644 --- a/conf/config.yaml +++ b/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 diff --git a/constvar/const.go b/constvar/const.go index c2ae672..16cb4da 100644 --- a/constvar/const.go +++ b/constvar/const.go @@ -1 +1,10 @@ package constvar + +type DictType int + +const ( + DictTypeMarket DictType = iota //搴勫彛 + DictTypeWorkshop //杞﹂棿 + DictTypeColor //棰滆壊 + DictTypeSpec //瑙勬牸 +) diff --git a/controllers/department.go b/controllers/department.go index bd19708..c35c97d 100644 --- a/controllers/department.go +++ b/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) } diff --git a/controllers/dict.go b/controllers/dict.go new file mode 100644 index 0000000..1bd386c --- /dev/null +++ b/controllers/dict.go @@ -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, "鍙傛暟瑙f瀽澶辫触锛屾暟鎹被鍨嬮敊璇�") + return + } + if err := structx.AssignTo(reqParams, ¶ms); 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(¶ms); 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("鍙傛暟瑙f瀽澶辫触: %v"+err.Error())) + return + } + if err := structx.AssignTo(reqParams, ¶ms); 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(¶ms) + + 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(¶ms); 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, "鍒犻櫎鎴愬姛") +} diff --git a/request/common.go b/controllers/request/common.go similarity index 100% rename from request/common.go rename to controllers/request/common.go diff --git a/request/department.go b/controllers/request/department.go similarity index 100% rename from request/department.go rename to controllers/request/department.go diff --git a/controllers/request/dict.go b/controllers/request/dict.go new file mode 100644 index 0000000..372beb2 --- /dev/null +++ b/controllers/request/dict.go @@ -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 +} diff --git a/response/common.go b/controllers/response/common.go similarity index 100% rename from response/common.go rename to controllers/response/common.go diff --git a/docs/docs.go b/docs/docs.go index 491292f..fc40334 100644 --- a/docs/docs.go +++ b/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": { diff --git a/docs/swagger.json b/docs/swagger.json index 2c096c4..cd8e1d4 100644 --- a/docs/swagger.json +++ b/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": { diff --git a/docs/swagger.yaml b/docs/swagger.yaml index de92cf1..38da383 100644 --- a/docs/swagger.yaml +++ b/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: diff --git a/main.go b/main.go index 4bd4b4c..8929e55 100644 --- a/main.go +++ b/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...") diff --git a/models/db.go b/models/db.go index a909796..ce81ab1 100644 --- a/models/db.go +++ b/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 { diff --git a/models/department.go b/models/department.go index 53a54f1..56d003b 100644 --- a/models/department.go +++ b/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() diff --git a/models/dict.go b/models/dict.go new file mode 100644 index 0000000..7da1803 --- /dev/null +++ b/models/dict.go @@ -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 +} diff --git a/router/router.go b/router/router.go index ac5f169..c24fa3c 100644 --- a/router/router.go +++ b/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 } -- Gitblit v1.8.0