diff --git a/Providers/DataProvider.cs b/Providers/DataProvider.cs index 4801c573e6c161c649a6a681129e528617f7c724..486e094c39d251b5b38b66465f7b467856af3bc3 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 b0d54e8dd0cf37a779b3568b091c3b4e63ee7237..e96b8d728a6ddc4ded9892bc4058c8e3b04e47fb 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); }