ImportGLdataForm.cs 13 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332
  1. using System;
  2. using System.Collections.Generic;
  3. using System.ComponentModel;
  4. using System.Data;
  5. using System.Drawing;
  6. using System.Text;
  7. using System.Linq;
  8. using System.Threading.Tasks;
  9. using System.Windows.Forms;
  10. using DevExpress.XtraEditors;
  11. using Uninpho.DBOperation.Model;
  12. using System.IO;
  13. using Uninpho.DBOperation.Operation;
  14. using System.Text.RegularExpressions;
  15. using System.Reflection;
  16. namespace Uninpho.Tools.components.DWForm
  17. {
  18. public partial class ImportGLdataForm : DevExpress.XtraEditors.XtraForm
  19. {
  20. public ImportGLdataForm()
  21. {
  22. InitializeComponent();
  23. }
  24. private void labelControl7_Click(object sender, EventArgs e)
  25. {
  26. }
  27. private void labelControl1_Click(object sender, EventArgs e)
  28. {
  29. }
  30. private void xianlumingText_SelectedIndexChanged(object sender, EventArgs e)
  31. {
  32. }
  33. private void zhanmingText_SelectedIndexChanged(object sender, EventArgs e)
  34. {
  35. }
  36. private void groupBox1_Enter(object sender, EventArgs e)
  37. {
  38. }
  39. private void lblShow_Click(object sender, EventArgs e)
  40. {
  41. }
  42. private void txtindir_TextChanged(object sender, EventArgs e)
  43. {
  44. }
  45. private void simpleButton2_Click(object sender, EventArgs e)
  46. {
  47. // 创建一个OpenFileDialog实例
  48. OpenFileDialog openFileDialog = new OpenFileDialog
  49. {
  50. // 设置文件对话框的标题
  51. Title = "选择文件",
  52. // 初始目录,可选
  53. InitialDirectory = Environment.GetFolderPath(Environment.SpecialFolder.Desktop),
  54. // 设置过滤条件,只显示文本文件,可以根据需要修改
  55. Filter = "所有文件 (*.*)|*.*"
  56. };
  57. // 显示文件对话框并检查用户是否选择了文件
  58. if (openFileDialog.ShowDialog() == DialogResult.OK)
  59. {
  60. // 将选择的文件路径写入文本框
  61. txtindir.Text = openFileDialog.FileName;
  62. }
  63. }
  64. private void newCreateBtnSon_Click(object sender, EventArgs e)
  65. {
  66. if (this.Text == "导入缺口配置文件")
  67. {
  68. saveqkData2PG(txtindir.Text, xianlumingText.Text, zhanmingText.Text);
  69. }else {
  70. saveData2PG(txtindir.Text, xianlumingText.Text, zhanmingText.Text);
  71. }
  72. }
  73. /// <summary>
  74. /// メソッド概要 取文件的编码格式
  75. /// </summary>
  76. /// <remarks>
  77. /// 機能仕様 取文件的编码格式
  78. /// </remarks>
  79. public static Encoding GetEncoding(string filePath)
  80. {
  81. using (var reader = new StreamReader(filePath, Encoding.Default, true))
  82. {
  83. if (reader.Peek() >= 0)
  84. {
  85. reader.Read();
  86. }
  87. Encoding encoding = reader.CurrentEncoding;
  88. reader.Close();
  89. return encoding;
  90. }
  91. }
  92. /// <summary>
  93. /// 读scv
  94. /// </summary>
  95. /// <param name="url"></param>
  96. private static void readqkSCV(string url, string xianlu, string chezhan, ref List<T_quekou_account> gonglvCsvdata)
  97. {
  98. StreamReader sr = new StreamReader(url, GetEncoding(url));
  99. sr.ReadLine();
  100. sr.ReadLine();
  101. while (!sr.EndOfStream)
  102. {
  103. string origonss = sr.ReadLine();
  104. origonss = origonss.Replace(" ", " ");
  105. origonss = origonss.Replace("/t", ",");
  106. origonss = origonss.Replace(" ", ",");
  107. var arrayarr = origonss.ToCharArray();
  108. if (arrayarr[0] == ',' && arrayarr[1] == ',')
  109. {
  110. //简单判断这行是否是多余,例如“,,,,,,,,,,,,,,,,”这种行的录入
  111. continue;
  112. }
  113. T_quekou_account onescvdata = new T_quekou_account();
  114. onescvdata.Chezhan = chezhan;
  115. onescvdata.Xianlu = xianlu;
  116. onescvdata.Qxxh = arrayarr[0].ToString();
  117. // onescvdata.Id = Guid.NewGuid().ToString();
  118. List<string> loopstring = new List<string>();
  119. //使用正则去除属性字段中的逗号的影响
  120. Regex re = new Regex("(?<=,\").*?(?=\",)", RegexOptions.None);
  121. MatchCollection mc = re.Matches(origonss);
  122. //保存原始
  123. foreach (var item in mc)
  124. {
  125. //string ddd = item.ToString().Replace("\"\"", "\"");
  126. loopstring.Add(item.ToString());
  127. }
  128. //替换属性字段中的逗号为"$$$$$$$$$$$$$yxc$$$$$$$$$$$$$$$"
  129. origonss = Regex.Replace(origonss, "(?<=,\").*?(?=\",)", "$$$$$$$$$$$$$yxc$$$$$$$$$$$$$$$");
  130. string[] looparr = origonss.Split(',');
  131. //还原之前被替换的字段属性值
  132. int ii = 0;
  133. for (int i = 0; i < looparr.Length; i++)
  134. {
  135. if (looparr[i] == "\"$$$$$$$yxc$$$$$$$$\"")
  136. {
  137. looparr[i] = loopstring[ii++];
  138. }
  139. }
  140. ii = 0;
  141. //存入T_xxxw_csvdata
  142. //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" };
  143. Type t = onescvdata.GetType();//获得该类的Type
  144. foreach (PropertyInfo pi in t.GetProperties())
  145. {
  146. if (pi.Name != "Id" && pi.Name != "Qxxh" && pi.Name != "Xianlu" && pi.Name != "Chezhan")
  147. {
  148. pi.SetValue(onescvdata, looparr[ii++]);
  149. }
  150. }
  151. gonglvCsvdata.Add(onescvdata);
  152. }
  153. sr.Close();
  154. }
  155. /// <summary>
  156. /// 读scv
  157. /// </summary>
  158. /// <param name="url"></param>
  159. private static void readSCV(string url, string xianlu, string chezhan, ref List<T_gonglv_account> gonglvCsvdata)
  160. {
  161. StreamReader sr = new StreamReader(url, GetEncoding(url));
  162. sr.ReadLine();
  163. sr.ReadLine();
  164. while (!sr.EndOfStream)
  165. {
  166. string origonss = sr.ReadLine();
  167. origonss = origonss.Replace(" ", " ");
  168. origonss = origonss.Replace("/t", ",");
  169. origonss = origonss.Replace(" ", ",");
  170. var arrayarr = origonss.ToCharArray();
  171. if (arrayarr[0] == ',' && arrayarr[1] == ',')
  172. {
  173. //简单判断这行是否是多余,例如“,,,,,,,,,,,,,,,,”这种行的录入
  174. continue;
  175. }
  176. T_gonglv_account onescvdata = new T_gonglv_account();
  177. onescvdata.Chezhan = chezhan;
  178. onescvdata.Xianlu = xianlu;
  179. onescvdata.Qxxh = arrayarr[0].ToString();
  180. // onescvdata.Id = Guid.NewGuid().ToString();
  181. List<string> loopstring = new List<string>();
  182. //使用正则去除属性字段中的逗号的影响
  183. Regex re = new Regex("(?<=,\").*?(?=\",)", RegexOptions.None);
  184. MatchCollection mc = re.Matches(origonss);
  185. //保存原始
  186. foreach (var item in mc)
  187. {
  188. //string ddd = item.ToString().Replace("\"\"", "\"");
  189. loopstring.Add(item.ToString());
  190. }
  191. //替换属性字段中的逗号为"$$$$$$$$$$$$$yxc$$$$$$$$$$$$$$$"
  192. origonss = Regex.Replace(origonss, "(?<=,\").*?(?=\",)", "$$$$$$$$$$$$$yxc$$$$$$$$$$$$$$$");
  193. string[] looparr = origonss.Split(',');
  194. //还原之前被替换的字段属性值
  195. int ii = 0;
  196. for (int i = 0; i < looparr.Length; i++)
  197. {
  198. if (looparr[i] == "\"$$$$$$$yxc$$$$$$$$\"")
  199. {
  200. looparr[i] = loopstring[ii++];
  201. }
  202. }
  203. ii = 0;
  204. //存入T_xxxw_csvdata
  205. //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" };
  206. Type t = onescvdata.GetType();//获得该类的Type
  207. foreach (PropertyInfo pi in t.GetProperties())
  208. {
  209. if (pi.Name != "Id" && pi.Name != "Qxxh" && pi.Name != "Xianlu" && pi.Name != "Chezhan")
  210. {
  211. pi.SetValue(onescvdata, looparr[ii++]);
  212. }
  213. }
  214. gonglvCsvdata.Add(onescvdata);
  215. }
  216. sr.Close();
  217. }
  218. static string uuid_ = string.Empty;//预防报错
  219. public static string saveData2PG(string url, string xianlu, string chezhan)
  220. {
  221. string uuid = uuid_;
  222. List<T_gonglv_account> gonglvCsvdata = new List<T_gonglv_account>();
  223. try
  224. {
  225. readSCV(url, xianlu, chezhan, ref gonglvCsvdata);
  226. DataAnalysisToPG.Insert_gl_data_CsvData(gonglvCsvdata);
  227. XtraMessageBox.Show("导入成功!", "提示", MessageBoxButtons.OK, MessageBoxIcon.Information);
  228. }
  229. catch (Exception)
  230. {
  231. XtraMessageBox.Show("导入失败!", "提示", MessageBoxButtons.OK, MessageBoxIcon.Information);
  232. }
  233. return uuid;
  234. }
  235. public static string saveqkData2PG(string url, string xianlu, string chezhan)
  236. {
  237. string uuid = uuid_;
  238. List<T_quekou_account> quekouCsvdata = new List<T_quekou_account>();
  239. try
  240. {
  241. readqkSCV(url, xianlu, chezhan, ref quekouCsvdata);
  242. DataAnalysisToPG.Insert_qk_data_CsvData(quekouCsvdata);
  243. XtraMessageBox.Show("导入成功!", "提示", MessageBoxButtons.OK, MessageBoxIcon.Information);
  244. }
  245. catch (Exception)
  246. {
  247. XtraMessageBox.Show("导入失败!", "提示", MessageBoxButtons.OK, MessageBoxIcon.Information);
  248. }
  249. return uuid;
  250. }
  251. private void NewcancelBtn_Click(object sender, EventArgs e)
  252. {
  253. this.Close();
  254. }
  255. List<T_daocha_account> daochaData = new List<T_daocha_account>();
  256. /// <summary>
  257. /// 加载模糊查询的数据
  258. /// </summary>
  259. private void ImportGLdataForm_Load(object sender, EventArgs e)
  260. {
  261. daochaData = DBOperation.Operation.DataAnalysisToPG.QueryDCTZALL();
  262. //整理线路名集合
  263. var listtgXLMALL = daochaData.GroupBy(c => c.Xlm).Select(c => c.First()).ToList();
  264. foreach (var item in listtgXLMALL)
  265. {
  266. xianlumingText.Properties.Items.Add(item.Xlm);
  267. }
  268. }
  269. /// <summary>
  270. /// 线路名查询
  271. /// </summary>
  272. private void xianlumingText_TextChanged(object sender, EventArgs e)
  273. {
  274. zhanmingText.Properties.Items.Clear();
  275. var isInputTrueXLM = daochaData.FindAll(delegate (T_daocha_account dc)
  276. {
  277. return dc.Xlm == xianlumingText.Text;
  278. });
  279. //线路名
  280. if (isInputTrueXLM.Count != 0)
  281. {
  282. var listtg = isInputTrueXLM.GroupBy(c => c.Czm).Select(c => c.First()).ToList();
  283. foreach (var item in listtg)
  284. {
  285. zhanmingText.Properties.Items.Add(item.Czm);
  286. }
  287. zhanmingText.Enabled = true;
  288. }
  289. else
  290. {
  291. zhanmingText.Enabled = false;
  292. }
  293. zhanmingText.Text = "";
  294. }
  295. }
  296. }