| 123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459 |
- package mongo
- import (
- "context"
- "fwh-toolbox-server/database"
- "fwh-toolbox-server/domain"
- "fwh-toolbox-server/dto"
- "time"
- "go.mongodb.org/mongo-driver/bson"
- "go.mongodb.org/mongo-driver/bson/primitive"
- "go.mongodb.org/mongo-driver/mongo/options"
- )
- // 从数据库获取批量下载数据列表
- func GetBatchDownloadManageList(page, size uint, sn, progress *string) (*dto.ResPage[domain.BatchDownloadInfo], error) {
- ctx, cancel := context.WithTimeout(context.Background(), 5*time.Second)
- defer cancel()
- collection := database.GetCollection("BatchDownloadConfig")
- filter := bson.M{}
- // 条件
- if sn != nil {
- filter["sn"] = *sn
- }
- if progress != nil {
- filter["progress"] = *progress
- }
- findOptions := options.Find().
- SetProjection(bson.M{
- "_id": 1,
- "sn": 1,
- "description": 1,
- "timePeriod": 1,
- "options": 1,
- "progress": 1,
- "failReasons": 1,
- }).
- SetSkip(int64((page - 1) * size)).
- SetLimit(int64(size)).
- SetSort(bson.D{
- {Key: "_id", Value: -1}, // 倒序
- })
- cursor, err := collection.Find(ctx, filter, findOptions)
- if err != nil {
- return nil, err
- }
- defer cursor.Close(ctx)
- var configList []domain.BatchDownloadInfo
- for cursor.Next(ctx) {
- var cfg domain.BatchDownloadInfo
- if err := cursor.Decode(&cfg); err != nil {
- return nil, err
- }
- configList = append(configList, cfg)
- }
- // 查询总数
- total, err := collection.CountDocuments(ctx, filter)
- if err != nil {
- return nil, err
- }
- res := dto.ResPage[domain.BatchDownloadInfo]{
- Data: configList,
- PageSize: size,
- PageNum: page,
- Total: uint(total),
- }
- return &res, nil
- }
- // 从数据库获取一条批量下载数据配置
- func GetBatchDownloadConfig(id primitive.ObjectID) (*domain.BatchDownloadConfig, error) {
- ctx, cancel := context.WithTimeout(context.Background(), 5*time.Second)
- defer cancel()
- collection := database.GetCollection("BatchDownloadConfig")
- var cfg domain.BatchDownloadConfig
- err := collection.FindOne(ctx, bson.M{"_id": id}).Decode(&cfg)
- if err != nil {
- return nil, err
- }
- return &cfg, nil
- }
- // 插入一条批量下载配置到数据库
- func InsertBatchDownloadConfig(writeConfig domain.BatchDownloadConfigWrite) (primitive.ObjectID, error) {
- ctx, cancel := context.WithTimeout(context.Background(), 5*time.Second)
- defer cancel()
- config := domain.BatchDownloadConfig{
- Sn: writeConfig.Sn,
- Description: writeConfig.Description,
- TimePeriod: writeConfig.TimePeriod,
- Options: writeConfig.Options,
- Progress: domain.ProgressPending,
- Data: map[string]domain.BatchDownloadData{},
- FailReasons: []string{},
- }
- collection := database.GetCollection("BatchDownloadConfig")
- res, err := collection.InsertOne(ctx, config)
- if err != nil {
- return primitive.ObjectID{}, err
- }
- return res.InsertedID.(primitive.ObjectID), nil
- }
- // 更新一条批量下载描述描述到数据库
- func UpdateBatchDownloadDescription(id primitive.ObjectID, desc string) error {
- ctx, cancel := context.WithTimeout(context.Background(), 5*time.Second)
- defer cancel()
- collection := database.GetCollection("BatchDownloadConfig")
- // _, err := collection.InsertOne(ctx, config)
- filter := bson.M{"_id": id} // 按ID匹配
- update := bson.M{
- // 如果存在就更新全部字段
- "$set": bson.M{
- "description": desc,
- },
- }
- opts := options.Update().SetUpsert(true)
- _, err := collection.UpdateOne(ctx, filter, update, opts)
- if err != nil {
- return err
- }
- return nil
- }
- // 更新一条批量下载数据owner到数据库
- func UpdateBatchDownloadOwner(id primitive.ObjectID, owner []string) error {
- ctx, cancel := context.WithTimeout(context.Background(), 5*time.Second)
- defer cancel()
- collection := database.GetCollection("BatchDownloadConfig")
- // _, err := collection.InsertOne(ctx, config)
- filter := bson.M{"_id": id} // 按ID匹配
- update := bson.M{
- // 如果存在就更新全部字段
- "$set": bson.M{
- "owner": owner,
- },
- }
- opts := options.Update().SetUpsert(true)
- _, err := collection.UpdateOne(ctx, filter, update, opts)
- if err != nil {
- return err
- }
- return nil
- }
- // 更新一条批量下载结果到数据库
- func UpdateBatchDownloadResult(id primitive.ObjectID, result domain.Progress, reasons []string) error {
- ctx, cancel := context.WithTimeout(context.Background(), 5*time.Second)
- defer cancel()
- collection := database.GetCollection("BatchDownloadConfig")
- // _, err := collection.InsertOne(ctx, config)
- filter := bson.M{"_id": id} // 按ID匹配
- update := bson.M{
- // 如果存在就更新非progress字段
- "$set": bson.M{
- "progress": result,
- "failReasons": reasons,
- },
- }
- opts := options.Update().SetUpsert(true)
- _, err := collection.UpdateOne(ctx, filter, update, opts)
- if err != nil {
- return err
- }
- return nil
- }
- // 删除一条批量下载配置
- func DeleteBatchDownloadConfig(id primitive.ObjectID) error {
- ctx, cancel := context.WithTimeout(context.Background(), 5*time.Second)
- defer cancel()
- filter := bson.M{"_id": id} // 删除条件
- collection := database.GetCollection("BatchDownloadConfig")
- _, err := collection.DeleteOne(ctx, filter)
- if err != nil {
- return err
- }
- return nil
- }
- // 插入批量下载结果数据
- func InsertBatchDownloadData(id primitive.ObjectID, data domain.BatchDownloadData) error {
- ctx, cancel := context.WithTimeout(context.Background(), 5*time.Second)
- defer cancel()
- collection := database.GetCollection("BatchDownloadConfig")
- // _, err := collection.InsertOne(ctx, config)
- filter := bson.M{"_id": id} // 按ID匹配
- update := bson.M{
- // 如果存在就更新data字段
- "$set": bson.M{
- "data": data,
- },
- }
- opts := options.Update().SetUpsert(true)
- _, err := collection.UpdateOne(ctx, filter, update, opts)
- if err != nil {
- return err
- }
- return nil
- }
- // 获取下载数据包路径
- func GetBatchDownloadData(id primitive.ObjectID) (map[string]domain.BatchDownloadData, error) {
- ctx, cancel := context.WithTimeout(context.Background(), 5*time.Second)
- defer cancel()
- collection := database.GetCollection("BatchDownloadConfig")
- var cfg domain.BatchDownloadConfig
- err := collection.FindOne(ctx, bson.M{"_id": id}).Decode(&cfg)
- if err != nil {
- return nil, err
- }
- return cfg.Data, nil
- }
- // 插入批量下载结果数据 - PowerFlow
- func InsertBatchDownloadPathPowerFlow(id primitive.ObjectID, sn string, date string, path string) error {
- ctx, cancel := context.WithTimeout(context.Background(), 5*time.Second)
- defer cancel()
- collection := database.GetCollection("BatchDownloadConfig")
- // _, err := collection.InsertOne(ctx, config)
- filter := bson.M{"_id": id} // 按ID匹配
- update := bson.M{
- // 如果存在就更新data字段
- "$set": bson.M{
- "data." + sn + ".powerFlow." + date: path,
- },
- }
- opts := options.Update().SetUpsert(true)
- _, err := collection.UpdateOne(ctx, filter, update, opts)
- if err != nil {
- return err
- }
- return nil
- }
- // 插入批量下载结果数据 - MqttLogs
- func InsertBatchDownloadPathMqttLogs(id primitive.ObjectID, sn string, date string, logType string, path string) error {
- ctx, cancel := context.WithTimeout(context.Background(), 5*time.Second)
- defer cancel()
- collection := database.GetCollection("BatchDownloadConfig")
- // _, err := collection.InsertOne(ctx, config)
- filter := bson.M{"_id": id} // 按ID匹配
- update := bson.M{
- // 如果存在就更新data字段
- "$set": bson.M{
- "data." + sn + ".mqttLogs." + date + "." + logType: path,
- },
- }
- opts := options.Update().SetUpsert(true)
- _, err := collection.UpdateOne(ctx, filter, update, opts)
- if err != nil {
- return err
- }
- return nil
- }
- // 插入批量下载结果数据 - EventAlarm
- func InsertBatchDownloadPathEventAlarm(id primitive.ObjectID, sn string, date string, path string) error {
- ctx, cancel := context.WithTimeout(context.Background(), 5*time.Second)
- defer cancel()
- collection := database.GetCollection("BatchDownloadConfig")
- // _, err := collection.InsertOne(ctx, config)
- filter := bson.M{"_id": id} // 按ID匹配
- update := bson.M{
- // 如果存在就更新data字段
- "$set": bson.M{
- "data." + sn + ".eventAlarm." + date: path,
- },
- }
- opts := options.Update().SetUpsert(true)
- _, err := collection.UpdateOne(ctx, filter, update, opts)
- if err != nil {
- return err
- }
- return nil
- }
- // 插入批量下载结果数据 - AlarmSnapshot
- func InsertBatchDownloadPathAlarmSnapshot(id primitive.ObjectID, sn string, date string, path string) error {
- ctx, cancel := context.WithTimeout(context.Background(), 5*time.Second)
- defer cancel()
- collection := database.GetCollection("BatchDownloadConfig")
- // _, err := collection.InsertOne(ctx, config)
- filter := bson.M{"_id": id} // 按ID匹配
- update := bson.M{
- // 如果存在就更新data字段
- "$set": bson.M{
- "data." + sn + ".alarmSnapshot." + date: path,
- },
- }
- opts := options.Update().SetUpsert(true)
- _, err := collection.UpdateOne(ctx, filter, update, opts)
- if err != nil {
- return err
- }
- return nil
- }
- // 插入批量下载结果数据 - EmsLogs
- func InsertBatchDownloadPathEmsLogs(id primitive.ObjectID, sn string, date string, logType string, path string) error {
- ctx, cancel := context.WithTimeout(context.Background(), 5*time.Second)
- defer cancel()
- collection := database.GetCollection("BatchDownloadConfig")
- // _, err := collection.InsertOne(ctx, config)
- filter := bson.M{"_id": id} // 按ID匹配
- update := bson.M{
- // 如果存在就更新data字段
- "$set": bson.M{
- "data." + sn + ".emsLogs." + date + "." + logType: path,
- },
- }
- opts := options.Update().SetUpsert(true)
- _, err := collection.UpdateOne(ctx, filter, update, opts)
- if err != nil {
- return err
- }
- return nil
- }
- // 插入批量下载结果数据 - FaultRecord
- func InsertBatchDownloadPathFaultRecord(id primitive.ObjectID, sn string, fileName string, path string) error {
- ctx, cancel := context.WithTimeout(context.Background(), 5*time.Second)
- defer cancel()
- collection := database.GetCollection("BatchDownloadConfig")
- // _, err := collection.InsertOne(ctx, config)
- filter := bson.M{"_id": id} // 按ID匹配
- update := bson.M{
- // 如果存在就更新data字段
- "$set": bson.M{
- "data." + sn + ".faultRecord." + fileName: path,
- },
- }
- opts := options.Update().SetUpsert(true)
- _, err := collection.UpdateOne(ctx, filter, update, opts)
- if err != nil {
- return err
- }
- return nil
- }
- // 插入批量下载结果数据 - UpdateLog
- func InsertBatchDownloadPathUpdateLog(id primitive.ObjectID, sn string, path string) error {
- ctx, cancel := context.WithTimeout(context.Background(), 5*time.Second)
- defer cancel()
- collection := database.GetCollection("BatchDownloadConfig")
- // _, err := collection.InsertOne(ctx, config)
- filter := bson.M{"_id": id} // 按ID匹配
- update := bson.M{
- // 如果存在就更新data字段
- "$set": bson.M{
- "data." + sn + ".updateLog": path,
- },
- }
- opts := options.Update().SetUpsert(true)
- _, err := collection.UpdateOne(ctx, filter, update, opts)
- if err != nil {
- return err
- }
- return nil
- }
- // 插入批量下载结果数据 - Settings
- func InsertBatchDownloadPathSettings(id primitive.ObjectID, sn string, path string) error {
- ctx, cancel := context.WithTimeout(context.Background(), 5*time.Second)
- defer cancel()
- collection := database.GetCollection("BatchDownloadConfig")
- // _, err := collection.InsertOne(ctx, config)
- filter := bson.M{"_id": id} // 按ID匹配
- update := bson.M{
- // 如果存在就更新data字段
- "$set": bson.M{
- "data." + sn + ".settings": path,
- },
- }
- opts := options.Update().SetUpsert(true)
- _, err := collection.UpdateOne(ctx, filter, update, opts)
- if err != nil {
- return err
- }
- return nil
- }
|