using Learun.Application.Excel;
using System.Collections.Generic;
using System.Web.Mvc;
using Learun.Util;
using System.Data;
using Learun.Application.Base.SystemModule;
using System;
using System.Drawing;
namespace Learun.Application.Web.Areas.LR_SystemModule.Controllers
{
///
/// 版 本 Learun-ADMS-Ultimate V7.0.0 数字化智慧校园
/// Copyright (c) 2013-2018 北京泉江科技有限公司
/// 创建人:陈彬彬
/// 日 期:2017.04.01
/// 描 述:Excel导入管理
///
public class ExcelImportController : MvcControllerBase
{
private ExcelImportIBLL excelImportIBLL = new ExcelImportBLL();
private AnnexesFileIBLL annexesFileIBLL = new AnnexesFileBLL();
#region 视图功能
///
/// 导入模板管理页面
///
///
[HttpGet]
public ActionResult Index()
{
return View();
}
///
/// 导入模板管理表单
///
///
[HttpGet]
public ActionResult Form()
{
return View();
}
///
/// 设置字段属性
///
///
[HttpGet]
public ActionResult SetFieldForm()
{
return View();
}
///
/// 导入页面
///
///
[HttpGet]
public ActionResult ImportForm()
{
return View();
}
#endregion
#region 获取数据
///
/// 获取分页数据
///
/// 分页参数
/// 查询参数
///
[HttpGet]
[AjaxOnly]
public ActionResult GetPageList(string pagination, string queryJson)
{
Pagination paginationobj = pagination.ToObject();
var data = excelImportIBLL.GetPageList(paginationobj, queryJson);
var jsonData = new
{
rows = data,
total = paginationobj.total,
page = paginationobj.page,
records = paginationobj.records,
};
return JsonResult(jsonData);
}
///
/// 获取分页数据
///
/// 功能模块主键
///
[HttpGet]
[AjaxOnly]
public ActionResult GetList(string moduleId)
{
var data = excelImportIBLL.GetList(moduleId);
return JsonResult(data);
}
///
/// 获取表单数据
/// 主键
///
///
[HttpGet]
[AjaxOnly]
public ActionResult GetFormData(string keyValue)
{
ExcelImportEntity entity = excelImportIBLL.GetEntity(keyValue);
IEnumerable list = excelImportIBLL.GetFieldList(keyValue);
var data = new
{
entity = entity,
list = list
};
return JsonResult(data);
}
#endregion
#region 提交数据
///
/// 保存表单数据
///
/// 主键
/// 实体
///
[HttpPost]
[ValidateAntiForgeryToken]
[AjaxOnly]
public ActionResult SaveForm(string keyValue, string strEntity, string strList)
{
ExcelImportEntity entity = strEntity.ToObject();
List filedList = strList.ToObject>();
excelImportIBLL.SaveEntity(keyValue, entity, filedList);
return Success("保存成功!");
}
///
/// 删除表单数据
///
/// 主键
///
[HttpPost]
[AjaxOnly]
public ActionResult DeleteForm(string keyValue)
{
excelImportIBLL.DeleteEntity(keyValue);
return Success("删除成功!");
}
///
/// 更新表单数据
///
/// 主键
/// 实体数据
///
[HttpPost]
[AjaxOnly]
public ActionResult UpdateForm(string keyValue, ExcelImportEntity entity)
{
excelImportIBLL.UpdateEntity(keyValue, entity);
return Success("操作成功!");
}
#endregion
#region 扩展方法
///
/// 下载文件
///
/// 文件id
///
[HttpPost]
[ValidateAntiForgeryToken]
public void DownSchemeFile(string keyValue)
{
ExcelImportEntity templateInfo = excelImportIBLL.GetEntity(keyValue);
IEnumerable fileds = excelImportIBLL.GetFieldList(keyValue);
//设置导出格式
ExcelConfig excelconfig = new ExcelConfig();
excelconfig.FileName = Server.UrlDecode(templateInfo.F_Name) + ".xls";
excelconfig.IsAllSizeColumn = true;
excelconfig.ColumnEntity = new List();
//表头
DataTable dt = new DataTable();
foreach (var col in fileds)
{
if (col.F_RelationType != 1 && col.F_RelationType != 4 && col.F_RelationType != 5 && col.F_RelationType != 6 && col.F_RelationType != 7)
{
excelconfig.ColumnEntity.Add(new ColumnModel()
{
Column = col.F_Name,
ExcelColumn = col.F_ColName,
Alignment = "center",
Background = col.F_IsMandatory == true ? Color.Red : new Color()
});
dt.Columns.Add(col.F_Name, typeof(string));
}
}
ExcelHelper.ExcelDownload(dt, excelconfig);
}
///
/// excel文件导入(通用)
///
/// 模板Id
/// 文件主键
/// 分片数
/// 文件扩展名
///
[HttpPost]
[ValidateAntiForgeryToken]
public ActionResult ExecuteImportExcel(string templateId, string fileId, int chunks,string ext)
{
UserInfo userInfo = LoginUserInfo.Get();
string path = annexesFileIBLL.SaveAnnexes(fileId, fileId + "."+ ext, chunks, userInfo);
if (!string.IsNullOrEmpty(path))
{
DataTable dt = ExcelHelper.ExcelImport(path);
string res = excelImportIBLL.ImportTable(templateId, fileId, dt);
var data = new
{
Success = res.Split('|')[0],
Fail = res.Split('|')[1]
};
return JsonResult(data);
}
else
{
return Fail("导入数据失败!");
}
}
///
/// 工资导入
///
/// 模板Id
/// 文件主键
/// 分片数
/// 文件扩展名
///
[HttpPost]
[ValidateAntiForgeryToken]
public ActionResult ExecuteImportSaralExcel(string fileId, int chunks,string ext)
{
UserInfo userInfo = LoginUserInfo.Get();
string path = annexesFileIBLL.SaveAnnexes(fileId, fileId + "."+ ext, chunks, userInfo);
if (!string.IsNullOrEmpty(path))
{
DataTable dt = ExcelHelper.ExcelImport(path);
string res = excelImportIBLL.ImportSalaryInfo(dt,fileId);
var data = new
{
Success = res.Split('|')[0],
Fail = res.Split('|')[1]
};
return JsonResult(data);
}
else
{
return Fail("导入数据失败!");
}
}
///
/// 金隅教师信息导入
///
/// 模板Id
/// 文件主键
/// 分片数
/// 文件扩展名
///
[HttpPost]
[ValidateAntiForgeryToken]
public ActionResult EmpInfoImport(string fileId, int chunks,string ext)
{
UserInfo userInfo = LoginUserInfo.Get();
string path = annexesFileIBLL.SaveAnnexes(fileId, fileId + "."+ ext, chunks, userInfo);
if (!string.IsNullOrEmpty(path))
{
DataTable dt = ExcelHelper.ExcelImport(path);
string res = excelImportIBLL.EmpInfoImport(dt,fileId);
var data = new
{
Success = res.Split('|')[0],
Fail = res.Split('|')[1]
};
return JsonResult(data);
}
else
{
return Fail("导入数据失败!");
}
}
///
/// 下载文件(导入文件未被导入的数据)
///
/// 文件id
///
[HttpPost]
[ValidateAntiForgeryToken]
public void DownImportErrorFile(string fileId,string fileName)
{
//设置导出格式
ExcelConfig excelconfig = new ExcelConfig();
excelconfig.FileName = Server.UrlDecode("未导入错误数据【" + fileName + "】") + ".xls";
excelconfig.IsAllSizeColumn = true;
excelconfig.ColumnEntity = new List();
//表头
DataTable dt = excelImportIBLL.GetImportError(fileId);
foreach (DataColumn col in dt.Columns)
{
if (col.ColumnName == "导入错误")
{
excelconfig.ColumnEntity.Add(new ColumnModel()
{
Column = col.ColumnName,
ExcelColumn = col.ColumnName,
Alignment = "center",
Background = Color.Red
});
}
else
{
excelconfig.ColumnEntity.Add(new ColumnModel()
{
Column = col.ColumnName,
ExcelColumn = col.ColumnName,
Alignment = "center",
});
}
}
ExcelHelper.ExcelDownload(dt, excelconfig);
}
#endregion
}
}