AppEnv.cs 23 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557
  1. using System;
  2. using System.Collections.Generic;
  3. using System.Text;
  4. using System.Data;
  5. using SysBaseLibs;
  6. using SysDataLibs.TableClass;
  7. namespace SysDataLibs
  8. {
  9. public class AppEnv
  10. {
  11. /// <summary>
  12. /// 得到表的一条记录的信息
  13. /// </summary>
  14. /// <param name="pcSql">传入的SQL语句</param>
  15. /// <param name="poTableInfo">要创建的对应的表信息对象</param>
  16. /// <returns>创建是否成功</returns>
  17. public static bool GetTableInfo(string pcSql, ITableInfo poTableInfo)
  18. {
  19. UserSession loSession = WebLibs.CheckLogin();
  20. return GetTableInfo(pcSql, poTableInfo, loSession.DBConn);
  21. }
  22. public static bool GetTableInfo(string pcSql, ITableInfo poTableInfo, DBConnSql poDBConn)
  23. {
  24. string lcError = "";
  25. return GetTableInfo(pcSql, poTableInfo, poDBConn, ref lcError);
  26. }
  27. /// <summary>
  28. /// 得到表的一条记录的信息
  29. /// </summary>
  30. /// <param name="pcSql">传入的SQL语句</param>
  31. /// <param name="poTableInfo">要创建的对应的表信息对象</param>
  32. /// <param name="poDBConn">数据库连接对象</param>
  33. /// <param name="pcErrMsg">创建失败的时候返回的错误信息</param>
  34. /// <returns>创建是否成功</returns>
  35. public static bool GetTableInfo(string pcSql, ITableInfo poTableInfo, DBConnSql poDBConn, ref string pcErrMsg)
  36. {
  37. bool lbRetVal = false;
  38. if (pcSql != "" && poTableInfo != null && poDBConn != null && poDBConn.IsOpened)
  39. {
  40. try
  41. {
  42. rsQuery loQuery = poDBConn.OpenQuery(pcSql);
  43. if (loQuery != null && loQuery.IsOpened && loQuery.RecCount > 0)
  44. {
  45. loQuery.MoveFirst();
  46. poTableInfo.CreateTableInfo(loQuery.CurrentRow);
  47. lbRetVal = true;
  48. }
  49. else
  50. pcErrMsg = "传入的SQL语句不合法!";
  51. }
  52. catch (Exception e)
  53. {
  54. ThreadLog.LogException(e);
  55. pcErrMsg = e.Message;
  56. }
  57. }
  58. else
  59. pcErrMsg = "传入的参数不合法!";
  60. return lbRetVal;
  61. }
  62. /// <summary>
  63. /// 执行一个SQL 语句,并将数据跟 sysstatus表数据进行翻译
  64. /// </summary>
  65. /// <param name="pcSql">传入的SQL语句</param>
  66. /// <param name="pcTableName">要翻译的表名</param>
  67. /// <param name="pcData">返回的XML形式的数据集</param>
  68. /// <returns>执行成功返回true 否则 false</returns>
  69. public static bool GetDataOfTranslate(string pcSql, string pcTableName, ref string pcData)
  70. {
  71. string pcDataSchema = "";
  72. string pcErrorMsg = "";
  73. return GetDataOfTranslate(pcSql, pcTableName, ref pcData, ref pcDataSchema, ref pcErrorMsg);
  74. }
  75. public static bool GetDataOfTranslate(string pcSql, string pcTableName, ref string pcData,ref string pcDataSchema)
  76. {
  77. string pcErrorMsg = "";
  78. return GetDataOfTranslate(pcSql, pcTableName, ref pcData, ref pcDataSchema, ref pcErrorMsg);
  79. }
  80. /// <summary>
  81. /// 执行一个SQL 语句,并将数据跟 sysstatus表数据进行翻译
  82. /// </summary>
  83. /// <param name="pcSql">传入的SQL语句</param>
  84. /// <param name="pcTableName">要翻译的表名</param>
  85. /// <param name="pcData">返回的XML形式的数据集</param>
  86. /// <param name="pcDataSchema">返回的XMLd数据表现框架</param>
  87. /// <param name="pcErrorMsg">返回的错误信息</param>
  88. /// <returns>执行成功返回true 否则 false</returns>
  89. public static bool GetDataOfTranslate(string pcSql, string pcTableName, ref string pcData, ref string pcDataSchema, ref string pcErrorMsg)
  90. {
  91. UserSession loSession = WebLibs.CheckLogin();
  92. return GetDataOfTranslate(pcSql, pcTableName, loSession.DBConn, ref pcData, ref pcDataSchema, ref pcErrorMsg);
  93. }
  94. /// <summary>
  95. /// 执行一个SQL 语句,并将数据跟 sysstatus表数据进行翻译
  96. /// </summary>
  97. /// <param name="pcSql">传入的SQL语句</param>
  98. /// <param name="pcTableName">要翻译的表名</param>
  99. /// <param name="poConn">数据库连接对象</param>
  100. /// <param name="pcData">返回的XML形式的数据集</param>
  101. /// <param name="pcDataSchema">返回的XMLd数据表现框架</param>
  102. /// <param name="pcErrorMsg">返回的错误信息</param>
  103. /// <returns>执行成功返回true 否则 false</returns>
  104. public static bool GetDataOfTranslate(string pcSql, string pcTableName, DBConnSql poConn, ref string pcData, ref string pcDataSchema, ref string pcErrorMsg)
  105. {
  106. bool lbRetVal = false;
  107. if (pcSql.Trim().Length > 0 && pcTableName.Trim().Length > 0 && poConn != null && poConn.IsOpened)
  108. {
  109. try
  110. {
  111. DataSet loDS = poConn.OpenDataSet(pcSql);
  112. if (loDS != null && loDS.Tables.Count == 1)
  113. {
  114. pcData = loDS.GetXml();
  115. pcDataSchema = loDS.GetXmlSchema();
  116. DataTranslateFromSysStatus(ref pcData, pcTableName, poConn);
  117. }
  118. lbRetVal = true;
  119. }
  120. catch (Exception e)
  121. {
  122. ThreadLog.LogException(e);
  123. pcErrorMsg = e.Message;
  124. lbRetVal = false;
  125. }
  126. }
  127. return lbRetVal;
  128. }
  129. public static bool DataTranslateFromSysStatus(ref string pcDataXML, string pcTableName, DBConnSql poConn)
  130. {
  131. bool lbRetVal = false;
  132. if (UtilStr.UAndT(pcDataXML) == UtilStr.UAndT("<NewDataSet />") || pcDataXML == "")
  133. return true;
  134. try
  135. {
  136. // ColName,CodeValue,DisplayValue
  137. string lcSqlTemp = "SELECT " + Sys_Status_info.cColName + "," + Sys_Status_info.cCodeValue + "," + Sys_Status_info.cDisplayValue + " FROM " + Tn.Sys_Status + " "
  138. + " WHERE " + Sys_Status_info.cTableName + " = '" + pcTableName + "'";
  139. rsQuery loQuery = poConn.OpenQuery(lcSqlTemp);
  140. if (loQuery.IsOpened && loQuery.RecCount > 0)
  141. {
  142. rsXmlNode loXmlNode = rsXmlNode.ParseGenericXml(pcDataXML);
  143. if (loXmlNode != null && loXmlNode.Nodes.Count > 0)
  144. {
  145. foreach (rsXmlNode loNode in loXmlNode.Nodes)
  146. {
  147. loQuery.MoveFirst();
  148. for (int i = 0; i < loQuery.RecCount; i++)
  149. {
  150. string lcColumnId = loQuery.GetString(Sys_Status_info.cColName);
  151. lcColumnId = UtilStr.UAndT(lcColumnId);
  152. rsXmlNode loColNode = loNode.GetChildNode(lcColumnId);
  153. if (loColNode != null)
  154. {
  155. string lcValue = loQuery.GetString(Sys_Status_info.cCodeValue);
  156. string lcSValue = loColNode.Value;
  157. if (UtilStr.UAndT(lcSValue) == UtilStr.UAndT(lcValue))
  158. {
  159. string lcDisplay = loQuery.GetString(Sys_Status_info.cDisplayValue);
  160. loColNode.Value = lcDisplay;
  161. }
  162. }
  163. loQuery.MoveNext();
  164. }
  165. }
  166. pcDataXML = loXmlNode.ToXmlString();
  167. lbRetVal = true;
  168. }
  169. }
  170. }
  171. catch(Exception e) {
  172. ThreadLog.LogException(e);
  173. }
  174. return lbRetVal;
  175. }
  176. /// <param name="pcTranslateFromSysStatusTableName">如果不为空则 再从sysStatus表中再翻译</param>
  177. /// <returns></returns>
  178. public static bool GetDataAfterTrans(string pcSql, DBConnSql poConn, List<TransFrame> poList, ref string pcDataXml, ref string pcDataSchema, ref string pcErrorMsg, string pcTranslateFromSysStatusTableName)
  179. {
  180. bool lbRetVal = false;
  181. if (poConn == null)
  182. {
  183. return false;
  184. }
  185. lbRetVal = poConn.GetData(pcSql, ref pcDataXml, ref pcDataSchema);
  186. if (lbRetVal)
  187. {
  188. GetDataAfterTrans(ref pcDataXml, poConn, poList);
  189. if (pcTranslateFromSysStatusTableName.Length > 0)
  190. {
  191. //如果不为空则 再从sysStatus表中再翻译
  192. DataTranslateFromSysStatus(ref pcDataXml, pcTranslateFromSysStatusTableName, poConn);
  193. }
  194. }
  195. else
  196. pcErrorMsg = poConn.ErrorMsg;
  197. return lbRetVal;
  198. }
  199. public static bool GetDataAfterTrans(string pcSql, DBConnSql poConn, List<TransFrame> poList, ref string pcDataXml, ref string pcDataSchema, ref string pcErrorMsg)
  200. {
  201. bool lbRetVal = false;
  202. if (poConn == null)
  203. {
  204. return false;
  205. }
  206. lbRetVal = poConn.GetData(pcSql, ref pcDataXml, ref pcDataSchema);
  207. if (lbRetVal)
  208. {
  209. GetDataAfterTrans(ref pcDataXml, poConn, poList);
  210. }
  211. else
  212. pcErrorMsg = poConn.ErrorMsg;
  213. return lbRetVal;
  214. }
  215. // 扩展
  216. public static bool GetDataAfterTrans(string pcSql, DBConnSql poConn, List<TransFrame> poList, ref string pcDataXml, ref string pcDataSchema, ref string pcErrorMsg, ref int piRecCount)
  217. {
  218. bool lbRetVal = false;
  219. if (poConn == null)
  220. {
  221. return false;
  222. }
  223. lbRetVal = poConn.GetData(pcSql, ref pcDataXml, ref pcDataSchema, ref piRecCount);
  224. if (lbRetVal)
  225. {
  226. GetDataAfterTrans(ref pcDataXml, poConn, poList);
  227. }
  228. else
  229. pcErrorMsg = poConn.ErrorMsg;
  230. return lbRetVal;
  231. }
  232. public static bool GetDataAfterTrans(ref string pcDataXml, DBConnSql poConn, List<TransFrame> poList)
  233. {
  234. bool lbRetVal = false;
  235. try
  236. {
  237. rsXmlNode loXmlNode = rsXmlNode.ParseGenericXml(pcDataXml);
  238. if (loXmlNode != null && loXmlNode.Nodes.Count > 0)
  239. {
  240. Dictionary<string, TransFrame> loDesList = new Dictionary<string, TransFrame>();
  241. foreach (TransFrame loTF in poList)
  242. {
  243. if (loTF.DataTransType == DataTransType.Trans)
  244. {
  245. if (loTF.IsAvail)
  246. {
  247. if (loTF.CreatQuery(poConn))
  248. {
  249. if (loDesList.ContainsKey(loTF.SourceField))
  250. {
  251. loDesList[loTF.SourceField] = loTF;
  252. }
  253. else
  254. loDesList.Add(loTF.SourceField, loTF);
  255. }
  256. }
  257. }
  258. else if (loTF.DataTransType == DataTransType.Format)
  259. {
  260. if (loDesList.ContainsKey(loTF.SourceField))
  261. {
  262. loDesList[loTF.SourceField] = loTF;
  263. }
  264. else
  265. loDesList.Add(loTF.SourceField, loTF);
  266. }
  267. }
  268. if (loDesList.Count > 0)
  269. {
  270. foreach (rsXmlNode loNodes in loXmlNode.Nodes)
  271. {
  272. foreach (string lcKey in loDesList.Keys)
  273. {
  274. rsXmlNode loNode = loNodes.GetChildNode(lcKey);
  275. if (loNode != null)
  276. {
  277. loNode.Value = loDesList[lcKey].GetDisplayValue(loNode.Value);
  278. }
  279. }
  280. }
  281. }
  282. pcDataXml = loXmlNode.ToXmlString();
  283. }
  284. }
  285. catch(Exception e) {
  286. ThreadLog.LogException(e);
  287. }
  288. return lbRetVal;
  289. }
  290. public static void MoveRecordSort(string pcTableName, string pcKeyField, string pcKeyFieldCurValue, string pcSortField, bool pbMoveUp)
  291. {
  292. UserSession loSession = WebLibs.CheckLogin();
  293. string lcSql = "select " + pcKeyField + "," + pcSortField + " from " + pcTableName + " order by " + pcSortField;
  294. rsQuery loQuery = loSession.DBConn.OpenQuery(lcSql);
  295. if (loQuery != null && loQuery.IsOpened && loQuery.RecCount > 0)
  296. {
  297. if (loQuery.GoToRecordByFieldsAndValues(pcKeyField, pcKeyFieldCurValue))
  298. {
  299. int loCurSort = loQuery.GetInt(pcSortField);
  300. int liAim = 0;
  301. string lcAim = "";
  302. if (pbMoveUp)
  303. {
  304. if (loQuery.RecNo > 0)
  305. {
  306. loQuery.MovePrev();
  307. lcAim = loQuery.GetString(pcKeyField);
  308. liAim = loQuery.GetInt(pcSortField);
  309. }
  310. }
  311. else
  312. {
  313. if (loQuery.RecNo < (loQuery.RecCount - 1))
  314. {
  315. loQuery.MoveNext();
  316. lcAim = loQuery.GetString(pcKeyField);
  317. liAim = loQuery.GetInt(pcSortField);
  318. }
  319. }
  320. if (lcAim.Trim().Length > 0)
  321. {
  322. lcSql = "update " + pcTableName + " set " + pcSortField + " = " + liAim + " where " + pcKeyField + "='" + pcKeyFieldCurValue + "' ";
  323. lcSql += "update " + pcTableName + " set " + pcSortField + " = " + loCurSort + " where " + pcKeyField + "='" + lcAim + "' ";
  324. loSession.DBConn.ExcuteSqlTran(lcSql);
  325. }
  326. }
  327. }
  328. }
  329. public static int GetNextSortNumber(string pcTableName, string pcSortField, DBConnSql poConn)
  330. {
  331. int liRetVal = 0;
  332. string lcSql = " select " + pcSortField + " from " + pcTableName + " order by " + pcSortField + " desc ";
  333. rsQuery loQuery = poConn.OpenQuery(lcSql);
  334. if (loQuery != null && loQuery.IsOpened && loQuery.RecCount > 0)
  335. {
  336. loQuery.MoveFirst();
  337. liRetVal = loQuery.GetInt(pcSortField);
  338. liRetVal++;
  339. }
  340. return liRetVal;
  341. }
  342. /// <summary>
  343. /// 判断新插入的键值在表中的是否存在
  344. /// </summary>
  345. /// <param name="pcTableName">表名</param>
  346. /// <param name="pcKeyField">主键字段</param>
  347. /// <param name="pcKeyValue">主键值</param>
  348. /// <param name="poConn">数据库连接串</param>
  349. /// <returns></returns>
  350. public static bool IsExistPrKey(string pcTableName, string pcKeyField, string pcKeyValue, DBConnSql poConn)
  351. {
  352. bool lbRetVal = false;
  353. string lcSql = " select " + pcKeyField + " from " + pcTableName + " where " + pcKeyField + "='" + pcKeyValue + "'";
  354. rsQuery loQuery = poConn.OpenQuery(lcSql);
  355. if (loQuery != null && loQuery.IsOpened && loQuery.RecCount > 0)
  356. {
  357. lbRetVal = true;
  358. }
  359. return lbRetVal;
  360. }
  361. private static SysSettingObj _SysSettingObj = null;
  362. public static SysSettingObj SysSetObj
  363. {
  364. get
  365. {
  366. if (_SysSettingObj == null)
  367. {
  368. _SysSettingObj = new SysSettingObj();
  369. }
  370. return _SysSettingObj;
  371. }
  372. }
  373. /// <summary>
  374. ///
  375. /// </summary>
  376. private static YtDataMappingObj _YtDataMappingObj = null;
  377. public static YtDataMappingObj YtDataMappingObj
  378. {
  379. get
  380. {
  381. if (_YtDataMappingObj == null)
  382. {
  383. _YtDataMappingObj = new YtDataMappingObj();
  384. }
  385. return _YtDataMappingObj;
  386. }
  387. }
  388. public static string GetTextByID(string pcId, int piIdValue, string pcNameCol, string pcTableId, DBConnSql poConn)
  389. {
  390. string lcRetVal = "", lcSql = "";
  391. lcSql = " SELECT " + pcNameCol + " FROM " + pcTableId + " WHERE " + pcId + " =" + piIdValue;
  392. rsQuery loQuery = poConn.OpenQuery(lcSql);
  393. if (loQuery != null && loQuery.IsOpened)
  394. {
  395. lcRetVal = loQuery.GetString(pcNameCol);
  396. }
  397. return lcRetVal;
  398. }
  399. /// <summary>
  400. /// 传入表名及表的ID名和值、需要的字段名、返回所需字段的(string)值
  401. /// </summary>
  402. /// <param name="pcId"></param>
  403. /// <param name="pcIdValue"></param>
  404. /// <param name="pcNameCol"></param>
  405. /// <param name="pcTableId"></param>
  406. /// <param name="poConn"></param>
  407. /// <returns></returns>
  408. public static string GetTextByID(string pcId, string pcIdValue, string pcNameCol, string pcTableId, DBConnSql poConn)
  409. {
  410. string lcRetVal = "", lcSql = "";
  411. lcSql = " SELECT " + pcNameCol + " FROM " + pcTableId + " WHERE " + pcId + " ='" + pcIdValue + "' ";
  412. rsQuery loQuery = poConn.OpenQuery(lcSql);
  413. if (loQuery != null && loQuery.IsOpened)
  414. {
  415. lcRetVal = loQuery.GetString(pcNameCol);
  416. }
  417. return lcRetVal;
  418. }
  419. /// <summary>
  420. /// 传入sql语句 表名 及需要的字段名、返回所需字段的(string)值
  421. /// </summary>
  422. public static string GetTextWithSQL(string pcSQL, string pcColName, DBConnSql poConn)
  423. {
  424. string lcRetVal = "";
  425. rsQuery loQuery = poConn.OpenQuery(pcSQL);
  426. if (loQuery != null && loQuery.IsOpened)
  427. {
  428. lcRetVal = loQuery.GetString(pcColName);
  429. }
  430. return lcRetVal;
  431. }
  432. /// <summary>
  433. /// 从sysstatus表里得到显示值
  434. /// </summary>
  435. /// <param name="pcTable">表名</param>
  436. /// <param name="pcColumn">列名</param>
  437. /// <param name="pcVal">值</param>
  438. /// <returns>显示的值</returns>
  439. public static string GetShowTextFromSysStatus(string pcTable, string pcColumn, string pcVal, DBConnSql poConn)
  440. {
  441. string lcRetVal = "";
  442. string lcSql = "select " + Sys_Status_info.cDisplayValue + " from " + Tn.Sys_Status
  443. + " where " + Sys_Status_info.cTableName + " ='" + pcTable
  444. + "' and " + Sys_Status_info.cColName + "='" + pcColumn
  445. + "' and " + Sys_Status_info.cCodeValue + "='" + pcVal + "'";
  446. rsQuery loQuery = poConn.OpenQuery(lcSql);
  447. if (loQuery != null && loQuery.IsOpened)
  448. {
  449. lcRetVal = loQuery.GetString(Sys_Status_info.cDisplayValue);
  450. }
  451. return lcRetVal;
  452. }
  453. public static decimal GetPreBalance(string pcSellerId, string pcMarketId, DBConnSql poConn)
  454. {
  455. decimal lfRetVal = 0;
  456. if (pcSellerId.Trim().Length > 0 && pcMarketId.Trim().Length > 0 && poConn != null)
  457. {
  458. string lcSql = "select ID, PostBalance from " + Tn.ReserveCompensateLog + " where SellerID ='" + pcSellerId + "' and MarketID='" + pcMarketId + "'";
  459. rsQuery loQuery = poConn.OpenQuery(lcSql);
  460. if (loQuery != null && loQuery.IsOpened && loQuery.RecCount > 0)
  461. {
  462. loQuery.SortBy("ID", false);
  463. loQuery.MoveFirst();
  464. lfRetVal = loQuery.GetDecimal("PostBalance");
  465. }
  466. }
  467. return lfRetVal;
  468. }
  469. /// <summary>
  470. /// 获取KeyValueByStatus
  471. /// </summary>
  472. /// <param name="tableName"></param>
  473. /// <param name="colName"></param>
  474. /// <param name="dbConn"></param>
  475. /// <returns></returns>
  476. public static Dictionary<string,string> GetKeyValueByStatus(string tableName, string colName, DBConnSql dbConn)
  477. {
  478. Dictionary<string, string> loDic = new Dictionary<string, string>();
  479. rsQuery loQuery = dbConn.OpenQuery(
  480. "SELECT CodeValue,DisplayValue FROM Sys_Status WHERE TableName='" + tableName + "' AND ColName='" + colName +
  481. "'");
  482. if (loQuery.IsOpened && loQuery.RecCount > 0)
  483. {
  484. loQuery.MoveFirst();
  485. for (int i = 0; i < loQuery.RecCount; i++)
  486. {
  487. loDic.Add(loQuery.GetString(Sys_Status_info.cCodeValue), loQuery.GetString(Sys_Status_info.cDisplayValue));
  488. loQuery.MoveNext();
  489. }
  490. }
  491. return loDic;
  492. }
  493. /// <summary>
  494. /// 获取KeyValueByTABLE
  495. /// </summary>
  496. /// <param name="tableName"></param>
  497. /// <param name="colName"></param>
  498. /// <param name="dbConn"></param>
  499. /// <returns></returns>
  500. public static Dictionary<string, string> GetKeyValueByTable(string tableName, string DisplayColName, string ValueColName, DBConnSql dbConn, string poWhereSql="")
  501. {
  502. Dictionary<string, string> loDic = new Dictionary<string, string>();
  503. string lcSql = "SELECT "+ ValueColName + ","+ DisplayColName + " FROM " + tableName;
  504. if (!string.IsNullOrEmpty(poWhereSql))
  505. {
  506. lcSql += " WHERE " + poWhereSql;
  507. }
  508. rsQuery loQuery = dbConn.OpenQuery(lcSql);
  509. if (loQuery.IsOpened && loQuery.RecCount > 0)
  510. {
  511. loQuery.MoveFirst();
  512. for (int i = 0; i < loQuery.RecCount; i++)
  513. {
  514. loDic.Add(loQuery.GetString(ValueColName), loQuery.GetString(DisplayColName));
  515. loQuery.MoveNext();
  516. }
  517. }
  518. return loDic;
  519. }
  520. }
  521. }