UploadMakePlan.cs 12 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213
  1. using System;
  2. using System.IO;
  3. using System.Windows.Forms;
  4. using System.Text;
  5. using NPOI.SS.UserModel;
  6. using NPOI.XSSF.UserModel;
  7. using Oracle.ManagedDataAccess.Client;
  8. namespace FileWatcher
  9. {
  10. public partial class UploadMakePlan : Form
  11. {
  12. string connectionString = "Connection Timeout=0;Pooling=false;Password=select!#%*(;User ID=N_MES;Pooling=false;Data Source=(DESCRIPTION=(ADDRESS_LIST=(ADDRESS=(PROTOCOL=TCP)(HOST=10.1.81.208)(PORT=11710)))(CONNECT_DATA=(SERVER=DEDICATED)(SERVICE_NAME=orcl)));";
  13. StringBuilder sql = new StringBuilder();
  14. DataHelper dh;
  15. string imaster;
  16. public UploadMakePlan()
  17. {
  18. InitializeComponent();
  19. StartPosition = FormStartPosition.CenterScreen;
  20. }
  21. string fieltype = "";
  22. string ftpulr = "";
  23. private void Form1_Load(object sender, EventArgs e)
  24. {
  25. dh = new DataHelper(connectionString);
  26. }
  27. private void Upload_Click(object sender, EventArgs e)
  28. {
  29. DateTime today = DateTime.Value;
  30. string todayDate = today.ToString("MMdd");
  31. string code = "";
  32. string[] param = new string[] { "MakePlan", "1", code };
  33. dh.CallProcedure("SP_GETMAXNUMBER", ref param);
  34. code = param[2];
  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. {
  57. //Console.WriteLine($"Processing sheet: {sheetName}");
  58. // 获取标题行(第4行)
  59. string wccode = sheet.GetRow(2).GetCell(2).StringCellValue; // 第4行索引为3
  60. if (wccode == "")
  61. {
  62. OperateResult.AppendText("工作中心不存在");
  63. return;
  64. }
  65. IRow headerRow = sheet.GetRow(4); // 第4行索引为3
  66. if (headerRow == null)
  67. {
  68. Console.WriteLine("Header row not found.");
  69. continue;
  70. }
  71. // 获取标题列的索引
  72. int dateIndex = -1;
  73. int orderdetnoIndex = -1;
  74. int ordercodeIndex = -1;
  75. int planqtyIndex = -1;
  76. int remarkIndex = -1;
  77. int stepcodeIndex = -1;
  78. for (int colIndex = 0; colIndex < headerRow.LastCellNum; colIndex++)
  79. {
  80. ICell cell = headerRow.GetCell(colIndex);
  81. if (cell != null)
  82. {
  83. string headerValue = cell.ToString().Trim().Replace("\r\n", "").Replace("\n", "").Replace("\r", "");
  84. 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 == "日期")
  101. {
  102. dateIndex = colIndex;
  103. }
  104. else if (headerValue.Contains("备注") && !headerValue.Contains("生产异常备注"))
  105. {
  106. remarkIndex = colIndex;
  107. }
  108. }
  109. }
  110. if (orderdetnoIndex == -1 || ordercodeIndex == -1 || planqtyIndex == -1 || remarkIndex == -1 || stepcodeIndex == -1)
  111. {
  112. OperateResult.AppendText("列头缺少,请检查表结构");
  113. return;
  114. }
  115. using (OracleConnection conn = new OracleConnection(connectionString))
  116. {
  117. conn.Open();
  118. for (int rowIndex = 5; rowIndex <= sheet.LastRowNum; rowIndex++) // 从第5行开始解析数据
  119. {
  120. IRow row = sheet.GetRow(rowIndex);
  121. if (row != null)
  122. {
  123. string date = row.GetCell(dateIndex, MissingCellPolicy.CREATE_NULL_AS_BLANK).ToString();
  124. if (date == "")
  125. {
  126. break;
  127. }
  128. string orderdetno = row.GetCell(orderdetnoIndex, MissingCellPolicy.CREATE_NULL_AS_BLANK).ToString();
  129. string ordercode = row.GetCell(ordercodeIndex, MissingCellPolicy.CREATE_NULL_AS_BLANK).ToString();
  130. string planqty = row.GetCell(planqtyIndex, MissingCellPolicy.CREATE_NULL_AS_BLANK).NumericCellValue.ToString();
  131. string remark = row.GetCell(remarkIndex, MissingCellPolicy.CREATE_NULL_AS_BLANK).ToString();
  132. string stepcode = row.GetCell(stepcodeIndex, MissingCellPolicy.CREATE_NULL_AS_BLANK).ToString();
  133. if (todayDate == date && !string.IsNullOrEmpty(wccode) && !string.IsNullOrEmpty(orderdetno) && !string.IsNullOrEmpty(ordercode))
  134. {
  135. string day = today.ToString("yyyy-MM-dd");
  136. 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 + "'"))
  137. {
  138. OperateResult.AppendText(" 序号'" + orderdetno + "' 销售订单'" + ordercode + "' 工作中心'" + wccode + "' 重复");
  139. return;
  140. }
  141. //销售订单+订单序号存在ERP中,才允许上传
  142. //if (!dh.CheckExist("saledetail@ERP left join sale@ERP on sa_id=sd_said", "SD_DETNO='" + orderdetno + "' and sa_code='" + ordercode + "'"))
  143. //{
  144. // OperateResult.AppendText(" 序号'" + orderdetno + "' 销售订单'" + ordercode + "'不存在");
  145. // return;
  146. //}
  147. //dt = (System.Data.DataTable)dh.ExecuteSql("select * from saledetail@ERP left join sale@ERP on sa_id=sd_said left join " +
  148. // "(select min(mpd_outqty)mpd_outqty, mpd_ordercode,mpd_orderdetno from(select mpd_stepcode,nvl(sum(mpd_outqty),0)mpd_outqty, " +
  149. // "mpd_ordercode, mpd_orderdetno from MakePlanDetail where mpd_wccode='" + wccode + "' and nvl(mpd_stepcode,' ')='" + stepcode + "' group by mpd_ordercode, mpd_orderdetno,mpd_stepcode)group " +
  150. // "by mpd_ordercode, mpd_orderdetno,mpd_stepcode) on sa_code = mpd_ordercode and sd_detno =mpd_orderdetno " +
  151. // "where sd_qty<nvl(mpd_outqty,0)+" + planqty + " and sa_code='" + ordercode + "' and sd_detno='" + orderdetno + "'", "select");
  152. //if (dt.Rows.Count > 0)
  153. //{
  154. // OperateResult.AppendText(" 序号'" + orderdetno + "' 销售订单'" + ordercode + "'工序" + stepcode + "累计排产数量超出");
  155. // return;
  156. //}
  157. 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)";
  158. using (OracleCommand cmd = new OracleCommand(insertQuery, conn))
  159. {
  160. cmd.Parameters.Add(new OracleParameter(":1", wccode));
  161. cmd.Parameters.Add(new OracleParameter(":2", orderdetno));
  162. cmd.Parameters.Add(new OracleParameter(":3", ordercode));
  163. cmd.Parameters.Add(new OracleParameter(":4", planqty));
  164. cmd.Parameters.Add(new OracleParameter(":5", remark));
  165. cmd.Parameters.Add(new OracleParameter(":6", stepcode));
  166. cmd.ExecuteNonQuery();
  167. }
  168. }
  169. }
  170. detno = detno + 1;
  171. }
  172. }
  173. dh.ExecuteSql("delete from MakePlanDetail where mpd_orderdetno is null", "delete");
  174. OperateResult.AppendText($"Processing sheet: {sheetName}计划上传成功\n");
  175. string Error = "";
  176. param = new string[] { id, "0", Error };
  177. dh.CallProcedure("USER_PLANSPLIT_COMMIT", ref param);
  178. dh.CallProcedure("USER_PLANINSERT_WORK", ref param);
  179. }
  180. }
  181. }
  182. }
  183. private void ChooseFile_Click(object sender, EventArgs e)
  184. {
  185. DialogResult result;
  186. result = ImportExcel1.ShowDialog();
  187. if (result == DialogResult.OK)
  188. {
  189. FilePath.Text = ImportExcel1.FileName;
  190. }
  191. }
  192. }
  193. }