From 11b67f857831b553924c2c208fcec91c0053a833 Mon Sep 17 00:00:00 2001 From: 15168347908 <296778233@qq.com> Date: Mon, 26 Aug 2024 16:09:47 +0800 Subject: [PATCH] =?UTF-8?q?fix:=20=E9=94=81=E5=A4=84=E7=90=86?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- Providers/DataProvider.cs | 4 +- Services/ThingDepreciationService.cs | 69 +++++++++++++++++++++++++++- 2 files changed, 69 insertions(+), 4 deletions(-) diff --git a/Providers/DataProvider.cs b/Providers/DataProvider.cs index 4801c57..486e094 100644 --- a/Providers/DataProvider.cs +++ b/Providers/DataProvider.cs @@ -305,7 +305,7 @@ namespace anydata.Providers update = update.Set((i) => i.isDeleted, thing.isDeleted == true); foreach (var kp in thing.extensions) { - if (kp.Key != "archives") + if (kp.Key != "archives" && kp.Key != "locks") { update = update.Set(kp.Key, kp.Value); } @@ -327,7 +327,7 @@ namespace anydata.Providers Console.WriteLine($"更改数量:{res.ModifiedCount}"); } instanceObj.extensions.Remove("data"); - update = Builders.Update.Set("archives.T" + DateTime.Now.ToString("yyyyMMddHHmmss"), instanceObj); + update = Builders.Update.Set("archives.T" + DateTime.Now.ToString("yyyyMMddHHmmss"), instanceObj).Unset("locks"); await latestCollection!.UpdateOneAsync(filter, update); } } diff --git a/Services/ThingDepreciationService.cs b/Services/ThingDepreciationService.cs index b0d54e8..e96b8d7 100644 --- a/Services/ThingDepreciationService.cs +++ b/Services/ThingDepreciationService.cs @@ -2,7 +2,10 @@ using anydata.Loaders.ResponseModel; using anydata.Models; using Microsoft.Extensions.Logging; +using Microsoft.VisualBasic; +using MongoDB.Bson; using MongoDB.Bson.IO; +using MongoDB.Driver; using Newtonsoft.Json.Linq; using StackExchange.Redis; using System.Threading.Channels; @@ -61,10 +64,10 @@ public class ThingDepreciationService : ITransient var options = BuildOptions(context, things.Select(item => item.id).ToList()); var result = await provider.LoadAsync(context.Token, DataProvider.ThingCollName, options); if (!result.success) - { + { return (changes, snapshots); } - var oldThings = ((result.data as LoadResult)?.data as List) ?? new List(); + var oldThings = ((result.data as LoadResult)?.data as List) ?? new List(); foreach (var newThing in things) { var oldThing = oldThings.Find(a => a.id == newThing.id); @@ -124,6 +127,27 @@ public class ThingDepreciationService : ITransient { return OperateResult.Faild($"{period.Period} 已折旧!"); } + if (args.Type == "Confirm") + { + var coll = provider.TryGetCollection(token, "_system-things"); + if (coll.success) + { + var filter = Builders.Filter.And( + Builders.Filter.Eq("belongId", token.BelongId.ToString()), + Builders.Filter.Eq("isDeleted", false), + Builders.Filter.Exists("locks") + ); + var count = await coll.data.Find(filter).Limit(1).CountDocumentsAsync(); + if (count > 0) + { + return OperateResult.Faild($"存在锁定中资产,业务完结后可确认折旧!"); + } + } + else + { + return OperateResult.Faild($"获取 _system-things 集合失败!"); + } + } break; case "Revoke": if (!(period.depreciated ?? false)) @@ -238,6 +262,45 @@ public class ThingDepreciationService : ITransient } } + private async Task LockThings(DepreciationContext context, List things) + { + var coll = provider.TryGetCollection(context.Token, DataProvider.ThingCollName); + if (coll.success) + { + try + { + var filter = Builders.Filter.In(item => item.id, things.Select(item => item.id).ToList()); + var update = Builders.Update + .Set("locks.exclusion.id", context.Instance.id) + .Set("locks.exclusion.name", context.Instance.title + "-" + context.Current.Period) + .Set("locks.exclusion.typeName", context.Instance.title); + await coll.data.UpdateManyAsync(filter, update); + } + catch (Exception error) + { + Console.WriteLine("锁定失败:" + error.ToString()); + } + } + } + + private async Task UnLockThings(DepreciationContext context, List things) + { + var coll = provider.TryGetCollection(context.Token, DataProvider.ThingCollName); + if (coll.success) + { + try + { + var filter = Builders.Filter.In(item => item.id, things.Select(item => item.id).ToList()); + var update = Builders.Update.Unset("locks"); + await coll.data.UpdateManyAsync(filter, update); + } + catch (Exception error) + { + Console.WriteLine("解锁失败:" + error.ToString()); + } + } + } + private async Task StartDepreciation(DepreciationContext context, bool confirm, int totalCount, Func onSkip) { var comparer = new CompareContext(context.Config.Fields) { Token = context.Token, Instance = context.Instance, ChangeTime = context.Current.Period }; @@ -281,6 +344,7 @@ public class ThingDepreciationService : ITransient if (confirm) { await UpdateThings(context, endThings); + await LockThings(context, endThings); } if (oldThings.Count < take) { @@ -316,6 +380,7 @@ public class ThingDepreciationService : ITransient oldThings.ForEach(thing => thing.id = thing["thingId"].ToString()); await UpdateThings(context, oldThings); + await UnLockThings(context, oldThings); onSkip(skip); } -- Gitee