You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

106 lines
3.6 KiB

3 years ago
3 years ago
3 years ago
3 years ago
3 years ago
  1. package system
  2. import (
  3. "errors"
  4. "gin-vue-admin/global"
  5. "gin-vue-admin/model/system"
  6. "gin-vue-admin/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. //@author: [piexlmax](https://github.com/piexlmax)
  66. //@author: [SliverHorn](https://github.com/SliverHorn)
  67. //@function: GetSysDictionaryInfoList
  68. //@description: 分页获取字典列表
  69. //@param: info request.SysDictionarySearch
  70. //@return: err error, list interface{}, total int64
  71. func (dictionaryService *DictionaryService) GetSysDictionaryInfoList(info request.SysDictionarySearch) (err error, list interface{}, total int64) {
  72. limit := info.PageSize
  73. offset := info.PageSize * (info.Page - 1)
  74. // 创建db
  75. db := global.GVA_DB.Model(&system.SysDictionary{})
  76. var sysDictionarys []system.SysDictionary
  77. // 如果有条件搜索 下方会自动创建搜索语句
  78. if info.Name != "" {
  79. db = db.Where("`name` LIKE ?", "%"+info.Name+"%")
  80. }
  81. if info.Type != "" {
  82. db = db.Where("`type` LIKE ?", "%"+info.Type+"%")
  83. }
  84. if info.Status != nil {
  85. db = db.Where("`status` = ?", info.Status)
  86. }
  87. if info.Desc != "" {
  88. db = db.Where("`desc` LIKE ?", "%"+info.Desc+"%")
  89. }
  90. err = db.Count(&total).Error
  91. err = db.Limit(limit).Offset(offset).Find(&sysDictionarys).Error
  92. return err, sysDictionarys, total
  93. }