using System; using System.Collections.Concurrent; using System.Collections.Generic; using System.Diagnostics; using System.Linq; using System.Text; using System.Threading; using System.Threading.Tasks; using DQ.Construction.NewLook.DataManager; using WorkData; using WorkData.Entity; #region 文件说明 /*----------------------------------------------------- * 版权所有 (c) 2024 jdfcd * CLR版本 4.0.30319.42000 * 命名空间 DQ.Construction.NewLook.Utility * * 创建者:pnpe * 电子邮件:pnpe@qq.com * 创建时间:2024/7/6 16:52:09 */ #endregion 文件说明 namespace WellWorkDataUI { public delegate void ProgressCallback(double progress); public delegate void ProgressCallbackTwo(int progress, int count); public delegate void ProgressCallbackThree(string name, int progress, int count); public class CalcDataHelp { public static void Reset() { sysDeflectionsDict.Clear(); } private static Dictionary sysDeflectionsDict = new Dictionary(); /// /// 坐标计算 /// /// 自定义类 /// 数据list /// 获取井号 /// 获取深度 /// 回写xy方法 /// 返回数据 public static List ComputedXYCoreDetectionOrVertical(List list, Func getName, Func getHeight, Action onXY) where T : class { WellDeflectionHelp deflectionHelp = new WellDeflectionHelp(); deflectionHelp.Initialize(); global::System.Collections.Concurrent.ConcurrentBag listUpdate = new ConcurrentBag(); Parallel.ForEach(list, item => { double? x = 0, y = 0; // , v = 0; double? sd1 = getHeight(item).sd; double? cs1 = getHeight(item).cs; if (sd1.HasValue && deflectionHelp.ComputedXYByDetection(getName(item), sd1, ref x, ref y)) { onXY(item, x.GetValueOrDefault(0), y.GetValueOrDefault(0)); listUpdate.Add(item); } else if (cs1.HasValue && deflectionHelp.ComputedXYByVerticalNew(getName(item), cs1, ref x, ref y)) { onXY(item, x.GetValueOrDefault(0), y.GetValueOrDefault(0)); listUpdate.Add(item); } }); return listUpdate.ToList(); } /// /// Computeds the x y and z2 core single. /// /// The item. /// The get item. /// The dic_calc. /// The update item. /// A T. public static T ComputedXYAndZ2CoreSingle(T item, WellDeflectionHelp deflectionHelp, string JH, double? sd1, double? sd2, double? cs1, double? cs2, double? hd, double? v_hd, double? x, double? y, HashSet dic_calc, Action> updateItem) { // 井斜xy Dictionary update = new Dictionary(); if (cs1.HasValue && v_hd.HasValue && !cs2.HasValue) { cs2 = cs1.Value + v_hd.Value; update.Add("CS_B", cs2); } else if (cs2.HasValue && v_hd.HasValue && !cs1.HasValue) { cs1 = cs2.Value - v_hd.Value; update.Add("CS_T", cs1); } if (sd1.HasValue && hd.HasValue && !sd2.HasValue) { sd2 = sd1.Value + hd.Value; update.Add("DJSD", sd2); } else if (sd2.HasValue && hd.HasValue && !sd1.HasValue) { sd1 = sd2.Value - hd.Value; update.Add("SD", sd1); } // bool csStatus = true; // bool sdStatus = true; // if (!sd2.HasValue || !sd1.HasValue) // { // sdStatus = false; // } // if (!cs2.HasValue || !cs1.HasValue) // { // csStatus = false; // } if (dic_calc.Contains(3) && !cs1.HasValue && deflectionHelp.ComputedZByDetection(JH, sd1, ref cs1)) { update.Add("CS_T", cs1); } if (dic_calc.Contains(4) && !cs2.HasValue && deflectionHelp.ComputedZByDetection(JH, sd2, ref cs2)) { update.Add("CS_B", cs2); } // 垂深算测深 if (dic_calc.Contains(1) && !sd1.HasValue && deflectionHelp.ComputedZByVertical(JH, cs1, ref sd1)) { update.Add("SD", sd1); } // 垂深算测深 if (dic_calc.Contains(2) && !sd2.HasValue && deflectionHelp.ComputedZByVertical(JH, cs2, ref sd2)) { update.Add("DJSD", sd2); } if (!x.HasValue || !y.HasValue) { if (dic_calc.Contains(7) && sd1.HasValue && deflectionHelp.ComputedXYByDetection(JH, sd1, ref x, ref y)) { update.Add("X", x); update.Add("Y", y); } else if (dic_calc.Contains(7) && cs1.HasValue && deflectionHelp.ComputedXYByVerticalNew(JH, cs1, ref x, ref y)) { update.Add("X", x); update.Add("Y", y); } } updateItem(item, update); return item; } /// /// 使用多线程计算xyz /// /// The list. /// The get item. /// The dic_calc. /// The update item. /// A list of TS. public static List ComputedXYAndZ2Core(List list, Func getItem, HashSet dic_calc, Action> updateItem, ProgressCallback progressCallback) where T : class { int totalCount = list.Count; int processedCount = 0; ConcurrentBag listUpdate = new ConcurrentBag(); Stopwatch sw = Stopwatch.StartNew(); SimpleWell[] simpleWells = DBHelp.Db.Queryable().Select(it => new SimpleWell() { JH = it.JH, X = it.X.Value, Y = it.Y.Value }) .Where(it => it.X > 0 && it.Y > 0).ToArray(); int i = 0; List wds = DBHelp.Db.Queryable().ToList(); List wellDeflections = new List(); Parallel.ForEach(wds, EntityHelp.Options, item => { try { i++; if (item == null || string.IsNullOrWhiteSpace(item.JH) || sysDeflectionsDict.ContainsKey(item.JH)) { return; } List itemDatas = EntityHelp.GetWellDeflections(item.FileName); List sds = new List(); if (itemDatas != null && itemDatas.Any()) { for (var j = 0; j < itemDatas.Count; j++) { var itemData = itemDatas[j]; var sd = new SimpleDeflection { JH = item.JH, BZB = itemData.BZB.HasValue ? itemData.BZB.Value : 0, DZB = itemData.DZB.HasValue ? itemData.DZB.Value : 0, JS = itemData.JS.HasValue ? itemData.JS.Value : 0, CS = itemData.CS.HasValue ? itemData.CS.Value : 0, }; sds.Add(sd); } } var deflectionHelp1 = new WellDeflectionHelp(); if (sds != null && sds.Count > 0) { deflectionHelp1.Initialize(item.JH, simpleWells, sds?.ToList().ToArray()); } try { //Console.WriteLine($"******************{item.JH}*******************"); if (deflectionHelp1 != null) { sysDeflectionsDict.Add(item.JH, deflectionHelp1); } else { sysDeflectionsDict.Add(item.JH, new WellDeflectionHelp()); } } catch (Exception iex) { //Console.WriteLine($"******************{item.JH}*******************"); Console.WriteLine($"计算数据异常:{iex.Message}"); } } catch (Exception ex) { Console.WriteLine($"=============计算XYZ异常:{ex.Message} ======================="); } }); Console.WriteLine($"数据准备完毕{sw.Elapsed}"); object obj = new object(); Parallel.ForEach(list, (item, index) => { string jh = getItem(item).JH; if (jh != null) { double? sd1 = getItem(item).sd1; double? sd2 = getItem(item).sd2; double? cs1 = getItem(item).cs1; double? cs2 = getItem(item).cs2; double? hd = getItem(item).hd; double? v_hd = getItem(item).v_hd; double? x = getItem(item).x; double? y = getItem(item).y; WellDeflectionHelp deflectionHelp; if (!sysDeflectionsDict.ContainsKey(jh)) { deflectionHelp = new WellDeflectionHelp(); deflectionHelp.Initialize(jh, simpleWells, new List().ToArray()); } else { deflectionHelp = sysDeflectionsDict[jh]; } listUpdate.Add(ComputedXYAndZ2CoreSingle(item, deflectionHelp, jh, sd1, sd2, cs1, cs2, hd, v_hd, x, y, dic_calc, updateItem)); //Console.WriteLine($"{processedCount}耗时:{sw.Elapsed.TotalMilliseconds}"); } Interlocked.Increment(ref processedCount); double currentProgress = processedCount * 100.00 / totalCount; if (currentProgress <= 100) { progressCallback(currentProgress); } }); sw.Stop(); Console.WriteLine($"总耗时:{sw.Elapsed.TotalMilliseconds}"); progressCallback(100); return listUpdate.ToList(); } } }