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 CompanyController struct{}
|
|
// Add
|
// @Tags 公司
|
// @Summary 添加公司
|
// @Produce application/json
|
// @Param object body request.AddCompany true "公司信息"
|
// @Success 200 {object} util.Response "成功"
|
// @Router /api-wms/v1/company/company [post]
|
func (slf CompanyController) Add(c *gin.Context) {
|
var reqParams request.AddCompany
|
var params models.Company
|
if err := c.BindJSON(&reqParams); err != nil {
|
util.ResponseFormat(c, code.RequestParamError, "参数解析失败,数据类型错误")
|
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.NewCompanySearch().Create(¶ms); err != nil {
|
logx.Errorf("Company 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.UpdateCompany true "公司信息"
|
// @Param id path string true "公司id"
|
// @Success 200 {object} util.Response "成功"
|
// @Router /api-wms/v1/company/company/{id} [put]
|
func (slf CompanyController) Update(c *gin.Context) {
|
id := cast.ToUint(c.Param("id"))
|
if id == 0 {
|
util.ResponseFormat(c, code.RequestParamError, "空的记录id")
|
return
|
}
|
var (
|
reqParams request.UpdateCompany
|
params models.Company
|
)
|
if err := c.BindJSON(&reqParams); err != nil {
|
util.ResponseFormat(c, code.RequestParamError, fmt.Sprintf("参数解析失败: %v"+err.Error()))
|
return
|
}
|
if err := structx.AssignTo(reqParams, ¶ms); 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.NewCompanySearch().SetID(params.ID).Update(¶ms)
|
|
if err != nil {
|
util.ResponseFormat(c, code.RequestParamError, "修改失败")
|
return
|
}
|
|
util.ResponseFormat(c, code.UpdateSuccess, "更新成功")
|
}
|
|
func (slf CompanyController) ParamsCheck(params models.Company) (err error) {
|
var oldRecord *models.Company
|
if params.ID != 0 {
|
oldRecord, err = models.NewCompanySearch().SetID(params.ID).First()
|
if err == gorm.ErrRecordNotFound {
|
return errors.New("记录不存在")
|
}
|
}
|
if oldRecord == nil || params.Name != oldRecord.Name {
|
_, err = models.NewCompanySearch().SetName(params.Name).First()
|
if err != gorm.ErrRecordNotFound {
|
return errors.New("公司名称重复")
|
}
|
}
|
|
return nil
|
}
|
|
// List
|
// @Tags 公司
|
// @Summary 查询公司列表
|
// @Produce application/json
|
// @Param object query request.GetCompanyList true "查询参数"
|
// @Success 200 {object} util.ResponseList{data=[]models.Company} "成功"
|
// @Router /api-wms/v1/company/company [get]
|
func (slf CompanyController) List(c *gin.Context) {
|
var params request.GetCompanyList
|
if err := c.ShouldBindQuery(¶ms); err != nil {
|
util.ResponseFormat(c, code.RequestParamError, err.Error())
|
return
|
}
|
list, total, err := models.NewCompanySearch().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/company/company/{id} [delete]
|
func (slf CompanyController) Delete(c *gin.Context) {
|
id := cast.ToUint(c.Param("id"))
|
if id == 0 {
|
util.ResponseFormat(c, code.RequestParamError, "空的记录id")
|
return
|
}
|
|
err := models.NewCompanySearch().SetID(id).Delete()
|
if err != nil {
|
util.ResponseFormat(c, code.RequestParamError, "删除失败")
|
return
|
}
|
util.ResponseFormat(c, code.UpdateSuccess, "删除成功")
|
}
|