280 lines
9.8 KiB

using CounsellorBL;
using Newtonsoft.Json;
using OT.COM.LogisticsUtil;
using OT.COM.SignalerMessage;
using System;
using System.Collections.Generic;
using System.Collections.Specialized;
using System.Data;
using System.Linq;
using System.Net.Http;
using System.Reflection;
using System.Web;
using System.Web.Http;
namespace DefenseWeb.Models
{
public partial class CmdService : AApiServiceBase
{
static public string DecodeParm(string i_sEncodedData)
{
i_sEncodedData = i_sEncodedData.Replace(" ", "+");
byte[] encodedDataAsBytes = System.Convert.FromBase64String(i_sEncodedData);
string returnValue = System.Text.ASCIIEncoding.ASCII.GetString(encodedDataAsBytes);
return HttpUtility.UrlDecode(returnValue);
}
public CResponseMessage GetDataJsonString(bool i_bDecode, HttpRequestBase i_rRequest)
{
CResponseMessage sRes = null;
try
{
string value = (true == i_bDecode) ? DecodeParm(i_rRequest.Form[""]) : null;
sRes = ProgessJson2(value, i_rRequest);
}
catch (Exception ex)
{
Exception exCur = ex;
while (null != exCur.InnerException)
{
exCur = exCur.InnerException;
}
sRes = new CErrorResponseMessage(exCur.Message);
}
return sRes;
}
public HttpResponseMessage GetData([FromBody]dynamic i_value, bool i_bDecode, HttpRequestMessage i_rRequest)
{
string sRes = null;
try
{
string value = (true == i_bDecode) ? DecodeParm(i_value) : null;
sRes = ProgessJson(value, i_rRequest);
}
catch (Exception ex)
{
Exception exCur = ex;
while (null != exCur.InnerException)
{
exCur = exCur.InnerException;
}
sRes = JsonConvert.SerializeObject(new CErrorResponseMessage(exCur.Message));
}
return new HttpResponseMessage()
{
Content = new StringContent(sRes,
System.Text.Encoding.UTF8, "application/json")
};
}
protected override CResponseMessage HandleRequest2(CRequestMessage i_joRequest, HttpRequestMessage i_rRequest)
{
CResponseMessage sRes = null;
try
{
do
{
if (i_joRequest == null || string.IsNullOrEmpty(i_joRequest.TYPE) || string.IsNullOrEmpty(i_joRequest.MODULE))
{
//sRes = makeErrorReturn(i_joRequest, string.Format(BLWording.REQUEST_IS_NULL));
sRes = new CErrorResponseMessage(string.Format(BLWording.REQUEST_IS_NULL), i_joRequest);
break;
}
string sModuleName = i_joRequest.MODULE + "Service";
ClassHelper ch = new ClassHelper();
object oModule = null;
string sCreateError = ch.GetInstByClassName(sModuleName, out oModule);
if (sCreateError != null || oModule == null)
{
// sRes = makeErrorReturn(i_joRequest, sCreateError);
sRes = new CErrorResponseMessage(sCreateError, i_joRequest);
break;
}
ServiceBase bls = oModule as ServiceBase;
if (bls == null)
{
//sRes = makeErrorReturn(i_joRequest, BLWording.COVERT_FAIL);
sRes = new CErrorResponseMessage(BLWording.COVERT_FAIL, i_joRequest);
break;
}
//sRes = makeMessage(bls.Entry(i_joRequest));
sRes = bls.Entry(i_joRequest);
}
while (false);
}
catch (Exception e)
{
// sRes = makeErrorReturn(i_joRequest, e.Message);
sRes = new CErrorResponseMessage(e.Message, i_joRequest);
}
//if (true == string.IsNullOrWhiteSpace(sRes))
//{
// sRes = makeErrorReturn(i_joRequest, "Unknow Error");
//}
return sRes;
}
protected override string HandleRequest(CRequestMessage i_joRequest, HttpRequestMessage i_rRequest)
{
string sRes = "";
try
{
do
{
if (i_joRequest == null || string.IsNullOrEmpty(i_joRequest.TYPE) || string.IsNullOrEmpty(i_joRequest.MODULE))
{
sRes = makeErrorReturn(i_joRequest, string.Format(BLWording.REQUEST_IS_NULL));
break;
}
string sModuleType = i_joRequest.CUSTOMDATA.ContainsKey("module_id") ? i_joRequest.CUSTOMDATA["module_id"] : "";
string sModuleName = i_joRequest.MODULE + "Service";
// ClassHelper ch = new ClassHelper();
object oModule = null;
string sCreateError = this.GetInstByClassName(sModuleName, sModuleType, out oModule);
if (sCreateError != null || oModule == null)
{
sRes = makeErrorReturn(i_joRequest, sCreateError);
break;
}
ServiceBase bls = oModule as ServiceBase;
if (bls == null)
{
sRes = makeErrorReturn(i_joRequest, BLWording.COVERT_FAIL);
break;
}
sRes = makeMessage(bls.Entry(i_joRequest));
}
while (false);
}
catch (Exception e)
{
sRes = makeErrorReturn(i_joRequest, e.Message);
}
if (true == string.IsNullOrWhiteSpace(sRes))
{
sRes = makeErrorReturn(i_joRequest, "Unknow Error");
}
return sRes;
}
public string GetInstByClassName(string i_sTypeName, string i_sModuleId, out object o_oRes)
{
object obj2 = null;
string str = null;
Type typeByTypeName = this.GetTypeByTypeName(i_sTypeName, i_sModuleId);
if (typeByTypeName == null)
{
str = "NO THIS ENTITY";
}
else
{
obj2 = Activator.CreateInstance(typeByTypeName);
if (obj2 == null)
{
str = "ENTITY CREATE FAIL";
}
}
o_oRes = obj2;
return str;
}
public Type GetTypeByTypeName(string i_sTypeName, string i_sModuleId)//
{
Type type = null;
string codeBase = Assembly.GetExecutingAssembly().GetName().CodeBase;
codeBase = codeBase.Substring(0, codeBase.LastIndexOf("/"));
Assembly[] assemblyArray = (from f in AppDomain.CurrentDomain.GetAssemblies()
where !f.IsDynamic
&& f.CodeBase != null
&& f.CodeBase.StartsWith(codeBase)
&& f.IsDefined(typeof(AssemblyCompanyAttribute), false) == true
&& f.IsDefined(typeof(AssemblyDescriptionAttribute), false) == true
&& f.IsDefined(typeof(AssemblyProductAttribute), false) == true
&& f.GetCustomAttributes(typeof(AssemblyCompanyAttribute), false).OfType<AssemblyCompanyAttribute>().FirstOrDefault().Company == "Origtek"
&& f.GetCustomAttributes(typeof(AssemblyDescriptionAttribute), false).OfType<AssemblyDescriptionAttribute>().FirstOrDefault().Description == "Service"
&& ((f.GetCustomAttributes(typeof(AssemblyProductAttribute), false).OfType<AssemblyProductAttribute>().FirstOrDefault().Product.Equals("CounsellorBL"))
|| (f.GetCustomAttributes(typeof(AssemblyProductAttribute), false).OfType<AssemblyProductAttribute>().FirstOrDefault().Product.Contains("CounsellorBL." + i_sModuleId.ToUpper()) == true))
orderby f.GetCustomAttributes(typeof(AssemblyProductAttribute), false).OfType<AssemblyProductAttribute>().FirstOrDefault().Product descending
select f).ToArray<Assembly>();
// && f.GetCustomAttributes(typeof(AssemblyCompanyAttribute), false).OfType<AssemblyCompanyAttribute>().FirstOrDefault() != null
// && f.GetCustomAttributes(typeof(AssemblyDescriptionAttribute), false).OfType<AssemblyDescriptionAttribute>().FirstOrDefault() != null
// && f.GetCustomAttributes(typeof(AssemblyProductAttribute), false).OfType<AssemblyProductAttribute>().FirstOrDefault() != null
foreach (Assembly assembly2 in assemblyArray)
{
var typeArray = assembly2.GetTypes().Where(x => x.IsSubclassOf(typeof(ServiceBase))).ToArray<Type>();
foreach (Type classType in typeArray)
{
if (classType.IsClass && (classType.Name == i_sTypeName))
{
type = classType;
return type;
}
}
}
return type;
}
}
}