UploadMakePlan.cs 27 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459
  1. using System;
  2. using System.Data;
  3. using System.IO;
  4. using System.Windows.Forms;
  5. using System.Text;
  6. using NPOI.SS.UserModel;
  7. using NPOI.XSSF.UserModel;
  8. using Oracle.ManagedDataAccess.Client;
  9. namespace FileWatcher
  10. {
  11. public partial class UploadMakePlan : Form
  12. {
  13. string connectionString = "Connection Timeout=0;Pooling=false;Password=select!#%*(;User ID=N_MES;Pooling=false;Data Source=(DESCRIPTION=(ADDRESS_LIST=(ADDRESS=(PROTOCOL=TCP)(HOST=192.168.3.7)(PORT=1521)))(CONNECT_DATA=(SERVER=DEDICATED)(SERVICE_NAME=orcl)));";
  14. StringBuilder sql = new StringBuilder();
  15. DataHelper dh;
  16. string imaster;
  17. public UploadMakePlan()
  18. {
  19. InitializeComponent();
  20. StartPosition = FormStartPosition.CenterScreen;
  21. }
  22. string fieltype = "";
  23. string ftpulr = "";
  24. private void Form1_Load(object sender, EventArgs e)
  25. {
  26. dh = new DataHelper(connectionString);
  27. }
  28. private void Upload_Click(object sender, EventArgs e)
  29. {
  30. DateTime today = DateTime.Value;
  31. string todayDate = today.ToString("MMdd");
  32. string code = dh.GetSerialNumberByCaller("MakePlan");
  33. string id = "";
  34. DataTable dt = (DataTable)dh.ExecuteSql("select mp_id from MakePlan where to_char(mp_begintime,'yyyymmdd')='" + today.ToString("yyyyMMdd") + "'", "select");
  35. if (dt.Rows.Count == 0)
  36. {
  37. id = dh.GetSEQ("MakePlan_seq");
  38. dh.ExecuteSql("insert into MakePlan(MP_ID, MP_CODE, MP_KIND, MP_INDATE, MP_STATUS, MP_STATUSCODE, MP_BEGINTIME)" +
  39. "values(" + id + ",'" + code + "','日计划',sysdate,'在录入','ENTERING',to_date('" + today.ToString("yyyyMMdd") + "','yyyymmdd'))", "insert");
  40. }
  41. else
  42. {
  43. id = dt.Rows[0]["mp_id"].ToString();
  44. dh.ExecuteSql("delete from MakePlandetail where mpd_mpid=" + id, "delete");
  45. }
  46. using (FileStream file = new FileStream(FilePath.Text, FileMode.Open, FileAccess.Read))
  47. {
  48. XSSFWorkbook workbook = new XSSFWorkbook(file);
  49. int detno = 1;
  50. for (int i = 0; i < workbook.NumberOfSheets; i++)
  51. {
  52. ISheet sheet = workbook.GetSheetAt(i);
  53. string sheetName = sheet.SheetName;
  54. if (sheetName.Contains(todayDate))
  55. {
  56. OperateResult.AppendText(sheetName);
  57. OperateResult.AppendText($"Processing sheet: {sheetName}\n");
  58. //Console.WriteLine($"Processing sheet: {sheetName}");
  59. // 获取标题行(第4行)
  60. IRow headerRow = sheet.GetRow(3); // 第4行索引为3
  61. if (headerRow == null)
  62. {
  63. Console.WriteLine("Header row not found.");
  64. continue;
  65. }
  66. // 获取标题列的索引
  67. int wccodeIndex = -1;
  68. int orderdetnoIndex = -1;
  69. int ordercodeIndex = -1;
  70. int planqtyIndex = -1;
  71. int remarkIndex = -1;
  72. for (int colIndex = 0; colIndex < headerRow.LastCellNum; colIndex++)
  73. {
  74. ICell cell = headerRow.GetCell(colIndex);
  75. if (cell != null)
  76. {
  77. string headerValue = cell.ToString().Trim().Replace("\r\n", "").Replace("\n", "").Replace("\r", "");
  78. if (headerValue == "工作中心")
  79. {
  80. wccodeIndex = colIndex;
  81. }
  82. else if (headerValue == "订单序号")
  83. {
  84. orderdetnoIndex = colIndex;
  85. }
  86. else if (headerValue == "订单编号")
  87. {
  88. ordercodeIndex = colIndex;
  89. }
  90. else if (headerValue == "计划数量")
  91. {
  92. planqtyIndex = colIndex;
  93. }
  94. else if (headerValue.Contains("备注") && !headerValue.Contains("生产异常备注"))
  95. {
  96. remarkIndex = colIndex;
  97. }
  98. }
  99. }
  100. if (wccodeIndex == -1 || orderdetnoIndex == -1 || ordercodeIndex == -1 || planqtyIndex == -1 || remarkIndex == -1)
  101. {
  102. Console.WriteLine("Required columns not found in the header.");
  103. continue;
  104. }
  105. using (OracleConnection conn = new OracleConnection(connectionString))
  106. {
  107. conn.Open();
  108. for (int rowIndex = 4; rowIndex <= sheet.LastRowNum; rowIndex++) // 从第5行开始解析数据
  109. {
  110. IRow row = sheet.GetRow(rowIndex);
  111. if (row != null)
  112. {
  113. string wccode = row.GetCell(wccodeIndex, MissingCellPolicy.CREATE_NULL_AS_BLANK).StringCellValue.ToString();
  114. if (wccode == "")
  115. {
  116. continue;
  117. }
  118. string orderdetno = row.GetCell(orderdetnoIndex, MissingCellPolicy.CREATE_NULL_AS_BLANK).ToString();
  119. string ordercode = row.GetCell(ordercodeIndex, MissingCellPolicy.CREATE_NULL_AS_BLANK).ToString();
  120. string planqty = row.GetCell(planqtyIndex, MissingCellPolicy.CREATE_NULL_AS_BLANK).NumericCellValue.ToString();
  121. string remark = row.GetCell(remarkIndex, MissingCellPolicy.CREATE_NULL_AS_BLANK).ToString();
  122. if (!string.IsNullOrEmpty(wccode) && !string.IsNullOrEmpty(orderdetno) && !string.IsNullOrEmpty(ordercode))
  123. {
  124. string day = today.ToString("yyyy-MM-dd");
  125. if (dh.CheckExist("MakePlanDetail left join makeplan on mpd_mpid=mp_id ", "trunc(mp_begintime)=to_date('" + day + "','yyyy-mm-dd') and mpd_orderdetno='" + orderdetno + "' and mpd_ordercode='" + ordercode + "' and mpd_wccode='" + wccode + "'"))
  126. {
  127. OperateResult.AppendText(" 序号'" + orderdetno + "' 销售订单'" + ordercode + "' 工作中心'" + wccode + "' 重复");
  128. return;
  129. }
  130. //销售订单+订单序号存在ERP中,才允许上传
  131. if (dh.CheckExist("saledetail@ERP left join sale@ERP on sa_id=sd_said", "SD_DETNO='" + orderdetno + "' and sa_code='" + ordercode + "'"))
  132. {
  133. OperateResult.AppendText(" 序号'" + orderdetno + "' 销售订单'" + ordercode + "'不存在");
  134. return;
  135. }
  136. if (dh.CheckExist("saledetail@ERP left join sale@ERP on sa_id=sd_said left join (select min(mpd_outqty)mpd_outqty, mpd_ordercode," +
  137. " mpd_orderdetno from(select sum(mpd_outqty)mpd_outqty, mpd_ordercode, mpd_orderdetnofrom MakePlanDetail group by " +
  138. "mpd_ordercode, mpd_orderdetno, MPD_WCCODE)group by mpd_ordercode, mpd_orderdetno) on sa_code = mpd_ordercode and sd_detno" +
  139. " = mpd_orderdetno", "sd_qty<mpd_outqty+" + planqty + " and sa_code='" + ordercode + "' and sd_detno='" + orderdetno + "'"))
  140. {
  141. OperateResult.AppendText(" 序号'" + orderdetno + "' 销售订单'" + ordercode + "'累计排产数量超出");
  142. return;
  143. }
  144. string insertQuery = "INSERT INTO MakePlanDetail (mpd_mpid,mpd_detno,mpd_id,mpd_wccode, mpd_orderdetno, mpd_ordercode,mpd_outqty,mpd_remark) VALUES (" + id + "," + detno + ",MakePlanDetail_seq.nextval,:1, :2, :3,:4,:5)";
  145. using (OracleCommand cmd = new OracleCommand(insertQuery, conn))
  146. {
  147. cmd.Parameters.Add(new OracleParameter(":1", wccode));
  148. cmd.Parameters.Add(new OracleParameter(":2", orderdetno));
  149. cmd.Parameters.Add(new OracleParameter(":3", ordercode));
  150. cmd.Parameters.Add(new OracleParameter(":4", planqty));
  151. cmd.Parameters.Add(new OracleParameter(":5", remark));
  152. cmd.ExecuteNonQuery();
  153. }
  154. }
  155. }
  156. detno = detno + 1;
  157. }
  158. }
  159. dh.ExecuteSql("delete from MakePlanDetail where mpd_orderdetno is null", "delete");
  160. OperateResult.AppendText("计划上传成功");
  161. }
  162. }
  163. }
  164. //上传日期1
  165. if (DateTime1.Value != DateTime.Value)
  166. {
  167. today = DateTime1.Value;
  168. todayDate = today.ToString("MMdd");
  169. code = dh.GetSerialNumberByCaller("MakePlan");
  170. id = "";
  171. dt = (DataTable)dh.ExecuteSql("select mp_id from MakePlan where to_char(mp_begintime,'yyyymmdd')='" + today.ToString("yyyyMMdd") + "'", "select");
  172. if (dt.Rows.Count == 0)
  173. {
  174. id = dh.GetSEQ("MakePlan_seq");
  175. dh.ExecuteSql("insert into MakePlan(MP_ID, MP_CODE, MP_KIND, MP_INDATE, MP_STATUS, MP_STATUSCODE, MP_BEGINTIME)" +
  176. "values(" + id + ",'" + code + "','日计划',sysdate,'在录入','ENTERING',to_date('" + today.ToString("yyyyMMdd") + "','yyyymmdd'))", "insert");
  177. }
  178. else
  179. {
  180. id = dt.Rows[0]["mp_id"].ToString();
  181. dh.ExecuteSql("delete from MakePlandetail where mpd_mpid=" + id, "delete");
  182. }
  183. using (FileStream file = new FileStream(FilePath.Text, FileMode.Open, FileAccess.Read))
  184. {
  185. XSSFWorkbook workbook = new XSSFWorkbook(file);
  186. int detno = 1;
  187. for (int i = 0; i < workbook.NumberOfSheets; i++)
  188. {
  189. ISheet sheet = workbook.GetSheetAt(i);
  190. string sheetName = sheet.SheetName;
  191. if (sheetName.Contains(todayDate))
  192. {
  193. OperateResult.AppendText(sheetName);
  194. OperateResult.AppendText($"Processing sheet: {sheetName}\n");
  195. //Console.WriteLine($"Processing sheet: {sheetName}");
  196. // 获取标题行(第4行)
  197. IRow headerRow = sheet.GetRow(3); // 第4行索引为3
  198. if (headerRow == null)
  199. {
  200. Console.WriteLine("Header row not found.");
  201. continue;
  202. }
  203. // 获取标题列的索引
  204. int wccodeIndex = -1;
  205. int orderdetnoIndex = -1;
  206. int ordercodeIndex = -1;
  207. int planqtyIndex = -1;
  208. int remarkIndex = -1;
  209. for (int colIndex = 0; colIndex < headerRow.LastCellNum; colIndex++)
  210. {
  211. ICell cell = headerRow.GetCell(colIndex);
  212. if (cell != null)
  213. {
  214. string headerValue = cell.ToString().Trim().Replace("\r\n", "").Replace("\n", "").Replace("\r", "");
  215. if (headerValue == "工作中心")
  216. {
  217. wccodeIndex = colIndex;
  218. }
  219. else if (headerValue == "订单序号")
  220. {
  221. orderdetnoIndex = colIndex;
  222. }
  223. else if (headerValue == "订单编号")
  224. {
  225. ordercodeIndex = colIndex;
  226. }
  227. else if (headerValue == "计划数量")
  228. {
  229. planqtyIndex = colIndex;
  230. }
  231. else if (headerValue.Contains("备注") && !headerValue.Contains("生产异常备注"))
  232. {
  233. remarkIndex = colIndex;
  234. }
  235. }
  236. }
  237. if (wccodeIndex == -1 || orderdetnoIndex == -1 || ordercodeIndex == -1 || planqtyIndex == -1 || remarkIndex == -1)
  238. {
  239. Console.WriteLine("Required columns not found in the header.");
  240. continue;
  241. }
  242. using (OracleConnection conn = new OracleConnection(connectionString))
  243. {
  244. conn.Open();
  245. for (int rowIndex = 4; rowIndex <= sheet.LastRowNum; rowIndex++) // 从第5行开始解析数据
  246. {
  247. IRow row = sheet.GetRow(rowIndex);
  248. if (row != null)
  249. {
  250. string wccode = row.GetCell(wccodeIndex, MissingCellPolicy.CREATE_NULL_AS_BLANK).StringCellValue.ToString();
  251. if (wccode == "")
  252. {
  253. continue;
  254. }
  255. string orderdetno = row.GetCell(orderdetnoIndex, MissingCellPolicy.CREATE_NULL_AS_BLANK).ToString();
  256. string ordercode = row.GetCell(ordercodeIndex, MissingCellPolicy.CREATE_NULL_AS_BLANK).ToString();
  257. string planqty = row.GetCell(planqtyIndex, MissingCellPolicy.CREATE_NULL_AS_BLANK).NumericCellValue.ToString();
  258. string remark = row.GetCell(remarkIndex, MissingCellPolicy.CREATE_NULL_AS_BLANK).ToString();
  259. if (!string.IsNullOrEmpty(wccode) && !string.IsNullOrEmpty(orderdetno) && !string.IsNullOrEmpty(ordercode))
  260. {
  261. string day = today.ToString("yyyy-MM-dd");
  262. if (!dh.CheckExist("MakePlanDetail left join makeplan on mpd_mpid=mp_id ", "trunc(mp_begintime)=to_date('" + day + "','yyyy-mm-dd') and mpd_orderdetno='" + orderdetno + "' and mpd_ordercode='" + ordercode + "' and mpd_wccode='" + wccode + "'"))
  263. {
  264. string insertQuery = "INSERT INTO MakePlanDetail (mpd_mpid,mpd_detno,mpd_id,mpd_wccode, mpd_orderdetno, mpd_ordercode,mpd_outqty,mpd_remark) VALUES (" + id + "," + detno + ",MakePlanDetail_seq.nextval,:1, :2, :3,:4,:5)";
  265. using (OracleCommand cmd = new OracleCommand(insertQuery, conn))
  266. {
  267. cmd.Parameters.Add(new OracleParameter(":1", wccode));
  268. cmd.Parameters.Add(new OracleParameter(":2", orderdetno));
  269. cmd.Parameters.Add(new OracleParameter(":3", ordercode));
  270. cmd.Parameters.Add(new OracleParameter(":4", planqty));
  271. cmd.Parameters.Add(new OracleParameter(":5", remark));
  272. cmd.ExecuteNonQuery();
  273. }
  274. }
  275. else
  276. {
  277. OperateResult.AppendText(" 序号'" + orderdetno + "' 销售订单'" + ordercode + "' 工作中心'" + wccode + "' 重复");
  278. }
  279. }
  280. }
  281. detno = detno + 1;
  282. }
  283. }
  284. dh.ExecuteSql("delete from MakePlanDetail where mpd_orderdetno is null", "delete");
  285. OperateResult.AppendText("计划上传成功");
  286. }
  287. }
  288. }
  289. }
  290. //上传日期2
  291. if (DateTime2.Value != DateTime1.Value && DateTime2.Value != DateTime.Value)
  292. {
  293. today = DateTime2.Value;
  294. todayDate = today.ToString("MMdd");
  295. code = dh.GetSerialNumberByCaller("MakePlan");
  296. id = "";
  297. dt = (DataTable)dh.ExecuteSql("select mp_id from MakePlan where to_char(mp_begintime,'yyyymmdd')='" + today.ToString("yyyyMMdd") + "'", "select");
  298. if (dt.Rows.Count == 0)
  299. {
  300. id = dh.GetSEQ("MakePlan_seq");
  301. dh.ExecuteSql("insert into MakePlan(MP_ID, MP_CODE, MP_KIND, MP_INDATE, MP_STATUS, MP_STATUSCODE, MP_BEGINTIME)" +
  302. "values(" + id + ",'" + code + "','日计划',sysdate,'在录入','ENTERING',to_date('" + today.ToString("yyyyMMdd") + "','yyyymmdd'))", "insert");
  303. }
  304. else
  305. {
  306. id = dt.Rows[0]["mp_id"].ToString();
  307. dh.ExecuteSql("delete from MakePlandetail where mpd_mpid=" + id, "delete");
  308. }
  309. using (FileStream file = new FileStream(FilePath.Text, FileMode.Open, FileAccess.Read))
  310. {
  311. XSSFWorkbook workbook = new XSSFWorkbook(file);
  312. int detno = 1;
  313. for (int i = 0; i < workbook.NumberOfSheets; i++)
  314. {
  315. ISheet sheet = workbook.GetSheetAt(i);
  316. string sheetName = sheet.SheetName;
  317. if (sheetName.Contains(todayDate))
  318. {
  319. OperateResult.AppendText(sheetName);
  320. OperateResult.AppendText($"Processing sheet: {sheetName}\n");
  321. //Console.WriteLine($"Processing sheet: {sheetName}");
  322. // 获取标题行(第4行)
  323. IRow headerRow = sheet.GetRow(3); // 第4行索引为3
  324. if (headerRow == null)
  325. {
  326. Console.WriteLine("Header row not found.");
  327. continue;
  328. }
  329. // 获取标题列的索引
  330. int wccodeIndex = -1;
  331. int orderdetnoIndex = -1;
  332. int ordercodeIndex = -1;
  333. int planqtyIndex = -1;
  334. int remarkIndex = -1;
  335. for (int colIndex = 0; colIndex < headerRow.LastCellNum; colIndex++)
  336. {
  337. ICell cell = headerRow.GetCell(colIndex);
  338. if (cell != null)
  339. {
  340. string headerValue = cell.ToString().Trim().Replace("\r\n", "").Replace("\n", "").Replace("\r", "");
  341. if (headerValue == "工作中心")
  342. {
  343. wccodeIndex = colIndex;
  344. }
  345. else if (headerValue == "订单序号")
  346. {
  347. orderdetnoIndex = colIndex;
  348. }
  349. else if (headerValue == "订单编号")
  350. {
  351. ordercodeIndex = colIndex;
  352. }
  353. else if (headerValue == "计划数量")
  354. {
  355. planqtyIndex = colIndex;
  356. }
  357. else if (headerValue.Contains("备注") && !headerValue.Contains("生产异常备注"))
  358. {
  359. remarkIndex = colIndex;
  360. }
  361. }
  362. }
  363. if (wccodeIndex == -1 || orderdetnoIndex == -1 || ordercodeIndex == -1 || planqtyIndex == -1 || remarkIndex == -1)
  364. {
  365. Console.WriteLine("Required columns not found in the header.");
  366. continue;
  367. }
  368. using (OracleConnection conn = new OracleConnection(connectionString))
  369. {
  370. conn.Open();
  371. for (int rowIndex = 4; rowIndex <= sheet.LastRowNum; rowIndex++) // 从第5行开始解析数据
  372. {
  373. IRow row = sheet.GetRow(rowIndex);
  374. if (row != null)
  375. {
  376. string wccode = row.GetCell(wccodeIndex, MissingCellPolicy.CREATE_NULL_AS_BLANK).StringCellValue.ToString();
  377. if (wccode == "")
  378. {
  379. continue;
  380. }
  381. string orderdetno = row.GetCell(orderdetnoIndex, MissingCellPolicy.CREATE_NULL_AS_BLANK).ToString();
  382. string ordercode = row.GetCell(ordercodeIndex, MissingCellPolicy.CREATE_NULL_AS_BLANK).ToString();
  383. string planqty = row.GetCell(planqtyIndex, MissingCellPolicy.CREATE_NULL_AS_BLANK).NumericCellValue.ToString();
  384. string remark = row.GetCell(remarkIndex, MissingCellPolicy.CREATE_NULL_AS_BLANK).ToString();
  385. if (!string.IsNullOrEmpty(wccode) && !string.IsNullOrEmpty(orderdetno) && !string.IsNullOrEmpty(ordercode))
  386. {
  387. string day = today.ToString("yyyy-MM-dd");
  388. if (!dh.CheckExist("MakePlanDetail left join makeplan on mpd_mpid=mp_id ", "trunc(mp_begintime)=to_date('" + day + "','yyyy-mm-dd') and mpd_orderdetno='" + orderdetno + "' and mpd_ordercode='" + ordercode + "' and mpd_wccode='" + wccode + "'"))
  389. {
  390. string insertQuery = "INSERT INTO MakePlanDetail (mpd_mpid,mpd_detno,mpd_id,mpd_wccode, mpd_orderdetno, mpd_ordercode,mpd_outqty,mpd_remark) VALUES (" + id + "," + detno + ",MakePlanDetail_seq.nextval,:1, :2, :3,:4,:5)";
  391. using (OracleCommand cmd = new OracleCommand(insertQuery, conn))
  392. {
  393. cmd.Parameters.Add(new OracleParameter(":1", wccode));
  394. cmd.Parameters.Add(new OracleParameter(":2", orderdetno));
  395. cmd.Parameters.Add(new OracleParameter(":3", ordercode));
  396. cmd.Parameters.Add(new OracleParameter(":4", planqty));
  397. cmd.Parameters.Add(new OracleParameter(":5", remark));
  398. cmd.ExecuteNonQuery();
  399. }
  400. }
  401. else
  402. {
  403. OperateResult.AppendText(" 序号'" + orderdetno + "' 销售订单'" + ordercode + "' 工作中心'" + wccode + "' 重复");
  404. }
  405. }
  406. }
  407. detno = detno + 1;
  408. }
  409. }
  410. dh.ExecuteSql("delete from MakePlanDetail where mpd_orderdetno is null", "delete");
  411. OperateResult.AppendText("计划上传成功");
  412. }
  413. }
  414. }
  415. }
  416. }
  417. private void ChooseFile_Click(object sender, EventArgs e)
  418. {
  419. DialogResult result;
  420. result = ImportExcel1.ShowDialog();
  421. if (result == DialogResult.OK)
  422. {
  423. FilePath.Text = ImportExcel1.FileName;
  424. }
  425. }
  426. }
  427. }