using System;
using System.Collections.Generic;
using System.Data;
using System.IO;
using System.Linq;
using WMS.Core;
using WMS.Util;
using WMS.Info;
using System.Web;
using System.Text;
using WMS.Core.ImportExecl;
using Microsoft.AspNetCore.Mvc;
using WMS.BZWeb;
using System.Net;
using WMS.BZModels;
using WMS.BZServices.HJ;
using WMS.BZUtil;
using WMS.BZServices;
//using FastReport.Web;
//using FastReport;
namespace WMS.BZWeb.Controllers
{
///
/// 描 述:通用控制器,处理通用的接口
///
//[HandlerLogin(EFilterMode.Ignore)]
public class UtilityController : MvcControllerBase
{
private readonly WarehouseService _warehouseService;
private readonly AnnexesFileService _annexesFileService;
private readonly ImportDataOrExcelService _importDataOrExeclService;
public UtilityController(WarehouseService warehouse, AnnexesFileService annexesFileService, ImportDataOrExcelService importDataOrExeclService)
{
_warehouseService = warehouse;
_annexesFileService = annexesFileService;
_importDataOrExeclService = importDataOrExeclService;
}
AnnexesFileCore AnnexesFile = new AnnexesFileCore();
///
/// 图标的选择
///
///
[HttpGet]
///[HandlerLogin(EFilterMode.Enforce)]
public ActionResult Icon()
{
return View();
}
///
/// 移动图标的选择
///
///
[HttpGet]
//[HandlerLogin(EFilterMode.Enforce)]
public ActionResult AppIcon()
{
return View();
}
///
/// 导入数据
///
///
[HttpGet]
//[HandlerLogin(EFilterMode.Enforce)]
public ActionResult ImportForm()
{
return View();
}
///
/// 请选择要导出的字段页面
///
///
[HttpGet]
//[HandlerLogin(EFilterMode.Enforce)]
public ActionResult ExcelExportForm()
{
return View();
}
[HttpGet]
public ActionResult ExcelExportColumn()
{
return View();
}
///
///
///
///
//[HttpGet]
//public ActionResult FastReportIndex(int id)
//{
// FastReport.Web.WebReport webReport = new FastReport.Web.WebReport();
// SetReport(webReport);
// webReport.Width = "1080";
// webReport.Height = "925";
// // webReport.ToolbarIconsStyle = ToolbarIconsStyle.Black;
// // webReport.LocalizationFile = "~/Localization/German.frl";
// webReport.EmbedPictures = true;
// //webReport.XlsxPageBreaks = false;
// // webReport.XlsxSeamless = true;
// // webReport.ZoomMode = ZoomMode.Width;
// //本地化文件 this.Server.MapPath("~/DataFiles/FastReport/") ConfigHelper.GetValue("baseDir")
// webReport.LocalizationFile = ConfigHelper.GetValue("baseDir") + "/wwwroot/DataFiles/FastReport/Localization/Chinese (Simplified).frl";
// //webReport.PrintInPdf = false;
// // fill the parameters on startup
// //webReport.Report.SetParameterValue("Test parameter 1", "Value of parameter 1 from application code");
// //webReport.Report.SetParameterValue("parameter2", "1002");
// ViewBag.WebReport = webReport;
// return View();
//}
//private void SetReport(WebReport webReport)
//{
// string report_path = GetReportPath();
// System.Data.DataSet dataSet = new System.Data.DataSet();
// dataSet.ReadXml(report_path + "nwind.xml");
// webReport.Report.Load(Path.Combine(report_path, "Simple List.frx"));
// webReport.Report.RegisterData(dataSet, "NorthWind");
// //model.WebReport.SinglePage = true;
// webReport.Designer.Path = "/WebReportDesigner/index.html";
// //model.WebReport.Designer.SaveCallBack = "/SaveDesignedReport";
// webReport.Designer.SaveMethod = (string reportID, string filename, string report) =>
// {
// string pathToSave = Path.Combine(report_path, "DesignedReports", filename);
// if (!Directory.Exists(pathToSave))
// Directory.CreateDirectory(Path.GetDirectoryName(pathToSave));
// System.IO.File.WriteAllTextAsync(pathToSave, report);
// return "OK";
// };
// // tab 4
// //Report report4 = new Report();
// //webReport.Report.Load(report_path + "wcsdevices.frx");
// //report4.SetParameterValue("parameter2", "1002");
// //webReport.Report.SetParameterValue("parameter2", "1002");
// //webReport.CurrentTab.Name = "wcsdevices";
// //System.Data.DataSet dataSet = new System.Data.DataSet();
// //dataSet.ReadXml(report_path + "nwind.xml");
// //Report report = new Report();
// //report.RegisterData(dataSet, "NorthWind");
// //report.Load(report_path + "Simple List.frx");
// //report.SetParameterValue("Test parameter 1", "Value of parameter 1 from application code");
// // webReport.CurrentTab.Name = "Simple List";
// // webReport.AddTab(report, "Simple List");
// // tab 2
// //Report report2 = new Report();
// //report2.RegisterData(dataSet, "NorthWind");
// //report2.Load(report_path + "Chart.frx");
// //webReport.AddTab(report2, "Charts");
// // tab 3
// //Report report3 = new Report();
// //report3.RegisterData(dataSet, "NorthWind");
// //report3.Load(report_path + "Master-Detail.frx");
// //webReport.AddTab(report3, "Master-Detail");
// //Report report5 = new Report();
// //report5.Report.Load(report_path + "wcsdevices.frx");
// //report5.SetParameterValue("parameter2", "1003");
// //webReport.Report.SetParameterValue("parameter2", "1003");
// //webReport.AddTab(report5, "wcsdevices5");
//}
private string GetReportPath()
{
// this.Server.MapPath("~/DataFiles/FastReport/");
return ConfigHelper.GetValue("baseDir") +"/wwwroot/DataFiles/FastReport/";
}
[HttpPost ]
public IActionResult ExportExcel(string fileName, string columnJson, string dataJson, string orderJson, string exportField)
{
//设置导出格式
ExcelConfig excelconfig = new ExcelConfig();
excelconfig.Title = WebUtility.UrlDecode(fileName);
excelconfig.TitleFont = "微软雅黑";
excelconfig.TitlePoint = 15;
excelconfig.FileName = WebUtility.UrlDecode(fileName)+DateTime.Now.Ticks + ".xls";
excelconfig.IsAllSizeColumn = true;
excelconfig.ColumnEntity = new List();
//表头
List columnList = columnJson.ToList();
string columns = string.Empty;
//写入Excel表头
Dictionary exportFieldMap = new Dictionary();
if (!string.IsNullOrEmpty(exportField))
{
string[] exportFields = exportField.Split(',');
foreach (var field in exportFields)
{
if (field == "F_ORDERNOSPLIT")
//columns += "(case when regexp_substr(F_FROMORDERNO,'[^_]+',1,2) is null then F_FROMORDERNO else regexp_substr(F_FROMORDERNO,'[^_]+',1,2) end) F_ORDERNOSPLIT,";
columns += "F_FROMORDERNO F_ORDERNOSPLIT,";
else
columns += field + ",";
if (!exportFieldMap.ContainsKey(field))
{
exportFieldMap.Add(field, "1");
}
}
}
else
{
throw new Exception("没找到表头");
}
columns = columns.Trim(',');
//行数据
DataTable rowData = null;
// 如果表头数据为空,则表示按照表头导出
if (string.IsNullOrEmpty(orderJson))
{
try
{
rowData = dataJson.ToTable();
}
catch (Exception ex)
{
throw;
}
}
else
{
//string keyNo = string.Empty;
//if (orderJson.ToTable().Columns.Contains("F_NOSPLIT"))
// keyNo = "F_NOSPLIT";
//else
// keyNo = "F_NO";
//var orderlist = orderJson.ToTable().AsEnumerable().Select(v => Convert.ToString(v.Field(keyNo))).ToArray();
//rowData = new OrderCore().GetOrderItem(orderlist, columns);
}
foreach (jfGridModel columnModel in columnList)
{
if (exportFieldMap.ContainsKey(columnModel.name) || string.IsNullOrEmpty(exportField))
{
excelconfig.ColumnEntity.Add(new ColumnModel()
{
Column = columnModel.name,
ExcelColumn = columnModel.label,
Alignment = columnModel.align,
});
}
}
// ExcelHelper.ExcelDownload(rowData, excelconfig);
return File(ExcelHelper.ExportMemoryStream(rowData, excelconfig), "application/ms-excel", excelconfig.FileName);
}
///
/// 上传附件分片数据
///
/// 文件主键
/// 分片序号
/// 文件数据
///
[HttpPost]
[ValidateAntiForgeryToken]
//public ActionResult UploadAnnexesFileChunk(string fileGuid, int chunk, int chunks, IFormFile Filedata)
//{
// //没有文件上传,直接返回
// if (Filedata == null || string.IsNullOrEmpty(Filedata.FileName) || Filedata.Length == 0)
// {
// return null;
// }
// AnnexesFile.SaveChunkAnnexes(fileGuid, chunk, Filedata.OpenReadStream());
// return Success("保存成功");
//}
///
/// 上传附件分片数据
///
/// 文件主键
/// 分片序号
/// 文件数据
///
[HttpPost]
public IActionResult UploadAnnexesFileChunk(string fileGuid, int chunk, int chunks)
{
var files = Request.Form.Files;
//没有文件上传,直接返回
if (files[0].Length == 0 || string.IsNullOrEmpty(files[0].FileName))
{
return Fail("没有文件信息");
}
byte[] bytes = new byte[files[0].Length];
MemoryStream ms = new MemoryStream();
files[0].CopyTo(ms);
ms.Flush();
ms.Position = 0;
ms.Read(bytes, 0, bytes.Length);
ms.Close();
ms.Dispose();
// FileHelper.WriteCache(chunk + "_" + fileGuid, bytes);
_annexesFileService.SaveChunkAnnexes(fileGuid, chunk, bytes);
return Success("保存成功");
}
#region 下载文件
///
/// 下载文件
///
/// 文件id
///
[HttpPost]
[ValidateAntiForgeryToken]
public IActionResult DownSchemeFile(int bustype)
{
return AnnexesFile.DownSchemeFile(bustype);
}
///
/// excel文件导入(通用)
///
/// 模板Id
/// 文件主键
/// 分片数
/// 文件扩展名
///
[HttpPost]
//////[AjaxOnly]
////[HandlerValidateAntiForgeryToken]
public ActionResult ExecuteImportExcel(string fileId, int ordertype, int chunks, string ext) // EOrderType
{
try
{
JsonExecl json = null;
LoginUserInfo LoginUser = GetLoginUser();
DataTable dt = _annexesFileService.SaveAnnexesToDataTable(fileId, ext, chunks);
if (ordertype == 100)
json = _importDataOrExeclService.ImportBillMatInfo(dt, (int)ordertype, LoginUser.UserNo);
//else
// json = itmp.ImportData(dt, (int)ordertype, LoginBLLCore.GetLoginUser().UserNo);
if (json.Type == 0)
// 异常
return FailEx(json.Message);
else if (json.Type == 1)
{
if (json.dtSou == null || json.dtSou.Rows.Count == 0)
return FailEx(json.Message);
AnnexesFile.SaveErrJsonExecl(fileId, json);
return Fail(json.Message);
}
else
// 成功
return Success(json.Message);
}
catch (Exception ex)
{
return ToJsonResult(SysExCore.GetResErr(ex));
}
}
///
/// 下载文件(导入文件未被导入的数据)
///
/// 文件id
///
[HttpPost]
[ValidateAntiForgeryToken]
public void DownImportErrorFile(string fileId, string fileName)
{
string fname = WebUtility.UrlDecode("未导入错误数据【" + fileName + "】") + ".xls";
AnnexesFile.DownErrJsonExecl(fileId, fname);
}
///
/// 移除附件分片数据
///
/// 文件主键
/// 总分片数
///
[HttpPost]
[ValidateAntiForgeryToken]
public ActionResult RemoveAnnexesFileChunk(string fileGuid, int chunks)
{
AnnexesFile.RemoveChunkAnnexes(fileGuid, chunks);
return Success("移除成功");
}
#endregion
#region jfgrid弹层选择
///
/// 列表选择弹层
///
///
[HttpGet]
//[HandlerLogin(FilterMode.Enforce)]
public ActionResult JfGirdLayerForm()
{
return View();
}
#endregion
///
/// 获取仓库的树形数据
///
/// 父级id
///
[HttpGet]
////[AjaxOnly]
public ActionResult GetWareHouseTree(string parentId)
{
var data = _warehouseService.GetTree(parentId);
return Success("", data);
}
[HttpGet]
public ActionResult WearHouseList()
{
return Success(_warehouseService.WearHouseList());
}
}
}