batchDownloadRepo.go 12 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463
  1. package mongo
  2. import (
  3. "context"
  4. "fwh-toolbox-server/database"
  5. "fwh-toolbox-server/domain"
  6. "fwh-toolbox-server/dto"
  7. "time"
  8. "go.mongodb.org/mongo-driver/bson"
  9. "go.mongodb.org/mongo-driver/bson/primitive"
  10. "go.mongodb.org/mongo-driver/mongo/options"
  11. )
  12. const (
  13. CollectionName_BatchDownloadConfig = "BatchDownloadConfig"
  14. )
  15. // 从数据库获取批量下载数据列表
  16. func GetBatchDownloadManageList(page, size uint, sn, progress *string) (*dto.ResPage[domain.BatchDownloadInfo], error) {
  17. ctx, cancel := context.WithTimeout(context.Background(), 5*time.Second)
  18. defer cancel()
  19. collection := database.GetCollection(CollectionName_BatchDownloadConfig)
  20. filter := bson.M{}
  21. // 条件
  22. if sn != nil {
  23. filter["sn"] = *sn
  24. }
  25. if progress != nil {
  26. filter["progress"] = *progress
  27. }
  28. findOptions := options.Find().
  29. SetProjection(bson.M{
  30. "_id": 1,
  31. "sn": 1,
  32. "description": 1,
  33. "timePeriod": 1,
  34. "options": 1,
  35. "progress": 1,
  36. "failReasons": 1,
  37. }).
  38. SetSkip(int64((page - 1) * size)).
  39. SetLimit(int64(size)).
  40. SetSort(bson.D{
  41. {Key: "_id", Value: -1}, // 倒序
  42. })
  43. cursor, err := collection.Find(ctx, filter, findOptions)
  44. if err != nil {
  45. return nil, err
  46. }
  47. defer cursor.Close(ctx)
  48. var configList []domain.BatchDownloadInfo
  49. for cursor.Next(ctx) {
  50. var cfg domain.BatchDownloadInfo
  51. if err := cursor.Decode(&cfg); err != nil {
  52. return nil, err
  53. }
  54. configList = append(configList, cfg)
  55. }
  56. // 查询总数
  57. total, err := collection.CountDocuments(ctx, filter)
  58. if err != nil {
  59. return nil, err
  60. }
  61. res := dto.ResPage[domain.BatchDownloadInfo]{
  62. Data: configList,
  63. PageSize: size,
  64. PageNum: page,
  65. Total: uint(total),
  66. }
  67. return &res, nil
  68. }
  69. // 从数据库获取一条批量下载数据配置
  70. func GetBatchDownloadConfig(id primitive.ObjectID) (*domain.BatchDownloadConfig, error) {
  71. ctx, cancel := context.WithTimeout(context.Background(), 5*time.Second)
  72. defer cancel()
  73. collection := database.GetCollection(CollectionName_BatchDownloadConfig)
  74. var cfg domain.BatchDownloadConfig
  75. err := collection.FindOne(ctx, bson.M{"_id": id}).Decode(&cfg)
  76. if err != nil {
  77. return nil, err
  78. }
  79. return &cfg, nil
  80. }
  81. // 插入一条批量下载配置到数据库
  82. func InsertBatchDownloadConfig(writeConfig domain.BatchDownloadConfigWrite) (primitive.ObjectID, error) {
  83. ctx, cancel := context.WithTimeout(context.Background(), 5*time.Second)
  84. defer cancel()
  85. config := domain.BatchDownloadConfig{
  86. Sn: writeConfig.Sn,
  87. Description: writeConfig.Description,
  88. TimePeriod: writeConfig.TimePeriod,
  89. Options: writeConfig.Options,
  90. Progress: domain.ProgressPending,
  91. Data: map[string]domain.BatchDownloadData{},
  92. FailReasons: []string{},
  93. }
  94. collection := database.GetCollection(CollectionName_BatchDownloadConfig)
  95. res, err := collection.InsertOne(ctx, config)
  96. if err != nil {
  97. return primitive.ObjectID{}, err
  98. }
  99. return res.InsertedID.(primitive.ObjectID), nil
  100. }
  101. // 更新一条批量下载描述描述到数据库
  102. func UpdateBatchDownloadDescription(id primitive.ObjectID, desc string) error {
  103. ctx, cancel := context.WithTimeout(context.Background(), 5*time.Second)
  104. defer cancel()
  105. collection := database.GetCollection(CollectionName_BatchDownloadConfig)
  106. // _, err := collection.InsertOne(ctx, config)
  107. filter := bson.M{"_id": id} // 按ID匹配
  108. update := bson.M{
  109. // 如果存在就更新全部字段
  110. "$set": bson.M{
  111. "description": desc,
  112. },
  113. }
  114. opts := options.Update().SetUpsert(true)
  115. _, err := collection.UpdateOne(ctx, filter, update, opts)
  116. if err != nil {
  117. return err
  118. }
  119. return nil
  120. }
  121. // 更新一条批量下载数据owner到数据库
  122. func UpdateBatchDownloadOwner(id primitive.ObjectID, owner []string) error {
  123. ctx, cancel := context.WithTimeout(context.Background(), 5*time.Second)
  124. defer cancel()
  125. collection := database.GetCollection(CollectionName_BatchDownloadConfig)
  126. // _, err := collection.InsertOne(ctx, config)
  127. filter := bson.M{"_id": id} // 按ID匹配
  128. update := bson.M{
  129. // 如果存在就更新全部字段
  130. "$set": bson.M{
  131. "owner": owner,
  132. },
  133. }
  134. opts := options.Update().SetUpsert(true)
  135. _, err := collection.UpdateOne(ctx, filter, update, opts)
  136. if err != nil {
  137. return err
  138. }
  139. return nil
  140. }
  141. // 更新一条批量下载结果到数据库
  142. func UpdateBatchDownloadResult(id primitive.ObjectID, result domain.Progress, reasons []string) error {
  143. ctx, cancel := context.WithTimeout(context.Background(), 5*time.Second)
  144. defer cancel()
  145. collection := database.GetCollection(CollectionName_BatchDownloadConfig)
  146. // _, err := collection.InsertOne(ctx, config)
  147. filter := bson.M{"_id": id} // 按ID匹配
  148. update := bson.M{
  149. // 如果存在就更新非progress字段
  150. "$set": bson.M{
  151. "progress": result,
  152. "failReasons": reasons,
  153. },
  154. }
  155. opts := options.Update().SetUpsert(true)
  156. _, err := collection.UpdateOne(ctx, filter, update, opts)
  157. if err != nil {
  158. return err
  159. }
  160. return nil
  161. }
  162. // 删除一条批量下载配置
  163. func DeleteBatchDownloadConfig(id primitive.ObjectID) error {
  164. ctx, cancel := context.WithTimeout(context.Background(), 5*time.Second)
  165. defer cancel()
  166. filter := bson.M{"_id": id} // 删除条件
  167. collection := database.GetCollection(CollectionName_BatchDownloadConfig)
  168. _, err := collection.DeleteOne(ctx, filter)
  169. if err != nil {
  170. return err
  171. }
  172. return nil
  173. }
  174. // 插入批量下载结果数据
  175. func InsertBatchDownloadData(id primitive.ObjectID, data domain.BatchDownloadData) error {
  176. ctx, cancel := context.WithTimeout(context.Background(), 5*time.Second)
  177. defer cancel()
  178. collection := database.GetCollection(CollectionName_BatchDownloadConfig)
  179. // _, err := collection.InsertOne(ctx, config)
  180. filter := bson.M{"_id": id} // 按ID匹配
  181. update := bson.M{
  182. // 如果存在就更新data字段
  183. "$set": bson.M{
  184. "data": data,
  185. },
  186. }
  187. opts := options.Update().SetUpsert(true)
  188. _, err := collection.UpdateOne(ctx, filter, update, opts)
  189. if err != nil {
  190. return err
  191. }
  192. return nil
  193. }
  194. // 获取下载数据包路径
  195. func GetBatchDownloadData(id primitive.ObjectID) (map[string]domain.BatchDownloadData, error) {
  196. ctx, cancel := context.WithTimeout(context.Background(), 5*time.Second)
  197. defer cancel()
  198. collection := database.GetCollection(CollectionName_BatchDownloadConfig)
  199. var cfg domain.BatchDownloadConfig
  200. err := collection.FindOne(ctx, bson.M{"_id": id}).Decode(&cfg)
  201. if err != nil {
  202. return nil, err
  203. }
  204. return cfg.Data, nil
  205. }
  206. // 插入批量下载结果数据 - PowerFlow
  207. func InsertBatchDownloadPathPowerFlow(id primitive.ObjectID, sn string, date string, path string) error {
  208. ctx, cancel := context.WithTimeout(context.Background(), 5*time.Second)
  209. defer cancel()
  210. collection := database.GetCollection(CollectionName_BatchDownloadConfig)
  211. // _, err := collection.InsertOne(ctx, config)
  212. filter := bson.M{"_id": id} // 按ID匹配
  213. update := bson.M{
  214. // 如果存在就更新data字段
  215. "$set": bson.M{
  216. "data." + sn + ".powerFlow." + date: path,
  217. },
  218. }
  219. opts := options.Update().SetUpsert(true)
  220. _, err := collection.UpdateOne(ctx, filter, update, opts)
  221. if err != nil {
  222. return err
  223. }
  224. return nil
  225. }
  226. // 插入批量下载结果数据 - MqttLogs
  227. func InsertBatchDownloadPathMqttLogs(id primitive.ObjectID, sn string, date string, logType string, path string) error {
  228. ctx, cancel := context.WithTimeout(context.Background(), 5*time.Second)
  229. defer cancel()
  230. collection := database.GetCollection(CollectionName_BatchDownloadConfig)
  231. // _, err := collection.InsertOne(ctx, config)
  232. filter := bson.M{"_id": id} // 按ID匹配
  233. update := bson.M{
  234. // 如果存在就更新data字段
  235. "$set": bson.M{
  236. "data." + sn + ".mqttLogs." + date + "." + logType: path,
  237. },
  238. }
  239. opts := options.Update().SetUpsert(true)
  240. _, err := collection.UpdateOne(ctx, filter, update, opts)
  241. if err != nil {
  242. return err
  243. }
  244. return nil
  245. }
  246. // 插入批量下载结果数据 - EventAlarm
  247. func InsertBatchDownloadPathEventAlarm(id primitive.ObjectID, sn string, date string, path string) error {
  248. ctx, cancel := context.WithTimeout(context.Background(), 5*time.Second)
  249. defer cancel()
  250. collection := database.GetCollection(CollectionName_BatchDownloadConfig)
  251. // _, err := collection.InsertOne(ctx, config)
  252. filter := bson.M{"_id": id} // 按ID匹配
  253. update := bson.M{
  254. // 如果存在就更新data字段
  255. "$set": bson.M{
  256. "data." + sn + ".eventAlarm." + date: path,
  257. },
  258. }
  259. opts := options.Update().SetUpsert(true)
  260. _, err := collection.UpdateOne(ctx, filter, update, opts)
  261. if err != nil {
  262. return err
  263. }
  264. return nil
  265. }
  266. // 插入批量下载结果数据 - AlarmSnapshot
  267. func InsertBatchDownloadPathAlarmSnapshot(id primitive.ObjectID, sn string, date string, path string) error {
  268. ctx, cancel := context.WithTimeout(context.Background(), 5*time.Second)
  269. defer cancel()
  270. collection := database.GetCollection(CollectionName_BatchDownloadConfig)
  271. // _, err := collection.InsertOne(ctx, config)
  272. filter := bson.M{"_id": id} // 按ID匹配
  273. update := bson.M{
  274. // 如果存在就更新data字段
  275. "$set": bson.M{
  276. "data." + sn + ".alarmSnapshot." + date: path,
  277. },
  278. }
  279. opts := options.Update().SetUpsert(true)
  280. _, err := collection.UpdateOne(ctx, filter, update, opts)
  281. if err != nil {
  282. return err
  283. }
  284. return nil
  285. }
  286. // 插入批量下载结果数据 - EmsLogs
  287. func InsertBatchDownloadPathEmsLogs(id primitive.ObjectID, sn string, date string, logType string, path string) error {
  288. ctx, cancel := context.WithTimeout(context.Background(), 5*time.Second)
  289. defer cancel()
  290. collection := database.GetCollection(CollectionName_BatchDownloadConfig)
  291. // _, err := collection.InsertOne(ctx, config)
  292. filter := bson.M{"_id": id} // 按ID匹配
  293. update := bson.M{
  294. // 如果存在就更新data字段
  295. "$set": bson.M{
  296. "data." + sn + ".emsLogs." + date + "." + logType: path,
  297. },
  298. }
  299. opts := options.Update().SetUpsert(true)
  300. _, err := collection.UpdateOne(ctx, filter, update, opts)
  301. if err != nil {
  302. return err
  303. }
  304. return nil
  305. }
  306. // 插入批量下载结果数据 - FaultRecord
  307. func InsertBatchDownloadPathFaultRecord(id primitive.ObjectID, sn string, fileName string, path string) error {
  308. ctx, cancel := context.WithTimeout(context.Background(), 5*time.Second)
  309. defer cancel()
  310. collection := database.GetCollection(CollectionName_BatchDownloadConfig)
  311. // _, err := collection.InsertOne(ctx, config)
  312. filter := bson.M{"_id": id} // 按ID匹配
  313. update := bson.M{
  314. // 如果存在就更新data字段
  315. "$set": bson.M{
  316. "data." + sn + ".faultRecord." + fileName: path,
  317. },
  318. }
  319. opts := options.Update().SetUpsert(true)
  320. _, err := collection.UpdateOne(ctx, filter, update, opts)
  321. if err != nil {
  322. return err
  323. }
  324. return nil
  325. }
  326. // 插入批量下载结果数据 - UpdateLog
  327. func InsertBatchDownloadPathUpdateLog(id primitive.ObjectID, sn string, path string) error {
  328. ctx, cancel := context.WithTimeout(context.Background(), 5*time.Second)
  329. defer cancel()
  330. collection := database.GetCollection(CollectionName_BatchDownloadConfig)
  331. // _, err := collection.InsertOne(ctx, config)
  332. filter := bson.M{"_id": id} // 按ID匹配
  333. update := bson.M{
  334. // 如果存在就更新data字段
  335. "$set": bson.M{
  336. "data." + sn + ".updateLog": path,
  337. },
  338. }
  339. opts := options.Update().SetUpsert(true)
  340. _, err := collection.UpdateOne(ctx, filter, update, opts)
  341. if err != nil {
  342. return err
  343. }
  344. return nil
  345. }
  346. // 插入批量下载结果数据 - Settings
  347. func InsertBatchDownloadPathSettings(id primitive.ObjectID, sn string, path string) error {
  348. ctx, cancel := context.WithTimeout(context.Background(), 5*time.Second)
  349. defer cancel()
  350. collection := database.GetCollection(CollectionName_BatchDownloadConfig)
  351. // _, err := collection.InsertOne(ctx, config)
  352. filter := bson.M{"_id": id} // 按ID匹配
  353. update := bson.M{
  354. // 如果存在就更新data字段
  355. "$set": bson.M{
  356. "data." + sn + ".settings": path,
  357. },
  358. }
  359. opts := options.Update().SetUpsert(true)
  360. _, err := collection.UpdateOne(ctx, filter, update, opts)
  361. if err != nil {
  362. return err
  363. }
  364. return nil
  365. }