UploadMakePlan.cs 13 KB

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