using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.Mvc;
using EMIS.ViewModel;
using EMIS.Web.Controls;
using Bowin.Web.Controls.Mvc;
using EMIS.ViewModel.SystemView;
using EMIS.CommonLogic.TeachingMaterial;
using EMIS.ViewModel.TeachingMaterial;
using Bowin.Common.Utility;
using Bowin.Common.Data;
using Bowin.Common.Exceptions;
using System.IO;
using EMIS.Utility;
using System.Text;
namespace EMIS.Web.Controllers.TeachingMaterial
{
[Authorization]
public class PublisherController : Controller
{
public IPublishServices PublishServices { get; set; }
///
/// 出版单位页面
///
///
[HttpGet]
public ActionResult List()
{
return View();
}
///
/// 列表查询
///
///
///
[HttpPost]
public ActionResult List(QueryParamsModel pararms)
{
ConfiguretView configuretView = ConfiguretExtensions.GetConfiguretermsView(pararms);
//避开全选值
bool? isprivide = null;
bool? ispublish = null;
bool? isprint = null;
if (pararms.getExtraInt("PrivideDropdown") != null && pararms.getExtraInt("PrivideDropdown") != DropdownList.SELECT_ALL)
{
isprivide = pararms.getExtraInt("PrivideDropdown") == 1 ? true : false; ;
}
if (pararms.getExtraInt("PublishDropdown") != null && pararms.getExtraInt("PublishDropdown") != DropdownList.SELECT_ALL)
{
ispublish = pararms.getExtraInt("PublishDropdown") == 1 ? true : false; ;
}
if (pararms.getExtraInt("PrintDropdown") != null && pararms.getExtraInt("PrintDropdown") != DropdownList.SELECT_ALL)
{
isprint = pararms.getExtraInt("PrintDropdown") == 1 ? true : false; ;
}
if (configuretView.Attribute == DropdownList.SELECT_ALL.ToString()) configuretView.Attribute = "";
return base.Json(PublishServices.GetPublishViewGrid(configuretView, isprivide, ispublish, isprint, (int)pararms.page, (int)pararms.rows));
}
///
/// 查询是供应商的列表
///
///
///
[HttpPost]
public ActionResult SupplierList(QueryParamsModel pararms)
{
ConfiguretView configuretView = ConfiguretExtensions.GetConfiguretermsView(pararms);
bool? isSupplier = true;
if (configuretView.Attribute == DropdownList.SELECT_ALL.ToString()) configuretView.Attribute = "";
return base.Json(PublishServices.GetPublishViewGrid(configuretView, isSupplier, null, null, (int)pararms.page, (int)pararms.rows));
}
[HttpPost]
public ActionResult Excel(QueryParamsModel pararms)
{
ConfiguretView configuretView = ConfiguretExtensions.GetConfiguretermsView(null);
//避开全选值
bool? isprivide = null;
bool? ispublish = null;
bool? isprint = null;
if (Request.Form["PrivideDropdown"].ParseStrTo() != null && Request.Form["PrivideDropdown"].ParseStrTo() != DropdownList.SELECT_ALL)
{
isprivide = Request.Form["PrivideDropdown"].ParseStrTo() == 1 ? true : false; ;
}
if (Request.Form["PublishDropdown"].ParseStrTo() != null && Request.Form["PublishDropdown"].ParseStrTo() != DropdownList.SELECT_ALL)
{
ispublish = Request.Form["PublishDropdown"].ParseStrTo() == 1 ? true : false; ;
}
if (Request.Form["PrintDropdown"].ParseStrTo() != null && Request.Form["PrintDropdown"].ParseStrTo() != DropdownList.SELECT_ALL)
{
isprint = Request.Form["PrintDropdown"].ParseStrTo() == 1 ? true : false; ;
}
if (configuretView.Attribute == DropdownList.SELECT_ALL.ToString()) configuretView.Attribute = "";
NpoiExcelHelper neh = new NpoiExcelHelper();
var dt = PublishServices.GetPublishViewExcel(configuretView, isprivide, ispublish, isprint).Select(x => new
{
x.UnitCode,
x.UnitName,
x.ContectUser,
x.Mobile,
x.Phone,
x.Address
}).ToTable();
string[] liststring = { "单位编码", "单位名称", "联系人", "移动电话", "固定电话", "单位地址" };
neh.Export(dt, liststring, "出版单位信息");
return RedirectToAction("MsgShow", "Common", new
{
msg = "导出成功!",
url = Url.Content("~/Publisher/List").AddMenuParameter()
});
}
///
/// 编辑页面
///
///
[HttpGet]
public ActionResult Edit(Guid? publishID)
{
PublisherView publishView;
if (publishID != null && publishID != Guid.Empty)
{
publishView = PublishServices.GetSinglePublish(publishID.Value);
}
else
{
publishView = new PublisherView()
{
PublishID = Guid.Empty
};
}
return View(publishView);
}
///
/// 新增
///
///
[HttpPost]
public ActionResult Edit(PublisherView publisherView)
{
try
{
var user = HttpContext.User as EMIS.Utility.FormValidate.CustomPrincipal;
PublishServices.EditPulish(publisherView, user.UserID);
return Json(new ReturnMessage()
{
IsSuccess = true,
Message = "保存成功!"
});
}
catch (Exception ex)
{
return Json(new ReturnMessage()
{
IsSuccess = false,
Message = "保存失败,原因:" + ex.Message
});
}
}
///
/// 删除
///
///
///
[HttpPost]
public ActionResult Delete(string publishIDs)
{
try
{
var publishIDList = publishIDs.Split(',').Select(x => (Guid)new Guid(x)).ToList();
PublishServices.DeletePublish(publishIDList);
return base.Json("删除成功");
}
catch (Exception ex)
{
string mge = ex.Message;
System.Data.SqlClient.SqlException num = ExceptionHelper.GetSqlException(ex);
if (num != null)
{
if (num.Number == 547)
mge = "请先删除所有关联的数据,如:教材信息等";
}
return base.Json("删除失败,原因:" + mge + "!");
}
}
#region 13.0 出版单位信息导入
[HttpGet]
public ActionResult Import(string errorFile, string operationTips)
{
ViewBag.ErrorFile = errorFile;
if (string.IsNullOrEmpty(operationTips))
{
operationTips = "错误数据下载";
}
ViewBag.operationTips = operationTips;
return View();
}
[HttpPost]
public ActionResult Import(HttpPostedFileBase file)
{
try
{
//1.0 HSSFWorkbook:是操作Excel2003以前(包括2003)的版本,扩展名是.xls
//2.0 XSSFWorkbook:是操作Excel2007的版本,扩展名是.xlsx
var accept = new[] { ".xls", ".xlsx" };
var p = Path.GetExtension(file.FileName);
if (!accept.Contains(p))
{
throw new Exception("只允许上传xls格式的Excel文件!");
}
Dictionary cellheader = new Dictionary
{
{ "UnitCode", "单位编号" },
{ "UnitName", "单位名称" },
{ "ContectUser", "联系人" },
{ "Mobile", "移动电话" },
{ "Phone", "固定电话" }, { "Fax", "传真" },
{ "Email", "邮箱" },
{ "BandName", "开户行名称" },{ "BandCard", "银行帐号" },
{ "Ein", "税号" },
{ "IsSupplierName", "是否供应商" }, { "IsPulishName", "是否出版单位" },
{ "IsPrintName", "是否印刷厂" }, { "Address", "单位地址" },{ "Desc", "备注" }, { "ErrorMessage", "错误信息" }
};
StringBuilder errorMsg = new StringBuilder(); // 错误信息
string sourceWebPath = FileUploadHelper.UploadFile(file);
var sourcePhysicalPath = Server.MapPath(sourceWebPath);
List errList = new List();
List dataList = new List();
int errCount = 0;
int OkCount = 0;
PublishServices.PublishImport(cellheader, out errList, out errCount, out OkCount, sourcePhysicalPath);
System.IO.File.Delete(sourcePhysicalPath);//删除本地缓存文件
if (errList.Count() > 0)
{
//将异常文件路径显示
string errorWebPath = string.Format("{0}", NpoiExcelHelper.EntityListToExcel2003(cellheader, errList, "出版单位导入失败文件", sourcePhysicalPath));
ViewBag.ErrorFile = errorWebPath;
string Errinfo = string.Format("提示:成功导入{0}条记录,失败{1}条,详情请点击错误数据下载查看。", OkCount < 1 ? 0 : OkCount, errCount < 1 ? 0 : errCount);
ViewBag.operationTips = Errinfo;
// //return RedirectToAction("Import", new { errorFile = errorWebPath, operationTips = Errinfo });
return RedirectToAction("MsgShow", "Common", new
{
WindowID = "none",
msg = "导入失败!",
url = Url.Action("Import").AddMenuParameter() + "&errorFile=" + errorWebPath + "&operationTips=" + Errinfo + "&WindowID=" + Request["WindowID"]
});
}
else
{
return RedirectToAction("MsgShow", "Common", new
{
WindowID = Request["WindowID"],
msg = "导入成功!",
url = Url.Action("List").AddMenuParameter()
});
}
}
catch (Exception ex)
{
return RedirectToAction("MsgShow", "Common", new
{
WindowID = Request["WindowID"],
msg = "导入失败,原因:" + ex.Message,
url = Url.Action("List").AddMenuParameter()
});
}
#endregion
}
}
}