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.

SyncServiceOperation.cs 15KB

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