package controller import ( "strings" "be.ems/src/framework/i18n" "be.ems/src/framework/utils/ctx" "be.ems/src/framework/utils/parse" "be.ems/src/framework/vo/result" "be.ems/src/modules/network_element/model" neService "be.ems/src/modules/network_element/service" "github.com/gin-gonic/gin" "github.com/gin-gonic/gin/binding" ) // 实例化控制层 NeSoftwareController 结构体 var NewNeSoftware = &NeSoftwareController{ neSoftwareService: neService.NewNeSoftwareImpl, } // 网元软件包信息请求 // // PATH /software type NeSoftwareController struct { // 网元软件包信息服务 neSoftwareService neService.INeSoftware } // 网元软件包信息列表 // // GET /list func (s *NeSoftwareController) List(c *gin.Context) { querys := ctx.QueryMap(c) data := s.neSoftwareService.SelectPage(querys) c.JSON(200, result.Ok(data)) } // 网元软件包信息信息 // // GET /:softwareId func (s *NeSoftwareController) Info(c *gin.Context) { language := ctx.AcceptLanguage(c) softwareId := c.Param("softwareId") if softwareId == "" { c.JSON(400, result.CodeMsg(400, i18n.TKey(language, "app.common.err400"))) return } neSoftware := s.neSoftwareService.SelectById(softwareId) if neSoftware.ID != softwareId { // 没有可访问网元包信息数据! c.JSON(200, result.ErrMsg(i18n.TKey(language, "neSoftware.noData"))) return } c.JSON(200, result.OkData(neSoftware)) } // 网元软件包信息新增 // // POST / func (s *NeSoftwareController) Add(c *gin.Context) { language := ctx.AcceptLanguage(c) var body model.NeSoftware err := c.ShouldBindBodyWith(&body, binding.JSON) if err != nil || body.Path == "" || body.ID != "" { c.JSON(400, result.CodeMsg(400, i18n.TKey(language, "app.common.err400"))) return } // 找到已存在的删除后重新添加 neSoftwares := s.neSoftwareService.SelectList(model.NeSoftware{ NeType: body.NeType, Name: body.Name, Version: body.Version, }) if len(neSoftwares) > 0 { neSoftware := neSoftwares[0] s.neSoftwareService.DeleteByIds([]string{neSoftware.ID}) } // 检查属性值唯一 // uniqueSoftware := s.neSoftwareService.CheckUniqueTypeAndNameAndVersion(body.NeType, body.Name, body.Version, "") // if !uniqueSoftware { // // 网元软件包操作【%s】失败,网元类型与文件名版本已存在 // msg := i18n.TTemplate(language, "neSoftware.errKeyExists", map[string]any{"name": body.Name}) // c.JSON(200, result.ErrMsg(msg)) // return // } body.CreateBy = ctx.LoginUserToUserName(c) insertId := s.neSoftwareService.Insert(body) if insertId != "" { c.JSON(200, result.Ok(nil)) return } c.JSON(200, result.Err(nil)) } // 网元软件包信息修改 // // PUT / func (s *NeSoftwareController) Edit(c *gin.Context) { language := ctx.AcceptLanguage(c) var body model.NeSoftware err := c.ShouldBindBodyWith(&body, binding.JSON) if err != nil || body.Path == "" || body.ID == "" { c.JSON(400, result.CodeMsg(400, i18n.TKey(language, "app.common.err400"))) return } // 检查属性值唯一 uniqueSoftware := s.neSoftwareService.CheckUniqueTypeAndNameAndVersion(body.NeType, body.Name, body.Version, body.ID) if !uniqueSoftware { // 网元软件包操作【%s】失败,网元类型与文件名版本已存在 msg := i18n.TTemplate(language, "neSoftware.errKeyExists", map[string]any{"name": body.Name}) c.JSON(200, result.ErrMsg(msg)) return } // 检查是否存在 neSoftware := s.neSoftwareService.SelectById(body.ID) if neSoftware.ID != body.ID { // 没有可访问网元包信息数据! c.JSON(200, result.ErrMsg(i18n.TKey(language, "neSoftware.noData"))) return } body.UpdateBy = ctx.LoginUserToUserName(c) rows := s.neSoftwareService.Update(body) if rows > 0 { c.JSON(200, result.Ok(nil)) return } c.JSON(200, result.Err(nil)) } // 网元软件包信息删除 // // DELETE /:softwareIds func (s *NeSoftwareController) Remove(c *gin.Context) { language := ctx.AcceptLanguage(c) softwareIds := c.Param("softwareIds") if softwareIds == "" { c.JSON(400, result.CodeMsg(400, i18n.TKey(language, "app.common.err400"))) return } // 处理字符转id数组后去重 ids := strings.Split(softwareIds, ",") uniqueIDs := parse.RemoveDuplicates(ids) if len(uniqueIDs) <= 0 { c.JSON(200, result.Err(nil)) return } rows, err := s.neSoftwareService.DeleteByIds(uniqueIDs) if err != nil { c.JSON(200, result.ErrMsg(i18n.TKey(language, err.Error()))) return } msg := i18n.TTemplate(language, "app.common.deleteSuccess", map[string]any{"num": rows}) c.JSON(200, result.OkMsg(msg)) } // 网元软件包安装 // // POST /install func (s *NeSoftwareController) Install(c *gin.Context) { language := ctx.AcceptLanguage(c) var body struct { Software model.NeSoftware `json:"software" binding:"required"` // 软件包信息 Preinput map[string]string `json:"preinput" binding:"required"` // 预先输入参数 } err := c.ShouldBindBodyWith(&body, binding.JSON) if err != nil || body.Software.NeId == "" { c.JSON(400, result.CodeMsg(400, i18n.TKey(language, "app.common.err400"))) return } // 检查是否存在软件包记录 neSoftwares := s.neSoftwareService.SelectList(model.NeSoftware{ NeType: body.Software.NeType, Name: body.Software.Name, Version: body.Software.Version, }) if len(neSoftwares) <= 0 { body.Software.CreateBy = ctx.LoginUserToUserName(c) body.Software.ID = s.neSoftwareService.Insert(body.Software) } else { neSoftware := neSoftwares[0] neSoftware.Path = body.Software.Path neSoftware.Description = body.Software.Description neSoftware.UpdateBy = ctx.LoginUserToUserName(c) s.neSoftwareService.Update(neSoftware) } // 进行安装 output, err := s.neSoftwareService.InstallToNeHost(body.Software, body.Preinput) if err != nil { c.JSON(200, result.ErrMsg(i18n.TKey(language, err.Error()))) return } c.JSON(200, result.OkData(output)) }