using NFine.Application.WMS;
using System;
using System.Collections.Generic;
using System.Data;
using System.IO;
using System.Linq;
using System.Web;
using System.Web.Mvc;
using NFine.Code;
using System.Data.SqlClient;
using NFine.Data.Extensions;
using System.Data.OleDb;
using System.Configuration;
using ICS.Application.Entity;
using NFine.Domain._03_Entity.WMS;
using System.Data.Common;
namespace NFine.Web.Areas.WMS.Controllers
{
public class PrintController : ControllerBase
{
PrintApp App = new PrintApp();
// GET: WMS/Print
public ActionResult PrintView()
{
return View();
}
///
/// 打印数据参数:服务器的URL+打印的文件名,转化为Base64编码
///
protected string strPrintData;
///
/// 标识是否安装了控件
///
protected bool bIsInstallPrintControl = true;
///
/// 打印控件的Cookie值
///
protected string strPrintControlCookie = "";
///
/// 获取Url中去掉文件名的路径
///
///
private string GetUrlPath()
{
string strUrl = Request.Url.ToString();
int iEnd = strUrl.LastIndexOf("/");
strUrl = strUrl.Substring(0, iEnd + 1);
return strUrl;
}
[HttpGet]
[HandlerAjaxOnly]
public ActionResult GetLableType()
{
DataTable dt = App.GetLableType();
return Content(dt.ToJson());
}
[HttpGet]
[HandlerAjaxOnly]
public ActionResult GetLableSourceID()
{
DataTable dt = App.GetLableSourceID();
return Content(dt.ToJson());
}
[HttpGet]
[HandlerAjaxOnly]
public ActionResult GetSys_LablesID(string LableType)
{
DataTable dt = App.GetSys_LablesID(LableType);
return Content(dt.ToJson());
}
//打印
[HttpPost]
[HandlerAjaxOnly]
public ActionResult PrintItemLot(string keyValue, string Parameter)
{
string WorkPoint = "'" + NFine.Code.OperatorProvider.Provider.GetCurrent().Location.TrimEnd(',') + "'";
string MUSER = "'" + NFine.Code.OperatorProvider.Provider.GetCurrent().UserCode + "'";
string sqltxt = "";
Parameter = Parameter.Substring(1, Parameter.Length - 3);
var queryParam = keyValue.ToJObject();
List parameter = new List();
var Sys_LablesID = queryParam["Sys_LablesID"].ToString();
List parasList = new List();
keyValue = keyValue.Substring(1, keyValue.Length - 2);
string sqlValue = @"select LableSourceID,FileAddress from Sys_Lables where ID='{0}'";
sqlValue = string.Format(sqlValue, Sys_LablesID);
DataRow drV = SqlHelper.GetDataRowBySql(sqlValue);
string LableSourceID = drV["LableSourceID"].ToString();
string FileAddress = drV["FileAddress"].ToString();
string sqlLableDataSource = @"SELECT ID,WorkPointCode,OtherDataBaseID,WorkPointDataFlag,
SysDataFlag,SqlTxt,SourceName,FormatSqlTxt,Paras,DbParas,LinkCols,CreateUserID,
CreateDate,ModifyUserID,ModifyDate,Link2Cols FROM dbo.Sys_LableDataSource
where ID='{0}'";
sqlLableDataSource = string.Format(sqlLableDataSource, LableSourceID);
DataRow dr = SqlHelper.GetDataRowBySql(sqlLableDataSource);
//var PrintParas = Parameter.Replace(",","").Split('|').Distinct().ToArray();
//foreach (var p in PrintParas)
//{
// sqltxt = dr["SqlTxt"].ToString();
// if (dr["Paras"].ToString() != "")
// {
// string[] ps = dr["Paras"].ToString().Split(',');
// for (int i = 0; i < ps.Length; i++)
// {
// if (!string.IsNullOrEmpty(ps[i]))
// {
// sqltxt = sqltxt.Replace(ps[i], p.ToString());
// }
// }
// }
// if (sqltxt.Contains("[WorkPoint]"))
// {
// sqltxt = sqltxt.Replace("[WorkPoint]", WorkPoint);
// }
//}
var PrintParas = Parameter.Replace("|", "");
sqltxt = dr["SqlTxt"].ToString();
if (PrintParas != null)
{
string[] ps = dr["Paras"].ToString().Split(',');
for (int i = 0; i < ps.Length; i++)
{
if (!string.IsNullOrEmpty(ps[i]))
{
sqltxt = sqltxt.Replace(ps[i], PrintParas);
}
}
}
if (sqltxt.Contains("[WorkPoint]"))
{
sqltxt = sqltxt.Replace("[WorkPoint]", WorkPoint);
}
if (sqltxt.Contains("[MUSER]"))
{
sqltxt = sqltxt.Replace("[MUSER]", MUSER);
}
string strPrintFileName = Server.MapPath("/File/LablesFile/") + "" + FileAddress + "";
PrintJson pJson = new PrintJson(Server.MapPath("./PrintTemp"), strPrintFileName);
pJson.MasterOptions(1, "Code", true); //主从关系
pJson.CheckRegister("苏州智合诚信息科技有限公司", "56DD3B4C172D0D140841CAC98A58A819F4E28EDA5D6E45711DDD64F6A439F68B6A7870CD7DAFD69A919CB870207FE4BB206F92BE3D53C221B019E0797E739EBA4"); //注册信息
//pJson.CheckRegister("注册姓名", "8ECCCD6A1302DFEE1A6456A5D"); //注册信息
//pJson.AddPrintParam("ShopName", "测试酒楼");
//pJson.AddPrintParam("PrintDepositAdd", "说明:本单据为贵客押金收取凭证,盖章有效。退房时请出示,遗失者自负,请妥善保存。退房时间为12:00时,延时退房18:00时以前按半天房费收取,18:00时以后算全天房价。押金单有效期为一个月,过期作废。 贵重物品请交前台寄存,未寄存丢失自负。 谢谢!");
SetCookieAndURL(pJson);// 设置控件调用的Cookie值,判断是否安装了打印控件
string strSql = sqltxt;
//DataTable dtCashLog = DbHelper.ExecuteTable(DbHelper.ConnectionString, CommandType.Text, strSql, true, null);
DataSet dtCashLog = DbHelper.GetDataSet(CommandType.Text, strSql);
DataTable dt1 = null;
DataTable dt2 = null;
DataTable dt3 = null;
string strPrintTempFile = "";
if (dtCashLog.Tables.Count > 0 && dtCashLog.Tables.Count<=1)
{
dt1 = dtCashLog.Tables[0];
strPrintTempFile = pJson.ShowReport(dt1);
}
if (dtCashLog.Tables.Count > 1 && dtCashLog.Tables.Count <= 2)
{
dt1 = dtCashLog.Tables[0];
dt2 = dtCashLog.Tables[1];
strPrintTempFile = pJson.ShowReport(dt1, dt2);
}
if (dtCashLog.Tables.Count > 2)
{
dt1 = dtCashLog.Tables[0];
dt2 = dtCashLog.Tables[1];
dt3 = dtCashLog.Tables[2];
strPrintTempFile = pJson.ShowReport(dt1, dt2, dt3);
}
//string strPrintTempFile = pJson.ShowReport(dt1, dt2); //产生JSON文件内容
//把服务器的URL + 此文件名 传递给控件,由控件下载还原数据进行打印
string strServerURL = GetUrlPath() + "PrintTemp/";
string strData = strServerURL + strPrintTempFile;
strPrintData = PrintFunction.EnBase64(strData);
var JsonData = new
{
strPrintData_1 = strPrintData,
bIsInstallPrintControl_1 = bIsInstallPrintControl,
strPrintControlCookie_1 = strPrintControlCookie
};
//多站点
//string sql = @"UPDATE dbo.ICSInventoryLot
// SET PrintTimes=ISNULL(PrintTimes,0)+1,
// LastPrintUser='',
// LastPrintTime=GETDATE()
// WHERE ID IN (" + keyValue.TrimEnd(',') + ") and WorkPoint in ('" + WorkPoint.TrimEnd(',') + "')";
//SqlHelper.ExecuteNonQuery(sql);
return Content(JsonData.ToJson());
}
///
/// 设置控件调用的Cookie值,判断是否安装了打印控件
///
///
private void SetCookieAndURL(PrintJson pJson)
{
bIsInstallPrintControl = false;
strPrintControlCookie = "";
HttpCookie pCookieInstall = Request.Cookies["InstallPrintControl"];
if (pCookieInstall != null)
{ //Cookie存在
strPrintControlCookie = pCookieInstall.Value.ToString();
//以Cookie值查找在数据表中是否存在
string strSql = @"Select * From sys_SRM_CheckInstall Where Cookie = @Cookie";
SqlParameter[] pmcCookie = { new SqlParameter("Cookie", strPrintControlCookie) };
using (SqlDataReader drCookie = DbHelper.ExecuteReader(DbHelper.ConnectionString, CommandType.Text, strSql, pmcCookie))
{
if (drCookie.Read())
{ //标识为已经安装
bIsInstallPrintControl = true;
}
drCookie.Close();
}
//更新Cookie的保存时间
pCookieInstall.Expires = DateTime.Now.AddYears(10);
Response.SetCookie(pCookieInstall);
}
else
{//Cookie不存在,则新建Cookie
strPrintControlCookie = System.Guid.NewGuid().ToString();
pCookieInstall = new HttpCookie("InstallPrintControl", strPrintControlCookie);
pCookieInstall.Expires = DateTime.Now.AddYears(10);
Response.Cookies.Add(pCookieInstall);
}
string strUrl = Server.MapPath("/PrintTemp/") + "IsCheckInstall";
pJson.SetCookieAndURL(strPrintControlCookie, strUrl);
}
[HttpGet]
public ActionResult SelectColumnName(string BeginTime, string EndTime)
{
var data = App.SelectColumnName(BeginTime, EndTime);
return Content(data.ToJson());
}
[HttpGet]
public ActionResult GetCnValue(string Code)
{
string sql = string.Empty;
try
{
string str = App.GetCnValue(Code);
var JsonData = new
{
content = str
};
return Content(JsonData.ToJson());
}
catch (Exception ex)
{
return Error(ex.Message);
}
}
}
}