using System;
using System.Collections.Generic;
using System.Text;
using System.Data;
using System.Diagnostics;
using System.IO;
using System.Configuration;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using QMFrameWork.Data;
using QMAPP.FJC.Entity.FIS;
using System.Data;
using System.Collections;
using Model;
namespace DCS.Common
{
public class PrintCode
{
///
/// 获取打印标签模板
///
string strGetPrintModel = ConfigUtil.GetPrintModel();
///
/// 获取第三方打印程序
///
string strGetPrintExe = ConfigUtil.GetPrintExe();
///
/// 获取打印标签文本数据
///
string strGetDataFile = ConfigUtil.GetDataFile();
#region 生成txt文件(打印条码使用)
///
/// 生成txt文件
///
/// 状态:0 下线打印 1 补打条码
/// 总成条码
/// 零件号
public void WriteFile(string _type, string zcNum, string prodNo, DateTime time)
{
StringBuilder sb = new StringBuilder();
//工厂名称
string factoryName = "FJC Chengdu";
#region 获得零件号名称
//获得零件号名称判断字段
string zcNumTemp = zcNum.Substring(7, 1);
string prodName = "";
if (zcNumTemp == "A")
{
prodName = "NCS GP IP 黑";
}
else if (zcNumTemp == "B")
{
prodName = "NCS GP IP 灰";
}
else if (zcNumTemp == "C")
{
prodName = "NCS IP";
}
#endregion
#region 获得生产时间
string dateString = "";//生产时间
string paraId = "";//参数ID,用来标记班次
string classOneTime = "";//标记班次1的班次时间
string classTwoTime = "";//标记班次2的班次时间
string classThreeTime = "";//标记班次3的班次时间
StringBuilder sbsql = new StringBuilder();
sbsql.Append("SELECT [PARAID],[PARAVALUE] FROM T_QM_PARA WHERE FLAGDEL=0 AND PARATYPE = 7");
//DataTable dt = SqlHelper.QueryReturnDataTable(sbsql.ToString());
QMAPP.ServicesAgent.ServiceAgent agent = QMAPP.WinForm.ClientContext.GetServiceAgent();
DataTable dt = agent.InvokeServiceFunction(QMAPP.ServicesAgent.FrmPlaceManage.FrmPlaceBLL_WriteFile.ToString(), sbsql);
if (dt.Rows.Count > 0)
{
// 取得班次时间
foreach (DataRow item in dt.Rows)
{
if (item["PARAID"].ToString() == "05")
{
classOneTime = item["PARAVALUE"].ToString();
}
if (item["PARAID"].ToString() == "06")
{
classTwoTime = item["PARAVALUE"].ToString();
}
if (item["PARAID"].ToString() == "07")
{
classThreeTime = item["PARAVALUE"].ToString();
}
}
// 确定日期--------------------------------------------------------------------------
// 如果早于一班时间,算前一天的班次
string resultDate = time.ToString("yyyy-MM-dd");
string classThisTime = time.ToString("yyyy-MM-dd HH:mm:ss").Substring(11, 8);
if (Convert.ToDateTime(classThisTime) < Convert.ToDateTime(classOneTime))
{
resultDate = time.AddDays(-1).ToString("yyyy-MM-dd");
}
// 计算班次--------------------------------------------------------------------------
// 计算班次开始时间点
string resultClass = "";
DateTime testTime0 = Convert.ToDateTime(classThisTime);
DateTime testTime1 = Convert.ToDateTime(classOneTime);
DateTime testTime2 = Convert.ToDateTime(classTwoTime);
DateTime testTime3 = Convert.ToDateTime(classThreeTime);
if (testTime2 > testTime3)
{
testTime3 = Convert.ToDateTime(classThreeTime).AddDays(1);
}
if (testTime0 < testTime1)
{
testTime0 = Convert.ToDateTime(classThisTime).AddDays(1);
}
if (testTime0 >= testTime1 && testTime0 < testTime2)
{
resultClass = " A";
}
else if (testTime0 >= testTime2 && testTime0 < testTime3)
{
resultClass = " B";
}
else
{
resultClass = " C";
}
//拼写时间串
dateString = resultDate + resultClass;
}
#endregion
//生成文本头
sb.Append("\"总成条码\",");
sb.Append("\"零件名称\",");
sb.Append("\"工厂名称\",");
sb.Append("\"零件号\",");
sb.Append("\"时间\"");
//生成文本数据
sb.Append("\r\n\"" + zcNum + "\",");//加\R\N
sb.Append("\"" + prodName + "\",");//加\R\N
sb.Append("\"" + factoryName + "\",");//加\R\N
sb.Append("\"" + prodNo + "\",");//零件号
sb.Append("\"" + dateString.Replace("/",".") + "\"");
//默认数据文件位置
string labelDataFile = ConfigurationManager.AppSettings.Get("LabelDataFile"); //"d://barcode.txt";
labelDataFile = strGetDataFile;
if (String.IsNullOrEmpty(labelDataFile))
{
labelDataFile = ConfigurationManager.AppSettings.Get("LabelDataFile"); //"d://barcode.txt";
}
StreamWriter sw = new StreamWriter(labelDataFile, false, Encoding.Default);
sw.Write(sb.ToString());
sw.Flush();
sw.Close();
}
#endregion
#region 打印标签
///
/// 打印标签
///
public void PinrtHU()
{
//string logFile = "d:\\logFile.txt";
//if (System.IO.File.Exists(logFile))
//{
// System.IO.File.AppendAllText(logFile, "A |" + DateTime.Now.ToLongTimeString());
//}
//else
//{
// System.IO.File.WriteAllText(logFile, "B |" + DateTime.Now.ToLongTimeString());
//}
//string labelQdfFile = ConfigurationManager.AppSettings.Get("LabelQdfFile");//"d://barcode.qdf";
string labelQdfFile = strGetPrintModel;
if (String.IsNullOrEmpty(labelQdfFile))
{
labelQdfFile = ConfigurationManager.AppSettings.Get("LabelQdfFile");//"d://barcode.qdf";
}
//string labelPrinter = ConfigurationManager.AppSettings.Get("LabelPrinter"); //"C://Program Files//lmw32//lmwprint.exe";
string labelPrinter = strGetPrintExe;
if (String.IsNullOrEmpty(labelPrinter))
{
labelPrinter = ConfigurationManager.AppSettings.Get("LabelPrinter"); //"C://Program Files//lmw32//lmwprint.exe";
}
//System.IO.File.AppendAllText(logFile, "C |" + DateTime.Now.ToLongTimeString());
//ProcessStartInfo psi = new ProcessStartInfo();
//System.IO.File.AppendAllText(logFile, "D |" + DateTime.Now.ToLongTimeString());
//psi.FileName = labelPrinter;
//System.IO.File.AppendAllText(logFile, "E |" + DateTime.Now.ToLongTimeString());
//psi.Arguments = "\"" + labelQdfFile + "\"";
//System.IO.File.AppendAllText(logFile, "F |" + DateTime.Now.ToLongTimeString());
Process[] pro = Process.GetProcesses();
foreach (Process proces in pro)
{
if (proces.ProcessName == "Lmwprint"&&(DateTime.Now-proces.StartTime).Seconds>3)
{
proces.Kill();
}
}
Process.Start(labelPrinter, "\"" + labelQdfFile + "\"");
//System.IO.File.AppendAllText(logFile, "G |" + DateTime.Now.ToLongTimeString());
}
#endregion
#region 打印标签
///
/// 打印标签
///
public void PinrtFirstPlaceHU()
{
string labelQdfFile = ConfigUtil.GetFirstPlacePrintModel();
if (String.IsNullOrEmpty(labelQdfFile))
{
labelQdfFile = ConfigurationManager.AppSettings.Get("LabelQdfFilePlace");//"d://barcode.qdf";
}
string labelPrinter = strGetPrintExe;
if (String.IsNullOrEmpty(labelPrinter))
{
labelPrinter = ConfigurationManager.AppSettings.Get("LabelMatrix"); //"C://Program Files//lmw32//lmwprint.exe";
}
//System.IO.File.AppendAllText(logFile, "C |" + DateTime.Now.ToLongTimeString());
//ProcessStartInfo psi = new ProcessStartInfo();
//System.IO.File.AppendAllText(logFile, "D |" + DateTime.Now.ToLongTimeString());
//psi.FileName = labelPrinter;
//System.IO.File.AppendAllText(logFile, "E |" + DateTime.Now.ToLongTimeString());
//psi.Arguments = "\"" + labelQdfFile + "\"";
//System.IO.File.AppendAllText(logFile, "F |" + DateTime.Now.ToLongTimeString());
WriteFileForFirstPlace("");
Process[] pro = Process.GetProcesses();
foreach (Process proces in pro)
{
if (proces.ProcessName == "Lmwprint" && (DateTime.Now - proces.StartTime).Seconds > 3)
{
proces.Kill();
}
}
Process.Start(labelPrinter, "\"" + labelQdfFile + "\"");
//System.IO.File.AppendAllText(logFile, "G |" + DateTime.Now.ToLongTimeString());
}
#endregion
#region 生成txt文件(打印条码使用)
///
/// 生成txt文件
///
public void WriteFileForFirstPlace(string _filePath)
{
StringBuilder sb = new StringBuilder();
//生成文本头
sb.Append("编码,显示值");
//生成文本数据
sb.Append("\r\n\"" + ConfigurationManager.AppSettings.Get("PlaceFirstPositionCode") + "\",");//加\R\N
sb.Append("\"" + ConfigurationManager.AppSettings.Get("PlaceFirstPositionText") + "\",");//加\R\N
//默认数据文件位置
string labelDataFile = ConfigurationManager.AppSettings.Get("LabelDataFilePlace"); //"d://barcode.txt";
//labelDataFile = strGetDataFile;
if (String.IsNullOrEmpty(labelDataFile))
{
labelDataFile = ConfigurationManager.AppSettings.Get("LabelDataFilePlace"); //"d://barcode.txt";
}
StreamWriter sw = new StreamWriter(labelDataFile, false, Encoding.Default);
sw.Write(sb.ToString());
sw.Flush();
sw.Close();
}
#endregion
}
}