Special_ChangeMake.cs 16 KB

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