package controller import ( "fmt" "be.ems/src/framework/i18n" "be.ems/src/framework/reqctx" "be.ems/src/framework/resp" "be.ems/src/framework/utils/parse" neService "be.ems/src/modules/network_element/service" "github.com/gin-gonic/gin" ) // 实例化控制层 NeVersionController 结构体 var NewNeVersion = &NeVersionController{ neVersionService: neService.NewNeVersion, } // 网元版本请求 // // PATH /version type NeVersionController struct { neVersionService *neService.NeVersion // 网元版本服务 } // 网元版本列表 // // GET /list func (s *NeVersionController) List(c *gin.Context) { query := reqctx.QueryMap(c) rows, total := s.neVersionService.FindByPage(query, true) c.JSON(200, resp.OkData(map[string]any{"total": total, "rows": rows})) } // 网元版本信息 // // GET /:id func (s *NeVersionController) Info(c *gin.Context) { language := reqctx.AcceptLanguage(c) id := parse.Number(c.Param("id")) if id <= 0 { c.JSON(422, resp.CodeMsg(422002, "bind err: id is empty")) return } neVersion := s.neVersionService.FindById(id) if neVersion.ID != id { // 没有可访问网元版本数据! c.JSON(200, resp.ErrMsg(i18n.TKey(language, "neVersion.noData"))) return } c.JSON(200, resp.OkData(neVersion)) } // 网元版本操作 // // POST /operate // // @Tags network_element/version // @Accept json // @Produce json // @Param data body object true "Request Param" // @Success 200 {object} object "Response Results" // @Security TokenAuth // @Summary Network element version operation // @Description Network element version operation // @Router /ne/version/operate [post] func (s *NeVersionController) Operate(c *gin.Context) { language := reqctx.AcceptLanguage(c) var body struct { Action string `json:"action" binding:"required,oneof=install upgrade rollback"` // 操作行为 NeType string `json:"neType" gorm:"ne_type" binding:"required"` // 网元类型 NeId string `json:"neId" gorm:"ne_id" binding:"required"` // 网元ID Preinput map[string]string `json:"preinput" ` // 预先输入参数 } if err := c.ShouldBindBodyWithJSON(&body); err != nil { errMsgs := fmt.Sprintf("bind err: %s", resp.FormatBindError(err)) c.JSON(422, resp.CodeMsg(422001, errMsgs)) return } neVersion := s.neVersionService.FindByNeTypeAndNeID(body.NeType, body.NeId) if neVersion.NeId != body.NeId { // 没有可访问网元版本数据! c.JSON(200, resp.ErrMsg(i18n.TKey(language, "neVersion.noData"))) return } // 进行相关命令操作 output, err := s.neVersionService.Operate(body.Action, neVersion, body.Preinput) if err != nil { c.JSON(200, resp.ErrMsg(i18n.TKey(language, err.Error()))) return } c.JSON(200, resp.OkData(output)) }