UploadMakePlan.cs 13 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227
  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. using Microsoft.Office.Interop.Excel;
  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 = "";
  33. string[] param = new string[] { "MakePlan", "1", code };
  34. dh.CallProcedure("SP_GETMAXNUMBER", ref param);
  35. code = param[2];
  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 == "工艺库")
  58. {
  59. continue;
  60. }
  61. {
  62. //Console.WriteLine($"Processing sheet: {sheetName}");
  63. // 获取标题行(第4行)
  64. string wccode = sheet.GetRow(2).GetCell(1).StringCellValue; // 第4行索引为3
  65. if (wccode == "")
  66. {
  67. OperateResult.AppendText("工作中心不存在");
  68. return;
  69. }
  70. IRow headerRow = sheet.GetRow(4); // 第4行索引为3
  71. if (headerRow == null)
  72. {
  73. Console.WriteLine("Header row not found.");
  74. continue;
  75. }
  76. // 获取标题列的索引
  77. int dateIndex = -1;
  78. int orderdetnoIndex = -1;
  79. int ordercodeIndex = -1;
  80. int planqtyIndex = -1;
  81. int remarkIndex = -1;
  82. int stepcodeIndex = -1;
  83. for (int colIndex = 0; colIndex < headerRow.LastCellNum; colIndex++)
  84. {
  85. ICell cell = headerRow.GetCell(colIndex);
  86. if (cell != null)
  87. {
  88. string headerValue = cell.ToString().Trim().Replace("\r\n", "").Replace("\n", "").Replace("\r", "");
  89. if (headerValue == "订单序号")
  90. {
  91. orderdetnoIndex = colIndex;
  92. }
  93. else if (headerValue == "订单编号")
  94. {
  95. ordercodeIndex = colIndex;
  96. }
  97. else if (headerValue == "计划数量")
  98. {
  99. planqtyIndex = colIndex;
  100. }
  101. else if (headerValue == "工序名称")
  102. {
  103. stepcodeIndex = colIndex;
  104. }
  105. else if (headerValue == "日期")
  106. {
  107. dateIndex = colIndex;
  108. }
  109. else if (headerValue.Contains("备注") && !headerValue.Contains("生产异常备注"))
  110. {
  111. remarkIndex = colIndex;
  112. }
  113. }
  114. }
  115. if (orderdetnoIndex == -1 || ordercodeIndex == -1 || planqtyIndex == -1 || remarkIndex == -1 || stepcodeIndex == -1)
  116. {
  117. OperateResult.AppendText("列头缺少,请检查表结构");
  118. return;
  119. }
  120. bool GetPlan = false;
  121. using (OracleConnection conn = new OracleConnection(connectionString))
  122. {
  123. conn.Open();
  124. for (int rowIndex = 5; rowIndex <= sheet.LastRowNum; rowIndex++) // 从第5行开始解析数据
  125. {
  126. IRow row = sheet.GetRow(rowIndex);
  127. if (row != null)
  128. {
  129. string date = row.GetCell(dateIndex, MissingCellPolicy.CREATE_NULL_AS_BLANK).ToString();
  130. if (date == "")
  131. {
  132. break;
  133. }
  134. string orderdetno = row.GetCell(orderdetnoIndex, MissingCellPolicy.CREATE_NULL_AS_BLANK).ToString();
  135. string ordercode = row.GetCell(ordercodeIndex, MissingCellPolicy.CREATE_NULL_AS_BLANK).ToString();
  136. string planqty = row.GetCell(planqtyIndex, MissingCellPolicy.CREATE_NULL_AS_BLANK).NumericCellValue.ToString();
  137. string remark = row.GetCell(remarkIndex, MissingCellPolicy.CREATE_NULL_AS_BLANK).ToString();
  138. string stepcode = row.GetCell(stepcodeIndex, MissingCellPolicy.CREATE_NULL_AS_BLANK).ToString();
  139. if (todayDate == date && !string.IsNullOrEmpty(wccode) && !string.IsNullOrEmpty(orderdetno) && !string.IsNullOrEmpty(ordercode))
  140. {
  141. string day = today.ToString("yyyy-MM-dd");
  142. 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 + "'"))
  143. {
  144. OperateResult.AppendText(" 序号'" + orderdetno + "' 销售订单'" + ordercode + "' 工作中心'" + wccode + "' 重复");
  145. return;
  146. }
  147. //销售订单 + 订单序号存在ERP中,才允许上传
  148. if (!dh.CheckExist("saledetail@ERP left join sale@ERP on sa_id=sd_said", "SD_DETNO='" + orderdetno + "' and sa_code='" + ordercode + "'"))
  149. {
  150. OperateResult.AppendText(" 序号'" + orderdetno + "' 销售订单'" + ordercode + "'不存在");
  151. return;
  152. }
  153. if (!dh.CheckExist("make", "ma_saledetno='" + orderdetno + "' and ma_salecode='" + ordercode + "'"))
  154. {
  155. OperateResult.AppendText(" 序号'" + orderdetno + "' 销售订单'" + ordercode + "'未匹配到工单号");
  156. return;
  157. }
  158. dt = (System.Data.DataTable)dh.ExecuteSql("select * from saledetail@ERP left join sale@ERP on sa_id=sd_said left join " +
  159. "(select min(mpd_outqty)mpd_outqty, mpd_ordercode,mpd_orderdetno from(select mpd_stepcode,nvl(sum(mpd_outqty),0)mpd_outqty, " +
  160. "mpd_ordercode, mpd_orderdetno from MakePlanDetail where mpd_wccode='" + wccode + "' and nvl(mpd_stepcode,' ')='" + stepcode + "' group by mpd_ordercode, mpd_orderdetno,mpd_stepcode)group " +
  161. "by mpd_ordercode, mpd_orderdetno,mpd_stepcode) on sa_code = mpd_ordercode and sd_detno =mpd_orderdetno " +
  162. "where sd_qty<nvl(mpd_outqty,0)+" + planqty + " and sa_code='" + ordercode + "' and sd_detno='" + orderdetno + "'", "select");
  163. if (dt.Rows.Count > 0)
  164. {
  165. OperateResult.AppendText(" 序号'" + orderdetno + "' 销售订单'" + ordercode + "'工序" + stepcode + "累计排产数量超出");
  166. return;
  167. }
  168. 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)";
  169. using (OracleCommand cmd = new OracleCommand(insertQuery, conn))
  170. {
  171. cmd.Parameters.Add(new OracleParameter(":1", wccode));
  172. cmd.Parameters.Add(new OracleParameter(":2", orderdetno));
  173. cmd.Parameters.Add(new OracleParameter(":3", ordercode));
  174. cmd.Parameters.Add(new OracleParameter(":4", planqty));
  175. cmd.Parameters.Add(new OracleParameter(":5", remark));
  176. cmd.Parameters.Add(new OracleParameter(":6", stepcode));
  177. cmd.ExecuteNonQuery();
  178. }
  179. GetPlan = true;
  180. }
  181. }
  182. detno = detno + 1;
  183. }
  184. }
  185. dh.ExecuteSql("delete from MakePlanDetail where mpd_orderdetno is null", "delete");
  186. if (GetPlan)
  187. {
  188. OperateResult.AppendText($"Processing sheet: {sheetName}计划上传成功\n");
  189. }
  190. else
  191. {
  192. OperateResult.AppendText($"Processing sheet: {sheetName}没有需要上传的计划\n");
  193. }
  194. }
  195. }
  196. string Error = "";
  197. param = new string[] { id, "0", Error };
  198. dh.CallProcedure("USER_PLANSPLIT_COMMIT", ref param);
  199. dh.CallProcedure("USER_PLANINSERT_WORK", ref param);
  200. }
  201. }
  202. private void ChooseFile_Click(object sender, EventArgs e)
  203. {
  204. DialogResult result;
  205. result = ImportExcel1.ShowDialog();
  206. if (result == DialogResult.OK)
  207. {
  208. FilePath.Text = ImportExcel1.FileName;
  209. }
  210. }
  211. }
  212. }