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. } else {
  50. err = db.Updates(sysDictionaryMap).Error
  51. }
  52. }
  53. return err
  54. }
  55. // @title GetSysDictionary
  56. // @description get the info of a SysDictionary
  57. // @auth (2020/04/05 20:22)
  58. // @param id uint
  59. // @return error
  60. // @return SysDictionary SysDictionary
  61. func GetSysDictionary(Type string, Id uint) (err error, sysDictionary model.SysDictionary) {
  62. err = global.GVA_DB.Where("type = ? OR id = ?", Type, Id).Preload("SysDictionaryDetails").First(&sysDictionary).Error
  63. return
  64. }
  65. // @title GetSysDictionaryInfoList
  66. // @description get SysDictionary list by pagination, 分页获取用户列表
  67. // @auth (2020/04/05 20:22)
  68. // @param info PageInfo
  69. // @return error
  70. func GetSysDictionaryInfoList(info request.SysDictionarySearch) (err error, list interface{}, total int64) {
  71. limit := info.PageSize
  72. offset := info.PageSize * (info.Page - 1)
  73. // 创建db
  74. db := global.GVA_DB.Model(&model.SysDictionary{})
  75. var sysDictionarys []model.SysDictionary
  76. // 如果有条件搜索 下方会自动创建搜索语句
  77. if info.Name != "" {
  78. db = db.Where("`name` LIKE ?", "%"+info.Name+"%")
  79. }
  80. if info.Type != "" {
  81. db = db.Where("`type` LIKE ?", "%"+info.Type+"%")
  82. }
  83. if info.Status != nil {
  84. db = db.Where("`status` = ?", info.Status)
  85. }
  86. if info.Desc != "" {
  87. db = db.Where("`desc` LIKE ?", "%"+info.Desc+"%")
  88. }
  89. err = db.Count(&total).Error
  90. err = db.Limit(limit).Offset(offset).Find(&sysDictionarys).Error
  91. return err, sysDictionarys, total
  92. }