using log4net; using Newtonsoft.Json; using RDIFramework.Utilities; using System; using System.Collections.Generic; using System.ComponentModel; using System.Configuration; using System.Data; using System.Drawing; using System.IO; using System.Linq; using System.Net; using System.Net.Http; using System.Text; using System.Windows.Forms; using System.Xml; using TimedUpload; namespace DataUpload { public partial class FormMain : Form { private readonly ILog log = LogManager.GetLogger(typeof(Form)); public string dbMeter = ConfigurationManager.AppSettings["DBMeter"]; public string nbMeter = ConfigurationManager.AppSettings["NBMeter"]; public string loraMeter = ConfigurationManager.AppSettings["LoraMeter"]; public string User = ConfigurationManager.AppSettings["UserName"]; public string Pwd = ConfigurationManager.AppSettings["Pwd"]; static readonly HttpClient client = new HttpClient(); public string upDataUrl = ConfigurationManager.AppSettings["upDataUrl"]; IDbProvider dbHelper { get { var DbDefine = DbFactoryProvider.GetProvider(CurrentDbType.Access, dbMeter); return DbDefine; } } IDbProvider nbHelper { get { var DbDefine = DbFactoryProvider.GetProvider(CurrentDbType.SqlServer, nbMeter); return DbDefine; } } IDbProvider loraHelper { get { var DbDefine = DbFactoryProvider.GetProvider(CurrentDbType.SqlServer, loraMeter); return DbDefine; } } public FormMain() { InitializeComponent(); } private void btnSearch_Click(object sender, EventArgs e) { FormSearch form = new FormSearch(); form.FillListView += form_FillListView; form.ShowDialog(); } void form_FillListView(string sql, string type, string start, string end) { try { //this.loading.Visible = true; //this.loading.Start(); DataTable dt = dbHelper.Fill(sql); this.lstData.Items.Clear(); this.lstData.BeginUpdate(); for (int i = 0; i < dt.Rows.Count; i++) { ListViewItem item = new ListViewItem(); item.Text = (i + 1).ToString(); item.SubItems.Add(dt.Rows[i]["小区名称"].ToString()); item.SubItems.Add(dt.Rows[i]["楼宇编号"].ToString() + dt.Rows[i]["楼宇名称"].ToString()); item.SubItems.Add(dt.Rows[i]["单元编号"].ToString() + dt.Rows[i]["单元名称"].ToString()); item.SubItems.Add(dt.Rows[i]["门牌编号"].ToString() + dt.Rows[i]["门牌名称"].ToString()); item.SubItems.Add(dt.Rows[i]["UserNo"].ToString()); item.SubItems.Add(dt.Rows[i]["表地址"].ToString()); string reading = dt.Rows[i]["本次读数"].ToString(); item.SubItems.Add(Math.Round(decimal.Parse(reading), MidpointRounding.AwayFromZero).ToString()); item.SubItems.Add(dt.Rows[i]["本次抄表日期"].ToString()); item.SubItems.Add(""); this.lstData.Items.Add(item); } this.lstData.EndUpdate(); } catch (Exception ex) { MessageBox.Show(ex.Message); } finally { //this.loading.Visible = false; } } private void FormMain_Load(object sender, EventArgs e) { this.lstData.Columns[0].Width = 80; int width = this.Width - 10 - 80; int per = width / 9; for (int i = 1; i < this.lstData.Columns.Count; i++) { this.lstData.Columns[i].Width = per; } } //private void btnUpload_Click(object sender, EventArgs e) //{ // if (this.lstData.Items.Count <= 0) // { // MessageBox.Show("没有可上传的数据"); // } // else // { // try // { // this.btnSearch.Enabled = false; // this.btnUpload.Enabled = false; // this.pBar.Visible = true; // this.pBar.Maximum = this.lstData.Items.Count; // this.pBar.Value = 0; // string company = "2971"; // for (int i = 0; i < this.lstData.Items.Count; i++) // { // string userNo = this.lstData.Items[i].SubItems[5].Text; // string waterMeterNo = this.lstData.Items[i].SubItems[6].Text; // string reading = this.lstData.Items[i].SubItems[7].Text; // string date = this.lstData.Items[i].SubItems[8].Text; // // 解析日期字符串 // DateTime dateTime = DateTime.ParseExact(date, "yyyy/M/d H:mm:ss", null); // //// 如果日期在每月的24号之后,则设置为下个月的第一天 // //if (dateTime.Day > 24) // //{ // // dateTime = dateTime.AddMonths(1); // // dateTime = new DateTime(dateTime.Year, dateTime.Month, 1); // //} // // 格式化日期为 "yyyyMM" 格式 // string formattedDate = dateTime.ToString("yyyy-MM-dd HH:mm:ss"); // string result = ""; // try // { // string param = "{\"userId\":\"" + userNo + "\",\"waterNum\":" + reading + ",\"quantity\":0,\"waterMonth\":\"" + formattedDate + "\"}"; // param = "data=[{\"company\":" + company + ",\"meterdata\":[{\"code\":\"" + userNo + "\",\"watermeter\":\"" + waterMeterNo + "\",\"currentindex\":\"" + reading + "\",\"meterdate\":\"" + formattedDate + "\"}]}]"; // string resultData = PostWebRequest(upDataUrl, param); // // 找到最后一个"}"的位置 // int lastBracePosition = resultData.LastIndexOf("}"); // // 如果找到了"}",那么删除后面的所有字符 // if (lastBracePosition != -1) // { // resultData = resultData.Remove(lastBracePosition + 1); // } // var jsonObject = JsonConvert.DeserializeObject(resultData); // int code = jsonObject.errcode; // // 成功 // if (jsonObject.message.Contains("成功")) // { // log.Info("数据:" + param + "\n上报成功!"); // result = "成功!" + jsonObject.message; // } // // 失败 // else // { // log.Info("数据:" + param + "\n上报失败!返回编码:" + code); // // result = "失败!数据:" + param + "code =" + code +";message="+ jsonObject.message; // result = "失败!" + jsonObject.message; // } // } // catch (Exception ex) // { // result = ex.Message; // log.Info("失败!出现异常:" + result); // //异常保存数据 // //sql = " insert into Data_temp(userNo,reading,[date]) values('" + userNo + "'," + reading + ",'" + date + "')"; // //int num = dbHelper.ExecuteNonQuery(sql); // } // this.lstData.Items[i].SubItems[9].Text = result; // this.pBar.Value = i; // } // this.pBar.Visible = false; // } // catch (Exception ex) // { // MessageBox.Show(ex.Message); // this.btnSearch.Enabled = true; // this.btnUpload.Enabled = true; // } // this.btnSearch.Enabled = true; // this.btnUpload.Enabled = true; // } //} private void button1_Click(object sender, EventArgs e) { DataTable dt; try { String sql = " select * from Bsc_AreaInfo "; dt = dbHelper.Fill(sql); if (dt != null && dt.Rows.Count > 0) { SaveFileDialog savefile = new SaveFileDialog(); savefile.Filter = "Excel 99-03文件|*.xls"; DialogResult re = savefile.ShowDialog(); if (re == DialogResult.OK) { ExcelUtil.Export(dt, null, savefile.FileName); MessageBox.Show("导出完成!"); } } } catch (Exception ex) { MessageBox.Show("导出数据失败"); } } /// /// Post提交数据 /// /// URL /// 参数 /// private string PostWebRequest(string postUrl, string paramData) { string ret = string.Empty; try { if (!postUrl.StartsWith("http://")) return ""; byte[] byteArray = Encoding.Default.GetBytes(paramData); //转化 / HttpWebRequest webReq = (HttpWebRequest)WebRequest.Create(new Uri(postUrl)); webReq.Method = "POST"; webReq.ContentType = "application/x-www-form-urlencoded"; webReq.ContentLength = byteArray.Length; Stream newStream = webReq.GetRequestStream(); newStream.Write(byteArray, 0, byteArray.Length);//写入参数 newStream.Close(); HttpWebResponse response = (HttpWebResponse)webReq.GetResponse(); StreamReader sr = new StreamReader(response.GetResponseStream(), Encoding.UTF8); ret = sr.ReadToEnd(); sr.Close(); response.Close(); newStream.Close(); } catch (Exception ex) { return ex.Message; } return ret; } public class Result { public int errcode { get; set; } public string message { get; set; } } private void lstData_SelectedIndexChanged(object sender, EventArgs e) { } private void loading_Load(object sender, EventArgs e) { } private void pBar_Click(object sender, EventArgs e) { } private void button2_Click(object sender, EventArgs e) { NBFormSearch form = new NBFormSearch(); form.FillListView += form_NBFillListView; form.ShowDialog(); } void form_NBFillListView(string sql, string type, string start, string end) { try { //this.loading.Visible = true; //this.loading.Start(); DataTable dt = nbHelper.Fill(sql); this.lstData.Items.Clear(); this.lstData.BeginUpdate(); for (int i = 0; i < dt.Rows.Count; i++) { ListViewItem item = new ListViewItem(); item.Text = (i + 1).ToString(); item.SubItems.Add(dt.Rows[i]["小区名称"] + ""); item.SubItems.Add(dt.Rows[i]["楼宇名称"] + ""); item.SubItems.Add(dt.Rows[i]["单元名称"] + ""); item.SubItems.Add(dt.Rows[i]["门牌名称"] + ""); item.SubItems.Add(dt.Rows[i]["UserNo"] + ""); item.SubItems.Add(dt.Rows[i]["表编号"] + ""); string reading = dt.Rows[i]["本次读数"] + ""; item.SubItems.Add(Math.Round(decimal.Parse(reading), MidpointRounding.AwayFromZero).ToString()); item.SubItems.Add(dt.Rows[i]["本次抄表日期"] + ""); item.SubItems.Add(""); this.lstData.Items.Add(item); } this.lstData.EndUpdate(); } catch (Exception ex) { MessageBox.Show(ex.Message); } finally { //this.loading.Visible = false; } } void form_ManyFillListView(string sql, string type, string start, string end) { try { //this.loading.Visible = true; //this.loading.Start(); DataTable dt = dbHelper.Fill(sql); this.lstData.Items.Clear(); this.lstData.BeginUpdate(); for (int i = 0; i < dt.Rows.Count; i++) { ListViewItem item = new ListViewItem(); item.Text = (i + 1).ToString(); item.SubItems.Add(dt.Rows[i]["小区名称"].ToString()); item.SubItems.Add(dt.Rows[i]["楼宇编号"].ToString() + dt.Rows[i]["楼宇名称"].ToString()); item.SubItems.Add(dt.Rows[i]["单元编号"].ToString() + dt.Rows[i]["单元名称"].ToString()); item.SubItems.Add(dt.Rows[i]["门牌编号"].ToString() + dt.Rows[i]["门牌名称"].ToString()); item.SubItems.Add(dt.Rows[i]["UserNo"].ToString()); item.SubItems.Add(dt.Rows[i]["表地址"].ToString()); string reading = dt.Rows[i]["本次读数"].ToString(); item.SubItems.Add(Math.Round(decimal.Parse(reading), MidpointRounding.AwayFromZero).ToString()); item.SubItems.Add(dt.Rows[i]["本次抄表日期"].ToString()); item.SubItems.Add(""); this.lstData.Items.Add(item); } this.lstData.EndUpdate(); } catch (Exception ex) { MessageBox.Show(ex.Message); } finally { //this.loading.Visible = false; } } void form_LoraFillListView(string sql, string type, string start, string end) { try { //this.loading.Visible = true; //this.loading.Start(); DataTable dt = loraHelper.Fill(sql); this.lstData.Items.Clear(); this.lstData.BeginUpdate(); for (int i = 0; i < dt.Rows.Count; i++) { ListViewItem item = new ListViewItem(); item.Text = (i + 1).ToString(); item.SubItems.Add(dt.Rows[i]["小区名称"] + ""); item.SubItems.Add(dt.Rows[i]["楼宇编号"].ToString() + dt.Rows[i]["楼宇名称"].ToString()); item.SubItems.Add(dt.Rows[i]["单元编号"].ToString() + dt.Rows[i]["单元名称"].ToString()); item.SubItems.Add(dt.Rows[i]["门牌编号"].ToString() + dt.Rows[i]["门牌名称"].ToString()); item.SubItems.Add(dt.Rows[i]["UserNo"] + ""); item.SubItems.Add(dt.Rows[i]["表地址"] + ""); string reading = dt.Rows[i]["表读数"] + ""; item.SubItems.Add(Math.Round(decimal.Parse(reading), MidpointRounding.AwayFromZero).ToString()); item.SubItems.Add(dt.Rows[i]["抄表日期"] + ""); item.SubItems.Add(""); this.lstData.Items.Add(item); } this.lstData.EndUpdate(); } catch (Exception ex) { MessageBox.Show(ex.Message); } finally { //this.loading.Visible = false; } } private void button1_Click_2(object sender, EventArgs e) { ManyFormSearch form = new ManyFormSearch(); form.FillListView += form_ManyFillListView; form.ShowDialog(); } private void button3_Click(object sender, EventArgs e) { LoraFormSearch form = new LoraFormSearch(); form.FillListView += form_LoraFillListView; form.ShowDialog(); } private void button4_Click(object sender, EventArgs e) { try { // 创建一个新的DataTable DataTable dt = new DataTable(); dt.Columns.Add("*用户编号", typeof(string)); dt.Columns.Add("*远传表厂商", typeof(string)); dt.Columns.Add("*表身码", typeof(string)); dt.Columns.Add("*抄表止码", typeof(string)); dt.Columns.Add("采集时间", typeof(string)); dt.Columns.Add("*抄表表况", typeof(string)); DataRow row = dt.NewRow(); for (int i = 0; i < this.lstData.Items.Count; i++) { string userNo = this.lstData.Items[i].SubItems[5].Text; string waterMeterNo = this.lstData.Items[i].SubItems[6].Text; string reading = this.lstData.Items[i].SubItems[7].Text; string date = this.lstData.Items[i].SubItems[8].Text; // 解析日期字符串 DateTime dateTime = DateTime.ParseExact(date, "yyyy/M/d H:mm:ss", null); string formattedDate = dateTime.ToString("yyyy-MM-dd HH:mm:ss"); DataRow dr = dt.NewRow(); dr["*用户编号"] = userNo; dr["*远传表厂商"] = "山东维微!^004"; dr["*表身码"] = waterMeterNo; dr["*抄表止码"] = reading; dr["采集时间"] = formattedDate; dr["*抄表表况"] = "正常!^0"; dt.Rows.Add(dr); } if (dt != null && dt.Rows.Count > 0) { SaveFileDialog savefile = new SaveFileDialog(); savefile.Filter = "Excel 99-03文件|*.xls"; DialogResult re = savefile.ShowDialog(); if (re == DialogResult.OK) { ExcelUtil.Export(dt, null, savefile.FileName); MessageBox.Show("导出完成!"); } } } catch (Exception ex) { MessageBox.Show("导出数据失败"); } } public static DataTable ConvertEntityListToDataTable(IEnumerable entityList) where T : class { Type type = typeof(T); var properties = type.GetProperties(); DataTable table = new DataTable(); foreach (var propertyInfo in properties) { if (!table.Columns.Contains(propertyInfo.Name)) table.Columns.Add(new DataColumn(propertyInfo.Name)); } foreach (var item in entityList) { object[] values = new object[properties.Length]; for (int i = 0; i < properties.Length; i++) { values[i] = properties[i].GetValue(item,null); } table.Rows.Add(values); } return table; } public class DATA { //上月表数 public String SYBS { get; set; } //抄表日期 public String CBRQ { get; set; } //水表编号 public String SBBH { get; set; } //用户编号 public String CUSERID { get; set; } //抄表状态 public String CBZT { get; set; } //本月表数 public double BYBS { get; set; } //抄表标记 public Int32 CCOPY { get; set; } //加水量 public double JJSL { get; set; } //抄表备注 public String REMARK { get; set; } } } }