CNAS取数仪器端升级
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.

GlobalCommonOperation.cs 9.6KB

4 months ago
123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236
  1. using CnasSynchrousModel;
  2. using System;
  3. using System.Collections.Generic;
  4. using System.Data;
  5. using System.IO;
  6. using System.Linq;
  7. using System.Runtime.Serialization;
  8. using System.Runtime.Serialization.Formatters.Binary;
  9. using System.Text;
  10. namespace CnasSynchronusClient
  11. {
  12. /// <summary>
  13. /// 全局通用方法类
  14. /// </summary>
  15. public class GlobalCommonOperation
  16. {
  17. public static string strUserName = "";
  18. public static string strLoginName = "";
  19. public static string strStartWebApi = "";
  20. public static string strStartGeneralVersion = "";
  21. public static string strWebApiUrl = "";
  22. public static string strTargetDbType = "MYSQL";
  23. /// <summary>
  24. /// 定义的一些静态全局参数,后面将改成配置参数存储
  25. /// </summary>
  26. #region 写死的关键字段,已放弃
  27. public static Dictionary<string, List<string>> dictPrimaryKey = new Dictionary<string, List<string>>()
  28. {
  29. { "水分仪" ,new List<string>(){ "SerialNumber" } },
  30. { "测硫仪" ,new List<string>(){"id" } },
  31. { "工业分析仪" ,new List<string>(){ "SerialNumber" } },
  32. { "灰熔点" ,new List<string>(){ "SampleNo" } },
  33. { "红外测硫仪" ,new List<string>(){ "Number" } }
  34. };
  35. public static InstrumentType GetInstruTypeBySelectedItem(string strTitle)
  36. {
  37. InstrumentType type = InstrumentType.None;
  38. switch (strTitle)
  39. {
  40. case "水分仪":
  41. type = InstrumentType.MoistureMeter;
  42. break;
  43. case "测硫仪":
  44. type = InstrumentType.SulphurMeter;
  45. break;
  46. case "灰熔点":
  47. type = InstrumentType.AshMeltingPoint;
  48. break;
  49. case "红外测硫仪":
  50. type = InstrumentType.InfraredSulfurMeter;
  51. break;
  52. case "工业分析仪":
  53. type = InstrumentType.IndustrialAnalyzer;
  54. break;
  55. case "元素分析仪":
  56. type = InstrumentType.ElementalAnalyzer;
  57. break;
  58. case "量热仪":
  59. type = InstrumentType.CalorimetricApparatus;
  60. break;
  61. default:
  62. type = InstrumentType.None;
  63. break;
  64. }
  65. return type;
  66. }
  67. public static string GetTitleByInstruType(InstrumentType type)
  68. {
  69. string strReturn = "";
  70. switch (type)
  71. {
  72. case InstrumentType.SulphurMeter:
  73. strReturn = "测硫仪";
  74. break;
  75. case InstrumentType.IndustrialAnalyzer:
  76. strReturn = "工业分析仪";
  77. break;
  78. case InstrumentType.AshMeltingPoint:
  79. strReturn = "灰熔点";
  80. break;
  81. case InstrumentType.MoistureMeter:
  82. strReturn = "水分仪";
  83. break;
  84. case InstrumentType.InfraredSulfurMeter:
  85. strReturn = "红外测硫仪";
  86. break;
  87. case InstrumentType.CalorimetricApparatus:
  88. strReturn = "量热仪";
  89. break;
  90. case InstrumentType.None:
  91. default:
  92. strReturn = "";
  93. break;
  94. }
  95. return strReturn;
  96. }
  97. /// <summary>
  98. /// 检查存储映射数据是否存在主键值
  99. /// </summary>
  100. /// <returns></returns>
  101. public static string CheckInstuDataHaveKey(List<SyncInstrumentItemInfo> itemInfos)
  102. {
  103. string strErrorMsg = "";
  104. foreach (var item in itemInfos)
  105. {
  106. switch (item.InstruType)
  107. {
  108. case InstrumentType.SulphurMeter:
  109. if(!CheckContainPrimaryKey(item.LstSyncPramas, dictPrimaryKey["测硫仪"]))
  110. strErrorMsg = $"仪器{item.Code}映射列必须包含唯一标识列{ListRowToColumn(dictPrimaryKey["测硫仪"])},无法保存!";
  111. break;
  112. case InstrumentType.IndustrialAnalyzer:
  113. if (!CheckContainPrimaryKey(item.LstSyncPramas, dictPrimaryKey["工业分析仪"]))
  114. strErrorMsg = $"仪器{item.Code}映射列必须包含唯一标识列{ListRowToColumn(dictPrimaryKey["工业分析仪"])},无法保存!";
  115. break;
  116. case InstrumentType.AshMeltingPoint:
  117. if (!CheckContainPrimaryKey(item.LstSyncPramas, dictPrimaryKey["灰熔点"]))
  118. strErrorMsg = $"仪器{item.Code}映射列必须包含唯一标识列{ListRowToColumn(dictPrimaryKey["灰熔点"])},无法保存!";
  119. break;
  120. case InstrumentType.MoistureMeter:
  121. if (!CheckContainPrimaryKey(item.LstSyncPramas, dictPrimaryKey["水分仪"]))
  122. strErrorMsg = $"仪器{item.Code}映射列必须包含唯一标识列{ListRowToColumn(dictPrimaryKey["水分仪"])},无法保存!";
  123. break;
  124. case InstrumentType.InfraredSulfurMeter:
  125. if (!CheckContainPrimaryKey(item.LstSyncPramas, dictPrimaryKey["红外测硫仪"]))
  126. strErrorMsg = $"仪器{item.Code}映射列必须包含唯一标识列{ListRowToColumn(dictPrimaryKey["红外测硫仪"])},无法保存!";
  127. break;
  128. case InstrumentType.CalorimetricApparatus:
  129. if (!CheckContainPrimaryKey(item.LstSyncPramas, dictPrimaryKey["量热仪"]))
  130. strErrorMsg = $"仪器{item.Code}映射列必须包含唯一标识列{ListRowToColumn(dictPrimaryKey["红外测硫仪"])},无法保存!";
  131. break;
  132. case InstrumentType.None:
  133. default:
  134. strErrorMsg = $"仪器{item.Code}未指定仪器类型,无法保存,请先制定仪器类型!";
  135. break;
  136. }
  137. }
  138. return strErrorMsg;
  139. }
  140. private static bool CheckContainPrimaryKey(List<SyncParamasInfo> syncParamasInfos, List<string> primaryKeys)
  141. {
  142. bool bContains = true;
  143. foreach (var key in primaryKeys)
  144. {
  145. if (syncParamasInfos.Where(s => s.SourceField == key).Count() <= 0)
  146. {
  147. bContains = false;
  148. break;
  149. }
  150. }
  151. return bContains;
  152. }
  153. private static string ListRowToColumn(List<string> lst)
  154. {
  155. string strReturnString = "";
  156. foreach (var item in lst)
  157. {
  158. strReturnString += item + ",";
  159. }
  160. if (strReturnString != "")
  161. strReturnString= strReturnString.Substring(0, strReturnString.Length - 1);
  162. return strReturnString;
  163. }
  164. #endregion
  165. /// <summary>
  166. /// 解析IFThen类型字符串
  167. /// </summary>
  168. public static List<IFThenConditionParams> AnanlysisIFThenString(string strString)
  169. {
  170. List<IFThenConditionParams> lstparams = new List<IFThenConditionParams>();
  171. string[] strStringConditions = strString.Split(new string[] {";" }, StringSplitOptions.RemoveEmptyEntries);
  172. if (strStringConditions.Length > 0)
  173. {
  174. foreach (string strConditionString in strStringConditions)
  175. {
  176. string[] strStrings = strConditionString.Split(new string[] { "IF", "THEN", "{", "}"}, StringSplitOptions.RemoveEmptyEntries);
  177. if (strStrings.Length == 4)
  178. {
  179. IFThenConditionParams conditionParam = new IFThenConditionParams
  180. {
  181. ConditionColumnName = strStrings[0],
  182. ConditionAlgorithm=strStrings[1],
  183. ConditionColumnValue=strStrings[2],
  184. ColumnValue=strStrings[3]
  185. };
  186. lstparams.Add(conditionParam);
  187. }
  188. }
  189. }
  190. return lstparams;
  191. }
  192. /// <summary>
  193. /// datarow转换成datatable
  194. /// </summary>
  195. /// <param name="dr"></param>
  196. /// <returns></returns>
  197. public static DataTable ConvertDataRowToTable(DataRow dr)
  198. {
  199. DataTable dt = dr.Table.Clone();
  200. DataRow drNew = dt.NewRow();
  201. drNew.ItemArray = dr.ItemArray;
  202. dt.Rows.Add(drNew);
  203. return dt;
  204. }
  205. /// <summary>
  206. /// 对象的复制方法
  207. /// </summary>
  208. /// <typeparam name="T"></typeparam>
  209. /// <param name="RealObject"></param>
  210. /// <returns></returns>
  211. public static T Clone<T>(T RealObject)
  212. {
  213. using (Stream objectStream = new MemoryStream())
  214. {
  215. //利用 System.Runtime.Serialization序列化与反序列化完成引用对象的复制
  216. IFormatter formatter = new BinaryFormatter();
  217. formatter.Serialize(objectStream, RealObject);
  218. objectStream.Seek(0, SeekOrigin.Begin);
  219. return (T)formatter.Deserialize(objectStream);
  220. }
  221. }
  222. }
  223. }