636 lines
22 KiB
C#
636 lines
22 KiB
C#
using System;
|
|
using System.Collections.Generic;
|
|
using System.Data;
|
|
using System.Linq;
|
|
using System.Reflection;
|
|
using System.Security.Cryptography;
|
|
using System.Text;
|
|
using System.Web.UI.WebControls;
|
|
using ADODB;
|
|
using GetComObj;
|
|
using log4net;
|
|
using MSXML2;
|
|
using Newtonsoft.Json.Linq;
|
|
using UFIDA.U8.Framework.Api.Common.Entity;
|
|
using UFIDA.U8.Framework.Api.ControlObject.Entity;
|
|
using UFIDA.U8.Framework.Api.ControlObject.Json;
|
|
|
|
namespace UFIDA.U8.Framework.Api.ControlObject.Common
|
|
{
|
|
public static class CommonHelper
|
|
{
|
|
/// <summary>
|
|
/// 日志组件
|
|
/// </summary>
|
|
private static readonly ILog logInfo = LogManager.GetLogger("logInfo");
|
|
|
|
public static void SetPropValue(this PropertyInfo prop, object obj, object value)
|
|
{
|
|
if (prop.PropertyType == typeof(string))
|
|
{
|
|
prop.SetValue(obj, value);
|
|
return;
|
|
}
|
|
|
|
if (prop.PropertyType == typeof(DateTime))
|
|
{
|
|
prop.SetValue(obj, DateTime.Parse($"{value}"));
|
|
return;
|
|
}
|
|
|
|
if (prop.PropertyType == typeof(byte))
|
|
{
|
|
prop.SetValue(obj, byte.Parse($"{value}"));
|
|
return;
|
|
}
|
|
|
|
if (prop.PropertyType == typeof(int))
|
|
{
|
|
prop.SetValue(obj, int.Parse($"{value}"));
|
|
return;
|
|
}
|
|
|
|
if (prop.PropertyType == typeof(long))
|
|
{
|
|
prop.SetValue(obj, long.Parse($"{value}"));
|
|
return;
|
|
}
|
|
|
|
if (prop.PropertyType == typeof(decimal))
|
|
{
|
|
prop.SetValue(obj, decimal.Parse($"{value}"));
|
|
return;
|
|
}
|
|
|
|
if (prop.PropertyType == typeof(double))
|
|
{
|
|
prop.SetValue(obj, double.Parse($"{value}"));
|
|
return;
|
|
}
|
|
|
|
if (prop.PropertyType == typeof(float))
|
|
{
|
|
prop.SetValue(obj, float.Parse($"{value}"));
|
|
return;
|
|
}
|
|
|
|
if (prop.PropertyType == typeof(short))
|
|
{
|
|
prop.SetValue(obj, short.Parse($"{value}"));
|
|
return;
|
|
}
|
|
}
|
|
|
|
//public static void SetPropValue(this PropertyInfo prop, VoucherData obj, object value)
|
|
//{
|
|
// if (prop.PropertyType == typeof(string))
|
|
// {
|
|
// prop.SetValue(obj, value);
|
|
// return;
|
|
// }
|
|
|
|
// if (prop.PropertyType == typeof(DateTime))
|
|
// {
|
|
// prop.SetValue(obj, DateTime.Parse($"{value}"));
|
|
// return;
|
|
// }
|
|
|
|
// if (prop.PropertyType == typeof(byte))
|
|
// {
|
|
// prop.SetValue(obj, byte.Parse($"{value}"));
|
|
// return;
|
|
// }
|
|
|
|
// if (prop.PropertyType == typeof(int))
|
|
// {
|
|
// prop.SetValue(obj, int.Parse($"{value}"));
|
|
// return;
|
|
// }
|
|
|
|
// if (prop.PropertyType == typeof(long))
|
|
// {
|
|
// prop.SetValue(obj, long.Parse($"{value}"));
|
|
// return;
|
|
// }
|
|
|
|
// if (prop.PropertyType == typeof(decimal))
|
|
// {
|
|
// prop.SetValue(obj, decimal.Parse($"{value}"));
|
|
// return;
|
|
// }
|
|
|
|
// if (prop.PropertyType == typeof(double))
|
|
// {
|
|
// prop.SetValue(obj, double.Parse($"{value}"));
|
|
// return;
|
|
// }
|
|
|
|
// if (prop.PropertyType == typeof(float))
|
|
// {
|
|
// prop.SetValue(obj, float.Parse($"{value}"));
|
|
// return;
|
|
// }
|
|
|
|
// if (prop.PropertyType == typeof(short))
|
|
// {
|
|
// prop.SetValue(obj, short.Parse($"{value}"));
|
|
// return;
|
|
// }
|
|
|
|
// //if (prop.PropertyType == typeof(object))
|
|
// //{
|
|
// // switch (prop.Name)
|
|
// // {
|
|
// // case "cDefine1":
|
|
// // obj.set_cDefine1($"{value}");
|
|
// // break;
|
|
// // case "cDefine2":
|
|
// // obj.set_cDefine2($"{value}");
|
|
// // break;
|
|
// // case "cDefine3":
|
|
// // obj.set_cDefine3($"{value}");
|
|
// // break;
|
|
// // case "cDefine4":
|
|
// // obj.set_cDefine4(DateTime.Parse($"{value}"));
|
|
// // break;
|
|
// // case "cDefine5":
|
|
// // obj.set_cDefine5(int.Parse($"{value}"));
|
|
// // break;
|
|
// // case "cDefine6":
|
|
// // obj.set_cDefine6(DateTime.Parse($"{value}"));
|
|
// // break;
|
|
// // case "cDefine7":
|
|
// // obj.set_cDefine7(float.Parse($"{value}"));
|
|
// // break;
|
|
// // case "cDefine8":
|
|
// // obj.set_cDefine8($"{value}");
|
|
// // break;
|
|
// // case "cDefine9":
|
|
// // obj.set_cDefine9($"{value}");
|
|
// // break;
|
|
// // case "cDefine10":
|
|
// // obj.set_cDefine10($"{value}");
|
|
// // break;
|
|
// // case "cDefine11":
|
|
// // obj.set_cDefine11($"{value}");
|
|
// // break;
|
|
// // case "cDefine12":
|
|
// // obj.set_cDefine12($"{value}");
|
|
// // break;
|
|
// // case "cDefine13":
|
|
// // obj.set_cDefine13($"{value}");
|
|
// // break;
|
|
// // case "cDefine14":
|
|
// // obj.set_cDefine14($"{value}");
|
|
// // break;
|
|
// // case "cDefine15":
|
|
// // obj.set_cDefine15(int.Parse($"{value}"));
|
|
// // break;
|
|
// // case "cDefine16":
|
|
// // obj.set_cDefine16(float.Parse($"{value}"));
|
|
// // break;
|
|
// // default:
|
|
// // prop.SetValue(obj, $"{value}");
|
|
// // break;
|
|
|
|
// // }
|
|
// //}
|
|
//}
|
|
|
|
public static IXMLDOMDocument2 ToDocument2(this Dictionary<string,object> dictionary,IXMLDOMDocument2 domDocument2=null)
|
|
{
|
|
|
|
if (domDocument2 == null)
|
|
{
|
|
domDocument2 = new DOMDocument();
|
|
}
|
|
|
|
var root = domDocument2.ClearDocument();
|
|
|
|
var newRow = SetRowData(dictionary, domDocument2);
|
|
|
|
root.appendChild(newRow);
|
|
|
|
return domDocument2;
|
|
|
|
}
|
|
|
|
public static IXMLDOMNode SetRowData(this Dictionary<string, object> dictionary, IXMLDOMDocument2 domDocument2 = null,string bodyKey="body")
|
|
{
|
|
if (domDocument2 == null)
|
|
{
|
|
domDocument2 = new DOMDocument();
|
|
}
|
|
|
|
var newRow = domDocument2.createElement("z:row");
|
|
|
|
foreach (var key in dictionary.Keys)
|
|
{
|
|
if(key==bodyKey)
|
|
continue;
|
|
|
|
var attr = domDocument2.createAttribute(key);
|
|
|
|
attr.value = dictionary[key];
|
|
|
|
newRow.attributes.setNamedItem(attr);
|
|
}
|
|
|
|
return newRow;
|
|
}
|
|
|
|
public static IXMLDOMDocument2 ToDocument2(this List<Dictionary<string, object>> dictionaryList, IXMLDOMDocument2 domDocument2 = null)
|
|
{
|
|
|
|
if (domDocument2 == null)
|
|
{
|
|
domDocument2 = new DOMDocument();
|
|
}
|
|
|
|
var root = domDocument2.ClearDocument();
|
|
|
|
foreach (var dictionary in dictionaryList)
|
|
{
|
|
var newRow = SetRowData(dictionary, domDocument2);
|
|
|
|
root.appendChild(newRow);
|
|
}
|
|
|
|
return domDocument2;
|
|
|
|
}
|
|
|
|
public static IXMLDOMNode ClearDocument(this IXMLDOMDocument2 domDocument2)
|
|
{
|
|
var root = domDocument2.selectSingleNode("//rs:data");
|
|
|
|
if (root == null)
|
|
{
|
|
root = domDocument2.createElement("rs:data");
|
|
|
|
domDocument2.appendChild(root);
|
|
}
|
|
|
|
var rows = domDocument2.selectNodes("//z:row");
|
|
|
|
if (rows != null && rows.length > 0)
|
|
{
|
|
for (var i = 0; i < rows.length; i++)
|
|
{
|
|
var row = rows[i];
|
|
|
|
root.removeChild(row);
|
|
}
|
|
}
|
|
|
|
return root;
|
|
}
|
|
|
|
public static object SetDomKeyValue(this IXMLDOMDocument2 domDocument2,string key,object value)
|
|
{
|
|
var node= domDocument2.selectSingleNode("//z:row");
|
|
|
|
if (node == null)
|
|
return null;
|
|
|
|
var attr = domDocument2.createAttribute(key);
|
|
|
|
attr.nodeValue=value;
|
|
|
|
node.attributes.setNamedItem(attr);
|
|
|
|
|
|
return value;
|
|
}
|
|
|
|
public static void ResetDictionary(this Dictionary<string, object> dictionary, string bodyKey = "body")
|
|
{
|
|
if (dictionary.ContainsKey(bodyKey))
|
|
{
|
|
var body= dictionary[bodyKey].ToJson().ToList<Dictionary<string,object>>();
|
|
|
|
dictionary[bodyKey] = body;
|
|
}
|
|
}
|
|
|
|
public static dynamic GetComObject(this string comName)
|
|
{
|
|
var comInstance = new GetComInstance();
|
|
|
|
var obj = comInstance.GetComObject(ref comName);
|
|
|
|
return obj;
|
|
}
|
|
|
|
public static string U8Encrypt(string input)
|
|
{
|
|
var rethash = "";
|
|
|
|
var hash = SHA1.Create();
|
|
|
|
var encoder = new ASCIIEncoding();
|
|
|
|
var combined = encoder.GetBytes(input);
|
|
|
|
hash.ComputeHash(combined);
|
|
|
|
rethash = Convert.ToBase64String(hash.Hash);
|
|
|
|
if (string.IsNullOrWhiteSpace(rethash))
|
|
return rethash;
|
|
|
|
|
|
return rethash + (char)3;
|
|
}
|
|
|
|
/// <summary>
|
|
/// 获取币种汇率
|
|
/// </summary>
|
|
/// <param name="dDate">日期</param>
|
|
/// <param name="cAccId">账套</param>
|
|
/// <param name="exchName">币种</param>
|
|
/// <param name="conn">连接对象</param>
|
|
/// <returns></returns>
|
|
public static Exch GetExch(DateTime dDate, string cAccId, string exchName, Connection conn)
|
|
{
|
|
var strSql =
|
|
$@"SELECT a.cexch_name,a.bcal,a.idec,round(convert(decimal(28,20),a.nerror),a.idec) nerror,b.itype,a.iotherused,b.nflat
|
|
FROM foreigncurrency a
|
|
left join exch b on b.cexch_name=a.cexch_name
|
|
left join ufsystem.dbo.UA_Period c on c.iYear=b.iyear and c.iId=b.iperiod and cAcc_Id='{cAccId}' and bIsDelete=0
|
|
WHERE (a.iotherused=-1 or (b.itype=2 and c.dBegin>='{dDate:yyyy-MM-dd}' and c.dEnd<='{dDate:yyyy-MM-dd}')) and a.cexch_name='{exchName}'
|
|
order by b.itype ";
|
|
|
|
var rs = new Recordset();
|
|
rs.Open(strSql, conn); //查询档案
|
|
|
|
if (rs.BOF || rs.EOF)
|
|
throw new Exception($"当前币种:{exchName}在当前会计期间未设置汇率");
|
|
|
|
rs.MoveFirst();
|
|
|
|
var exch = new Exch
|
|
{
|
|
cexch_name = $"{rs.Fields["cexch_name"].Value}"
|
|
};
|
|
|
|
if (rs.Fields["bcal"].Value != null)
|
|
{
|
|
exch.bcal = Convert.ToBoolean(rs.Fields["bcal"].Value);
|
|
}
|
|
|
|
if (rs.Fields["idec"].Value != null)
|
|
{
|
|
exch.idec = Convert.ToInt16(rs.Fields["idec"].Value);
|
|
}
|
|
|
|
if (rs.Fields["nerror"].Value != null)
|
|
{
|
|
exch.nerror = Convert.ToDecimal(rs.Fields["nerror"].Value);
|
|
}
|
|
|
|
if (rs.Fields["iotherused"].Value != null)
|
|
{
|
|
exch.iotherused = Convert.ToInt32(rs.Fields["iotherused"].Value);
|
|
}
|
|
|
|
if (rs.Fields["nflat"].Value != null && !string.IsNullOrWhiteSpace($"{rs.Fields["nflat"].Value}"))
|
|
{
|
|
exch.nflat = Convert.ToDecimal(rs.Fields["nflat"].Value);
|
|
}
|
|
|
|
return exch;
|
|
|
|
}
|
|
|
|
/// <summary>
|
|
/// 设置字典对象数据
|
|
/// </summary>
|
|
/// <param name="dict">字典对象</param>
|
|
/// <param name="defs">实体字段配置信息</param>
|
|
/// <param name="conn">数据库连接对象</param>
|
|
/// <param name="newCode">新单号</param>
|
|
/// <param name="rowNo">行号</param>
|
|
/// <param name="bUpdate">是否更新</param>
|
|
/// <returns></returns>
|
|
public static Dictionary<string, object> SetDictionaryData(this Dictionary<string, object> dict, List<SysU8FieldDefine> defs, Connection conn = null, string newCode = "", int rowNo = 1, Exch exch = null, bool bUpdate = false)
|
|
{
|
|
var adef = defs.Where(s => s.bDefault ?? false).ToList();
|
|
//var codeField = "";
|
|
if (adef.Count > 0 && !bUpdate)
|
|
{
|
|
foreach (var def in adef)
|
|
{
|
|
switch (def.DefaultValue)
|
|
{
|
|
case "当前日期":
|
|
dict[def.FieldName] = $"{DateTime.Today:yyyy-MM-dd}";
|
|
break;
|
|
case "当前日期时间":
|
|
dict[def.FieldName] = $"{DateTime.Now:yyyy-MM-dd HH:mm:ss.fff}";
|
|
break;
|
|
case "单号":
|
|
dict[def.FieldName] = newCode; //一直取不到,怀疑是带字段的关系,需要后续研究后确定
|
|
//codeField = def.FieldName;
|
|
break;
|
|
case "行号":
|
|
dict[def.FieldName] = rowNo;
|
|
break;
|
|
case "汇率":
|
|
if (exch?.nflat != null && string.IsNullOrWhiteSpace($"{dict[def.FieldName]}"))
|
|
{
|
|
dict[def.FieldName] = exch.nflat;
|
|
}
|
|
|
|
if (exch?.iotherused == -1)
|
|
{
|
|
dict[def.FieldName] = 1;
|
|
}
|
|
|
|
break;
|
|
default:
|
|
if (!dict.ContainsKey(def.FieldName) || string.IsNullOrWhiteSpace($"{dict[def.FieldName]}"))
|
|
dict[def.FieldName] = def.DefaultValue ?? "";
|
|
break;
|
|
}
|
|
}
|
|
}
|
|
|
|
//查找档案关联的字段并赋值
|
|
var keys = dict.Keys.ToList();
|
|
|
|
foreach (var key in keys)
|
|
{
|
|
var bdef = defs.Where(s => s.ModelName == key && (s.bArchives ?? false)).ToList(); //获取当前字段的关联信息
|
|
|
|
if (bdef.Count <= 0) continue;
|
|
|
|
var archivesTablesList = bdef.Select(s => s.ArchivesTable).Distinct().ToList(); //合并关联的档案表
|
|
|
|
if (archivesTablesList.Count <= 0) continue;
|
|
|
|
foreach (var archives in archivesTablesList) //根据档案表查询
|
|
{
|
|
var fieldList = bdef.Where(s => s.ArchivesTable == archives)
|
|
.Select(s => new { s.FieldName, s.ArchivesField }).ToList(); //获取需要查询的字段
|
|
var field = bdef.Where(s => s.ArchivesTable == archives).Select(s => s.ArchivesSource).First(); //获取档案与实体的关联字段
|
|
var strField = string.Join(",", fieldList.Select(s => s.ArchivesField)); //组装需要查询的字段
|
|
|
|
var strSql = $"SELECT {strField} FROM {archives} WHERE {field}='{dict[key]}'"; //构建查询语句
|
|
|
|
var rs = new Recordset();
|
|
rs.Open(strSql, conn); //查询档案
|
|
|
|
if (rs.BOF || rs.EOF) continue; //无数据不赋值
|
|
|
|
rs.MoveFirst();
|
|
foreach (var f in fieldList)
|
|
{
|
|
dict[f.FieldName] = $"{rs.Fields[f.ArchivesField].Value}"; //根据字段值赋值
|
|
}
|
|
}
|
|
}
|
|
|
|
var cdef = defs.Where(s => s.bCalc ?? false).OrderBy(s => s.iSort).ToList(); //获取计算列的配置
|
|
|
|
if (cdef.Count > 0)
|
|
{
|
|
foreach (var def in cdef)
|
|
{
|
|
var calcFormula = def.CalcFormula;
|
|
var cPrecision = def.cPrecision;
|
|
foreach (var key in dict.Keys.Where(key => calcFormula.Contains($"{{{key}}}"))) //遍历字段来替换公式中的参数
|
|
{
|
|
calcFormula = calcFormula.Replace($"{{{key}}}", $"{dict[key]}");
|
|
}
|
|
|
|
//汇率单独处理
|
|
if (calcFormula.Contains("{汇率:B}") || calcFormula.Contains("{汇率:Y}"))
|
|
{
|
|
var YB = "B";
|
|
var pp = "*";
|
|
if (calcFormula.Contains("{汇率:Y}"))
|
|
{
|
|
YB = "Y";
|
|
}
|
|
|
|
var nflat = 1m;
|
|
if (exch != null)
|
|
{
|
|
if (exch.iotherused != -1)
|
|
{
|
|
if (exch.idec != null)
|
|
cPrecision = Convert.ToInt32(exch.idec);
|
|
|
|
nflat = exch.nflat ?? 1;
|
|
|
|
if (YB == "Y" && !(exch.bcal ?? false))
|
|
{
|
|
pp = "/";
|
|
}
|
|
|
|
if (YB == "B" && (exch.bcal ?? false))
|
|
{
|
|
pp = "/";
|
|
}
|
|
}
|
|
else
|
|
{
|
|
cPrecision = null;
|
|
}
|
|
}
|
|
|
|
calcFormula = calcFormula.Replace("{汇率:B}", $"{pp}{nflat}");
|
|
calcFormula = calcFormula.Replace("{汇率:Y}", $"{pp}{nflat}");
|
|
}
|
|
|
|
try
|
|
{
|
|
var dt = new DataTable();
|
|
|
|
var val = cPrecision == null
|
|
? dt.Compute(calcFormula, "")
|
|
: Math.Round(Convert.ToDecimal(dt.Compute(calcFormula, "")), cPrecision ?? 6); //计算公式结果
|
|
|
|
dict[def.FieldName] = val;
|
|
}
|
|
catch (Exception ex)
|
|
{
|
|
logInfo.Debug($"字段:{def.FieldName}公式解析失败,公式结果:{calcFormula}");
|
|
}
|
|
|
|
|
|
}
|
|
}
|
|
|
|
return dict;
|
|
}
|
|
|
|
|
|
/// <summary>
|
|
/// 把对象转换为Boolean类型
|
|
/// </summary>
|
|
/// <param name="obj">需要转换的对象</param>
|
|
/// <returns></returns>
|
|
public static bool ToBoolean(this object obj)
|
|
{
|
|
return bool.TryParse($"{obj}", out var re) && re;
|
|
}
|
|
|
|
/// <summary>
|
|
/// 把对象转换为Int类型
|
|
/// </summary>
|
|
/// <param name="obj">需要转换的对象</param>
|
|
/// <returns></returns>
|
|
public static int ToShort(this object obj)
|
|
{
|
|
if (!short.TryParse($"{obj}", out var re))
|
|
throw new InvalidCastException($"值:{obj}无法转换为短数字格式!");
|
|
|
|
return re;
|
|
}
|
|
|
|
/// <summary>
|
|
/// 把对象转换为Int类型
|
|
/// </summary>
|
|
/// <param name="obj">需要转换的对象</param>
|
|
/// <returns></returns>
|
|
public static int ToInt(this object obj)
|
|
{
|
|
if (!int.TryParse($"{obj}", out var re))
|
|
throw new InvalidCastException($"值:{obj}无法转换为数字格式!");
|
|
|
|
return re;
|
|
}
|
|
|
|
/// <summary>
|
|
///
|
|
/// </summary>
|
|
/// <param name="length"></param>
|
|
/// <param name="useNum"></param>
|
|
/// <param name="useLow"></param>
|
|
/// <param name="useUpp"></param>
|
|
/// <param name="useSpe"></param>
|
|
/// <param name="customerStr"></param>
|
|
/// <returns></returns>
|
|
public static string GenRandomString(int length, bool useNum, bool useLow, bool useUpp, bool useSpe,
|
|
string customerStr)
|
|
{
|
|
var b = new byte[4];
|
|
new RNGCryptoServiceProvider().GetBytes(b);
|
|
var r = new Random(BitConverter.ToInt32(b, 0));
|
|
string s = null, str = customerStr;
|
|
if (useNum) str += "0123456789";
|
|
if (useLow) str += @"abcdefghijklmnopqrstuvwxyz";
|
|
if (useUpp) str += @"ABCDEFGHIJKLMNOPQRSTUVWXYZ";
|
|
if (useSpe) str += "!\"#$%&'()*+,-./:;<=>?@[\\]^_`{|}~";
|
|
for (var i = 0; i < length; i++)
|
|
{
|
|
s += str.Substring(r.Next(0, str.Length - 1), 1);
|
|
}
|
|
|
|
return s;
|
|
}
|
|
}
|
|
}
|