package networkelement

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

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

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

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

	neGroup := router.Group("/ne")

	// 网元操作处理
	neActionGroup := neGroup.Group("/action")
	{
		neActionGroup.GET("/files",
			middleware.PreAuthorize(nil),
			controller.NewNeAction.Files,
		)
		neActionGroup.GET("/pullFile",
			middleware.PreAuthorize(nil),
			controller.NewNeAction.PullFile,
		)
		neActionGroup.POST("/pushFile",
			middleware.PreAuthorize(nil),
			collectlogs.OperateLog(collectlogs.OptionNew("log.operate.title.neAction", collectlogs.BUSINESS_TYPE_IMPORT)),
			controller.NewNeAction.PushFile,
		)
	}

	// 网元信息
	neInfoGroup := neGroup.Group("/info")
	{
		neInfoGroup.GET("/listAll",
			middleware.PreAuthorize(nil),
			controller.NewNeInfo.ListAll,
		)
		neInfoGroup.GET("/list",
			middleware.PreAuthorize(nil),
			controller.NewNeInfo.List,
		)
		neInfoGroup.GET("/:infoId",
			middleware.PreAuthorize(nil),
			controller.NewNeInfo.Info,
		)
		neInfoGroup.POST("",
			middleware.PreAuthorize(nil),
			collectlogs.OperateLog(collectlogs.OptionNew("log.operate.title.neInfo", collectlogs.BUSINESS_TYPE_INSERT)),
			controller.NewNeInfo.Add,
		)
		neInfoGroup.PUT("",
			middleware.PreAuthorize(nil),
			collectlogs.OperateLog(collectlogs.OptionNew("log.operate.title.neInfo", collectlogs.BUSINESS_TYPE_UPDATE)),
			controller.NewNeInfo.Edit,
		)
		neInfoGroup.DELETE("/:infoIds",
			middleware.PreAuthorize(nil),
			collectlogs.OperateLog(collectlogs.OptionNew("log.operate.title.neInfo", collectlogs.BUSINESS_TYPE_DELETE)),
			controller.NewNeInfo.Remove,
		)
		neInfoGroup.GET("/state",
			middleware.PreAuthorize(nil),
			controller.NewNeInfo.State,
		)
		neInfoGroup.GET("/byTypeAndID",
			middleware.PreAuthorize(nil),
			controller.NewNeInfo.NeTypeAndID,
		)
	}

	// 网元主机
	neHostGroup := neGroup.Group("/host")
	{
		neHostGroup.GET("/list",
			middleware.PreAuthorize(nil),
			controller.NewNeHost.List,
		)
		neHostGroup.GET("/:hostId",
			middleware.PreAuthorize(nil),
			controller.NewNeHost.Info,
		)
		neHostGroup.POST("",
			middleware.PreAuthorize(nil),
			collectlogs.OperateLog(collectlogs.OptionNew("log.operate.title.neHost", collectlogs.BUSINESS_TYPE_INSERT)),
			controller.NewNeHost.Add,
		)
		neHostGroup.PUT("",
			middleware.PreAuthorize(nil),
			collectlogs.OperateLog(collectlogs.OptionNew("log.operate.title.neHost", collectlogs.BUSINESS_TYPE_UPDATE)),
			controller.NewNeHost.Edit,
		)
		neHostGroup.DELETE("/:hostIds",
			middleware.PreAuthorize(nil),
			collectlogs.OperateLog(collectlogs.OptionNew("log.operate.title.neHost", collectlogs.BUSINESS_TYPE_DELETE)),
			controller.NewNeHost.Remove,
		)
		neHostGroup.POST("/test",
			middleware.PreAuthorize(nil),
			collectlogs.OperateLog(collectlogs.OptionNew("log.operate.title.neHost", collectlogs.BUSINESS_TYPE_OTHER)),
			controller.NewNeHost.Test,
		)
		neHostGroup.POST("/cmd",
			middleware.PreAuthorize(nil),
			collectlogs.OperateLog(collectlogs.OptionNew("log.operate.title.neHost", collectlogs.BUSINESS_TYPE_OTHER)),
			controller.NewNeHost.Cmd,
		)
		neHostGroup.POST("/checkBySSH",
			middleware.PreAuthorize(nil),
			collectlogs.OperateLog(collectlogs.OptionNew("log.operate.title.neHost", collectlogs.BUSINESS_TYPE_OTHER)),
			controller.NewNeHost.CheckBySSH,
		)
	}

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

	// 网元版本信息
	neVersionGroup := neGroup.Group("/version")
	{
		neVersionGroup.GET("/list",
			middleware.PreAuthorize(nil),
			controller.NewNeVersion.List,
		)
		neVersionGroup.GET("/:versionId",
			middleware.PreAuthorize(nil),
			controller.NewNeVersion.Info,
		)
		neVersionGroup.POST("",
			middleware.PreAuthorize(nil),
			collectlogs.OperateLog(collectlogs.OptionNew("log.operate.title.neVersion", collectlogs.BUSINESS_TYPE_INSERT)),
			controller.NewNeVersion.Add,
		)
		neVersionGroup.PUT("",
			middleware.PreAuthorize(nil),
			collectlogs.OperateLog(collectlogs.OptionNew("log.operate.title.neVersion", collectlogs.BUSINESS_TYPE_UPDATE)),
			controller.NewNeVersion.Edit,
		)
		neVersionGroup.DELETE("/:versionIds",
			middleware.PreAuthorize(nil),
			collectlogs.OperateLog(collectlogs.OptionNew("log.operate.title.neVersion", collectlogs.BUSINESS_TYPE_DELETE)),
			controller.NewNeVersion.Remove,
		)
	}

	// 网元软件包信息
	neSoftwareGroup := neGroup.Group("/software")
	{
		neSoftwareGroup.GET("/list",
			middleware.PreAuthorize(nil),
			controller.NewNeSoftware.List,
		)
		neSoftwareGroup.GET("/:softwareId",
			middleware.PreAuthorize(nil),
			controller.NewNeSoftware.Info,
		)
		neSoftwareGroup.POST("",
			middleware.PreAuthorize(nil),
			collectlogs.OperateLog(collectlogs.OptionNew("log.operate.title.neSoftware", collectlogs.BUSINESS_TYPE_INSERT)),
			controller.NewNeSoftware.Add,
		)
		neSoftwareGroup.PUT("",
			middleware.PreAuthorize(nil),
			collectlogs.OperateLog(collectlogs.OptionNew("log.operate.title.neSoftware", collectlogs.BUSINESS_TYPE_UPDATE)),
			controller.NewNeSoftware.Edit,
		)
		neSoftwareGroup.DELETE("/:softwareIds",
			middleware.PreAuthorize(nil),
			collectlogs.OperateLog(collectlogs.OptionNew("log.operate.title.neSoftware", collectlogs.BUSINESS_TYPE_DELETE)),
			controller.NewNeSoftware.Remove,
		)
		neSoftwareGroup.POST("/install",
			middleware.PreAuthorize(nil),
			collectlogs.OperateLog(collectlogs.OptionNew("log.operate.title.neSoftware", collectlogs.BUSINESS_TYPE_DELETE)),
			controller.NewNeSoftware.Install,
		)
	}

	// UDM鉴权用户信息
	udmAuthGroup := neGroup.Group("/udm/auth")
	{
		udmAuthGroup.PUT("/resetData/:neId",
			repeat.RepeatSubmit(5),
			middleware.PreAuthorize(nil),
			collectlogs.OperateLog(collectlogs.OptionNew("log.operate.title.udmAuth", collectlogs.BUSINESS_TYPE_CLEAN)),
			controller.NewUDMAuth.ResetData,
		)
		udmAuthGroup.GET("/list",
			middleware.PreAuthorize(nil),
			controller.NewUDMAuth.List,
		)
		udmAuthGroup.GET("/:neId/:imsi",
			middleware.PreAuthorize(nil),
			controller.NewUDMAuth.Info,
		)
		udmAuthGroup.POST("/:neId",
			middleware.PreAuthorize(nil),
			collectlogs.OperateLog(collectlogs.OptionNew("log.operate.title.udmAuth", collectlogs.BUSINESS_TYPE_INSERT)),
			controller.NewUDMAuth.Add,
		)
		udmAuthGroup.POST("/:neId/:num",
			middleware.PreAuthorize(nil),
			collectlogs.OperateLog(collectlogs.OptionNew("log.operate.title.udmAuth", collectlogs.BUSINESS_TYPE_INSERT)),
			controller.NewUDMAuth.Adds,
		)
		udmAuthGroup.PUT("/:neId",
			middleware.PreAuthorize(nil),
			collectlogs.OperateLog(collectlogs.OptionNew("log.operate.title.udmAuth", collectlogs.BUSINESS_TYPE_UPDATE)),
			controller.NewUDMAuth.Edit,
		)
		udmAuthGroup.DELETE("/:neId/:imsi",
			middleware.PreAuthorize(nil),
			collectlogs.OperateLog(collectlogs.OptionNew("log.operate.title.udmAuth", collectlogs.BUSINESS_TYPE_DELETE)),
			controller.NewUDMAuth.Remove,
		)
		udmAuthGroup.DELETE("/:neId/:imsi/:num",
			middleware.PreAuthorize(nil),
			collectlogs.OperateLog(collectlogs.OptionNew("log.operate.title.udmAuth", collectlogs.BUSINESS_TYPE_DELETE)),
			controller.NewUDMAuth.Removes,
		)
		udmAuthGroup.POST("/export",
			middleware.PreAuthorize(nil),
			collectlogs.OperateLog(collectlogs.OptionNew("log.operate.title.udmAuth", collectlogs.BUSINESS_TYPE_EXPORT)),
			controller.NewUDMAuth.Export,
		)
		udmAuthGroup.POST("/import",
			middleware.PreAuthorize(nil),
			collectlogs.OperateLog(collectlogs.OptionNew("log.operate.title.udmAuth", collectlogs.BUSINESS_TYPE_IMPORT)),
			controller.NewUDMAuth.Import,
		)
	}

	// UDM签约用户信息
	udmSubGroup := neGroup.Group("/udm/sub")
	{
		udmSubGroup.PUT("/resetData/:neId",
			repeat.RepeatSubmit(5),
			middleware.PreAuthorize(nil),
			collectlogs.OperateLog(collectlogs.OptionNew("log.operate.title.udmSub", collectlogs.BUSINESS_TYPE_CLEAN)),
			controller.NewUDMSub.ResetData,
		)
		udmSubGroup.GET("/list",
			middleware.PreAuthorize(nil),
			controller.NewUDMSub.List,
		)
		udmSubGroup.GET("/:neId/:imsi",
			middleware.PreAuthorize(nil),
			controller.NewUDMSub.Info,
		)
		udmSubGroup.POST("/:neId",
			middleware.PreAuthorize(nil),
			collectlogs.OperateLog(collectlogs.OptionNew("log.operate.title.udmSub", collectlogs.BUSINESS_TYPE_INSERT)),
			controller.NewUDMSub.Add,
		)
		udmSubGroup.POST("/:neId/:num",
			middleware.PreAuthorize(nil),
			collectlogs.OperateLog(collectlogs.OptionNew("log.operate.title.udmSub", collectlogs.BUSINESS_TYPE_INSERT)),
			controller.NewUDMSub.Adds,
		)
		udmSubGroup.PUT("/:neId",
			middleware.PreAuthorize(nil),
			collectlogs.OperateLog(collectlogs.OptionNew("log.operate.title.udmSub", collectlogs.BUSINESS_TYPE_UPDATE)),
			controller.NewUDMSub.Edit,
		)
		udmSubGroup.DELETE("/:neId/:imsi",
			middleware.PreAuthorize(nil),
			collectlogs.OperateLog(collectlogs.OptionNew("log.operate.title.udmSub", collectlogs.BUSINESS_TYPE_DELETE)),
			controller.NewUDMSub.Remove,
		)
		udmSubGroup.DELETE("/:neId/:imsi/:num",
			middleware.PreAuthorize(nil),
			collectlogs.OperateLog(collectlogs.OptionNew("log.operate.title.udmSub", collectlogs.BUSINESS_TYPE_DELETE)),
			controller.NewUDMSub.Removes,
		)
		udmSubGroup.POST("/export",
			middleware.PreAuthorize(nil),
			collectlogs.OperateLog(collectlogs.OptionNew("log.operate.title.udmSub", collectlogs.BUSINESS_TYPE_EXPORT)),
			controller.NewUDMSub.Export,
		)
		udmSubGroup.POST("/import",
			middleware.PreAuthorize(nil),
			collectlogs.OperateLog(collectlogs.OptionNew("log.operate.title.udmSub", collectlogs.BUSINESS_TYPE_IMPORT)),
			controller.NewUDMSub.Import,
		)
	}
}

// InitLoad 初始参数
func InitLoad() {
	// 启动时，清除缓存-网元类型
	service.NewNeInfoImpl.ClearNeCacheByNeType("*")
	service.NewNeInfoImpl.SelectNeInfoByRmuid("")
}
