package service import ( "basic.com/valib/bhomedbapi.git" "basic.com/valib/logger.git" "github.com/satori/go.uuid" "strings" "vamicro/system-service/models" ) //初始化应用权限 func InitAppPermissions() { var appApi bhomedbapi.AppApi apps := appApi.FindAll("") if apps != nil && len(apps) > 0 { //logger.Debug("app found:", len(apps)) appIds := "" for _, a := range apps { appIds += "'" + a.Id + "'," } if appIds != "" { appIds = strings.TrimRight(appIds, ",") } var count int e := models.GetDB().Raw("select count(1) as count from sys_menu where id not in (" + appIds + ") and parent_id not in (" + appIds + ")").Count(&count).Error if e == nil && count > 0 { err := models.GetDB().Exec("delete from sys_menu where id not in (" + appIds + ") and parent_id not in (" + appIds + ");delete from sys_role_menu where menu_id not in (select id from sys_menu);").Error if err != nil { logger.Debug("init permission err:", err) } } for _, a := range apps { if a.IsDefault { //logger.Debug("default app:", a.Package, " found") sort := 1 var sqls []string if a.Package == "cameraVideo" { sort = 1 } if a.Package == "search" { //统计查询 sort = 2 sqls = append(sqls, "INSERT INTO sys_menu(`id`,`parent_id`,`parent_ids`,`type`,`name`,`module`,`permission`,`sort`,`icon`,`url`,`enable`,`del_flag`) select '"+uuid.NewV4().String()+"', '"+a.Id+"', ',0,"+a.Id+",', 1, '摄像机', 'SEARCH', 'search:camera', 1, NULL, NULL, 1, 0 where not exists (select 1 from sys_menu where permission='search:camera');") sqls = append(sqls, "INSERT INTO sys_menu(`id`,`parent_id`,`parent_ids`,`type`,`name`,`module`,`permission`,`sort`,`icon`,`url`,`enable`,`del_flag`) select '"+uuid.NewV4().String()+"', '"+a.Id+"', ',0,"+a.Id+",', 1, '数据栈', 'SEARCH', 'search:stack', 2, NULL, NULL, 1, 0 where not exists (select 1 from sys_menu where permission='search:stack');") } else if a.Package == "cameraAccess" { //摄像机配置 sort = 3 sqls = append(sqls, "INSERT INTO sys_menu(`id`,`parent_id`,`parent_ids`,`type`,`name`,`module`,`permission`,`sort`,`icon`,`url`,`enable`,`del_flag`) select '"+uuid.NewV4().String()+"', '"+a.Id+"', ',0,"+a.Id+",', 1, '摄像机信息', 'CAMERAACCESS', 'cameraAccess:cameraInfo', 1, NULL, NULL, 1, 0 where not exists (select 1 from sys_menu where permission='cameraAccess:cameraInfo');") sqls = append(sqls, "INSERT INTO sys_menu(`id`,`parent_id`,`parent_ids`,`type`,`name`,`module`,`permission`,`sort`,`icon`,`url`,`enable`,`del_flag`) select '"+uuid.NewV4().String()+"', '"+a.Id+"', ',0,"+a.Id+",', 1, '独立场景', 'CAMERAACCESS', 'cameraAccess:selfRule', 2, NULL, NULL, 1, 0 where not exists (select 1 from sys_menu where permission='cameraAccess:selfRule');") sqls = append(sqls, "INSERT INTO sys_menu(`id`,`parent_id`,`parent_ids`,`type`,`name`,`module`,`permission`,`sort`,`icon`,`url`,`enable`,`del_flag`) select '"+uuid.NewV4().String()+"', '"+a.Id+"', ',0,"+a.Id+",', 1, '联动场景', 'CAMERAACCESS', 'cameraAccess:linkRule', 3, NULL, NULL, 1, 0 where not exists (select 1 from sys_menu where permission='cameraAccess:linkRule');") } else if a.Package == "analysisPower" { //算力管理 sort = 5 sqls = append(sqls, "INSERT INTO sys_menu(`id`,`parent_id`,`parent_ids`,`type`,`name`,`module`,`permission`,`sort`,`icon`,`url`,`enable`,`del_flag`) select '"+uuid.NewV4().String()+"', '"+a.Id+"', ',0,"+a.Id+",', 1, '设置', 'ANALYSISPOWER', 'analysisPower:set', 1, NULL, NULL, 1, 0 where not exists (select 1 from sys_menu where permission='analysisPower:set');") sqls = append(sqls, "INSERT INTO sys_menu(`id`,`parent_id`,`parent_ids`,`type`,`name`,`module`,`permission`,`sort`,`icon`,`url`,`enable`,`del_flag`) select '"+uuid.NewV4().String()+"', '"+a.Id+"', ',0,"+a.Id+",', 1, '查看', 'ANALYSISPOWER', 'analysisPower:show', 2, NULL, NULL, 1, 0 where not exists (select 1 from sys_menu where permission='analysisPower:show');") } else if a.Package == "library" { //比对库管理 sort = 6 sqls = append(sqls, "INSERT INTO sys_menu(`id`,`parent_id`,`parent_ids`,`type`,`name`,`module`,`permission`,`sort`,`icon`,`url`,`enable`,`del_flag`) select '"+uuid.NewV4().String()+"', '"+a.Id+"', ',0,"+a.Id+",', 1, '设置', 'LIBRARY', 'library:set', 1, NULL, NULL, 1, 0 where not exists (select 1 from sys_menu where permission='library:set');") sqls = append(sqls, "INSERT INTO sys_menu(`id`,`parent_id`,`parent_ids`,`type`,`name`,`module`,`permission`,`sort`,`icon`,`url`,`enable`,`del_flag`) select '"+uuid.NewV4().String()+"', '"+a.Id+"', ',0,"+a.Id+",', 1, '查看', 'LIBRARY', 'library:show', 2, NULL, NULL, 1, 0 where not exists (select 1 from sys_menu where permission='library:show');") } else if a.Package == "algorithmManage" { // 应用中心 sort = 7 sqls = append(sqls, "INSERT INTO sys_menu(`id`,`parent_id`,`parent_ids`,`type`,`name`,`module`,`permission`,`sort`,`icon`,`url`,`enable`,`del_flag`) select '"+uuid.NewV4().String()+"', '"+a.Id+"', ',0,"+a.Id+",', 1, '已安装', 'ALGORITHMMANAGE', 'algorithmManage:installed', 1, NULL, NULL, 1, 0 where not exists (select 1 from sys_menu where permission='algorithmManage:installed');") sqls = append(sqls, "INSERT INTO sys_menu(`id`,`parent_id`,`parent_ids`,`type`,`name`,`module`,`permission`,`sort`,`icon`,`url`,`enable`,`del_flag`) select '"+uuid.NewV4().String()+"', '"+a.Id+"', ',0,"+a.Id+",', 1, '离线升级/安装', 'ALGORITHMMANAGE', 'algorithmManage:insOffLine', 2, NULL, NULL, 1, 0 where not exists (select 1 from sys_menu where permission='algorithmManage:insOffLine');") sqls = append(sqls, "INSERT INTO sys_menu(`id`,`parent_id`,`parent_ids`,`type`,`name`,`module`,`permission`,`sort`,`icon`,`url`,`enable`,`del_flag`) select '"+uuid.NewV4().String()+"', '"+a.Id+"', ',0,"+a.Id+",', 1, '应用中心购买', 'ALGORITHMMANAGE', 'algorithmManage:buy', 3, NULL, NULL, 1, 0 where not exists (select 1 from sys_menu where permission='algorithmManage:buy');") } else if a.Package == "datapush" { //数据推送 sort = 8 } else if a.Package == "settings" { //系统设置 sort = 9 /*sqls = append(sqls, "INSERT INTO sys_menu(`id`,`parent_id`,`parent_ids`,`type`,`name`,`module`,`permission`,`sort`,`icon`,`url`,`enable`,`del_flag`) select '"+uuid.NewV4().String()+"', '"+a.Id+"', ',0,"+a.Id+",', 1, '本机信息', 'SETTINGS', 'settings:sysInfo', 1, NULL, NULL, 1, 0 where not exists (select 1 from sys_menu where permission='settings:sysInfo');")*/ sqls = append(sqls, "INSERT INTO sys_menu(`id`,`parent_id`,`parent_ids`,`type`,`name`,`module`,`permission`,`sort`,`icon`,`url`,`enable`,`del_flag`) select '"+uuid.NewV4().String()+"', '"+a.Id+"', ',0,"+a.Id+",', 1, '账户管理', 'SETTINGS', 'settings:account', 1, NULL, NULL, 1, 0 where not exists (select 1 from sys_menu where permission='settings:account');") sqls = append(sqls, "INSERT INTO sys_menu(`id`,`parent_id`,`parent_ids`,`type`,`name`,`module`,`permission`,`sort`,`icon`,`url`,`enable`,`del_flag`) select '"+uuid.NewV4().String()+"', '"+a.Id+"', ',0,"+a.Id+",', 1, '日期时间', 'SETTINGS', 'settings:timeSet', 2, NULL, NULL, 1, 0 where not exists (select 1 from sys_menu where permission='settings:timeSet');") sqls = append(sqls, "INSERT INTO sys_menu(`id`,`parent_id`,`parent_ids`,`type`,`name`,`module`,`permission`,`sort`,`icon`,`url`,`enable`,`del_flag`) select '"+uuid.NewV4().String()+"', '"+a.Id+"', ',0,"+a.Id+",', 1, '集群管理', 'SETTINGS', 'settings:cluster', 3, NULL, NULL, 1, 0 where not exists (select 1 from sys_menu where permission='settings:cluster');") sqls = append(sqls, "INSERT INTO sys_menu(`id`,`parent_id`,`parent_ids`,`type`,`name`,`module`,`permission`,`sort`,`icon`,`url`,`enable`,`del_flag`) select '"+uuid.NewV4().String()+"', '"+a.Id+"', ',0,"+a.Id+",', 1, '网络设置', 'SETTINGS', 'settings:network', 4, NULL, NULL, 1, 0 where not exists (select 1 from sys_menu where permission='settings:network');") sqls = append(sqls, "INSERT INTO sys_menu(`id`,`parent_id`,`parent_ids`,`type`,`name`,`module`,`permission`,`sort`,`icon`,`url`,`enable`,`del_flag`) select '"+uuid.NewV4().String()+"', '"+a.Id+"', ',0,"+a.Id+",', 1, '键盘和语言', 'SETTINGS', 'settings:keyboard', 5, NULL, NULL, 1, 0 where not exists (select 1 from sys_menu where permission='settings:keyboard');") sqls = append(sqls, "INSERT INTO sys_menu(`id`,`parent_id`,`parent_ids`,`type`,`name`,`module`,`permission`,`sort`,`icon`,`url`,`enable`,`del_flag`) select '"+uuid.NewV4().String()+"', '"+a.Id+"', ',0,"+a.Id+",', 1, '通用设置', 'SETTINGS', 'settings:common', 6, NULL, NULL, 1, 0 where not exists (select 1 from sys_menu where permission='settings:common');") sqls = append(sqls, "INSERT INTO sys_menu(`id`,`parent_id`,`parent_ids`,`type`,`name`,`module`,`permission`,`sort`,`icon`,`url`,`enable`,`del_flag`) select '"+uuid.NewV4().String()+"', '"+a.Id+"', ',0,"+a.Id+",', 1, '设备信息', 'SETTINGS', 'settings:sysInfo', 7, NULL, NULL, 1, 0 where not exists (select 1 from sys_menu where permission='settings:sysInfo');") /*sqls = append(sqls, "INSERT INTO sys_menu(`id`,`parent_id`,`parent_ids`,`type`,`name`,`module`,`permission`,`sort`,`icon`,`url`,`enable`,`del_flag`) select '"+uuid.NewV4().String()+"', '"+a.Id+"', ',0,"+a.Id+",', 1, '权限管理', 'SETTINGS', 'settings:permission', 4, NULL, NULL, 1, 0 where not exists (select 1 from sys_menu where permission='settings:permission');")*/ /*sqls = append(sqls, "INSERT INTO sys_menu(`id`,`parent_id`,`parent_ids`,`type`,`name`,`module`,`permission`,`sort`,`icon`,`url`,`enable`,`del_flag`) select '"+uuid.NewV4().String()+"', '"+a.Id+"', ',0,"+a.Id+",', 1, '广播设置', 'SETTINGS', 'settings:broadcast', 5, NULL, NULL, 1, 0 where not exists (select 1 from sys_menu where permission='settings:broadcast');")*/ } else if a.Package == "vindicate" { //系统维护 sort = 10 sqls = append(sqls, "INSERT INTO sys_menu(`id`,`parent_id`,`parent_ids`,`type`,`name`,`module`,`permission`,`sort`,`icon`,`url`,`enable`,`del_flag`) select '"+uuid.NewV4().String()+"', '"+a.Id+"', ',0,"+a.Id+",', 1, '设备维护', 'VINDICATE', 'vindicate:device', 1, NULL, NULL, 1, 0 where not exists (select 1 from sys_menu where permission='vindicate:device');") sqls = append(sqls, "INSERT INTO sys_menu(`id`,`parent_id`,`parent_ids`,`type`,`name`,`module`,`permission`,`sort`,`icon`,`url`,`enable`,`del_flag`) select '"+uuid.NewV4().String()+"', '"+a.Id+"', ',0,"+a.Id+",', 1, '数据库维护', 'VINDICATE', 'vindicate:db', 2, NULL, NULL, 1, 0 where not exists (select 1 from sys_menu where permission='vindicate:db');") } else if a.Package == "syslog" { sort = 11 } menu := models.SysMenu{} if rows, _ := menu.SelectById(a.Id); rows == 0 { newM := models.SysMenu{ Id: a.Id, ParentId: "0", ParentIds: ",0,", Type: 0, Name: a.Name, Module: strings.ToUpper(a.Package), Permission: a.Package, Sort: sort, Enable: true, DelFlag: false, } newM.Insert() } if len(sqls) > 0 { models.GetDB().Exec(strings.Join(sqls, "")) } } } for _, a := range apps { if !a.IsDefault { var sqls []string menu := models.SysMenu{} maxSort := menu.GetMaxSort() if rows, _ := menu.SelectById(a.Id); rows == 0 { newM := models.SysMenu{ Id: a.Id, ParentId: "0", ParentIds: ",0,", Type: 0, Name: a.Name, Module: strings.ToUpper(a.Package), Permission: a.Package, Sort: maxSort + 1, Enable: true, DelFlag: false, } if a.Package == "dataStack" { newM.Sort = 4 sqls = append(sqls, "INSERT INTO sys_menu(`id`,`parent_id`,`parent_ids`,`type`,`name`,`module`,`permission`,`sort`,`icon`,`url`,`enable`,`del_flag`) select '"+uuid.NewV4().String()+"', '"+a.Id+"', ',0,"+a.Id+",', 1, '数据栈信息', 'DATASTACK', 'dataStack:cameraInfo', 1, NULL, NULL, 1, 0 where not exists (select 1 from sys_menu where permission='dataStack:cameraInfo');") sqls = append(sqls, "INSERT INTO sys_menu(`id`,`parent_id`,`parent_ids`,`type`,`name`,`module`,`permission`,`sort`,`icon`,`url`,`enable`,`del_flag`) select '"+uuid.NewV4().String()+"', '"+a.Id+"', ',0,"+a.Id+",', 1, '独立场景', 'DATASTACK', 'dataStack:selfRule', 2, NULL, NULL, 1, 0 where not exists (select 1 from sys_menu where permission='dataStack:selfRule');") } newM.Insert() if len(sqls) > 0 { models.GetDB().Exec(strings.Join(sqls, "")) } } } } } else { //logger.Debug("app found:",apps) } }