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.

1370 lines
47 KiB

  1. 
  2. using Newtonsoft.Json.Linq;
  3. using Newtonsoft.Json;
  4. using NPOI.HSSF.UserModel;
  5. using NPOI.SS.UserModel;
  6. using OT.COM.ArsenalDB;
  7. using OT.COM.LogisticsUtil;
  8. using OT.COM.SignalerMessage;
  9. using SoldierData;
  10. using SoldierDataEntity;
  11. using System;
  12. using System.Collections.Generic;
  13. using System.Data;
  14. using System.IO;
  15. using System.Linq;
  16. using System.Net;
  17. using System.Reflection;
  18. using System.Text;
  19. using SoldierData.syserp;
  20. namespace CounsellorBL
  21. {
  22. public class DBService : ServiceBase
  23. {
  24. protected CustomizeDBMgr cdbm = new CustomizeDBMgr();
  25. protected ArsenalDBMgr adbm = new ArsenalDBMgr();
  26. public ArsenalDBMgr ArsenalDBMgrInst
  27. {
  28. get { return adbm; }
  29. }
  30. public int NumPerPage { get; set; }
  31. public DBService()
  32. : base()
  33. {
  34. NumPerPage = 1000;
  35. //
  36. if(Command.SingleConnection == null)
  37. {
  38. Command.SetupSingleConnection(cdbm.SingleConnection);
  39. }
  40. }
  41. public TableInfo GetMasterDBTableInfo(Type i_t)
  42. {
  43. TableInfo tiRes = cdbm.GetTableInfo(i_t);
  44. return tiRes;
  45. }
  46. protected string getViewModelByEntityName(string i_sTypeName, out object o_ebEntity)
  47. {
  48. string sMsg = null;
  49. object oWhere = null;
  50. try
  51. {
  52. ClassHelper ch = new ClassHelper();
  53. string sEntityName = null;
  54. int nIdx = i_sTypeName.IndexOf(".");
  55. if (nIdx == -1)
  56. {
  57. sEntityName = string.Format("CounsellorBL.ViewModels.master.ViewModel_{0},CounsellorBL", i_sTypeName);
  58. }
  59. else
  60. {
  61. string sCatelog = i_sTypeName.Substring(0, nIdx);
  62. string sSubEntityName = i_sTypeName.Substring(nIdx + 1);
  63. sEntityName = string.Format("CounsellorBL.ViewModels.{0}.ViewModel_{1},CounsellorBL", sCatelog, sSubEntityName);
  64. }
  65. sMsg = ch.GetInstByFullName(sEntityName, out oWhere);
  66. }
  67. catch (Exception ex)
  68. {
  69. sMsg = new Util().GetLastExceptionMsg(ex);
  70. }
  71. o_ebEntity = oWhere;
  72. return sMsg;
  73. }
  74. protected string getEntity(string i_sEntityName, out object o_ebEntity)
  75. {
  76. string sMsg = null;
  77. object oWhere = null;
  78. try
  79. {
  80. ClassHelper ch = new ClassHelper();
  81. string sEntityName = cdbm.GetFullEntityName(i_sEntityName);
  82. sMsg = ch.GetInstByFullName(sEntityName + ",SoldierData", out oWhere); //錯誤時回傳No Type 或Create Type Fail錯誤
  83. }
  84. catch (Exception ex)
  85. {
  86. sMsg = new Util().GetLastExceptionMsg(ex);
  87. }
  88. o_ebEntity = oWhere;
  89. return sMsg;
  90. }
  91. /// <summary>
  92. /// Session Validation
  93. /// </summary>
  94. /// <param name="i_sToken"></param>
  95. /// <param name="i_bRenewExpireTime"></param>
  96. /// <returns></returns>
  97. private CResponseMessage _checkTokenValid(string i_sToken, bool i_bRenewExpireTime = false)
  98. {
  99. CResponseMessage crm = null;
  100. string sMsg = null;
  101. do
  102. {
  103. WhereNode wn = new WhereNode(otb_session.CN_GUID, WhereNode.EColumnOperation.EOT_EQ, typeof(otb_session), i_sToken);
  104. Command cSelectSeesion = Command.SetupSelectCmd(GetMasterDBTableInfo(typeof(otb_session)), null, wn);
  105. otb_session sCur = adbm.RunQuerySingleORM<otb_session>(cSelectSeesion);
  106. if (null == sCur)
  107. {
  108. sMsg = BaseExceptionWord.ex000023; //SESSION_NO_SESSION
  109. break;
  110. }
  111. if (new Util().GetSettingInt("ExpireTimeMinute") < DateTime.Now.Subtract(sCur.modify_date).TotalMinutes)
  112. {
  113. sMsg = BaseExceptionWord.ex000024; //SESSION_EXPIRED SESSION過期
  114. break;
  115. }
  116. crm = new CResponseMessage() { RESULT = EResponseResult.RR_TRUE };
  117. crm.DATA.Add(BLWording.SESSION_USER_ID, sCur.create_user_guid);
  118. if (true == i_bRenewExpireTime)
  119. {
  120. otb_session sModifyData = new otb_session() { modify_date = DateTime.Now };
  121. WhereNode wnUpdate = new WhereNode(otb_session.CN_GUID, WhereNode.EColumnOperation.EOT_EQ, typeof(otb_session), i_sToken);
  122. Command cUpdate = Command.SetupUpdateCmd(GetMasterDBTableInfo(typeof(otb_session)), sModifyData, wnUpdate);
  123. adbm.RunEditSingleCmd(cUpdate);
  124. }
  125. }
  126. while (false);
  127. if (null != sMsg)
  128. {
  129. crm = new CErrorResponseMessage(sMsg);
  130. }
  131. return crm;
  132. }
  133. protected CResponseMessage checkTokenWithCRequestMessage(CRequestMessage i_crm, out Dictionary<string, object> o_oDicData)
  134. {
  135. CResponseMessage crm = null;
  136. string sMsg = null;
  137. Dictionary<string, object> dicFormData = new Dictionary<string, object>();
  138. try
  139. {
  140. do
  141. {
  142. foreach (string sKey in i_crm.DATA.Keys)
  143. {
  144. dicFormData.Add(sKey, i_crm.DATA[sKey]);
  145. }
  146. //if (false == dicFormData.Keys.Contains(BLWording.TOKEN))
  147. if (i_crm.TOKEN == null)
  148. {
  149. sMsg = BaseExceptionWord.ex000025; //NO TOKEN
  150. break;
  151. }
  152. crm = this._checkTokenValid(i_crm.TOKEN, true);
  153. }
  154. while (false);
  155. }
  156. catch (Exception ex)
  157. {
  158. sMsg = new Util().GetLastExceptionMsg(ex);
  159. }
  160. if (null != sMsg)
  161. {
  162. crm = new CErrorResponseMessage(sMsg, i_crm);
  163. }
  164. o_oDicData = dicFormData;
  165. return crm;
  166. }
  167. /// <summary>
  168. /// For Query result
  169. /// </summary>
  170. protected class queryResponse
  171. {
  172. public long TotalCount { get; set; }
  173. public long CurrentCount { get; set; }
  174. public List<Dictionary<string, object>> Records { get; set; }
  175. public queryResponse(QueryDataSet i_qds)
  176. {
  177. List<Dictionary<string, object>> ldicData = new List<Dictionary<string, object>>();
  178. DataTable dt = i_qds.DATA.Tables[0];
  179. DataColumnCollection dcc = dt.Columns;
  180. int nSNCount = 1;
  181. this.TotalCount = i_qds.Total;
  182. this.CurrentCount = dt.Rows.Count;
  183. foreach (DataRow dr in dt.Rows)
  184. {
  185. Dictionary<string, object> dicData = new Dictionary<string, object>();
  186. dicData.Add("SN", nSNCount);
  187. foreach (DataColumn dc in dcc)
  188. {
  189. object oData = dr[dc.ColumnName];
  190. string sColumnName = dc.ColumnName.ToLower();
  191. //dicData.Add(sColumnName, EntityUtil.Removepadding(sData, sColumnName, i_qds.DataType));
  192. if (oData is string)
  193. {
  194. string sData = oData.ToString();
  195. dicData.Add(sColumnName, sData.TrimEnd());
  196. }
  197. else if (oData is DateTime)
  198. {
  199. dicData.Add(sColumnName, ((DateTime)(oData)).ToString("yyyy/MM/dd HH:mm:ss"));
  200. }
  201. else
  202. {
  203. dicData.Add(sColumnName, oData.ToString());
  204. }
  205. }
  206. ldicData.Add(dicData);
  207. nSNCount++;
  208. }
  209. this.Records = ldicData;
  210. }
  211. }
  212. /// <summary>
  213. /// Get table row
  214. /// </summary>
  215. /// <param name="i_crm"></param>
  216. /// <returns></returns>
  217. public CResponseMessage GetInfoDataBase(string sEntityName, CRequestMessage i_crm, EntityBase ebDisplay)
  218. {
  219. string sMsg = null;
  220. CResponseMessage crmRes = null;
  221. ClassHelper ch = new ClassHelper();
  222. int nNumOfPage = -1;
  223. int nPageIdx = -1;
  224. try
  225. {
  226. do
  227. {
  228. string sSearchMode = _fetchString(i_crm, BLWording.SEARCHMODE);
  229. if (null == sSearchMode)
  230. {
  231. sMsg = BaseExceptionWord.ex000026; //NO SEARCH Mode
  232. break;
  233. }
  234. if (sSearchMode == BLWording.SEARCHMODE_RECORDIDX)
  235. {
  236. nNumOfPage = 1;
  237. string sIDX = _fetchString(i_crm, BLWording.SEARCHMODE_RECORDIDX);
  238. if (null == sIDX || Int32.TryParse(sIDX, out nPageIdx) == false)
  239. {
  240. sMsg = BaseExceptionWord.ex000027; //NO RECORDIDX
  241. break;
  242. }
  243. }
  244. /*else if (sSearchMode == BLWording.SEARCHMODE_FID)
  245. {
  246. int nID = -1;
  247. string sFID = _fetchString(i_crm, BLWording.SEARCHMODE_FID);
  248. if (null == sFID && Int32.TryParse(sFID, out nID))
  249. {
  250. sMsg = "NO COMMONPK";
  251. break;
  252. }
  253. i_wnWhere.SetValue(BLWording.COMMONPK, Int32.Parse(sFID));
  254. }*/
  255. else if (sSearchMode == BLWording.SEARCHMODE_PAGEIDX)
  256. {
  257. string sPageIDX = _fetchString(i_crm, BLWording.SEARCHMODE_PAGEIDX);
  258. if (null == sPageIDX || Int32.TryParse(sPageIDX, out nPageIdx) == false)
  259. {
  260. sMsg = BaseExceptionWord.ex000028; //NO PAGEIDX
  261. break;
  262. }
  263. string sNumPerPage = _fetchString(i_crm, BLWording.SEARCHMODE_NUMPERGAGE);
  264. if (null == sPageIDX || Int32.TryParse(sNumPerPage, out nNumOfPage) == false)
  265. {
  266. sMsg = BaseExceptionWord.ex000029; //NO NUMPERPAGE
  267. break;
  268. }
  269. }
  270. string sToken = _fetchString(i_crm, BLWording.TOKEN);
  271. //#if !DEBUG
  272. // Dictionary<string, object> dicFormData = null;
  273. // CResponseMessage crmCheckToken = checkTokenWithCRequestMessage(i_crm, out dicFormData);
  274. // if (EResponseResult.RR_FALSE == crmCheckToken.RESULT)
  275. // {
  276. // sMsg = crmCheckToken.MSG;
  277. // break;
  278. // }
  279. //#endif
  280. Command cSelect = null;
  281. if (i_crm.DATA.ContainsKey(BLWording.BLOCKS))
  282. {
  283. List<QueryJsonElement> lBlocks = null;
  284. if (i_crm.DATA[BLWording.BLOCKS] is JArray)
  285. {
  286. JArray joBlocks = i_crm.DATA[BLWording.BLOCKS] as JArray;
  287. lBlocks = joBlocks.ToObject<List<QueryJsonElement>>();
  288. }
  289. else if (i_crm.DATA[BLWording.BLOCKS] is List<QueryJsonElement>)
  290. {
  291. lBlocks = i_crm.DATA[BLWording.BLOCKS] as List<QueryJsonElement>;
  292. }
  293. else
  294. {
  295. sMsg = BaseExceptionWord.ex000030;//NO SUPPORT BLOCKS
  296. break;
  297. }
  298. sMsg = MakeSelectJoinByBlocks(lBlocks, out cSelect);
  299. }
  300. else if (i_crm.DATA.ContainsKey(BLWording.DATA))
  301. {
  302. JObject joDic = i_crm.DATA[BLWording.DATA] as JObject;
  303. Dictionary<string, object> dicSO = joDic.ToObject<Dictionary<string, object>>();
  304. Dictionary<string, string> dicSelect = new Dictionary<string, string>();
  305. foreach (string sKey in dicSO.Keys)
  306. {
  307. object o = dicSO[sKey];
  308. if (o != null)
  309. {
  310. string s = dicSO[sKey].ToString();
  311. if (s.Trim().Length != 0)
  312. {
  313. if (ebDisplay.GetType().GetProperty(sKey) != null)
  314. {
  315. dicSelect.Add(sKey, s);
  316. }
  317. }
  318. }
  319. }
  320. cSelect = Command.SetupSelectCmd(GetMasterDBTableInfo(ebDisplay.GetType()), ebDisplay, dicSelect);
  321. }
  322. else
  323. {
  324. cSelect = Command.SetupSelectCmd(GetMasterDBTableInfo(ebDisplay.GetType()), ebDisplay);
  325. }
  326. if (sMsg != null)
  327. {
  328. break;
  329. }
  330. //nNumOfPage = reRangeNumOfPage(nNumOfPage);
  331. QueryDataSet qds = adbm.RunQueryDataSet(cSelect, nPageIdx, nNumOfPage);
  332. if (qds.IsSuccess == true)
  333. {
  334. crmRes = new CSuccessResponseMessage(null, i_crm);
  335. queryResponse qr = new queryResponse(qds);
  336. crmRes.DATA.Add(BLWording.ENTITYS, qr);
  337. }
  338. else
  339. {
  340. sMsg = qds.ErrorCode;
  341. }
  342. }
  343. while (false);
  344. }
  345. catch (Exception ex)
  346. {
  347. sMsg = new Util().GetLastExceptionMsg(ex);
  348. }
  349. if (null != sMsg)
  350. {
  351. crmRes = new CErrorResponseMessage(sMsg, i_crm);
  352. }
  353. return crmRes;
  354. }
  355. public string MakeSelectJoinByBlocks(List<QueryJsonElement> i_lBlocks, out Command o_cSelect)
  356. {
  357. string sMsg = null;
  358. Command cTemp = null;
  359. do
  360. {
  361. if (i_lBlocks == null || i_lBlocks.Count() == 0)
  362. {
  363. sMsg = BaseExceptionWord.ex000032; //NO BLOCK
  364. break;
  365. }
  366. convertWherenode(ref i_lBlocks);
  367. QueryJson qj = new QueryJson();
  368. qj.AddBlock(i_lBlocks.ToArray());
  369. sMsg = qj.MakeCommand(cdbm.GetTableInfo(new CustomizeDBMgr().GetEntityType(i_lBlocks[0].table)), out cTemp);
  370. }
  371. while (false);
  372. o_cSelect = cTemp;
  373. return sMsg;
  374. }
  375. protected void convertWherenode(ref List<QueryJsonElement> io_data)
  376. {
  377. ClassHelper ch = new ClassHelper();
  378. for (int i = 0; i < io_data.Count(); i++)
  379. {
  380. QueryJsonElement qjs = io_data[i];
  381. Type t = new CustomizeDBMgr().GetEntityType(qjs.table);
  382. qjs.databaseinfo = t.FullName.Split('.')[1];
  383. WhereNode wnDicwherecolsCompile = Command.whereDictionary2WhereNode(GetMasterDBTableInfo(t), qjs.dicwherecols);
  384. if (qjs.wherecols == null)
  385. {
  386. qjs.wherecols = wnDicwherecolsCompile;
  387. }
  388. else
  389. {
  390. if (wnDicwherecolsCompile != null)
  391. {
  392. qjs.wherecols = new WhereNode(WhereNode.ENodeOperation.ENO_AND, qjs.wherecols, wnDicwherecolsCompile);
  393. }
  394. }
  395. }
  396. }
  397. /// <summary>
  398. /// Get table row
  399. /// </summary>
  400. /// <param name="i_crm"></param>
  401. /// <returns></returns>
  402. public CResponseMessage GetInfoData(CRequestMessage i_crm)
  403. {
  404. string sMsg = null;
  405. CResponseMessage crmRes = null;
  406. try
  407. {
  408. do
  409. {
  410. string sEntityName = _fetchString(i_crm, BLWording.CMDENTITYTYPE);
  411. if (null == sEntityName)
  412. {
  413. sMsg = BaseExceptionWord.ex000032;// NO MATCHED ENTITY
  414. break;
  415. }
  416. object oWhere = null;
  417. sMsg = getEntity(sEntityName, out oWhere);
  418. if (sMsg != null)
  419. {
  420. break;
  421. }
  422. crmRes = GetInfoDataBase(sEntityName, i_crm, oWhere as EntityBase);
  423. }
  424. while (false);
  425. }
  426. catch (Exception ex)
  427. {
  428. sMsg = new Util().GetLastExceptionMsg(ex);
  429. }
  430. if (null != sMsg)
  431. {
  432. crmRes = new CErrorResponseMessage(sMsg, i_crm);
  433. }
  434. return crmRes;
  435. }
  436. protected string makeWhereNode(CRequestMessage i_crm, Type tRun, Dictionary<string, object> dicFormData, ClassHelper ch, ref WhereNode io_wn)
  437. {
  438. string sMsg = null;
  439. try
  440. {
  441. do
  442. {
  443. EntityBase ebWhere = ch.GetInstByType(tRun) as EntityBase;
  444. Dictionary<string, object> dicInput = new Dictionary<string, object>();
  445. List<WhereNode> lwnWhere = new List<WhereNode>();
  446. io_wn = null;
  447. JObject jo = null;
  448. // Support Ethan request: Delete/Update detail but not PK
  449. bool bUsePK = true;
  450. if (dicFormData.ContainsKey(BLWording.WHEREDATA))
  451. {
  452. bUsePK = false;
  453. jo = dicFormData[BLWording.WHEREDATA] as JObject;
  454. }
  455. else if (dicFormData.ContainsKey(BLWording.ORIGINDATA))
  456. {
  457. jo = dicFormData[BLWording.ORIGINDATA] as JObject;
  458. }
  459. else
  460. {
  461. jo = dicFormData[BLWording.DATA] as JObject;
  462. }
  463. foreach (JProperty property in jo.Properties())
  464. {
  465. JToken jv = property.Value;
  466. dicInput.Add(property.Name, jv.Value<string>());
  467. }
  468. PropertyInfo[] pis = tRun.GetProperties();
  469. foreach (PropertyInfo pi in pis)
  470. {
  471. System.Attribute attr = System.Attribute.GetCustomAttribute(pi, typeof(ColumnMiscAttribute));
  472. bool bSystemColumn = false; // Default: Not System Column
  473. bool bPK = false;
  474. if (null != attr)
  475. {
  476. ColumnMiscAttribute cma = attr as ColumnMiscAttribute;
  477. DBColumnInfo dbi = cma.Data;
  478. bSystemColumn = dbi.DescriptionExtension != null && dbi.DescriptionExtension.SysColumn;
  479. bPK = dbi.IsPK;
  480. }
  481. if (dicInput.ContainsKey(pi.Name))
  482. {
  483. object oValue = dicInput[pi.Name];
  484. if (bUsePK == true)
  485. {
  486. if (bPK == true)
  487. {
  488. if (null != oValue)
  489. {
  490. //pi.SetValue(ebWhere, ch.ConvertValue(pi, oValue), null);
  491. WhereNode wnTemp = new WhereNode(pi.Name, WhereNode.EColumnOperation.EOT_EQ, tRun, ch.ConvertValue(pi, oValue));
  492. lwnWhere.Add(wnTemp);
  493. }
  494. }
  495. }
  496. else
  497. {
  498. if (null != oValue)
  499. {
  500. WhereNode wnTemp = new WhereNode(pi.Name, WhereNode.EColumnOperation.EOT_EQ, tRun, ch.ConvertValue(pi, oValue));
  501. lwnWhere.Add(wnTemp);
  502. }
  503. }
  504. }
  505. }
  506. io_wn = new WhereNode(WhereNode.ENodeOperation.ENO_AND, lwnWhere.ToArray());
  507. }
  508. while (false);
  509. }
  510. catch (Exception ex)
  511. {
  512. sMsg = new Util().GetLastExceptionMsg(ex);
  513. }
  514. return sMsg;
  515. }
  516. public CResponseMessage EraseEntity(CRequestMessage i_crm)
  517. {
  518. CResponseMessage crm = null;
  519. string sMsg = null;
  520. try
  521. {
  522. do
  523. {
  524. ClassHelper ch = new ClassHelper();
  525. Dictionary<string, object> dicFormData = null;
  526. CResponseMessage crmCheckToken = checkTokenWithCRequestMessage(i_crm, out dicFormData);
  527. if (EResponseResult.RR_FALSE == crmCheckToken.RESULT)
  528. {
  529. sMsg = crmCheckToken.MSG;
  530. break;
  531. }
  532. string sEntityName = _fetchString(i_crm, BLWording.CMDENTITYTYPE);
  533. if (null == sEntityName)
  534. {
  535. sMsg = BaseExceptionWord.ex000033;//NO MATCHED7 ENTITY
  536. break;
  537. }
  538. object oData = null;
  539. sMsg = getEntity(sEntityName, out oData);
  540. if (null != sMsg)
  541. {
  542. break;
  543. }
  544. EntityBase ebData = oData as EntityBase;
  545. // Only return valid data
  546. if (i_crm.DATA.ContainsKey(BLWording.ACTIONDATA))
  547. {
  548. JObject oActionData = i_crm.DATA[BLWording.ACTIONDATA] as JObject;
  549. Dictionary<string, string> dicSS = oActionData.ToObject<Dictionary<string, string>>();
  550. ebData.SetValue(otb_user.CN_ACTIVE_FLAG, dicSS[BLWording.MARKVALUE]);
  551. }
  552. else
  553. {
  554. ebData.SetValue(otb_user.CN_ACTIVE_FLAG, "Y");
  555. }
  556. WhereNode wnWhere = null;
  557. sMsg = makeWhereNode(i_crm, oData.GetType(), dicFormData, ch, ref wnWhere);
  558. if (null != sMsg)
  559. {
  560. break;
  561. }
  562. Command c = Command.SetupUpdateCmd(GetMasterDBTableInfo(oData.GetType()), ebData, wnWhere);
  563. int nRes = adbm.RunEditSingleCmd(c);
  564. if (0 == nRes)
  565. {
  566. sMsg = BaseExceptionWord.ex000034; //MARK FAIL
  567. break;
  568. }
  569. crm = new CSuccessResponseMessage(null, i_crm);
  570. }
  571. while (false);
  572. }
  573. catch (Exception ex)
  574. {
  575. sMsg = new Util().GetLastExceptionMsg(ex);
  576. }
  577. if (null != sMsg)
  578. {
  579. crm = new CErrorResponseMessage(sMsg, i_crm);
  580. }
  581. return crm;
  582. }
  583. public CResponseMessage DeleteEntity(CRequestMessage i_crm)
  584. {
  585. CResponseMessage crm = null;
  586. Util u = new Util();
  587. ClassHelper ch = new ClassHelper();
  588. string sMsg = null;
  589. try
  590. {
  591. do
  592. {
  593. Dictionary<string, object> dicFormData = null;
  594. CResponseMessage crmCheckToken = checkTokenWithCRequestMessage(i_crm, out dicFormData);
  595. if (EResponseResult.RR_FALSE == crmCheckToken.RESULT)
  596. {
  597. sMsg = crmCheckToken.MSG;
  598. break;
  599. }
  600. string sEntityName = _fetchString(i_crm, BLWording.CMDENTITYTYPE);
  601. if (null == sEntityName)
  602. {
  603. sMsg = BaseExceptionWord.ex000033; //NO MATCHED ENTITY
  604. break;
  605. }
  606. object oData = null;
  607. sMsg = getEntity(sEntityName, out oData);
  608. if (null != sMsg)
  609. {
  610. break;
  611. }
  612. WhereNode wnWhere = null;
  613. sMsg = makeWhereNode(i_crm, oData.GetType(), dicFormData, ch, ref wnWhere);
  614. if (null != sMsg)
  615. {
  616. break;
  617. }
  618. if (wnWhere.IsLeaf == false && wnWhere.ChildrenNodes.Count == 0)
  619. {
  620. sMsg = BaseExceptionWord.ex000013; //NO CONDITION
  621. break;
  622. }
  623. TableInfo ti = GetMasterDBTableInfo(oData.GetType());
  624. // Backup
  625. {
  626. string sBackupTable = _fetchString(i_crm, BLWording.CMDBACKUPENTITYTYPE);
  627. if (string.IsNullOrEmpty(sBackupTable) == false)
  628. {
  629. ti.BackTable = new CustomizeDBMgr().GetEntityType(sBackupTable);
  630. }
  631. }
  632. // Add User
  633. Command c = Command.SetupDeleteCmd(ti, wnWhere);
  634. int nRes = adbm.RunEditSingleCmd(c);
  635. /*if (0 == nRes)
  636. {
  637. sMsg = "REMOVE FAIL";
  638. break;
  639. }*/
  640. crm = new CSuccessResponseMessage(null, i_crm);
  641. crm.DATA.Add(BLWording.REMOVECOUNT, nRes);
  642. //crm.DATA.Add(BLWording.COMMONPK, c.LastInsertIdentity);
  643. }
  644. while (false);
  645. }
  646. catch (Exception ex)
  647. {
  648. sMsg = u.GetLastExceptionMsg(ex);
  649. }
  650. if (null != sMsg)
  651. {
  652. crm = new CErrorResponseMessage(sMsg, i_crm);
  653. }
  654. return crm;
  655. }
  656. public CResponseMessage UpdateEntity(CRequestMessage i_crm)
  657. {
  658. Util u = new Util();
  659. ClassHelper ch = new ClassHelper();
  660. CResponseMessage crm = null;
  661. string sMsg = null;
  662. try
  663. {
  664. do
  665. {
  666. Dictionary<string, object> dicFormData = null;
  667. CResponseMessage crmCheckToken = checkTokenWithCRequestMessage(i_crm, out dicFormData);
  668. if (EResponseResult.RR_FALSE == crmCheckToken.RESULT)
  669. {
  670. sMsg = crmCheckToken.MSG;
  671. break;
  672. }
  673. string sEntityName = _fetchString(i_crm, BLWording.CMDENTITYTYPE);
  674. if (null == sEntityName)
  675. {
  676. sMsg = BaseExceptionWord.ex000033; //NO MATCHED ENTITY
  677. break;
  678. }
  679. object oData = null;
  680. sMsg = getEntity(sEntityName, out oData);
  681. if (null != sMsg)
  682. {
  683. break;
  684. }
  685. Type tRun = oData.GetType();
  686. EntityBase ebData = oData as EntityBase;
  687. EntityBase ebWhere = ch.GetInstByType(tRun) as EntityBase;
  688. Dictionary<string, object> dicInput = new Dictionary<string, object>();
  689. JObject jo = dicFormData[BLWording.DATA] as JObject;
  690. foreach (JProperty property in jo.Properties())
  691. {
  692. JToken jv = property.Value;
  693. if (property.Name == otb_user.CN_CREATE_DATE)
  694. {
  695. continue;
  696. }
  697. dicInput.Add(property.Name, jv.Value<string>());
  698. }
  699. Dictionary<string, string> dicFillRes = ebData.FillUpadateData(dicInput);
  700. if (0 != dicFillRes.Count)
  701. {
  702. sMsg = BaseExceptionWord.ex000022; //PARAMETER ERROR 原+=
  703. break;
  704. }
  705. WhereNode wnWhere = null;
  706. sMsg = makeWhereNode(i_crm, oData.GetType(), dicFormData, ch, ref wnWhere);
  707. if (null != sMsg)
  708. {
  709. break;
  710. }
  711. Command c = Command.SetupUpdateCmd(GetMasterDBTableInfo(oData.GetType()), ebData, wnWhere);
  712. // Fix me
  713. //int nID = (int)crmCheckToken.DATA[BLWording.SESSION_USER_ID];
  714. //ch.SetValueByPropertyName(ebData, tb_user.CN_FK_N_MODIFIER_USER_ID, nID);
  715. int nRes = adbm.RunEditSingleCmd(c);
  716. if (0 == nRes)
  717. {
  718. sMsg = BaseExceptionWord.ex000011; //UPDATE FAIL
  719. break;
  720. }
  721. crm = new CSuccessResponseMessage(null, i_crm);
  722. }
  723. while (false);
  724. }
  725. catch (Exception ex)
  726. {
  727. sMsg = u.GetLastExceptionMsg(ex);
  728. }
  729. if (null != sMsg)
  730. {
  731. crm = new CErrorResponseMessage(sMsg, i_crm);
  732. }
  733. return crm;
  734. }
  735. public CResponseMessage CreateEntity(CRequestMessage i_crm)
  736. {
  737. CResponseMessage crm = null;
  738. string sMsg = null;
  739. Util u = new Util();
  740. ClassHelper ch = new ClassHelper();
  741. try
  742. {
  743. do
  744. {
  745. Dictionary<string, object> dicFormData = null;
  746. CResponseMessage crmCheckToken = checkTokenWithCRequestMessage(i_crm, out dicFormData);
  747. if (EResponseResult.RR_FALSE == crmCheckToken.RESULT)
  748. {
  749. sMsg = crmCheckToken.MSG;
  750. break;
  751. }
  752. string sEntityName = _fetchString(i_crm, BLWording.CMDENTITYTYPE);
  753. if (null == sEntityName)
  754. {
  755. sMsg = BaseExceptionWord.ex000033; //NO MATCHED ENTITY
  756. break;
  757. }
  758. object oInsert = null;
  759. sMsg = getEntity(sEntityName, out oInsert);
  760. if (null != sMsg)
  761. {
  762. break;
  763. }
  764. Type tRun = oInsert.GetType();
  765. EntityBase ebInsert = oInsert as EntityBase;
  766. Dictionary<string, object> dicInput = new Dictionary<string, object>();
  767. JObject jo = dicFormData[BLWording.DATA] as JObject;
  768. foreach (JProperty property in jo.Properties())
  769. {
  770. JToken jv = property.Value;
  771. dicInput.Add(property.Name, jv.Value<string>());
  772. }
  773. Dictionary<string, string> dicFillRes = ebInsert.FillInsertData(dicInput);
  774. if (0 != dicFillRes.Count)
  775. {
  776. sMsg = BaseExceptionWord.ex000022; //PARAMETER ERROR 原+=
  777. break;
  778. }
  779. // Add User
  780. Command c = Command.SetupInsertCmd(GetMasterDBTableInfo(oInsert.GetType()), ebInsert);
  781. // Fix me
  782. //int nID = (int)crmCheckToken.DATA[BLWording.SESSION_USER_ID];
  783. //ch.SetValueByPropertyName(ebInsert, tb_user.CN_FK_N_CREATOR_USER_ID, nID);
  784. //ch.SetValueByPropertyName(ebInsert, tb_user.CN_FK_N_MODIFIER_USER_ID, nID);
  785. int nRes = adbm.RunEditSingleCmd(c);
  786. if (0 >= nRes)
  787. {
  788. sMsg = c.LastErrorCode;
  789. break;
  790. }
  791. crm = new CSuccessResponseMessage(null, i_crm);
  792. Command cSelect = Command.SetupSelectCmd(GetMasterDBTableInfo(oInsert.GetType()), null, Command.MakeWhereEntityBase2WhereNode(ebInsert));
  793. QueryDataSet qds = adbm.RunQueryDataSet(cSelect);
  794. queryResponse qr = new queryResponse(qds);
  795. crm.DATA.Add(BLWording.ENTITYS, qr);
  796. }
  797. while (false);
  798. }
  799. catch (Exception ex)
  800. {
  801. sMsg = u.GetLastExceptionMsg(ex);
  802. }
  803. if (null != sMsg)
  804. {
  805. crm = new CErrorResponseMessage(sMsg, i_crm);
  806. }
  807. return crm;
  808. }
  809. public CResponseMessage ExcelEntity(CRequestMessage i_crm)
  810. {
  811. string sMsg = null;
  812. string sFilePath = "";
  813. bool bStatus = false;
  814. CResponseMessage crm = null;
  815. List<Dictionary<string, object>> list = null;
  816. try
  817. {
  818. do
  819. {
  820. string sEntityName = _fetchString(i_crm, BLWording.CMDENTITYTYPE);
  821. if (null == sEntityName)
  822. {
  823. sMsg = BaseExceptionWord.ex000033; //NO MATCHED ENTITY
  824. break;
  825. }
  826. object oWhere = null;
  827. sMsg = getEntity(sEntityName, out oWhere);
  828. if (sMsg != null)
  829. {
  830. break;
  831. }
  832. string sdata = _fetchString(i_crm, "data") ?? "";
  833. if (sdata != "")
  834. {
  835. if (i_crm.DATA["data"] is JArray)
  836. {
  837. JArray joData = i_crm.DATA["data"] as JArray;
  838. list = joData.ToObject<List<Dictionary<string, object>>>();
  839. }
  840. }
  841. else
  842. {
  843. crm = GetInfoDataBase(sEntityName, i_crm, oWhere as EntityBase);
  844. queryResponse data = (queryResponse)crm.DATA[BLWording.ENTITYS];
  845. list = data.Records;
  846. }
  847. bStatus = CreateExcelFiles(list, i_crm, out sFilePath);
  848. crm = new CSuccessResponseMessage(null, i_crm);
  849. crm.DATA.Add(BLWording.FILESTATUS, bStatus);
  850. crm.DATA.Add(BLWording.FILEPATH, sFilePath);
  851. //Directory.Delete(strFileName);
  852. }
  853. while (false);
  854. }
  855. catch (Exception ex)
  856. {
  857. sMsg = new Util().GetLastExceptionMsg(ex);
  858. }
  859. if (null != sMsg)
  860. {
  861. crm = new CErrorResponseMessage(sMsg, i_crm);
  862. }
  863. return crm;
  864. }
  865. public bool CreateExcelFiles(List<Dictionary<string, object>> i_dicData, CRequestMessage i_crm, out string o_sFilePath)
  866. {
  867. string sFilePath = null;
  868. string sFileName = null;
  869. bool bStatus = false;
  870. int imaxcell = 0;
  871. do
  872. {
  873. Dictionary<string, object> items = null;
  874. HSSFWorkbook workbook = new HSSFWorkbook();
  875. ISheet sheet = workbook.CreateSheet("sheet1");
  876. ICellStyle style = workbook.CreateCellStyle();
  877. style.BorderBottom = BorderStyle.Thin;
  878. style.BorderLeft = BorderStyle.Thin;
  879. style.BorderRight = BorderStyle.Thin;
  880. style.BorderTop = BorderStyle.Thin;
  881. IRow dataRow = sheet.CreateRow(0);
  882. if (i_crm.DATA.ContainsKey(BLWording.ITEMS))
  883. {
  884. JObject joItems = i_crm.DATA[BLWording.ITEMS] as JObject;
  885. items = joItems.ToObject<Dictionary<string, object>>();
  886. }
  887. // Workarond: Forward capbility for DBService.ExcelEntity
  888. if (items == null && i_crm.DATA.ContainsKey(BLWording.DATA))
  889. {
  890. JObject dic = i_crm.DATA[BLWording.DATA] as JObject;
  891. JObject joItems = dic[BLWording.ITEMS] as JObject;
  892. if (null == joItems)
  893. {
  894. break;
  895. }
  896. items = joItems.ToObject<Dictionary<string, object>>();
  897. }
  898. //填充表头
  899. if (items != null)
  900. {
  901. int index = 0;
  902. foreach (string sKey in items.Keys)
  903. {
  904. object sValue = items[sKey];
  905. string s = sValue.ToString() ?? "";
  906. if (s != "")
  907. {
  908. ICell cell = dataRow.CreateCell(index);
  909. cell.CellStyle = style;
  910. cell.SetCellValue(s);
  911. index++;
  912. }
  913. }
  914. }
  915. //填充内容
  916. string sJsonKey = _fetchString(i_crm, "sjsonkey");
  917. string scartonkey = _fetchString(i_crm, "scartonkey");
  918. sJsonKey = sJsonKey ?? "";
  919. scartonkey = scartonkey ?? "";
  920. int cellIndex = 0;
  921. foreach (Dictionary<string, object> _list in i_dicData)
  922. {
  923. if (_list != null)
  924. {
  925. dataRow = sheet.CreateRow(dataRow.RowNum + 1);
  926. imaxcell = cellIndex > imaxcell ? cellIndex : imaxcell;
  927. cellIndex = 0;
  928. foreach (string sKey in items.Keys)
  929. {
  930. if (_list.Keys.Contains(sKey))
  931. {
  932. object sValue = _list[sKey];
  933. string s = sValue.ToString() ?? "";
  934. if (sKey != sJsonKey)
  935. {
  936. ICell cell = dataRow.CreateCell(cellIndex);
  937. cell.CellStyle = style;
  938. if (sKey == scartonkey)
  939. { //箱號當為0時顯示為空
  940. cell.SetCellValue((s == "0" ? "" : s));
  941. }
  942. else
  943. {
  944. cell.SetCellValue(s);
  945. }
  946. cellIndex++;
  947. }
  948. else
  949. {
  950. Dictionary<string, object> dicData = new Dictionary<string, object>();
  951. JObject jsonObj = (JObject)JsonConvert.DeserializeObject(s);
  952. Dictionary<string, object> jsonData = jsonObj.ToObject<Dictionary<string, object>>();
  953. dicData = jsonData;
  954. //if (jsonData["scan_type_flag"].ToString() == "1")
  955. //{
  956. // JArray jDatas = jsonData["ScanDatas"] as JArray;
  957. // jsonData = jDatas.Count > 0 ? getToExcelData(jDatas) : new Dictionary<string, object>();
  958. //}
  959. foreach (string _sKey in jsonData.Keys)
  960. {
  961. if (_sKey.ToLower().IndexOf("_scan_sval") > -1)
  962. {//整合性報表特殊處理(公用部分走上邊部分)
  963. object sJson = jsonData[_sKey];
  964. string sVal = sJson.ToString() ?? "";
  965. ICell cell = dataRow.CreateCell(cellIndex);
  966. cell.CellStyle = style;
  967. cell.SetCellValue(sVal);
  968. cellIndex++;
  969. }
  970. }
  971. }
  972. }
  973. }
  974. }
  975. }
  976. //自適應
  977. for (int cellindex = 0; cellindex <= imaxcell; cellindex++)
  978. {
  979. sheet.AutoSizeColumn(cellindex, true);
  980. }
  981. sFilePath = "Uploads/ExpExcel/";
  982. sFileName = _fetchString(i_crm, "filename");
  983. sFileName = (sFileName ?? "") + DateTime.Now.ToString("yyyyMMddHHmmss") + ".xls";
  984. string sSeverPath = AppDomain.CurrentDomain.BaseDirectory + "/" + sFilePath;
  985. string sFileFullName = sSeverPath + "/" + sFileName;
  986. if (!Directory.Exists(sSeverPath))
  987. {
  988. Directory.CreateDirectory(sSeverPath);
  989. }
  990. //保存
  991. using (MemoryStream ms = new MemoryStream())
  992. {
  993. using (FileStream fs = new FileStream(sFileFullName, FileMode.Create, FileAccess.Write))
  994. {
  995. workbook.Write(fs);
  996. bStatus = true;
  997. }
  998. }
  999. }
  1000. while (false);
  1001. o_sFilePath = sFilePath + sFileName;
  1002. return bStatus;
  1003. }
  1004. /// <summary>
  1005. /// 整合性報表案例1json字串處理
  1006. /// </summary>
  1007. /// <param name="i_crm">With given table name</param>
  1008. /// <returns>list of property</returns>
  1009. public Dictionary<string, object> getToExcelData(JArray i_jaData)
  1010. {
  1011. Dictionary<string, object> dicData = i_jaData.ToObject<Dictionary<string, object>>();
  1012. if (i_jaData.Count > 0)
  1013. {
  1014. List<Dictionary<string, object>> lsDic = i_jaData.ToObject<List<Dictionary<string, object>>>();
  1015. foreach (Dictionary<string, object> dic in lsDic)
  1016. {
  1017. foreach (string sKey in dic.Keys)
  1018. {
  1019. if (sKey.ToLower().IndexOf("_scan_sval") > -1)
  1020. {
  1021. if (dicData[sKey] != null)
  1022. {
  1023. dicData[sKey] += ";" + dic[sKey];
  1024. }
  1025. else
  1026. {
  1027. dicData.Add(sKey, dic[sKey]);
  1028. }
  1029. }
  1030. }
  1031. }
  1032. }
  1033. return dicData;
  1034. }
  1035. /// <summary>
  1036. /// To fetch column info from a given table name
  1037. /// </summary>
  1038. /// <param name="i_crm">With given table name</param>
  1039. /// <returns>list of property</returns>
  1040. public CResponseMessage GetEntityProperties(CRequestMessage i_crm)
  1041. {
  1042. string sMsg = null;
  1043. CResponseMessage crm = null;
  1044. try
  1045. {
  1046. do
  1047. {
  1048. Dictionary<string, object> dicFormData = null;
  1049. CResponseMessage crmCheckToken = checkTokenWithCRequestMessage(i_crm, out dicFormData);
  1050. if (EResponseResult.RR_FALSE == crmCheckToken.RESULT)
  1051. {
  1052. sMsg = crmCheckToken.MSG;
  1053. break;
  1054. }
  1055. string sEntityName = _fetchString(i_crm, BLWording.CMDENTITYTYPE);
  1056. if (sEntityName == null)
  1057. {
  1058. sMsg = BaseExceptionWord.ex000035; //NO ENTITY
  1059. break;
  1060. }
  1061. Type tTest = new CustomizeDBMgr().GetEntityType(sEntityName);
  1062. if (tTest == null)
  1063. {
  1064. sMsg = BaseExceptionWord.ex000033; //NO MATCHED ENTITY
  1065. break;
  1066. }
  1067. PropertyInfo[] pis = tTest.GetProperties();
  1068. List<DBColumnInfo> lColumns = new List<DBColumnInfo>();
  1069. foreach (PropertyInfo pi in pis)
  1070. {
  1071. System.Attribute attr = System.Attribute.GetCustomAttribute(pi, typeof(ColumnMiscAttribute));
  1072. if (null != attr)
  1073. {
  1074. ColumnMiscAttribute cma = attr as ColumnMiscAttribute;
  1075. DBColumnInfo dbi = cma.Data;
  1076. if (dbi != null)
  1077. {
  1078. lColumns.Add(dbi);
  1079. }
  1080. }
  1081. }
  1082. lColumns = lColumns.OrderBy(o => o.COLUMN_NAME).ToList();
  1083. crm = new CSuccessResponseMessage(null, i_crm);
  1084. crm.DATA.Add(BLWording.LISTCOLUMNS, lColumns);
  1085. }
  1086. while (false);
  1087. }
  1088. catch (Exception ex)
  1089. {
  1090. sMsg = new Util().GetLastExceptionMsg(ex);
  1091. }
  1092. if (null != sMsg)
  1093. {
  1094. crm = new CErrorResponseMessage(sMsg, i_crm);
  1095. }
  1096. return crm;
  1097. }
  1098. public static string GetLastErrorCode(Command i_cCmd)
  1099. {
  1100. return (i_cCmd.IsSuccess == false) ? i_cCmd.LastErrorCode : null;
  1101. }
  1102. public static string GetLastErrorCode(List<Command> i_lcCmds)
  1103. {
  1104. string sRes = null;
  1105. if (i_lcCmds.Find(c => (c.IsSuccess == false)) != null)
  1106. {
  1107. sRes = i_lcCmds.Find(c => (c.IsSuccess == false)).LastErrorCode;
  1108. }
  1109. return sRes;
  1110. }
  1111. }
  1112. }