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.

104 lines
3.6 KiB

3 years ago
3 years ago
3 years ago
3 years ago
  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. if !errors.Is(global.GVA_DB.First(&system.SysDictionary{}, "type = ?", sysDictionary.Type).Error, gorm.ErrRecordNotFound) {
  48. return errors.New("存在相同的type,不允许创建")
  49. }
  50. }
  51. err = db.Updates(sysDictionaryMap).Error
  52. return err
  53. }
  54. //@author: [piexlmax](https://github.com/piexlmax)
  55. //@function: GetSysDictionary
  56. //@description: 根据id或者type获取字典单条数据
  57. //@param: Type string, Id uint
  58. //@return: err error, sysDictionary model.SysDictionary
  59. func (dictionaryService *DictionaryService) GetSysDictionary(Type string, Id uint) (err error, sysDictionary system.SysDictionary) {
  60. err = global.GVA_DB.Where("type = ? OR id = ?", Type, Id).Preload("SysDictionaryDetails").First(&sysDictionary).Error
  61. return
  62. }
  63. //@author: [piexlmax](https://github.com/piexlmax)
  64. //@author: [SliverHorn](https://github.com/SliverHorn)
  65. //@function: GetSysDictionaryInfoList
  66. //@description: 分页获取字典列表
  67. //@param: info request.SysDictionarySearch
  68. //@return: err error, list interface{}, total int64
  69. func (dictionaryService *DictionaryService) GetSysDictionaryInfoList(info request.SysDictionarySearch) (err error, list interface{}, total int64) {
  70. limit := info.PageSize
  71. offset := info.PageSize * (info.Page - 1)
  72. // 创建db
  73. db := global.GVA_DB.Model(&system.SysDictionary{})
  74. var sysDictionarys []system.SysDictionary
  75. // 如果有条件搜索 下方会自动创建搜索语句
  76. if info.Name != "" {
  77. db = db.Where("`name` LIKE ?", "%"+info.Name+"%")
  78. }
  79. if info.Type != "" {
  80. db = db.Where("`type` LIKE ?", "%"+info.Type+"%")
  81. }
  82. if info.Status != nil {
  83. db = db.Where("`status` = ?", info.Status)
  84. }
  85. if info.Desc != "" {
  86. db = db.Where("`desc` LIKE ?", "%"+info.Desc+"%")
  87. }
  88. err = db.Count(&total).Error
  89. err = db.Limit(limit).Offset(offset).Find(&sysDictionarys).Error
  90. return err, sysDictionarys, total
  91. }