using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Text;
using System.Linq;
using System.Threading.Tasks;
using System.Windows.Forms;
using DevExpress.XtraEditors;
using Uninpho.DBOperation.Model;
using System.IO;
using Uninpho.DBOperation.Operation;
using System.Text.RegularExpressions;
using System.Reflection;
namespace Uninpho.Tools.components.DWForm
{
public partial class ImportGLdataForm : DevExpress.XtraEditors.XtraForm
{
public ImportGLdataForm()
{
InitializeComponent();
}
private void labelControl7_Click(object sender, EventArgs e)
{
}
private void labelControl1_Click(object sender, EventArgs e)
{
}
private void xianlumingText_SelectedIndexChanged(object sender, EventArgs e)
{
}
private void zhanmingText_SelectedIndexChanged(object sender, EventArgs e)
{
}
private void groupBox1_Enter(object sender, EventArgs e)
{
}
private void lblShow_Click(object sender, EventArgs e)
{
}
private void txtindir_TextChanged(object sender, EventArgs e)
{
}
private void simpleButton2_Click(object sender, EventArgs e)
{
// 创建一个OpenFileDialog实例
OpenFileDialog openFileDialog = new OpenFileDialog
{
// 设置文件对话框的标题
Title = "选择文件",
// 初始目录,可选
InitialDirectory = Environment.GetFolderPath(Environment.SpecialFolder.Desktop),
// 设置过滤条件,只显示文本文件,可以根据需要修改
Filter = "所有文件 (*.*)|*.*"
};
// 显示文件对话框并检查用户是否选择了文件
if (openFileDialog.ShowDialog() == DialogResult.OK)
{
// 将选择的文件路径写入文本框
txtindir.Text = openFileDialog.FileName;
}
}
private void newCreateBtnSon_Click(object sender, EventArgs e)
{
if (this.Text == "导入缺口配置文件")
{
saveqkData2PG(txtindir.Text, xianlumingText.Text, zhanmingText.Text);
}else {
saveData2PG(txtindir.Text, xianlumingText.Text, zhanmingText.Text);
}
}
///
/// メソッド概要 取文件的编码格式
///
///
/// 機能仕様 取文件的编码格式
///
public static Encoding GetEncoding(string filePath)
{
using (var reader = new StreamReader(filePath, Encoding.Default, true))
{
if (reader.Peek() >= 0)
{
reader.Read();
}
Encoding encoding = reader.CurrentEncoding;
reader.Close();
return encoding;
}
}
///
/// 读scv
///
///
private static void readqkSCV(string url, string xianlu, string chezhan, ref List gonglvCsvdata)
{
StreamReader sr = new StreamReader(url, GetEncoding(url));
sr.ReadLine();
sr.ReadLine();
while (!sr.EndOfStream)
{
string origonss = sr.ReadLine();
origonss = origonss.Replace(" ", " ");
origonss = origonss.Replace("/t", ",");
origonss = origonss.Replace(" ", ",");
var arrayarr = origonss.ToCharArray();
if (arrayarr[0] == ',' && arrayarr[1] == ',')
{
//简单判断这行是否是多余,例如“,,,,,,,,,,,,,,,,”这种行的录入
continue;
}
T_quekou_account onescvdata = new T_quekou_account();
onescvdata.Chezhan = chezhan;
onescvdata.Xianlu = xianlu;
onescvdata.Qxxh = arrayarr[0].ToString();
// onescvdata.Id = Guid.NewGuid().ToString();
List loopstring = new List();
//使用正则去除属性字段中的逗号的影响
Regex re = new Regex("(?<=,\").*?(?=\",)", RegexOptions.None);
MatchCollection mc = re.Matches(origonss);
//保存原始
foreach (var item in mc)
{
//string ddd = item.ToString().Replace("\"\"", "\"");
loopstring.Add(item.ToString());
}
//替换属性字段中的逗号为"$$$$$$$$$$$$$yxc$$$$$$$$$$$$$$$"
origonss = Regex.Replace(origonss, "(?<=,\").*?(?=\",)", "$$$$$$$$$$$$$yxc$$$$$$$$$$$$$$$");
string[] looparr = origonss.Split(',');
//还原之前被替换的字段属性值
int ii = 0;
for (int i = 0; i < looparr.Length; i++)
{
if (looparr[i] == "\"$$$$$$$yxc$$$$$$$$\"")
{
looparr[i] = loopstring[ii++];
}
}
ii = 0;
//存入T_xxxw_csvdata
//string[] propnames = new string[] { "Id", "Listid", "Cld", "Gzidh", "Nc", "Lc", "Gc", "Pmwz", "Spcg", "Gj", "Gddhdb", "Gddb", "Gddhcb", "Gdcb", "Gxdhdb", "Gxdb", "Gxdbcb", "Gxcb", "Dxg", "Zggc", "Yggc", "Zgpm", "Ygpm", "Sygc", "Sygddb", "Sygdcb", "Sypmwz", "Sygxdb", "Sygxcb", "Syspcg", "Sygj", "Fjzj", "Fjidh", "Fjsjcg", "Fjsccg", "Fjsjgj", "Fjscgj", "Fjsj5mgd", "Fjsc5mgd", "Fjsj150mgd", "Fjsc150mgd", "Fjsj5mgx", "Fjsc5mgx", "Fjsj150mgx", "Fjsc150mgx" };
Type t = onescvdata.GetType();//获得该类的Type
foreach (PropertyInfo pi in t.GetProperties())
{
if (pi.Name != "Id" && pi.Name != "Qxxh" && pi.Name != "Xianlu" && pi.Name != "Chezhan")
{
pi.SetValue(onescvdata, looparr[ii++]);
}
}
gonglvCsvdata.Add(onescvdata);
}
sr.Close();
}
///
/// 读scv
///
///
private static void readSCV(string url, string xianlu, string chezhan, ref List gonglvCsvdata)
{
StreamReader sr = new StreamReader(url, GetEncoding(url));
sr.ReadLine();
sr.ReadLine();
while (!sr.EndOfStream)
{
string origonss = sr.ReadLine();
origonss = origonss.Replace(" ", " ");
origonss = origonss.Replace("/t", ",");
origonss = origonss.Replace(" ", ",");
var arrayarr = origonss.ToCharArray();
if (arrayarr[0] == ',' && arrayarr[1] == ',')
{
//简单判断这行是否是多余,例如“,,,,,,,,,,,,,,,,”这种行的录入
continue;
}
T_gonglv_account onescvdata = new T_gonglv_account();
onescvdata.Chezhan = chezhan;
onescvdata.Xianlu = xianlu;
onescvdata.Qxxh = arrayarr[0].ToString();
// onescvdata.Id = Guid.NewGuid().ToString();
List loopstring = new List();
//使用正则去除属性字段中的逗号的影响
Regex re = new Regex("(?<=,\").*?(?=\",)", RegexOptions.None);
MatchCollection mc = re.Matches(origonss);
//保存原始
foreach (var item in mc)
{
//string ddd = item.ToString().Replace("\"\"", "\"");
loopstring.Add(item.ToString());
}
//替换属性字段中的逗号为"$$$$$$$$$$$$$yxc$$$$$$$$$$$$$$$"
origonss = Regex.Replace(origonss, "(?<=,\").*?(?=\",)", "$$$$$$$$$$$$$yxc$$$$$$$$$$$$$$$");
string[] looparr = origonss.Split(',');
//还原之前被替换的字段属性值
int ii = 0;
for (int i = 0; i < looparr.Length; i++)
{
if (looparr[i] == "\"$$$$$$$yxc$$$$$$$$\"")
{
looparr[i] = loopstring[ii++];
}
}
ii = 0;
//存入T_xxxw_csvdata
//string[] propnames = new string[] { "Id", "Listid", "Cld", "Gzidh", "Nc", "Lc", "Gc", "Pmwz", "Spcg", "Gj", "Gddhdb", "Gddb", "Gddhcb", "Gdcb", "Gxdhdb", "Gxdb", "Gxdbcb", "Gxcb", "Dxg", "Zggc", "Yggc", "Zgpm", "Ygpm", "Sygc", "Sygddb", "Sygdcb", "Sypmwz", "Sygxdb", "Sygxcb", "Syspcg", "Sygj", "Fjzj", "Fjidh", "Fjsjcg", "Fjsccg", "Fjsjgj", "Fjscgj", "Fjsj5mgd", "Fjsc5mgd", "Fjsj150mgd", "Fjsc150mgd", "Fjsj5mgx", "Fjsc5mgx", "Fjsj150mgx", "Fjsc150mgx" };
Type t = onescvdata.GetType();//获得该类的Type
foreach (PropertyInfo pi in t.GetProperties())
{
if (pi.Name != "Id" && pi.Name != "Qxxh" && pi.Name != "Xianlu" && pi.Name != "Chezhan")
{
pi.SetValue(onescvdata, looparr[ii++]);
}
}
gonglvCsvdata.Add(onescvdata);
}
sr.Close();
}
static string uuid_ = string.Empty;//预防报错
public static string saveData2PG(string url, string xianlu, string chezhan)
{
string uuid = uuid_;
List gonglvCsvdata = new List();
try
{
readSCV(url, xianlu, chezhan, ref gonglvCsvdata);
DataAnalysisToPG.Insert_gl_data_CsvData(gonglvCsvdata);
XtraMessageBox.Show("导入成功!", "提示", MessageBoxButtons.OK, MessageBoxIcon.Information);
}
catch (Exception)
{
XtraMessageBox.Show("导入失败!", "提示", MessageBoxButtons.OK, MessageBoxIcon.Information);
}
return uuid;
}
public static string saveqkData2PG(string url, string xianlu, string chezhan)
{
string uuid = uuid_;
List quekouCsvdata = new List();
try
{
readqkSCV(url, xianlu, chezhan, ref quekouCsvdata);
DataAnalysisToPG.Insert_qk_data_CsvData(quekouCsvdata);
XtraMessageBox.Show("导入成功!", "提示", MessageBoxButtons.OK, MessageBoxIcon.Information);
}
catch (Exception)
{
XtraMessageBox.Show("导入失败!", "提示", MessageBoxButtons.OK, MessageBoxIcon.Information);
}
return uuid;
}
private void NewcancelBtn_Click(object sender, EventArgs e)
{
this.Close();
}
List daochaData = new List();
///
/// 加载模糊查询的数据
///
private void ImportGLdataForm_Load(object sender, EventArgs e)
{
daochaData = DBOperation.Operation.DataAnalysisToPG.QueryDCTZALL();
//整理线路名集合
var listtgXLMALL = daochaData.GroupBy(c => c.Xlm).Select(c => c.First()).ToList();
foreach (var item in listtgXLMALL)
{
xianlumingText.Properties.Items.Add(item.Xlm);
}
}
///
/// 线路名查询
///
private void xianlumingText_TextChanged(object sender, EventArgs e)
{
zhanmingText.Properties.Items.Clear();
var isInputTrueXLM = daochaData.FindAll(delegate (T_daocha_account dc)
{
return dc.Xlm == xianlumingText.Text;
});
//线路名
if (isInputTrueXLM.Count != 0)
{
var listtg = isInputTrueXLM.GroupBy(c => c.Czm).Select(c => c.First()).ToList();
foreach (var item in listtg)
{
zhanmingText.Properties.Items.Add(item.Czm);
}
zhanmingText.Enabled = true;
}
else
{
zhanmingText.Enabled = false;
}
zhanmingText.Text = "";
}
}
}