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.4 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. // @title CreateSysDictionary
  10. // @description create a SysDictionary
  11. // @param sysDictionary model.SysDictionary
  12. // @auth (2020/04/05 20:22)
  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. // @title DeleteSysDictionary
  22. // @description delete a SysDictionary
  23. // @auth (2020/04/05 20:22)
  24. // @param sysDictionary model.SysDictionary
  25. // @return 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. // @title UpdateSysDictionary
  31. // @description update a SysDictionary
  32. // @param sysDictionary *model.SysDictionary
  33. // @auth (2020/04/05 20:22)
  34. // @return 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. // @title GetSysDictionary
  55. // @description get the info of a SysDictionary
  56. // @auth (2020/04/05 20:22)
  57. // @param id uint
  58. // @return error
  59. // @return SysDictionary SysDictionary
  60. func GetSysDictionary(Type string, Id uint) (err error, sysDictionary model.SysDictionary) {
  61. err = global.GVA_DB.Where("type = ? OR id = ?", Type, Id).Preload("SysDictionaryDetails").First(&sysDictionary).Error
  62. return
  63. }
  64. // @title GetSysDictionaryInfoList
  65. // @description get SysDictionary list by pagination, 分页获取用户列表
  66. // @auth (2020/04/05 20:22)
  67. // @param info PageInfo
  68. // @return error
  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. }