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.

103 lines
3.3 KiB

  1. package service
  2. import (
  3. "errors"
  4. "gin-vue-admin/global"
  5. "gin-vue-admin/model"
  6. "gin-vue-admin/model/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. func CreateSysDictionary(sysDictionary model.SysDictionary) (err error) {
  15. if (!errors.Is(global.GVA_DB.First(&model.SysDictionary{}, "type = ?", sysDictionary.Type).Error, gorm.ErrRecordNotFound)) {
  16. return errors.New("存在相同的type,不允许创建")
  17. }
  18. err = global.GVA_DB.Create(&sysDictionary).Error
  19. return err
  20. }
  21. //@author: [piexlmax](https://github.com/piexlmax)
  22. //@function: DeleteSysDictionary
  23. //@description: 删除字典数据
  24. //@param: sysDictionary model.SysDictionary
  25. //@return: err error
  26. func DeleteSysDictionary(sysDictionary model.SysDictionary) (err error) {
  27. err = global.GVA_DB.Delete(sysDictionary).Delete(&sysDictionary.SysDictionaryDetails).Error
  28. return err
  29. }
  30. //@author: [piexlmax](https://github.com/piexlmax)
  31. //@function: UpdateSysDictionary
  32. //@description: 更新字典数据
  33. //@param: sysDictionary *model.SysDictionary
  34. //@return: err error
  35. func UpdateSysDictionary(sysDictionary *model.SysDictionary) (err error) {
  36. var dict model.SysDictionary
  37. sysDictionaryMap := map[string]interface{}{
  38. "Name": sysDictionary.Name,
  39. "Type": sysDictionary.Type,
  40. "Status": sysDictionary.Status,
  41. "Desc": sysDictionary.Desc,
  42. }
  43. db := global.GVA_DB.Where("id = ?", sysDictionary.ID).First(&dict)
  44. if dict.Type == sysDictionary.Type {
  45. err = db.Updates(sysDictionaryMap).Error
  46. } else {
  47. if (!errors.Is(global.GVA_DB.First(&model.SysDictionary{}, "type = ?", sysDictionary.Type).Error, gorm.ErrRecordNotFound)) {
  48. return errors.New("存在相同的type,不允许创建")
  49. }
  50. err = db.Updates(sysDictionaryMap).Error
  51. }
  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 GetSysDictionary(Type string, Id uint) (err error, sysDictionary model.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 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(&model.SysDictionary{})
  74. var sysDictionarys []model.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. }