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.

79 lines
2.1 KiB

3 years ago
  1. package system
  2. import "github.com/gookit/color"
  3. type InitDBFunc interface {
  4. Init() (err error)
  5. }
  6. const (
  7. Mysql = "mysql"
  8. Pgsql = "pgsql"
  9. InitSuccess = "\n[%v] --> 初始数据成功!\n"
  10. AuthorityMenu = "\n[%v] --> %v 视图已存在!\n"
  11. InitDataExist = "\n[%v] --> %v 表的初始数据已存在!\n"
  12. InitDataFailed = "\n[%v] --> %v 表初始数据失败! \nerr: %+v\n"
  13. InitDataSuccess = "\n[%v] --> %v 表初始数据成功!\n"
  14. )
  15. type InitData interface {
  16. TableName() string
  17. Initialize() error
  18. CheckDataExist() bool
  19. }
  20. // MysqlDataInitialize Mysql 初始化接口使用封装
  21. // Author [SliverHorn](https://github.com/SliverHorn)
  22. func MysqlDataInitialize(inits ...InitData) error {
  23. var entity SysMenu
  24. for i := 0; i < len(inits); i++ {
  25. if inits[i].TableName() == entity.TableName() {
  26. if k := inits[i].CheckDataExist(); k {
  27. color.Info.Printf(AuthorityMenu, Mysql, inits[i].TableName())
  28. continue
  29. }
  30. } else {
  31. if inits[i].CheckDataExist() {
  32. color.Info.Printf(InitDataExist, Mysql, inits[i].TableName())
  33. continue
  34. }
  35. }
  36. if err := inits[i].Initialize(); err != nil {
  37. color.Info.Printf(InitDataFailed, Mysql, err)
  38. return err
  39. } else {
  40. color.Info.Printf(InitDataSuccess, Mysql, inits[i].TableName())
  41. }
  42. }
  43. color.Info.Printf(InitSuccess, Mysql)
  44. return nil
  45. }
  46. // PgsqlDataInitialize Pgsql 初始化接口使用封装
  47. // Author [SliverHorn](https://github.com/SliverHorn)
  48. func PgsqlDataInitialize(inits ...InitData) error {
  49. var entity SysMenu
  50. for i := 0; i < len(inits); i++ {
  51. if inits[i].TableName() == entity.TableName() {
  52. if k := inits[i].CheckDataExist(); k {
  53. color.Info.Printf(AuthorityMenu, Pgsql, inits[i].TableName())
  54. continue
  55. }
  56. } else {
  57. if inits[i].CheckDataExist() {
  58. color.Info.Printf(InitDataExist, Pgsql, inits[i].TableName())
  59. continue
  60. }
  61. }
  62. if err := inits[i].Initialize(); err != nil {
  63. color.Info.Printf(InitDataFailed, Pgsql, err)
  64. continue
  65. } else {
  66. color.Info.Printf(InitDataSuccess, Pgsql, inits[i].TableName())
  67. }
  68. }
  69. color.Info.Printf(InitSuccess, Pgsql)
  70. return nil
  71. }