You cannot select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

325 lines
12 KiB
C#

This file contains ambiguous Unicode characters!

This file contains ambiguous Unicode characters that may be confused with others in your current locale. If your use case is intentional and legitimate, you can safely ignore this warning. Use the Escape button to highlight these characters.

using FastReport.Export.OoXML;
using FastReport.Export.Pdf;
using Myshipping.Report.Models;
using Newtonsoft.Json;
using System;
using System.Collections.Generic;
using System.Configuration;
using System.IO;
using System.Linq;
using System.Web;
using System.Web.Mvc;
using Myshipping.Report.DB;
using Myshipping.Application.Entity;
using System.Text;
using Myshipping.Core.Entity;
using System.Management;
using Microsoft.Ajax.Utilities;
namespace Myshipping.Report.Controllers
{
public class ReportController : Controller
{
private readonly NLog.Logger log = NLog.LogManager.GetCurrentClassLogger();
private SqlSugar.SqlSugarClient sqlSugarClient;
public ReportController()
{
sqlSugarClient = new SqlSugar.SqlSugarClient(new BookingConnectionConfig());
}
/// <summary>
/// 生成订舱报表结果
/// </summary>
/// <param name="bookingId">订舱ID</param>
/// <param name="type">类型1pdf、2xlsx、3docx</param>
/// <returns></returns>
[HttpPost]
public ActionResult BookingReport(long bookingId, int type = 1)
{
var resp = new RespCommonData();
if (Request.Files.Count > 0)
{
try
{
var savePath = ConfigurationManager.AppSettings["ReportFileSavePath"];
if (string.IsNullOrEmpty(savePath)) //未配置保存路径时,使用当前目录
{
savePath = Server.MapPath("~/ReportFiles");
}
if (!Directory.Exists(savePath))
{
Directory.CreateDirectory(savePath);
}
var fileExt = Path.GetExtension(Request.Files[0].FileName).ToLower();
if (fileExt != ".frx")
{
resp.Success = false;
resp.Message = $"文件类型不正确:{fileExt}";
return Json(resp);
}
var tmpFile = Path.Combine(savePath, $"{Guid.NewGuid()}{fileExt}");
Request.Files[0].SaveAs(tmpFile);
log.Debug($"临时保存模板文件:{tmpFile}");
//生成报表
FastReport.Report report = new FastReport.Report();
report.Load(tmpFile);
foreach (FastReport.Data.JsonConnection.JsonDataSourceConnection conn in report.Dictionary.Connections)
{
if (conn.Name == "booking_order")
{
conn.ConnectionString = $"Json={Convert.ToBase64String(Encoding.UTF8.GetBytes(GetBookingJson(bookingId)))};Encoding=utf-8";
}
}
report.Prepare();
if (type == 1) //pdf
{
var fileName = $"{bookingId}_{DateTime.Now.Ticks}.pdf";
var saveFile = Path.Combine(savePath, fileName);
PDFExport pdfExport = new PDFExport();
pdfExport.Export(report, saveFile);
resp.Success = true;
resp.Message = "生成成功";
resp.Data = fileName;
}
else if (type == 2) //
{
var fileName = $"{bookingId}_{DateTime.Now.Ticks}.xlsx";
var saveFile = Path.Combine(savePath, fileName);
Excel2007Export excelExport = new Excel2007Export();
excelExport.Export(report, saveFile);
resp.Success = true;
resp.Message = "生成成功";
resp.Data = fileName;
}
else if (type == 3) //
{
var fileName = $"{bookingId}_{DateTime.Now.Ticks}.docx";
var saveFile = Path.Combine(savePath, fileName);
Word2007Export excelExport = new Word2007Export();
excelExport.Export(report, saveFile);
resp.Success = true;
resp.Message = "生成成功";
resp.Data = fileName;
}
else
{
resp.Success = false;
resp.Message = "类型参数不正确";
}
return Json(resp);
}
catch (Exception ex)
{
resp.Success = false;
resp.Message = ex.Message;
log.Error(ex.Message);
log.Error(ex.StackTrace);
}
}
else
{
resp.Success = false;
resp.Message = "未上传模板文件";
}
return Json(resp);
}
/// <summary>
/// 打印报表文件
/// </summary>
/// <returns></returns>
[HttpPost]
public ActionResult PrintReport()
{
var resp = new RespCommonData();
var printType = Request.Form["printType"];
if (printType != "pdf"
&& printType != "xlsx"
&& printType != "docx")
{
resp.Success = false;
resp.Message = $"打印类型不正确:{printType}应该为pdf、xlsx、docx中的一个";
return Json(resp);
}
var dicJson = new Dictionary<string, string>();
Request.Form.AllKeys
.Where(x => x.StartsWith("dataJson"))
.ForEach(x =>
{
dicJson.Add(x.Replace("dataJson", ""), Request.Form[x]);
});
if (Request.Files.Count > 0)
{
try
{
var savePath = ConfigurationManager.AppSettings["ReportFileSavePath"];
if (string.IsNullOrEmpty(savePath)) //未配置保存路径时,使用当前目录
{
savePath = Server.MapPath("~/ReportFiles");
}
if (!Directory.Exists(savePath))
{
Directory.CreateDirectory(savePath);
}
var fileExt = Path.GetExtension(Request.Files[0].FileName).ToLower();
if (fileExt != ".frx")
{
resp.Success = false;
resp.Message = $"文件类型不正确:{fileExt}";
return Json(resp);
}
var tmpFile = Path.Combine(savePath, $"{Guid.NewGuid()}{fileExt}");
Request.Files[0].SaveAs(tmpFile);
log.Debug($"临时保存模板文件:{tmpFile}");
//生成报表
FastReport.Report report = new FastReport.Report();
report.Load(tmpFile);
foreach (FastReport.Data.JsonConnection.JsonDataSourceConnection conn in report.Dictionary.Connections)
{
if (dicJson.ContainsKey(conn.Name))
{
conn.ConnectionString = $"Json={Convert.ToBase64String(Encoding.UTF8.GetBytes(dicJson[conn.Name]))};Encoding=utf-8";
}
}
report.Prepare();
if (printType == "pdf") //pdf
{
var fileName = $"{DateTime.Now.Ticks}.pdf";
var saveFile = Path.Combine(savePath, fileName);
PDFExport pdfExport = new PDFExport();
pdfExport.Export(report, saveFile);
resp.Success = true;
resp.Message = "生成成功";
resp.Data = fileName;
}
else if (printType == "xlsx") //
{
var fileName = $"{DateTime.Now.Ticks}.xlsx";
var saveFile = Path.Combine(savePath, fileName);
Excel2007Export excelExport = new Excel2007Export();
excelExport.Export(report, saveFile);
resp.Success = true;
resp.Message = "生成成功";
resp.Data = fileName;
}
else if (printType == "docx") //
{
var fileName = $"{DateTime.Now.Ticks}.docx";
var saveFile = Path.Combine(savePath, fileName);
Word2007Export excelExport = new Word2007Export();
excelExport.Export(report, saveFile);
resp.Success = true;
resp.Message = "生成成功";
resp.Data = fileName;
}
else
{
resp.Success = false;
resp.Message = "类型参数不正确";
}
return Json(resp);
}
catch (Exception ex)
{
resp.Success = false;
resp.Message = ex.Message;
log.Error(ex.Message);
log.Error(ex.StackTrace);
}
}
else
{
resp.Success = false;
resp.Message = "未上传模板文件";
}
return Json(resp);
}
/// <summary>
/// 根据文件名获取文件
/// </summary>
/// <param name="fileName"></param>
/// <returns></returns>
[HttpGet]
public ActionResult GetFile(string fileName)
{
var basePath = ConfigurationManager.AppSettings["ReportFileSavePath"];
if (string.IsNullOrEmpty(basePath)) //未配置保存路径时,使用当前目录
{
basePath = Server.MapPath("~/ReportFiles");
}
var saveFile = Path.Combine(basePath, fileName);
if (System.IO.File.Exists(saveFile))
{
return File(saveFile, "application/octet-stream", HttpUtility.UrlEncode(fileName, Encoding.GetEncoding("UTF-8")));
}
return HttpNotFound();
}
[HttpGet]
public ActionResult BookingReportJson(long bookingId)
{
log.Debug("test");
return Content(GetBookingJson(bookingId));
}
[NonAction]
private string GetBookingJson(long bookingId)
{
log.Debug($"准备获取订舱数据:{bookingId}");
var order = sqlSugarClient.Queryable<BookingOrder>().First(x => x.Id == bookingId);
var ctns = sqlSugarClient.Queryable<BookingCtn>().Where(x => x.BILLID == bookingId).ToList();
var bookingEdi = sqlSugarClient.Queryable<BookingEDIExt>().First(x => x.BookingId == bookingId);
var letterYard = sqlSugarClient.Queryable<BookingLetteryard>().First(x => x.BookingId == bookingId);
DjyCustomer customer = null;
if (!string.IsNullOrEmpty(order.AGENTID)) //根据国外代理,获取客户的提单信息
{
customer = sqlSugarClient.Queryable<DjyCustomer>().First(x => x.CodeName == order.AGENTID && x.TenantId == order.TenantId);
}
var strObj = JsonConvert.SerializeObject(new
{
order = order,
ctns = ctns,
letterYard,
agentCustomer = customer,
ediExt = bookingEdi,
});
log.Debug($"订舱数据:{strObj}");
return strObj;
}
}
}