ConvertRule.cs 24 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877
  1. using Microsoft.AspNetCore.Http;
  2. using System;
  3. using System.Collections;
  4. using System.Collections.Generic;
  5. using System.ComponentModel;
  6. using System.Data;
  7. using System.Linq;
  8. using System.Reflection;
  9. using System.Text;
  10. using System.Threading.Tasks;
  11. using System.Xml;
  12. namespace WebAPIBase.Utils
  13. {
  14. public class ConvertRule
  15. {
  16. public static decimal ToDecimal(object val)
  17. {
  18. decimal result = default(decimal);
  19. if (val != null && val.ToString() != "")
  20. {
  21. try
  22. {
  23. try
  24. {
  25. result = Convert.ToDecimal(val);
  26. return result;
  27. }
  28. catch
  29. {
  30. result = decimal.Parse(val.ToString());
  31. return result;
  32. }
  33. }
  34. catch
  35. {
  36. }
  37. }
  38. return result;
  39. }
  40. public static object ToDecimalObj(object val)
  41. {
  42. object result = DBNull.Value;
  43. if (val != null && val.ToString() != "")
  44. {
  45. try
  46. {
  47. result = decimal.Parse(val.ToString());
  48. }
  49. catch
  50. {
  51. }
  52. }
  53. return result;
  54. }
  55. public static double ToDouble(object val)
  56. {
  57. double result = 0.0;
  58. if (val != null && val.ToString() != "")
  59. {
  60. try
  61. {
  62. result = double.Parse(val.ToString());
  63. }
  64. catch
  65. {
  66. }
  67. }
  68. return result;
  69. }
  70. public static object ToDoubleObj(object val)
  71. {
  72. object result = DBNull.Value;
  73. if (val != null && val.ToString() != "")
  74. {
  75. try
  76. {
  77. result = double.Parse(val.ToString());
  78. }
  79. catch
  80. {
  81. }
  82. }
  83. return result;
  84. }
  85. public static int ToInt(object val)
  86. {
  87. int result = 0;
  88. if (val != null && val.ToString() != "")
  89. {
  90. try
  91. {
  92. result = int.Parse(val.ToString().Replace(",", ""));
  93. }
  94. catch
  95. {
  96. }
  97. }
  98. return result;
  99. }
  100. public static int? ToIntNull(object val)
  101. {
  102. int? result = null;
  103. if (val != null && val.ToString() != "")
  104. {
  105. try
  106. {
  107. result = int.Parse(val.ToString().Replace(",", ""));
  108. return result;
  109. }
  110. catch
  111. {
  112. }
  113. }
  114. return result;
  115. }
  116. public static object ToIntObj(object val)
  117. {
  118. object result = DBNull.Value;
  119. if (val != null && val.ToString() != "")
  120. {
  121. try
  122. {
  123. result = int.Parse(val.ToString().Replace(",", ""));
  124. }
  125. catch
  126. {
  127. }
  128. }
  129. return result;
  130. }
  131. public static long ToLong(object val)
  132. {
  133. long result = 0L;
  134. if (val != null && val.ToString() != "")
  135. {
  136. try
  137. {
  138. result = long.Parse(val.ToString());
  139. }
  140. catch
  141. {
  142. }
  143. }
  144. return result;
  145. }
  146. public static object ToLongObj(object val)
  147. {
  148. object result = DBNull.Value;
  149. if (val != null && val.ToString() != "")
  150. {
  151. try
  152. {
  153. result = long.Parse(val.ToString());
  154. }
  155. catch
  156. {
  157. }
  158. }
  159. return result;
  160. }
  161. public static object ToDate(object val)
  162. {
  163. object result = DBNull.Value;
  164. if (val != null && val.ToString() != "")
  165. {
  166. try
  167. {
  168. result = DateTime.Parse(val.ToString());
  169. }
  170. catch
  171. {
  172. }
  173. }
  174. return result;
  175. }
  176. public static DateTime? ToDateTime(object val)
  177. {
  178. DateTime? result = null;
  179. if (val != null && val.ToString() != "")
  180. {
  181. try
  182. {
  183. result = DateTime.Parse(val.ToString());
  184. return result;
  185. }
  186. catch
  187. {
  188. }
  189. }
  190. return result;
  191. }
  192. public static string ToDateString(object val, string format)
  193. {
  194. string result = "";
  195. if (val != null && val.ToString() != "")
  196. {
  197. try
  198. {
  199. if (DateTime.Parse(val.ToString()) != DateTime.MinValue)
  200. {
  201. result = DateTime.Parse(val.ToString()).ToString(format);
  202. }
  203. }
  204. catch
  205. {
  206. }
  207. }
  208. return result;
  209. }
  210. public static string ToDateString(object val, string format, string nullTimeFormat)
  211. {
  212. string result = "";
  213. if (val != null && val.ToString() != "")
  214. {
  215. try
  216. {
  217. if (DateTime.Parse(val.ToString()) != DateTime.MinValue)
  218. {
  219. DateTime dateTime = DateTime.Parse(val.ToString());
  220. result = ((dateTime.Hour != 0 || dateTime.Minute != 0 || dateTime.Second != 0) ? dateTime.ToString(format) : dateTime.ToString(nullTimeFormat));
  221. }
  222. }
  223. catch
  224. {
  225. }
  226. }
  227. return result;
  228. }
  229. public static string ToDateString(object val)
  230. {
  231. return ToDateString(val, "yyyy-MM-dd");
  232. }
  233. public static string ToString(object val)
  234. {
  235. string result = "";
  236. try
  237. {
  238. if (val != null && val != DBNull.Value)
  239. {
  240. try
  241. {
  242. result = val.ToString();
  243. }
  244. catch
  245. {
  246. }
  247. }
  248. }
  249. catch
  250. {
  251. }
  252. return result;
  253. }
  254. public static bool ToBool(object val)
  255. {
  256. bool result = false;
  257. try
  258. {
  259. if (val != null && val != DBNull.Value)
  260. {
  261. try
  262. {
  263. result = bool.Parse(val.ToString());
  264. }
  265. catch
  266. {
  267. }
  268. }
  269. }
  270. catch
  271. {
  272. }
  273. return result;
  274. }
  275. public static string[] ArrayConcat(object[] arr1, object[] arr2)
  276. {
  277. int num = 0;
  278. if (arr1 != null)
  279. {
  280. num += arr1.Length;
  281. }
  282. if (arr2 != null)
  283. {
  284. num += arr2.Length;
  285. }
  286. string[] array = new string[num];
  287. int num2 = 0;
  288. if (arr1 != null)
  289. {
  290. arr1.CopyTo(array, num2);
  291. num2 += arr1.Length;
  292. }
  293. if (arr2 != null)
  294. {
  295. arr2.CopyTo(array, num2);
  296. num2 += arr2.Length;
  297. }
  298. return array;
  299. }
  300. public static string JoinArray(object[] arr, string separator)
  301. {
  302. string text = "";
  303. int num = arr.Length;
  304. for (int i = 0; i < num; i++)
  305. {
  306. if (i > 0)
  307. {
  308. text += separator;
  309. }
  310. text += arr[i].ToString();
  311. }
  312. return text;
  313. }
  314. public static string JoinArrayList(ArrayList al, string separator)
  315. {
  316. string text = "";
  317. int count = al.Count;
  318. for (int i = 0; i < count; i++)
  319. {
  320. if (i > 0)
  321. {
  322. text += separator;
  323. }
  324. text += ToString(al[i]);
  325. }
  326. return text;
  327. }
  328. public static string JoinTableColumn(DataTable tb, string ColumnName, string separator)
  329. {
  330. string text = "";
  331. int count = tb.Rows.Count;
  332. for (int i = 0; i < count; i++)
  333. {
  334. if (i > 0)
  335. {
  336. text += separator;
  337. }
  338. text += ToString(tb.Rows[i][ColumnName]);
  339. }
  340. return text;
  341. }
  342. public static int FindArray(string[] arr, string val)
  343. {
  344. return FindArray(arr, val, isEgnoreCase: false);
  345. }
  346. public static int FindArray(string[] arr, string val, bool isEgnoreCase)
  347. {
  348. int result = -1;
  349. if (arr == null)
  350. {
  351. return result;
  352. }
  353. int num = arr.Length;
  354. for (int i = 0; i < num; i++)
  355. {
  356. if (isEgnoreCase)
  357. {
  358. if (arr[i].ToUpper() == val.ToUpper())
  359. {
  360. return i;
  361. }
  362. }
  363. else if (arr[i] == val)
  364. {
  365. return i;
  366. }
  367. }
  368. return result;
  369. }
  370. public static int FindArrayLike(string[] arr, string val)
  371. {
  372. int num = arr.Length;
  373. int length = val.Length;
  374. for (int i = 0; i < num; i++)
  375. {
  376. string text = arr[i];
  377. if (text.Length > length)
  378. {
  379. text = text.Substring(0, length);
  380. }
  381. if (text == val)
  382. {
  383. return i;
  384. }
  385. }
  386. return -1;
  387. }
  388. public static object GetArrayItem(object[] arr, int index)
  389. {
  390. object result = null;
  391. if (arr.Length > index)
  392. {
  393. result = arr[index];
  394. }
  395. return result;
  396. }
  397. public static string GetArrayItemString(object[] arr, int index)
  398. {
  399. object arrayItem = GetArrayItem(arr, index);
  400. return ToString(arrayItem);
  401. }
  402. public static string Concat(DataTable tb, string ColumnName, string sep)
  403. {
  404. try
  405. {
  406. string text = "";
  407. return Concat(tb, ColumnName, sep, "");
  408. }
  409. catch (Exception ex)
  410. {
  411. throw ex;
  412. }
  413. }
  414. public static string Concat(DataTable tb, string ColumnName, string sep, string filter)
  415. {
  416. try
  417. {
  418. string text = "";
  419. if (!tb.Columns.Contains(ColumnName))
  420. {
  421. throw new ApplicationException($"表中未包含列{ColumnName}");
  422. }
  423. DataRow[] drs = tb.Select(filter);
  424. return Concat(drs, ColumnName, sep);
  425. }
  426. catch (Exception ex)
  427. {
  428. throw ex;
  429. }
  430. }
  431. public static string Concat(DataRow[] drs, string ColumnName, string sep)
  432. {
  433. try
  434. {
  435. string text = "";
  436. int num = drs.Length;
  437. for (int i = 0; i < num; i++)
  438. {
  439. DataRow dataRow = drs[i];
  440. if (i > 0)
  441. {
  442. text += sep;
  443. }
  444. text += ToString(dataRow[ColumnName]);
  445. }
  446. return text;
  447. }
  448. catch (Exception ex)
  449. {
  450. throw ex;
  451. }
  452. }
  453. public static DataTable GetDistinct(DataTable tb, string ColumnName, string filter)
  454. {
  455. try
  456. {
  457. if (!tb.Columns.Contains(ColumnName))
  458. {
  459. throw new ApplicationException($"表中未包含列{ColumnName}");
  460. }
  461. DataRow[] drs = tb.Select(filter);
  462. return GetDistinct(drs, ColumnName);
  463. }
  464. catch (Exception ex)
  465. {
  466. throw ex;
  467. }
  468. }
  469. public static DataTable GetDistinct(DataRow[] drs, string ColumnName)
  470. {
  471. try
  472. {
  473. DataTable dataTable = new DataTable();
  474. if (drs.Length != 0)
  475. {
  476. dataTable = drs[0].Table.Clone();
  477. }
  478. else
  479. {
  480. Type type = typeof(string);
  481. if (drs.Length != 0)
  482. {
  483. type = drs[0].Table.Columns[ColumnName].DataType;
  484. }
  485. dataTable.Columns.Add(ColumnName, type);
  486. }
  487. int num = drs.Length;
  488. for (int i = 0; i < num; i++)
  489. {
  490. DataRow dataRow = drs[i];
  491. string str = ToString(dataRow[ColumnName]);
  492. if (dataTable.Select(ColumnName + "='" + str + "'").Length == 0)
  493. {
  494. dataTable.ImportRow(drs[i]);
  495. }
  496. }
  497. return dataTable;
  498. }
  499. catch (Exception ex)
  500. {
  501. throw ex;
  502. }
  503. }
  504. public static DataView GetDistinct(DataView dv, string ColumnName)
  505. {
  506. try
  507. {
  508. DataTable dataTable = new DataTable();
  509. Type type = typeof(string);
  510. if (dv.Count > 0 && dv[0].Row[ColumnName].GetType() != typeof(DBNull))
  511. {
  512. type = dv[0].Row[ColumnName].GetType();
  513. }
  514. dataTable.Columns.Add(ColumnName, type);
  515. int count = dv.Count;
  516. for (int i = 0; i < count; i++)
  517. {
  518. string text = ToString(dv[i].Row[ColumnName]);
  519. if (dataTable.Select(ColumnName + "='" + text + "'").Length == 0)
  520. {
  521. DataRow dataRow = dataTable.NewRow();
  522. dataRow[ColumnName] = text;
  523. dataTable.Rows.Add(dataRow);
  524. }
  525. }
  526. return new DataView(dataTable);
  527. }
  528. catch (Exception ex)
  529. {
  530. throw ex;
  531. }
  532. }
  533. public static string GetDistinctStr(DataTable tb, string ColumnName, string filter, string sep)
  534. {
  535. try
  536. {
  537. if (!tb.Columns.Contains(ColumnName))
  538. {
  539. throw new ApplicationException($"表中未包含列{ColumnName}");
  540. }
  541. DataRow[] drs = tb.Select(filter);
  542. return GetDistinctStr(drs, ColumnName, sep);
  543. }
  544. catch (Exception ex)
  545. {
  546. throw ex;
  547. }
  548. }
  549. public static string GetDistinctStr(DataRow[] drs, string ColumnName, string sep)
  550. {
  551. try
  552. {
  553. DataTable distinct = GetDistinct(drs, ColumnName);
  554. return Concat(distinct, ColumnName, sep);
  555. }
  556. catch (Exception ex)
  557. {
  558. throw ex;
  559. }
  560. }
  561. public static string ToEnumString(object objValue, Type enumType)
  562. {
  563. string result = "";
  564. string b = ToString(objValue);
  565. foreach (object value in Enum.GetValues(enumType))
  566. {
  567. if (((int)value).ToString() == b)
  568. {
  569. result = value.ToString();
  570. break;
  571. }
  572. }
  573. return result;
  574. }
  575. public static string ToEnumDescription(object objValue, Type enumType)
  576. {
  577. string result = "";
  578. string b = ToString(objValue);
  579. foreach (object value in Enum.GetValues(enumType))
  580. {
  581. if (((int)value).ToString() == b)
  582. {
  583. FieldInfo field = value.GetType().GetField(value.ToString());
  584. DescriptionAttribute[] array = (DescriptionAttribute[])field.GetCustomAttributes(typeof(DescriptionAttribute), inherit: false);
  585. result = ((array.Length == 0) ? value.ToString() : array[0].Description);
  586. break;
  587. }
  588. }
  589. return result;
  590. }
  591. public static int ToEnumIndex(object objValue, Type enumType)
  592. {
  593. int result = -1;
  594. string b = ToString(objValue);
  595. foreach (object value in Enum.GetValues(enumType))
  596. {
  597. if (value.ToString() == b)
  598. {
  599. result = (int)value;
  600. break;
  601. }
  602. }
  603. return result;
  604. }
  605. public static bool IsDateStartLessThanEnd(object StartDate, object EndDate)
  606. {
  607. try
  608. {
  609. bool flag = true;
  610. object obj = ToDate(StartDate);
  611. object obj2 = ToDate(EndDate);
  612. if (obj == DBNull.Value || obj2 == DBNull.Value)
  613. {
  614. return true;
  615. }
  616. DateTime t = (DateTime)obj;
  617. DateTime t2 = (DateTime)obj2;
  618. return t <= t2;
  619. }
  620. catch (Exception ex)
  621. {
  622. throw ex;
  623. }
  624. }
  625. public static void DataRowCopy(DataRow drSrc, DataRow drDst, DataTable tbSrc, DataTable tbDst)
  626. {
  627. try
  628. {
  629. DataRowCopy(drSrc, drDst, tbSrc, tbDst, null);
  630. }
  631. catch (Exception ex)
  632. {
  633. throw ex;
  634. }
  635. }
  636. public static void DataRowCopy(DataRow drSrc, DataRow drDst, DataTable tbSrc, DataTable tbDst, string[] ExceptField)
  637. {
  638. try
  639. {
  640. foreach (DataColumn column in tbDst.Columns)
  641. {
  642. if (tbSrc.Columns.Contains(column.ColumnName) && FindArray(ExceptField, column.ColumnName, isEgnoreCase: true) < 0)
  643. {
  644. drDst[column.ColumnName] = drSrc[column.ColumnName];
  645. }
  646. }
  647. }
  648. catch (Exception ex)
  649. {
  650. throw ex;
  651. }
  652. }
  653. public static void DataTableCopyRow(DataTable tbSrc, DataTable tbDst)
  654. {
  655. try
  656. {
  657. foreach (DataRow row in tbSrc.Rows)
  658. {
  659. DataRow dataRow = tbDst.NewRow();
  660. DataRowCopy(row, dataRow, tbSrc, tbDst);
  661. tbDst.Rows.Add(dataRow);
  662. }
  663. }
  664. catch (Exception ex)
  665. {
  666. throw ex;
  667. }
  668. }
  669. public static void DataTableCopyRow(DataView dvSrc, DataTable tbDst)
  670. {
  671. try
  672. {
  673. DataTable table = dvSrc.Table;
  674. foreach (DataRowView item in dvSrc)
  675. {
  676. DataRow row = item.Row;
  677. DataRow dataRow = tbDst.NewRow();
  678. DataRowCopy(row, dataRow, table, tbDst);
  679. tbDst.Rows.Add(dataRow);
  680. }
  681. }
  682. catch (Exception ex)
  683. {
  684. throw ex;
  685. }
  686. }
  687. public static void DataTableAddColumn(DataTable tbSrc, DataTable tbDst)
  688. {
  689. try
  690. {
  691. foreach (DataColumn column in tbSrc.Columns)
  692. {
  693. if (!tbDst.Columns.Contains(column.ColumnName))
  694. {
  695. tbDst.Columns.Add(column.ColumnName, column.DataType);
  696. }
  697. }
  698. }
  699. catch (Exception ex)
  700. {
  701. throw ex;
  702. }
  703. }
  704. public static void DataRowAddDecimal(DataRow drBase, DataRow drAdd, string ColumnName)
  705. {
  706. try
  707. {
  708. drBase[ColumnName] = ToDecimal(drBase[ColumnName]) + ToDecimal(drAdd[ColumnName]);
  709. }
  710. catch (Exception ex)
  711. {
  712. throw ex;
  713. }
  714. }
  715. public static void DataRowAddDecimal(DataRow drBase, DataRow drAdd, string[] ColumnNames)
  716. {
  717. try
  718. {
  719. foreach (string columnName in ColumnNames)
  720. {
  721. DataRowAddDecimal(drBase, drAdd, columnName);
  722. }
  723. }
  724. catch (Exception ex)
  725. {
  726. throw ex;
  727. }
  728. }
  729. public static string GetArrayLinkString(ArrayList ar)
  730. {
  731. return GetArrayLinkString(ar, ",");
  732. }
  733. public static string GetArrayLinkString(ArrayList ar, string separator)
  734. {
  735. string text = "";
  736. int count = ar.Count;
  737. for (int i = 0; i < count; i++)
  738. {
  739. if (text != "")
  740. {
  741. text += separator;
  742. }
  743. text += (string)ar[i];
  744. }
  745. return text;
  746. }
  747. public static int GetIntDayOfWeek(DayOfWeek d)
  748. {
  749. try
  750. {
  751. int result = 0;
  752. switch (d.ToString())
  753. {
  754. case "Monday":
  755. result = 1;
  756. break;
  757. case "Tuesday":
  758. result = 2;
  759. break;
  760. case "Wednesday":
  761. result = 3;
  762. break;
  763. case "Thursday":
  764. result = 4;
  765. break;
  766. case "Friday":
  767. result = 5;
  768. break;
  769. case "Saturday":
  770. result = 6;
  771. break;
  772. case "Sunday":
  773. result = 7;
  774. break;
  775. }
  776. return result;
  777. }
  778. catch (Exception ex)
  779. {
  780. throw ex;
  781. }
  782. }
  783. public static string FormatMM(object objMonth)
  784. {
  785. try
  786. {
  787. string text = ToString(objMonth);
  788. return text.PadLeft(2, "0"[0]);
  789. }
  790. catch (Exception ex)
  791. {
  792. throw ex;
  793. }
  794. }
  795. public static string FormatYYYYMM(object objYear, object objMonth)
  796. {
  797. try
  798. {
  799. string text = ToString(objYear);
  800. string text2 = ToString(objMonth);
  801. return text.PadLeft(4, "0"[0]) + text2.PadLeft(2, "0"[0]);
  802. }
  803. catch (Exception ex)
  804. {
  805. throw ex;
  806. }
  807. }
  808. public static int GetDays(string StartDate, string FinishDate)
  809. {
  810. int result = 0;
  811. if (!string.IsNullOrEmpty(StartDate) && !string.IsNullOrEmpty(FinishDate))
  812. {
  813. TimeSpan timeSpan = new TimeSpan(DateTime.Parse(StartDate).Ticks);
  814. TimeSpan ts = new TimeSpan(DateTime.Parse(FinishDate).Ticks);
  815. result = timeSpan.Subtract(ts).Duration().Days;
  816. }
  817. return result;
  818. }
  819. }
  820. }