feat: 更新多个模块以支持新的数据结构和日志格式
This commit is contained in:
@@ -1,297 +1,148 @@
|
||||
package repository
|
||||
|
||||
import (
|
||||
"strings"
|
||||
"time"
|
||||
|
||||
"be.ems/src/framework/datasource"
|
||||
"be.ems/src/framework/database/db"
|
||||
"be.ems/src/framework/logger"
|
||||
"be.ems/src/framework/utils/parse"
|
||||
"be.ems/src/framework/utils/repo"
|
||||
"be.ems/src/modules/network_element/model"
|
||||
)
|
||||
|
||||
// 实例化数据层 NeLicense 结构体
|
||||
var NewNeLicense = &NeLicense{
|
||||
selectSql: `select
|
||||
id, ne_type, ne_id, activation_request_code, license_path, serial_num, expiry_date, status, remark, create_by, create_time, update_by, update_time
|
||||
from ne_license`,
|
||||
|
||||
resultMap: map[string]string{
|
||||
"id": "ID",
|
||||
"ne_type": "NeType",
|
||||
"ne_id": "NeId",
|
||||
"activation_request_code": "ActivationRequestCode",
|
||||
"license_path": "LicensePath",
|
||||
"serial_num": "SerialNum",
|
||||
"expiry_date": "ExpiryDate",
|
||||
"status": "Status",
|
||||
"remark": "Remark",
|
||||
"create_by": "CreateBy",
|
||||
"create_time": "CreateTime",
|
||||
"update_by": "UpdateBy",
|
||||
"update_time": "UpdateTime",
|
||||
},
|
||||
}
|
||||
var NewNeLicense = &NeLicense{}
|
||||
|
||||
// NeLicense 网元授权激活信息 数据层处理
|
||||
type NeLicense struct {
|
||||
// 查询视图对象SQL
|
||||
selectSql string
|
||||
// 结果字段与实体映射
|
||||
resultMap map[string]string
|
||||
}
|
||||
type NeLicense struct{}
|
||||
|
||||
// convertResultRows 将结果记录转实体结果组
|
||||
func (r *NeLicense) convertResultRows(rows []map[string]any) []model.NeLicense {
|
||||
arr := make([]model.NeLicense, 0)
|
||||
for _, row := range rows {
|
||||
item := model.NeLicense{}
|
||||
for key, value := range row {
|
||||
if keyMapper, ok := r.resultMap[key]; ok {
|
||||
repo.SetFieldValue(&item, keyMapper, value)
|
||||
}
|
||||
}
|
||||
arr = append(arr, item)
|
||||
}
|
||||
return arr
|
||||
}
|
||||
|
||||
// SelectPage 根据条件分页查询字典类型
|
||||
func (r *NeLicense) SelectPage(query map[string]any) map[string]any {
|
||||
// SelectByPage 分页查询集合
|
||||
func (r NeLicense) SelectByPage(query map[string]string) ([]model.NeLicense, int64) {
|
||||
tx := db.DB("").Model(&model.NeLicense{})
|
||||
// 查询条件拼接
|
||||
var conditions []string
|
||||
var params []any
|
||||
if v, ok := query["neType"]; ok && v != "" {
|
||||
conditions = append(conditions, "ne_type = ?")
|
||||
params = append(params, strings.Trim(v.(string), " "))
|
||||
tx = tx.Where("ne_type = ?", v)
|
||||
}
|
||||
if v, ok := query["neId"]; ok && v != "" {
|
||||
conditions = append(conditions, "ne_id = ?")
|
||||
params = append(params, strings.Trim(v.(string), " "))
|
||||
tx = tx.Where("ne_id = ?", v)
|
||||
}
|
||||
if v, ok := query["expiryDate"]; ok && v != "" {
|
||||
conditions = append(conditions, "expiry_date = ?")
|
||||
params = append(params, strings.Trim(v.(string), " "))
|
||||
tx = tx.Where("expiry_date = ?", v)
|
||||
}
|
||||
if v, ok := query["serialNum"]; ok && v != "" {
|
||||
tx = tx.Where("serial_num = ?", v)
|
||||
}
|
||||
if v, ok := query["createBy"]; ok && v != "" {
|
||||
conditions = append(conditions, "create_by like concat(?, '%')")
|
||||
params = append(params, strings.Trim(v.(string), " "))
|
||||
tx = tx.Where("create_by like concat(?, '%')", v)
|
||||
}
|
||||
|
||||
// 构建查询条件语句
|
||||
whereSql := ""
|
||||
if len(conditions) > 0 {
|
||||
whereSql += " where " + strings.Join(conditions, " and ")
|
||||
// 查询结果
|
||||
var total int64 = 0
|
||||
rows := []model.NeLicense{}
|
||||
|
||||
// 查询数量为0直接返回
|
||||
if err := tx.Count(&total).Error; err != nil || total <= 0 {
|
||||
return rows, total
|
||||
}
|
||||
|
||||
result := map[string]any{
|
||||
"total": 0,
|
||||
"rows": []model.NeHost{},
|
||||
}
|
||||
|
||||
// 查询数量 长度为0直接返回
|
||||
totalSql := "select count(1) as 'total' from ne_license"
|
||||
totalRows, err := datasource.RawDB("", totalSql+whereSql, params)
|
||||
// 查询数据分页
|
||||
pageNum, pageSize := db.PageNumSize(query["pageNum"], query["pageSize"])
|
||||
tx = tx.Limit(pageSize).Offset(pageSize * pageNum)
|
||||
tx = tx.Order("id desc")
|
||||
err := tx.Find(&rows).Error
|
||||
if err != nil {
|
||||
logger.Errorf("total err => %v", err)
|
||||
return result
|
||||
logger.Errorf("query find err => %v", err.Error())
|
||||
return rows, total
|
||||
}
|
||||
total := parse.Number(totalRows[0]["total"])
|
||||
if total == 0 {
|
||||
return result
|
||||
} else {
|
||||
result["total"] = total
|
||||
}
|
||||
|
||||
// 分页
|
||||
pageNum, pageSize := repo.PageNumSize(query["pageNum"], query["pageSize"])
|
||||
pageSql := " limit ?,? "
|
||||
params = append(params, pageNum*pageSize)
|
||||
params = append(params, pageSize)
|
||||
|
||||
// 查询数据
|
||||
querySql := r.selectSql + whereSql + pageSql
|
||||
results, err := datasource.RawDB("", querySql, params)
|
||||
if err != nil {
|
||||
logger.Errorf("query err => %v", err)
|
||||
return result
|
||||
}
|
||||
|
||||
// 转换实体
|
||||
result["rows"] = r.convertResultRows(results)
|
||||
return result
|
||||
return rows, total
|
||||
}
|
||||
|
||||
// SelectList 根据实体查询
|
||||
func (r *NeLicense) SelectList(neLicense model.NeLicense) []model.NeLicense {
|
||||
// Select 查询集合
|
||||
func (r NeLicense) Select(param model.NeLicense) []model.NeLicense {
|
||||
tx := db.DB("").Model(&model.NeLicense{})
|
||||
// 查询条件拼接
|
||||
var conditions []string
|
||||
var params []any
|
||||
if neLicense.NeType != "" {
|
||||
conditions = append(conditions, "ne_type = ?")
|
||||
params = append(params, neLicense.NeType)
|
||||
if param.NeType != "" {
|
||||
tx = tx.Where("ne_type = ?", param.NeType)
|
||||
}
|
||||
if neLicense.NeId != "" {
|
||||
conditions = append(conditions, "ne_id = ?")
|
||||
params = append(params, neLicense.NeId)
|
||||
if param.NeId != "" {
|
||||
tx = tx.Where("ne_id = ?", param.NeId)
|
||||
}
|
||||
if neLicense.ExpiryDate != "" {
|
||||
conditions = append(conditions, "expiry_date = ?")
|
||||
params = append(params, neLicense.ExpiryDate)
|
||||
if param.ExpiryDate != "" {
|
||||
tx = tx.Where("expiry_date = ?", param.ExpiryDate)
|
||||
}
|
||||
if neLicense.CreateBy != "" {
|
||||
conditions = append(conditions, "create_by like concat(?, '%')")
|
||||
params = append(params, neLicense.CreateBy)
|
||||
}
|
||||
|
||||
// 构建查询条件语句
|
||||
whereSql := ""
|
||||
if len(conditions) > 0 {
|
||||
whereSql += " where " + strings.Join(conditions, " and ")
|
||||
if param.CreateBy != "" {
|
||||
tx = tx.Where("create_by like concat(?, '%')", param.CreateBy)
|
||||
}
|
||||
|
||||
// 查询数据
|
||||
querySql := r.selectSql + whereSql + " order by id asc "
|
||||
results, err := datasource.RawDB("", querySql, params)
|
||||
if err != nil {
|
||||
logger.Errorf("query err => %v", err)
|
||||
}
|
||||
|
||||
// 转换实体
|
||||
return r.convertResultRows(results)
|
||||
}
|
||||
|
||||
// SelectByIds 通过ID查询
|
||||
func (r *NeLicense) SelectByIds(cmdIds []string) []model.NeLicense {
|
||||
placeholder := repo.KeyPlaceholderByQuery(len(cmdIds))
|
||||
querySql := r.selectSql + " where id in (" + placeholder + ")"
|
||||
parameters := repo.ConvertIdsSlice(cmdIds)
|
||||
results, err := datasource.RawDB("", querySql, parameters)
|
||||
if err != nil {
|
||||
logger.Errorf("query err => %v", err)
|
||||
return []model.NeLicense{}
|
||||
}
|
||||
// 转换实体
|
||||
return r.convertResultRows(results)
|
||||
}
|
||||
|
||||
// Insert 新增信息
|
||||
func (r *NeLicense) Insert(neLicense model.NeLicense) string {
|
||||
// 参数拼接
|
||||
params := make(map[string]any)
|
||||
if neLicense.NeType != "" {
|
||||
params["ne_type"] = neLicense.NeType
|
||||
}
|
||||
if neLicense.NeId != "" {
|
||||
params["ne_id"] = neLicense.NeId
|
||||
}
|
||||
if neLicense.ActivationRequestCode != "" {
|
||||
params["activation_request_code"] = neLicense.ActivationRequestCode
|
||||
}
|
||||
if neLicense.LicensePath != "" {
|
||||
params["license_path"] = neLicense.LicensePath
|
||||
}
|
||||
if neLicense.SerialNum != "" {
|
||||
params["serial_num"] = neLicense.SerialNum
|
||||
}
|
||||
if neLicense.ExpiryDate != "" {
|
||||
params["expiry_date"] = neLicense.ExpiryDate
|
||||
}
|
||||
if neLicense.Status != "" {
|
||||
params["status"] = neLicense.Status
|
||||
}
|
||||
if neLicense.Remark != "" {
|
||||
params["remark"] = neLicense.Remark
|
||||
}
|
||||
if neLicense.CreateBy != "" {
|
||||
params["create_by"] = neLicense.CreateBy
|
||||
params["create_time"] = time.Now().UnixMilli()
|
||||
}
|
||||
|
||||
// 构建执行语句
|
||||
keys, placeholder, values := repo.KeyPlaceholderValueByInsert(params)
|
||||
sql := "insert into ne_license (" + strings.Join(keys, ",") + ")values(" + placeholder + ")"
|
||||
|
||||
db := datasource.DefaultDB()
|
||||
// 开启事务
|
||||
tx := db.Begin()
|
||||
// 执行插入
|
||||
err := tx.Exec(sql, values...).Error
|
||||
if err != nil {
|
||||
logger.Errorf("insert row : %v", err.Error())
|
||||
tx.Rollback()
|
||||
return ""
|
||||
}
|
||||
// 获取生成的自增 ID
|
||||
var insertedID string
|
||||
err = tx.Raw("select last_insert_id()").Row().Scan(&insertedID)
|
||||
if err != nil {
|
||||
logger.Errorf("insert last id : %v", err.Error())
|
||||
tx.Rollback()
|
||||
return ""
|
||||
}
|
||||
// 提交事务
|
||||
tx.Commit()
|
||||
return insertedID
|
||||
}
|
||||
|
||||
// Update 修改信息
|
||||
func (r *NeLicense) Update(neLicense model.NeLicense) int64 {
|
||||
// 参数拼接
|
||||
params := make(map[string]any)
|
||||
if neLicense.NeType != "" {
|
||||
params["ne_type"] = neLicense.NeType
|
||||
}
|
||||
if neLicense.NeId != "" {
|
||||
params["ne_id"] = neLicense.NeId
|
||||
}
|
||||
if neLicense.ActivationRequestCode != "" {
|
||||
params["activation_request_code"] = neLicense.ActivationRequestCode
|
||||
}
|
||||
if neLicense.LicensePath != "" {
|
||||
params["license_path"] = neLicense.LicensePath
|
||||
}
|
||||
if neLicense.SerialNum != "" {
|
||||
params["serial_num"] = neLicense.SerialNum
|
||||
}
|
||||
if neLicense.ExpiryDate != "" {
|
||||
params["expiry_date"] = neLicense.ExpiryDate
|
||||
}
|
||||
if neLicense.Status != "" {
|
||||
params["status"] = neLicense.Status
|
||||
}
|
||||
if neLicense.Remark != "" {
|
||||
params["remark"] = neLicense.Remark
|
||||
}
|
||||
if neLicense.UpdateBy != "" {
|
||||
params["update_by"] = neLicense.UpdateBy
|
||||
params["update_time"] = time.Now().UnixMilli()
|
||||
}
|
||||
|
||||
// 构建执行语句
|
||||
keys, values := repo.KeyValueByUpdate(params)
|
||||
sql := "update ne_license set " + strings.Join(keys, ",") + " where id = ?"
|
||||
|
||||
// 执行更新
|
||||
values = append(values, neLicense.ID)
|
||||
rows, err := datasource.ExecDB("", sql, values)
|
||||
if err != nil {
|
||||
logger.Errorf("update row : %v", err.Error())
|
||||
return 0
|
||||
rows := []model.NeLicense{}
|
||||
if err := tx.Find(&rows).Error; err != nil {
|
||||
logger.Errorf("query find err => %v", err.Error())
|
||||
return rows
|
||||
}
|
||||
return rows
|
||||
}
|
||||
|
||||
// DeleteByIds 批量删除信息
|
||||
func (r *NeLicense) DeleteByIds(cmdIds []string) int64 {
|
||||
placeholder := repo.KeyPlaceholderByQuery(len(cmdIds))
|
||||
sql := "delete from ne_license where id in (" + placeholder + ")"
|
||||
parameters := repo.ConvertIdsSlice(cmdIds)
|
||||
results, err := datasource.ExecDB("", sql, parameters)
|
||||
if err != nil {
|
||||
logger.Errorf("delete err => %v", err)
|
||||
// SelectByIds 通过ID查询
|
||||
func (r NeLicense) SelectByIds(ids []int64) []model.NeLicense {
|
||||
rows := []model.NeLicense{}
|
||||
if len(ids) <= 0 {
|
||||
return rows
|
||||
}
|
||||
tx := db.DB("").Model(&model.NeLicense{})
|
||||
// 构建查询条件
|
||||
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 NeLicense) Insert(neInfo model.NeLicense) int64 {
|
||||
if neInfo.CreateBy != "" {
|
||||
ms := time.Now().UnixMilli()
|
||||
neInfo.CreateTime = ms
|
||||
neInfo.UpdateTime = ms
|
||||
neInfo.UpdateBy = neInfo.CreateBy
|
||||
}
|
||||
tx := db.DB("").Create(&neInfo)
|
||||
if err := tx.Error; err != nil {
|
||||
logger.Errorf("CreateInBatches err => %v", err)
|
||||
}
|
||||
return neInfo.ID
|
||||
}
|
||||
|
||||
// Update 修改信息
|
||||
func (r NeLicense) Update(param model.NeLicense) int64 {
|
||||
if param.ID <= 0 {
|
||||
return 0
|
||||
}
|
||||
return results
|
||||
if param.UpdateBy != "" {
|
||||
param.UpdateTime = time.Now().UnixMilli()
|
||||
}
|
||||
param.UpdateTime = time.Now().UnixMilli()
|
||||
tx := db.DB("").Model(&model.NeLicense{})
|
||||
// 构建查询条件
|
||||
tx = tx.Where("id = ?", param.ID)
|
||||
tx = tx.Omit("id", "create_by", "create_time")
|
||||
// 执行更新
|
||||
if err := tx.Updates(param).Error; err != nil {
|
||||
logger.Errorf("update err => %v", err.Error())
|
||||
return 0
|
||||
}
|
||||
return tx.RowsAffected
|
||||
}
|
||||
|
||||
// DeleteByIds 批量删除信息
|
||||
func (r NeLicense) DeleteByIds(ids []int64) int64 {
|
||||
if len(ids) <= 0 {
|
||||
return 0
|
||||
}
|
||||
tx := db.DB("").Where("id in ?", ids)
|
||||
if err := tx.Delete(&model.NeLicense{}).Error; err != nil {
|
||||
logger.Errorf("delete err => %v", err.Error())
|
||||
return 0
|
||||
}
|
||||
return tx.RowsAffected
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user