Special_ChangeMake.cs 16 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341
  1. using NPOI.HSSF.UserModel;
  2. using NPOI.SS.UserModel;
  3. using System;
  4. using System.Collections.Generic;
  5. using System.Data;
  6. using System.Drawing;
  7. using System.IO;
  8. using System.Windows.Forms;
  9. using UAS_MES_NEW.DataOperate;
  10. using UAS_MES_NEW.Entity;
  11. using UAS_MES_NEW.PublicMethod;
  12. namespace UAS_MES_NEW.Special
  13. {
  14. public partial class Special_ChangeMake : Form
  15. {
  16. DataHelper dh;
  17. LogStringBuilder sql = new LogStringBuilder();
  18. AutoSizeFormClass asc = new AutoSizeFormClass();
  19. DataTable Dbfind;
  20. DataTable dt;
  21. List<String> sb = new List<string>();
  22. public Special_ChangeMake()
  23. {
  24. InitializeComponent();
  25. }
  26. private void Special_BoxSplit_Load(object sender, EventArgs e)
  27. {
  28. ma_code.TableName = " make left join product on ma_prodcode=pr_code";
  29. ma_code.SelectField = "ma_code # 工单编号,pr_code # 产品编号,pr_spec # 型号";
  30. ma_code.FormName = Name;
  31. ma_code.SetValueField = new string[] { "ma_code" };
  32. ma_code.Condition = "ma_statuscode='STARTED'";
  33. ma_code.DbChange += pr_code_DbChange;
  34. ma_code1.TableName = " make left join product on ma_prodcode=pr_code";
  35. ma_code1.SelectField = "ma_code # 工单编号,pr_code # 产品编号,pr_spec # 型号";
  36. ma_code1.FormName = Name;
  37. ma_code1.SetValueField = new string[] { "ma_code ma_code1" };
  38. ma_code1.Condition = "ma_statuscode='STARTED'";
  39. ma_code1.DbChange += pr_code_DbChange1;
  40. dh = SystemInf.dh;
  41. //asc.controllInitializeSize(this);
  42. }
  43. private void pr_code_DbChange(object sender, EventArgs e)
  44. {
  45. Dbfind = ma_code.ReturnData;
  46. BaseUtil.SetFormValue(this.Controls, Dbfind);
  47. DataTable dt = (DataTable)dh.ExecuteSql("select ma_qty,ma_prodcode,pr_detail from make left join product on ma_prodcode=pr_code where ma_code='" + ma_code.Text + "'", "select");
  48. if (dt.Rows.Count > 0)
  49. {
  50. pr_code.Text = dt.Rows[0]["ma_prodcode"].ToString();
  51. pr_detail.Text = dt.Rows[0]["pr_detail"].ToString();
  52. }
  53. }
  54. private void pr_code_DbChange1(object sender, EventArgs e)
  55. {
  56. Dbfind = ma_code1.ReturnData;
  57. BaseUtil.SetFormValue(this.Controls, Dbfind);
  58. DataTable dt = (DataTable)dh.ExecuteSql("select ma_qty,ma_prodcode,pr_detail from make left join product on ma_prodcode=pr_code where ma_code='" + ma_code1.Text + "'", "select");
  59. if (dt.Rows.Count > 0)
  60. {
  61. pr_code1.Text = dt.Rows[0]["ma_prodcode"].ToString();
  62. pr_detail1.Text = dt.Rows[0]["pr_detail"].ToString();
  63. }
  64. }
  65. private bool ReSN(string sncode)
  66. {
  67. DataTable dt = (DataTable)dh.ExecuteSql("select ms_sncode,ms_makecode from makeserial where ms_sncode='" + sncode + "' order by ms_id desc", "select");
  68. if (dt.Rows.Count == 0)
  69. {
  70. OperatResult.AppendText("" + sncode + "不存在\n");
  71. return false;
  72. }
  73. else
  74. {
  75. if (dt.Rows[0]["ms_makecode"].ToString() != ma_code.Text)
  76. {
  77. OperatResult.AppendText("" + sncode + "不存在工单" + ma_code.Text + "\n");
  78. return false;
  79. }
  80. sncode = dt.Rows[0]["ms_sncode"].ToString();
  81. }
  82. if (ma_code.Text == "" || ma_code1.Text == "")
  83. {
  84. OperatResult.AppendText("工单号不允许为空\n");
  85. return false;
  86. }
  87. if (!dh.CheckExist("make", "ma_code='" + ma_code.Text + "'"))
  88. {
  89. OperatResult.AppendText("工单号" + ma_code.Text + "不存在\n");
  90. return false;
  91. }
  92. if (!dh.CheckExist("make", "ma_code='" + ma_code1.Text + "'"))
  93. {
  94. OperatResult.AppendText("工单号" + ma_code1.Text + "不存在\n");
  95. return false;
  96. }
  97. if (dh.getFieldDataByCondition("make", "ma_prodcode", "ma_code='" + ma_code.Text + "'").ToString() != dh.getFieldDataByCondition("make", "ma_prodcode", "ma_code='" + ma_code1.Text + "'").ToString())
  98. {
  99. OperatResult.AppendText("工单号产品编号不对应,不允许切换\n");
  100. return false;
  101. }
  102. List<string> sqls = new List<string>();
  103. sqls.Add("update CRAFTMATERIAL set cm_makecode='" + ma_code1.Text + "' where cm_makecode= '" + ma_code.Text + "' and cm_sncode='" + sncode + "'");
  104. sqls.Add("update steppassed set sp_makecode='" + ma_code1.Text + "' where sp_makecode= '" + ma_code.Text + "' and sp_sncode='" + sncode + "'");
  105. sqls.Add("update makeserial set ms_makecode='" + ma_code1.Text + "' where ms_makecode= '" + ma_code.Text + "' and ms_sncode='" + sncode + "'");
  106. sqls.Add("update makeprocess set mp_makecode='" + ma_code1.Text + "' where mp_makecode= '" + ma_code.Text + "' and mp_sncode='" + sncode + "'");
  107. sqls.Add("update commandlog set cl_makecode='" + ma_code1.Text + "' where cl_makecode= '" + ma_code.Text + "' and cl_sncode='" + sncode + "'");
  108. sqls.Add("update makebad set mb_makecode='" + ma_code1.Text + "' where mb_makecode= '" + ma_code.Text + "' and mb_sncode='" + sncode + "'");
  109. sqls.Add("update labelprintlog set lpl_makecode='" + ma_code1.Text + "' where lpl_makecode= '" + ma_code.Text + "' and lpl_value='" + sncode + "'");
  110. sqls.Add("update make set ma_inqty=(select count(1) from makeserial where ms_makecode='" + ma_code1.Text + "') where ma_code='" + ma_code1.Text + "' ");
  111. sqls.Add("update make set ma_endqty=(select count(1) from makeserial where ms_makecode='" + ma_code1.Text + "' and ms_status=2) where ma_code='" + ma_code1.Text + "'");
  112. dh.ExecuteSQLTran(sqls.ToArray());
  113. OperatResult.AppendText("工单号切换成功\n");
  114. LogicHandler.DoCommandLog(Tag.ToString(), User.UserName, ma_code.Text, User.UserLineCode, User.UserSourceCode, "工单变更", "工单变更" + ma_code.Text + "切换到" + ma_code1.Text, sncode, "");
  115. return true;
  116. }
  117. private void Split_Click(object sender, EventArgs e)
  118. {
  119. string Delete = MessageBox.Show(this.ParentForm, "是否确认切换工单?", "提示", MessageBoxButtons.YesNo, MessageBoxIcon.Question).ToString();
  120. if (Delete == "Yes")
  121. {
  122. for (int i = 0; i < sb.Count; i++)
  123. {
  124. if (ReSN(sb[i].ToString()))
  125. OperatResult.AppendText("SN:" + sb[i].ToString() + "切换到工单" + ma_code1.Text + "完成\n", Color.Green);
  126. }
  127. WeighRecord.Clear();
  128. sb.Clear();
  129. }
  130. }
  131. private void input_KeyDown(object sender, KeyEventArgs e)
  132. {
  133. if (e.KeyCode == Keys.Enter)
  134. {
  135. if (serBtn.Checked)
  136. {
  137. dt = (DataTable)dh.ExecuteSql("select ms_sncode, ms_makecode,ms_prodcode,ms_sncode,ms_craftcode cr_code,ms_status,ms_outboxcode from makeserial where ms_sncode='" + input.Text + "' order by ms_id desc", "select");
  138. if (dt.Rows.Count > 0)
  139. {
  140. if (dt.Rows[0]["ms_status"].ToString() == "3")
  141. {
  142. OperatResult.AppendText("SN:" + input.Text + "为不良品,不允许采集\n", Color.Red, input);
  143. return;
  144. }
  145. if (dt.Rows[0]["ms_outboxcode"].ToString() != "")
  146. {
  147. OperatResult.AppendText("SN:" + input.Text + "已装箱" + dt.Rows[0]["ms_outboxcode"].ToString() + ",不允许采集\n", Color.Red, input);
  148. return;
  149. }
  150. if (sb.Contains(dt.Rows[0]["ms_sncode"].ToString()))
  151. {
  152. OperatResult.AppendText("SN:" + dt.Rows[0]["ms_sncode"].ToString() + "已采集\n", Color.Red, input);
  153. return;
  154. }
  155. WeighRecord.Items.Add(dt.Rows[0]["ms_sncode"].ToString());
  156. sb.Add(dt.Rows[0]["ms_sncode"].ToString());
  157. }
  158. else
  159. {
  160. OperatResult.AppendText("SN:" + input.Text + "不存在\n", Color.Red, input);
  161. return;
  162. }
  163. }
  164. }
  165. }
  166. private void 数据清空_Click(object sender, EventArgs e)
  167. {
  168. BaseUtil.CleanControls(this.Controls);
  169. WeighRecord.Items.Clear();
  170. sb.Clear();
  171. }
  172. private void Special_Reset_SizeChanged(object sender, EventArgs e)
  173. {
  174. asc.controlAutoSize(this);
  175. }
  176. private void Export_Click(object sender, EventArgs e)
  177. {
  178. ExportFileDialog.Description = "选择导出的路径";
  179. DialogResult result = ExportFileDialog.ShowDialog();
  180. if (result == DialogResult.OK)
  181. {
  182. ExcelHandler eh = new ExcelHandler();
  183. sql.Clear();
  184. sql.Append("select ''箱号,'' SN from dual ");
  185. dt = (DataTable)dh.ExecuteSql(sql.GetString(), "select");
  186. eh.ExportExcel(dt, ExportFileDialog.SelectedPath + "\\导出模板.xls");
  187. //string close = MessageBox.Show(this.ParentForm, "导出成功,是否打开文件", "提示", MessageBoxButtons.YesNo, MessageBoxIcon.Question).ToString();
  188. }
  189. }
  190. private void ImportExcel_Click(object sender, EventArgs e)
  191. {
  192. ImportExcel1.Filter = "(*.xls)|*.xls";
  193. DialogResult result;
  194. result = ImportExcel1.ShowDialog();
  195. if (result == DialogResult.OK)
  196. {
  197. DataTable dt = ExcelToDataTable(ImportExcel1.FileName, true);
  198. for (int i = 0; i < dt.Rows.Count; i++)
  199. {
  200. if (serBtn.Checked)
  201. {
  202. input.Text = dt.Rows[i]["SN"].ToString();
  203. input_KeyDown(sender, new KeyEventArgs(Keys.Enter));
  204. }
  205. }
  206. }
  207. }
  208. public static DataTable ExcelToDataTable(string filePath, bool isColumnName)
  209. {
  210. DataTable dataTable = null;
  211. FileStream fs = null;
  212. DataColumn column = null;
  213. DataRow dataRow = null;
  214. IWorkbook workbook = null;
  215. ISheet sheet = null;
  216. IRow row = null;
  217. ICell cell = null;
  218. int startRow = 0;
  219. try
  220. {
  221. using (fs = File.OpenRead(filePath))
  222. {
  223. // 2007版本
  224. workbook = new HSSFWorkbook(fs);
  225. if (workbook != null)
  226. {
  227. sheet = workbook.GetSheetAt(0);//读取第一个sheet,当然也可以循环读取每个sheet
  228. dataTable = new DataTable();
  229. if (sheet != null)
  230. {
  231. int rowCount = sheet.LastRowNum;//总行数
  232. if (rowCount > 0)
  233. {
  234. IRow firstRow = sheet.GetRow(0);//第一行
  235. int cellCount = firstRow.LastCellNum;//列数
  236. //构建datatable的列
  237. if (isColumnName)
  238. {
  239. startRow = 1;//如果第一行是列名,则从第二行开始读取
  240. for (int i = firstRow.FirstCellNum; i < cellCount; ++i)
  241. {
  242. cell = firstRow.GetCell(i);
  243. if (cell != null)
  244. {
  245. if (cell.StringCellValue != null)
  246. {
  247. column = new DataColumn(cell.StringCellValue);
  248. dataTable.Columns.Add(column);
  249. }
  250. }
  251. }
  252. }
  253. else
  254. {
  255. for (int i = firstRow.FirstCellNum; i < cellCount; ++i)
  256. {
  257. column = new DataColumn("column" + (i + 1));
  258. dataTable.Columns.Add(column);
  259. }
  260. }
  261. //填充行
  262. for (int i = startRow; i <= rowCount; ++i)
  263. {
  264. row = sheet.GetRow(i);
  265. if (row == null) continue;
  266. dataRow = dataTable.NewRow();
  267. for (int j = row.FirstCellNum; j < cellCount; ++j)
  268. {
  269. cell = row.GetCell(j);
  270. if (cell == null)
  271. {
  272. dataRow[j] = "";
  273. }
  274. else
  275. {
  276. //CellType(Unknown = -1,Numeric = 0,String = 1,Formula = 2,Blank = 3,Boolean = 4,Error = 5,)
  277. switch (cell.CellType)
  278. {
  279. case CellType.BLANK:
  280. dataRow[j] = "";
  281. break;
  282. case CellType.NUMERIC:
  283. short format = cell.CellStyle.DataFormat;
  284. //对时间格式(2015.12.5、2015/12/5、2015-12-5等)的处理
  285. if (format == 14 || format == 31 || format == 57 || format == 58)
  286. dataRow[j] = cell.DateCellValue;
  287. else
  288. dataRow[j] = cell.NumericCellValue;
  289. break;
  290. case CellType.STRING:
  291. dataRow[j] = cell.StringCellValue;
  292. break;
  293. case CellType.FORMULA:
  294. dataRow[j] = cell.StringCellValue;
  295. break;
  296. }
  297. }
  298. }
  299. dataTable.Rows.Add(dataRow);
  300. }
  301. }
  302. }
  303. }
  304. }
  305. return dataTable;
  306. }
  307. catch (Exception ex)
  308. {
  309. Console.WriteLine(ex.Message);
  310. if (fs != null)
  311. {
  312. fs.Close();
  313. }
  314. return null;
  315. }
  316. }
  317. }
  318. }