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.

102 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. )
  8. // @title CreateSysDictionary
  9. // @description create a SysDictionary
  10. // @param sysDictionary model.SysDictionary
  11. // @auth (2020/04/05 20:22)
  12. // @return err error
  13. func CreateSysDictionary(sysDictionary model.SysDictionary) (err error) {
  14. if (!global.GVA_DB.First(&model.SysDictionary{}, "type = ?", sysDictionary.Type).RecordNotFound()) {
  15. return errors.New("存在相同的type,不允许创建")
  16. }
  17. err = global.GVA_DB.Create(&sysDictionary).Error
  18. return err
  19. }
  20. // @title DeleteSysDictionary
  21. // @description delete a SysDictionary
  22. // @auth (2020/04/05 20:22)
  23. // @param sysDictionary model.SysDictionary
  24. // @return error
  25. func DeleteSysDictionary(sysDictionary model.SysDictionary) (err error) {
  26. err = global.GVA_DB.Delete(sysDictionary).Related(&sysDictionary.SysDictionaryDetails).Delete(&sysDictionary.SysDictionaryDetails).Error
  27. return err
  28. }
  29. // @title UpdateSysDictionary
  30. // @description update a SysDictionary
  31. // @param sysDictionary *model.SysDictionary
  32. // @auth (2020/04/05 20:22)
  33. // @return error
  34. func UpdateSysDictionary(sysDictionary *model.SysDictionary) (err error) {
  35. var dict model.SysDictionary
  36. sysDictionaryMap := map[string]interface{}{
  37. "Name": sysDictionary.Name,
  38. "Type": sysDictionary.Type,
  39. "Status": sysDictionary.Status,
  40. "Desc": sysDictionary.Desc,
  41. }
  42. db := global.GVA_DB.Where("id = ?", sysDictionary.ID).First(&dict)
  43. if dict.Type == sysDictionary.Type {
  44. err = db.Updates(sysDictionaryMap).Error
  45. } else {
  46. if (!global.GVA_DB.First(&model.SysDictionary{}, "type = ?", sysDictionary.Type).RecordNotFound()) {
  47. return errors.New("存在相同的type,不允许创建")
  48. } else {
  49. err = db.Updates(sysDictionaryMap).Error
  50. }
  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 int) {
  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. }