146 lines
3.8 KiB
Go
146 lines
3.8 KiB
Go
package utils
|
||
|
||
import (
|
||
"archive/zip"
|
||
"encoding/json"
|
||
"fmt"
|
||
"io"
|
||
"omc/handle/model"
|
||
"os"
|
||
"path"
|
||
"path/filepath"
|
||
"strings"
|
||
"time"
|
||
|
||
"github.com/aceld/zinx/zlog"
|
||
)
|
||
|
||
//BJ/HX/RJ/OMC/FM/告警文件生成时间
|
||
// /FTP根目录/省份简称/专业简称/厂家编码/OMC名称/数据类别/日期或时间/
|
||
//<省份简称>-<数据类别>-<网元类型>[-网元子类]-<主机编号>-<数据版本>-<数据时间>[-登录用户名][-同步请求标识][-Ri][-统计周期] [-序列号].<后缀>
|
||
// BJ-FM-OMC-主机编码-v0-告警文件生成时间-001.txt
|
||
|
||
type FileNameMeta struct {
|
||
FTPRoot string `json:"ftp_root"` // FTP根目录
|
||
Province string `json:"province"` // 网元所在省份
|
||
DeviceCode string `json:"device_code"` // 主机编码 四位,每1位可用0-9、A-Z编码
|
||
ReqId string `json:"req_id"` // 同步请求标识
|
||
Suffix string `json:"suffix"` // 文件后缀
|
||
}
|
||
|
||
// 生成告警文件文件名
|
||
// GD-FM-OMC-100A-V1.1.0-20150611011603-001.txt.zip
|
||
func FileName(meta *FileNameMeta) string {
|
||
// 路径分层
|
||
timeDayStr := time.Now().Format("20060102")
|
||
filePath := path.Join(meta.FTPRoot, meta.Province, "HX", "RJ", "OMC", "FM", timeDayStr)
|
||
|
||
// 文件名组合
|
||
reqIdStr := fmt.Sprintf("%03s", meta.ReqId)
|
||
timeStr := time.Now().Format("20060102150405")
|
||
fileNameGroup := []string{meta.Province, "FM", "OMC", meta.DeviceCode, "V0", timeStr, reqIdStr}
|
||
fileName := strings.Join(fileNameGroup, "-")
|
||
fileName = strings.ToUpper(fileName) + meta.Suffix
|
||
return path.Join(filePath, fileName)
|
||
}
|
||
|
||
func CreateFile(filePath string, data []model.OmcAlarm) error {
|
||
// 获取文件所在的目录路径
|
||
dirPath := filepath.Dir(filePath)
|
||
|
||
// 确保文件夹路径存在
|
||
err := os.MkdirAll(dirPath, os.ModePerm)
|
||
if err != nil {
|
||
zlog.Ins().ErrorF("创建文件夹失败 CreateFile %v", err)
|
||
}
|
||
|
||
// 创建或打开文件
|
||
file, err := os.Create(filePath)
|
||
if err != nil {
|
||
return err
|
||
}
|
||
defer file.Close()
|
||
|
||
// 遍历切片并将每个元素序列化为 JSON 字符串并写入文件
|
||
for _, alarm := range data {
|
||
jsonData, err := json.Marshal(alarm)
|
||
if err != nil {
|
||
return err
|
||
}
|
||
|
||
// 写入 JSON 字符串到文件,并换行
|
||
_, err = file.WriteString(string(jsonData) + "\r\n")
|
||
if err != nil {
|
||
return err
|
||
}
|
||
}
|
||
return nil
|
||
}
|
||
|
||
// 将文件添加到 ZIP 压缩文件
|
||
func FileToZip(zipFileName, filePath string) error {
|
||
// 创建一个新的 ZIP 文件
|
||
newZipFile, err := os.Create(zipFileName)
|
||
if err != nil {
|
||
return fmt.Errorf("创建 ZIP 文件失败: %w", err)
|
||
}
|
||
defer newZipFile.Close()
|
||
|
||
// 创建 ZIP 写入器
|
||
zipWriter := zip.NewWriter(newZipFile)
|
||
defer zipWriter.Close()
|
||
|
||
fileToCompress, err := os.Open(filePath)
|
||
if err != nil {
|
||
return fmt.Errorf("打开文件失败: %w", err)
|
||
}
|
||
defer fileToCompress.Close()
|
||
|
||
// 获取文件信息
|
||
fileInfo, err := fileToCompress.Stat()
|
||
if err != nil {
|
||
return fmt.Errorf("获取文件信息失败: %w", err)
|
||
}
|
||
|
||
// 创建文件头
|
||
header, err := zip.FileInfoHeader(fileInfo)
|
||
if err != nil {
|
||
return fmt.Errorf("创建文件头失败: %w", err)
|
||
}
|
||
|
||
// 设置文件头中的名称
|
||
header.Name = fileInfo.Name()
|
||
|
||
// 创建文件在 ZIP 中的写入器
|
||
writer, err := zipWriter.CreateHeader(header)
|
||
if err != nil {
|
||
return fmt.Errorf("创建文件写入器失败: %w", err)
|
||
}
|
||
|
||
// 将文件内容复制到 ZIP 文件中
|
||
_, err = io.Copy(writer, fileToCompress)
|
||
if err != nil {
|
||
return fmt.Errorf("将文件内容复制到 ZIP 失败: %w", err)
|
||
}
|
||
|
||
return nil
|
||
}
|
||
|
||
// 文件同级进行zip压缩
|
||
func FileToCompress(filePath string, data []model.OmcAlarm, delOrgFile bool) (string, error) {
|
||
defer os.Remove(filePath)
|
||
// 导出文件
|
||
err := CreateFile(filePath, data)
|
||
if err != nil {
|
||
return "", err
|
||
}
|
||
|
||
// 压缩文件
|
||
zipFfileNmae := filePath + ".zip"
|
||
err = FileToZip(zipFfileNmae, filePath)
|
||
if err != nil {
|
||
return "", err
|
||
}
|
||
return zipFfileNmae, nil
|
||
}
|