FrmStandAloneMetering.cs 28 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700
  1. using com.hnshituo.core.webapp.vo;
  2. using Common;
  3. using System;
  4. using System.Collections.Generic;
  5. using System.Configuration;
  6. using System.Data;
  7. using System.IO;
  8. using System.IO.Ports;
  9. using System.Linq;
  10. using System.Text;
  11. using System.Windows.Forms;
  12. namespace FrmStandAloneMetering
  13. {
  14. /// <summary>
  15. /// 仪表状态
  16. /// </summary>
  17. public enum MeterStatus
  18. {
  19. /// <summary>
  20. /// Stable 稳定
  21. /// </summary>
  22. Stable,
  23. /// <summary>
  24. /// 不稳定
  25. /// </summary>
  26. UnStable,
  27. /// <summary>
  28. /// 空磅
  29. /// </summary>
  30. Null,
  31. ///// <summary>
  32. ///// 未连接
  33. ///// </summary>
  34. UnConnect,
  35. ///// <summary>
  36. ///// 已连接
  37. ///// </summary>
  38. Connect
  39. }
  40. public partial class FrmStandAloneMetering : Form
  41. {
  42. private string strComPara; //仪表参数
  43. private int messageLength; //报文长度
  44. private string strSeparate;//分割符
  45. private int startPosition; //开始取值位置
  46. private int dataLength; //数据长度
  47. private int sleepTime; //采样频率
  48. private int stableTime; //稳定时间
  49. private int stableCount; //稳定次数
  50. private int stableDiff; //稳定次数
  51. private double tmpWeight; //临时重量,用于判断是否稳定状态
  52. private int weightStabCount; //波动次数
  53. private SerialPort serialPort1; //数据采集串口
  54. private bool blThreadFlag;//数据采集线程开关
  55. private StringBuilder weightLog = new StringBuilder(); //重量日志的文件
  56. private MeterWorkCarActFirAlonService alonService = new MeterWorkCarActFirAlonService();
  57. public FrmStandAloneMetering()
  58. {
  59. InitializeComponent();
  60. }
  61. private void FrmStandAloneMetering_Load(object sender, EventArgs e)
  62. {
  63. strComPara = ConfigurationManager.AppSettings["ComPara"].ToString().Trim(); //仪表参数COM口
  64. messageLength = Convert.ToInt32(ConfigurationManager.AppSettings["MessageLength"].ToString().Trim()); //报文长度
  65. strSeparate = ConfigurationManager.AppSettings["Separate"].ToString().Trim();//分割符
  66. startPosition = Convert.ToInt32(ConfigurationManager.AppSettings["StartPosition"].ToString().Trim());//开始取值位置
  67. dataLength = Convert.ToInt32(ConfigurationManager.AppSettings["DataLength"].ToString().Trim());// 数据长度
  68. sleepTime = Convert.ToInt32(ConfigurationManager.AppSettings["SleepTime"].ToString().Trim());//采样频率
  69. stableTime = Convert.ToInt32(ConfigurationManager.AppSettings["StableTime"].ToString().Trim());//稳定时间
  70. stableDiff = Convert.ToInt32(ConfigurationManager.AppSettings["StableDiff"].ToString().Trim());//稳定重量
  71. stableCount = stableTime * 1000 / sleepTime;//稳定次数
  72. //界面COM赋值
  73. if (!String.IsNullOrEmpty(strComPara) && strComPara.Contains(","))
  74. {
  75. string[] strParams = strComPara.Split(new char[] { ',' });
  76. cbChooseCom.Text = strParams[0];
  77. }
  78. //界面稳定时间赋值
  79. cbStableSchedule.Text = stableTime.ToString();
  80. tmpWeight = 0;//临时重量
  81. weightStabCount = 0; //波动次数
  82. }
  83. #region 用户控件的事件
  84. /// <summary>
  85. /// 开启数据采集
  86. /// </summary>
  87. /// <param name="sender"></param>
  88. /// <param name="e"></param>
  89. private void btnCollection_Click(object sender, EventArgs e)
  90. {
  91. if (blThreadFlag)
  92. {
  93. MessageBox.Show("采集程序已经运行,禁止重新进行采集!");
  94. return;
  95. }
  96. blThreadFlag = true;
  97. System.Threading.Thread DataCollectThread = new System.Threading.Thread(new System.Threading.ThreadStart(DataCollect));
  98. DataCollectThread.Start();
  99. }
  100. /// <summary>
  101. /// 上传本地的计量数据
  102. /// </summary>
  103. /// <param name="sender"></param>
  104. /// <param name="e"></param>
  105. private void btnUploadData_Click(object sender, EventArgs e)
  106. {
  107. uploadData();
  108. }
  109. /// <summary>
  110. /// 下载服务器的基础数据
  111. /// </summary>
  112. /// <param name="sender"></param>
  113. /// <param name="e"></param>
  114. private void btnDownloadData_Click(object sender, EventArgs e)
  115. {
  116. // 删除CSV文件
  117. Utils.FileUtil.DeleteFile(AppDomain.CurrentDomain.BaseDirectory + "baseData\\MeterBaseMatterInfo.csv");
  118. // 获取物料信息
  119. MeterBaseMatterInfoService meterBaseMatterInfo = new MeterBaseMatterInfoService();//物料服务
  120. MeterBaseMatterInfo MatterInfo1 = new MeterBaseMatterInfo();
  121. MatterInfo1.validFlag = "1";
  122. MatterInfo1.pageNum = 1;
  123. MatterInfo1.pageSize = 9999;
  124. RESTfulResult<List<MeterBaseMatterInfo>> rmx = meterBaseMatterInfo.doQueryListLike(MatterInfo1);
  125. // 重新保存CSV文件
  126. saveMatterInfo(rmx);
  127. // 删除CSV文件
  128. Utils.FileUtil.DeleteFile(AppDomain.CurrentDomain.BaseDirectory + "baseData\\MeterBaseCustomerSupplier.csv");
  129. // 获取客商信息
  130. MeterBaseCustomerSupplierService service2 = new MeterBaseCustomerSupplierService();//物料服务
  131. MeterBaseCustomerSupplier customerInfo1 = new MeterBaseCustomerSupplier();
  132. customerInfo1.validFlag = "1";
  133. RESTfulResult<List<MeterBaseCustomerSupplier>> rmx2 = service2.doQuery(customerInfo1);
  134. // 重新保存CSV文件
  135. saveCustomerInfo(rmx2);
  136. }
  137. /// <summary>
  138. /// 数据查询
  139. /// </summary>
  140. /// <param name="sender"></param>
  141. /// <param name="e"></param>
  142. private void btQuery_Click(object sender, EventArgs e)
  143. {
  144. doQuery();
  145. }
  146. /// <summary>
  147. /// 毛重保存
  148. /// </summary>
  149. /// <param name="sender"></param>
  150. /// <param name="e"></param>
  151. private void btSaveGross_Click(object sender, EventArgs e)
  152. {
  153. saveData("0"); //毛重保存(0=毛重;1=常规皮重)
  154. doQuery();
  155. }
  156. /// <summary>
  157. /// 皮重保存
  158. /// </summary>
  159. /// <param name="sender"></param>
  160. /// <param name="e"></param>
  161. private void btSaveTare_Click(object sender, EventArgs e)
  162. {
  163. saveData("1"); //毛重保存(0=毛重;1=常规皮重)
  164. doQuery();
  165. }
  166. /// <summary>
  167. /// 界面关闭
  168. /// </summary>
  169. /// <param name="sender"></param>
  170. /// <param name="e"></param>
  171. private void FrmStandAloneMetering_FormClosing(object sender, FormClosingEventArgs e)
  172. {
  173. blThreadFlag = false;
  174. System.Threading.Thread.Sleep(sleepTime * 2);
  175. if (serialPort1 != null)
  176. {
  177. serialPort1.Close();
  178. }
  179. Application.Exit();
  180. }
  181. /// <summary>
  182. /// 稳定时间选择事件
  183. /// </summary>
  184. /// <param name="sender"></param>
  185. /// <param name="e"></param>
  186. private void cbStableSchedule_TextChanged(object sender, EventArgs e)
  187. {
  188. stableTime = Convert.ToInt32(cbStableSchedule.Text.Trim());//稳定时间
  189. stableCount = stableTime * 1000 / sleepTime;//稳定次数
  190. }
  191. /// <summary>
  192. /// COM口下拉事件
  193. /// </summary>
  194. /// <param name="sender"></param>
  195. /// <param name="e"></param>
  196. private void cbChooseCom_TextChanged(object sender, EventArgs e)
  197. {
  198. if (blThreadFlag)
  199. {
  200. MessageBox.Show("采集程序已经运行,禁止重新调整COM口!");
  201. return;
  202. }
  203. }
  204. #endregion 用户控件的事件
  205. #region 数据采集
  206. /// <summary>
  207. /// 数据采集线程
  208. /// </summary>
  209. private void DataCollect()
  210. {
  211. if (!blThreadFlag) return;
  212. string[] strParams = strComPara.Split(new char[] { ',' });
  213. Parity parity = Parity.None;
  214. if (strParams[2].ToUpper() == "ODD")
  215. {
  216. parity = Parity.Odd;
  217. }
  218. if (strParams[2].ToUpper() == "EVEN")
  219. {
  220. parity = Parity.Even;
  221. }
  222. StopBits stopBits = StopBits.None;
  223. if (strParams[4] == "1")
  224. {
  225. stopBits = StopBits.One;
  226. }
  227. if (strParams[4] == "1.5")
  228. {
  229. stopBits = StopBits.OnePointFive;
  230. }
  231. if (strParams[4] == "2")
  232. {
  233. stopBits = StopBits.Two;
  234. }
  235. serialPort1 = new SerialPort(strParams[0], Int32.Parse(strParams[1]),
  236. parity, Int32.Parse(strParams[3]), stopBits);
  237. while (!serialPort1.IsOpen)
  238. {
  239. try
  240. {
  241. serialPort1.Open();
  242. }
  243. catch (Exception err)
  244. {
  245. WriteCatchLog(err.ToString());
  246. }
  247. System.Threading.Thread.Sleep(500);
  248. }
  249. StringBuilder stringBuilder = new StringBuilder();
  250. string strtmp = "";
  251. while (blThreadFlag)
  252. {
  253. try
  254. {
  255. System.Threading.Thread.Sleep(sleepTime);
  256. if (serialPort1.BytesToRead > 0)
  257. {
  258. strtmp = serialPort1.ReadExisting();
  259. stringBuilder.Append(strtmp);
  260. //"US,GS,+0073.34 t\r\nUS,GS,+0073.96 t\r\nUS,GS,+0071.36 t\r\nUS,GS,+0070.40 t\r\nUS,GS,+0073.06 t\r\nUS,GS,+0073.16 t\r\nUS,GS,+0071.72 t\r\nUS,GS,+0073.30 t\r\nUS,GS,+0074.16 t\r\n"
  261. //0252.50 kg
  262. //20 20 0D 0A 7F 30 32 35 32 2E 35 30 20 6B 67
  263. //03 04 0D 0A FF 30 30 30 30 31 35 20 6B 67
  264. //char strSeparate = (char)0x6B;
  265. if (stringBuilder.ToString().LastIndexOf(strSeparate) >= 0)
  266. {
  267. if (stringBuilder.ToString().LastIndexOf(strSeparate) < messageLength)//不完整报文,抛掉
  268. {
  269. stringBuilder.Remove(0, stringBuilder.ToString().LastIndexOf(strSeparate) + strSeparate.Length);
  270. //stringBuilder.Remove(0, stringBuilder.ToString().LastIndexOf(strSeparate) + strSeparate.Length);
  271. }
  272. int index = stringBuilder.ToString().LastIndexOf(strSeparate);
  273. if ((index - messageLength) < 0)
  274. continue;
  275. string strData = stringBuilder.ToString().Substring(index - messageLength, messageLength + strSeparate.Length);
  276. // WriteCatchLog("strData " + strData);
  277. string weight = strData.Substring(startPosition, dataLength);
  278. if ("T".Equals(strSeparate.ToUpper()))
  279. {
  280. //单位为T是做特殊处理,界面显示的是T
  281. weight = (Convert.ToDouble(weight) * 1000) + "";
  282. }
  283. //WriteCatchLog("weight " + weight);
  284. stringBuilder.Remove(0, stringBuilder.Length);
  285. ucStorageWeightT1.setWgt(Math.Round(Convert.ToDouble(weight), 3));
  286. DoShowWeightStatus(Convert.ToDouble(weight));
  287. }
  288. }
  289. }
  290. catch (Exception err)
  291. {
  292. MessageBox.Show("串口打开异常!异常原因" + err.ToString());
  293. WriteCatchLog("DataCollect1" + err.ToString());
  294. }
  295. }
  296. }
  297. /// <summary>
  298. /// 重量稳定判断
  299. /// </summary>
  300. /// <param name="_weight"></param>
  301. public void DoShowWeightStatus(double _weight)
  302. {
  303. if (Math.Abs(_weight) < 50)
  304. {
  305. //不稳定状态;红灯
  306. ucStorageWeightT1.setStable(false);
  307. return;
  308. }
  309. //波动在范围内时,稳定次数+1,临时重量不变;波动范围超出时,稳定次数清零,更新临时重量,
  310. int inDiff = Math.Abs(Convert.ToInt32(tmpWeight - _weight));
  311. if (inDiff < stableDiff)
  312. {
  313. weightStabCount += 1;
  314. }
  315. else
  316. {
  317. weightStabCount = 0;
  318. }
  319. //WriteCatchLog("_tmpWeight: " + _tmpWeight + " _weight: " + _weight + " inDiff: "+inDiff+ " StabCount : " + weightStabCount);
  320. tmpWeight = Convert.ToSingle(_weight);
  321. //重量30kg内波动超过10次认为重量稳定
  322. if (weightStabCount > stableCount)
  323. {
  324. //稳定状态;绿灯
  325. ucStorageWeightT1.setStable(true);
  326. weightStabCount = stableCount;
  327. }
  328. else
  329. {
  330. ucStorageWeightT1.setStable(false);
  331. }
  332. }
  333. #endregion 数据采集
  334. /// <summary>
  335. /// 重量保存:0=毛重;1=常规皮重
  336. /// </summary>
  337. /// <param name="strWeightType"></param>
  338. private void saveData(string strWeightType)
  339. {
  340. MeterWorkCarActualFirst actualFirst = new MeterWorkCarActualFirst(); //一次计量实体
  341. actualFirst.actualFirstNo = "CAR" + DateTime.Now.ToString("yyyyMMddHHmmssfff"); //主键
  342. actualFirst.carNo = tbCarNo.Text.Trim();//车号
  343. actualFirst.baseSpotName = cbBaseSpot.Text.Trim();//计量点
  344. actualFirst.meterTypeName = cbMeterTypeName.Text.Trim();//业务类型
  345. actualFirst.contractNo = cbContractNo.Text.Trim();//合同号
  346. actualFirst.batchNo = tbBatchNo.Text.Trim();//批次号
  347. actualFirst.heatNo = tbHeatNo.Text.Trim();//炉号
  348. actualFirst.shipmentNum = Convert.ToInt32(string.IsNullOrEmpty(tbShipmentNum.Text.Trim()) ? "0" : tbShipmentNum.Text.Trim());//包数/数量
  349. actualFirst.matterName = cbMatterName.Text.Trim();//物资名称
  350. actualFirst.receivingUintName = cbReceivingUintName.Text.Trim();//收货单位
  351. actualFirst.forwardingUnitName = cbForwardingUnitName.Text.Trim();//发货单位
  352. actualFirst.customerSupplierName = cbCustomerSupplierName.Text.Trim();//供应商
  353. actualFirst.loadPointName = cbLoadPointName.Text.Trim();//卸货地点
  354. actualFirst.memo = tbMemo.Text.Trim();//备注
  355. actualFirst.weightType = strWeightType;//重量类型
  356. actualFirst.meterWeight = ucStorageWeightT1.getWgt() * 1000;
  357. //DataTable csvDataTable = OpenCSV(AppDomain.CurrentDomain.BaseDirectory + "alonData\\" + DateTime.Now.ToString("yyyy-MM-dd") + "\\actualFirst.csv");
  358. if (System.IO.Directory.Exists(AppDomain.CurrentDomain.BaseDirectory + "alonData\\" + DateTime.Now.ToString("yyyy-MM-dd")) == false
  359. || System.IO.File.Exists(AppDomain.CurrentDomain.BaseDirectory + "alonData\\" + DateTime.Now.ToString("yyyy-MM-dd") + "\\actualFirst.csv") == false)
  360. {
  361. StringBuilder actualFirstLogHead = new StringBuilder(); //一次计量数据保存文件
  362. actualFirstLogHead.Append("createTime,");//若第一行默认为当前时间则第一行多个逗号
  363. actualFirstLogHead.Append("actualFirstNo,");
  364. actualFirstLogHead.Append("carNo,");
  365. actualFirstLogHead.Append("baseSpotName,");
  366. actualFirstLogHead.Append("meterTypeName,");
  367. actualFirstLogHead.Append("contractNo,");
  368. actualFirstLogHead.Append("batchNo,");
  369. actualFirstLogHead.Append("heatNo,");
  370. actualFirstLogHead.Append("shipmentNum,");
  371. actualFirstLogHead.Append("matterName,");
  372. actualFirstLogHead.Append("receivingUintName,");
  373. actualFirstLogHead.Append("forwardingUnitName,");
  374. actualFirstLogHead.Append("customerSupplierName,");
  375. actualFirstLogHead.Append("loadPointName,");
  376. actualFirstLogHead.Append("memo,");
  377. actualFirstLogHead.Append("weightType,"); //最后一行不要逗号
  378. actualFirstLogHead.Append("meterWeight");
  379. logCsv.WriteDataLog("actualFirst", actualFirstLogHead.ToString());
  380. }
  381. StringBuilder actualFirstLog = new StringBuilder(); //一次计量数据保存文件
  382. actualFirstLog.Append(DateTime.Now.ToString("yyyy-MM-dd HH:mm:ss") + ",");
  383. actualFirstLog.Append(actualFirst.actualFirstNo.ToString() + ",");
  384. actualFirstLog.Append(actualFirst.carNo.ToString() + ",");
  385. actualFirstLog.Append(actualFirst.baseSpotName.ToString() + ",");
  386. actualFirstLog.Append(actualFirst.meterTypeName.ToString() + ",");
  387. actualFirstLog.Append(actualFirst.contractNo.ToString() + ",");
  388. actualFirstLog.Append(actualFirst.batchNo.ToString() + ",");
  389. actualFirstLog.Append(actualFirst.heatNo.ToString() + ",");
  390. actualFirstLog.Append(actualFirst.shipmentNum.ToString() + ",");
  391. actualFirstLog.Append(actualFirst.matterName.ToString() + ",");
  392. actualFirstLog.Append(actualFirst.receivingUintName.ToString() + ",");
  393. actualFirstLog.Append(actualFirst.forwardingUnitName.ToString() + ",");
  394. actualFirstLog.Append(actualFirst.customerSupplierName.ToString() + ",");
  395. actualFirstLog.Append(actualFirst.loadPointName.ToString() + ",");
  396. actualFirstLog.Append(actualFirst.memo.ToString() + ",");
  397. actualFirstLog.Append(actualFirst.weightType.ToString() + ",");
  398. actualFirstLog.Append(actualFirst.meterWeight.ToString());
  399. logCsv.WriteDataLog("actualFirst", actualFirstLog.ToString());
  400. }
  401. /// <summary>
  402. /// 上传数据
  403. /// </summary>
  404. private void uploadData()
  405. {
  406. openFileDialogUpData.Filter = "csv文件|*.csv";//只允许csv文件;*.csv;*.xlsx
  407. openFileDialogUpData.Title = "选择要导入的计量数据文件"; //弹出框头部显示
  408. openFileDialogUpData.AddExtension = true; //自动增加后缀
  409. openFileDialogUpData.AutoUpgradeEnabled = true; //是否随系统自动升级弹出窗口样式
  410. openFileDialogUpData.InitialDirectory = Application.StartupPath + "\\alonData\\" + DateTime.Now.ToString("yyyy-MM-dd");//默认打开当前目录
  411. //openFileDialog1.Multiselect = true;//该值确定是否可以选择多个文件
  412. if (openFileDialogUpData.ShowDialog() == DialogResult.OK)
  413. {
  414. DataTable csvDataTable = OpenCSV(openFileDialogUpData.FileName);
  415. int rowCount = csvDataTable.Rows.Count;
  416. if (csvDataTable != null && csvDataTable.Rows.Count > 0)
  417. {
  418. List<MeterWorkCarActFirAlon> lp = csvDataTable.TableToDataList<MeterWorkCarActFirAlon>();
  419. DialogResult dr = MessageBox.Show("共 " + lp.Count + " 条计量数据,是否继续上传?", "提示", MessageBoxButtons.OKCancel);
  420. if (dr != DialogResult.OK)
  421. {
  422. return;
  423. }
  424. RESTfulResult<string> rES = alonService.batchInsertAlonData(lp);
  425. if (rES.Succeed)
  426. {
  427. MessageBox.Show("数据上传成功!");
  428. }
  429. else
  430. {
  431. MessageBox.Show("数据上传失败:" + rES.Message);
  432. }
  433. }
  434. };
  435. }
  436. public static DataTable OpenCSV(string filePath)
  437. {
  438. Encoding encoding = Encoding.UTF8; //Common.GetType(filePath); //Encoding.ASCII;//
  439. DataTable dt = new DataTable();
  440. FileStream fs = new FileStream(filePath, System.IO.FileMode.Open, System.IO.FileAccess.Read);
  441. //StreamReader sr = new StreamReader(fs, Encoding.UTF8);
  442. StreamReader sr = new StreamReader(fs, encoding);
  443. //string fileContent = sr.ReadToEnd();
  444. //encoding = sr.CurrentEncoding;
  445. //记录每次读取的一行记录
  446. string strLine = "";
  447. //记录每行记录中的各字段内容
  448. string[] aryLine = null;
  449. string[] tableHead = null;
  450. //标示列数
  451. int columnCount = 0;
  452. //标示是否是读取的第一行
  453. bool IsFirst = true;
  454. //逐行读取CSV中的数据
  455. while ((strLine = sr.ReadLine()) != null)
  456. {
  457. //strLine = Common.ConvertStringUTF8(strLine, encoding);
  458. //strLine = Common.ConvertStringUTF8(strLine);
  459. if (IsFirst == true)
  460. {
  461. tableHead = strLine.Split(',');
  462. IsFirst = false;
  463. columnCount = tableHead.Length;
  464. //创建列
  465. for (int i = 0; i < columnCount; i++)
  466. {
  467. DataColumn dc = new DataColumn(tableHead[i]);
  468. //DataColumn dc = new DataColumn(i.ToString());
  469. dt.Columns.Add(dc);
  470. switch (i) { }
  471. }
  472. }
  473. else
  474. {
  475. aryLine = strLine.Split(',');
  476. DataRow dr = dt.NewRow();
  477. for (int j = 0; j < columnCount; j++)
  478. {
  479. dr[j] = aryLine[j];
  480. }
  481. dt.Rows.Add(dr);
  482. }
  483. }
  484. //if (aryLine != null && aryLine.Length > 0)
  485. //{
  486. // dt.DefaultView.Sort = tableHead[0] + " " + "asc";
  487. //}
  488. sr.Close();
  489. fs.Close();
  490. return dt;
  491. }
  492. /// <summary>
  493. /// 数据查询
  494. /// </summary>
  495. private void doQuery()
  496. {
  497. if (System.IO.Directory.Exists(AppDomain.CurrentDomain.BaseDirectory + "alonData\\" + DateTime.Now.ToString("yyyy-MM-dd")) == true
  498. && System.IO.File.Exists(AppDomain.CurrentDomain.BaseDirectory + "alonData\\" + DateTime.Now.ToString("yyyy-MM-dd") + "\\actualFirst.csv") == true)
  499. {
  500. DataTable csvDataTable = OpenCSV(AppDomain.CurrentDomain.BaseDirectory + "alonData\\" + DateTime.Now.ToString("yyyy-MM-dd") + "\\actualFirst.csv");
  501. int rowCount = csvDataTable.Rows.Count;
  502. if (csvDataTable != null && csvDataTable.Rows.Count > 0)
  503. {
  504. List<MeterWorkCarActFirAlon> lp = csvDataTable.TableToDataList<MeterWorkCarActFirAlon>();
  505. rowCount = lp.Count;
  506. //毛重数据查询
  507. DataTable dtGross = csvDataTable.Clone();
  508. List<MeterWorkCarActFirAlon> lpGross = lp.Where(s => s.weightType == "0").ToList();
  509. if (lpGross != null && lpGross.Count > 0)
  510. {
  511. dtGross = lpGross.ListToDataTable<MeterWorkCarActFirAlon>();
  512. }
  513. ClsControlPack.CopyDataToDatatable(ref dtGross, ref this.dtCarActFirstGross, true);
  514. foreach (var item in this.ultraGridGross.Rows)
  515. {
  516. item.Cells["weightType"].Value = GetWeightType(item.Cells["weightType"].Value.ToString());
  517. }
  518. ClsControlPack.RefreshAndAutoSize(ultraGridGross);
  519. //皮重数据查询
  520. DataTable dtTare = csvDataTable.Clone();
  521. List<MeterWorkCarActFirAlon> lpTare = lp.Where(s => s.weightType == "1").ToList();
  522. if (lpTare != null && lpTare.Count > 0)
  523. {
  524. dtTare = lpTare.ListToDataTable<MeterWorkCarActFirAlon>();
  525. }
  526. ClsControlPack.CopyDataToDatatable(ref dtTare, ref this.dtCarActFirstTare, true);
  527. foreach (var item in this.ultraGridTare.Rows)
  528. {
  529. item.Cells["weightType"].Value = GetWeightType(item.Cells["weightType"].Value.ToString());
  530. }
  531. ClsControlPack.RefreshAndAutoSize(ultraGridTare);
  532. }
  533. }
  534. else
  535. {
  536. MessageBox.Show("未查询到当天的数据文件!");
  537. }
  538. }
  539. /// <summary>
  540. /// 重量类型
  541. /// </summary>
  542. /// <param name="strCode"></param>
  543. /// <returns></returns>
  544. public static string GetWeightType(string strCode)
  545. {
  546. string weightType = ""; //重量类型(0:毛重;1:皮重)
  547. switch (strCode)
  548. {
  549. case "0": weightType = "毛重"; break;
  550. case "1": weightType = "皮重"; break;
  551. default:
  552. break;
  553. }
  554. return weightType;
  555. }
  556. /// <summary>
  557. /// 记录错误日志
  558. /// </summary>
  559. /// <param name="str"></param>
  560. private void WriteCatchLog(string str)
  561. {
  562. try
  563. {
  564. string m_szRunPath = System.Environment.CurrentDirectory.ToString();
  565. if (!(System.IO.Directory.Exists(m_szRunPath + "\\log")))
  566. {
  567. System.IO.Directory.CreateDirectory(m_szRunPath + "\\log");
  568. }
  569. string strDate = System.DateTime.Now.ToString("yyyyMMddhh");
  570. System.IO.TextWriter tw = new System.IO.StreamWriter(m_szRunPath + "\\log\\catch.log", true);
  571. tw.WriteLine(System.DateTime.Now.ToString("yyyy-MM-dd HH:mm:ss") + "\t" + str);
  572. tw.Close();
  573. }
  574. catch
  575. {
  576. }
  577. }
  578. private void saveMatterInfo(RESTfulResult<List<MeterBaseMatterInfo>> result)
  579. {
  580. StringBuilder materInfoHead = new StringBuilder(); // 物料头
  581. materInfoHead.Append("matterNo,");//若第一行默认为当前时间则第一行多个逗号
  582. materInfoHead.Append("matterName");
  583. logCsv.WriteBaseData("MeterBaseMatterInfo", materInfoHead.ToString());
  584. if (result.Succeed)
  585. {
  586. StringBuilder materInfo = new StringBuilder(); // 物料体
  587. List<MeterBaseMatterInfo> list = result.Data;
  588. foreach (MeterBaseMatterInfo info in list)
  589. {
  590. materInfo.Clear();
  591. materInfo.Append(info.matterNo + ",");
  592. materInfo.Append(info.matterName + ",");
  593. logCsv.WriteBaseData("MeterBaseMatterInfo", materInfo.ToString());
  594. }
  595. }
  596. }
  597. private void saveCustomerInfo(RESTfulResult<List<MeterBaseCustomerSupplier>> result)
  598. {
  599. StringBuilder customerInfoHead = new StringBuilder(); // 客商头
  600. customerInfoHead.Append("customerSupplierNo,");//若第一行默认为当前时间则第一行多个逗号
  601. customerInfoHead.Append("customerSupplierName");
  602. logCsv.WriteBaseData("MeterBaseCustomerSupplier", customerInfoHead.ToString());
  603. if (result.Succeed)
  604. {
  605. StringBuilder customerInfo = new StringBuilder(); // 物料体
  606. List<MeterBaseCustomerSupplier> list = result.Data;
  607. if(result.Data != null)
  608. {
  609. foreach (MeterBaseCustomerSupplier info in list)
  610. {
  611. customerInfo.Clear();
  612. customerInfo.Append(info.customerSupplierNo + ",");
  613. customerInfo.Append(info.customerSupplierName + ",");
  614. logCsv.WriteBaseData("MeterBaseCustomerSupplier", customerInfo.ToString());
  615. }
  616. }
  617. }
  618. }
  619. }
  620. }