sys_dictionary.go 4.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117
  1. package system
  2. import (
  3. "errors"
  4. "github.com/flipped-aurora/gin-vue-admin/server/global"
  5. "github.com/flipped-aurora/gin-vue-admin/server/model/system"
  6. "github.com/flipped-aurora/gin-vue-admin/server/model/system/request"
  7. "gorm.io/gorm"
  8. )
  9. //@author: [piexlmax](https://github.com/piexlmax)
  10. //@function: DeleteSysDictionary
  11. //@description: 创建字典数据
  12. //@param: sysDictionary model.SysDictionary
  13. //@return: err error
  14. type DictionaryService struct {
  15. }
  16. func (dictionaryService *DictionaryService) CreateSysDictionary(sysDictionary system.SysDictionary) (err error) {
  17. if (!errors.Is(global.GVA_DB.First(&system.SysDictionary{}, "type = ?", sysDictionary.Type).Error, gorm.ErrRecordNotFound)) {
  18. return errors.New("存在相同的type,不允许创建")
  19. }
  20. err = global.GVA_DB.Create(&sysDictionary).Error
  21. return err
  22. }
  23. //@author: [piexlmax](https://github.com/piexlmax)
  24. //@function: DeleteSysDictionary
  25. //@description: 删除字典数据
  26. //@param: sysDictionary model.SysDictionary
  27. //@return: err error
  28. func (dictionaryService *DictionaryService) DeleteSysDictionary(sysDictionary system.SysDictionary) (err error) {
  29. err = global.GVA_DB.Delete(&sysDictionary).Delete(&sysDictionary.SysDictionaryDetails).Error
  30. return err
  31. }
  32. //@author: [piexlmax](https://github.com/piexlmax)
  33. //@function: UpdateSysDictionary
  34. //@description: 更新字典数据
  35. //@param: sysDictionary *model.SysDictionary
  36. //@return: err error
  37. func (dictionaryService *DictionaryService) UpdateSysDictionary(sysDictionary *system.SysDictionary) (err error) {
  38. var dict system.SysDictionary
  39. sysDictionaryMap := map[string]interface{}{
  40. "Name": sysDictionary.Name,
  41. "Type": sysDictionary.Type,
  42. "Status": sysDictionary.Status,
  43. "Desc": sysDictionary.Desc,
  44. }
  45. db := global.GVA_DB.Where("id = ?", sysDictionary.ID).First(&dict)
  46. if dict.Type == sysDictionary.Type {
  47. err = db.Updates(sysDictionaryMap).Error
  48. } else {
  49. if (!errors.Is(global.GVA_DB.First(&system.SysDictionary{}, "type = ?", sysDictionary.Type).Error, gorm.ErrRecordNotFound)) {
  50. return errors.New("存在相同的type,不允许创建")
  51. }
  52. err = db.Updates(sysDictionaryMap).Error
  53. }
  54. return err
  55. }
  56. //@author: [piexlmax](https://github.com/piexlmax)
  57. //@function: GetSysDictionary
  58. //@description: 根据id或者type获取字典单条数据
  59. //@param: Type string, Id uint
  60. //@return: err error, sysDictionary model.SysDictionary
  61. func (dictionaryService *DictionaryService) GetSysDictionary(Type string, Id uint) (err error, sysDictionary system.SysDictionary) {
  62. err = global.GVA_DB.Where("type = ? OR id = ?", Type, Id).Preload("SysDictionaryDetails").First(&sysDictionary).Error
  63. return
  64. }
  65. func (dictionaryService *DictionaryService) GetSysDictionaryDetail(Type string) (err error, sysDictionary system.SysDictionary) {
  66. err = global.GVA_DB.Where("name = ? ", Type).Preload("SysDictionaryDetails").First(&sysDictionary).Error
  67. return
  68. }
  69. func (dictionaryService *DictionaryService) GetSysDictionaryDetailList() (err error, sysDictionary []system.SysDictionary) {
  70. err = global.GVA_DB.Where("`type` LIKE ?", "site_%").Preload("SysDictionaryDetails").Find(&sysDictionary).Error
  71. return
  72. }
  73. //@author: [piexlmax](https://github.com/piexlmax)
  74. //@author: [SliverHorn](https://github.com/SliverHorn)
  75. //@function: GetSysDictionaryInfoList
  76. //@description: 分页获取字典列表
  77. //@param: info request.SysDictionarySearch
  78. //@return: err error, list interface{}, total int64
  79. func (dictionaryService *DictionaryService) GetSysDictionaryInfoList(info request.SysDictionarySearch) (err error, list interface{}, total int64) {
  80. limit := info.PageSize
  81. offset := info.PageSize * (info.Page - 1)
  82. // 创建db
  83. db := global.GVA_DB.Model(&system.SysDictionary{})
  84. var sysDictionarys []system.SysDictionary
  85. // 如果有条件搜索 下方会自动创建搜索语句
  86. if info.Name != "" {
  87. db = db.Where("`name` LIKE ?", "%"+info.Name+"%")
  88. }
  89. if info.Type != "" {
  90. db = db.Where("`type` LIKE ?", "%"+info.Type+"%")
  91. }
  92. if info.Status != nil {
  93. db = db.Where("`status` = ?", info.Status)
  94. }
  95. if info.Desc != "" {
  96. db = db.Where("`desc` LIKE ?", "%"+info.Desc+"%")
  97. }
  98. err = db.Count(&total).Error
  99. err = db.Limit(limit).Offset(offset).Find(&sysDictionarys).Error
  100. return err, sysDictionarys, total
  101. }