dictionary_detail.go 4.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121
  1. package system
  2. import (
  3. "context"
  4. "fmt"
  5. "github.com/pkg/errors"
  6. "gorm.io/gorm"
  7. sysModel "server/model/system"
  8. "server/service/system"
  9. )
  10. const initOrderDictDetail = initOrderDict + 1
  11. type initDictDetail struct{}
  12. // auto run
  13. func init() {
  14. system.RegisterInit(initOrderDictDetail, &initDictDetail{})
  15. }
  16. func (i *initDictDetail) MigrateTable(ctx context.Context) (context.Context, error) {
  17. db, ok := ctx.Value("db").(*gorm.DB)
  18. if !ok {
  19. return ctx, system.ErrMissingDBContext
  20. }
  21. return ctx, db.AutoMigrate(&sysModel.SysDictionaryDetail{})
  22. }
  23. func (i *initDictDetail) TableCreated(ctx context.Context) bool {
  24. db, ok := ctx.Value("db").(*gorm.DB)
  25. if !ok {
  26. return false
  27. }
  28. return db.Migrator().HasTable(&sysModel.SysDictionaryDetail{})
  29. }
  30. func (i initDictDetail) InitializerName() string {
  31. return sysModel.SysDictionaryDetail{}.TableName()
  32. }
  33. func (i *initDictDetail) InitializeData(ctx context.Context) (context.Context, error) {
  34. db, ok := ctx.Value("db").(*gorm.DB)
  35. if !ok {
  36. return ctx, system.ErrMissingDBContext
  37. }
  38. dicts, ok := ctx.Value(initDict{}.InitializerName()).([]sysModel.SysDictionary)
  39. if !ok {
  40. return ctx, errors.Wrap(system.ErrMissingDependentContext,
  41. fmt.Sprintf("未找到 %s 表初始化数据", sysModel.SysDictionary{}.TableName()))
  42. }
  43. True := true
  44. dicts[0].SysDictionaryDetails = []sysModel.SysDictionaryDetail{
  45. {Label: "男", Value: "1", Status: &True, Sort: 1},
  46. {Label: "女", Value: "2", Status: &True, Sort: 2},
  47. }
  48. dicts[1].SysDictionaryDetails = []sysModel.SysDictionaryDetail{
  49. {Label: "smallint", Value: "1", Status: &True, Extend: "mysql", Sort: 1},
  50. {Label: "mediumint", Value: "2", Status: &True, Extend: "mysql", Sort: 2},
  51. {Label: "int", Value: "3", Status: &True, Extend: "mysql", Sort: 3},
  52. {Label: "bigint", Value: "4", Status: &True, Extend: "mysql", Sort: 4},
  53. {Label: "int2", Value: "5", Status: &True, Extend: "pgsql", Sort: 5},
  54. {Label: "int4", Value: "6", Status: &True, Extend: "pgsql", Sort: 6},
  55. {Label: "int6", Value: "7", Status: &True, Extend: "pgsql", Sort: 7},
  56. {Label: "int8", Value: "8", Status: &True, Extend: "pgsql", Sort: 8},
  57. }
  58. dicts[2].SysDictionaryDetails = []sysModel.SysDictionaryDetail{
  59. {Label: "date", Status: &True},
  60. {Label: "time", Value: "1", Status: &True, Extend: "mysql", Sort: 1},
  61. {Label: "year", Value: "2", Status: &True, Extend: "mysql", Sort: 2},
  62. {Label: "datetime", Value: "3", Status: &True, Extend: "mysql", Sort: 3},
  63. {Label: "timestamp", Value: "5", Status: &True, Extend: "mysql", Sort: 5},
  64. {Label: "timestamptz", Value: "6", Status: &True, Extend: "pgsql", Sort: 5},
  65. }
  66. dicts[3].SysDictionaryDetails = []sysModel.SysDictionaryDetail{
  67. {Label: "float", Status: &True},
  68. {Label: "double", Value: "1", Status: &True, Extend: "mysql", Sort: 1},
  69. {Label: "decimal", Value: "2", Status: &True, Extend: "mysql", Sort: 2},
  70. {Label: "numeric", Value: "3", Status: &True, Extend: "pgsql", Sort: 3},
  71. {Label: "smallserial", Value: "4", Status: &True, Extend: "pgsql", Sort: 4},
  72. }
  73. dicts[4].SysDictionaryDetails = []sysModel.SysDictionaryDetail{
  74. {Label: "char", Status: &True},
  75. {Label: "varchar", Value: "1", Status: &True, Extend: "mysql", Sort: 1},
  76. {Label: "tinyblob", Value: "2", Status: &True, Extend: "mysql", Sort: 2},
  77. {Label: "tinytext", Value: "3", Status: &True, Extend: "mysql", Sort: 3},
  78. {Label: "text", Value: "4", Status: &True, Extend: "mysql", Sort: 4},
  79. {Label: "blob", Value: "5", Status: &True, Extend: "mysql", Sort: 5},
  80. {Label: "mediumblob", Value: "6", Status: &True, Extend: "mysql", Sort: 6},
  81. {Label: "mediumtext", Value: "7", Status: &True, Extend: "mysql", Sort: 7},
  82. {Label: "longblob", Value: "8", Status: &True, Extend: "mysql", Sort: 8},
  83. {Label: "longtext", Value: "9", Status: &True, Extend: "mysql", Sort: 9},
  84. }
  85. dicts[5].SysDictionaryDetails = []sysModel.SysDictionaryDetail{
  86. {Label: "tinyint", Value: "1", Extend: "mysql", Status: &True},
  87. {Label: "bool", Value: "2", Extend: "pgsql", Status: &True},
  88. }
  89. for _, dict := range dicts {
  90. if err := db.Model(&dict).Association("SysDictionaryDetails").
  91. Replace(dict.SysDictionaryDetails); err != nil {
  92. return ctx, errors.Wrap(err, sysModel.SysDictionaryDetail{}.TableName()+"表数据初始化失败!")
  93. }
  94. }
  95. return ctx, nil
  96. }
  97. func (i *initDictDetail) DataInserted(ctx context.Context) bool {
  98. db, ok := ctx.Value("db").(*gorm.DB)
  99. if !ok {
  100. return false
  101. }
  102. var dict sysModel.SysDictionary
  103. if err := db.Preload("SysDictionaryDetails").
  104. First(&dict, &sysModel.SysDictionary{Name: "数据库bool类型"}).Error; err != nil {
  105. return false
  106. }
  107. return len(dict.SysDictionaryDetails) > 0 && dict.SysDictionaryDetails[0].Label == "tinyint"
  108. }