CNAS取数仪器端升级
Du kannst nicht mehr als 25 Themen auswählen Themen müssen entweder mit einem Buchstaben oder einer Ziffer beginnen. Sie können Bindestriche („-“) enthalten und bis zu 35 Zeichen lang sein.

368 Zeilen
15KB

  1. using CnasSynchronousCommon;
  2. using CnasSynchronusClient;
  3. using CnasSynchrousModel;
  4. using System;
  5. using System.Collections.Generic;
  6. using System.Data;
  7. using System.IO;
  8. using System.Linq;
  9. using System.Text;
  10. using System.Timers;
  11. using System.Xml.Serialization;
  12. namespace CNAS_SyncService
  13. {
  14. public class SyncServiceOperation
  15. {
  16. System.Timers.Timer timerSync;
  17. ServiceConfig config = new ServiceConfig();
  18. bool bRunComplete = true;
  19. int WaitCount = 0;
  20. string strNowDate = "";
  21. FileWatcherOperation fileWatcherOperation;
  22. public void Start()
  23. {
  24. AppLog.ServiceInfo("SyncSerivce:服务启动");
  25. //读取全局配置信息
  26. SystemFormatConfig systemFormat = FileOperation.GetSystemFormatConfigData();
  27. GlobalCommonOperation.strStartGeneralVersion = systemFormat.StartGeneralVersion;
  28. GlobalCommonOperation.strStartWebApi = systemFormat.StartWebApi;
  29. GlobalCommonOperation.strWebApiUrl = systemFormat.WebApiUrl;
  30. GlobalCommonOperation.strTargetDbType = systemFormat.TargetDBType;
  31. //读取服务配置信息
  32. config = ReadServiceConfigData();
  33. AppLog.Error("打印日志1" + config.Minutes);
  34. if (config.Minutes != 0)
  35. {
  36. //启动文件/文件夹监控
  37. if (config.IfStartFileWatcher == "1")
  38. {
  39. fileWatcherOperation = new FileWatcherOperation(ReadSyncInstruments());
  40. fileWatcherOperation.GetFileWatcherItems();
  41. fileWatcherOperation.StartFileWatcher();
  42. }
  43. // 启动定时器
  44. timerSync = new System.Timers.Timer();
  45. timerSync.Interval = config.Minutes * 60 * 1000; //设置计时器事件间隔执行时间
  46. timerSync.Elapsed += new System.Timers.ElapsedEventHandler(timerSync_Elapsed);
  47. timerSync.Enabled = true;
  48. timerSync.AutoReset = true;
  49. }
  50. else
  51. {
  52. //写入日志
  53. AppLog.ServiceInfo("SyncSerivce:未能成功读取定时器时间,本次同步失败。");
  54. }
  55. }
  56. private void timerSync_Elapsed(object sender, ElapsedEventArgs e)
  57. {
  58. AppLog.ServiceInfo("定时器启动");
  59. //如果等待执行次数超出最大数值,则认为当前正在执行出现未知名问题,直接回收当前执行,执行下次
  60. if (WaitCount >= config.MaxWaitCount)
  61. {
  62. //回收垃圾
  63. GC.Collect();
  64. bRunComplete = true;
  65. }
  66. if (bRunComplete)
  67. {
  68. WaitCount = 0; //只要正常执行,等待标识都将恢复为0
  69. bRunComplete = false;
  70. //清理过期日志
  71. if (strNowDate != DateTime.Now.ToString("yyyyMMdd"))
  72. {
  73. AppLog.ServiceInfo("清理过期日志");
  74. strNowDate = DateTime.Now.ToString("yyyyMMdd");
  75. //删除日志文件
  76. string logDays = FileOperation.GetSystemFormatConfigData().ShowLogDays;
  77. int logday;
  78. if (int.TryParse(logDays, out logday))
  79. AppLog.DeleteLog(logday);
  80. }
  81. //执行同步程序
  82. //1.读取配置信息
  83. List<SyncInstrumentItemInfo> lstSyncInstrument = ReadSyncInstruments();
  84. //2 遍历仪器列表,逐个列表进行同步
  85. foreach (var instrumentItem in lstSyncInstrument)
  86. {
  87. //如果是文件,使用FileSystemWatcher查看文件是否发生了变化
  88. if (fileWatcherOperation != null && fileWatcherOperation.LstWatcherItems.Count > 0)
  89. {
  90. var watcherItem = fileWatcherOperation.LstWatcherItems.Where(p => p.SyncItemId == instrumentItem.GUID && p.SyncIfChanged == true).FirstOrDefault();
  91. if (watcherItem!=null)
  92. {
  93. RunSyncByInstrument(instrumentItem,watcherItem);
  94. }
  95. else
  96. {
  97. AppLog.ServiceInfo("检测到文件/文件夹没有发生变化,跳过本次同步执行");
  98. }
  99. }
  100. else
  101. {
  102. RunSyncByInstrument(instrumentItem);
  103. }
  104. }
  105. bRunComplete = true;
  106. //回收垃圾
  107. GC.Collect();
  108. }
  109. else
  110. {
  111. WaitCount++;
  112. AppLog.ServiceInfo($"等待标识:{WaitCount}");
  113. }
  114. }
  115. private List<SyncInstrumentItemInfo> ReadSyncInstruments()
  116. {
  117. List<SyncInstrumentItemInfo> lstSyncInstrument = new List<SyncInstrumentItemInfo>();
  118. lstSyncInstrument = FileOperation.GetLocalSyncInStrumentData();
  119. AppLog.ServiceInfo($"读取到本地配置信息行数{lstSyncInstrument.Count}");
  120. return lstSyncInstrument;
  121. }
  122. private void RunSyncByInstrument(SyncInstrumentItemInfo syncInstrumentItem,FileWatcherItem fileWatcherItem=null)
  123. {
  124. //0.读取目标库表数据的最大时间 1.读取来源的数据 2.对读取到的数据进行逻辑处理,整理格式 3.执行同步
  125. AppLog.ServiceInfo("准备根据日期字段查询筛选");
  126. var query = syncInstrumentItem.LstSyncPramas.Where(s => s.IfDateField == true).ToList<SyncParamasInfo>();
  127. if (query.Count == 1)
  128. {
  129. string strCompareTime = "";
  130. string strDateTime = CnasDataOperationFact.CnasDataOperation().GetMaxTimeByTableName(syncInstrumentItem.SyncTargetDBInfo, syncInstrumentItem.LstSyncPramas[0].TargetTable, query[0].TargetField, syncInstrumentItem.CnasInstrumentColumn,syncInstrumentItem.GUID);
  131. //strDateTime = "2016-07-01 00:00:00";
  132. if (strDateTime == "1899-1-1")
  133. {
  134. AppLog.ServiceInfo($"读取上次最晚执行时间失败,请检查原因。");
  135. return;
  136. }
  137. else if (strDateTime == "")
  138. {
  139. strCompareTime = Convert.ToDateTime(config.InitalDT.ToString()).ToString("yyyy-MM-dd HH:mm:ss");
  140. }
  141. else
  142. strCompareTime = Convert.ToDateTime(strDateTime).AddDays(-3).ToString("yyyy-MM-dd HH:mm:ss");
  143. AppLog.ServiceInfo($"读取上次最晚执行时间为{strCompareTime}");
  144. object[] obj = new object[]
  145. {
  146. syncInstrumentItem.LstSyncPramas[0].SourceTable,
  147. query[0].SourceField,
  148. strCompareTime
  149. };
  150. AppLog.ServiceInfo($"准备构建数据源读取工厂");
  151. InstrumentData instrumentData = InstrumentDataFact.CreateInstrumentDataSource(syncInstrumentItem.SyncInstrumentDSInfo, obj);
  152. AppLog.ServiceInfo($"构建数据源读取工厂完成,准备读取数据源");
  153. DataTable dtReadySource = instrumentData.GetInstrumentDataByDate();
  154. AppLog.ServiceInfo($"根据日期读取准备插入的来源数据,共{dtReadySource.Rows.Count}条数据");
  155. string strErrorMsg = "";
  156. RunUpLoad(syncInstrumentItem, dtReadySource, ref strErrorMsg);
  157. if (strErrorMsg != "")
  158. {
  159. //写入日志
  160. AppLog.ServiceInfo(strErrorMsg);
  161. }
  162. else
  163. {
  164. if (fileWatcherItem != null) fileWatcherItem.SyncIfChanged = false;
  165. }
  166. }
  167. else
  168. {
  169. AppLog.ServiceInfo("SyncSerivce:未能成功读取日期字段,可能不存在或存在多个日期字段,本次同步失败。");
  170. }
  171. }
  172. private void RunUpLoad(SyncInstrumentItemInfo syncInstrumentItem,DataTable dtReadySource,ref string strMsg)
  173. {
  174. //根据映射字段获取准备上传的所有数据
  175. if (dtReadySource == null || dtReadySource.Rows.Count <= 0) return;
  176. //构建准备插入的数据
  177. DataTable dtTarget = CnasDataOperationFact.CnasDataOperation().GetCNASTablesStruct(syncInstrumentItem.LstSyncPramas[0].TargetTable, syncInstrumentItem.SyncTargetDBInfo);
  178. AppLog.ServiceInfo("读取目标数据的数据结构");
  179. if (dtTarget.Columns.Count <= 0)
  180. {
  181. strMsg="未能成功读取CNAS数据库列,请检查数据库配置。";
  182. return;
  183. }
  184. CnasInsertOperation insertOperation = new CnasInsertOperation()
  185. {
  186. syncInstrumentItem = syncInstrumentItem
  187. };
  188. AppLog.ServiceInfo("遍历所有来源数据,构造准备插入的数据");
  189. strMsg=insertOperation.CreateInputData(dtReadySource, ref dtTarget);
  190. if (strMsg != "") return;
  191. if (dtTarget.Rows.Count <= 0)
  192. {
  193. strMsg = "创建准备插入的数据行为空。";
  194. return;
  195. }
  196. AppLog.ServiceInfo("检查准备插入的数据是否合法");
  197. insertOperation.CheckInsertDataFormat(dtTarget, ref strMsg);
  198. if (strMsg != "") return;
  199. //逐行执行插入
  200. List<DataRow> lstError = new List<DataRow>();
  201. int ErrorCount = 0;
  202. int SuccessCount = 0;
  203. int OtherCount = 0;
  204. AppLog.ServiceInfo($"逐行插入目标数据,最终准备插入(更新)数据行{dtTarget.Rows.Count}");
  205. foreach (DataRow dr in dtTarget.Rows)
  206. {
  207. if (dr[11].ToString().Length > 1)
  208. {
  209. AppLog.Error($"逐行插入目标数据,最终准备插入(更新)数据行{dr[11]}");
  210. dr[11] = kxjsf(dr[11].ToString());
  211. }
  212. if (dr[12].ToString().Length > 1)
  213. {
  214. AppLog.Error($"逐行插入目标数据,最终准备插入(更新)数据行{dr[12]}");
  215. }
  216. if (dr[13].ToString().Length > 1)
  217. {
  218. AppLog.Error($"逐行插入目标数据,最终准备插入(更新)数据行{dr[13]}");
  219. }
  220. if (dr[14].ToString().Length > 1)
  221. {
  222. AppLog.Error($"逐行插入目标数据,最终准备插入(更新)数据行{dr[14]}");
  223. }
  224. if (dr[15].ToString().Length > 1)
  225. {
  226. AppLog.Error($"逐行插入目标数据,最终准备插入(更新)数据行{dr[15]}");
  227. }
  228. if (dr[16].ToString().Length > 1)
  229. {
  230. AppLog.Error($"逐行插入目标数据,最终准备插入(更新)数据行{dr[16]}");
  231. }
  232. dr.AcceptChanges();
  233. int iReturn = CnasDataOperationFact.CnasDataOperation().InsertDataToCNASTable(GlobalCommonOperation.ConvertDataRowToTable(dr), syncInstrumentItem.SyncTargetDBInfo, syncInstrumentItem.LstSyncPramas, syncInstrumentItem.CnasInstrumentColumn, syncInstrumentItem.lstFixedValue);
  234. if (iReturn <= 0) //此时出现问题
  235. {
  236. if (iReturn == -1)
  237. {
  238. AppLog.ServiceInfo("数据库连接中断,终止本次上传。");
  239. break; //此时数据库连接中断,直接跳出循环,结束本次数据同步传输
  240. }
  241. else if (iReturn == -2) //等于-2表示插入准备更新时发现数据一致,不再执行
  242. {
  243. OtherCount++;
  244. }
  245. else
  246. {
  247. ErrorCount++;
  248. //lstError.Add(GlobalCommonOperation.ConvertDataRowToTable(dr).Rows[0]);
  249. }
  250. }
  251. else
  252. {
  253. SuccessCount++;
  254. AppLog.Info("成功插入(更新)1条数据。");
  255. }
  256. }
  257. AppLog.ServiceInfo($"上传操作完成!其中成功{SuccessCount}条,失败{ErrorCount}条,其他{OtherCount}条。............................................................................................................");
  258. }
  259. private string kxjsf(string sss)
  260. {
  261. double sd = Convert.ToDouble(sss);
  262. //double sd = 123.123456;
  263. string temps2 = "0";
  264. string temps3 = "0";
  265. string temps4 = "0";
  266. int tempi2 = 0;
  267. int tempi3 = 0;
  268. int tempi4 = 0;
  269. if (sss.Contains('.'))
  270. {
  271. string[] s1 = sss.Split('.');
  272. if (s1[1].Length > 2)
  273. {
  274. if (s1[1].Length > 2)
  275. {
  276. temps2 = s1[1].Substring(1, 1);
  277. temps3 = s1[1].Substring(2, 1);
  278. }
  279. tempi2 = int.Parse(temps2);
  280. tempi3 = int.Parse(temps3);
  281. if (s1[1].Length > 3)
  282. {
  283. temps4 = s1[1].Substring(3, 1);
  284. }
  285. tempi4 = int.Parse(temps4);
  286. if (tempi3 > 5 || tempi3 < 5)
  287. {
  288. return sd.ToString("0.00");
  289. }
  290. else
  291. {
  292. if (tempi4 != 0)
  293. {
  294. return sd.ToString("0.00");
  295. }
  296. else
  297. {
  298. if (tempi2 == 0 || tempi2 == 2 || tempi2 == 4 || tempi2 == 6 || tempi2 == 8)
  299. {
  300. return sss.Substring(0, 5);
  301. }
  302. else
  303. {
  304. return sd.ToString("0.00");
  305. }
  306. }
  307. }
  308. }
  309. }
  310. return sss;
  311. }
  312. private ServiceConfig ReadServiceConfigData()
  313. {
  314. ServiceConfig config = new ServiceConfig();
  315. try
  316. {
  317. //读取本地文件中存储的配置信息
  318. XmlSerializer serializer = new XmlSerializer(config.GetType());
  319. FileStream stream = new FileStream(FileHelper.getBasePath() + "/DataConfig/SyncServiceData.xml", FileMode.Open);
  320. config = (ServiceConfig)serializer.Deserialize(stream);
  321. stream.Close();
  322. }
  323. catch (Exception ex)
  324. {
  325. AppLog.ServiceInfo(ex.Message);
  326. }
  327. return config;
  328. }
  329. }
  330. }