Browse Source

标签打印方法调整

Hcsy 8 years ago
parent
commit
45fdb83e17
2 changed files with 112 additions and 4 deletions
  1. 4 4
      UAS-MES/FunctionCode/Make/Make_RePrintLabel.cs
  2. 108 0
      UAS-MES/PublicMethod/Print.cs

+ 4 - 4
UAS-MES/FunctionCode/Make/Make_RePrintLabel.cs

@@ -221,8 +221,8 @@ namespace UAS_MES.Make
                     return;
                     return;
                 }
                 }
                 //判定通过进行打印
                 //判定通过进行打印
-                doc = lbl.Documents.Open(PrintLabel.Text);
-                if (Print.CodeSoft(Tag.ToString(), doc, PrintLabel.Text, PrintLabel.SelectedValue.ToString(), Printer.Text, inputValue.Text, int.Parse(printNum.Text),ms_makecode.Text,pr_code.Text,giftBox.Checked?"彩盒标":"机身标","-1",out errorMessage))
+                //doc = lbl.Documents.Open(PrintLabel.Text);
+                if (Print.CodeSoft(Tag.ToString(), lbl, PrintLabel.Text, PrintLabel.SelectedValue.ToString(), Printer.Text, inputValue.Text, int.Parse(printNum.Text),ms_makecode.Text,pr_code.Text,giftBox.Checked?"彩盒标":"机身标","-1",out errorMessage))
                 {
                 {
                     //按照打印张数打印
                     //按照打印张数打印
                     OperateResult.AppendText("<<打印成功\n", Color.Green);
                     OperateResult.AppendText("<<打印成功\n", Color.Green);
@@ -253,8 +253,8 @@ namespace UAS_MES.Make
                     OperateResult.AppendText("<<箱号:" + inputValue.Text + "错误,不存在\n", Color.Red);
                     OperateResult.AppendText("<<箱号:" + inputValue.Text + "错误,不存在\n", Color.Red);
                     return;
                     return;
                 }
                 }
-                doc = lbl.Documents.Open(PrintLabel.Text);
-                if (Print.CodeSoft(Tag.ToString(), doc, PrintLabel.Text, PrintLabel.SelectedValue.ToString(), Printer.Text, inputValue.Text, int.Parse(printNum.Text),ms_makecode.Text,pr_code.Text,"卡通箱标","-1",out errorMessage))
+                //doc = lbl.Documents.Open(PrintLabel.Text);
+                if (Print.CodeSoft(Tag.ToString(), lbl, PrintLabel.Text, PrintLabel.SelectedValue.ToString(), Printer.Text, inputValue.Text, int.Parse(printNum.Text),ms_makecode.Text,pr_code.Text,"卡通箱标","-1",out errorMessage))
                 {
                 {
                     //判断通过,打印
                     //判断通过,打印
                     OperateResult.AppendText("<<打印成功\n", Color.Green);
                     OperateResult.AppendText("<<打印成功\n", Color.Green);

+ 108 - 0
UAS-MES/PublicMethod/Print.cs

@@ -118,5 +118,113 @@ namespace UAS_MES.PublicMethod
             LogicHandler.doLabelPrintLog(SnCode, LabelType, MakeCode, ProdCode, User.UserSourceCode, User.CurrentStepCode, IfRePrint, User.UserCode);
             LogicHandler.doLabelPrintLog(SnCode, LabelType, MakeCode, ProdCode, User.UserSourceCode, User.CurrentStepCode, IfRePrint, User.UserCode);
             return true;
             return true;
         }
         }
+
+
+        public static bool CodeSoft(string iCaller, ApplicationClass lbl, string LabelName, string LaID, string PrinterName, string SnCode, int PrintNum, string MakeCode, string ProdCode, string LabelType, string IfRePrint, out string ErrorMessage)
+        {
+            ErrorMessage = "";
+            DataTable dt = new DataTable();
+            if (IfRePrint != "-1")
+            {
+                if (LabelType == "卡通箱标" || LabelType == "大箱标" || LabelType == "栈板标")
+                {
+                    dt = (DataTable)dh.ExecuteSql("select lpl_id from labelprintlog where lpl_value='" + SnCode + "' and lpl_type='" + LabelType + "' and lpl_stepcode='" + User.CurrentStepCode + "'", "select");
+                }
+                else
+                {
+                    dt = (DataTable)dh.ExecuteSql("select lpl_id from labelprintlog where lpl_value='" + SnCode + "' and lpl_makecode='" + MakeCode + "' and lpl_type='" + LabelType + "' and lpl_stepcode='" + User.CurrentStepCode + "'", "select");
+                }
+                ////如果已经打印过了,则不允许再打印
+                if (dt.Rows.Count > 0)
+                {
+                    ErrorMessage = SnCode + LabelType + "已打印";
+                    return false;
+                }
+            }
+            string filelastwritetime = dh.getFieldDataByCondition("label", "la_lastwritetime", "la_id = '"+ LaID + "'").ToString();
+            FileInfo PrintFile = new FileInfo(LabelName);
+            //打开模板路径
+            //查询模板对应的取值SQL和参数名称
+            dt = (DataTable)dh.ExecuteSql("select lp_name,lp_sql from label left join LABELPARAMETER on la_id=lp_laid where la_id='" + LaID + "'", "select");
+            StringBuilder sb = new StringBuilder();
+            if (PrintFile == null)
+            {
+                MessageBox.Show("打印文件不存在");
+                return false;
+            }
+            if (PrintFile.LastWriteTime.ToString() != filelastwritetime)
+            {
+                lbl = new ApplicationClass();
+                BaseUtil.WriteLbl(lbl);
+                dh.UpdateByCondition("label", "la_lastwritetime = to_date('"+PrintFile.LastWriteTime.ToString()+"', 'yyyy-mm-dd hh24:mi:ss') ", "la_id = '"+ LaID + "'");
+            }
+            doc = lbl.Documents.Open(LabelName,true);
+            //执行全部的SQL
+            for (int i = 0; i < dt.Rows.Count; i++)
+            {
+                string sql = dt.Rows[i]["lp_sql"].ToString();
+                try
+                {
+                    Regex ConnoteA = new Regex("{\\w+}");
+                    foreach (Match mch in ConnoteA.Matches(sql))
+                    {
+                        string x = mch.Value.Trim();
+                        sql = sql.Replace(x, "'" + SnCode + "'");
+                    }
+                    DataTable Param = (DataTable)dh.ExecuteSql(sql.ToString(), "select");
+                    if (Param.Rows.Count == 0)
+                        continue;
+                    //查询的结果的参数个数大于1需要给标签的多个参数赋值
+                    if (Param.Rows.Count > 0)
+                    {
+                        int LoopTime = Param.Rows.Count > 100 ? 100 : Param.Rows.Count;
+                        for (int j = 0; j < LoopTime; j++)
+                        {
+                            for (int k = 0; k < doc.Variables.FormVariables.Count; k++)
+                            {
+                                if (j == 0 & doc.Variables.FormVariables.Item(k + 1).Name.ToUpper() == dt.Rows[i]["lp_name"].ToString().ToUpper())
+                                {
+                                    doc.Variables.FormVariables.Item(k + 1).Value = Param.Rows[0][0].ToString();
+                                }
+                                //使用SN开头的参数赋值SN1,SN2,SN3等参数
+                                if (doc.Variables.FormVariables.Item(k + 1).Name.ToUpper() == dt.Rows[i]["lp_name"].ToString().ToUpper() + (j + 1))
+                                {
+                                    doc.Variables.FormVariables.Item(k + 1).Value = Param.Rows[j][0].ToString();
+                                }
+                            }
+                        }
+                    }
+                    ////否则只查询名称相等的参数赋值
+                    //else
+                    //{
+                    //    for (int j = 0; j < doc.Variables.FormVariables.Count; j++)
+                    //    {
+                    //        if (doc.Variables.FormVariables.Item(j + 1).Name == dt.Rows[i]["lp_name"].ToString())
+                    //        {
+                    //            doc.Variables.FormVariables.Item(j + 1).Value = Param.Rows[0][0].ToString();
+                    //            break;
+                    //        }
+                    //    }
+                    //}
+                }
+                catch (System.Exception ex)
+                {
+                    MessageBox.Show("SQL维护不正确");
+                }
+            }
+            LogManager.DoLog(sb.ToString());
+            //保存本次赋值进行打印
+            //doc.Save();
+            doc.Printer.SwitchTo(PrinterName);
+            doc.PrintDocument(PrintNum);
+            //doc.Close();
+            LogicHandler.DoCommandLog(iCaller, User.UserCode, "", User.UserLineCode, User.UserSourceCode, "打印", "成功打印", SnCode, "");
+            for (int k = 0; k < doc.Variables.FormVariables.Count; k++)
+            {
+                doc.Variables.FormVariables.Item(k + 1).Value = null;
+            }
+            LogicHandler.doLabelPrintLog(SnCode, LabelType, MakeCode, ProdCode, User.UserSourceCode, User.CurrentStepCode, IfRePrint, User.UserCode);
+            return true;
+        }
     }
     }
 }
 }