You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

544 lines
19 KiB

  1. using Newtonsoft.Json;
  2. using Newtonsoft.Json.Linq;
  3. using OT.COM.ArsenalDB;
  4. using OT.COM.LogisticsUtil;
  5. using OT.COM.SignalerMessage;
  6. using SoldierData.syserp;
  7. using SoldierDataEntity;
  8. using System;
  9. using System.Collections.Generic;
  10. using System.Linq;
  11. using System.Reflection;
  12. using System.Text;
  13. namespace CounsellorBL
  14. {
  15. /// <summary>
  16. /// 類別名稱: DBhelper
  17. /// 類別說明 新增,修改,刪除方法
  18. /// 起始作者: John
  19. /// 起始日期: 2016/06/04
  20. /// 最新修改人: John
  21. /// 最新修日期: 2016/06/14
  22. /// </summary>
  23. public class DBhelper : DBService
  24. {
  25. #region getQryListCommand
  26. /// <summary>
  27. /// 函式名稱:getQryListCommand
  28. /// 函式說明:獲取批次查詢Command
  29. /// 起始作者:Justin
  30. /// 起始日期:2016/10/05
  31. /// 最新修改人:Justin
  32. /// 最新日期:2016/10/05
  33. /// </summary>
  34. /// <param name="i_crmInput">
  35. /// 參數說明i_crm(Object):所需查詢資料...
  36. /// 參數說明i_sTbName:Tabel名稱
  37. /// 參數說明o_sMsg:錯誤訊息
  38. /// </param>
  39. /// <returns>
  40. /// 回傳 lComd(Object):新增狀態相關訊息
  41. /// </returns>
  42. protected List<Command> getQryListCommand(object i_dicData, string i_sTbName, out string o_sMsg, List<Dictionary<string, string>> i_ordercols = null, Dictionary<string, string> i_aliascols = null, bool i_distinct = false)
  43. {
  44. string sMsg = null;
  45. OT.COM.LogisticsUtil.ClassHelper chTool = new OT.COM.LogisticsUtil.ClassHelper();
  46. Dictionary<string, string> dicData = null;
  47. List<Command> lComd = new List<Command>();
  48. if (i_dicData is JArray)
  49. {
  50. JArray jaData = i_dicData as JArray;
  51. foreach (JObject jo in jaData)
  52. {
  53. object tbData = null;
  54. sMsg = getEntity(i_sTbName, out tbData); //傳出來一個物件
  55. Type Tabletype = tbData.GetType();
  56. EntityBase ebSelect = tbData as EntityBase;
  57. if (null != sMsg)
  58. {
  59. sMsg = BaseExceptionWord.ex000022; //參數錯誤
  60. break;
  61. }
  62. dicData = jo["wheredata"].ToObject<Dictionary<string, string>>();
  63. // WhereNode wnWhere = Command.whereDictionary2WhereNode(GetMasterDBTableInfo(Tabletype), dicData);
  64. Command c = Command.SetupSelectCmdByParam(GetMasterDBTableInfo(tbData.GetType()), ebSelect, dicData, i_ordercols, i_aliascols, i_distinct);
  65. lComd.Add(c);
  66. }
  67. }
  68. o_sMsg = sMsg;
  69. return lComd;
  70. }
  71. #endregion
  72. #region getAddListCommand
  73. /// <summary>
  74. /// 函式名稱:getAddListCommand
  75. /// 函式說明:獲取批次新增Command
  76. /// 起始作者:John
  77. /// 起始日期:2016/06/04
  78. /// 最新修改人:John
  79. /// 最新日期:2016/06/14
  80. /// </summary>
  81. /// <param name="i_crmInput">
  82. /// 參數說明i_crm(Object):所需新增資料...
  83. /// 參數說明i_sTbName:Tabel名稱
  84. /// 參數說明o_sMsg:錯誤訊息
  85. /// </param>
  86. /// <returns>
  87. /// 回傳 lComd(Object):新增狀態相關訊息
  88. /// </returns>
  89. protected List<Command> getAddListCommand(object i_dicData, string i_sTbName, out string o_sMsg)
  90. {
  91. string sMsg = null;
  92. OT.COM.LogisticsUtil.ClassHelper chTool = new OT.COM.LogisticsUtil.ClassHelper();
  93. List<Command> lComd = new List<Command>();
  94. if (i_dicData is JArray)
  95. {
  96. JArray jaData = i_dicData as JArray;
  97. foreach (JObject jo in jaData)
  98. {
  99. Dictionary<string, object> dicData = jo.ToObject<Dictionary<string, object>>();
  100. object oInsert = null;
  101. sMsg = getEntity(i_sTbName, out oInsert);
  102. if (null != sMsg)
  103. {
  104. sMsg = BaseExceptionWord.ex000022; //參數錯誤
  105. break;
  106. }
  107. EntityBase ebInsert = oInsert as EntityBase;
  108. Dictionary<string, object> dicInput = new Dictionary<string, object>();
  109. JObject jdata = (JObject)JsonConvert.DeserializeObject(dicData[BLWording.DATA].ToString());
  110. foreach (JProperty property in jdata.Properties())
  111. {
  112. JToken jv = property.Value;
  113. dicInput.Add(property.Name, jv.Value<string>());
  114. }
  115. Dictionary<string, string> dicFillRes = ebInsert.FillInsertData(dicInput);
  116. if (0 != dicFillRes.Count)
  117. {
  118. sMsg = BaseExceptionWord.ex000022; //參數錯誤
  119. break;
  120. }
  121. // Add User
  122. Command c = Command.SetupInsertCmd(GetMasterDBTableInfo(oInsert.GetType()), ebInsert);
  123. lComd.Add(c);
  124. }
  125. }
  126. o_sMsg = sMsg;
  127. return lComd;
  128. }
  129. #endregion
  130. #region getUpdListCommand
  131. /// <summary>
  132. /// 函式名稱:getUpdListCommand
  133. /// 函式說明:獲取批次修改Command
  134. /// 起始作者:John
  135. /// 起始日期:2016/06/04
  136. /// 最新修改人:John
  137. /// 最新日期:2016/06/14
  138. /// </summary>
  139. /// <param name="i_crmInput">
  140. /// 參數說明i_crm(Object):所需修改資料...
  141. /// 參數說明i_sTbName:Tabel名稱
  142. /// 參數說明o_sMsg:錯誤訊息
  143. /// </param>
  144. /// <returns>
  145. /// 回傳 lComd(Object):修改狀態相關訊息
  146. /// </returns>
  147. protected List<Command> getUpdListCommand(object i_dicData, string i_sTbName, List<string> i_saUpdKeys, out string o_sMsg)
  148. {
  149. string sMsg = null;
  150. bool bIsHasPK = true;
  151. OT.COM.LogisticsUtil.ClassHelper chTool = new OT.COM.LogisticsUtil.ClassHelper();
  152. List<Command> lComd = new List<Command>();
  153. if (i_dicData is JArray)
  154. {
  155. JArray jaData = i_dicData as JArray;
  156. foreach (JObject joData in jaData)
  157. {
  158. Dictionary<string, object> dicData = joData.ToObject<Dictionary<string, object>>();
  159. object oData = null;
  160. sMsg = getEntity(i_sTbName, out oData);
  161. if (null != sMsg)
  162. {
  163. sMsg = BaseExceptionWord.ex000022; //參數錯誤
  164. break;
  165. }
  166. Type tRun = oData.GetType();
  167. EntityBase ebData = oData as EntityBase;
  168. EntityBase ebWhere = chTool.GetInstByType(tRun) as EntityBase;
  169. Dictionary<string, object> dicInput = new Dictionary<string, object>();
  170. if (!dicData.ContainsKey(BLWording.DATA))
  171. {
  172. sMsg = BaseExceptionWord.ex000022; //參數錯誤
  173. break;
  174. }
  175. JObject jdata = (JObject)JsonConvert.DeserializeObject(dicData[BLWording.DATA].ToString());
  176. foreach (JProperty property in jdata.Properties())
  177. {
  178. JToken jv = property.Value;
  179. dicInput.Add(property.Name, jv.Value<string>());
  180. }
  181. Dictionary<string, string> dicFillRes = ebData.FillUpadateData(dicInput);
  182. if (0 != dicFillRes.Count)
  183. {
  184. sMsg = BaseExceptionWord.ex000022; //參數錯誤
  185. break;
  186. }
  187. if (!dicData.ContainsKey(BLWording.WHEREDATA))
  188. {
  189. sMsg = BaseExceptionWord.ex000022; //參數錯誤
  190. break;
  191. }
  192. Dictionary<string, object> dicWhere = new Dictionary<string, object>();
  193. JObject jwheredata = (JObject)JsonConvert.DeserializeObject(dicData[BLWording.WHEREDATA].ToString());
  194. if (null != JProperty2Dic(jwheredata, ref dicWhere))
  195. {
  196. sMsg = BaseExceptionWord.ex000022; //參數錯誤
  197. break;
  198. }
  199. WhereNode wnWhere = null;
  200. List<WhereNode> lwnWhere = new List<WhereNode>();
  201. foreach (string sKey in i_saUpdKeys)
  202. {
  203. if (dicWhere.ContainsKey(sKey) == false)
  204. {
  205. bIsHasPK = false;
  206. break;
  207. }
  208. if (dicWhere[sKey].ToString() == "" || dicWhere[sKey] == null)
  209. {
  210. bIsHasPK = false;
  211. break;
  212. }
  213. WhereNode wn = new WhereNode(sKey, WhereNode.EColumnOperation.EOT_EQ, tRun, dicWhere[sKey]);
  214. lwnWhere.Add(wn);
  215. }
  216. if (!bIsHasPK)
  217. {
  218. sMsg = BaseExceptionWord.ex000022; //參數錯誤
  219. break;
  220. }
  221. wnWhere = new WhereNode(WhereNode.ENodeOperation.ENO_AND, lwnWhere.ToArray());
  222. Command c = Command.SetupUpdateCmd(GetMasterDBTableInfo(oData.GetType()), ebData, wnWhere);
  223. lComd.Add(c);
  224. }
  225. }
  226. o_sMsg = sMsg;
  227. return lComd;
  228. }
  229. #endregion
  230. #region getDelListCommand
  231. /// <summary>
  232. /// 函式名稱:getDelListCommand
  233. /// 函式說明:獲取批次刪除Command
  234. /// 起始作者:John
  235. /// 起始日期:2016/06/04
  236. /// 最新修改人:John
  237. /// 最新日期:2016/06/14
  238. /// </summary>
  239. /// <param name="i_crmInput">
  240. /// 參數說明i_crm(Object):所需刪除資料...
  241. /// 參數說明i_sTbName:Tabel名稱
  242. /// 參數說明o_sMsg:錯誤訊息
  243. /// </param>
  244. /// <returns>
  245. /// 回傳 lComd(Object):刪除狀態相關訊息
  246. /// </returns>
  247. protected List<Command> getDelListCommand(CRequestMessage i_crm, object i_dicData, string i_sTbName, List<string> i_saDelKeys, out string o_sMsg)
  248. {
  249. string sMsg = null;
  250. bool bIsHasPK = true;
  251. OT.COM.LogisticsUtil.ClassHelper chTool = new OT.COM.LogisticsUtil.ClassHelper();
  252. List<Command> lComd = new List<Command>();
  253. if (i_dicData is JArray)
  254. {
  255. JArray jaWhereData = i_dicData as JArray;
  256. foreach (JObject joData in jaWhereData)
  257. {
  258. Dictionary<string, object> dicData = joData.ToObject<Dictionary<string, object>>();
  259. object oData = null;
  260. sMsg = getEntity(i_sTbName, out oData);
  261. Type tRun = oData.GetType();
  262. if (null != sMsg)
  263. {
  264. sMsg = BaseExceptionWord.ex000022; //參數錯誤
  265. break;
  266. }
  267. if (!dicData.ContainsKey(BLWording.WHEREDATA))
  268. {
  269. sMsg = BaseExceptionWord.ex000022; //參數錯誤
  270. break;
  271. }
  272. Dictionary<string, object> dicWhere = new Dictionary<string, object>();
  273. JObject jwheredata = (JObject)JsonConvert.DeserializeObject(dicData[BLWording.WHEREDATA].ToString());
  274. foreach (JProperty property in jwheredata.Properties())
  275. {
  276. JToken jv = property.Value;
  277. dicWhere.Add(property.Name, jv.Value<string>());
  278. }
  279. WhereNode wnWhere = null;
  280. List<WhereNode> lwnWhere = new List<WhereNode>();
  281. foreach (string sKey in i_saDelKeys)
  282. {
  283. if (dicWhere.ContainsKey(sKey) == false)
  284. {
  285. bIsHasPK = false;
  286. break;
  287. }
  288. if (dicWhere[sKey].ToString() == "" || dicWhere[sKey] == null)
  289. {
  290. bIsHasPK = false;
  291. break;
  292. }
  293. WhereNode wn = new WhereNode(sKey, WhereNode.EColumnOperation.EOT_EQ, tRun, dicWhere[sKey]);
  294. lwnWhere.Add(wn);
  295. }
  296. if (!bIsHasPK)
  297. {
  298. sMsg = BaseExceptionWord.ex000022; //參數錯誤
  299. break;
  300. }
  301. wnWhere = new WhereNode(WhereNode.ENodeOperation.ENO_AND, lwnWhere.ToArray());
  302. TableInfo ti = GetMasterDBTableInfo(oData.GetType());
  303. // Backup
  304. string sBackupTable = _fetchString(i_crm, BLWording.CMDBACKUPENTITYTYPE);
  305. if (string.IsNullOrEmpty(sBackupTable) == false)
  306. {
  307. ti.BackTable = new CustomizeDBMgr().GetEntityType(sBackupTable);
  308. }
  309. // Add User
  310. Command c = Command.SetupDeleteCmd(ti, wnWhere);
  311. lComd.Add(c);
  312. }
  313. }
  314. o_sMsg = sMsg;
  315. return lComd;
  316. }
  317. #endregion
  318. }
  319. public class DBhelper<T> : DBService where T : EntityBase, new()
  320. {
  321. #region 根據ID,獲取
  322. /// <summary>
  323. /// 函式名稱:QueryByPk
  324. /// 函式說明:根據ID,獲取
  325. /// 起始作者:John
  326. /// 起始日期:2016/06/27
  327. /// 最新修改人:John
  328. /// 最新日期:2016/06/27
  329. /// </summary>
  330. /// <param name="i_sPk">Pk</param>
  331. /// <returns>單筆資料對象</returns>
  332. public T QueryByPk(string i_sPk)
  333. {
  334. T oEntity = new T();
  335. oEntity.SetValue(oEntity.PKNames[0],
  336. Convert.ChangeType(i_sPk, oEntity.GetPropertyType(oEntity.PKNames[0])));
  337. WhereNode wndPk = Command.MakeWhereEntityBase2WhereNode(oEntity);
  338. Command cSelect = Command.SetupSelectCmd(GetMasterDBTableInfo(typeof(T)), new T(), wndPk);
  339. T oResult = this.adbm.RunQuerySingleORM<T>(cSelect);
  340. return oResult;
  341. }
  342. public T QueryEntity(T i_oEntity)
  343. {
  344. WhereNode wndPk = Command.MakeWhereEntityBase2WhereNode(i_oEntity);
  345. Command cSelect = Command.SetupSelectCmd(GetMasterDBTableInfo(typeof(T)), new T(), wndPk);
  346. T oResult = this.adbm.RunQuerySingleORM<T>(cSelect);
  347. return oResult;
  348. }
  349. public IList<T> QueryByWhere(WhereNode i_oWhere)
  350. {
  351. T oEntity = new T();
  352. Command cSelect = Command.SetupSelectCmd(GetMasterDBTableInfo(typeof(T)), oEntity, i_oWhere);
  353. IList<T> oResult = this.adbm.RunQueryList<T>(cSelect);
  354. return oResult;
  355. }
  356. #endregion
  357. #region 根據ID,刪除
  358. /// <summary>
  359. /// 函式名稱:DeleteByPk
  360. /// 函式說明:根據ID,刪除
  361. /// 起始作者:John
  362. /// 起始日期:2016/06/27
  363. /// 最新修改人:John
  364. /// 最新日期:2016/06/27
  365. /// </summary>
  366. /// <param name="i_sPk">Pk</param>
  367. /// <returns>變動的行數</returns>
  368. public int DeleteByPk(string i_sPk)
  369. {
  370. T oEntity = new T();
  371. oEntity.SetValue(oEntity.PKNames[0],
  372. Convert.ChangeType(i_sPk, oEntity.GetPropertyType(oEntity.PKNames[0])));
  373. return DeleteEntity(oEntity);
  374. }
  375. public int DeleteEntity(T i_oEntity)
  376. {
  377. WhereNode wndPk = Command.MakeWhereEntityBase2WhereNode(i_oEntity);
  378. Command cDelete = Command.SetupDeleteCmd(GetMasterDBTableInfo(typeof(T)), wndPk);
  379. int iUpdateRowCount = this.adbm.RunEditSingleCmd(cDelete);
  380. return iUpdateRowCount;
  381. }
  382. #endregion
  383. #region 插入
  384. /// <summary>
  385. /// 函式名稱:InsertEntity
  386. /// 函式說明:插入多筆,用於不同實體類
  387. /// 起始作者:John
  388. /// 起始日期:2016/06/27
  389. /// 最新修改人:John
  390. /// 最新日期:2016/06/27
  391. /// </summary>
  392. /// <param name="i_lsCmd">多筆新增資料集合</param>
  393. /// <returns>失敗返回0,成功返回大於0</returns>
  394. public int InsertEntity(List<Command> i_lsCmd)
  395. {
  396. int iUpdateRowCount = this.adbm.RunEditCmds(i_lsCmd);
  397. return iUpdateRowCount;
  398. }
  399. /// <summary>
  400. /// 函式名稱:GetinsertCmds
  401. /// 函式說明:產生批次新增Command
  402. /// 起始作者:John
  403. /// 起始日期:2016/06/27
  404. /// 最新修改人:John
  405. /// 最新日期:2016/06/27
  406. /// </summary>
  407. /// <param name="i_lsEntity"></param>
  408. /// <returns></returns>
  409. public List<Command> GetinsertCmds(IList<T> i_lsEntity)
  410. {
  411. List<Command> lsCmd = new List<Command>();
  412. foreach (T oEntity in i_lsEntity)
  413. {
  414. Command cInsert = Command.SetupInsertCmd(GetMasterDBTableInfo(typeof(T)), oEntity);
  415. lsCmd.Add(cInsert);
  416. }
  417. return lsCmd;
  418. }
  419. #endregion
  420. #region 更新
  421. /// <summary>
  422. /// 函式名稱:UpdateEntity
  423. /// 函式說明:批次更新
  424. /// 起始作者:John
  425. /// 起始日期:2016/06/27
  426. /// 最新修改人:John
  427. /// 最新日期:2016/06/27
  428. /// </summary>
  429. /// <param name="i_lsEntity"></param>
  430. /// <returns>變動的行數</returns>
  431. public int UpdateEntity(List<T> i_lsEntity)
  432. {
  433. List<Command> lsCommands = GetUpdateCmds(i_lsEntity);
  434. int iUpdateRowCount = this.adbm.RunEditCmds(lsCommands);
  435. return iUpdateRowCount;
  436. }
  437. /// <summary>
  438. /// 函式名稱:GetUpdateCmds
  439. /// 函式說明:產生批次新增Command
  440. /// 起始作者:John
  441. /// 起始日期:2016/06/27
  442. /// 最新修改人:John
  443. /// 最新日期:2016/06/27
  444. /// </summary>
  445. /// <param name="i_lsEntity"></param>
  446. /// <returns></returns>
  447. public List<Command> GetUpdateCmds(IList<T> i_lsEntity)
  448. {
  449. List<Command> lsCmd = new List<Command>();
  450. foreach (T oEntity in i_lsEntity)
  451. {
  452. WhereNode wndPk = Command.MakeWhereEntityBase2WhereNode(oEntity);
  453. Command cUpdate = Command.SetupUpdateCmd(GetMasterDBTableInfo(typeof(T)), oEntity, wndPk);
  454. lsCmd.Add(cUpdate);
  455. }
  456. return lsCmd;
  457. }
  458. #endregion
  459. }
  460. }