excel_template.go 1.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475
  1. package system
  2. import (
  3. "context"
  4. "github.com/pkg/errors"
  5. "gorm.io/gorm"
  6. sysModel "server/model/system"
  7. "server/service/system"
  8. )
  9. type initExcelTemplate struct{}
  10. const initOrderExcelTemplate = initOrderDictDetail + 1
  11. // auto run
  12. func init() {
  13. system.RegisterInit(initOrderExcelTemplate, &initExcelTemplate{})
  14. }
  15. func (i initExcelTemplate) InitializerName() string {
  16. return "sys_export_templates"
  17. }
  18. func (i *initExcelTemplate) MigrateTable(ctx context.Context) (context.Context, error) {
  19. db, ok := ctx.Value("db").(*gorm.DB)
  20. if !ok {
  21. return ctx, system.ErrMissingDBContext
  22. }
  23. return ctx, db.AutoMigrate(&sysModel.SysExportTemplate{})
  24. }
  25. func (i *initExcelTemplate) TableCreated(ctx context.Context) bool {
  26. db, ok := ctx.Value("db").(*gorm.DB)
  27. if !ok {
  28. return false
  29. }
  30. return db.Migrator().HasTable(&sysModel.SysExportTemplate{})
  31. }
  32. func (i *initExcelTemplate) InitializeData(ctx context.Context) (context.Context, error) {
  33. db, ok := ctx.Value("db").(*gorm.DB)
  34. if !ok {
  35. return ctx, system.ErrMissingDBContext
  36. }
  37. entities := []sysModel.SysExportTemplate{
  38. {
  39. Name: "api",
  40. TableName: "sys_apis",
  41. TemplateID: "api",
  42. TemplateInfo: `{
  43. "path":"路径",
  44. "method":"方法(大写)",
  45. "description":"方法介绍",
  46. "api_group":"方法分组"
  47. }`,
  48. },
  49. }
  50. if err := db.Create(&entities).Error; err != nil {
  51. return ctx, errors.Wrap(err, "sys_export_templates"+"表数据初始化失败!")
  52. }
  53. next := context.WithValue(ctx, i.InitializerName(), entities)
  54. return next, nil
  55. }
  56. func (i *initExcelTemplate) DataInserted(ctx context.Context) bool {
  57. db, ok := ctx.Value("db").(*gorm.DB)
  58. if !ok {
  59. return false
  60. }
  61. if errors.Is(db.First(&sysModel.SysExportTemplate{}).Error, gorm.ErrRecordNotFound) {
  62. return false
  63. }
  64. return true
  65. }