// ***********************************************************************
// Assembly : Construction
// Author : zjx
// Created : 09-01-2020
//
// Last Modified By : zjx
// Last Modified On : 09-01-2020
// ***********************************************************************
//
// Copyright (c) jindongfang. All rights reserved.
//
//
// ***********************************************************************
namespace WellWorkDataUI
{
// using DQ.Construction.NewLook.Utility;
using Newtonsoft.Json;
using SqlSugar;
using System;
using System.Collections.Concurrent;
using System.Collections.Generic;
using System.Data;
using System.Diagnostics;
using System.IO;
using System.Linq;
using System.Linq.Expressions;
using System.Text;
using System.Threading;
using System.Threading.Tasks;
using WorkData;
using WorkData.Entity;
///
/// 获取文件字符编码
///
public class EncodingType
{
///
/// 给定文件的路径,读取文件的二进制数据,判断文件的编码类型
///
/// 文件路径
/// 文件的编码类型
public static System.Text.Encoding GetType(string fileName)
{
FileStream fs = new FileStream(fileName, FileMode.Open, FileAccess.Read);
Encoding r = GetType(fs);
fs.Close();
return r;
}
///
/// 通过给定的文件流,判断文件的编码类型
///
/// 文件流
/// 文件的编码类型
public static System.Text.Encoding GetType(FileStream fs)
{
byte[] unicode = new byte[] { 0xFF, 0xFE, 0x41 };
byte[] unicodeBIG = new byte[] { 0xFE, 0xFF, 0x00 };
byte[] uTF8 = new byte[] { 0xEF, 0xBB, 0xBF }; // 带BOM
BinaryReader r = new BinaryReader(fs, System.Text.Encoding.Default);
int i;
int.TryParse(fs.Length.ToString(), out i);
byte[] ss = r.ReadBytes(i);
Encoding reVal = GetType(ss);
r.Close();
return reVal;
}
///
/// 获得格式编码
///
/// 数据内容
/// 编码
public static System.Text.Encoding GetType(byte[] data)
{
Encoding reVal = Encoding.Default;
if (IsUTF8Bytes(data) || (data[0] == 0xEF && data[1] == 0xBB && data[2] == 0xBF))
{
reVal = Encoding.UTF8;
}
else if (data[0] == 0xFE && data[1] == 0xFF && data[2] == 0x00)
{
reVal = Encoding.BigEndianUnicode;
}
else if (data[0] == 0xFF && data[1] == 0xFE && data[2] == 0x41)
{
reVal = Encoding.Unicode;
}
return reVal;
}
///
/// 判断是否是不带 BOM 的 UTF8 格式
///
/// 数据
/// 是否UTF8
private static bool IsUTF8Bytes(byte[] data)
{
int charByteCounter = 1;
// 计算当前正分析的字符应还有的字节数
byte curByte; // 当前分析的字节.
for (int i = 0; i < data.Length; i++)
{
curByte = data[i];
if (charByteCounter == 1)
{
if (curByte >= 0x80)
{
// 判断当前
while (((curByte <<= 1) & 0x80) != 0)
{
charByteCounter++;
}
// 标记位首位若为非0 则至少以2个1开始 如:110XXXXX...........1111110X
if (charByteCounter == 1 || charByteCounter > 6)
{
return false;
}
}
}
else
{
// 若是UTF-8 此时第一位必须为1
if ((curByte & 0xC0) != 0x80)
{
return false;
}
charByteCounter--;
}
}
if (charByteCounter > 1)
{
throw new Exception("非预期的byte格式");
}
return true;
}
}
///
/// 全局数据类(不变数据), 一次加载, 全局调用
///
public static class DataHelp
{
private static ConcurrentDictionary> dictWellDeflection = new ConcurrentDictionary>();
private static ConcurrentDictionary> dictWellLayer = new ConcurrentDictionary>();
///
/// Gets the list system data tree.
///
/// The list system data tree.
public static List ListGeoLayer { get; } = new List();
///
/// Gets the list system data tree.
///
/// The list system data tree.
public static List ListSysDataTree { get; } = new List();
///
/// Gets the list table information.
///
/// The list table information.
public static List ListTableInfo { get; } = new List();
///
/// Gets the list system dictionary.
///
/// The list system dictionary.
public static List ListSysDictionary { get; } = new List();
///
/// 储层数据是否按(层位/井号)分组
///
public static bool IsReservoirDataGroupByLayer { get; set; } = false;
///
/// 井分层是否按(层位/井号)分组.
///
public static bool IsDrillwelllayeredGroupByLayer { get; set; } = false;
///
/// 井斜采样点大小, 为0表示不更新采样值
///
public static double WellDeflectionCollectCount { get; set; } = 0d;
///
/// 储层数据是否累加计算
///
public static bool IsReservoirDataStat { get; set; } = true;
///
/// ListColumnSearchWellName
///
public static List ListColumnSearchWellName { get; } = DBHelp.GetEntityColumnInfo(typeof(SearchWellName)).Select(o => o.DbColumnName).ToList();
///
/// 工区边界缓存
///
// public static (DrawerRange range, string path) workBorder { get; set; } = (null, string.Empty);
///
/// Resets the.
///
public static void Reset()
{
dictWellDeflection.Clear();
dictWellLayer.Clear();
ListGeoLayer.Clear();
}
///
/// Clones the table.
///
/// The source.
/// The dest.
/// if set to true [first row as column].
/// The count.
public static void CloneTable(
DataTable source,
DataTable dest,
bool firstRowAsColumn = false,
int? count = null)
{
dest.Columns.Clear();
dest.Rows.Clear();
for (int i = 0; i < source.Columns.Count; i++)
{
string colName = firstRowAsColumn ? source.Rows[0][i].ToString() : source.Columns[i].ColumnName;
while (dest.Columns.Contains(colName))
{
colName += "_";
}
dest.Columns.Add(colName);
}
int startRow = firstRowAsColumn ? 1 : 0;
int endRow = count == null ? source.Rows.Count : Math.Min(startRow + count.Value, source.Rows.Count);
for (int i = startRow; i < endRow; i++)
{
dest.Rows.Add(source.Rows[i].ItemArray);
}
}
///
/// Loads the data.
///
/// Task
public static async Task LoadData()
{
await LoadDataSysDataTree();
await LoadDataTableInfo();
await LoadDataSysDictionary();
await LoadGeoLayer();
}
///
/// Loads the data system dictionary.
///
/// A representing the asynchronous operation.
internal static async Task LoadDataSysDictionary()
{
ListSysDictionary.Clear();
ListSysDictionary.AddRange(await DBHelp.NewDb.Queryable().ToListAsync());
}
///
/// Loads the data system data tree.
///
/// A representing the asynchronous operation.
public static async Task LoadDataSysDataTree()
{
ListSysDataTree.Clear();
ListSysDataTree.AddRange(await DBHelp.NewDb.Queryable().ToListAsync());
}
///
/// Loads the data system data tree.
///
/// A representing the asynchronous operation.
public static async Task LoadGeoLayer()
{
ListGeoLayer.Clear();
ListGeoLayer.AddRange(await DBHelp.NewDb.Queryable().ToListAsync());
}
///
/// Loads the data table information.
///
/// A representing the asynchronous operation.
internal static async Task LoadDataTableInfo()
{
ListTableInfo.Clear();
var list = await DBHelp.NewDb.Queryable().ToListAsync();
ListTableInfo.AddRange(list);
}
///
/// 同步井索引
///
public static void SyncSearchWellName()
{
DBHelp.NewDb.UseTran(
db =>
{
db.Ado.ExecuteCommand(
@"insert into searchwellname(WellName, wellbase)
select JH,1 from well_base a where not EXISTS(select wellname from searchwellname b where a.jh==b.wellname)");
db.Ado.ExecuteCommand(
@"delete from searchwellname where wellname is null");
db.Ado.ExecuteCommand(
@"update searchwellname as a set wellbase=(select count(*) from well_base b where a.wellname=b.jh)");
//曲线索引
List wellCurves = db.Queryable().Select(c => c.JH).ToList().Distinct().ToList();
db.Updateable().SetColumns(x => x.WellCurve == 1).Where(x => wellCurves.Contains(x.WellName)).ExecuteCommand();
db.Updateable().SetColumns(x => x.WellCurve == 0).Where(x => !wellCurves.Contains(x.WellName)).ExecuteCommand();
UpdateAllSearchWellNameCore(db);
// 清理全为0的井索引
db.Deleteable().Where(o => o.InterpretDrillWellLayered == 0
&& o.WellBase == 0
&& o.WellBreakPoint == 0
&& o.WellCurve == 0
&& o.WellTimeDepth == 0
&& o.WellDeflection == 0
&& o.CcsjYxhd == 0
&& o.CcsjSyhd == 0
&& o.CcsjBhd == 0
&& o.CcsjKxd == 0
&& o.CcsjStl == 0
&& o.WellPerforation == 0).ExecuteCommand();
});
}
///
/// 更新井索引
///
/// 更新的列类型
public static void UpdateSearchWellName()
{
UpdateSearchWellNameCore(DBHelp.NewDb);
}
///
/// Updates the name of the search well.
///
/// The t.
public static void UpdateSearchWellName(Type t)
{
UpdateSearchWellNameCore(DBHelp.NewDb, t);
}
private static void UpdateSearchWellNameCore(CustomSqlSugarClient db)
{
try
{
string columnname = typeof(T).Name;
if (columnname.Equals("WellCurveFile"))
{
columnname = "WellCurve";
}//因为后面导入测井曲线都存到WellCurveFile表里去了,但是这个表里的字段名仍然为WellCurve
string tablename = DBHelp.GetTableName();
string sql = string.Format(
@"update searchwellname as a set {0} = (select count(*) from {1} b where a.wellname=b.jh)",
columnname,
tablename);
db.Ado.ExecuteCommand(sql);
}
catch (Exception /*ex*/)
{
// ex
}
}
private static void UpdateSearchWellNameCore(CustomSqlSugarClient db, Type t)
{
try
{
string columnname = t.Name;
string tablename = DBHelp.GetTableName(t);
if (ListColumnSearchWellName.Any(o => string.Equals(o, columnname, StringComparison.CurrentCultureIgnoreCase)))
{
string sql = string.Format(
@"update searchwellname as a set {0} = (select count(*) from {1} b where a.wellname=b.jh)",
columnname,
tablename);
db.Ado.ExecuteCommand(sql);
}
}
catch (Exception /*ex*/)
{
// ex
}
}
private static void UpdateAllSearchWellNameCore(CustomSqlSugarClient db)
{
Type tIdentify = typeof(IIdentifierEntity);
List list = EntityHelp.AllEntity
.Where(o => tIdentify.IsAssignableFrom(o.Type))
.ToList();
foreach (SqlSugar.EntityInfo item in list)
{
UpdateSearchWellNameCore(db, item.Type);
}
}
///
/// 更新井索引
///
public static void UpdateAllSearchWellName()
{
UpdateAllSearchWellNameCore(DBHelp.NewDb);
}
///
/// 更新和同步层位名.
///
/// The identifier.
/// The name.
public static void UpdateLayerName(int id, string name)
{
DBHelp.NewDb.UseTran(db =>
{
db.Updateable(o => new InterpretLayer { CW = name })
.Where(o => o.ID == id)
.ExecuteCommand();
db.Updateable(o => new InterpretTimeLayer { CW = name })
.Where(o => o.CWID == id)
.ExecuteCommand();
db.Updateable(o => new InterpretDepthLayer { CW = name })
.Where(o => o.CWID == id)
.ExecuteCommand();
db.Updateable(o => new InterpretBoundary { CW = name })
.Where(o => o.CWID == id)
.ExecuteCommand();
db.Updateable(o => new InterpretFault { CW = name })
.Where(o => o.CWID == id)
.ExecuteCommand();
db.Updateable(o => new InterpretAttribute { CW = name })
.Where(o => o.CWID == id)
.ExecuteCommand();
});
}
///
/// 更新和同步层顺序.
///
/// The order.
public static void UpdateLayerOrder(List order)
{
DBHelp.NewDb.UseTran(db =>
{
for (int i = 0; i < order.Count; i++)
{
db.Updateable(o => new InterpretTimeLayer { XHID = i })
.Where(o => o.CW == order[i])
.ExecuteCommand();
db.Updateable(o => new InterpretDepthLayer { XHID = i })
.Where(o => o.CW == order[i])
.ExecuteCommand();
db.Updateable(o => new InterpretBoundary { XHID = i })
.Where(o => o.CW == order[i])
.ExecuteCommand();
db.Updateable(o => new InterpretFault { XHID = i })
.Where(o => o.CW == order[i])
.ExecuteCommand();
}
});
}
///
/// 同步储层数据 (有效厚度发生变化)
///
//public static void SyncReservoirData(string v_jh, ProgressCallbackThree progressCallback = null)
//{
// try
// {
// List arys = new List();
// if (typeof(T) == typeof(CcsjKxd))
// {
// arys = new List() { "kxd", "bhd" };
// }
// else if (typeof(T) == typeof(CcsjBhd))
// {
// arys = new List() { "bhd" };
// }
// else if (typeof(T) == typeof(CcsjStl))
// {
// arys = new List() { "stl" };
// }
// else
// {
// arys = new List() { "kxd", "bhd", "stl" };
// }
// //获取有效厚度
// List yxhds = DBHelp.NewDb.Queryable().WhereIF(!string.IsNullOrWhiteSpace(v_jh), x => x.JH == v_jh).ToList();
// Dictionary> dict_yxhds = yxhds.GroupBy(x => x.JH).ToDictionary(x => x.Key, x => x.ToList());
// List kxds = DBHelp.NewDb.Queryable().WhereIF(!string.IsNullOrWhiteSpace(v_jh), x => x.JH == v_jh).ToList();
// if (arys.Contains("kxd"))
// {
// int processedCount = 0;
// int totalCount = kxds.Count;
// Parallel.ForEach(kxds, kxd =>
// {
// if (dict_yxhds.TryGetValue(kxd.JH, out List hds))
// {
// var hd = hds.Where(x => x.CW == kxd.CW);
// if (kxd.DJSD1.HasValue)
// {
// hd = hd.Where(x => x.DJSD1 == kxd.DJSD1);
// }
// else
// {
// hd = hd.Where(x => x.YXCS_T == kxd.YXCS_T);
// }
// if (hd.Count() > 0)
// {
// if (kxd.YXHD == null)
// {
// kxd.YXHD = hd.Average(x => x.YXHD);
// }
// if (kxd.YXHD_CS == null)
// {
// kxd.YXHD_CS = hd.Average(x => x.YXCS_HD);
// }
// }
// }
// Interlocked.Increment(ref processedCount);
// progressCallback?.Invoke("同步有效到孔隙度", processedCount, totalCount);
// });
// DBHelp.NewDb.Fastest().BulkUpdateAsync(kxds, new string[] { "JH", "CW", "ID" }, new string[] { "YXHD", "YXHD_CS" });
// }
// List bhds = new List();
// if (arys.Contains("bhd"))
// {
// bhds = DBHelp.NewDb.Queryable().WhereIF(!string.IsNullOrWhiteSpace(v_jh), x => x.JH == v_jh).ToList();
// int processedCount = 0;
// int totalCount = bhds.Count;
// Parallel.ForEach(bhds, bhd =>
// {
// if (dict_yxhds.TryGetValue(bhd.JH, out List hds))
// {
// var hd = hds.Where(x => x.CW == bhd.CW);
// if (bhd.DJSD1.HasValue)
// {
// hd = hd.Where(x => x.DJSD1 == bhd.DJSD1);
// }
// else
// {
// hd = hd.Where(x => x.YXCS_T == bhd.YXCS_T);
// }
// if (hd.Count() > 0)
// {
// if (bhd.YXHD == null)
// {
// bhd.YXHD = hd.Average(x => x.YXHD);
// }
// if (bhd.YXHD_CS == null)
// {
// bhd.YXHD_CS = hd.Average(x => x.YXCS_HD);
// }
// var kx = kxds.Where(x => x.JH == bhd.JH && x.CW == bhd.CW);
// if (bhd.DJSD1.HasValue)
// {
// kx = kx.Where(x => x.DJSD1 == bhd.DJSD1);
// }
// else
// {
// kx = kx.Where(x => x.YXCS_T == bhd.YXCS_T);
// }
// if (kx.Count() > 0)
// {
// bhd.KXD = kx.Average(x => x.KXD);
// }
// }
// }
// Interlocked.Increment(ref processedCount);
// progressCallback?.Invoke("同步有效和孔隙度到饱和度", processedCount, totalCount);
// });
// DBHelp.NewDb.Fastest().BulkUpdateAsync(bhds, new string[] { "JH", "CW", "ID" }, new string[] { "YXHD", "YXHD_CS", "KXD" });
// }
// List stls = new List();
// if (arys.Contains("stl"))
// {
// stls = DBHelp.NewDb.Queryable().WhereIF(!string.IsNullOrWhiteSpace(v_jh), x => x.JH == v_jh).ToList();
// int processedCount = 0;
// int totalCount = stls.Count;
// Parallel.ForEach(stls, stl =>
// {
// if (dict_yxhds.TryGetValue(stl.JH, out List hds))
// {
// var hd = hds.Where(x => x.CW == stl.CW);
// if (stl.DJSD1.HasValue)
// {
// hd = hd.Where(x => x.DJSD1 == stl.DJSD1);
// }
// else
// {
// hd = hd.Where(x => x.YXCS_T == stl.YXCS_T);
// }
// if (hd.Count() > 0)
// {
// if (stl.YXHD == null)
// {
// stl.YXHD = hd.Average(x => x.YXHD);
// }
// if (stl.YXHD_CS == null)
// {
// stl.YXHD_CS = hd.Average(x => x.YXCS_HD);
// }
// }
// }
// Interlocked.Increment(ref processedCount);
// progressCallback?.Invoke("同步有效到渗透率", processedCount, totalCount);
// });
// DBHelp.NewDb.Fastest().BulkUpdateAsync(stls, new string[] { "JH", "CW", "ID" }, new string[] { "YXHD", "YXHD_CS" });
// }
// }
// catch (Exception)
// {
// // ignore Exception
// }
//}
///
/// 获取工区边界静态存储,如果导入新边界请清空此值
///
public static object locker = new object();
///
/// 获取工区边界对象
///
/// 工区边界及路径
//public static (DrawerRange range, string path) GetWorkAreaOutLineRange()
//{
// if (workBorder.range != null)
// {
// return workBorder;
// }
// else
// {
// lock (locker) // 存在并行调用,写入时加锁
// {
// WorkArea areaBorder = DBHelp.NewDb.Queryable().First();
// if (areaBorder != null)
// {
// string workOutLinePath = Config.ProjectPath + areaBorder.PATH + "\\" + areaBorder.OUTFILENAME;
// if (File.Exists(workOutLinePath))
// {
// CsharpFileUtility.DecodeOverrideFile(workOutLinePath);
// Xy xy = new Xy();
// xy.DfdRead(workOutLinePath);
// var range = xy.GetCoordinateRange();
// CsharpFileUtility.EncodeFile(workOutLinePath);
// workBorder = (range, workOutLinePath);
// return workBorder;
// }
// }
// return (null, string.Empty);
// }
// }
//}
///
// 更新序号
//
/// 表
/// 序号列
/// 排序列
public static void UpdateTableOrder(string table, string idColumn, string orderColumn)
{
DBHelp.NewDb.Ado.ExecuteCommandAsync($@"
with tmp as (select {idColumn}, row_number() over (order by {idColumn}) rn from {table} )
update {table}
set {orderColumn} = (
select rn from tmp
where tmp.{idColumn} = {table}.{idColumn}
);
");
}
///
/// 更新井斜xy
///
public static void UpdateWellDeflectionXY()
{
DBHelp.NewDb.Ado.ExecuteCommandAsync($@"
with tmp as (select x,y,jh from well_base where jh in (select jh from well_deflection))
update well_deflection
set (x, y) = (
select
x + well_deflection.DZB,
y + well_deflection.BZB
from tmp
where tmp.jh == well_deflection.jh
);
");
// KEPDataManager.NotifyChange();
}
///
/// Gets the column to rows.
///
/// 表名
/// 返回值
/// A Dictionary.
public static Dictionary> GetCustomTableToRows(List tableData, List columnNamesToReturn = null)
{
// 并行将 List 转换为字典
Dictionary> dictionary = tableData
.AsParallel()
.GroupBy(data => data.RowGuid)
.ToDictionary(
group => group.Key,
group => group.Where(data => columnNamesToReturn != null ? columnNamesToReturn.Contains(data.ColumnName) : true).ToDictionary(data => data.ColumnName, data => data.ColumnValue));
return dictionary;
}
///
/// 获取动态表的压裂段
///
/// 条件
/// 返回列
/// 返回对象压裂段
public static List GetCustomTable(Expression> expression = null, List columnNamesToReturn = null)
{
//获取表名称
Stopwatch stopwatch = new Stopwatch();
stopwatch.Start();
var tb = DBHelp.NewDb.Queryable();
if (expression != null)
{
tb = tb.Where(expression);
}
List results = tb.ToList();
stopwatch.Stop();
Console.WriteLine($"曲线获取耗时:{stopwatch.Elapsed.TotalMilliseconds},条数:{results.Count}");
if (typeof(T) == typeof(HoriWellFracturParameter))
{
List specificResults = results.Cast().ToList();
specificResults.AsParallel().ForAll(x => x.ColumnValues = JsonConvert.DeserializeObject>(x.ColumnValue));
return specificResults.Cast().ToList();
}
else
{
List specificResults = results.Cast().ToList();
specificResults.AsParallel().ForAll(x => x.ColumnValues = JsonConvert.DeserializeObject>(x.ColumnValue));
return specificResults.Cast().ToList();
}
}
///
/// Gets the column to rows.
///
/// 条件
/// 返回值
/// A Dictionary.
//public static List GetCustomTableWellCurveT(Expression> expression = null, List columnNamesToReturn = null)
//{
// Stopwatch stopwatch = new Stopwatch();
// stopwatch.Start();
// string tableName = DBHelp.GetTableName();
// var tb = DBHelp.NewDb.Queryable().Where(x => x.TableName == tableName);
// if (expression != null)
// {
// tb = tb.Where(expression);
// }
// List tableData = tb.ToList();
// Console.WriteLine($"曲线获取耗时:{stopwatch.Elapsed.TotalMilliseconds},条数:{tableData.Count}");
// List wellCurves = tableData
// .AsParallel()
// .GroupBy(data => new { data.RowGuid, data.RowID })
// .Select(group => new WellCurve() { RowID = group.Key.RowID, RowGuid = group.Key.RowGuid, DEPTH = Convert.ToDouble(group.FirstOrDefault(x => x.ColumnName == "DEPTH").ColumnValue), JH = group.FirstOrDefault(x => x.ColumnName == "JH").ColumnValue, ColumnValues = group.Where(data => columnNamesToReturn != null ? columnNamesToReturn.Contains(data.ColumnName) : true).ToDictionary(data => data.ColumnName, data => data.ColumnValue) }).ToList();
// stopwatch.Stop();
// Console.WriteLine($"列曲线转行耗时:{stopwatch.Elapsed.TotalMilliseconds},条数:{wellCurves.Count}");
// return wellCurves;
//}
///
/// 自定义表格行
///
///
/// 传入数据
/// 返回
/// 列名
/// 搜索值
/// 返回字典
public static List> SearchCustomTableToRows(Dictionary> dictionary, List columnNamesToReturn, string columnName, List searchValues)
{
// 在字典中搜索并返回满足条件的所有行
List> searchResults = dictionary.AsParallel()
.Where(kv => kv.Value.ContainsKey(columnName) && searchValues.Contains(kv.Value[columnName]))
.Select(kv => kv.Value)
.ToList();
return searchResults;
}
///
/// 自定义表格行
///
///
/// 字典
/// 搜索值
/// List
public static List> SearchCustomTableToRows(Dictionary> dictionary, Dictionary searchKeyValues)
{
// 在字典中搜索并返回满足条件的所有行
List> searchResults = dictionary.AsParallel()
.Where(kv => kv.Value.Keys.Intersect(searchKeyValues.Keys).Count() == searchKeyValues.Keys.Count)
.Where(kv => searchKeyValues.All(entry => kv.Value.ContainsKey(entry.Key) && searchKeyValues[entry.Key] == entry.Value))
.Select(kv => kv.Value)
.ToList();
return searchResults;
}
/////
///// 获取压裂段坐标数据
/////
///// List
//public static List CalcHoriWellFracturCoord()
//{
// List stageParamtersResult = new List();
// try
// {
// string tableName = DBHelp.GetTableName();
// var columnNames = DBHelp.NewDb.Queryable().Where(x => x.TableName == tableName).Select(x => new { En = x.ColumnName, CH = x.ChineseName }).ToList();
// var lst_coords = DBHelp.NewDb.Queryable().ToList();
// List list = GetCustomTable();
// var lst_jhs = list.GroupBy(x => x.JH).Select(x => x.Key).ToList();
// string[] notColumns = { "ID", "XHID", "SYWZ1", "SYWZ2", "JH" };
// ConcurrentBag stageParamters = new ConcurrentBag();
// List wellStagePoint = new List();
// lst_coords.Select(x => new WellStagePoint()
// {
// JH = x.JH,
// Point = new DfPoint(x.X, x.Y),
// StageNo = x.XHID,
// });
// var lst_coord = lst_coords.GroupBy(x => new { x.JH, x.XHID }).Select(x => new { x.Key.JH, x.Key.XHID });
// //循环 压力段坐标分组井号和段序
// Parallel.ForEach(lst_coord, (coord) =>
// {
// var stageData = new StagePointParameterData()
// {
// JH = coord.JH,
// StageNo = coord.XHID,
// };
// var coords = lst_coords.Where(x => x.JH == coord.JH && x.XHID == coord.XHID).Select(x => new DfPoint() { X = x.X, Y = x.Y }).ToList();
// //需要判断2个以上
// stageData.StagePointRange = coords;
// //匹配压力段参数
// List lstResult = list.Where(x => x.JH == coord.JH && x.XHID == coord.XHID).ToList();
// foreach (var pairs in lstResult)
// {
// //行
// ConcurrentDictionary dicts = new ConcurrentDictionary();
// Parallel.ForEach(pairs.ColumnValues, (prop) =>
// {
// string dictName = prop.Key;
// if (!notColumns.Contains(dictName))
// {
// if (columnNames.Any(x => x.En == prop.Key))
// {
// dictName = columnNames.FirstOrDefault(x => x.En == prop.Key).CH;
// }
// double d = 0;
// string v = prop.Value.ToString();
// if (v.Contains("-"))
// {
// string[] ary = v.Split('-');
// double total = 0;
// int count = 0;
// foreach (var a in ary)
// {
// if (string.IsNullOrWhiteSpace(a))
// {
// continue;
// }
// else if (double.TryParse(a, out double d1))
// {
// count++;
// total += d1;
// }
// }
// d = total / count;
// dicts.TryAdd(dictName, d);
// }
// else if (v.Contains("~"))
// {
// string[] ary = v.Split('~');
// double total = 0;
// int count = 0;
// foreach (var a in ary)
// {
// if (string.IsNullOrWhiteSpace(a))
// {
// continue;
// }
// else if (double.TryParse(a, out double d1))
// {
// count++;
// total += d1;
// }
// }
// d = total / count;
// dicts.TryAdd(dictName, d);
// }
// else if (double.TryParse(v, out d))
// {
// dicts.TryAdd(dictName, d);
// }
// }
// });
// stageData.ParamData = dicts.ToDictionary(x => x.Key, x => x.Value);
// stageParamters.Add(stageData);
// }
// });
// var lst_jh = list.Where(x => !lst_coords.Any(c => c.JH == x.JH)).ToList();
// stageParamtersResult = stageParamters.ToList();
// if (lst_jh != null)
// {
// stageParamtersResult.AddRange(lst_jh.Select(x => new StagePointParameterData() { JH = x.JH, StageNo = "0", ParamData = new Dictionary(), StagePointRange = new List() }));
// }
// }
// catch
// {
// }
// return stageParamtersResult;
//}
///
/// 获取水平井的井支轨迹
///
//private static void CalcHoriALLWellBranchTracksAsync(Action> callback)
//{
// Task.Run(() =>
// {
// List wellStageTracks = new List();
// var wellDeflections = DBHelp.NewDb.Queryable().Select(x => new { x.JH, x.X, x.Y }).ToList();
// foreach (var item in wellDeflections.GroupBy(x => new { x.JH }).Select(x => new { x.Key.JH }))
// {
// var stageTrack = new WellStageTrack { JH = item.JH };
// var coords = wellDeflections.Where(x => x.JH == item.JH && x.X.HasValue && x.Y.HasValue).Select(x => new DfPoint() { X = x.X.Value, Y = x.Y.Value }).ToList();
// stageTrack.Points = coords;
// wellStageTracks.Add(stageTrack);
// }
// callback(wellStageTracks);
// });
//}
///
/// 获取井斜
///
/// 井号数据
/// 获取井斜LIST
public static List GetWellDeflections(List wells)
{
List wellDeflections = new List();
object obj = new object();
wells = wells.Distinct().ToList();
List tasks = new List();
foreach (var jh in wells)
{
bool isSearch = false;
lock (obj)
{
if (!dictWellDeflection.ContainsKey(jh))
{
isSearch = true;
}
}
if (isSearch)
{
tasks.Add(Task.Run(() =>
{
// 多线程访问db,使用新实例
using (var db = DBHelp.NewDb)
{
var lstDeflection = db.Queryable()
.Where(it => it.JH == jh && it.JS.HasValue)
.Select(x => new WellDeflection() { X = x.X, Y = x.Y, JS = x.JS, JH = x.JH }).ToList();
lock (obj)
{
dictWellDeflection.TryAdd(jh, lstDeflection);
wellDeflections.AddRange(lstDeflection);
}
}
}));
}
else
{
lock (obj)
{
var lst = new List();
dictWellDeflection.TryGetValue(jh, out lst);
wellDeflections.AddRange(lst);
}
}
}
Task.WaitAll(tasks.ToArray());
Console.WriteLine(string.Join(string.Empty, wells) + "OK");
return wellDeflections.ToList();
}
///
/// 获取井斜
///
/// 井号数据
/// 获取井斜LIST
public static List GetWellDeflectionsAll(List wells)
{
List wellDeflections = new List();
object obj = new object();
wells = wells.Distinct().ToList();
List tasks = new List();
foreach (var jh in wells)
{
bool isSearch = false;
lock (obj)
{
if (!dictWellDeflection.ContainsKey(jh))
{
isSearch = true;
}
}
if (isSearch)
{
tasks.Add(Task.Run(() =>
{
// 多线程访问db,使用新实例
using (var db = DBHelp.NewDb)
{
var lstDeflection = db.Queryable()
.Where(it => it.JH == jh && it.JS.HasValue).ToList();
lock (obj)
{
dictWellDeflection.TryAdd(jh, lstDeflection);
wellDeflections.AddRange(lstDeflection);
}
}
}));
}
else
{
lock (obj)
{
var lst = new List();
dictWellDeflection.TryGetValue(jh, out lst);
wellDeflections.AddRange(lst);
}
}
}
Task.WaitAll(tasks.ToArray());
Console.WriteLine(string.Join(string.Empty, wells) + "OK");
return wellDeflections.ToList();
}
///
/// 获取所有井井斜数据
///
/// 井斜LIST
public static List GetWellDeflectionsAllWell()
{
List wellDeflections = new List();
// 多线程访问db,使用新实例
using (var db = DBHelp.NewDb)
{
var lstDeflection = db.Queryable()
.Where(it => it.JS.HasValue).ToList();
wellDeflections.AddRange(lstDeflection);
}
return wellDeflections.ToList();
}
///
/// 获取井分层数据
///
/// 井号数据
/// 分层井数据LIST
public static List GetWellLayers(List wells)
{
List wellLayereds = new List();
using (var db1 = DBHelp.NewDb)
{
object obj = new object();
wells = wells.Distinct().ToList();
List tasks = new List();
foreach (var jh in wells)
{
bool isSearch = false;
lock (obj)
{
if (!dictWellLayer.ContainsKey(jh))
{
isSearch = true;
}
}
if (isSearch)
{
tasks.Add(Task.Run(() =>
{
var lstDeflection = db1.Queryable().Where(it => it.JH == jh && it.SD.HasValue).ToList();
lock (obj)
{
dictWellLayer.TryAdd(jh, lstDeflection);
wellLayereds.AddRange(lstDeflection);
}
}));
}
else
{
lock (obj)
{
var lst = new List();
dictWellLayer.TryGetValue(jh, out lst);
wellLayereds.AddRange(lst);
}
}
}
Task.WaitAll(tasks.ToArray());
}
return wellLayereds.ToList();
}
///
/// 获取所有层位
///
/// 层位数据LIST
public static List GetDistinctCWStrings()
{
using (var db = DBHelp.NewDb)
{
return db.Queryable()
.Where(it => it.CW != null)
.Select(it => it.CW)
.Distinct()
.ToList();
}
}
///
/// 获取井分层数据
///
/// 井号数据
/// 分层井数据LIST
public static List GetWellLayersNew(List wells)
{
List wellLayereds = new List();
object obj = new object();
wells = wells.Distinct().ToList();
List tasks = new List();
foreach (var jh in wells)
{
bool isSearch = false;
lock (obj)
{
if (!dictWellLayer.ContainsKey(jh))
{
isSearch = true;
}
}
if (isSearch)
{
tasks.Add(Task.Run(() =>
{
using (var db1 = DBHelp.NewDb)
{
var lstDeflection = db1.Queryable().Where(it => it.JH == jh && it.SD.HasValue).ToList();
lock (obj)
{
dictWellLayer.TryAdd(jh, lstDeflection);
wellLayereds.AddRange(lstDeflection);
}
}
}));
}
else
{
lock (obj)
{
var lst = new List();
dictWellLayer.TryGetValue(jh, out lst);
wellLayereds.AddRange(lst);
}
}
}
Task.WaitAll(tasks.ToArray());
return wellLayereds.ToList();
}
public class WellCurveData
{
///
/// 井号
///
public string JH { get; set; }
///
/// 曲线列表
///
public List CurveList { get; set; }
}
///
/// 曲线数据
///
public class CurveData
{
///
/// 曲线名称
///
public string CurveName { get; set; }
///
/// 深度
///
public double Depth { get; set; }
///
/// 曲线浮度点
///
public double Point { get; set; }
}
///
/// 获取测井曲线数据
///
/// List
///
public static List GetWellCurveDatas()
{
string tableName = DBHelp.GetTableName();
List wellCurveDatas = new List();
List lstColNames = DBHelp.NewDb.Queryable().Where(x => x.TableName == tableName).ToList();
List list = GetCustomTable();
var lst_jh = list.GroupBy(x => x.JH).Select(x => x.Key).ToList();
Parallel.ForEach(lst_jh, (jh) =>
{
var tableDatas = list.Where(x => x.JH == jh).ToList();
WellCurveData wellCurveData = new WellCurveData() { JH = jh, CurveList = new List() };
foreach (var row in tableDatas)
{
var rowData = row.ColumnValue;
if (row.DEPTH < 0)
{
continue;
}
Dictionary dict = JsonConvert.DeserializeObject>(rowData);
foreach (var data in dict)
{
if (double.TryParse(data.Value.ToString(), out double point))
{
string colName = data.Key;
if (lstColNames.Any(x => x.ColumnName == colName))
{
colName = lstColNames.FirstOrDefault(x => x.ColumnName == colName).ChineseName;
}
wellCurveData.CurveList.Add(new CurveData() { Depth = row.DEPTH, CurveName = colName, Point = point });
}
}
}
wellCurveDatas.Add(wellCurveData);
});
return wellCurveDatas;
}
///
/// 从曲线文件表里获取测井曲线数据
///
/// List
public static List GetWellCurveDatasFromFile()
{
List wellCurveDatas = new List();
//拿到每一口井的路径
List wellCurveFiles = DBHelp.NewDb.Queryable().ToList();
//拿到路径后读文件 得到datatable
foreach (var file in wellCurveFiles)
{
WellCurveData wellCurveData = new WellCurveData
{
JH = file.JH, // 井号
CurveList = new List()
};
List curveDatas = new List();
wellCurveData.JH = file.JH;
string filename = FileHelp.Convert2AbsolutePath(file.FilePath);
DataTable dataTable = new DataTable();
LasHelper.LasFileToDataTable(ref dataTable, filename, EncodingType.GetType(filename)?.BodyName, 0);
if (dataTable.Rows.Count > 0 && dataTable.Columns.Count > 1)
{
for (int i = 0; i < dataTable.Rows.Count; i++)
{
// 拿到这一行的 DEPTH 值(即第 0 列)
double depthVal = Convert.ToDouble(dataTable.Rows[i][0]);
// 从第 1 列开始,后续每一列的列名都是一个 CurveName
for (int j = 1; j < dataTable.Columns.Count; j++)
{
string curveName = dataTable.Columns[j].ColumnName;
// 当前行、当前列对应的数值
double pointVal = Convert.ToDouble(dataTable.Rows[i][j]);
// 构建 CurveData 并添加到列表
var curveData = new CurveData
{
CurveName = curveName,
Depth = depthVal,
Point = pointVal
};
wellCurveData.CurveList.Add(curveData);
}
}
}
wellCurveDatas.Add(wellCurveData);
}
return wellCurveDatas;
}
///
/// 通过井号获取 WellCurve 数据
///
/// 井号
/// WellCurveData
public static List GetWellCurvesByJH(string jh)
{
if (string.IsNullOrWhiteSpace(jh))
{
return new List();
}
try
{
// 获取井对应的文件信息
var wellCurveFile = DBHelp.NewDb.Queryable().First(item => item.JH == jh);
if (wellCurveFile == null)
{
return new List();
}
// 获取文件路径
string filename = FileHelp.Convert2AbsolutePath(wellCurveFile.FilePath);
if (!File.Exists(filename))
{
return new List();
}
// 读取 LAS 文件并转换为 DataTable
DataTable dataTable = new DataTable();
LasHelper.LasFileToDataTable(ref dataTable, filename, EncodingType.GetType(filename)?.BodyName, 0);
if (dataTable.Rows.Count == 0 || dataTable.Columns.Count <= 1)
{
return new List();
}
// 解析 DataTable 数据
return dataTable.AsEnumerable()
.Select(row => new WellCurve
{
JH = jh,
DEPTH = Convert.ToDouble(row[0]),
ColumnValues = dataTable.Columns.Cast()
.Skip(1)
.ToDictionary(col => col.ColumnName, col => row[col].ToString()),
})
.ToList();
}
catch (Exception ex)
{
Console.WriteLine($"读取井曲线数据出错: {ex.Message}");
return new List();
}
}
///
/// 列表转DataTable,用于自定义表
///
/// The list.
/// A DataTable.
public static DataTable ResTableByList(List list)
{
List> lst_pairs = new List>();
//转换
if (typeof(T) == typeof(HoriWellFracturParameter))
{
List specificResults = list.Cast().ToList();
foreach (var item in specificResults)
{
item.ColumnValues = JsonConvert.DeserializeObject>(item.ColumnValue);
var dic = item.ColumnValues;
Dictionary dynamicDictionary = new Dictionary
{
{ "JH", item.JH },
{ "XHID", item.XHID },
{ "SYWZ1", item.SYWZ1 },
{ "SYWZ2", item.SYWZ2 },
{ "RowID",item.RowID },
};
foreach (var k in dic)
{
if (!dynamicDictionary.ContainsKey(k.Key))
{
dynamicDictionary.Add(k.Key, k.Value);
}
else
{
dynamicDictionary[k.Key] = k.Value;
//这里存在错误,理论不应该出现此情况
}
}
lst_pairs.Add(dynamicDictionary);
}
}
else if (typeof(T) == typeof(WellCurve))
{
List specificResults = list.Cast().ToList();
foreach (var item in specificResults)
{
item.ColumnValues = JsonConvert.DeserializeObject>(item.ColumnValue);
var dic = item.ColumnValues;
Dictionary dynamicDictionary = new Dictionary
{
{ "JH", item.JH },
{ "DEPTH", item.DEPTH },
{ "RowID",item.RowID },
};
foreach (var k in dic)
{
dynamicDictionary.Add(k.Key, k.Value);
}
lst_pairs.Add(dynamicDictionary);
}
}
else
{
}
// 创建一个包含所有可能键的 HashSet(用于去重)
HashSet allKeys = new HashSet();
// 遍历每个字典来收集所有可能的键
foreach (var dict in lst_pairs)
{
foreach (var key in dict.Keys)
{
allKeys.Add(key);
}
}
// 创建一个新的 DataTable
DataTable dataTable = new DataTable();
// 添加所有可能的列
foreach (var key in allKeys)
{
dataTable.Columns.Add(key, typeof(string));
}
// 将字典中的每个键值对添加到表中
foreach (var dict in lst_pairs)
{
DataRow row = dataTable.NewRow();
foreach (var kvp in dict)
{
row[kvp.Key] = kvp.Value;
}
dataTable.Rows.Add(row);
}
return dataTable;
}
///
/// 通过井号获取 WellCurve 数据
///
/// 井号
/// WellCurveData
public static List GetWellCurvesStream(string jh)
{
if (string.IsNullOrWhiteSpace(jh))
{
return new List();
}
// 获取井对应的文件信息
var wellCurveFile = DBHelp.NewDb.Queryable().First(item => item.JH == jh);
if (wellCurveFile == null)
{
return new List();
}
// 获取文件路径
string filename = FileHelp.Convert2AbsolutePath(wellCurveFile.FilePath);
if (!File.Exists(filename))
{
return new List();
}
return LasHelper.LasFileToList(jh, filename, EncodingType.GetType(filename)?.BodyName).ToList();
}
public static Dictionary SplitDataTableByColumn(DataTable originalTable, string columnName)
{
Dictionary groupedTables = new Dictionary();
foreach (DataRow row in originalTable.Rows)
{
string columnValue = row[columnName].ToString();
if (!groupedTables.ContainsKey(columnValue))
{
DataTable newTable = originalTable.Clone();
groupedTables[columnValue] = newTable;
}
groupedTables[columnValue].ImportRow(row);
}
return groupedTables;
}
///
/// 同步储层数据 (有效厚度发生变化)
///
public static void SyncReservoirData(string v_jh, ProgressCallbackThree progressCallback = null)
{
try
{
List arys = new List();
if (typeof(T) == typeof(CcsjKxd))
{
arys = new List() { "kxd", "bhd" };
}
else if (typeof(T) == typeof(CcsjBhd))
{
arys = new List() { "bhd" };
}
else if (typeof(T) == typeof(CcsjStl))
{
arys = new List() { "stl" };
}
else
{
arys = new List() { "kxd", "bhd", "stl" };
}
//获取有效厚度
List yxhds = DBHelp.NewDb.Queryable().WhereIF(!string.IsNullOrWhiteSpace(v_jh), x => x.JH == v_jh).ToList();
Dictionary> dict_yxhds = yxhds.GroupBy(x => x.JH).ToDictionary(x => x.Key, x => x.ToList());
List kxds = DBHelp.NewDb.Queryable().WhereIF(!string.IsNullOrWhiteSpace(v_jh), x => x.JH == v_jh).ToList();
if (arys.Contains("kxd"))
{
int processedCount = 0;
int totalCount = kxds.Count;
Parallel.ForEach(kxds, kxd =>
{
if (dict_yxhds.TryGetValue(kxd.JH, out List hds))
{
var hd = hds.Where(x => x.CW == kxd.CW);
if (kxd.DJSD1.HasValue)
{
hd = hd.Where(x => x.DJSD1 == kxd.DJSD1);
}
else
{
hd = hd.Where(x => x.YXCS_T == kxd.YXCS_T);
}
if (hd.Count() > 0)
{
if (kxd.YXHD == null)
{
kxd.YXHD = hd.Average(x => x.YXHD);
}
if (kxd.YXHD_CS == null)
{
kxd.YXHD_CS = hd.Average(x => x.YXCS_HD);
}
}
}
Interlocked.Increment(ref processedCount);
progressCallback?.Invoke("同步有效到孔隙度", processedCount, totalCount);
});
DBHelp.NewDb.Fastest().BulkUpdateAsync(kxds, new string[] { "JH", "CW", "ID" }, new string[] { "YXHD", "YXHD_CS" });
}
List bhds = new List();
if (arys.Contains("bhd"))
{
bhds = DBHelp.NewDb.Queryable().WhereIF(!string.IsNullOrWhiteSpace(v_jh), x => x.JH == v_jh).ToList();
int processedCount = 0;
int totalCount = bhds.Count;
Parallel.ForEach(bhds, bhd =>
{
if (dict_yxhds.TryGetValue(bhd.JH, out List hds))
{
var hd = hds.Where(x => x.CW == bhd.CW);
if (bhd.DJSD1.HasValue)
{
hd = hd.Where(x => x.DJSD1 == bhd.DJSD1);
}
else
{
hd = hd.Where(x => x.YXCS_T == bhd.YXCS_T);
}
if (hd.Count() > 0)
{
if (bhd.YXHD == null)
{
bhd.YXHD = hd.Average(x => x.YXHD);
}
if (bhd.YXHD_CS == null)
{
bhd.YXHD_CS = hd.Average(x => x.YXCS_HD);
}
var kx = kxds.Where(x => x.JH == bhd.JH && x.CW == bhd.CW);
if (bhd.DJSD1.HasValue)
{
kx = kx.Where(x => x.DJSD1 == bhd.DJSD1);
}
else
{
kx = kx.Where(x => x.YXCS_T == bhd.YXCS_T);
}
if (kx.Count() > 0)
{
bhd.KXD = kx.Average(x => x.KXD);
}
}
}
Interlocked.Increment(ref processedCount);
progressCallback?.Invoke("同步有效和孔隙度到饱和度", processedCount, totalCount);
});
DBHelp.NewDb.Fastest().BulkUpdateAsync(bhds, new string[] { "JH", "CW", "ID" }, new string[] { "YXHD", "YXHD_CS", "KXD" });
}
List stls = new List();
if (arys.Contains("stl"))
{
stls = DBHelp.NewDb.Queryable().WhereIF(!string.IsNullOrWhiteSpace(v_jh), x => x.JH == v_jh).ToList();
int processedCount = 0;
int totalCount = stls.Count;
Parallel.ForEach(stls, stl =>
{
if (dict_yxhds.TryGetValue(stl.JH, out List hds))
{
var hd = hds.Where(x => x.CW == stl.CW);
if (stl.DJSD1.HasValue)
{
hd = hd.Where(x => x.DJSD1 == stl.DJSD1);
}
else
{
hd = hd.Where(x => x.YXCS_T == stl.YXCS_T);
}
if (hd.Count() > 0)
{
if (stl.YXHD == null)
{
stl.YXHD = hd.Average(x => x.YXHD);
}
if (stl.YXHD_CS == null)
{
stl.YXHD_CS = hd.Average(x => x.YXCS_HD);
}
}
}
Interlocked.Increment(ref processedCount);
progressCallback?.Invoke("同步有效到渗透率", processedCount, totalCount);
});
DBHelp.NewDb.Fastest().BulkUpdateAsync(stls, new string[] { "JH", "CW", "ID" }, new string[] { "YXHD", "YXHD_CS" });
}
}
catch (Exception)
{
// ignore Exception
}
}
}
}