diff --git a/build/database/std/install/core_info.sql b/build/database/std/install/core_info.sql index 4cdf5f85..32546d8f 100644 --- a/build/database/std/install/core_info.sql +++ b/build/database/std/install/core_info.sql @@ -5,16 +5,20 @@ DROP TABLE IF EXISTS `core_info`; CREATE TABLE `core_info` ( `id` bigint NOT NULL AUTO_INCREMENT COMMENT '核心网ID', `core_uid` varchar(64) CHARACTER SET utf8mb4 COLLATE utf8mb4_general_ci NOT NULL COMMENT '核心网唯一标识', - `core_name` varchar(64) CHARACTER SET utf8mb4 COLLATE utf8mb4_general_ci NOT NULL COMMENT '核心网名称', - `core_sn` varchar(16) CHARACTER SET utf8mb4 COLLATE utf8mb4_general_ci NOT NULL COMMENT '核心网序列号', + `name` varchar(64) CHARACTER SET utf8mb4 COLLATE utf8mb4_general_ci NOT NULL COMMENT '核心网名称', + `sn` varchar(16) CHARACTER SET utf8mb4 COLLATE utf8mb4_general_ci NOT NULL COMMENT '核心网序列号', `omc_id` varchar(128) CHARACTER SET utf8mb4 COLLATE utf8mb4_general_ci NOT NULL COMMENT 'OMC安装生成的唯一编码', + `time_zone` varchar(64) CHARACTER SET utf8mb4 COLLATE utf8mb4_general_ci DEFAULT '' COMMENT '时区 Asia/Hong_Kong', + `longitude` float DEFAULT '0' COMMENT '经度 -180 to 180', + `latitude` float DEFAULT '0' COMMENT '纬度 -90 to 90', + `address` varchar(255) CHARACTER SET utf8mb4 COLLATE utf8mb4_general_ci DEFAULT '' COMMENT '地址', `remark` varchar(255) CHARACTER SET utf8mb4 COLLATE utf8mb4_general_ci DEFAULT '' COMMENT '备注', `create_by` varchar(50) CHARACTER SET utf8mb4 COLLATE utf8mb4_general_ci DEFAULT '' COMMENT '创建者', `create_time` bigint DEFAULT '0' COMMENT '创建时间', `update_by` varchar(50) CHARACTER SET utf8mb4 COLLATE utf8mb4_general_ci DEFAULT '' COMMENT '更新者', `update_time` bigint DEFAULT '0' COMMENT '更新时间', PRIMARY KEY (`id`) USING BTREE, - UNIQUE KEY `uk_core_name_sn` (`core_name`,`core_sn`) USING BTREE COMMENT '唯一名称和序号定义' + UNIQUE KEY `uk_core_name_sn` (`name`,`sn`) USING BTREE COMMENT '唯一名称和序号定义' ) ENGINE=InnoDB DEFAULT CHARSET=utf8mb4 COLLATE=utf8mb4_general_ci COMMENT='核心网_基础信息表'; -- Dump completed on 2025-06-04 15:26:56 diff --git a/src/modules/core/controller/core_info.go b/src/modules/core/controller/core_info.go new file mode 100644 index 00000000..d961139c --- /dev/null +++ b/src/modules/core/controller/core_info.go @@ -0,0 +1,150 @@ +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" + "be.ems/src/modules/core/model" + "be.ems/src/modules/core/service" + + "github.com/gin-gonic/gin" +) + +// 实例化控制层 CoreInfoController 结构体 +var NewCoreInfo = &CoreInfoController{ + CoreInfoService: service.NewCoreInfo, +} + +// 核心网信息请求 +// +// PATH /core +type CoreInfoController struct { + CoreInfoService *service.CoreInfo //核心网信息服务 +} + +// 核心网信息列表 +// +// GET /list +func (s CoreInfoController) List(c *gin.Context) { + query := reqctx.QueryMap(c) + rows, total := s.CoreInfoService.FindByPage(query) + c.JSON(200, resp.OkData(map[string]any{"total": total, "rows": rows})) +} + +// 核心网信息信息 +// +// GET /:id +func (s CoreInfoController) Info(c *gin.Context) { + id := parse.Number(c.Param("id")) + if id <= 0 { + c.JSON(422, resp.CodeMsg(resp.CODE_PARAM_CHEACK, "bind err: id is empty")) + return + } + + neHost := s.CoreInfoService.FindById(id) + if neHost.ID != id { + c.JSON(200, resp.ErrMsg("not found data")) + return + } + + c.JSON(200, resp.OkData(neHost)) +} + +// 核心网信息新增 +// +// POST / +func (s CoreInfoController) Add(c *gin.Context) { + var body model.CoreInfo + if err := c.ShouldBindBodyWithJSON(&body); err != nil { + errMsgs := fmt.Sprintf("bind err: %s", resp.FormatBindError(err)) + c.JSON(422, resp.CodeMsg(resp.CODE_PARAM_PARSER, errMsgs)) + return + } + + // 检查属性值唯一 + uniqueInfo := s.CoreInfoService.CheckUniqueGroupTitle(body.Name, body.SN, 0) + if !uniqueInfo { + c.JSON(200, resp.ErrMsg("name/SN already exists")) + return + } + + body.CreateBy = reqctx.LoginUserToUserName(c) + insertId := s.CoreInfoService.Insert(body) + if insertId > 0 { + c.JSON(200, resp.Ok(nil)) + return + } + c.JSON(200, resp.Err(nil)) +} + +// 核心网信息修改 +// +// PUT / +func (s CoreInfoController) Edit(c *gin.Context) { + var body model.CoreInfo + if err := c.ShouldBindBodyWithJSON(&body); err != nil { + errMsgs := fmt.Sprintf("bind err: %s", resp.FormatBindError(err)) + c.JSON(422, resp.CodeMsg(resp.CODE_PARAM_PARSER, errMsgs)) + return + } + + // 检查属性值唯一 + uniqueHostCmd := s.CoreInfoService.CheckUniqueGroupTitle(body.Name, body.SN, body.ID) + if !uniqueHostCmd { + c.JSON(200, resp.ErrMsg("name/SN already exists")) + return + } + + // 检查是否存在 + coreInfo := s.CoreInfoService.FindById(body.ID) + if coreInfo.ID != body.ID { + c.JSON(200, resp.ErrMsg("not found data")) + return + } + + coreInfo.Name = body.Name + coreInfo.SN = body.SN + coreInfo.TimeZone = body.TimeZone + coreInfo.Longitude = body.Longitude + coreInfo.Latitude = body.Latitude + coreInfo.Address = body.Address + coreInfo.Remark = body.Remark + body.UpdateBy = reqctx.LoginUserToUserName(c) + rows := s.CoreInfoService.Update(coreInfo) + if rows > 0 { + c.JSON(200, resp.Ok(nil)) + return + } + c.JSON(200, resp.Err(nil)) +} + +// 核心网信息删除 +// +// DELETE /:id +func (s CoreInfoController) Remove(c *gin.Context) { + language := reqctx.AcceptLanguage(c) + id := c.Param("id") + if id == "" { + c.JSON(422, resp.CodeMsg(resp.CODE_PARAM_CHEACK, "bind err: id is empty")) + return + } + + // 处理字符转id数组后去重 + uniqueIDs := parse.RemoveDuplicatesToArray(id, ",") + // 转换成int64数组类型 + ids := make([]int64, 0) + for _, v := range uniqueIDs { + ids = append(ids, parse.Number(v)) + } + + rows, err := s.CoreInfoService.DeleteByIds(ids) + if err != nil { + c.JSON(200, resp.ErrMsg(i18n.TKey(language, err.Error()))) + return + } + msg := i18n.TTemplate(language, "app.common.deleteSuccess", map[string]any{"num": rows}) + c.JSON(200, resp.OkMsg(msg)) +} diff --git a/src/modules/core/core.go b/src/modules/core/core.go new file mode 100644 index 00000000..10a74e1e --- /dev/null +++ b/src/modules/core/core.go @@ -0,0 +1,47 @@ +package core + +import ( + "be.ems/src/framework/logger" + "be.ems/src/framework/middleware" + "be.ems/src/framework/middleware/collectlogs" + "be.ems/src/modules/core/controller" + + "github.com/gin-gonic/gin" +) + +// 模块路由注册 +func Setup(router *gin.Engine) { + logger.Infof("开始加载 ====> core 模块路由") + + // 核心网信息 + coreInfoGroup := router.Group("/core/info") + { + coreInfoGroup.GET("/list", + middleware.AuthorizeUser(nil), + controller.NewCoreInfo.List, + ) + coreInfoGroup.GET("/:id", + middleware.CryptoApi(false, true), + middleware.AuthorizeUser(nil), + controller.NewCoreInfo.Info, + ) + coreInfoGroup.POST("", + middleware.CryptoApi(true, true), + middleware.AuthorizeUser(nil), + collectlogs.OperateLog(collectlogs.OptionNew("log.operate.title.coreInfo", collectlogs.BUSINESS_TYPE_INSERT)), + controller.NewCoreInfo.Add, + ) + coreInfoGroup.PUT("", + middleware.CryptoApi(true, true), + middleware.AuthorizeUser(nil), + collectlogs.OperateLog(collectlogs.OptionNew("log.operate.title.coreInfo", collectlogs.BUSINESS_TYPE_UPDATE)), + controller.NewCoreInfo.Edit, + ) + coreInfoGroup.DELETE(":id", + middleware.AuthorizeUser(nil), + collectlogs.OperateLog(collectlogs.OptionNew("log.operate.title.coreInfo", collectlogs.BUSINESS_TYPE_DELETE)), + controller.NewCoreInfo.Remove, + ) + } + +} diff --git a/src/modules/core/model/core_info.go b/src/modules/core/model/core_info.go new file mode 100644 index 00000000..b1d5501b --- /dev/null +++ b/src/modules/core/model/core_info.go @@ -0,0 +1,24 @@ +package model + +// CoreInfo 核心网_基础信息 core_info +type CoreInfo struct { + ID int64 `json:"id" gorm:"column:id;primaryKey;autoIncrement"` // 核心网ID + CoreUID string `json:"coreUid" gorm:"column:core_uid"` // 核心网唯一标识 + Name string `json:"name" gorm:"column:name"` // 核心网名称 + SN string `json:"sn" gorm:"column:sn"` // 核心网序列号 + OmcId string `json:"omcId" gorm:"column:omc_id"` // OMC安装生成的唯一编码 + TimeZone string `json:"timeZone" gorm:"column:time_zone"` // 时区 Asia/Hong_Kong + Longitude float64 `json:"longitude" gorm:"column:longitude"` // 经度 -180 to 180 + Latitude float64 `json:"latitude" gorm:"column:latitude"` // 纬度 -90 to 90 + Address string `json:"address" gorm:"column:address"` // 地址 + Remark string `json:"remark" gorm:"column:remark"` // 备注 + CreateBy string `json:"createBy" gorm:"column:create_by"` // 创建者 + CreateTime int64 `json:"createTime" gorm:"column:create_time"` // 创建时间 + UpdateBy string `json:"updateBy" gorm:"column:update_by"` // 更新者 + UpdateTime int64 `json:"updateTime" gorm:"column:update_time"` // 更新时间 +} + +// TableName 表名称 +func (*CoreInfo) TableName() string { + return "core_info" +} diff --git a/src/modules/core/repository/core_info.go b/src/modules/core/repository/core_info.go new file mode 100644 index 00000000..0ea5f5af --- /dev/null +++ b/src/modules/core/repository/core_info.go @@ -0,0 +1,134 @@ +package repository + +import ( + "fmt" + "time" + + "be.ems/src/framework/database/db" + "be.ems/src/framework/logger" + "be.ems/src/modules/core/model" +) + +// 实例化数据层 CoreInfo 结构体 +var NewCoreInfo = &CoreInfo{} + +// CoreInfo 核心网信息 数据层处理 +type CoreInfo struct{} + +// SelectByPage 分页查询集合 +func (r CoreInfo) SelectByPage(query map[string]string) ([]model.CoreInfo, int64) { + tx := db.DB("").Model(&model.CoreInfo{}) + // 查询条件拼接 + if v, ok := query["coreUid"]; ok && v != "" { + tx = tx.Where("core_uid = ?", v) + } + if v, ok := query["sn"]; ok && v != "" { + tx = tx.Where("sn = ?", v) + } + if v, ok := query["name"]; ok && v != "" { + tx = tx.Where("name like ?", fmt.Sprintf("%s%%", v)) + } + + // 查询结果 + var total int64 = 0 + rows := []model.CoreInfo{} + + // 查询数量为0直接返回 + if err := tx.Count(&total).Error; err != nil || total <= 0 { + return rows, total + } + + // 查询数据分页 + pageNum, pageSize := db.PageNumSize(query["pageNum"], query["pageSize"]) + tx = tx.Limit(pageSize).Offset(pageSize * pageNum) + err := tx.Find(&rows).Error + if err != nil { + logger.Errorf("query find err => %v", err.Error()) + return rows, total + } + return rows, total +} + +// SelectByIds 通过ID查询 +func (r CoreInfo) SelectByIds(ids []int64) []model.CoreInfo { + rows := []model.CoreInfo{} + if len(ids) <= 0 { + return rows + } + tx := db.DB("").Model(&model.CoreInfo{}) + // 构建查询条件 + tx = tx.Where("id in ?", ids) + // 查询数据 + if err := tx.Find(&rows).Error; err != nil { + logger.Errorf("query find err => %v", err.Error()) + return rows + } + return rows +} + +// Insert 新增信息 +func (r CoreInfo) Insert(param model.CoreInfo) int64 { + if param.CreateBy != "" { + ms := time.Now().UnixMilli() + param.CreateTime = ms + param.UpdateTime = ms + param.UpdateBy = param.CreateBy + } + // 执行插入 + if err := db.DB("").Create(¶m).Error; err != nil { + logger.Errorf("insert err => %v", err.Error()) + return 0 + } + return param.ID +} + +// Update 修改信息 +func (r CoreInfo) Update(param model.CoreInfo) int64 { + if param.ID == 0 { + return 0 + } + param.UpdateTime = time.Now().UnixMilli() + tx := db.DB("").Model(&model.CoreInfo{}) + // 构建查询条件 + tx = tx.Where("id = ?", param.ID) + tx = tx.Omit("id") + // 执行更新 + if err := tx.Updates(param).Error; err != nil { + logger.Errorf("update err => %v", err.Error()) + return 0 + } + return tx.RowsAffected +} + +// DeleteByIds 批量删除信息 +func (r CoreInfo) DeleteByIds(ids []int64) int64 { + if len(ids) <= 0 { + return 0 + } + tx := db.DB("").Where("id in ?", ids) + if err := tx.Delete(&model.CoreInfo{}).Error; err != nil { + logger.Errorf("delete err => %v", err.Error()) + return 0 + } + return tx.RowsAffected +} + +// CheckUnique 检查信息是否唯一 返回数据ID +func (r CoreInfo) CheckUnique(param model.CoreInfo) int64 { + tx := db.DB("").Model(&model.CoreInfo{}) + // 查询条件拼接 + if param.Name != "" { + tx = tx.Where("name = ?", param.Name) + } + if param.SN != "" { + tx = tx.Where("sn = ?", param.SN) + } + + // 查询数据 + var id int64 = 0 + if err := tx.Select("id").Limit(1).Find(&id).Error; err != nil { + logger.Errorf("query find err => %v", err.Error()) + return id + } + return id +} diff --git a/src/modules/core/service/core_info.go b/src/modules/core/service/core_info.go new file mode 100644 index 00000000..16d12c83 --- /dev/null +++ b/src/modules/core/service/core_info.go @@ -0,0 +1,81 @@ +package service + +import ( + "fmt" + "strings" + + "be.ems/src/framework/utils/generate" + "be.ems/src/framework/utils/machine" + "be.ems/src/modules/core/model" + "be.ems/src/modules/core/repository" +) + +// 实例化服务层 CoreInfo 结构体 +var NewCoreInfo = &CoreInfo{ + CoreInfoRepository: repository.NewCoreInfo, +} + +// CoreInfo 核心网信息 服务层处理 +type CoreInfo struct { + CoreInfoRepository *repository.CoreInfo // 核心网信息表 +} + +// FindByPage 分页查询列表数据 +func (r CoreInfo) FindByPage(query map[string]string) ([]model.CoreInfo, int64) { + return r.CoreInfoRepository.SelectByPage(query) +} + +// FindById 通过ID查询 +func (r CoreInfo) FindById(id int64) model.CoreInfo { + if id <= 0 { + return model.CoreInfo{} + } + neHosts := r.CoreInfoRepository.SelectByIds([]int64{id}) + if len(neHosts) > 0 { + return neHosts[0] + } + return model.CoreInfo{} +} + +// Insert 新增信息 +func (r CoreInfo) Insert(param model.CoreInfo) int64 { + param.CoreUID = strings.ToUpper(generate.Code(8)) + if param.OmcId == "" { + param.OmcId = machine.Code + } + + return r.CoreInfoRepository.Insert(param) +} + +// Update 修改信息 +func (r CoreInfo) Update(param model.CoreInfo) int64 { + return r.CoreInfoRepository.Update(param) +} + +// DeleteByIds 批量删除信息 +func (r CoreInfo) DeleteByIds(ids []int64) (int64, error) { + // 检查是否存在 + rows := r.CoreInfoRepository.SelectByIds(ids) + if len(rows) <= 0 { + return 0, fmt.Errorf("coreInfo.noData") + } + + if len(rows) == len(ids) { + rows := r.CoreInfoRepository.DeleteByIds(ids) + return rows, nil + } + // 删除信息失败! + return 0, fmt.Errorf("delete fail") +} + +// CheckUniqueGroupTitle 校验唯一名称和序号 +func (r CoreInfo) CheckUniqueGroupTitle(name, sn string, id int64) bool { + uniqueId := r.CoreInfoRepository.CheckUnique(model.CoreInfo{ + Name: name, + SN: sn, + }) + if uniqueId == id { + return true + } + return uniqueId == 0 +}