batchDownloadRepo.go 11 KB

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