IcsFromERPJob
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.

514 lines
19 KiB

4 months ago
4 months ago
  1. using System;
  2. using System.Collections.Generic;
  3. using System.Linq;
  4. using System.Text;
  5. using System.Configuration;
  6. using System.Reflection;
  7. using System.IO;
  8. using System.Diagnostics;
  9. using System.Data.SqlClient;
  10. using System.Data;
  11. using System.Data.Common;
  12. using System.Threading.Tasks;
  13. //using NFine.Data.Extensions;
  14. namespace ICSSoft.FromERP
  15. {
  16. public class ICSHelper
  17. {
  18. private static log4net.ILog log = log4net.LogManager.GetLogger(System.Reflection.MethodBase.GetCurrentMethod().DeclaringType);
  19. public static string GetConnectString()
  20. {
  21. try
  22. {
  23. string connectionString = GetConfigString("SysConnectionString");
  24. return connectionString;
  25. }
  26. catch (Exception)
  27. {
  28. throw;
  29. }
  30. }
  31. public static string GetPLMConnectString()
  32. {
  33. try
  34. {
  35. string connectionString = GetConfigString("SysPlmConnectionString");
  36. return connectionString;
  37. }
  38. catch (Exception)
  39. {
  40. throw;
  41. }
  42. }
  43. public static string GetERPConnectString()
  44. {
  45. try
  46. {
  47. string connectionString = GetConfigString("SysErpConnectionString");
  48. return connectionString;
  49. }
  50. catch (Exception)
  51. {
  52. throw;
  53. }
  54. }
  55. public static string GetConfigString(string name)
  56. {
  57. try
  58. {
  59. Configuration config = GetConfig();
  60. string configString = config.ConnectionStrings.ConnectionStrings[name].ConnectionString.ToString();
  61. return configString;
  62. }
  63. catch (Exception)
  64. {
  65. throw;
  66. }
  67. }
  68. public static Dictionary<string, string> GetConfigString()
  69. {
  70. try
  71. {
  72. Dictionary<string, string> dictionary = new Dictionary<string, string>();
  73. Configuration config = GetConfig();
  74. var settings = config.AppSettings.Settings;
  75. foreach (var key in settings.AllKeys)
  76. {
  77. string value = settings[key].Value.ToString();
  78. dictionary.Add(key.ToString(), value);
  79. }
  80. return dictionary;
  81. }
  82. catch (Exception)
  83. {
  84. throw;
  85. }
  86. }
  87. public static Configuration GetConfig()
  88. {
  89. Assembly assembly = Assembly.GetCallingAssembly();
  90. string path = string.Format("{0}.config", assembly.Location);
  91. if (!File.Exists(path))
  92. {
  93. throw new FileNotFoundException(path + "路径下的文件未找到!");
  94. }
  95. try
  96. {
  97. ExeConfigurationFileMap configFile = new ExeConfigurationFileMap();
  98. configFile.ExeConfigFilename = path;
  99. Configuration config = ConfigurationManager.OpenMappedExeConfiguration(configFile, ConfigurationUserLevel.None);
  100. return config;
  101. }
  102. catch (Exception)
  103. {
  104. throw;
  105. }
  106. }
  107. public static void Log(string name)
  108. {
  109. string procName = Process.GetCurrentProcess().ProcessName;
  110. using (PerformanceCounter pc = new PerformanceCounter("Process", "Private Bytes", procName))
  111. {
  112. log.Warn(name + "-当前程序内存占用:" + pc.NextValue());
  113. }
  114. long memoryUsed = GC.GetTotalMemory(true);
  115. log.Warn(name + "-内存占用:" + memoryUsed);
  116. }
  117. public static string Time(string Namespace, string ClassName, string TenantId, string sql, string TempTableName)
  118. {
  119. try
  120. {
  121. string value = @"DECLARE @LastTime datetime='2000-01-01'
  122. --
  123. IF NOT EXISTS(SELECT ID FROM ICSERPTime WHERE Namespace='{0}' AND ClassName='{1}' AND TenantId='{2}')
  124. BEGIN
  125. INSERT INTO ICSERPTime VALUES('{0}','{1}',@LastTime
  126. ,'','','{2}'
  127. ,@LastTime,'job',@LastTime,'job','job','job')
  128. END
  129. --
  130. SELECT @LastTime=ModifyDate FROM ICSERPTime WHERE Namespace='{0}' AND ClassName='{1}' AND TenantId='{2}'
  131. --MTIME字段
  132. {3}
  133. --
  134. SELECT @LastTime=MAX(MTIME) FROM {4}
  135. --
  136. UPDATE ICSERPTime SET ModifyDate=@LastTime,LastModificationTime=GETDATE(),LastModifierUserName='job' ,LastModifierUserId='job'
  137. WHERE Namespace='{0}' AND ClassName='{1}' AND TenantId='{2}'
  138. ";
  139. return string.Format(value, Namespace, ClassName, TenantId, sql, TempTableName);
  140. }
  141. catch (Exception ex)
  142. {
  143. log.Error(ex.ToString());
  144. throw;
  145. }
  146. }
  147. public static string InsertSQL(string TableName, Dictionary<string, string> values)
  148. {
  149. try
  150. {
  151. string col = string.Empty;
  152. string value = string.Empty;
  153. foreach (var item in values)
  154. {
  155. col += item.Key + ",";
  156. value += item.Value + ",";
  157. }
  158. if (!string.IsNullOrWhiteSpace(value))
  159. {
  160. return string.Format("INSERT INTO {0} ({1}) SELECT {2} FROM ", TableName, col.TrimEnd(','), value.TrimEnd(','));
  161. }
  162. return value;
  163. }
  164. catch (Exception ex)
  165. {
  166. log.Error(ex.ToString());
  167. throw;
  168. }
  169. }
  170. public static string UpdateSQL(string TableName, Dictionary<string, string> values)
  171. {
  172. try
  173. {
  174. string value = string.Empty;
  175. foreach (var item in values)
  176. {
  177. value += item.Key +"="+ item.Value + ",";
  178. }
  179. if (!string.IsNullOrWhiteSpace(value))
  180. {
  181. return string.Format("UPDATE {0} SET {1} FROM ", TableName, value.TrimEnd(','));
  182. }
  183. return "";
  184. }
  185. catch (Exception ex)
  186. {
  187. log.Error(ex.ToString());
  188. throw;
  189. }
  190. }
  191. public static void ExecuteDate(string conStr, string sql)
  192. {
  193. try
  194. {
  195. using (SqlConnection con = new SqlConnection(conStr))
  196. {
  197. con.Open();
  198. try
  199. {
  200. Dictionary<string, string> dictionary = new Dictionary<string, string>();
  201. using (SqlTransaction tran = con.BeginTransaction())
  202. {
  203. using (SqlCommand command = new SqlCommand())
  204. {
  205. command.Connection = con;
  206. command.Transaction = tran;
  207. command.CommandTimeout = 100;
  208. command.CommandText = sql;
  209. try
  210. {
  211. int result = command.ExecuteNonQuery();
  212. tran.Commit();
  213. }
  214. catch (Exception ex)
  215. {
  216. tran.Rollback();
  217. log.Error( ex.ToString() + Environment.NewLine + "异常SQL:" + Environment.NewLine + sql);
  218. }
  219. }
  220. }
  221. }
  222. catch (Exception ex)
  223. {
  224. log.Error(ex.ToString());
  225. throw ex;
  226. }
  227. finally
  228. {
  229. if (con.State == ConnectionState.Open)
  230. con.Close();
  231. con.Dispose();
  232. }
  233. }
  234. }
  235. catch (Exception ex)
  236. {
  237. log.Error(ex.ToString());
  238. throw ex;
  239. }
  240. }
  241. /// <summary>
  242. /// 获取拼音
  243. /// </summary>
  244. /// <param name="str"></param>
  245. /// <returns></returns>
  246. public static string GetPYString(string str)
  247. {
  248. string tempStr = "";
  249. foreach (char c in str)
  250. {
  251. if ((int)c >= 33 && (int)c <= 126)
  252. {//字母和符号原样保留
  253. tempStr += c.ToString();
  254. }
  255. else
  256. {//累加拼音声母
  257. tempStr += GetPYChar(c.ToString());
  258. }
  259. }
  260. return tempStr;
  261. }
  262. ///
  263. /// 取单个字符的拼音声母
  264. ///
  265. /// 要转换的单个汉字
  266. /// 拼音声母
  267. public static string GetPYChar(string c)
  268. {
  269. byte[] array = new byte[2];
  270. array = System.Text.Encoding.Default.GetBytes(c);
  271. int i = (short)(array[0] - '\0') * 256 + ((short)(array[1] - '\0'));
  272. if (i < 0xB0A1) return "*";
  273. if (i < 0xB0C5) return "a";
  274. if (i < 0xB2C1) return "b";
  275. if (i < 0xB4EE) return "c";
  276. if (i < 0xB6EA) return "d";
  277. if (i < 0xB7A2) return "e";
  278. if (i < 0xB8C1) return "f";
  279. if (i < 0xB9FE) return "g";
  280. if (i < 0xBBF7) return "h";
  281. if (i < 0xBFA6) return "j";
  282. if (i < 0xC0AC) return "k";
  283. if (i < 0xC2E8) return "l";
  284. if (i < 0xC4C3) return "m";
  285. if (i < 0xC5B6) return "n";
  286. if (i < 0xC5BE) return "o";
  287. if (i < 0xC6DA) return "p";
  288. if (i < 0xC8BB) return "q";
  289. if (i < 0xC8F6) return "r";
  290. if (i < 0xCBFA) return "s";
  291. if (i < 0xCDDA) return "t";
  292. if (i < 0xCEF4) return "w";
  293. if (i < 0xD1B9) return "x";
  294. if (i < 0xD4D1) return "y";
  295. if (i < 0xD7FA) return "z";
  296. return "*";
  297. }
  298. /// <summary>
  299. /// 返回一个数据表
  300. /// </summary>
  301. /// <param name="connectionString">数据库连接字符串</param>
  302. /// <param name="cmdType">命令类型</param>
  303. /// <param name="cmdText">SQL语句</param>
  304. /// <param name="commandParameters">参数</param>
  305. /// <param name="strTableName">数据表名</param>
  306. /// <param name="bIsLoadStru">是否加载数据表结构</param>
  307. /// <returns></returns>
  308. public static DataTable ExecuteTable(string connection, string cmdText)
  309. {
  310. try
  311. {
  312. using (SqlConnection con = new SqlConnection(connection))
  313. {
  314. con.Open();
  315. try
  316. {
  317. Dictionary<string, string> dictionary = new Dictionary<string, string>();
  318. using (SqlCommand command = new SqlCommand())
  319. {
  320. command.CommandText = cmdText;
  321. DataSet ds = new DataSet();
  322. SqlDataAdapter da = new SqlDataAdapter(command);
  323. da.SelectCommand.Connection = con; //加上这个
  324. //da.FillSchema(ds, SchemaType.Source);
  325. da.Fill(ds);
  326. return ds.Tables[0];
  327. }
  328. }
  329. catch (Exception ex)
  330. {
  331. log.Error(ex.ToString());
  332. throw ex;
  333. }
  334. finally
  335. {
  336. if (con.State == ConnectionState.Open)
  337. con.Close();
  338. con.Dispose();
  339. }
  340. }
  341. }
  342. catch (Exception ex)
  343. {
  344. log.Error(ex.ToString());
  345. throw ex;
  346. }
  347. }
  348. public static DataTable GetERPDB(string conStr)
  349. {
  350. try
  351. {
  352. string value = @"SELECT * FROM SysWorkPoint";
  353. return ExecuteTable(conStr, value);
  354. }
  355. catch (Exception ex)
  356. {
  357. log.Error(ex.ToString());
  358. throw;
  359. }
  360. }
  361. public static object ExecuteScalar(string connString, string sql)
  362. {
  363. using (SqlConnection conn = new SqlConnection(connString))
  364. {
  365. conn.Open();
  366. object obj = null;
  367. try
  368. {
  369. SqlCommand cmd = new SqlCommand(sql, conn);
  370. //if (spArr.Length > 0)
  371. // cmd.Parameters.AddRange(spArr.SetDBNull());
  372. obj = cmd.ExecuteScalar();
  373. }
  374. catch (Exception ex)
  375. {
  376. log.Error(ex.ToString());
  377. throw ex;
  378. }
  379. finally
  380. {
  381. if (conn.State == ConnectionState.Open)
  382. conn.Close();
  383. //conn.Dispose();
  384. }
  385. return obj;
  386. }
  387. }
  388. public static DateTime GetTime(string Conn, string Namespace, string ClassName, string TenantId)
  389. {
  390. try
  391. {
  392. string value = @"DECLARE @LastTime datetime='2000-01-01'
  393. --
  394. IF NOT EXISTS(SELECT ID FROM ICSERPTime WHERE Namespace='{0}' AND ClassName='{1}' AND TenantId='{2}')
  395. BEGIN
  396. INSERT INTO ICSERPTime VALUES('{0}','{1}',@LastTime
  397. ,'','','{2}'
  398. ,@LastTime,'job',@LastTime,'job','job','job')
  399. END
  400. --
  401. SELECT @LastTime=ModifyDate FROM ICSERPTime WHERE Namespace='{0}' AND ClassName='{1}' AND TenantId='{2}'
  402. --MTIME字段
  403. Select @LastTime
  404. ";
  405. return ExecuteScalar(Conn, string.Format(value, Namespace, ClassName, TenantId)).ToDateTime();
  406. }
  407. catch (Exception ex)
  408. {
  409. log.Error(ex.ToString());
  410. throw;
  411. }
  412. }
  413. public static void UpdateTime(string Conn, string Namespace, string ClassName, string TenantId, DateTime time)
  414. {
  415. try
  416. {
  417. string value = @"DECLARE @LastTime datetime='2000-01-01'
  418. --
  419. IF NOT EXISTS(SELECT ID FROM ICSERPTime WHERE Namespace='{0}' AND ClassName='{1}' AND TenantId='{2}')
  420. BEGIN
  421. INSERT INTO ICSERPTime VALUES('{0}','{1}',@LastTime
  422. ,'','','{2}'
  423. ,@LastTime,'job',@LastTime,'job','job','job')
  424. END
  425. select @LastTime='{3}'
  426. --
  427. UPDATE ICSERPTime SET ModifyDate=@LastTime,LastModificationTime=GETDATE(),LastModifierUserName='job' ,LastModifierUserId='job'
  428. WHERE Namespace='{0}' AND ClassName='{1}' AND TenantId='{2}'
  429. ";
  430. ExecuteScalar(Conn, string.Format(value, Namespace, ClassName, TenantId, time.ToStringBz()));
  431. }
  432. catch (Exception ex)
  433. {
  434. log.Error(ex.ToString());
  435. throw;
  436. }
  437. }
  438. public static void ExecuteDateNew(string conStr, string sql)
  439. {
  440. using (SqlConnection con = new SqlConnection(conStr))
  441. {
  442. con.Open();
  443. try
  444. {
  445. Dictionary<string, string> dictionary = new Dictionary<string, string>();
  446. using (SqlTransaction tran = con.BeginTransaction())
  447. {
  448. using (SqlCommand command = new SqlCommand())
  449. {
  450. command.Connection = con;
  451. command.Transaction = tran;
  452. command.CommandTimeout = 100;
  453. command.CommandText = sql;
  454. try
  455. {
  456. int result = command.ExecuteNonQuery();
  457. tran.Commit();
  458. }
  459. catch (Exception ex)
  460. {
  461. tran.Rollback();
  462. throw ex;
  463. }
  464. }
  465. }
  466. }
  467. catch (Exception ex)
  468. {
  469. log.Error(ex.ToString() + Environment.NewLine + "异常SQL:" + Environment.NewLine + sql);
  470. throw ex;
  471. }
  472. finally
  473. {
  474. if (con.State == ConnectionState.Open)
  475. con.Close();
  476. con.Dispose();
  477. }
  478. }
  479. }
  480. }
  481. }