zhangqian
2023-09-12 fb409de4b6ad6bf4d4d0c5ddf652687fdfe7bacb
仓库增删改查接口
3个文件已删除
4个文件已添加
7个文件已修改
1320 ■■■■■ 已修改文件
conf/config.yaml 2 ●●● 补丁 | 查看 | 原始文档 | blame | 历史
constvar/global.go 9 ●●●●● 补丁 | 查看 | 原始文档 | blame | 历史
controllers/warehouse.go 153 ●●●●● 补丁 | 查看 | 原始文档 | blame | 历史
docs/docs.go 271 ●●●●● 补丁 | 查看 | 原始文档 | blame | 历史
docs/swagger.json 271 ●●●●● 补丁 | 查看 | 原始文档 | blame | 历史
docs/swagger.yaml 182 ●●●●● 补丁 | 查看 | 原始文档 | blame | 历史
main.go 24 ●●●●● 补丁 | 查看 | 原始文档 | blame | 历史
models/db.go 4 ●●● 补丁 | 查看 | 原始文档 | blame | 历史
models/model.go 17 ●●●●● 补丁 | 查看 | 原始文档 | blame | 历史
models/warehouse.go 249 ●●●●● 补丁 | 查看 | 原始文档 | blame | 历史
request/warehouse.go 20 ●●●●● 补丁 | 查看 | 原始文档 | blame | 历史
router/router.go 12 ●●●●● 补丁 | 查看 | 原始文档 | blame | 历史
service/initdb.go 79 ●●●●● 补丁 | 查看 | 原始文档 | blame | 历史
service/initdb_mysql.go 27 ●●●●● 补丁 | 查看 | 原始文档 | blame | 历史
conf/config.yaml
@@ -7,7 +7,7 @@
  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)/wms?charset=utf8&parseTime=True&loc=Local
  logMode: true
  maxIdleCon: 20
  maxOpenCon: 100
constvar/global.go
File was deleted
controllers/warehouse.go
New file
@@ -0,0 +1,153 @@
package controllers
import (
    "errors"
    "fmt"
    "github.com/gin-gonic/gin"
    "github.com/spf13/cast"
    "gorm.io/gorm"
    "wms/extend/code"
    "wms/extend/util"
    "wms/models"
    "wms/pkg/logx"
    "wms/pkg/structx"
    "wms/request"
)
type WarehouseController struct{}
// Add
// @Tags      仓库
// @Summary   添加仓库
// @Produce   application/json
// @Param     object  body  request.AddWarehouse true  "仓库信息"
// @Success   200 {object} util.Response "成功"
// @Router    /api-wms/v1/warehouse/warehouse [post]
func (slf WarehouseController) Add(c *gin.Context) {
    var reqParams request.AddWarehouse
    var params models.Warehouse
    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.NewWarehouseSearch().Create(&params); err != nil {
        logx.Errorf("warehouse create err: %v", err)
        util.ResponseFormat(c, code.SaveFail, "插入失败")
        return
    }
    util.ResponseFormat(c, code.Success, "添加成功")
}
// Update
// @Tags      仓库
// @Summary   编辑仓库
// @Produce   application/json
// @Param     object  body request.UpdateWarehouse true  "仓库信息"
// @Param     id  path string true  "仓库id"
// @Success   200 {object} util.Response "成功"
// @Router    /api-wms/v1/warehouse/warehouse/{id} [put]
func (slf WarehouseController) Update(c *gin.Context) {
    id := cast.ToUint(c.Param("id"))
    if id == 0 {
        util.ResponseFormat(c, code.RequestParamError, "空的记录id")
        return
    }
    var (
        reqParams request.UpdateWarehouse
        params    models.Warehouse
    )
    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
    }
    params.ID = id
    if err := slf.ParamsCheck(params); err != nil {
        util.ResponseFormat(c, code.RequestParamError, err.Error())
        return
    }
    err := models.NewWarehouseSearch().SetID(params.ID).Update(&params)
    if err != nil {
        util.ResponseFormat(c, code.RequestParamError, "修改失败")
        return
    }
    util.ResponseFormat(c, code.UpdateSuccess, "更新成功")
}
func (slf WarehouseController) ParamsCheck(params models.Warehouse) (err error) {
    var oldRecord *models.Warehouse
    if params.ID != 0 {
        oldRecord, err = models.NewWarehouseSearch().SetID(params.ID).First()
        if err == gorm.ErrRecordNotFound {
            return errors.New("记录不存在")
        }
    }
    if oldRecord == nil || params.Code != oldRecord.Code {
        _, err = models.NewWarehouseSearch().SetCode(params.Code).First()
        if err != gorm.ErrRecordNotFound {
            return errors.New("仓库编号重复")
        }
    }
    return nil
}
// List
// @Tags      仓库
// @Summary   查询仓库列表
// @Produce   application/json
// @Param     object  query    request.GetWarehouseList true  "查询参数"
// @Success   200   {object}  util.ResponseList{data=[]models.Warehouse}  "成功"
// @Router    /api-wms/v1/warehouse/warehouse [get]
func (slf WarehouseController) List(c *gin.Context) {
    var params request.GetWarehouseList
    if err := c.ShouldBindQuery(&params); err != nil {
        util.ResponseFormat(c, code.RequestParamError, err.Error())
        return
    }
    list, total, err := models.NewWarehouseSearch().SetPage(params.Page, params.PageSize).SetKeyword(params.Keyword).SetOrder("id desc").Find()
    if err != nil {
        util.ResponseFormat(c, code.RequestParamError, "查找失败")
        return
    }
    util.ResponseFormatList(c, code.Success, list, cast.ToInt(total))
}
// Delete
// @Tags      仓库
// @Summary   删除仓库
// @Produce   application/json
// @Param     id  path string true  "仓库id"
// @Success   200 {object} util.Response "成功"
// @Router    /api-wms/v1/warehouse/warehouse/{id} [delete]
func (slf WarehouseController) Delete(c *gin.Context) {
    id := cast.ToUint(c.Param("id"))
    if id == 0 {
        util.ResponseFormat(c, code.RequestParamError, "空的记录id")
        return
    }
    err := models.NewWarehouseSearch().SetID(id).Delete()
    if err != nil {
        util.ResponseFormat(c, code.RequestParamError, "删除失败")
        return
    }
    util.ResponseFormat(c, code.UpdateSuccess, "删除成功")
}
docs/docs.go
@@ -149,6 +149,150 @@
                    }
                }
            }
        },
        "/api-wms/v1/warehouse/warehouse": {
            "get": {
                "produces": [
                    "application/json"
                ],
                "tags": [
                    "仓库"
                ],
                "summary": "查询仓库列表",
                "parameters": [
                    {
                        "type": "string",
                        "name": "keyword",
                        "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.Warehouse"
                                            }
                                        }
                                    }
                                }
                            ]
                        }
                    }
                }
            },
            "post": {
                "produces": [
                    "application/json"
                ],
                "tags": [
                    "仓库"
                ],
                "summary": "添加仓库",
                "parameters": [
                    {
                        "description": "仓库信息",
                        "name": "object",
                        "in": "body",
                        "required": true,
                        "schema": {
                            "$ref": "#/definitions/request.AddWarehouse"
                        }
                    }
                ],
                "responses": {
                    "200": {
                        "description": "成功",
                        "schema": {
                            "$ref": "#/definitions/util.Response"
                        }
                    }
                }
            }
        },
        "/api-wms/v1/warehouse/warehouse/{id}": {
            "put": {
                "produces": [
                    "application/json"
                ],
                "tags": [
                    "仓库"
                ],
                "summary": "编辑仓库",
                "parameters": [
                    {
                        "description": "仓库信息",
                        "name": "object",
                        "in": "body",
                        "required": true,
                        "schema": {
                            "$ref": "#/definitions/request.UpdateWarehouse"
                        }
                    },
                    {
                        "type": "string",
                        "description": "仓库id",
                        "name": "id",
                        "in": "path",
                        "required": true
                    }
                ],
                "responses": {
                    "200": {
                        "description": "成功",
                        "schema": {
                            "$ref": "#/definitions/util.Response"
                        }
                    }
                }
            },
            "delete": {
                "produces": [
                    "application/json"
                ],
                "tags": [
                    "仓库"
                ],
                "summary": "删除仓库",
                "parameters": [
                    {
                        "type": "string",
                        "description": "仓库id",
                        "name": "id",
                        "in": "path",
                        "required": true
                    }
                ],
                "responses": {
                    "200": {
                        "description": "成功",
                        "schema": {
                            "$ref": "#/definitions/util.Response"
                        }
                    }
                }
            }
        }
    },
    "definitions": {
@@ -190,6 +334,56 @@
                }
            }
        },
        "models.Warehouse": {
            "type": "object",
            "required": [
                "code"
            ],
            "properties": {
                "active": {
                    "description": "是否启用,传true就行",
                    "type": "boolean"
                },
                "buyToResupply": {
                    "description": "是否购买补给,已购买产品能够发送到此仓库",
                    "type": "boolean"
                },
                "code": {
                    "description": "仓库编码",
                    "type": "string",
                    "maxLength": 5,
                    "minLength": 1
                },
                "createTime": {
                    "type": "string"
                },
                "name": {
                    "description": "仓库名称",
                    "type": "string"
                },
                "partnerId": {
                    "description": "合作伙伴id",
                    "type": "integer"
                },
                "resupplyWh": {
                    "description": "补给来源仓库",
                    "type": "array",
                    "items": {
                        "$ref": "#/definitions/models.Warehouse"
                    }
                },
                "resupplyWhIds": {
                    "description": "补给来源仓库ID",
                    "type": "array",
                    "items": {
                        "type": "string"
                    }
                },
                "updateTime": {
                    "type": "string"
                }
            }
        },
        "request.AddDepartment": {
            "type": "object",
            "properties": {
@@ -208,6 +402,43 @@
                "remark": {
                    "description": "备注",
                    "type": "string"
                }
            }
        },
        "request.AddWarehouse": {
            "type": "object",
            "required": [
                "code"
            ],
            "properties": {
                "active": {
                    "description": "是否启用,传true就行",
                    "type": "boolean"
                },
                "buyToResupply": {
                    "description": "购买补给,已购买产品能够发送到此仓库",
                    "type": "boolean"
                },
                "code": {
                    "description": "仓库编码",
                    "type": "string",
                    "maxLength": 5,
                    "minLength": 1
                },
                "name": {
                    "description": "仓库名称",
                    "type": "string"
                },
                "partnerId": {
                    "description": "合作伙伴id",
                    "type": "integer"
                },
                "resupplyWhIds": {
                    "description": "补给来源仓库ID",
                    "type": "array",
                    "items": {
                        "type": "string"
                    }
                }
            }
        },
@@ -235,6 +466,46 @@
                }
            }
        },
        "request.UpdateWarehouse": {
            "type": "object",
            "required": [
                "code"
            ],
            "properties": {
                "active": {
                    "description": "是否启用,传true就行",
                    "type": "boolean"
                },
                "buyToResupply": {
                    "description": "购买补给,已购买产品能够发送到此仓库",
                    "type": "boolean"
                },
                "code": {
                    "description": "仓库编码",
                    "type": "string",
                    "maxLength": 5,
                    "minLength": 1
                },
                "id": {
                    "type": "integer"
                },
                "name": {
                    "description": "仓库名称",
                    "type": "string"
                },
                "partnerId": {
                    "description": "合作伙伴id",
                    "type": "integer"
                },
                "resupplyWhIds": {
                    "description": "补给来源仓库ID",
                    "type": "array",
                    "items": {
                        "type": "string"
                    }
                }
            }
        },
        "util.Response": {
            "type": "object",
            "properties": {
docs/swagger.json
@@ -137,6 +137,150 @@
                    }
                }
            }
        },
        "/api-wms/v1/warehouse/warehouse": {
            "get": {
                "produces": [
                    "application/json"
                ],
                "tags": [
                    "仓库"
                ],
                "summary": "查询仓库列表",
                "parameters": [
                    {
                        "type": "string",
                        "name": "keyword",
                        "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.Warehouse"
                                            }
                                        }
                                    }
                                }
                            ]
                        }
                    }
                }
            },
            "post": {
                "produces": [
                    "application/json"
                ],
                "tags": [
                    "仓库"
                ],
                "summary": "添加仓库",
                "parameters": [
                    {
                        "description": "仓库信息",
                        "name": "object",
                        "in": "body",
                        "required": true,
                        "schema": {
                            "$ref": "#/definitions/request.AddWarehouse"
                        }
                    }
                ],
                "responses": {
                    "200": {
                        "description": "成功",
                        "schema": {
                            "$ref": "#/definitions/util.Response"
                        }
                    }
                }
            }
        },
        "/api-wms/v1/warehouse/warehouse/{id}": {
            "put": {
                "produces": [
                    "application/json"
                ],
                "tags": [
                    "仓库"
                ],
                "summary": "编辑仓库",
                "parameters": [
                    {
                        "description": "仓库信息",
                        "name": "object",
                        "in": "body",
                        "required": true,
                        "schema": {
                            "$ref": "#/definitions/request.UpdateWarehouse"
                        }
                    },
                    {
                        "type": "string",
                        "description": "仓库id",
                        "name": "id",
                        "in": "path",
                        "required": true
                    }
                ],
                "responses": {
                    "200": {
                        "description": "成功",
                        "schema": {
                            "$ref": "#/definitions/util.Response"
                        }
                    }
                }
            },
            "delete": {
                "produces": [
                    "application/json"
                ],
                "tags": [
                    "仓库"
                ],
                "summary": "删除仓库",
                "parameters": [
                    {
                        "type": "string",
                        "description": "仓库id",
                        "name": "id",
                        "in": "path",
                        "required": true
                    }
                ],
                "responses": {
                    "200": {
                        "description": "成功",
                        "schema": {
                            "$ref": "#/definitions/util.Response"
                        }
                    }
                }
            }
        }
    },
    "definitions": {
@@ -178,6 +322,56 @@
                }
            }
        },
        "models.Warehouse": {
            "type": "object",
            "required": [
                "code"
            ],
            "properties": {
                "active": {
                    "description": "是否启用,传true就行",
                    "type": "boolean"
                },
                "buyToResupply": {
                    "description": "是否购买补给,已购买产品能够发送到此仓库",
                    "type": "boolean"
                },
                "code": {
                    "description": "仓库编码",
                    "type": "string",
                    "maxLength": 5,
                    "minLength": 1
                },
                "createTime": {
                    "type": "string"
                },
                "name": {
                    "description": "仓库名称",
                    "type": "string"
                },
                "partnerId": {
                    "description": "合作伙伴id",
                    "type": "integer"
                },
                "resupplyWh": {
                    "description": "补给来源仓库",
                    "type": "array",
                    "items": {
                        "$ref": "#/definitions/models.Warehouse"
                    }
                },
                "resupplyWhIds": {
                    "description": "补给来源仓库ID",
                    "type": "array",
                    "items": {
                        "type": "string"
                    }
                },
                "updateTime": {
                    "type": "string"
                }
            }
        },
        "request.AddDepartment": {
            "type": "object",
            "properties": {
@@ -196,6 +390,43 @@
                "remark": {
                    "description": "备注",
                    "type": "string"
                }
            }
        },
        "request.AddWarehouse": {
            "type": "object",
            "required": [
                "code"
            ],
            "properties": {
                "active": {
                    "description": "是否启用,传true就行",
                    "type": "boolean"
                },
                "buyToResupply": {
                    "description": "购买补给,已购买产品能够发送到此仓库",
                    "type": "boolean"
                },
                "code": {
                    "description": "仓库编码",
                    "type": "string",
                    "maxLength": 5,
                    "minLength": 1
                },
                "name": {
                    "description": "仓库名称",
                    "type": "string"
                },
                "partnerId": {
                    "description": "合作伙伴id",
                    "type": "integer"
                },
                "resupplyWhIds": {
                    "description": "补给来源仓库ID",
                    "type": "array",
                    "items": {
                        "type": "string"
                    }
                }
            }
        },
@@ -223,6 +454,46 @@
                }
            }
        },
        "request.UpdateWarehouse": {
            "type": "object",
            "required": [
                "code"
            ],
            "properties": {
                "active": {
                    "description": "是否启用,传true就行",
                    "type": "boolean"
                },
                "buyToResupply": {
                    "description": "购买补给,已购买产品能够发送到此仓库",
                    "type": "boolean"
                },
                "code": {
                    "description": "仓库编码",
                    "type": "string",
                    "maxLength": 5,
                    "minLength": 1
                },
                "id": {
                    "type": "integer"
                },
                "name": {
                    "description": "仓库名称",
                    "type": "string"
                },
                "partnerId": {
                    "description": "合作伙伴id",
                    "type": "integer"
                },
                "resupplyWhIds": {
                    "description": "补给来源仓库ID",
                    "type": "array",
                    "items": {
                        "type": "string"
                    }
                }
            }
        },
        "util.Response": {
            "type": "object",
            "properties": {
docs/swagger.yaml
@@ -26,6 +26,42 @@
        description: 排序
        type: integer
    type: object
  models.Warehouse:
    properties:
      active:
        description: 是否启用,传true就行
        type: boolean
      buyToResupply:
        description: 是否购买补给,已购买产品能够发送到此仓库
        type: boolean
      code:
        description: 仓库编码
        maxLength: 5
        minLength: 1
        type: string
      createTime:
        type: string
      name:
        description: 仓库名称
        type: string
      partnerId:
        description: 合作伙伴id
        type: integer
      resupplyWh:
        description: 补给来源仓库
        items:
          $ref: '#/definitions/models.Warehouse'
        type: array
      resupplyWhIds:
        description: 补给来源仓库ID
        items:
          type: string
        type: array
      updateTime:
        type: string
    required:
    - code
    type: object
  request.AddDepartment:
    properties:
      name:
@@ -40,6 +76,33 @@
      remark:
        description: 备注
        type: string
    type: object
  request.AddWarehouse:
    properties:
      active:
        description: 是否启用,传true就行
        type: boolean
      buyToResupply:
        description: 购买补给,已购买产品能够发送到此仓库
        type: boolean
      code:
        description: 仓库编码
        maxLength: 5
        minLength: 1
        type: string
      name:
        description: 仓库名称
        type: string
      partnerId:
        description: 合作伙伴id
        type: integer
      resupplyWhIds:
        description: 补给来源仓库ID
        items:
          type: string
        type: array
    required:
    - code
    type: object
  request.UpdateDepartment:
    properties:
@@ -57,6 +120,35 @@
      remark:
        description: 备注
        type: string
    type: object
  request.UpdateWarehouse:
    properties:
      active:
        description: 是否启用,传true就行
        type: boolean
      buyToResupply:
        description: 购买补给,已购买产品能够发送到此仓库
        type: boolean
      code:
        description: 仓库编码
        maxLength: 5
        minLength: 1
        type: string
      id:
        type: integer
      name:
        description: 仓库名称
        type: string
      partnerId:
        description: 合作伙伴id
        type: integer
      resupplyWhIds:
        description: 补给来源仓库ID
        items:
          type: string
        type: array
    required:
    - code
    type: object
  util.Response:
    properties:
@@ -166,4 +258,94 @@
      summary: 编辑部门信息
      tags:
      - 部门信息
  /api-wms/v1/warehouse/warehouse:
    get:
      parameters:
      - in: query
        name: keyword
        type: string
      - 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.Warehouse'
                  type: array
              type: object
      summary: 查询仓库列表
      tags:
      - 仓库
    post:
      parameters:
      - description: 仓库信息
        in: body
        name: object
        required: true
        schema:
          $ref: '#/definitions/request.AddWarehouse'
      produces:
      - application/json
      responses:
        "200":
          description: 成功
          schema:
            $ref: '#/definitions/util.Response'
      summary: 添加仓库
      tags:
      - 仓库
  /api-wms/v1/warehouse/warehouse/{id}:
    delete:
      parameters:
      - description: 仓库id
        in: path
        name: id
        required: true
        type: string
      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.UpdateWarehouse'
      - description: 仓库id
        in: path
        name: id
        required: true
        type: string
      produces:
      - application/json
      responses:
        "200":
          description: 成功
          schema:
            $ref: '#/definitions/util.Response'
      summary: 编辑仓库
      tags:
      - 仓库
swagger: "2.0"
main.go
@@ -1,13 +1,13 @@
package main
import (
    "context"
    "net/http"
    "os"
    "os/signal"
    "syscall"
    "time"
    "wms/conf"
    "wms/constvar"
    "wms/models"
    "wms/pkg/logx"
    "wms/router"
@@ -30,10 +30,6 @@
        return
    }
    // 启动APS RPC服务
    //safe.Go(service.StartAPServer)
    go shutdown()
    logx.Infof("aps-server start serve...")
    server := &http.Server{
        Addr:         ":" + conf.WebConf.Port,
@@ -41,16 +37,24 @@
        ReadTimeout:  5 * time.Second,
        WriteTimeout: 5 * time.Second,
    }
    go shutdown(server)
    logx.Error(server.ListenAndServe().Error())
}
func shutdown() {
func shutdown(server *http.Server) {
    quit := make(chan os.Signal, 1)
    signal.Notify(quit, syscall.SIGKILL, syscall.SIGQUIT, syscall.SIGINT, syscall.SIGTERM)
    <-quit
    _ = constvar.GrpcClient.Close()
    logx.Infof("aps-server exited...")
    os.Exit(0)
    logx.Infof("server exiting...")
    // 创建一个上下文,设置超时时间
    ctx, cancel := context.WithTimeout(context.Background(), 10*time.Second)
    defer cancel()
    // 关闭HTTP服务器
    if err := server.Shutdown(ctx); err != nil {
        logx.Infof("服务优雅退出失败: %v", err)
        return
    }
    logx.Infof("server exited")
}
models/db.go
@@ -72,7 +72,9 @@
func RegisterTables() error {
    db := mysqlx.GetDB()
    err := db.AutoMigrate()
    err := db.AutoMigrate(
        Warehouse{},
    )
    return err
}
models/model.go
New file
@@ -0,0 +1,17 @@
package models
import (
    "gorm.io/gorm"
)
type WmsModel struct {
    gorm.Model `json:"-"`
    CreateTime string `json:"createTime"  gorm:"-"`
    UpdateTime string `json:"updateTime"  gorm:"-"`
}
func (slf *WmsModel) AfterFind(tx *gorm.DB) (err error) {
    slf.CreateTime = slf.CreatedAt.Format("2006-01-02 15:04")
    slf.UpdateTime = slf.UpdatedAt.Format("2006-01-02 15:04")
    return nil
}
models/warehouse.go
New file
@@ -0,0 +1,249 @@
package models
import (
    "fmt"
    "gorm.io/gorm"
    "strings"
    "wms/pkg/mysqlx"
)
type (
    // Warehouse 部门信息
    Warehouse struct {
        WmsModel
        Name             string       `json:"name" gorm:"index;type:varchar(255);not null;comment:仓库名称"`                                 //仓库名称
        Active           bool         `json:"active" gorm:"type:tinyint(1);not null;comment:是否激活"`                                       //是否启用,传true就行
        Code             string       `json:"code" binding:"required,min=1,max=5"  gorm:"index;type:varchar(255);not null;comment:仓库编码"` //仓库编码
        PartnerID        int          `json:"partnerId"  gorm:"type:int;not null;comment:合作伙伴id"`                                        //合作伙伴id
        BuyToResupply    bool         `json:"buyToResupply" gorm:"type:tinyint(1);not null;comment:是否购买补给"`                              //是否购买补给,已购买产品能够发送到此仓库
        ResupplyWhIdsStr string       `json:"-" gorm:"column:resupply_wh_ids;type:varchar(255);not null;comment:补给来源仓库ID"`               //补给来源仓库ID
        ResupplyWhIds    []string     `json:"resupplyWhIds" gorm:"-"`                                                                    //补给来源仓库ID
        ResupplyWh       []*Warehouse `json:"resupplyWh" gorm:"-"`                                                                       //补给来源仓库
    }
    WarehouseSearch struct {
        Warehouse
        Order    string
        PageNum  int
        PageSize int
        Keyword  string
        Orm      *gorm.DB
    }
)
func (slf *Warehouse) TableName() string {
    return "warehouse"
}
func (slf *Warehouse) BeforeCreate(tx *gorm.DB) (err error) {
    slf.ResupplyWhIdsStr = strings.Join(slf.ResupplyWhIds, ",")
    return nil
}
func NewWarehouseSearch() *WarehouseSearch {
    return &WarehouseSearch{Orm: mysqlx.GetDB()}
}
func (slf *WarehouseSearch) SetOrm(tx *gorm.DB) *WarehouseSearch {
    slf.Orm = tx
    return slf
}
func (slf *WarehouseSearch) SetPage(page, size int) *WarehouseSearch {
    slf.PageNum, slf.PageSize = page, size
    return slf
}
func (slf *WarehouseSearch) SetOrder(order string) *WarehouseSearch {
    slf.Order = order
    return slf
}
func (slf *WarehouseSearch) SetID(id uint) *WarehouseSearch {
    slf.ID = id
    return slf
}
func (slf *WarehouseSearch) SetCode(code string) *WarehouseSearch {
    slf.Code = code
    return slf
}
func (slf *WarehouseSearch) SetName(name string) *WarehouseSearch {
    slf.Name = name
    return slf
}
func (slf *WarehouseSearch) SetKeyword(keyword string) *WarehouseSearch {
    slf.Keyword = keyword
    return slf
}
func (slf *WarehouseSearch) 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.Keyword != "" {
        db = db.Where("name like ?", fmt.Sprintf("%%%v%%", slf.Keyword))
    }
    if slf.Name != "" {
        db = db.Where("name = ?", slf.Name)
    }
    return db
}
// Create 单条插入
func (slf *WarehouseSearch) Create(record *Warehouse) error {
    var db = slf.build()
    if err := db.Create(record).Error; err != nil {
        return err
    }
    return nil
}
// CreateBatch 批量插入
func (slf *WarehouseSearch) CreateBatch(records []*Warehouse) 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 *WarehouseSearch) Update(record *Warehouse) error {
    var db = slf.build()
    if err := db.Omit("CreatedAt").Updates(record).Error; err != nil {
        return fmt.Errorf("save err: %v, record: %+v", err, record)
    }
    return nil
}
func (slf *WarehouseSearch) 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 *WarehouseSearch) 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 *WarehouseSearch) Delete() error {
    var db = slf.build()
    return db.Delete(&Warehouse{}).Error
}
func (slf *WarehouseSearch) First() (*Warehouse, error) {
    var (
        record = new(Warehouse)
        db     = slf.build()
    )
    if err := db.First(record).Error; err != nil {
        return record, err
    }
    return record, nil
}
func (slf *WarehouseSearch) Find() ([]*Warehouse, int64, error) {
    var (
        records = make([]*Warehouse, 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 *WarehouseSearch) FindNotTotal() ([]*Warehouse, error) {
    var (
        records = make([]*Warehouse, 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 *WarehouseSearch) FindByQuery(query string, args []interface{}) ([]*Warehouse, int64, error) {
    var (
        records = make([]*Warehouse, 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 *WarehouseSearch) FindByQueryNotTotal(query string, args []interface{}) ([]*Warehouse, error) {
    var (
        records = make([]*Warehouse, 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
}
request/warehouse.go
New file
@@ -0,0 +1,20 @@
package request
type GetWarehouseList struct {
    PageInfo
    Keyword string `json:"keyword"`
}
type AddWarehouse struct {
    Name          string   `json:"name" gorm:"index;type:varchar(255);not null;comment:仓库名称"`                                 //仓库名称
    Active        bool     `json:"active" gorm:"type:tinyint(1);not null;comment:是否激活"`                                       //是否启用,传true就行
    Code          string   `json:"code" binding:"required,min=1,max=5"  gorm:"index;type:varchar(255);not null;comment:仓库编码"` //仓库编码
    PartnerID     int      `json:"partnerId"  gorm:"type:int;not null;comment:合作伙伴id"`                                        //合作伙伴id
    BuyToResupply bool     `json:"buyToResupply"`                                                                             //购买补给,已购买产品能够发送到此仓库
    ResupplyWhIds []string `json:"resupplyWhIds"`                                                                             //补给来源仓库ID
}
type UpdateWarehouse struct {
    ID uint `gorm:"comment:主键ID;primaryKey;" json:"id"`
    AddWarehouse
}
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-wms/v1"
    // 组织管理
    departmentController := new(controllers.DepartmentController)
@@ -31,5 +31,15 @@
        organizeAPI.DELETE("department/:id", departmentController.Delete) // 删除部门
    }
    // 仓库管理
    warehouseController := new(controllers.WarehouseController)
    warehouseAPI := r.Group(urlPrefix + "/warehouse")
    {
        warehouseAPI.GET("warehouse", warehouseController.List)          // 获取仓库列表
        warehouseAPI.POST("warehouse", warehouseController.Add)          // 新增仓库
        warehouseAPI.PUT("warehouse/:id", warehouseController.Update)    // 修改仓库
        warehouseAPI.DELETE("warehouse/:id", warehouseController.Delete) // 删除仓库
    }
    return r
}
service/initdb.go
File was deleted
service/initdb_mysql.go
File was deleted