using Newtonsoft.Json;
using Newtonsoft.Json.Linq;
using OT.COM.ArsenalDB;
using OT.COM.LogisticsUtil;
using OT.COM.SignalerMessage;
using SoldierData.syserp;
using SoldierDataEntity;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Reflection;
using System.Text;
namespace CounsellorBL
{
///
/// 類別名稱: DBhelper
/// 類別說明 新增,修改,刪除方法
/// 起始作者: John
/// 起始日期: 2016/06/04
/// 最新修改人: John
/// 最新修日期: 2016/06/14
///
public class DBhelper : DBService
{
#region getQryListCommand
///
/// 函式名稱:getQryListCommand
/// 函式說明:獲取批次查詢Command
/// 起始作者:Justin
/// 起始日期:2016/10/05
/// 最新修改人:Justin
/// 最新日期:2016/10/05
///
///
/// 參數說明i_crm(Object):所需查詢資料...
/// 參數說明i_sTbName:Tabel名稱
/// 參數說明o_sMsg:錯誤訊息
///
///
/// 回傳 lComd(Object):新增狀態相關訊息
///
protected List getQryListCommand(object i_dicData, string i_sTbName, out string o_sMsg, List> i_ordercols = null, Dictionary i_aliascols = null, bool i_distinct = false)
{
string sMsg = null;
OT.COM.LogisticsUtil.ClassHelper chTool = new OT.COM.LogisticsUtil.ClassHelper();
Dictionary dicData = null;
List lComd = new List();
if (i_dicData is JArray)
{
JArray jaData = i_dicData as JArray;
foreach (JObject jo in jaData)
{
object tbData = null;
sMsg = getEntity(i_sTbName, out tbData); //傳出來一個物件
Type Tabletype = tbData.GetType();
EntityBase ebSelect = tbData as EntityBase;
if (null != sMsg)
{
sMsg = BaseExceptionWord.ex000022; //參數錯誤
break;
}
dicData = jo["wheredata"].ToObject>();
// WhereNode wnWhere = Command.whereDictionary2WhereNode(GetMasterDBTableInfo(Tabletype), dicData);
Command c = Command.SetupSelectCmdByParam(GetMasterDBTableInfo(tbData.GetType()), ebSelect, dicData, i_ordercols, i_aliascols, i_distinct);
lComd.Add(c);
}
}
o_sMsg = sMsg;
return lComd;
}
#endregion
#region getAddListCommand
///
/// 函式名稱:getAddListCommand
/// 函式說明:獲取批次新增Command
/// 起始作者:John
/// 起始日期:2016/06/04
/// 最新修改人:John
/// 最新日期:2016/06/14
///
///
/// 參數說明i_crm(Object):所需新增資料...
/// 參數說明i_sTbName:Tabel名稱
/// 參數說明o_sMsg:錯誤訊息
///
///
/// 回傳 lComd(Object):新增狀態相關訊息
///
protected List getAddListCommand(object i_dicData, string i_sTbName, out string o_sMsg)
{
string sMsg = null;
OT.COM.LogisticsUtil.ClassHelper chTool = new OT.COM.LogisticsUtil.ClassHelper();
List lComd = new List();
if (i_dicData is JArray)
{
JArray jaData = i_dicData as JArray;
foreach (JObject jo in jaData)
{
Dictionary dicData = jo.ToObject>();
object oInsert = null;
sMsg = getEntity(i_sTbName, out oInsert);
if (null != sMsg)
{
sMsg = BaseExceptionWord.ex000022; //參數錯誤
break;
}
EntityBase ebInsert = oInsert as EntityBase;
Dictionary dicInput = new Dictionary();
JObject jdata = (JObject)JsonConvert.DeserializeObject(dicData[BLWording.DATA].ToString());
foreach (JProperty property in jdata.Properties())
{
JToken jv = property.Value;
dicInput.Add(property.Name, jv.Value());
}
Dictionary dicFillRes = ebInsert.FillInsertData(dicInput);
if (0 != dicFillRes.Count)
{
sMsg = BaseExceptionWord.ex000022; //參數錯誤
break;
}
// Add User
Command c = Command.SetupInsertCmd(GetMasterDBTableInfo(oInsert.GetType()), ebInsert);
lComd.Add(c);
}
}
o_sMsg = sMsg;
return lComd;
}
#endregion
#region getUpdListCommand
///
/// 函式名稱:getUpdListCommand
/// 函式說明:獲取批次修改Command
/// 起始作者:John
/// 起始日期:2016/06/04
/// 最新修改人:John
/// 最新日期:2016/06/14
///
///
/// 參數說明i_crm(Object):所需修改資料...
/// 參數說明i_sTbName:Tabel名稱
/// 參數說明o_sMsg:錯誤訊息
///
///
/// 回傳 lComd(Object):修改狀態相關訊息
///
protected List getUpdListCommand(object i_dicData, string i_sTbName, List i_saUpdKeys, out string o_sMsg)
{
string sMsg = null;
bool bIsHasPK = true;
OT.COM.LogisticsUtil.ClassHelper chTool = new OT.COM.LogisticsUtil.ClassHelper();
List lComd = new List();
if (i_dicData is JArray)
{
JArray jaData = i_dicData as JArray;
foreach (JObject joData in jaData)
{
Dictionary dicData = joData.ToObject>();
object oData = null;
sMsg = getEntity(i_sTbName, out oData);
if (null != sMsg)
{
sMsg = BaseExceptionWord.ex000022; //參數錯誤
break;
}
Type tRun = oData.GetType();
EntityBase ebData = oData as EntityBase;
EntityBase ebWhere = chTool.GetInstByType(tRun) as EntityBase;
Dictionary dicInput = new Dictionary();
if (!dicData.ContainsKey(BLWording.DATA))
{
sMsg = BaseExceptionWord.ex000022; //參數錯誤
break;
}
JObject jdata = (JObject)JsonConvert.DeserializeObject(dicData[BLWording.DATA].ToString());
foreach (JProperty property in jdata.Properties())
{
JToken jv = property.Value;
dicInput.Add(property.Name, jv.Value());
}
Dictionary dicFillRes = ebData.FillUpadateData(dicInput);
if (0 != dicFillRes.Count)
{
sMsg = BaseExceptionWord.ex000022; //參數錯誤
break;
}
if (!dicData.ContainsKey(BLWording.WHEREDATA))
{
sMsg = BaseExceptionWord.ex000022; //參數錯誤
break;
}
Dictionary dicWhere = new Dictionary();
JObject jwheredata = (JObject)JsonConvert.DeserializeObject(dicData[BLWording.WHEREDATA].ToString());
if (null != JProperty2Dic(jwheredata, ref dicWhere))
{
sMsg = BaseExceptionWord.ex000022; //參數錯誤
break;
}
WhereNode wnWhere = null;
List lwnWhere = new List();
foreach (string sKey in i_saUpdKeys)
{
if (dicWhere.ContainsKey(sKey) == false)
{
bIsHasPK = false;
break;
}
if (dicWhere[sKey].ToString() == "" || dicWhere[sKey] == null)
{
bIsHasPK = false;
break;
}
WhereNode wn = new WhereNode(sKey, WhereNode.EColumnOperation.EOT_EQ, tRun, dicWhere[sKey]);
lwnWhere.Add(wn);
}
if (!bIsHasPK)
{
sMsg = BaseExceptionWord.ex000022; //參數錯誤
break;
}
wnWhere = new WhereNode(WhereNode.ENodeOperation.ENO_AND, lwnWhere.ToArray());
Command c = Command.SetupUpdateCmd(GetMasterDBTableInfo(oData.GetType()), ebData, wnWhere);
lComd.Add(c);
}
}
o_sMsg = sMsg;
return lComd;
}
#endregion
#region getDelListCommand
///
/// 函式名稱:getDelListCommand
/// 函式說明:獲取批次刪除Command
/// 起始作者:John
/// 起始日期:2016/06/04
/// 最新修改人:John
/// 最新日期:2016/06/14
///
///
/// 參數說明i_crm(Object):所需刪除資料...
/// 參數說明i_sTbName:Tabel名稱
/// 參數說明o_sMsg:錯誤訊息
///
///
/// 回傳 lComd(Object):刪除狀態相關訊息
///
protected List getDelListCommand(CRequestMessage i_crm, object i_dicData, string i_sTbName, List i_saDelKeys, out string o_sMsg)
{
string sMsg = null;
bool bIsHasPK = true;
OT.COM.LogisticsUtil.ClassHelper chTool = new OT.COM.LogisticsUtil.ClassHelper();
List lComd = new List();
if (i_dicData is JArray)
{
JArray jaWhereData = i_dicData as JArray;
foreach (JObject joData in jaWhereData)
{
Dictionary dicData = joData.ToObject>();
object oData = null;
sMsg = getEntity(i_sTbName, out oData);
Type tRun = oData.GetType();
if (null != sMsg)
{
sMsg = BaseExceptionWord.ex000022; //參數錯誤
break;
}
if (!dicData.ContainsKey(BLWording.WHEREDATA))
{
sMsg = BaseExceptionWord.ex000022; //參數錯誤
break;
}
Dictionary dicWhere = new Dictionary();
JObject jwheredata = (JObject)JsonConvert.DeserializeObject(dicData[BLWording.WHEREDATA].ToString());
foreach (JProperty property in jwheredata.Properties())
{
JToken jv = property.Value;
dicWhere.Add(property.Name, jv.Value());
}
WhereNode wnWhere = null;
List lwnWhere = new List();
foreach (string sKey in i_saDelKeys)
{
if (dicWhere.ContainsKey(sKey) == false)
{
bIsHasPK = false;
break;
}
if (dicWhere[sKey].ToString() == "" || dicWhere[sKey] == null)
{
bIsHasPK = false;
break;
}
WhereNode wn = new WhereNode(sKey, WhereNode.EColumnOperation.EOT_EQ, tRun, dicWhere[sKey]);
lwnWhere.Add(wn);
}
if (!bIsHasPK)
{
sMsg = BaseExceptionWord.ex000022; //參數錯誤
break;
}
wnWhere = new WhereNode(WhereNode.ENodeOperation.ENO_AND, lwnWhere.ToArray());
TableInfo ti = GetMasterDBTableInfo(oData.GetType());
// Backup
string sBackupTable = _fetchString(i_crm, BLWording.CMDBACKUPENTITYTYPE);
if (string.IsNullOrEmpty(sBackupTable) == false)
{
ti.BackTable = new CustomizeDBMgr().GetEntityType(sBackupTable);
}
// Add User
Command c = Command.SetupDeleteCmd(ti, wnWhere);
lComd.Add(c);
}
}
o_sMsg = sMsg;
return lComd;
}
#endregion
}
public class DBhelper : DBService where T : EntityBase, new()
{
#region 根據ID,獲取
///
/// 函式名稱:QueryByPk
/// 函式說明:根據ID,獲取
/// 起始作者:John
/// 起始日期:2016/06/27
/// 最新修改人:John
/// 最新日期:2016/06/27
///
/// Pk
/// 單筆資料對象
public T QueryByPk(string i_sPk)
{
T oEntity = new T();
oEntity.SetValue(oEntity.PKNames[0],
Convert.ChangeType(i_sPk, oEntity.GetPropertyType(oEntity.PKNames[0])));
WhereNode wndPk = Command.MakeWhereEntityBase2WhereNode(oEntity);
Command cSelect = Command.SetupSelectCmd(GetMasterDBTableInfo(typeof(T)), new T(), wndPk);
T oResult = this.adbm.RunQuerySingleORM(cSelect);
return oResult;
}
public T QueryEntity(T i_oEntity)
{
WhereNode wndPk = Command.MakeWhereEntityBase2WhereNode(i_oEntity);
Command cSelect = Command.SetupSelectCmd(GetMasterDBTableInfo(typeof(T)), new T(), wndPk);
T oResult = this.adbm.RunQuerySingleORM(cSelect);
return oResult;
}
public IList QueryByWhere(WhereNode i_oWhere)
{
T oEntity = new T();
Command cSelect = Command.SetupSelectCmd(GetMasterDBTableInfo(typeof(T)), oEntity, i_oWhere);
IList oResult = this.adbm.RunQueryList(cSelect);
return oResult;
}
#endregion
#region 根據ID,刪除
///
/// 函式名稱:DeleteByPk
/// 函式說明:根據ID,刪除
/// 起始作者:John
/// 起始日期:2016/06/27
/// 最新修改人:John
/// 最新日期:2016/06/27
///
/// Pk
/// 變動的行數
public int DeleteByPk(string i_sPk)
{
T oEntity = new T();
oEntity.SetValue(oEntity.PKNames[0],
Convert.ChangeType(i_sPk, oEntity.GetPropertyType(oEntity.PKNames[0])));
return DeleteEntity(oEntity);
}
public int DeleteEntity(T i_oEntity)
{
WhereNode wndPk = Command.MakeWhereEntityBase2WhereNode(i_oEntity);
Command cDelete = Command.SetupDeleteCmd(GetMasterDBTableInfo(typeof(T)), wndPk);
int iUpdateRowCount = this.adbm.RunEditSingleCmd(cDelete);
return iUpdateRowCount;
}
#endregion
#region 插入
///
/// 函式名稱:InsertEntity
/// 函式說明:插入多筆,用於不同實體類
/// 起始作者:John
/// 起始日期:2016/06/27
/// 最新修改人:John
/// 最新日期:2016/06/27
///
/// 多筆新增資料集合
/// 失敗返回0,成功返回大於0
public int InsertEntity(List i_lsCmd)
{
int iUpdateRowCount = this.adbm.RunEditCmds(i_lsCmd);
return iUpdateRowCount;
}
///
/// 函式名稱:GetinsertCmds
/// 函式說明:產生批次新增Command
/// 起始作者:John
/// 起始日期:2016/06/27
/// 最新修改人:John
/// 最新日期:2016/06/27
///
///
///
public List GetinsertCmds(IList i_lsEntity)
{
List lsCmd = new List();
foreach (T oEntity in i_lsEntity)
{
Command cInsert = Command.SetupInsertCmd(GetMasterDBTableInfo(typeof(T)), oEntity);
lsCmd.Add(cInsert);
}
return lsCmd;
}
#endregion
#region 更新
///
/// 函式名稱:UpdateEntity
/// 函式說明:批次更新
/// 起始作者:John
/// 起始日期:2016/06/27
/// 最新修改人:John
/// 最新日期:2016/06/27
///
///
/// 變動的行數
public int UpdateEntity(List i_lsEntity)
{
List lsCommands = GetUpdateCmds(i_lsEntity);
int iUpdateRowCount = this.adbm.RunEditCmds(lsCommands);
return iUpdateRowCount;
}
///
/// 函式名稱:GetUpdateCmds
/// 函式說明:產生批次新增Command
/// 起始作者:John
/// 起始日期:2016/06/27
/// 最新修改人:John
/// 最新日期:2016/06/27
///
///
///
public List GetUpdateCmds(IList i_lsEntity)
{
List lsCmd = new List();
foreach (T oEntity in i_lsEntity)
{
WhereNode wndPk = Command.MakeWhereEntityBase2WhereNode(oEntity);
Command cUpdate = Command.SetupUpdateCmd(GetMasterDBTableInfo(typeof(T)), oEntity, wndPk);
lsCmd.Add(cUpdate);
}
return lsCmd;
}
#endregion
}
}