123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219 |
- package system
- import (
- "errors"
- "fmt"
- "go-nc/configs/global"
- "go-nc/internal/utils"
- "go-nc/model"
- "github.com/gin-gonic/gin"
- "github.com/jinzhu/copier"
- )
- // 创建字典
- func CreateDictionary(c *gin.Context) {
- type Param struct {
- Label string `json:"label" validate:"required"` // key
- Value string `json:"value" validate:"required"` // value
- TypeKey string `json:"typeKey" validate:"required"` // 类型
- TypeLabel string `json:"typeLabel" validate:"required"`
- Remark string `json:"remark" validate:"required"` // 备注
- }
- var param Param
- if err := c.ShouldBindJSON(¶m); err != nil {
- c.Error(errors.New("缺少参数:" + err.Error()))
- return
- }
- err := utils.ValidateStruct(param)
- if err != nil {
- c.Error(err)
- return
- }
- // 验证字典是否存在
- // if err := verifyDictionary(param.TypeKey, param.Value); err != nil {
- // c.Error(err)
- // return
- // }
- // 获取token
- userInfoInterface, _ := c.Get("userInfo")
- userInfo, _ := userInfoInterface.(model.Sys_user)
- // 创建
- dictionary := model.Sys_dictionary{}
- dictionary.Label = param.Label
- dictionary.Value = param.Value
- dictionary.TypeKey = param.TypeKey
- dictionary.TypeLabel = param.TypeLabel
- dictionary.CreateUserId = userInfo.Id
- dictionary.Remark = param.Remark
- if err := global.App.DB.Create(&dictionary).Error; err != nil {
- c.Error(err)
- return
- }
- c.Set("res_data", "创建成功!")
- }
- // // 获取字典-下级:管理中使用
- // func GetDictionary(c *gin.Context) {
- // TypeKey := c.Query("typeKey")
- // if TypeKey == "" {
- // c.Error(errors.New("缺少参数:typeKey"))
- // return
- // }
- // var dictionary []model.Sys_dictionary
- // if err := global.App.DB.Where("type_key = ?", TypeKey).Find(&dictionary).Error; err != nil {
- // c.Error(err)
- // return
- // }
- // c.Set("res_data", dictionary)
- // }
- // 字典列表-上级: 管理中使用
- func DictionaryList(c *gin.Context) {
- type Param struct {
- TypeLabel string `json:"typeLabel"`
- TypeKey string `json:"typeKey"`
- }
- var param Param
- param.TypeLabel = ""
- param.TypeKey = ""
- if err := c.ShouldBindJSON(¶m); err != nil {
- c.Error(errors.New("缺少参数:" + err.Error()))
- return
- }
- var data []model.Sys_dictionary
- if err := global.App.DB.Model(&model.Sys_dictionary{}).Select("id", "type_label", "type_key", "label", "value").
- Where("type_label LIKE ?", "%"+param.TypeLabel+"%").
- Where("type_key LIKE ?", "%"+param.TypeKey+"%").
- Find(&data).Error; err != nil {
- c.Error(err)
- return
- }
- // data根据对type_key去重
- type resultType struct {
- Id uint `json:"id"`
- TypeLabel string `json:"typeLabel"`
- TypeKey string `json:"typeKey"`
- Children []model.Sys_dictionary `json:"children"`
- }
- // 过滤重复的type_key
- var result []resultType
- for i, v := range data {
- if i == 0 {
- result = append(result, resultType{Id: v.Id, TypeLabel: v.TypeLabel, TypeKey: v.TypeKey, Children: []model.Sys_dictionary{}})
- }
- for j := 0; j < len(result); j++ {
- if result[j].TypeKey == v.TypeKey {
- result[j].Children = append(result[j].Children, model.Sys_dictionary{Model: model.Model{Id: v.Id}, Label: v.Label, Value: v.Value})
- break
- }
- if j == len(result)-1 {
- result = append(result, resultType{Id: v.Id, TypeLabel: v.TypeLabel, TypeKey: v.TypeKey, Children: []model.Sys_dictionary{}})
- result[j+1].Children = append(result[j+1].Children, model.Sys_dictionary{Model: model.Model{Id: v.Id}, Label: v.Label, Value: v.Value})
- break
- }
- }
- }
- c.Set("res_data", result)
- }
- // 删除字典
- func DeleteDictionary(c *gin.Context) {
- id := c.Query("id")
- if id == "" {
- c.Error(errors.New("缺少参数:id"))
- return
- }
- if err := global.App.DB.Delete(&model.Sys_dictionary{}, id).Error; err != nil {
- c.Error(err)
- return
- }
- c.Set("res_data", "删除成功!")
- }
- // 更新字典
- func UpdateDictionary(c *gin.Context) {
- type Param struct {
- Id uint `json:"id" validate:"required"`
- Label string `json:"label" validate:"required"` // key
- Value string `json:"value" validate:"required"` // value
- TypeKey string `json:"typeKey" validate:"required"` // 类型
- TypeLabel string `json:"typeLabel" validate:"required"`
- Remark string `json:"remark" validate:"required"` // 备注
- }
- var param Param
- if err := c.ShouldBindJSON(¶m); err != nil {
- c.Error(errors.New("缺少参数:" + err.Error()))
- return
- }
- err := utils.ValidateStruct(param)
- if err != nil {
- c.Error(err)
- return
- }
- // 获取token
- userInfoInterface, _ := c.Get("userInfo")
- userInfo, _ := userInfoInterface.(model.Sys_user)
- // 创建
- dictionary := model.Sys_dictionary{}
- dictionary.Id = param.Id
- dictionary.Label = param.Label
- dictionary.Value = param.Value
- dictionary.TypeKey = param.TypeKey
- dictionary.TypeLabel = param.TypeLabel
- dictionary.UpdateUserId = userInfo.Id
- dictionary.Remark = param.Remark
- if err := global.App.DB.Updates(&dictionary).Error; err != nil {
- c.Error(err)
- return
- }
- c.Set("res_data", "修改成功!")
- }
- // // 验证字典是否存在
- // func verifyDictionary(label string, key string) error {
- // var dictionary model.Sys_dictionary
- // // 允许查询空出来的数据
- // if err := global.App.DB.
- // Where("label = ?", label).
- // Where("value = ?", key).
- // First(&dictionary).Error; err != nil {
- // if err == gorm.ErrRecordNotFound {
- // return nil
- // }
- // return err
- // }
- // if dictionary.Id != 0 {
- // return errors.New("该字典名称或者值已存在!")
- // }
- // return nil
- // }
- // 业务-获取
- func GetEnu(c *gin.Context) {
- TypeKey := c.Query("typeKey")
- if TypeKey == "" {
- c.Error(errors.New("缺少参数:typeKey"))
- return
- }
- var dictionary []model.Sys_dictionary
- if err := global.App.DB.Where("type_key = ?", TypeKey).Select("label", "value").Find(&dictionary).Error; err != nil {
- c.Error(err)
- return
- }
- fmt.Println("dictionary======", dictionary, TypeKey)
- type resultType struct {
- Label string `json:"label"`
- Value string `json:"value"`
- }
- var result []resultType
- copier.Copy(&result, &dictionary)
- c.Set("res_data", result)
- }
|