dictionary_detail.go 3.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114
  1. package system
  2. import (
  3. "context"
  4. "fmt"
  5. "github.com/pkg/errors"
  6. "gorm.io/gorm"
  7. sysModel "log-server/model/system"
  8. "log-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, Sort: 1},
  50. {Label: "mediumint", Value: 2, Status: &True, Sort: 2},
  51. {Label: "int", Value: 3, Status: &True, Sort: 3},
  52. {Label: "bigint", Value: 4, Status: &True, Sort: 4},
  53. }
  54. dicts[2].SysDictionaryDetails = []sysModel.SysDictionaryDetail{
  55. {Label: "date", Status: &True},
  56. {Label: "time", Value: 1, Status: &True, Sort: 1},
  57. {Label: "year", Value: 2, Status: &True, Sort: 2},
  58. {Label: "datetime", Value: 3, Status: &True, Sort: 3},
  59. {Label: "timestamp", Value: 5, Status: &True, Sort: 5},
  60. }
  61. dicts[3].SysDictionaryDetails = []sysModel.SysDictionaryDetail{
  62. {Label: "float", Status: &True},
  63. {Label: "double", Value: 1, Status: &True, Sort: 1},
  64. {Label: "decimal", Value: 2, Status: &True, Sort: 2},
  65. }
  66. dicts[4].SysDictionaryDetails = []sysModel.SysDictionaryDetail{
  67. {Label: "char", Status: &True},
  68. {Label: "varchar", Value: 1, Status: &True, Sort: 1},
  69. {Label: "tinyblob", Value: 2, Status: &True, Sort: 2},
  70. {Label: "tinytext", Value: 3, Status: &True, Sort: 3},
  71. {Label: "text", Value: 4, Status: &True, Sort: 4},
  72. {Label: "blob", Value: 5, Status: &True, Sort: 5},
  73. {Label: "mediumblob", Value: 6, Status: &True, Sort: 6},
  74. {Label: "mediumtext", Value: 7, Status: &True, Sort: 7},
  75. {Label: "longblob", Value: 8, Status: &True, Sort: 8},
  76. {Label: "longtext", Value: 9, Status: &True, Sort: 9},
  77. }
  78. dicts[5].SysDictionaryDetails = []sysModel.SysDictionaryDetail{
  79. {Label: "tinyint", Status: &True},
  80. }
  81. for _, dict := range dicts {
  82. if err := db.Model(&dict).Association("SysDictionaryDetails").
  83. Replace(dict.SysDictionaryDetails); err != nil {
  84. return ctx, errors.Wrap(err, sysModel.SysDictionaryDetail{}.TableName()+"表数据初始化失败!")
  85. }
  86. }
  87. return ctx, nil
  88. }
  89. func (i *initDictDetail) DataInserted(ctx context.Context) bool {
  90. db, ok := ctx.Value("db").(*gorm.DB)
  91. if !ok {
  92. return false
  93. }
  94. var dict sysModel.SysDictionary
  95. if err := db.Preload("SysDictionaryDetails").
  96. First(&dict, &sysModel.SysDictionary{Name: "数据库bool类型"}).Error; err != nil {
  97. return false
  98. }
  99. return len(dict.SysDictionaryDetails) > 0 && dict.SysDictionaryDetails[0].Label == "tinyint"
  100. }