123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451 |
- using UAS_MES_NEW.DataOperate;
- using LabelManager2;
- using System.Data;
- using System.Text;
- using System.IO;
- using UAS_MES_NEW.Entity;
- using System.Text.RegularExpressions;
- using System.Windows.Forms;
- using Seagull.BarTender.Print;
- using System.Net;
- using System;
- using System.Linq;
- namespace UAS_MES_NEW.PublicMethod
- {
- class Print
- {
- static DataHelper dh = SystemInf.dh;
- //CodeSoft打印的驱动和文件
- static Document doc;
- ////CodeSoft的打印机
- //string CodeSpft_Printer;
- static LabelFormatDocument format;
- public Print() { }
- public static bool CodeSoft(string iCaller, Document doc, 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;
- }
- }
- //打开模板路径
- //查询模板对应的取值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 (doc == null)
- {
- MessageBox.Show("打印文件不存在");
- return false;
- }
- //执行全部的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 > 200 ? 200 : 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();
- }
- }
- }
- }
- }
- catch (System.Exception)
- {
- MessageBox.Show("SQL维护不正确");
- }
- }
- LogManager.DoLog(sb.ToString());
- //保存本次赋值进行打印
- doc.Printer.SwitchTo(PrinterName);
- doc.PrintDocument(PrintNum);
- 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;
- }
- public static bool CodeSoft(string iCaller, ref 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 + LabelName + "' 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 + LabelName + "' 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 + "' order by to_number(lp_detno)", "select");
- StringBuilder sb = new StringBuilder();
- if (!PrintFile.Exists)
- {
- MessageBox.Show("打印文件不存在");
- return false;
- }
- string filechangetime = PrintFile.LastWriteTime.ToString();
- if (filechangetime != filelastwritetime)
- {
- lbl.Quit();
- lbl = new ApplicationClass();
- BaseUtil.WriteLbl();
- filechangetime = PrintFile.LastWriteTime.ToString("yyyy-MM-dd HH:mm:ss");
- string update = "la_lastwritetime = to_date((regexp_substr('" + filechangetime + "','\\d+.+\\d+')),'yyyy-mm-dd hh24:mi:ss')";
- dh.UpdateByCondition("label", update, "la_id = '" + LaID + "'");
- }
- doc = lbl.Documents.Open(LabelName, true);
- if (doc == null)
- {
- MessageBox.Show("标签文件打开失败");
- return false;
- }
- //执行全部的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 > 200 ? 200 : 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();
- }
- }
- }
- }
- }
- catch (System.Exception)
- {
- MessageBox.Show("SQL维护不正确");
- }
- }
- LogManager.DoLog(sb.ToString());
- //保存本次赋值进行打印
- doc.Printer.SwitchTo(PrinterName);
- doc.PrintDocument(PrintNum);
- 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 + LabelName, MakeCode, ProdCode, User.UserSourceCode, User.CurrentStepCode, IfRePrint, User.UserCode);
- return true;
- }
- public static bool BarTender(string iCaller, ref Engine 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 (PrintNum == 0)
- {
- PrintNum = 1;
- }
- if (IfRePrint == "-1")
- {
- string printNumber = dh.getFieldDataByCondition("CONFIGS", "DATA", "code='printNumber' and caller='MESSetting'").ToString();
- if (dh.getRowCount("labelprintlog", "lpl_type='" + LabelType + "' and lpl_value='" + SnCode + "'") > int.Parse(printNumber))
- {
- ErrorMessage = "标签补打超出允许打印次数【" + printNumber + "】";
- return false;
- }
- }
- if (IfRePrint != "-1" && LabelType != "机身标")
- {
- bool allowPallte = true;
- if ((SystemInf.dh.getFieldDataByCondition("master", "MA_FUNCTION", "ma_user='" + SystemInf.CurrentDB + "'").ToString() == "万年MES系统(正式)"))
- {
- allowPallte = false;
- }
- if (LabelType == "卡通箱标" || LabelType == "大箱标" || (LabelType == "栈板标" && allowPallte) || LabelType == "彩盒标")
- {
- dt = (DataTable)dh.ExecuteSql("select lpl_id from labelprintlog where lpl_value='" + SnCode + "' and lpl_type='" + LabelType + "' and lpl_stepcode='" + User.CurrentStepCode + "' and lpl_file='" + LabelName + "'", "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;
- }
- }
- else
- {
- if (LabelType == "卡通箱标" || LabelType == "大箱标" || LabelType == "彩盒标")
- {
- dt = (DataTable)dh.ExecuteSql("select lpl_id from labelprintlog where lpl_value='" + SnCode + "' and lpl_type='" + LabelType + "'", "select");
- if (dt.Rows.Count == 0)
- {
- ErrorMessage = SnCode + LabelType + "未打印,不允许补打";
- return false;
- }
- }
- }
- string filelastwritetime = dh.getFieldDataByCondition("label", "la_lastwritetime", "la_id = '" + LaID + "'").ToString();
- string LA_SOFTTYPE = dh.getFieldDataByCondition("label", "LA_SOFTTYPE", "la_id = '" + LaID + "'").ToString();
- //如果有附件上传的话
- string fp_name = "";
- //下载附件
- FileInfo PrintFile = new FileInfo(@"C:\打印标签\" + LabelName);
- if (!PrintFile.Exists)
- {
- if (LA_SOFTTYPE != "")
- {
- string[] fpid = LA_SOFTTYPE.Split(';');
- for (int i = 0; i < fpid.Length; i++)
- {
- if (fpid[i] != "")
- {
- DataTable label = (DataTable)dh.ExecuteSql("select FP_PATH, FP_DATE, FP_NAME from FILEPATH where fp_id='" + fpid[i] + "'", "select");
- if (label.Rows.Count > 0)
- {
- string fp_path = label.Rows[0]["FP_PATH"].ToString().Replace("/app/uas/webapps/", "");
- fp_name = label.Rows[0]["fp_name"].ToString();
- WebClient wc = new WebClient();
- wc.DownloadFile("http://10.11.28.21:8099/" + fp_path, @"C:\打印标签\" + fp_name);
- }
- }
- }
- lbl.Stop();
- lbl = new Engine(true);
- BaseUtil.WriteLbl();
- }
- }
- else
- {
- string filechangetime = PrintFile.LastWriteTime.ToString();
- System.DateTime dateTime1 = System.DateTime.Parse(filechangetime);
- System.DateTime dateTime2 = System.DateTime.Parse(filelastwritetime);
- if (dateTime1 < dateTime2)
- {
- if (LA_SOFTTYPE != "")
- {
- string[] fpid = LA_SOFTTYPE.Split(';');
- for (int i = 0; i < fpid.Length; i++)
- {
- if (fpid[i] != "")
- {
- DataTable label = (DataTable)dh.ExecuteSql("select FP_PATH, FP_DATE, FP_NAME from FILEPATH where fp_id='" + fpid[i] + "'", "select");
- if (label.Rows.Count > 0)
- {
- string fp_path = label.Rows[0]["FP_PATH"].ToString().Replace("/app/uas/webapps/", "");
- fp_name = label.Rows[0]["fp_name"].ToString();
- WebClient wc = new WebClient();
- wc.DownloadFile("http://10.11.28.21:8099/" + fp_path, @"C:\打印标签\" + fp_name);
- FileInfo file = new FileInfo(@"C:\打印标签\" + fp_name);
- file.CreationTime = Convert.ToDateTime(filelastwritetime);
- }
- }
- }
- }
- lbl.Stop();
- lbl = new Engine(true);
- BaseUtil.WriteLbl();
- }
- }
- PrintFile = new FileInfo(@"C:\打印标签\" + LabelName);
- if (!PrintFile.Exists)
- {
- MessageBox.Show("打印文件不存在");
- return false;
- }
- //查询模板对应的取值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.Exists)
- {
- MessageBox.Show("打印文件不存在");
- return false;
- }
- format = lbl.Documents.Open(@"C:\打印标签\" + LabelName);
- if (format == null)
- {
- MessageBox.Show("标签文件打开失败");
- return false;
- }
- string ParamValue = format.SubStrings.GetAll("#", "$");
- string[] paramname = ParamValue.Split('$');
- for (int i = 0; i < paramname.Length; i++)
- {
- paramname[i] = paramname[i].Split('#')[0];
- }
- //执行全部的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 > 200 ? 200 : Param.Rows.Count;
- for (int j = 0; j < LoopTime; j++)
- {
- //if (paramname.Contains(dt.Rows[i]["lp_name"].ToString().ToUpper()))
- //{
- // if (format.SubStrings[dt.Rows[i]["lp_name"].ToString().ToUpper()] != null)
- // {
- // format.SubStrings[dt.Rows[i]["lp_name"].ToString().ToUpper()].Value = Param.Rows[0][0].ToString();
- // }
- //}
- //if (paramname.Contains(dt.Rows[i]["lp_name"].ToString().ToUpper() + (j + 1)))
- //{
- // if (format.SubStrings[dt.Rows[i]["lp_name"].ToString().ToUpper() + (j + 1)] != null)
- // {
- // format.SubStrings[dt.Rows[i]["lp_name"].ToString().ToUpper() + (j + 1)].Value = Param.Rows[j][0].ToString(); ;
- // }
- //}
- //try
- //{
- // if (format.SubStrings[dt.Rows[i]["lp_name"].ToString().ToUpper()] != null)
- // {
- // format.SubStrings[dt.Rows[i]["lp_name"].ToString().ToUpper()].Value = Param.Rows[0][0].ToString();
- // }
- // if (format.SubStrings[dt.Rows[i]["lp_name"].ToString().ToUpper() + (j + 1)] != null)
- // {
- // format.SubStrings[dt.Rows[i]["lp_name"].ToString().ToUpper() + (j + 1)].Value = Param.Rows[j][0].ToString(); ;
- // }
- //}
- //catch (Exception e)
- //{
- // Console.WriteLine( e.Message);
- //}
- for (int k = 0; k < format.SubStrings.Count; k++)
- {
- if (j == 0 & format.SubStrings[k].Name.ToUpper() == dt.Rows[i]["lp_name"].ToString().ToUpper())
- {
- Console.WriteLine(format.SubStrings[k].Name.ToUpper());
- format.SubStrings[k].Value = Param.Rows[0][0].ToString();
- break;
- }
- //使用SN开头的参数赋值SN1,SN2,SN3等参数
- if (format.SubStrings[k].Name.ToUpper() == dt.Rows[i]["lp_name"].ToString().ToUpper() + (j + 1))
- {
- Console.WriteLine(format.SubStrings[k].Name.ToUpper());
- format.SubStrings[k].Value = Param.Rows[j][0].ToString();
- break;
- }
- }
- }
- }
- }
- catch (System.Exception)
- {
- MessageBox.Show("SQL维护不正确");
- }
- }
- LogManager.DoLog(sb.ToString());
- //保存本次赋值进行打印a
- format.PrintSetup.PrinterName = PrinterName;
- format.PrintSetup.IdenticalCopiesOfLabel = PrintNum;
- format.Print();
- LogicHandler.DoCommandLog(iCaller, User.UserCode, "", User.UserLineCode, User.UserSourceCode, "打印", "成功打印", SnCode, "");
- try
- {
- for (int k = 0; k < format.SubStrings.Count; k++)
- {
- format.SubStrings[k].Value = null;
- }
- }
- catch (Exception ex)
- {
- LogManager.DoLog(ex.Message + ex.StackTrace);
- }
- LogicHandler.doLabelPrintLog(SnCode, LabelType, MakeCode, ProdCode, User.UserSourceCode, User.CurrentStepCode, IfRePrint, User.UserCode, PrintFile.Name);
- return true;
- }
- }
- }
|