176 lines
4.6 KiB
Go
176 lines
4.6 KiB
Go
package system
|
||
|
||
import (
|
||
"context"
|
||
"gateway/internal/otelTrace"
|
||
|
||
"time"
|
||
|
||
"github.com/beego/beego/v2/client/orm"
|
||
"go.uber.org/zap"
|
||
)
|
||
|
||
type MenuInfo struct {
|
||
Id int
|
||
MenuOrder int
|
||
MenuUid string
|
||
FirstMenu string
|
||
SecondMenu string
|
||
Creater string
|
||
Status string
|
||
CreateTime time.Time
|
||
UpdateTime time.Time
|
||
}
|
||
|
||
// 实现排序的三个接口函数
|
||
type MenuInfoSlice []MenuInfo
|
||
|
||
func (m MenuInfoSlice) Len() int {
|
||
return len(m)
|
||
}
|
||
|
||
func (m MenuInfoSlice) Swap(i, j int) {
|
||
m[i], m[j] = m[j], m[i]
|
||
}
|
||
|
||
func (m MenuInfoSlice) Less(i, j int) bool {
|
||
return m[i].MenuOrder < m[j].MenuOrder // 从小到大排序
|
||
}
|
||
|
||
const MENUINFO = "menu_info"
|
||
|
||
func InsertMenu(ctx context.Context, menuInfo MenuInfo) bool {
|
||
o := orm.NewOrm()
|
||
_, err := o.Insert(&menuInfo)
|
||
if err != nil {
|
||
otelTrace.Logger.WithContext(ctx).Error("insert new menu info fail:", zap.Error(err))
|
||
return false
|
||
}
|
||
return true
|
||
}
|
||
|
||
func FirstMenuIsExists(firstMenu string) bool {
|
||
o := orm.NewOrm()
|
||
exist := o.QueryTable(MENUINFO).Filter("first_menu", firstMenu).Exist()
|
||
return exist
|
||
}
|
||
|
||
func FirstMenuUidIsExists(firstMenUid string) bool {
|
||
o := orm.NewOrm()
|
||
exist := o.QueryTable(MENUINFO).Filter("menu_uid", firstMenUid).Exist()
|
||
return exist
|
||
}
|
||
|
||
func MenuOrderIsExists(menuOrder int) bool {
|
||
o := orm.NewOrm()
|
||
exist := o.QueryTable(MENUINFO).Filter("menu_order", menuOrder).Exist()
|
||
return exist
|
||
}
|
||
|
||
func GetMenuLen(ctx context.Context) int {
|
||
o := orm.NewOrm()
|
||
cnt, err := o.QueryTable(MENUINFO).Count()
|
||
if err != nil {
|
||
otelTrace.Logger.WithContext(ctx).Error("get menu info len length fail: ", zap.Error(err))
|
||
}
|
||
return int(cnt)
|
||
}
|
||
|
||
func GetMenuInfoByMenuUid(ctx context.Context, menuUid string) MenuInfo {
|
||
o := orm.NewOrm()
|
||
var menuInfo MenuInfo
|
||
_, err := o.QueryTable(MENUINFO).Filter("menu_uid", menuUid).Limit(1).All(&menuInfo)
|
||
if err != nil {
|
||
otelTrace.Logger.WithContext(ctx).Error("get menu info by menuUid fail: ", zap.Error(err))
|
||
}
|
||
return menuInfo
|
||
}
|
||
|
||
func GetMenuInfosByMenuUids(ctx context.Context, menuUids []string) []MenuInfo {
|
||
menuInfoList := make([]MenuInfo, 0)
|
||
for _, v := range menuUids {
|
||
m := GetMenuInfoByMenuUid(ctx, v)
|
||
menuInfoList = append(menuInfoList, m)
|
||
}
|
||
return menuInfoList
|
||
}
|
||
|
||
func GetMenuInfoByMenuOrder(ctx context.Context, menuOrder int) MenuInfo {
|
||
o := orm.NewOrm()
|
||
var menuInfo MenuInfo
|
||
_, err := o.QueryTable(MENUINFO).Filter("menu_order", menuOrder).Limit(1).All(&menuInfo)
|
||
if err != nil {
|
||
otelTrace.Logger.WithContext(ctx).Error("get menu info by menu order fail: ", zap.Error(err))
|
||
}
|
||
return menuInfo
|
||
}
|
||
|
||
func GetMenuAll(ctx context.Context) []MenuInfo {
|
||
o := orm.NewOrm()
|
||
var menuInfoList []MenuInfo
|
||
_, err := o.QueryTable(MENUINFO).OrderBy("-update_time").All(&menuInfoList)
|
||
if err != nil {
|
||
otelTrace.Logger.WithContext(ctx).Error("get all menu list fail:", zap.Error(err))
|
||
}
|
||
return menuInfoList
|
||
}
|
||
|
||
func GetMenuOffset(ctx context.Context, displayCount, offset int) []MenuInfo {
|
||
o := orm.NewOrm()
|
||
var menuInfoList []MenuInfo
|
||
_, err := o.QueryTable(MENUINFO).Limit(displayCount, offset).All(&menuInfoList)
|
||
if err != nil {
|
||
otelTrace.Logger.WithContext(ctx).Error("get menu offset fail: ", zap.Error(err))
|
||
}
|
||
return menuInfoList
|
||
}
|
||
|
||
func GetMenuOffsetByMap(ctx context.Context, params map[string]string, displayCount, offset int) []MenuInfo {
|
||
o := orm.NewOrm()
|
||
var menuInfoList []MenuInfo
|
||
qs := o.QueryTable(MENUINFO)
|
||
for k, v := range params {
|
||
if len(v) > 0 {
|
||
qs = qs.Filter(k, v)
|
||
}
|
||
}
|
||
_, err := qs.Limit(displayCount, offset).OrderBy("-update_time").All(&menuInfoList)
|
||
if err != nil {
|
||
otelTrace.Logger.WithContext(ctx).Error("get menu offset by map fail: ", zap.Error(err))
|
||
}
|
||
return menuInfoList
|
||
}
|
||
|
||
func GetMenuLenByMap(ctx context.Context, params map[string]string) int {
|
||
o := orm.NewOrm()
|
||
qs := o.QueryTable(MENUINFO)
|
||
for k, v := range params {
|
||
if len(v) > 0 {
|
||
qs = qs.Filter(k, v)
|
||
}
|
||
}
|
||
cnt, err := qs.Count()
|
||
if err != nil {
|
||
otelTrace.Logger.WithContext(ctx).Error("get menu len by map fail: ", zap.Error(err))
|
||
}
|
||
return int(cnt)
|
||
}
|
||
|
||
func UpdateMenuInfo(ctx context.Context, menuInfo MenuInfo) {
|
||
o := orm.NewOrm()
|
||
cnt, err := o.Update(&menuInfo)
|
||
if err != nil {
|
||
otelTrace.Logger.WithContext(ctx).Error("update menu info fail: ", zap.Error(err))
|
||
}
|
||
otelTrace.Logger.WithContext(ctx).Info("update menu info success, num: ", zap.Int64("cnt", cnt))
|
||
}
|
||
|
||
func DeleteMenuInfo(ctx context.Context, menuUid string) {
|
||
o := orm.NewOrm()
|
||
cnt, err := o.QueryTable(MENUINFO).Filter("menu_uid", menuUid).Delete()
|
||
if err != nil {
|
||
otelTrace.Logger.WithContext(ctx).Error("delete menu info fail: ", zap.Error(err))
|
||
}
|
||
otelTrace.Logger.WithContext(ctx).Info("delete menu info num: ", zap.Int64("cnt", cnt))
|
||
}
|