using FastReport;
using FastReport.Export.Pdf;
using Newtonsoft.Json.Converters;
using System;
using System.Collections.Generic;
using System.Data;
using System.IO;
using System.Linq;
using System.Text;
using System.Web;
namespace VilnkLib
{
public class PrintHelp
{
public static string reportPath = AppDomain.CurrentDomain.BaseDirectory;
public static string getPrint(DataSet dataSet, string TN, DateTime? effective = null)
{
JsonParser Jsp = new JsonParser();
Jsp.data = dataSet;
Jsp.TN = TN;
Jsp.pv = "v";
Jsp.report = "";
Jsp.PN = "1".ToString();
string sql = "";
if (!effective.HasValue)
{
effective = DateTime.Parse("2019-09-02");
}
if (effective < DateTime.Parse("2019-10-01"))
{
effective = DateTime.Parse("2019-10-01");
}
if (true)
{
sql = "SELECT TOP 1 FileURL FROM dbo.VL_Sys_ReportFile WHERE FileName='" + TN + "' AND effective<='" + effective + "' ORDER BY effective desc";
}
else
{
sql = "SELECT TOP 1 FileURL FROM dbo.VL_Sys_ReportFile WHERE FileName='" + TN + "' ORDER BY effective desc";
}
string fileName = "/PrintFile/" + TN;
string path = PrintHelp.reportPath + "/PrintFile/" + TN + ".frl";
DataTable dts = DBCtr.SqlTable(sql);
if (dts.Rows.Count > 0)
{
path = PrintHelp.reportPath + dts.Rows[0][0].ToString();
fileName = dts.Rows[0][0].ToString().Replace("/", "_").Replace("\\", "_");
}
else
{
if (File.Exists(path))
{
DBCtr.ExeScalar("INSERT INTO [VL_Sys_ReportFile]([FileName],[Version],[effective],[FileURL],[AddUser],[AddTime])VALUES('" + TN + "','1.0','2019-10-01','/PrintFile/" + TN + ".frl','sys',getdate())");
}
}
Jsp.report = FileToBase64String(path);
var iso = new IsoDateTimeConverter();
iso.DateTimeFormat = "yyyy年MM月dd日 HH:mm:ss";
return Newtonsoft.Json.JsonConvert.SerializeObject(Jsp, iso).Replace(" 00:00:00", "");
}
public static string FileToBase64String(string Path)
{
FileStream fsForRead = new FileStream(Path, FileMode.Open);
string base64Str = "";
try
{
fsForRead.Seek(0, SeekOrigin.Begin);
byte[] bs = new byte[fsForRead.Length];
int log = Convert.ToInt32(fsForRead.Length);
fsForRead.Read(bs, 0, log);
base64Str = Convert.ToBase64String(bs);
return base64Str;
}
catch (Exception ex)
{
return ex.Message;
}
finally
{
fsForRead.Close();
}
}
public static string getExpPDFQY(DataSet dataSet, string TN) {
string sql = "";
sql = "SELECT TOP 1 FileURL FROM dbo.VL_Sys_ReportFile WHERE FileName='" + TN + "' ORDER BY effective desc";
string path = PrintHelp.reportPath + "/PrintFile/" + TN + ".frl";
DataTable dts = DBCtr.SqlTable(sql);
if (dts.Rows.Count > 0)
{
path = dts.Rows[0][0].ToString();
}
else
{
if (File.Exists(path))
{
DBCtr.ExeScalar("INSERT INTO [VL_Sys_ReportFile]([FileName],[Version],[effective],[FileURL],[AddUser],[AddTime])VALUES('" + TN + "','1.0','2015-12-01','/PrintFile/" + TN + ".frl','sys',getdate())");
}
}
if (File.Exists(PrintHelp.reportPath + path))
{
path = PrintHelp.reportPath + path;
}
else
{
path = System.Configuration.ConfigurationSettings.AppSettings["FJURL"].ToString() + "/" + path; ;
}
Report report = new Report();
report.Load(path);
report.RegisterData(dataSet);
report.Prepare();
PDFExport export = new PDFExport();
string str = "/ExpReports/" + TN + DateTime.Now.ToString("yyMMddHHmmss") + ".pdf";
report.Export(export, reportPath + str);
report.Dispose();
return str;
}
public static string getExpPDF(DataSet dataSet, string TN, DateTime? effective)
{
try
{
string strss=HttpContext.Current.Request.QueryString["pattern"];
if (strss == "design") {
return getPrint(dataSet, TN,effective);
}
string sql = "";
if (!effective.HasValue) {
effective = DateTime.Parse("2019-09-02");
}
if (effective < DateTime.Parse("2019-10-01"))
{
effective = DateTime.Parse("2019-10-01");
}
if (true)
{
sql = "SELECT TOP 1 FileURL FROM dbo.VL_Sys_ReportFile WHERE FileName='" + TN + "' AND effective<='" + effective + "' ORDER BY effective desc";
}
else
{
sql = "SELECT TOP 1 FileURL FROM dbo.VL_Sys_ReportFile WHERE FileName='" + TN + "' ORDER BY effective desc";
}
string fileName = "/PrintFile/"+TN;
string path = PrintHelp.reportPath + "/PrintFile/" + TN + ".frl";
DataTable dts = DBCtr.SqlTable(sql);
if (dts.Rows.Count > 0)
{
path = PrintHelp.reportPath + dts.Rows[0][0].ToString();
fileName = dts.Rows[0][0].ToString().Replace("/","_").Replace("\\", "_");
}
else
{
if (File.Exists(path))
{
DBCtr.ExeScalar("INSERT INTO [VL_Sys_ReportFile]([FileName],[Version],[effective],[FileURL],[AddUser],[AddTime])VALUES('" + TN + "','1.0','2019-10-01','/PrintFile/" + TN + ".frl','sys',getdate())");
}
}
string guid = Guid.NewGuid().ToString("N");
Report report = new Report();
report.Load(path);
report.RegisterData(dataSet);
report.Prepare();
fileName = fileName.Replace("/", "_").Replace("\\", "_");
PDFExport export = new PDFExport();
string str = "/ExpReports/" + fileName.Replace(".","_").Replace("@", "_").Replace(",", "_") + DateTime.Now.ToString("yyMMddHHmmss") + ".pdf";
report.Export(export, reportPath + str);
report.Dispose();
return str;
}
catch (Exception ee)
{
return ee.Message;
}
}
}
[Serializable]
public class JsonParser
{
public string report { get; set; }
public string pv { get; set; }
public string PN { get; set; }
public string TN { get; set; }
public DataSet data { get; set; }
}
}