UploadMakePlan.cs 11 KB

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