sys_dictionary.go 4.1 KB

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