/Las/Las.WinUI/Utility.cs

http://sgsoft-las.googlecode.com/ · C# · 193 lines · 149 code · 31 blank · 13 comment · 23 complexity · fea6f284bbbc04756d9b2f5fd84ca5ab MD5 · raw file

  1. using System;
  2. using System.Collections.Generic;
  3. using System.Text;
  4. using System.Windows.Forms;
  5. using Common.Logging;
  6. namespace Las.WinUI
  7. {
  8. public class Utility
  9. {
  10. public const int KEY_LEVEL_DEFINE_ALL = 99;
  11. static readonly ILog log = LogManager.GetCurrentClassLogger();
  12. static readonly Dictionary<int, string> _YearDic;
  13. public static Dictionary<int, string> YearDic
  14. {
  15. get { return Utility._YearDic; }
  16. }
  17. static readonly Dictionary<int, string> _DwLevelDefineDic;
  18. public static Dictionary<int, string> DwLevelDefineDic
  19. {
  20. get { return Utility._DwLevelDefineDic; }
  21. }
  22. static readonly Dictionary<int, string> _RyLevelDefineDic;
  23. public static Dictionary<int, string> RyLevelDefineDic
  24. {
  25. get { return Utility._RyLevelDefineDic; }
  26. }
  27. /// <summary>
  28. /// ???????=1,??=2????;????????????
  29. /// </summary>
  30. static readonly Dictionary<int, string> _EntityTypeDefineDic;
  31. public static Dictionary<int, string> EntityTypeDefineDic
  32. {
  33. get { return Utility._EntityTypeDefineDic; }
  34. }
  35. static Utility()
  36. {
  37. #region ???????????3????????7??
  38. int yearCount = 7;
  39. string yearCountStr = System.Configuration.ConfigurationManager.AppSettings["COMBOX_YEAR_COUNT"];
  40. if (!String.IsNullOrEmpty(yearCountStr))
  41. {
  42. if (!Int32.TryParse(yearCountStr, out yearCount))
  43. {
  44. yearCount = 7;
  45. }
  46. if (yearCount > 20) yearCount = 7;
  47. }
  48. _YearDic = new Dictionary<int, string>(yearCount);
  49. int minYear = DateTime.Today.Year - (yearCount / 2);
  50. for (int i = 0; i < yearCount; i++)
  51. {
  52. _YearDic.Add(minYear + i, (minYear + i).ToString());
  53. }
  54. #endregion
  55. #region ????????
  56. _DwLevelDefineDic = new Dictionary<int, string>();
  57. string dwLevelDefineDicStr = System.Configuration.ConfigurationManager.AppSettings["DW_LEVEL_DEFINE_DIC"];
  58. if (!String.IsNullOrEmpty(dwLevelDefineDicStr))
  59. {
  60. string[] strArray = dwLevelDefineDicStr.Split(new char[] { ',' }, StringSplitOptions.RemoveEmptyEntries);
  61. if (strArray == null || (strArray.Length % 2) != 0)
  62. {
  63. log.ErrorFormat("??????.??????DW_LEVEL_DEFINE_DIC={0}", dwLevelDefineDicStr);
  64. }
  65. else
  66. {
  67. int key = 0;
  68. for (int i = 0; i < strArray.Length/2; i++)
  69. {
  70. if (Int32.TryParse(strArray[2*i], out key))
  71. {
  72. _DwLevelDefineDic.Add(key, strArray[2 * i + 1]);
  73. }
  74. else
  75. {
  76. _DwLevelDefineDic.Clear();
  77. log.ErrorFormat("??????.??????DW_LEVEL_DEFINE_DIC={0}", dwLevelDefineDicStr);
  78. break;
  79. }
  80. }
  81. }
  82. }
  83. if(_DwLevelDefineDic.Count==0)
  84. {
  85. if (log.IsInfoEnabled) log.Info("??????????????:0??1???2 ??");
  86. _DwLevelDefineDic.Add(0,"?");
  87. _DwLevelDefineDic.Add(1,"??");
  88. _DwLevelDefineDic.Add(2,"??");
  89. }
  90. #endregion
  91. #region ????????
  92. _RyLevelDefineDic = new Dictionary<int, string>();
  93. string ryLevelDefineDicStr = System.Configuration.ConfigurationManager.AppSettings["RY_LEVEL_DEFINE_DIC"];
  94. if (!String.IsNullOrEmpty(ryLevelDefineDicStr))
  95. {
  96. string[] strArray = ryLevelDefineDicStr.Split(new char[] {','},StringSplitOptions.RemoveEmptyEntries);
  97. if (strArray == null || (strArray.Length % 2) != 0)
  98. {
  99. log.ErrorFormat("??????.??????RY_LEVEL_DEFINE_DIC={0}", ryLevelDefineDicStr);
  100. }
  101. else
  102. {
  103. int key = 0;
  104. for (int i = 0; i < strArray.Length/2; i++)
  105. {
  106. if (Int32.TryParse(strArray[2*i], out key))
  107. {
  108. _RyLevelDefineDic.Add(key,strArray[2*i+1]);
  109. }
  110. else
  111. {
  112. _RyLevelDefineDic.Clear();
  113. log.ErrorFormat("??????.??????RY_LEVEL_DEFINE_DIC={0}", ryLevelDefineDicStr);
  114. break;
  115. }
  116. }
  117. }
  118. }
  119. if (_RyLevelDefineDic.Count == 0)
  120. {
  121. if (log.IsInfoEnabled) log.Info("??????????????:0??1????2???3??");
  122. _RyLevelDefineDic.Add(0, "?");
  123. _RyLevelDefineDic.Add(1, "???");
  124. _RyLevelDefineDic.Add(2, "??");
  125. _RyLevelDefineDic.Add(3, "??");
  126. }
  127. #endregion
  128. #region ????
  129. _EntityTypeDefineDic = new Dictionary<int, string>(2);
  130. _EntityTypeDefineDic.Add(KEY_LEVEL_DEFINE_ALL, "??");
  131. _EntityTypeDefineDic.Add(1, "??");
  132. _EntityTypeDefineDic.Add(2, "??");
  133. #endregion
  134. }
  135. /// <summary>
  136. /// ????Dictionary??????????
  137. /// </summary>
  138. /// <param name="srcDic"></param>
  139. /// <returns></returns>
  140. public static Dictionary<int, string> CloneLevelDictionaryWithKeyAll(Dictionary<int, string> src)
  141. {
  142. Dictionary<int, string> t = new Dictionary<int, string>(src.Count + 1);
  143. foreach (int i in src.Keys)
  144. {
  145. t.Add(i, src[i]);
  146. }
  147. t.Add(KEY_LEVEL_DEFINE_ALL, "??");
  148. return t;
  149. }
  150. /// <summary>
  151. /// ??Combox???Dictionary???
  152. /// </summary>
  153. /// <param name="cb"></param>
  154. /// <param name="dic"></param>
  155. public static void BindCombox2Dictionary(ComboBox cb, System.Collections.IDictionary dic)
  156. {
  157. cb.DataSource = new BindingSource(dic, null);
  158. cb.DisplayMember = "Value";
  159. cb.ValueMember = "Key";
  160. }
  161. }
  162. }