package ne

import (
	"be.ems/src/framework/logger"
	"be.ems/src/framework/middleware"
	"be.ems/src/framework/middleware/collectlogs"
	"be.ems/src/modules/ne/controller"
	"be.ems/src/modules/ne/service"

	"github.com/gin-gonic/gin"
)

// 模块路由注册
func Setup(router *gin.Engine) {
	logger.Infof("开始加载 ====> ne 模块路由")

	// 启动时需要的初始参数
	InitLoad()

	// 网元操作处理
	neActionGroup := router.Group("/ne/action")
	{
		neActionGroup.GET("/files",
			middleware.AuthorizeUser(nil),
			controller.NewNeAction.Files,
		)
		neActionGroup.GET("/pullFile",
			middleware.AuthorizeUser(nil),
			controller.NewNeAction.PullFile,
		)
		neActionGroup.POST("/pushFile",
			middleware.AuthorizeUser(nil),
			collectlogs.OperateLog(collectlogs.OptionNew("log.operate.title.neAction", collectlogs.BUSINESS_TYPE_IMPORT)),
			controller.NewNeAction.PushFile,
		)
		neActionGroup.GET("/pullDirZip",
			middleware.AuthorizeUser(nil),
			controller.NewNeAction.PullDirZip,
		)
		neActionGroup.GET("/viewFile",
			middleware.AuthorizeUser(nil),
			controller.NewNeAction.ViewFile,
		)
		neActionGroup.PUT("/service",
			middleware.AuthorizeUser(nil),
			collectlogs.OperateLog(collectlogs.OptionNew("log.operate.title.neAction", collectlogs.BUSINESS_TYPE_OTHER)),
			controller.NewNeAction.Service,
		)
	}

	// 网元信息
	neInfoGroup := router.Group("/ne/info")
	{
		neInfoGroup.GET("/state",
			middleware.AuthorizeUser(nil),
			controller.NewNeInfo.State,
		)
		neInfoGroup.GET("/byTypeAndID",
			middleware.AuthorizeUser(nil),
			controller.NewNeInfo.NeTypeAndID,
		)
		neInfoGroup.GET("/listAll",
			middleware.AuthorizeUser(nil),
			controller.NewNeInfo.ListAll,
		)
		neInfoGroup.GET("/para5GFile",
			middleware.AuthorizeUser(nil),
			controller.NewNeInfo.Para5GFileRead,
		)
		neInfoGroup.PUT("/para5GFile",
			middleware.AuthorizeUser(nil),
			collectlogs.OperateLog(collectlogs.OptionNew("log.operate.title.neInfo", collectlogs.BUSINESS_TYPE_OTHER)),
			controller.NewNeInfo.Para5GFileWrite,
		)
		neInfoGroup.GET("/oamFile",
			middleware.AuthorizeUser(nil),
			controller.NewNeInfo.OAMFileRead,
		)
		neInfoGroup.PUT("/oamFile",
			middleware.AuthorizeUser(nil),
			collectlogs.OperateLog(collectlogs.OptionNew("log.operate.title.neInfo", collectlogs.BUSINESS_TYPE_OTHER)),
			controller.NewNeInfo.OAMFileWrite,
		)
		neInfoGroup.GET("/list",
			middleware.AuthorizeUser(nil),
			controller.NewNeInfo.List,
		)
		neInfoGroup.GET("/:id",
			middleware.CryptoApi(false, true),
			middleware.AuthorizeUser(nil),
			controller.NewNeInfo.Info,
		)
		neInfoGroup.POST("",
			middleware.CryptoApi(true, true),
			middleware.AuthorizeUser(nil),
			collectlogs.OperateLog(collectlogs.OptionNew("log.operate.title.neInfo", collectlogs.BUSINESS_TYPE_INSERT)),
			controller.NewNeInfo.Add,
		)
		neInfoGroup.PUT("",
			middleware.CryptoApi(true, true),
			middleware.AuthorizeUser(nil),
			collectlogs.OperateLog(collectlogs.OptionNew("log.operate.title.neInfo", collectlogs.BUSINESS_TYPE_UPDATE)),
			controller.NewNeInfo.Edit,
		)
		neInfoGroup.DELETE(":id",
			middleware.AuthorizeUser(nil),
			collectlogs.OperateLog(collectlogs.OptionNew("log.operate.title.neInfo", collectlogs.BUSINESS_TYPE_DELETE)),
			controller.NewNeInfo.Remove,
		)
	}

	// 网元主机
	neHostGroup := router.Group("/ne/host")
	{
		neHostGroup.GET("/list",
			middleware.AuthorizeUser(nil),
			controller.NewNeHost.List,
		)
		neHostGroup.GET("/:id",
			middleware.CryptoApi(false, true),
			middleware.AuthorizeUser(nil),
			controller.NewNeHost.Info,
		)
		neHostGroup.POST("",
			middleware.AuthorizeUser(nil),
			collectlogs.OperateLog(collectlogs.OptionNew("log.operate.title.neHost", collectlogs.BUSINESS_TYPE_INSERT)),
			controller.NewNeHost.Add,
		)
		neHostGroup.PUT("",
			middleware.AuthorizeUser(nil),
			collectlogs.OperateLog(collectlogs.OptionNew("log.operate.title.neHost", collectlogs.BUSINESS_TYPE_UPDATE)),
			controller.NewNeHost.Edit,
		)
		neHostGroup.DELETE("/:id",
			middleware.AuthorizeUser(nil),
			collectlogs.OperateLog(collectlogs.OptionNew("log.operate.title.neHost", collectlogs.BUSINESS_TYPE_DELETE)),
			controller.NewNeHost.Remove,
		)
		neHostGroup.POST("/test",
			middleware.AuthorizeUser(nil),
			collectlogs.OperateLog(collectlogs.OptionNew("log.operate.title.neHost", collectlogs.BUSINESS_TYPE_OTHER)),
			controller.NewNeHost.Test,
		)
		neHostGroup.POST("/cmd",
			middleware.AuthorizeUser(nil),
			collectlogs.OperateLog(collectlogs.OptionNew("log.operate.title.neHost", collectlogs.BUSINESS_TYPE_OTHER)),
			controller.NewNeHost.Cmd,
		)
		neHostGroup.POST("/checkBySSH",
			middleware.AuthorizeUser(nil),
			collectlogs.OperateLog(collectlogs.OptionNew("log.operate.title.neHost", collectlogs.BUSINESS_TYPE_OTHER)),
			controller.NewNeHost.CheckBySSH,
		)
		neHostGroup.POST("/authorizedBySSH",
			middleware.AuthorizeUser(nil),
			collectlogs.OperateLog(collectlogs.OptionNew("log.operate.title.neHost", collectlogs.BUSINESS_TYPE_OTHER)),
			controller.NewNeHost.AuthorizedBySSH,
		)
	}

	// 网元主机命令
	neHostCmdGroup := router.Group("/ne/hostCmd")
	{
		neHostCmdGroup.GET("/list",
			middleware.AuthorizeUser(nil),
			controller.NewNeHostCmd.List,
		)
		neHostCmdGroup.GET("/:id",
			middleware.AuthorizeUser(nil),
			controller.NewNeHostCmd.Info,
		)
		neHostCmdGroup.POST("",
			middleware.AuthorizeUser(nil),
			collectlogs.OperateLog(collectlogs.OptionNew("log.operate.title.neHostCmd", collectlogs.BUSINESS_TYPE_INSERT)),
			controller.NewNeHostCmd.Add,
		)
		neHostCmdGroup.PUT("",
			middleware.AuthorizeUser(nil),
			collectlogs.OperateLog(collectlogs.OptionNew("log.operate.title.neHostCmd", collectlogs.BUSINESS_TYPE_UPDATE)),
			controller.NewNeHostCmd.Edit,
		)
		neHostCmdGroup.DELETE(":id",
			middleware.AuthorizeUser(nil),
			collectlogs.OperateLog(collectlogs.OptionNew("log.operate.title.neHostCmd", collectlogs.BUSINESS_TYPE_DELETE)),
			controller.NewNeHostCmd.Remove,
		)
	}

	// 网元版本信息
	neVersionGroup := router.Group("/ne/version")
	{
		neVersionGroup.GET("/list",
			middleware.AuthorizeUser(nil),
			controller.NewNeVersion.List,
		)
		neVersionGroup.GET("/:id",
			middleware.AuthorizeUser(nil),
			controller.NewNeVersion.Info,
		)
		neVersionGroup.POST("/operate",
			middleware.AuthorizeUser(nil),
			collectlogs.OperateLog(collectlogs.OptionNew("log.operate.title.neVersion", collectlogs.BUSINESS_TYPE_OTHER)),
			controller.NewNeVersion.Operate,
		)
	}

	// 网元软件包信息
	neSoftwareGroup := router.Group("/ne/software")
	{
		neSoftwareGroup.GET("/list",
			middleware.AuthorizeUser(nil),
			controller.NewNeSoftware.List,
		)
		neSoftwareGroup.GET("/:id",
			middleware.AuthorizeUser(nil),
			controller.NewNeSoftware.Info,
		)
		neSoftwareGroup.POST("",
			middleware.AuthorizeUser(nil),
			collectlogs.OperateLog(collectlogs.OptionNew("log.operate.title.neSoftware", collectlogs.BUSINESS_TYPE_INSERT)),
			controller.NewNeSoftware.Add,
		)
		neSoftwareGroup.PUT("",
			middleware.AuthorizeUser(nil),
			collectlogs.OperateLog(collectlogs.OptionNew("log.operate.title.neSoftware", collectlogs.BUSINESS_TYPE_UPDATE)),
			controller.NewNeSoftware.Edit,
		)
		neSoftwareGroup.DELETE(":id",
			middleware.AuthorizeUser(nil),
			collectlogs.OperateLog(collectlogs.OptionNew("log.operate.title.neSoftware", collectlogs.BUSINESS_TYPE_DELETE)),
			controller.NewNeSoftware.Remove,
		)
		neSoftwareGroup.POST("/newNeVersion",
			middleware.AuthorizeUser(nil),
			collectlogs.OperateLog(collectlogs.OptionNew("log.operate.title.neSoftware", collectlogs.BUSINESS_TYPE_OTHER)),
			controller.NewNeSoftware.NewNeVersion,
		)
	}

	// 网元授权激活信息
	neLicenseGroup := router.Group("/ne/license")
	{
		neLicenseGroup.GET("/list",
			middleware.AuthorizeUser(nil),
			controller.NewNeLicense.List,
		)
		neLicenseGroup.GET("/:id",
			middleware.AuthorizeUser(nil),
			controller.NewNeLicense.Info,
		)
		neLicenseGroup.GET("/byTypeAndID",
			middleware.AuthorizeUser(nil),
			controller.NewNeLicense.NeTypeAndID,
		)
		neLicenseGroup.GET("/code",
			middleware.AuthorizeUser(nil),
			controller.NewNeLicense.Code,
		)
		neLicenseGroup.POST("/change",
			middleware.AuthorizeUser(nil),
			collectlogs.OperateLog(collectlogs.OptionNew("log.operate.title.neLicense", collectlogs.BUSINESS_TYPE_OTHER)),
			controller.NewNeLicense.Change,
		)
		neLicenseGroup.GET("/state",
			middleware.AuthorizeUser(nil),
			controller.NewNeLicense.State,
		)
	}

	// 网元参数配置
	neConfigGroup := router.Group("/ne/config")
	{
		// 网元参数配置可用属性值
		neConfigGroup.GET("/list",
			middleware.AuthorizeUser(nil),
			controller.NewNeConfig.List,
		)
		neConfigGroup.GET("/info/:id",
			middleware.AuthorizeUser(nil),
			controller.NewNeConfig.Info,
		)
		neConfigGroup.POST("",
			middleware.AuthorizeUser(nil),
			collectlogs.OperateLog(collectlogs.OptionNew("log.operate.title.neConfig", collectlogs.BUSINESS_TYPE_INSERT)),
			controller.NewNeConfig.Add,
		)
		neConfigGroup.PUT("",
			middleware.AuthorizeUser(nil),
			collectlogs.OperateLog(collectlogs.OptionNew("log.operate.title.neConfig", collectlogs.BUSINESS_TYPE_UPDATE)),
			controller.NewNeConfig.Edit,
		)
		neConfigGroup.DELETE("",
			middleware.AuthorizeUser(nil),
			collectlogs.OperateLog(collectlogs.OptionNew("log.operate.title.neConfig", collectlogs.BUSINESS_TYPE_DELETE)),
			controller.NewNeConfig.Remove,
		)
		neConfigGroup.GET("/list/:neType",
			middleware.AuthorizeUser(nil),
			controller.NewNeConfig.ListByNeType,
		)
		// 网元参数配置数据
		neConfigGroup.GET("/data",
			middleware.AuthorizeUser(nil),
			controller.NewNeConfig.DataInfo,
		)
		neConfigGroup.PUT("/data",
			middleware.AuthorizeUser(nil),
			collectlogs.OperateLog(collectlogs.OptionNew("log.operate.title.neConfig", collectlogs.BUSINESS_TYPE_UPDATE)),
			controller.NewNeConfig.DataEdit,
		)
		neConfigGroup.POST("/data",
			middleware.AuthorizeUser(nil),
			collectlogs.OperateLog(collectlogs.OptionNew("log.operate.title.neConfig", collectlogs.BUSINESS_TYPE_INSERT)),
			controller.NewNeConfig.DataAdd,
		)
		neConfigGroup.DELETE("/data",
			middleware.AuthorizeUser(nil),
			collectlogs.OperateLog(collectlogs.OptionNew("log.operate.title.neConfig", collectlogs.BUSINESS_TYPE_DELETE)),
			controller.NewNeConfig.DataRemove,
		)
	}

	// 网元配置文件备份记录
	neConfigBackupGroup := router.Group("/ne/config/backup")
	{
		neConfigBackupGroup.GET("/list",
			middleware.AuthorizeUser(nil),
			controller.NewNeConfigBackup.List,
		)
		neConfigBackupGroup.GET("/download",
			middleware.AuthorizeUser(nil),
			controller.NewNeConfigBackup.Download,
		)
		neConfigBackupGroup.PUT("",
			middleware.AuthorizeUser(map[string][]string{"hasPerms": {"ne:neConfigBackup:edit"}}),
			collectlogs.OperateLog(collectlogs.OptionNew("log.operate.title.neConfigBackup", collectlogs.BUSINESS_TYPE_UPDATE)),
			controller.NewNeConfigBackup.Edit,
		)
		neConfigBackupGroup.DELETE("",
			middleware.AuthorizeUser(map[string][]string{"hasPerms": {"ne:neConfigBackup:remove"}}),
			collectlogs.OperateLog(collectlogs.OptionNew("log.operate.title.neConfigBackup", collectlogs.BUSINESS_TYPE_DELETE)),
			controller.NewNeConfigBackup.Remove,
		)
		neConfigBackupGroup.POST("/import",
			middleware.AuthorizeUser(nil),
			collectlogs.OperateLog(collectlogs.OptionNew("log.operate.title.neConfigBackup", collectlogs.BUSINESS_TYPE_IMPORT)),
			controller.NewNeConfigBackup.Import,
		)
		neConfigBackupGroup.POST("/export",
			middleware.AuthorizeUser(nil),
			collectlogs.OperateLog(collectlogs.OptionNew("log.operate.title.neConfigBackup", collectlogs.BUSINESS_TYPE_EXPORT)),
			controller.NewNeConfigBackup.Export,
		)
	}
}

// InitLoad 初始参数
func InitLoad() {
	// 启动时，清除缓存-网元类型
	service.NewNeInfo.ClearNeCacheByNeType("*")
	service.NewNeInfo.FindByRmuid("")
	// 启动时，网元公共参数数据记录到全局变量
	if para5GMap, err := service.NewNeInfo.NeConfPara5GRead(); para5GMap != nil && err == nil {
		service.NewNeInfo.NeConfPara5GWirte(para5GMap, nil)
	}
	// 启动时，清除缓存-网元参数配置可用属性值
	service.NewNeConfig.ClearNeCacheByNeType("*")
	service.NewNeConfig.RefreshByNeTypeAndNeID("*")
}
