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.7 KiB

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