view.go 1.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475
  1. package upgrade
  2. import (
  3. "fmt"
  4. "github.com/xinliangnote/go-gin-api/configs"
  5. "github.com/xinliangnote/go-gin-api/internal/pkg/core"
  6. )
  7. type upgradeViewResponse struct {
  8. LockFile string `json:"lock_file"`
  9. List []upgradeViewData `json:"list"`
  10. }
  11. type upgradeViewData struct {
  12. TableName string `json:"table_name"` // 表名称
  13. IsHave int32 `json:"is_have"` // 是否已存在 1=存在 -1=不存在
  14. }
  15. var tableList = []string{"authorized", "authorized_api", "admin", "menu", "menu_action", "admin_menu", "cron_task"}
  16. func (h *handler) UpgradeView() core.HandlerFunc {
  17. return func(c core.Context) {
  18. type tableInfo struct {
  19. Name string // name
  20. Comment string // comment
  21. }
  22. var tableCollect []tableInfo
  23. mysqlConf := configs.Get().MySQL.Read
  24. sqlTables := fmt.Sprintf("SELECT `table_name`,`table_comment` FROM `information_schema`.`tables` WHERE `table_schema`= '%s'", mysqlConf.Name)
  25. rows, err := h.db.GetDbR().Raw(sqlTables).Rows()
  26. if err != nil {
  27. c.HTML("upgrade_view", tableCollect)
  28. return
  29. }
  30. defer rows.Close()
  31. for rows.Next() {
  32. var info tableInfo
  33. err = rows.Scan(&info.Name, &info.Comment)
  34. if err != nil {
  35. fmt.Printf("execute query tables action error,had ignored, detail is [%v]\n", err.Error())
  36. continue
  37. }
  38. tableCollect = append(tableCollect, info)
  39. }
  40. tableData := make([]upgradeViewData, len(tableList))
  41. for k, v := range tableList {
  42. data := upgradeViewData{
  43. TableName: v,
  44. IsHave: -1,
  45. }
  46. tableData[k] = data
  47. }
  48. for k, v := range tableData {
  49. for _, haveV := range tableCollect {
  50. if haveV.Name == v.TableName {
  51. tableData[k].IsHave = 1
  52. }
  53. }
  54. }
  55. obj := new(upgradeViewResponse)
  56. obj.List = tableData
  57. obj.LockFile = configs.ProjectInstallMark
  58. c.HTML("upgrade_view", obj)
  59. }
  60. }