UploadMakePlan.cs 11 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199
  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. using System.Reflection.Emit;
  10. using Microsoft.Office.Interop.Excel;
  11. namespace FileWatcher
  12. {
  13. public partial class UploadMakePlan : Form
  14. {
  15. 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)));";
  16. StringBuilder sql = new StringBuilder();
  17. DataHelper dh;
  18. string imaster;
  19. public UploadMakePlan()
  20. {
  21. InitializeComponent();
  22. StartPosition = FormStartPosition.CenterScreen;
  23. }
  24. string fieltype = "";
  25. string ftpulr = "";
  26. private void Form1_Load(object sender, EventArgs e)
  27. {
  28. dh = new DataHelper(connectionString);
  29. }
  30. private void Upload_Click(object sender, EventArgs e)
  31. {
  32. DateTime today = DateTime.Value;
  33. string todayDate = today.ToString("MMdd");
  34. string code = dh.GetSerialNumberByCaller("MakePlan");
  35. string id = "";
  36. System.Data.DataTable dt = (System.Data.DataTable)dh.ExecuteSql("select mp_id from MakePlan where to_char(mp_begintime,'yyyymmdd')='" + today.ToString("yyyyMMdd") + "'", "select");
  37. if (dt.Rows.Count == 0)
  38. {
  39. id = dh.GetSEQ("MakePlan_seq");
  40. dh.ExecuteSql("insert into MakePlan(MP_ID, MP_CODE, MP_KIND, MP_INDATE, MP_STATUS, MP_STATUSCODE, MP_BEGINTIME)" +
  41. "values(" + id + ",'" + code + "','日计划',sysdate,'在录入','ENTERING',to_date('" + today.ToString("yyyyMMdd") + "','yyyymmdd'))", "insert");
  42. }
  43. else
  44. {
  45. id = dt.Rows[0]["mp_id"].ToString();
  46. dh.ExecuteSql("delete from MakePlandetail where mpd_mpid=" + id, "delete");
  47. }
  48. using (FileStream file = new FileStream(FilePath.Text, FileMode.Open, FileAccess.Read))
  49. {
  50. XSSFWorkbook workbook = new XSSFWorkbook(file);
  51. int detno = 1;
  52. for (int i = 0; i < workbook.NumberOfSheets; i++)
  53. {
  54. ISheet sheet = workbook.GetSheetAt(i);
  55. string sheetName = sheet.SheetName;
  56. if (sheetName.Contains(todayDate))
  57. {
  58. OperateResult.AppendText(sheetName);
  59. OperateResult.AppendText($"Processing sheet: {sheetName}\n");
  60. //Console.WriteLine($"Processing sheet: {sheetName}");
  61. // 获取标题行(第4行)
  62. IRow headerRow = sheet.GetRow(3); // 第4行索引为3
  63. if (headerRow == null)
  64. {
  65. Console.WriteLine("Header row not found.");
  66. continue;
  67. }
  68. // 获取标题列的索引
  69. int wccodeIndex = -1;
  70. int orderdetnoIndex = -1;
  71. int ordercodeIndex = -1;
  72. int planqtyIndex = -1;
  73. int remarkIndex = -1;
  74. for (int colIndex = 0; colIndex < headerRow.LastCellNum; colIndex++)
  75. {
  76. ICell cell = headerRow.GetCell(colIndex);
  77. if (cell != null)
  78. {
  79. string headerValue = cell.ToString().Trim().Replace("\r\n", "").Replace("\n", "").Replace("\r", "");
  80. if (headerValue == "工作中心")
  81. {
  82. wccodeIndex = colIndex;
  83. }
  84. else if (headerValue == "订单序号")
  85. {
  86. orderdetnoIndex = colIndex;
  87. }
  88. else if (headerValue == "订单编号")
  89. {
  90. ordercodeIndex = colIndex;
  91. }
  92. else if (headerValue == "计划数量")
  93. {
  94. planqtyIndex = colIndex;
  95. }
  96. else if (headerValue.Contains("备注") && !headerValue.Contains("生产异常备注"))
  97. {
  98. remarkIndex = colIndex;
  99. }
  100. }
  101. }
  102. if (wccodeIndex == -1 || orderdetnoIndex == -1 || ordercodeIndex == -1 || planqtyIndex == -1 || remarkIndex == -1)
  103. {
  104. Console.WriteLine("Required columns not found in the header.");
  105. continue;
  106. }
  107. using (OracleConnection conn = new OracleConnection(connectionString))
  108. {
  109. conn.Open();
  110. for (int rowIndex = 4; rowIndex <= sheet.LastRowNum; rowIndex++) // 从第5行开始解析数据
  111. {
  112. IRow row = sheet.GetRow(rowIndex);
  113. if (row != null)
  114. {
  115. string wccode = row.GetCell(wccodeIndex, MissingCellPolicy.CREATE_NULL_AS_BLANK).StringCellValue.ToString();
  116. if (wccode == "")
  117. {
  118. continue;
  119. }
  120. string orderdetno = row.GetCell(orderdetnoIndex, MissingCellPolicy.CREATE_NULL_AS_BLANK).ToString();
  121. string ordercode = row.GetCell(ordercodeIndex, MissingCellPolicy.CREATE_NULL_AS_BLANK).ToString();
  122. string planqty = row.GetCell(planqtyIndex, MissingCellPolicy.CREATE_NULL_AS_BLANK).NumericCellValue.ToString();
  123. string remark = row.GetCell(remarkIndex, MissingCellPolicy.CREATE_NULL_AS_BLANK).ToString();
  124. if (!string.IsNullOrEmpty(wccode) && !string.IsNullOrEmpty(orderdetno) && !string.IsNullOrEmpty(ordercode))
  125. {
  126. string day = today.ToString("yyyy-MM-dd");
  127. 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 + "'"))
  128. {
  129. OperateResult.AppendText(" 序号'" + orderdetno + "' 销售订单'" + ordercode + "' 工作中心'" + wccode + "' 重复");
  130. return;
  131. }
  132. //销售订单+订单序号存在ERP中,才允许上传
  133. if (!dh.CheckExist("saledetail@ERP left join sale@ERP on sa_id=sd_said", "SD_DETNO='" + orderdetno + "' and sa_code='" + ordercode + "'"))
  134. {
  135. OperateResult.AppendText(" 序号'" + orderdetno + "' 销售订单'" + ordercode + "'不存在");
  136. return;
  137. }
  138. //if (dh.CheckExist("saledetail@ERP left join sale@ERP on sa_id=sd_said left join (select min(mpd_outqty)mpd_outqty, mpd_ordercode," +
  139. // " mpd_orderdetno from(select sum(mpd_outqty)mpd_outqty, mpd_ordercode, mpd_orderdetno from MakePlanDetail group by " +
  140. // "mpd_ordercode, mpd_orderdetno, MPD_WCCODE)group by mpd_ordercode, mpd_orderdetno) on sa_code = mpd_ordercode and sd_detno" +
  141. // " = mpd_orderdetno", "sd_qty<mpd_outqty+" + planqty + " and sa_code='" + ordercode + "' and sd_detno='" + orderdetno + "'"))
  142. //{
  143. // OperateResult.AppendText(" 序号'" + orderdetno + "' 销售订单'" + ordercode + "'累计排产数量超出");
  144. // //return;
  145. //}
  146. 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)";
  147. using (OracleCommand cmd = new OracleCommand(insertQuery, conn))
  148. {
  149. cmd.Parameters.Add(new OracleParameter(":1", wccode));
  150. cmd.Parameters.Add(new OracleParameter(":2", orderdetno));
  151. cmd.Parameters.Add(new OracleParameter(":3", ordercode));
  152. cmd.Parameters.Add(new OracleParameter(":4", planqty));
  153. cmd.Parameters.Add(new OracleParameter(":5", remark));
  154. cmd.ExecuteNonQuery();
  155. }
  156. }
  157. }
  158. detno = detno + 1;
  159. }
  160. }
  161. dh.ExecuteSql("delete from MakePlanDetail where mpd_orderdetno is null", "delete");
  162. OperateResult.AppendText("计划上传成功");
  163. string Error = "";
  164. string[] param = new string[] { id, "0", Error };
  165. dh.CallProcedure("USER_PLANSPLIT_COMMIT", ref param);
  166. dh.CallProcedure("USER_PLANINSERT_WORK", ref param);
  167. }
  168. }
  169. }
  170. }
  171. private void ChooseFile_Click(object sender, EventArgs e)
  172. {
  173. DialogResult result;
  174. result = ImportExcel1.ShowDialog();
  175. if (result == DialogResult.OK)
  176. {
  177. FilePath.Text = ImportExcel1.FileName;
  178. }
  179. }
  180. }
  181. }