package utils

import (
	"github.com/sirupsen/logrus"
	"main/database"
	"main/model"
)

var MenuList []model.Menus

func UpdateMenuList() {
	db := database.GetInstance().GetMysqlDb()
	if err := db.Order("menu_id").Find(&MenuList).Error; err != nil {
		logrus.Errorln("sql执行失败：", err)
	}
	logrus.Infoln("更新暂存MenuList列表")
	if len(MenuList) == 0 {
		logrus.Warningln("数据库MenuList为空，添加初始数据")
		if err := db.Create(&model.Menus{
			MenuId:    "999",
			Name:      "系统管理",
			Icon:      "bi-gear",
			Path:      "",
			UserType:  "admin",
			WhiteList: "",
		}).Error; err != nil {
			logrus.Errorln("sql执行失败：", err)
		}
		if err := db.Create(&model.Menus{
			MenuId:    "99901",
			Name:      "菜单管理",
			Icon:      "bi-list",
			Path:      "/admin/menus",
			UserType:  "admin",
			WhiteList: "",
		}).Error; err != nil {
			logrus.Errorln("sql执行失败：", err)
		}
		if err := db.Order("menu_id").Find(&MenuList).Error; err != nil {
			logrus.Errorln("sql执行失败：", err)
		}
	}
}
