package controllers
|
|
import (
|
"github.com/gin-gonic/gin"
|
"gorm.io/gorm"
|
"strconv"
|
"wms/extend/code"
|
"wms/extend/util"
|
"wms/models"
|
"wms/pkg/logx"
|
"wms/request"
|
)
|
|
type DictController struct{}
|
|
// AddMiniDict
|
//
|
// @Tags 数据字典
|
// @Summary 添加字典信息
|
// @Produce application/json
|
// @Param object body request.AddMiniDict true "参数"
|
// @Success 200 {object} util.Response "成功"
|
// @Router /api-wms/v1/dict/add [post]
|
func (slf DictController) AddMiniDict(c *gin.Context) {
|
var params request.AddMiniDict
|
if err := c.BindJSON(¶ms); err != nil {
|
util.ResponseFormat(c, code.RequestParamError, "参数解析失败,数据类型错误")
|
return
|
}
|
|
if !params.Type.Valid() {
|
util.ResponseFormat(c, code.RequestParamError, "字典类型错误")
|
return
|
}
|
|
if len(params.Name) == 0 {
|
util.ResponseFormat(c, code.RequestParamError, "名称为空")
|
return
|
}
|
|
record := models.MiniDict{
|
Type: params.Type,
|
Name: params.Name,
|
Value: params.Value,
|
IsDefault: params.IsDefault,
|
}
|
|
if err := models.NewMiniDictSearch().Create(&record); err != nil {
|
logx.Errorf("MiniDict add err: %v", err)
|
util.ResponseFormat(c, code.RequestParamError, "添加失败")
|
return
|
}
|
util.ResponseFormat(c, code.Success, "添加成功")
|
}
|
|
// EditMiniDict
|
//
|
// @Tags 数据字典
|
// @Summary 编辑字典信息
|
// @Produce application/json
|
// @Param object body request.EditMiniDict true "参数"
|
// @Success 200 {object} util.Response "成功"
|
// @Router /api-wms/v1/dict/edit [post]
|
func (slf DictController) EditMiniDict(c *gin.Context) {
|
var params request.EditMiniDict
|
if err := c.BindJSON(¶ms); err != nil {
|
util.ResponseFormat(c, code.RequestParamError, "参数解析失败,数据类型错误")
|
return
|
}
|
|
if params.ID == 0 {
|
util.ResponseFormat(c, code.RequestParamError, "无效ID")
|
return
|
}
|
|
if !params.Type.Valid() {
|
util.ResponseFormat(c, code.RequestParamError, "字典类型错误")
|
return
|
}
|
|
if len(params.Name) == 0 {
|
util.ResponseFormat(c, code.RequestParamError, "名称为空")
|
return
|
}
|
|
record := models.MiniDict{
|
Type: params.Type,
|
Name: params.Name,
|
Value: params.Value,
|
IsDefault: params.IsDefault,
|
}
|
record.ID = uint(params.ID)
|
|
if err := models.NewMiniDictSearch().SetID(uint(params.ID)).Save(&record); err != nil {
|
logx.Errorf("MiniDict edit err: %v", err)
|
util.ResponseFormat(c, code.RequestParamError, "编辑失败")
|
return
|
}
|
util.ResponseFormat(c, code.Success, "编辑成功")
|
}
|
|
// DeleteMiniDict
|
//
|
// @Tags 数据字典
|
// @Summary 删除字典信息
|
// @Param id path string true "id"
|
// @Success 200 {object} util.Response "成功"
|
// @Router /api-wms/v1/dict/delete/{id} [delete]
|
func (slf DictController) DeleteMiniDict(c *gin.Context) {
|
id, _ := strconv.ParseUint(c.Param("id"), 10, 64)
|
if id == 0 {
|
util.ResponseFormat(c, code.RequestParamError, "无效ID")
|
return
|
}
|
err := models.NewMiniDictSearch().SetID(uint(id)).Delete()
|
if err != nil {
|
logx.Errorf("MiniDict delete err: %v", err)
|
util.ResponseFormat(c, code.RequestParamError, "删除失败")
|
return
|
}
|
util.ResponseFormat(c, code.Success, "删除成功")
|
}
|
|
// SaveMiniDict
|
//
|
// @Tags 数据字典
|
// @Summary 批量更新迷你字典(会删除原数据)
|
// @Produce application/json
|
// @Param object body request.SaveMiniDict true "参数"
|
// @Success 200 {object} util.Response "成功"
|
// @Router /api-wms/v1/dict/save [post]
|
func (slf DictController) SaveMiniDict(c *gin.Context) {
|
var params request.SaveMiniDict
|
if err := c.BindJSON(¶ms); err != nil {
|
util.ResponseFormat(c, code.RequestParamError, "参数解析失败,数据类型错误")
|
return
|
}
|
|
if !params.Type.Valid() {
|
util.ResponseFormat(c, code.RequestParamError, "字典类型错误")
|
return
|
}
|
|
var records []*models.MiniDict
|
for _, v := range params.List {
|
if len(v.Name) == 0 {
|
util.ResponseFormat(c, code.RequestParamError, "名称为空")
|
return
|
}
|
|
records = append(records, &models.MiniDict{
|
Type: params.Type,
|
Name: v.Name,
|
IsDefault: v.IsDefault,
|
Value: v.Value,
|
})
|
}
|
|
err := models.WithTransaction(func(tx *gorm.DB) error {
|
err := models.NewMiniDictSearch().SetOrm(tx).SetType(params.Type).Delete()
|
if err != nil {
|
return err
|
}
|
|
err = models.NewMiniDictSearch().SetOrm(tx).CreateBatch(records)
|
if err != nil {
|
return err
|
}
|
return nil
|
})
|
if err != nil {
|
util.ResponseFormat(c, code.RequestParamError, "保存失败")
|
return
|
}
|
|
util.ResponseFormat(c, code.Success, "添加成功")
|
}
|
|
// GetMiniDictList
|
//
|
// @Tags 数据字典
|
// @Summary 获取字典信息列表
|
// @Produce application/json
|
// @Param object body request.GetMiniDictList true "参数"
|
// @Success 200 {object} util.ResponseList{data=[]models.MiniDict} "成功"
|
// @Router /api-wms/v1/dict/getDictList [post]
|
func (slf DictController) GetMiniDictList(c *gin.Context) {
|
var params request.GetMiniDictList
|
if err := c.BindJSON(¶ms); err != nil {
|
util.ResponseFormat(c, code.RequestParamError, "参数解析失败,数据类型错误")
|
return
|
}
|
|
//if !params.Type.Valid() {
|
// util.ResponseFormat(c, code.RequestParamError, "字典类型错误")
|
// return
|
//}
|
dictSearch := models.NewMiniDictSearch()
|
if params.Type.Valid() {
|
dictSearch.SetType(params.Type)
|
}
|
|
list, total, err := dictSearch.Find()
|
if err != nil {
|
util.ResponseFormat(c, code.RequestParamError, "查找失败")
|
return
|
}
|
|
util.ResponseFormatList(c, code.Success, list, int(total))
|
}
|