Browse Source

修改DataTable数据,耗时更少

章政 7 years ago
parent
commit
3d813f83e7

+ 9 - 9
UAS-出货标签管理(吉利通)/PublicMethod/AccessDBHelper.cs

@@ -170,12 +170,12 @@ namespace UAS_LabelMachine
             command.Dispose();
         }
 
-        public void DeleleFile(string accessFile )
+        public void DeleleFile(string accessFile)
         {
             var dbe = new Microsoft.Office.Interop.Access.Dao.DBEngine();
             try
             {
-                string tempFile = Path.Combine(Path.GetDirectoryName(accessFile),Path.GetRandomFileName() + Path.GetExtension(accessFile));
+                string tempFile = Path.Combine(Path.GetDirectoryName(accessFile), Path.GetRandomFileName() + Path.GetExtension(accessFile));
                 dbe.CompactDatabase(accessFile, tempFile);
                 FileInfo temp = new FileInfo(tempFile);
                 temp.CopyTo(accessFile, true);
@@ -211,7 +211,7 @@ namespace UAS_LabelMachine
             //    }
             //}
             sb.Clear();
-            sb.Append("insert into "+ TableName+" (");
+            sb.Append("insert into " + TableName + " (");
             string field = "";
             string valuefield = "";
             for (int i = 0; i < dt.Columns.Count; i++)
@@ -803,24 +803,24 @@ namespace UAS_LabelMachine
         /// <param name="TableName"></param>
         /// <param name="update"></param>
         /// <param name="condition"></param>
-        public string UpdateByCondition(string TableName, string update, string condition)
+        public int UpdateByCondition(string TableName, string update, string condition)
         {
             string sql = "update " + TableName + " set " + update + " where " + condition;
             command = new OleDbCommand(sql, this._odcConnection);
-
+            int Rowcount = 0;
             Reconnect(command);
             try
             {
-                command.ExecuteNonQuery();
+                Rowcount = command.ExecuteNonQuery();
             }
             catch (Exception)
             {
                 command.Connection = new OleDbConnection(this._connectionString);
                 command.Connection.Open();
-                command.ExecuteNonQuery();
+                Rowcount = command.ExecuteNonQuery();
             }
             command.Dispose();
-            return sql;
+            return Rowcount;
         }
 
         /// <summary>
@@ -1007,7 +1007,7 @@ namespace UAS_LabelMachine
             {
                 if (!dt2.Columns.Contains(item.ToString()))
                 {
-                    ExecuteSql("alter table "+ tablename + " add [" + item.ToString() + "] varchar(200) NULL", "update");
+                    ExecuteSql("alter table " + tablename + " add [" + item.ToString() + "] varchar(200) NULL", "update");
                 }
             }
         }

+ 21 - 4
UAS-出货标签管理(吉利通)/PublicMethod/LogicHandler.cs

@@ -1,4 +1,5 @@
-using System.Data;
+using System;
+using System.Data;
 using UAS_LabelMachine.Entity;
 
 namespace UAS_LabelMachine.PublicMethod
@@ -36,21 +37,37 @@ namespace UAS_LabelMachine.PublicMethod
             adh.UpdateByCondition("prodiobarcode", "pib_ifpick=-1,pib_modify=-1", "pib_id=" + iPibID);
         }
 
+        /// <summary>
+        /// 获取十六进制字节数组
+        /// </summary>
+        /// <param name="Data"></param>
+        /// <returns></returns>
+        public static byte[] SendPLCData(string Data)
+        {
+            byte[] arr = new byte[1];
+            arr[0] = (byte)Convert.ToInt32(Data, 16);
+            return arr;
+        }
+
         /// <summary>
         /// 检测数据库未上传的数据,根据设置的上传条数上传数据
         /// </summary>
         /// <param name="iInoutno"></param>
-        public static void CheckUploadData(string iInoutno, int iUploadNum)
+        public static bool CheckUploadData(string iInoutno, int iUploadNum)
         {
-            DataTable dt = (DataTable)adh.ExecuteSql("select pib_id,pib_lotno,pib_ifmodify,pib_datecode,pib_ifpick,-1 as pib_ifupload,pib_ifprint,pib_ifrecheck from prodiobarcode where pib_inoutno='" + iInoutno + "'  and pib_ifupload<>-1", "select");
+            DataTable dt = (DataTable)adh.ExecuteSql("select pib_id,pib_lotno,pib_ifmodify,pib_datecode,pib_ifpick,-1 as pib_ifupload,pib_ifprint,pib_ifrecheck from prodiobarcode where pib_inoutno='" + iInoutno + "'  and pib_ifupload=0 and pib_ifrecheck=-1", "select");
             //如果本地需要上传的数据已经达到了需要上传的条数
             if (iUploadNum <= dt.Rows.Count)
             {
                 //上传数据
                 dh.SaveDataTable(dt, "prodiobarcode", "pib_id");
                 //更新本地数据为已上传
-                adh.UpdateByCondition("prodiobarcode", "pib_ifupload=-1", "pib_inoutno='" + iInoutno + "' and pib_ifmodify=-1 and pib_ifupload<>-1");
+                if (adh.UpdateByCondition("prodiobarcode", "pib_ifupload=-1", "pib_inoutno='" + iInoutno + "' and pib_ifrecheck=-1 and pib_ifupload<>-1") > 0)
+                {
+                    return true;
+                }
             }
+            return false;
         }
     }
 }

+ 6 - 4
UAS-出货标签管理(吉利通)/UAS_出货标签管理.Designer.cs

@@ -70,7 +70,6 @@
             this.ListButtonMenu = new System.Windows.Forms.ListBox();
             this.pi_date = new System.Windows.Forms.Label();
             this.ExportFileDialog = new System.Windows.Forms.FolderBrowserDialog();
-            this.MidSource = new System.Windows.Forms.BindingSource(this.components);
             this.GetGridOnly = new System.Windows.Forms.CheckBox();
             this.RefreshDBConnect = new System.Windows.Forms.Timer(this.components);
             this.label3 = new System.Windows.Forms.Label();
@@ -220,7 +219,7 @@
             this.OutBoxCombox = new System.Windows.Forms.ComboBox();
             this.BackendCheck = new UAS_LabelMachine.CustomControl.SerialPortWithTag();
             this.FrontendCheck = new UAS_LabelMachine.CustomControl.SerialPortWithTag();
-            ((System.ComponentModel.ISupportInitialize)(this.MidSource)).BeginInit();
+            this.PLC1 = new UAS_LabelMachine.CustomControl.SerialPortWithTag();
             this.CollectionProcess.SuspendLayout();
             this.groupBox2.SuspendLayout();
             this.groupBox1.SuspendLayout();
@@ -2270,6 +2269,10 @@
             // 
             this.FrontendCheck.Tag = "FrontendCheck";
             // 
+            // PLC1
+            // 
+            this.PLC1.Tag = "PLC1";
+            // 
             // UAS_出货标签打印
             // 
             this.AutoScaleDimensions = new System.Drawing.SizeF(6F, 12F);
@@ -2319,7 +2322,6 @@
             this.FormClosing += new System.Windows.Forms.FormClosingEventHandler(this.贴标机条码打印_FormClosing);
             this.Load += new System.EventHandler(this.贴标机条码打印_Load);
             this.SizeChanged += new System.EventHandler(this.贴标机条码打印_SizeChanged);
-            ((System.ComponentModel.ISupportInitialize)(this.MidSource)).EndInit();
             this.CollectionProcess.ResumeLayout(false);
             this.CollectionProcess.PerformLayout();
             this.groupBox2.ResumeLayout(false);
@@ -2459,7 +2461,6 @@
         private System.Windows.Forms.ListBox ListButtonMenu;
         private System.Windows.Forms.Label pi_date;
         private System.Windows.Forms.FolderBrowserDialog ExportFileDialog;
-        private System.Windows.Forms.BindingSource MidSource;
         private System.Windows.Forms.CheckBox GetGridOnly;
         private System.Windows.Forms.Timer RefreshDBConnect;
         private System.Windows.Forms.Label label3;
@@ -2544,5 +2545,6 @@
         private System.Windows.Forms.DataGridViewTextBoxColumn pr_spec;
         private System.Windows.Forms.DataGridViewTextBoxColumn pib_outboxcode1;
         private System.Windows.Forms.DataGridViewTextBoxColumn pib_outboxcode2;
+        private CustomControl.SerialPortWithTag PLC1;
     }
 }

+ 56 - 35
UAS-出货标签管理(吉利通)/UAS_出货标签管理.cs

@@ -273,11 +273,11 @@ namespace UAS_LabelMachine
                                 {
                                     DateCode = matchs[index].Value;
                                 }
-                                LabelInf.Rows[CurrentRowIndex].Cells["pib_datecode"].Value = DateCode;
+                                LabelInfDataTable.Rows[CurrentRowIndex]["pib_datecode"] = DateCode;
                                 break;
                             case "LotNo":
                                 LotNo = matchs[index].Value;
-                                LabelInf.Rows[CurrentRowIndex].Cells["pib_lotno"].Value = LotNo;
+                                LabelInfDataTable.Rows[CurrentRowIndex]["pib_lotno"] = LotNo;
                                 break;
                             case "PN":
                                 PN = matchs[index].Value;
@@ -307,12 +307,14 @@ namespace UAS_LabelMachine
                     LabelInf.Refresh();
                 }
             }
-            LabelInf.Rows[CurrentRowIndex].Cells["pib_ifpick"].Value = true;
+            LabelInfDataTable.Rows[CurrentRowIndex]["pib_ifpick"] = true;
             LabelInf.Refresh();
         }
 
         private void BackendCheckFunction(string msg, string pib_id)
         {
+            Console.WriteLine(System.DateTime.Now.ToString("yyyy-MM-dd hh:mm:ss fff"));
+            DataTable dtt = LabelInf.DataSource as DataTable;
             string[] msgArr = msg.Split('@');
             //需要校验2项数据,完成校验后此项的值需要为2则表示校验成功
             int checkItemCount = 0;
@@ -359,7 +361,7 @@ namespace UAS_LabelMachine
                 MessageBox.Show("复核不通过");
                 return;
             }
-            LabelInf.Rows[CurrentRowIndex].Cells["pib_ifrecheck"].Value = true;
+            LabelInfDataTable.Rows[CurrentRowIndex]["pib_ifrecheck"] = true;
             adh.ExecuteSql("update prodiobarcode set pib_ifrecheck=-1 where pib_id=" + pib_id, "update");
             LabelInf.Refresh();
             //获取当前最大的箱号
@@ -398,14 +400,26 @@ namespace UAS_LabelMachine
                         outboxcode1 = outboxcode1 + 1;
                 }
             }
-            LabelInf.Rows[CurrentRowIndex].Cells["pib_outboxcode1"].Value = outboxcode1;
-            LabelInf.Rows[CurrentRowIndex].Cells["pib_outboxcode2"].Value = outboxcode2;
+            LabelInfDataTable.Rows[CurrentRowIndex]["pib_outboxcode1"] = outboxcode1;
+            LabelInfDataTable.Rows[CurrentRowIndex]["pib_outboxcode2"] = outboxcode2;
             adh.ExecuteSql("update prodiobarcode set pib_outboxcode2=" + outboxcode2 + ",pib_outboxcode1=" + outboxcode1 + ",pib_lotno='" + LotNo + "',pib_datecode='" + DateCode + "',pib_ifpick=-1 where pib_id=" + pib_id, "update");
             LabelInf.Refresh();
             if (CurrentRowIndex + 1 < LabelInf.Rows.Count)
                 CurrentRowIndex = CurrentRowIndex + 1;
             RefreshProcessData();
-            LogicHandler.CheckUploadData(pi_inoutno.Text, UploadNum);
+            if (CurrentRowIndex - 10 > 0)
+                LabelInf.FirstDisplayedScrollingRowIndex = CurrentRowIndex - 10;
+            //如果成功上传了数据
+            if (LogicHandler.CheckUploadData(pi_inoutno.Text, UploadNum))
+            {
+                for (int i = 0; i < dtt.Rows.Count; i++)
+                {
+                    if (dtt.Rows[i]["pib_ifrecheck"].ToString() != "0" && dtt.Rows[i]["pib_ifupload"].ToString() == "0")
+                    {
+                        dtt.Rows[i]["pib_ifupload"] = -1;
+                    }
+                }
+            }
         }
 
         //只执行一次窗体自适应
@@ -468,10 +482,9 @@ namespace UAS_LabelMachine
             if (CollectVeProdCodePass && CollectQTYPass)
             {
                 //如果按盒号采集,所有的此盒的均更新为
-                DataTable dt = (DataTable)MidSource.DataSource;
-                LabelInf.Rows[CurrentRowIndex].Cells["pib_ifpick"].Value = true;
+                LabelInfDataTable.Rows[CurrentRowIndex]["pib_ifpick"] = true;
                 LogicHandler.UpdateRowPicked(LabelInf.Rows[CurrentRowIndex].Cells["pib_id"].Value);
-                LabelInf.Rows[CurrentRowIndex].Cells["Choose"].Value = true;
+                LabelInfDataTable.Rows[CurrentRowIndex]["Choose"] = true;
                 //勾选了单盘自动打印
                 if (SingleLabelAutoPrint.Checked)
                 {
@@ -526,7 +539,7 @@ namespace UAS_LabelMachine
             SingleDoc.Printer.SwitchTo(SingleLabelPrinter.Text);
             SingleDoc.PrintDocument();
             LogicHandler.UpdateRowPrinted(pib_id);
-            LabelInf.Rows[CurrentRowIndex].Cells["pib_ifprint"].Value = true;
+            LabelInfDataTable.Rows[CurrentRowIndex]["pib_ifprint"] = true;
         }
 
         //关闭窗口前提示用户确认
@@ -850,7 +863,7 @@ namespace UAS_LabelMachine
                                 }
                             }
                             //勾选为已打印
-                            LabelInf.Rows[i].Cells["pib_ifprint"].Value = true;
+                            LabelInfDataTable.Rows[i]["pib_ifprint"] = true;
                         }
                         catch (Exception ex) { LogManager.DoLog(ex.Message); }
                     }
@@ -883,9 +896,9 @@ namespace UAS_LabelMachine
                         {
                             for (int i = begin - 1; i < end; i++)
                             {
-                                LabelInf.Rows[i].Cells["Choose"].Value = true;
-                                LabelInf.Rows[i].Cells["pib_ifpick"].Value = true;
-                                LabelInf.Rows[i].Cells["pib_ifprint"].Value = false;
+                                LabelInfDataTable.Rows[i]["Choose"] = true;
+                                LabelInfDataTable.Rows[i]["pib_ifpick"] = true;
+                                LabelInfDataTable.Rows[i]["pib_ifprint"] = false;
                             }
                         }
                         else
@@ -1178,7 +1191,7 @@ namespace UAS_LabelMachine
             if (LabelInf.Rows.Count > 0)
             {
                 //设置初始化的采集进度
-                int Count = 0;
+                int Count = Count = adh.getRowCount("prodiobarcode", "pib_ifrecheck=-1");
                 string midboxcode = "";
                 string outboxcode = "";
                 if (CurrentRowIndex - 1 >= 0)
@@ -1187,11 +1200,6 @@ namespace UAS_LabelMachine
                     midboxcode = LabelInf.Rows[CurrentRowIndex - 1].Cells["pib_outboxcode1"].Value.ToString();
                 }
                 //未超出当前范围的时候
-                for (int i = 0; i < LabelInf.RowCount; i++)
-                {
-                    if (LabelInf.Rows[i].Cells["pib_ifrecheck"].FormattedValue.ToString() == "True")
-                        Count++;
-                }
                 int midboxcount = adh.getRowCount("prodiobarcode", "pib_inoutno='" + pi_inoutno.Text + "' and pib_outboxcode1=" + (midboxcode == "" ? "0" : midboxcode));
                 int outboxcount = adh.getRowCount("prodiobarcode", "pib_inoutno='" + pi_inoutno.Text + "' and pib_outboxcode2=" + (outboxcode == "" ? "0" : outboxcode));
                 midboxProcess.Text = midboxcount + "/" + MidBoxCapacity.Value;
@@ -1200,7 +1208,7 @@ namespace UAS_LabelMachine
                 Process_midboxcode.Text = midboxcode;
                 Process_outboxcode.Text = outboxcode;
                 //设置当前总数和已采集数量
-                CollectedCount.Text = Count.ToString() + "/" + LabelInf.Rows.Count;
+                CollectedCount.Text = Count + "/" + PageNum.Text;
             }
         }
 
@@ -1212,6 +1220,7 @@ namespace UAS_LabelMachine
         int PageSize = 1000;
         int DataSize = 0;
         int MaxPageSize = 0;
+        DataTable LabelInfDataTable;
         /// <summary>
         /// 自定义函数  加载明细行的数据,多处使用添加进函数
         /// </summary>
@@ -1227,8 +1236,8 @@ namespace UAS_LabelMachine
                 sql.Append(" and pib_pdno between " + DetailRange1.Value + " and " + DetailRange2.Value);
             }
             sql.Append(" order by rownum");
-            dt = (DataTable)adh.ExecuteSql(sql.ToString(), "select");
-            if (dt.Rows.Count == 0)
+            LabelInfDataTable = (DataTable)adh.ExecuteSql(sql.ToString(), "select");
+            if (LabelInfDataTable.Rows.Count == 0)
             {
                 //嵌套查询重置RowNum
                 sql.Clear();
@@ -1236,18 +1245,18 @@ namespace UAS_LabelMachine
                 sql.Append("pib_id,pib_pdid,pib_piid,pib_pdno,pib_prodcode,nvl(nvl(pd_brand,pib_brand),pr_brand)pib_brand, pr_vendprodcode,pib_lotno,pib_datecode,pib_qty,pr_spec,pi_title,pr_zxbzs,pr_unit,pib_barcode,pib_outboxcode1,");
                 sql.Append("pib_outboxcode2,nvl(pib_ifpick,0)pib_ifpick,nvl(pib_ifprint,0)pib_ifprint from prodiobarcode left join prodinout on pib_piid=pi_id left join prodiodetail on pib_piid=pd_piid and pd_pdno=pib_pdno and ");
                 sql.Append("pd_prodcode=pib_prodcode left join product on pr_code=pib_prodcode left join sale on sa_code=pib_ordercode where pib_piid='" + PI_ID + "' order by to_number(pib_id))t");
-                dt = (DataTable)dh.ExecuteSql(sql.ToString(), "select");
+                LabelInfDataTable = (DataTable)dh.ExecuteSql(sql.ToString(), "select");
                 //检验查询的字段和本地数据库字段是否匹配
-                adh.AddColumFromDataTable(dt, "prodiobarcode");
+                adh.AddColumFromDataTable(LabelInfDataTable, "prodiobarcode");
                 //将数据保存在本地数据库
-                adh.SaveDataTable(dt, "prodiobarcode");
+                adh.SaveDataTable(LabelInfDataTable, "prodiobarcode");
                 if (!DetailRange.Checked)
                 {
-                    dt = BaseUtil.filterDataTable(dt, "rownum <=" + PageSize);
+                    LabelInfDataTable = BaseUtil.filterDataTable(LabelInfDataTable, "rownum <=" + PageSize);
                 }
                 else
                 {
-                    dt = BaseUtil.filterDataTable(dt, "rownum <=" + PageSize + " and pib_pdno >= " + DetailRange1.Value + " and pib_pdno<=" + DetailRange2.Value);
+                    LabelInfDataTable = BaseUtil.filterDataTable(LabelInfDataTable, "rownum <=" + PageSize + " and pib_pdno >= " + DetailRange1.Value + " and pib_pdno<=" + DetailRange2.Value);
                 }
             }
             //获取数据总条数
@@ -1262,8 +1271,7 @@ namespace UAS_LabelMachine
             PageNum.Text = "总数" + DataSize;
             MaxPageSize = DataSize % PageSize == 0 ? DataSize / PageSize : (DataSize / PageSize) + 1;
             PageInf.Text = CurrentPage + "/" + MaxPageSize;
-            MidSource.DataSource = dt;
-            BaseUtil.FillDgvWithDataTable(LabelInf, (DataTable)MidSource.DataSource);
+            BaseUtil.FillDgvWithDataTable(LabelInf, LabelInfDataTable);
             //获取系统设置的上传数量,如参数异常则默认为100
             dt = (DataTable)adh.ExecuteSql("select uploadnum,splitback,splitfront from dataextra", "select");
             if (dt.Rows.Count > 0)
@@ -1483,7 +1491,7 @@ namespace UAS_LabelMachine
             if (result == DialogResult.OK)
             {
                 ExcelHandler eh = new ExcelHandler();
-                DataTable dt = ((DataTable)MidSource.DataSource).Copy();
+                DataTable dt = LabelInfDataTable;
                 for (int i = dt.Columns.Count - 1; i >= 0; i--)
                 {
                     for (int j = 0; j < LabelInf.Columns.Count; j++)
@@ -1642,7 +1650,16 @@ namespace UAS_LabelMachine
 
         private void PLCStart_Click(object sender, EventArgs e)
         {
-
+            DataTable dt = (DataTable)adh.ExecuteSql("select * from cominfo", "select");
+            DataRow[] dr = dt.Select("comtype='PLC1'");
+            if (dr.Length > 0)
+            {
+                PLC1.PortName = dr[0]["COM"].ToString();
+                PLC1.Tag = "FrontendCheck";
+                PLC1.BaudRate = int.Parse(dr[0]["BaudRate"].ToString());
+                PLC1.DataReceived += Serial_DataReceived;
+                PLC1.Open();
+            }
         }
 
         private void PLCStop_Click(object sender, EventArgs e)
@@ -1652,12 +1669,16 @@ namespace UAS_LabelMachine
 
         private void ButtonCollectPass_Click(object sender, EventArgs e)
         {
-
+            string pib_id = LabelInf.Rows[CurrentRowIndex].Cells["pib_id1"].Value.ToString();
+            int pib_pdno = int.Parse(LabelInf.Rows[CurrentRowIndex].Cells["pib_pdno"].Value.ToString());
+            FrontCheckFunction("");
         }
 
         private void ButtonRecheckPass_Click(object sender, EventArgs e)
         {
-
+            string pib_id = LabelInf.Rows[CurrentRowIndex].Cells["pib_id1"].Value.ToString();
+            int pib_pdno = int.Parse(LabelInf.Rows[CurrentRowIndex].Cells["pib_pdno"].Value.ToString());
+            BackendCheckFunction("", pib_id);
         }
 
         private void ButtonSetMidBox_Click(object sender, EventArgs e)

+ 3 - 3
UAS-出货标签管理(吉利通)/UAS_出货标签管理.resx

@@ -120,9 +120,6 @@
   <metadata name="ExportFileDialog.TrayLocation" type="System.Drawing.Point, System.Drawing, Version=4.0.0.0, Culture=neutral, PublicKeyToken=b03f5f7f11d50a3a">
     <value>294, 17</value>
   </metadata>
-  <metadata name="MidSource.TrayLocation" type="System.Drawing.Point, System.Drawing, Version=4.0.0.0, Culture=neutral, PublicKeyToken=b03f5f7f11d50a3a">
-    <value>179, 17</value>
-  </metadata>
   <metadata name="RefreshDBConnect.TrayLocation" type="System.Drawing.Point, System.Drawing, Version=4.0.0.0, Culture=neutral, PublicKeyToken=b03f5f7f11d50a3a">
     <value>17, 17</value>
   </metadata>
@@ -168,6 +165,9 @@
   <metadata name="FrontendCheck.TrayLocation" type="System.Drawing.Point, System.Drawing, Version=4.0.0.0, Culture=neutral, PublicKeyToken=b03f5f7f11d50a3a">
     <value>442, 17</value>
   </metadata>
+  <metadata name="PLC1.TrayLocation" type="System.Drawing.Point, System.Drawing, Version=4.0.0.0, Culture=neutral, PublicKeyToken=b03f5f7f11d50a3a">
+    <value>721, 18</value>
+  </metadata>
   <assembly alias="System.Drawing" name="System.Drawing, Version=4.0.0.0, Culture=neutral, PublicKeyToken=b03f5f7f11d50a3a" />
   <data name="$this.Icon" type="System.Drawing.Icon, System.Drawing" mimetype="application/x-microsoft.net.object.bytearray.base64">
     <value>