123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541 |
- 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<Result>(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("导出数据失败");
- }
- }
- /// <summary>
- /// Post提交数据
- /// </summary>
- /// <param name="postUrl">URL</param>
- /// <param name="paramData">参数</param>
- /// <returns></returns>
- 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<T>(IEnumerable<T> 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; }
- }
- }
- }
|