using Core.Mes.Client.Comm.Tool;
using CoreFS.CA06;
using CoreFS.SA06;
using System;
using System.Collections;
using System.Collections.Generic;
using System.Data;
using System.Diagnostics;
using System.Reflection;
namespace Core.StlMes.Client.Judge.Commons
{
public class Dal
{
private OpeBase _ob = null;
public OpeBase Ob
{
get { return _ob; }
set { _ob = value; }
}
public Dal(OpeBase ob)
{
_ob = ob;
}
public DataTable GetTable(string remoteStr, params object[] param)
{
CoreClientParam ccp = new CoreClientParam
{
ServerName = remoteStr.Substring(0, remoteStr.LastIndexOf(".")),
MethodName = remoteStr.Substring(remoteStr.LastIndexOf(".") + 1),
ServerParams = param
};
ccp = _ob.ExecuteSortResultByQueryToDataTable(ccp, CoreInvokeType.Internal);
DataTable src = ccp.SourceDataTable;
foreach (DataColumn col in src.Columns)
{
col.ColumnName = GetLanguageFormat(col.ColumnName, true);
}
return src;
}
///
/// 直接通过服务端xmlId获取数据
///
///
///
///
public DataTable GetTableByXmlId(string xmlId, params object[] parms)
{
CoreClientParam ccp = new CoreClientParam();
ccp.ServerName = "com.steering.pss.judge.comm.ClientQueryHelper";
ccp.MethodName = "query";
ccp.ServerParams = new object[] { xmlId, parms };
ccp = _ob.ExecuteSortResultByQueryToDataTable(ccp, CoreInvokeType.Internal);
DataTable src = ccp.SourceDataTable;
if (src != null)
{
foreach (DataColumn col in src.Columns)
{
col.ColumnName = GetLanguageFormat(col.ColumnName, true);
}
}
return src;
}
public CoreClientParam Set(string remoteStr, params object[] param)
{
CoreClientParam ccp = new CoreClientParam
{
IfShowErrMsg = false,
ServerName = remoteStr.Substring(0, remoteStr.LastIndexOf(".")),
MethodName = remoteStr.Substring(remoteStr.LastIndexOf(".") + 1),
ServerParams = param
};
ccp = _ob.ExecuteNonQuery(ccp, CoreInvokeType.Internal);
return ccp;
}
///
/// 直接通过服务端xmlId获取数据
///
///
///
///
public DataRow GetRow(string remoteStr, params object[] param)
{
DataTable dt = GetTable(remoteStr, param);
if (dt.Rows.Count == 0)
{
return null;
}
else
{
return dt.Rows[0];
}
}
public DataRow GetRowByXmlId(string xmlId, params object[] parms)
{
DataTable dt = GetTableByXmlId(xmlId, parms);
if (dt.Rows.Count == 0)
{
return null;
}
else
{
return dt.Rows[0];
}
}
private string GetLanguageFormat(string strName, bool isLowerBegin)
{
string[] strs = strName.Split('_');
strName = "";
for (int i = 0; i < strs.Length; i++)
{
if (i == 0 && isLowerBegin)
{
strName += strs[i].ToLower();
}
else
{
strName += strs[i].Substring(0, 1).ToUpper() + strs[i].Substring(1).ToLower();
}
}
return strName;
}
public DataSourceList GetEntitysByXmlId(string xmlId, params object[] parms)
{
return GetEntitys("com.steering.pss.judge.comm.ClientQueryHelper.query", new object[] { xmlId, parms });
}
public DataSourceList GetEntitys(string remoteStr, params object[] parms)
{
string key;
try
{
DataSourceList listResult = new DataSourceList();
CoreClientParam ccp = new CoreClientParam
{
ServerName = remoteStr.Substring(0, remoteStr.LastIndexOf(".")),
MethodName = remoteStr.Substring(remoteStr.LastIndexOf(".") + 1),
ServerParams = parms
};
ccp = _ob.ExecuteQuery(ccp, CoreInvokeType.Internal);
string[] strColumns = (ccp.ReturnObject as CoreReturnSortObject).getColIdx();
string[] strColumnsEntity = ConvertColumnName(strColumns);
ArrayList list = (ccp.ReturnObject as CoreReturnSortObject).getResult();
Type type = typeof(T);
Dictionary propertyInfoDic = new Dictionary();
for (int j = 0; j < strColumns.Length; j++)
{
string column = strColumns[j];
string columnEntity = strColumnsEntity[j];
PropertyInfo propertyInfo = type.GetProperty(columnEntity);
if (propertyInfo == null) continue;
if (propertyInfoDic.ContainsKey(column))
{
propertyInfoDic[column] = propertyInfo;
}
else
{
propertyInfoDic.Add(column, propertyInfo);
}
}
for (int i = 0; i < list.Count; i++)
{
Hashtable hashtable = (Hashtable)list[i];
ArrayList list2 = new ArrayList();
T obj = (T)type.Assembly.CreateInstance(type.FullName);
foreach (KeyValuePair propertyInfoKV in propertyInfoDic)
{
key = propertyInfoKV.Key;
object obj2 = hashtable[propertyInfoKV.Key];
PropertyInfo propertyInfo = propertyInfoKV.Value;
if ((obj2 != null) && typeof(Hashtable).Equals(obj2.GetType()))
{
Hashtable hashtable2 = (Hashtable)obj2;
propertyInfo.SetValue(obj, ConvertDataType(propertyInfo.PropertyType, hashtable2["value"]), null);
}
else
{
propertyInfo.SetValue(obj, ConvertDataType(propertyInfo.PropertyType, obj2), null);
}
}
listResult.Add(obj);
}
return listResult;
}
catch (Exception ex)
{
Debug.WriteLine(ex.Message);
throw;
}
finally
{
}
}
private static object ConvertDataType(Type type, object objValue)
{
if (type == typeof(bool))
{
bool result;
if (bool.TryParse(objValue.ToString(), out result))
{
return result;
}
else
{
return false;
}
}
else if (type == typeof(bool?))
{
bool result;
if (bool.TryParse(objValue.ToString(), out result))
{
return result;
}
else
{
return null;
}
}
else if (type == typeof(int?))
{
if (objValue == null)
{
return null;
}
else
{
return int.Parse(objValue.ToString());
}
}
else if (type == typeof(long?))
{
if (objValue == null)
{
return null;
}
else
{
return long.Parse(objValue.ToString());
}
}
else if (type == typeof(short?))
{
if (objValue == null)
{
return null;
}
else
{
return short.Parse(objValue.ToString());
}
}
else if (type == typeof(float?))
{
if (objValue == null)
{
return null;
}
else
{
return float.Parse(objValue.ToString());
}
}
else if (type == typeof(double?))
{
if (objValue == null)
{
return null;
}
else
{
return double.Parse(objValue.ToString());
}
}
else if (type == typeof(decimal?))
{
if (objValue == null)
{
return null;
}
else
{
return decimal.Parse(objValue.ToString());
}
}
else if (type == typeof(DateTime?))
{
if (objValue == null)
{
return null;
}
else
{
return DateTime.Parse(objValue.ToString());
}
}
else
{
return objValue == null ? "" : objValue.ToString();
}
}
private string[] ConvertColumnName(string[] strColumns)
{
string[] strs = new string[strColumns.Length];
for (int i = 0; i < strColumns.Length; i++)
{
strs[i] = GetLanguageFormat(strColumns[i], false);
}
return strs;
}
public string GetJsonData(string remoteStr, params object[] param)
{
CoreClientParam ccp = new CoreClientParam
{
ServerName = remoteStr.Substring(0, remoteStr.LastIndexOf(".")),
MethodName = remoteStr.Substring(remoteStr.LastIndexOf(".") + 1),
ServerParams = param
};
ccp = _ob.ExecuteQuery(ccp, CoreInvokeType.Internal);
return ccp.ReturnObject.ToString2();
}
}
}