FormMain.cs 17 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442
  1. using log4net;
  2. using Newtonsoft.Json;
  3. using RDIFramework.Utilities;
  4. using System;
  5. using System.Collections.Generic;
  6. using System.ComponentModel;
  7. using System.Configuration;
  8. using System.Data;
  9. using System.Drawing;
  10. using System.IO;
  11. using System.Linq;
  12. using System.Net;
  13. using System.Net.Http;
  14. using System.Text;
  15. using System.Windows.Forms;
  16. using System.Xml;
  17. using TimedUpload;
  18. namespace DataUpload
  19. {
  20. public partial class FormMain : Form
  21. {
  22. private readonly ILog log = LogManager.GetLogger(typeof(Form));
  23. public string dbMeter = ConfigurationManager.AppSettings["DBMeter"];
  24. public string nbMeter = ConfigurationManager.AppSettings["NBMeter"];
  25. public string loraMeter = ConfigurationManager.AppSettings["LoraMeter"];
  26. public string User = ConfigurationManager.AppSettings["UserName"];
  27. public string Pwd = ConfigurationManager.AppSettings["Pwd"];
  28. static readonly HttpClient client = new HttpClient();
  29. public string upDataUrl = ConfigurationManager.AppSettings["upDataUrl"];
  30. IDbProvider dbHelper
  31. {
  32. get
  33. {
  34. var DbDefine = DbFactoryProvider.GetProvider(CurrentDbType.Access, dbMeter);
  35. return DbDefine;
  36. }
  37. }
  38. IDbProvider nbHelper
  39. {
  40. get
  41. {
  42. var DbDefine = DbFactoryProvider.GetProvider(CurrentDbType.SqlServer, nbMeter);
  43. return DbDefine;
  44. }
  45. }
  46. IDbProvider loraHelper
  47. {
  48. get
  49. {
  50. var DbDefine = DbFactoryProvider.GetProvider(CurrentDbType.SqlServer, loraMeter);
  51. return DbDefine;
  52. }
  53. }
  54. public FormMain()
  55. {
  56. InitializeComponent();
  57. }
  58. private void btnSearch_Click(object sender, EventArgs e)
  59. {
  60. FormSearch form = new FormSearch();
  61. form.FillListView += form_FillListView;
  62. form.ShowDialog();
  63. }
  64. void form_FillListView(string sql,string type,string start,string end)
  65. {
  66. try
  67. {
  68. //this.loading.Visible = true;
  69. //this.loading.Start();
  70. DataTable dt = dbHelper.Fill(sql);
  71. this.lstData.Items.Clear();
  72. this.lstData.BeginUpdate();
  73. for (int i = 0; i < dt.Rows.Count; i++)
  74. {
  75. ListViewItem item = new ListViewItem();
  76. item.Text = (i + 1).ToString();
  77. item.SubItems.Add(dt.Rows[i]["小区名称"].ToString());
  78. item.SubItems.Add(dt.Rows[i]["楼宇编号"].ToString() + dt.Rows[i]["楼宇名称"].ToString());
  79. item.SubItems.Add(dt.Rows[i]["单元编号"].ToString() + dt.Rows[i]["单元名称"].ToString());
  80. item.SubItems.Add(dt.Rows[i]["门牌编号"].ToString() + dt.Rows[i]["门牌名称"].ToString());
  81. item.SubItems.Add(dt.Rows[i]["UserNo"].ToString());
  82. item.SubItems.Add(dt.Rows[i]["表编号"].ToString());
  83. string reading = dt.Rows[i]["本次读数"].ToString();
  84. item.SubItems.Add(Math.Round(decimal.Parse(reading), MidpointRounding.AwayFromZero).ToString());
  85. item.SubItems.Add(dt.Rows[i]["本次抄表日期"].ToString());
  86. item.SubItems.Add("");
  87. this.lstData.Items.Add(item);
  88. }
  89. this.lstData.EndUpdate();
  90. }
  91. catch(Exception ex)
  92. {
  93. MessageBox.Show(ex.Message);
  94. }
  95. finally
  96. {
  97. //this.loading.Visible = false;
  98. }
  99. }
  100. private void FormMain_Load(object sender, EventArgs e)
  101. {
  102. this.lstData.Columns[0].Width = 80;
  103. int width = this.Width - 10-80;
  104. int per = width / 9;
  105. for (int i = 1; i < this.lstData.Columns.Count; i++)
  106. {
  107. this.lstData.Columns[i].Width = per;
  108. }
  109. }
  110. private void btnUpload_Click(object sender, EventArgs e)
  111. {
  112. if (this.lstData.Items.Count <= 0)
  113. {
  114. MessageBox.Show("没有可上传的数据");
  115. }
  116. else
  117. {
  118. try
  119. {
  120. this.btnSearch.Enabled = false;
  121. this.btnUpload.Enabled = false;
  122. this.pBar.Visible = true;
  123. this.pBar.Maximum = this.lstData.Items.Count;
  124. this.pBar.Value = 0;
  125. string company = "2971";
  126. for (int i = 0; i < this.lstData.Items.Count; i++)
  127. {
  128. string userNo = this.lstData.Items[i].SubItems[5].Text;
  129. string waterMeterNo = this.lstData.Items[i].SubItems[6].Text;
  130. string reading = this.lstData.Items[i].SubItems[7].Text;
  131. string date = this.lstData.Items[i].SubItems[8].Text;
  132. // 解析日期字符串
  133. DateTime dateTime = DateTime.ParseExact(date, "yyyy/M/d H:mm:ss", null);
  134. //// 如果日期在每月的24号之后,则设置为下个月的第一天
  135. //if (dateTime.Day > 24)
  136. //{
  137. // dateTime = dateTime.AddMonths(1);
  138. // dateTime = new DateTime(dateTime.Year, dateTime.Month, 1);
  139. //}
  140. // 格式化日期为 "yyyyMM" 格式
  141. string formattedDate = dateTime.ToString("yyyy-MM-dd HH:mm:ss");
  142. string result = "";
  143. try
  144. {
  145. string param = "{\"userId\":\""+ userNo + "\",\"waterNum\":"+ reading + ",\"quantity\":0,\"waterMonth\":\""+ formattedDate + "\"}";
  146. param = "data=[{\"company\":" + company + ",\"meterdata\":[{\"code\":\"" + userNo + "\",\"watermeter\":\""+ waterMeterNo + "\",\"currentindex\":\"" + reading + "\",\"meterdate\":\"" + formattedDate + "\"}]}]";
  147. string resultData = PostWebRequest(upDataUrl, param);
  148. // 找到最后一个"}"的位置
  149. int lastBracePosition = resultData.LastIndexOf("}");
  150. // 如果找到了"}",那么删除后面的所有字符
  151. if (lastBracePosition != -1)
  152. {
  153. resultData = resultData.Remove(lastBracePosition + 1);
  154. }
  155. var jsonObject = JsonConvert.DeserializeObject<Result>(resultData);
  156. int code = jsonObject.errcode;
  157. // 成功
  158. if (jsonObject.message.Contains("成功"))
  159. {
  160. log.Info("数据:"+param+ "\n上报成功!");
  161. result = "成功!"+jsonObject.message;
  162. }
  163. // 失败
  164. else
  165. {
  166. log.Info("数据:" + param + "\n上报失败!返回编码:"+ code);
  167. // result = "失败!数据:" + param + "code =" + code +";message="+ jsonObject.message;
  168. result = "失败!"+jsonObject.message;
  169. }
  170. }
  171. catch (Exception ex)
  172. {
  173. result = ex.Message;
  174. log.Info("失败!出现异常:" + result);
  175. //异常保存数据
  176. //sql = " insert into Data_temp(userNo,reading,[date]) values('" + userNo + "'," + reading + ",'" + date + "')";
  177. //int num = dbHelper.ExecuteNonQuery(sql);
  178. }
  179. this.lstData.Items[i].SubItems[9].Text = result;
  180. this.pBar.Value = i;
  181. }
  182. this.pBar.Visible = false;
  183. }
  184. catch(Exception ex)
  185. {
  186. MessageBox.Show(ex.Message);
  187. this.btnSearch.Enabled = true;
  188. this.btnUpload.Enabled = true;
  189. }
  190. this.btnSearch.Enabled = true;
  191. this.btnUpload.Enabled = true;
  192. }
  193. }
  194. private void button1_Click(object sender, EventArgs e)
  195. {
  196. DataTable dt;
  197. try
  198. {
  199. String sql = " select * from Bsc_AreaInfo ";
  200. dt = dbHelper.Fill(sql);
  201. if (dt != null && dt.Rows.Count > 0)
  202. {
  203. SaveFileDialog savefile = new SaveFileDialog();
  204. savefile.Filter = "Excel 99-03文件|*.xls";
  205. DialogResult re = savefile.ShowDialog();
  206. if (re == DialogResult.OK)
  207. {
  208. ExcelUtil.Export(dt, null, savefile.FileName);
  209. MessageBox.Show("导出完成!");
  210. }
  211. }
  212. }
  213. catch (Exception ex)
  214. {
  215. MessageBox.Show("导出数据失败");
  216. }
  217. }
  218. /// <summary>
  219. /// Post提交数据
  220. /// </summary>
  221. /// <param name="postUrl">URL</param>
  222. /// <param name="paramData">参数</param>
  223. /// <returns></returns>
  224. private string PostWebRequest(string postUrl, string paramData)
  225. {
  226. string ret = string.Empty;
  227. try
  228. {
  229. if (!postUrl.StartsWith("http://"))
  230. return "";
  231. byte[] byteArray = Encoding.Default.GetBytes(paramData); //转化 /
  232. HttpWebRequest webReq = (HttpWebRequest)WebRequest.Create(new Uri(postUrl));
  233. webReq.Method = "POST";
  234. webReq.ContentType = "application/x-www-form-urlencoded";
  235. webReq.ContentLength = byteArray.Length;
  236. Stream newStream = webReq.GetRequestStream();
  237. newStream.Write(byteArray, 0, byteArray.Length);//写入参数
  238. newStream.Close();
  239. HttpWebResponse response = (HttpWebResponse)webReq.GetResponse();
  240. StreamReader sr = new StreamReader(response.GetResponseStream(), Encoding.UTF8);
  241. ret = sr.ReadToEnd();
  242. sr.Close();
  243. response.Close();
  244. newStream.Close();
  245. }
  246. catch (Exception ex)
  247. {
  248. return ex.Message;
  249. }
  250. return ret;
  251. }
  252. public class Result
  253. {
  254. public int errcode { get; set; }
  255. public string message { get; set; }
  256. }
  257. private void lstData_SelectedIndexChanged(object sender, EventArgs e)
  258. {
  259. }
  260. private void loading_Load(object sender, EventArgs e)
  261. {
  262. }
  263. private void pBar_Click(object sender, EventArgs e)
  264. {
  265. }
  266. private void button2_Click(object sender, EventArgs e)
  267. {
  268. NBFormSearch form = new NBFormSearch();
  269. form.FillListView += form_NBFillListView;
  270. form.ShowDialog();
  271. }
  272. void form_NBFillListView(string sql, string type, string start, string end)
  273. {
  274. try
  275. {
  276. //this.loading.Visible = true;
  277. //this.loading.Start();
  278. DataTable dt = nbHelper.Fill(sql);
  279. this.lstData.Items.Clear();
  280. this.lstData.BeginUpdate();
  281. for (int i = 0; i < dt.Rows.Count; i++)
  282. {
  283. ListViewItem item = new ListViewItem();
  284. item.Text = (i + 1).ToString();
  285. item.SubItems.Add(dt.Rows[i]["小区名称"] + "");
  286. item.SubItems.Add(dt.Rows[i]["楼宇名称"] + "");
  287. item.SubItems.Add(dt.Rows[i]["单元名称"] + "");
  288. item.SubItems.Add(dt.Rows[i]["门牌名称"] + "");
  289. item.SubItems.Add(dt.Rows[i]["UserNo"] + "");
  290. item.SubItems.Add(dt.Rows[i]["表编号"] + "");
  291. string reading = dt.Rows[i]["本次读数"] + "";
  292. item.SubItems.Add(Math.Round(decimal.Parse(reading), MidpointRounding.AwayFromZero).ToString());
  293. item.SubItems.Add(dt.Rows[i]["本次抄表日期"] + "");
  294. item.SubItems.Add("");
  295. this.lstData.Items.Add(item);
  296. }
  297. this.lstData.EndUpdate();
  298. }
  299. catch (Exception ex)
  300. {
  301. MessageBox.Show(ex.Message);
  302. }
  303. finally
  304. {
  305. //this.loading.Visible = false;
  306. }
  307. }
  308. void form_ManyFillListView(string sql, string type, string start, string end)
  309. {
  310. try
  311. {
  312. //this.loading.Visible = true;
  313. //this.loading.Start();
  314. DataTable dt = dbHelper.Fill(sql);
  315. this.lstData.Items.Clear();
  316. this.lstData.BeginUpdate();
  317. for (int i = 0; i < dt.Rows.Count; i++)
  318. {
  319. ListViewItem item = new ListViewItem();
  320. item.Text = (i + 1).ToString();
  321. item.SubItems.Add(dt.Rows[i]["小区名称"].ToString());
  322. item.SubItems.Add(dt.Rows[i]["楼宇编号"].ToString() + dt.Rows[i]["楼宇名称"].ToString());
  323. item.SubItems.Add(dt.Rows[i]["单元编号"].ToString() + dt.Rows[i]["单元名称"].ToString());
  324. item.SubItems.Add(dt.Rows[i]["门牌编号"].ToString() + dt.Rows[i]["门牌名称"].ToString());
  325. item.SubItems.Add(dt.Rows[i]["UserNo"].ToString());
  326. item.SubItems.Add(dt.Rows[i]["表编号"].ToString());
  327. string reading = dt.Rows[i]["本次读数"].ToString();
  328. item.SubItems.Add(Math.Round(decimal.Parse(reading), MidpointRounding.AwayFromZero).ToString());
  329. item.SubItems.Add(dt.Rows[i]["本次抄表日期"].ToString());
  330. item.SubItems.Add("");
  331. this.lstData.Items.Add(item);
  332. }
  333. this.lstData.EndUpdate();
  334. }
  335. catch (Exception ex)
  336. {
  337. MessageBox.Show(ex.Message);
  338. }
  339. finally
  340. {
  341. //this.loading.Visible = false;
  342. }
  343. }
  344. void form_LoraFillListView(string sql, string type, string start, string end)
  345. {
  346. try
  347. {
  348. //this.loading.Visible = true;
  349. //this.loading.Start();
  350. DataTable dt = loraHelper.Fill(sql);
  351. this.lstData.Items.Clear();
  352. this.lstData.BeginUpdate();
  353. for (int i = 0; i < dt.Rows.Count; i++)
  354. {
  355. ListViewItem item = new ListViewItem();
  356. item.Text = (i + 1).ToString();
  357. item.SubItems.Add(dt.Rows[i]["小区名称"] + "");
  358. item.SubItems.Add(dt.Rows[i]["楼宇编号"].ToString() + dt.Rows[i]["楼宇名称"].ToString());
  359. item.SubItems.Add(dt.Rows[i]["单元编号"].ToString() + dt.Rows[i]["单元名称"].ToString());
  360. item.SubItems.Add(dt.Rows[i]["门牌编号"].ToString() + dt.Rows[i]["门牌名称"].ToString());
  361. item.SubItems.Add(dt.Rows[i]["UserNo"] + "");
  362. item.SubItems.Add(dt.Rows[i]["表地址"] + "");
  363. string reading = dt.Rows[i]["表读数"] + "";
  364. item.SubItems.Add(Math.Round(decimal.Parse(reading), MidpointRounding.AwayFromZero).ToString());
  365. item.SubItems.Add(dt.Rows[i]["抄表日期"] + "");
  366. item.SubItems.Add("");
  367. this.lstData.Items.Add(item);
  368. }
  369. this.lstData.EndUpdate();
  370. }
  371. catch (Exception ex)
  372. {
  373. MessageBox.Show(ex.Message);
  374. }
  375. finally
  376. {
  377. //this.loading.Visible = false;
  378. }
  379. }
  380. private void button1_Click_2(object sender, EventArgs e)
  381. {
  382. ManyFormSearch form = new ManyFormSearch();
  383. form.FillListView += form_ManyFillListView;
  384. form.ShowDialog();
  385. }
  386. private void button3_Click(object sender, EventArgs e)
  387. {
  388. LoraFormSearch form = new LoraFormSearch();
  389. form.FillListView += form_LoraFillListView;
  390. form.ShowDialog();
  391. }
  392. }
  393. }