package v1
|
|
import (
|
"github.com/gin-gonic/gin"
|
"gorm.io/gorm"
|
"srm/constvar"
|
"srm/model"
|
"srm/utils"
|
"srm/utils/code"
|
)
|
|
type DictController struct{}
|
|
type MiniDict struct {
|
Name string `gorm:"type:varchar(191);not null;comment:名称" json:"name"`
|
IsDefault bool `gorm:"type:tinyint(1);comment:是否默认" json:"isDefault"`
|
Value string `gorm:"type:varchar(191);;comment:值" json:"value"`
|
}
|
|
type SaveMiniDict struct {
|
Type constvar.MiniDictType `gorm:"type:int(11);comment:字典类型" json:"type"`
|
List []*MiniDict `json:"list"`
|
}
|
|
type GetMiniDictList struct {
|
Type constvar.MiniDictType `gorm:"type:int(11);comment:字典类型" json:"type"`
|
}
|
|
// SaveMiniDict
|
// @Tags 数据字典
|
// @Summary 更新迷你字典
|
// @Produce application/json
|
// @Param object body SaveMiniDict true "参数"
|
// @Param Authorization header string true "token"
|
// @Success 200 {object} utils.Response "成功"
|
// @Router /dict/saveMiniDict [post]
|
func (slf DictController) SaveMiniDict(c *gin.Context) {
|
var params SaveMiniDict
|
if err := c.BindJSON(¶ms); err != nil {
|
utils.ResponseFormat(c, code.RequestParamError, "参数解析失败,数据类型错误")
|
return
|
}
|
|
if !params.Type.Valid() {
|
utils.ResponseFormat(c, code.RequestParamError, "字典类型错误")
|
return
|
}
|
|
var records []*model.MiniDict
|
for _, v := range params.List {
|
if len(v.Name) == 0 {
|
utils.ResponseFormat(c, code.RequestParamError, "名称为空")
|
return
|
}
|
|
records = append(records, &model.MiniDict{
|
Type: params.Type,
|
Name: v.Name,
|
IsDefault: v.IsDefault,
|
Value: v.Value,
|
})
|
}
|
|
err := model.WithTransaction(func(tx *gorm.DB) error {
|
err := model.NewMiniDictSearch().SetOrm(tx).SetType(params.Type).Delete()
|
if err != nil {
|
return err
|
}
|
|
err = model.NewMiniDictSearch().SetOrm(tx).CreateBatch(records)
|
if err != nil {
|
return err
|
}
|
return nil
|
})
|
if err != nil {
|
utils.ResponseFormat(c, code.RequestParamError, "保存失败")
|
return
|
}
|
|
utils.ResponseFormat(c, code.Success, "添加成功")
|
}
|
|
// GetMiniDictList
|
// @Tags 数据字典
|
// @Summary 获取迷你字典列表
|
// @Produce application/json
|
// @Param object body GetMiniDictList true "参数"
|
// @Param Authorization header string true "token"
|
// @Success 200 {object} utils.ResponseList{data=[]model.MiniDict} "成功"
|
// @Router /dict/getMiniDictList [post]
|
func (slf DictController) GetMiniDictList(c *gin.Context) {
|
var params GetMiniDictList
|
if err := c.BindJSON(¶ms); err != nil {
|
utils.ResponseFormat(c, code.RequestParamError, "参数解析失败,数据类型错误")
|
return
|
}
|
|
if !params.Type.Valid() {
|
utils.ResponseFormat(c, code.RequestParamError, "字典类型错误")
|
return
|
}
|
|
list, total, err := model.NewMiniDictSearch().SetType(params.Type).Find()
|
if err != nil {
|
utils.ResponseFormat(c, code.RequestParamError, "查找失败")
|
return
|
}
|
|
utils.ResponseFormatList(c, code.Success, list, int(total))
|
}
|