PublisherController.cs 11 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291
  1. using System;
  2. using System.Collections.Generic;
  3. using System.Linq;
  4. using System.Web;
  5. using System.Web.Mvc;
  6. using EMIS.ViewModel;
  7. using EMIS.Web.Controls;
  8. using Bowin.Web.Controls.Mvc;
  9. using EMIS.ViewModel.SystemView;
  10. using EMIS.CommonLogic.TeachingMaterial;
  11. using EMIS.ViewModel.TeachingMaterial;
  12. using Bowin.Common.Utility;
  13. using Bowin.Common.Data;
  14. using Bowin.Common.Exceptions;
  15. using System.IO;
  16. using EMIS.Utility;
  17. using System.Text;
  18. namespace EMIS.Web.Controllers.TeachingMaterial
  19. {
  20. [Authorization]
  21. public class PublisherController : Controller
  22. {
  23. public IPublishServices PublishServices { get; set; }
  24. /// <summary>
  25. /// 出版单位页面
  26. /// </summary>
  27. /// <returns></returns>
  28. [HttpGet]
  29. public ActionResult List()
  30. {
  31. return View();
  32. }
  33. /// <summary>
  34. /// 列表查询
  35. /// </summary>
  36. /// <param name="pararms"></param>
  37. /// <returns></returns>
  38. [HttpPost]
  39. public ActionResult List(QueryParamsModel pararms)
  40. {
  41. ConfiguretView configuretView = ConfiguretExtensions.GetConfiguretermsView(pararms);
  42. //避开全选值
  43. bool? isprivide = null;
  44. bool? ispublish = null;
  45. bool? isprint = null;
  46. if (pararms.getExtraInt("PrivideDropdown") != null && pararms.getExtraInt("PrivideDropdown") != DropdownList.SELECT_ALL)
  47. {
  48. isprivide = pararms.getExtraInt("PrivideDropdown") == 1 ? true : false; ;
  49. }
  50. if (pararms.getExtraInt("PublishDropdown") != null && pararms.getExtraInt("PublishDropdown") != DropdownList.SELECT_ALL)
  51. {
  52. ispublish = pararms.getExtraInt("PublishDropdown") == 1 ? true : false; ;
  53. }
  54. if (pararms.getExtraInt("PrintDropdown") != null && pararms.getExtraInt("PrintDropdown") != DropdownList.SELECT_ALL)
  55. {
  56. isprint = pararms.getExtraInt("PrintDropdown") == 1 ? true : false; ;
  57. }
  58. if (configuretView.Attribute == DropdownList.SELECT_ALL.ToString()) configuretView.Attribute = "";
  59. return base.Json(PublishServices.GetPublishViewGrid(configuretView, isprivide, ispublish, isprint, (int)pararms.page, (int)pararms.rows));
  60. }
  61. /// <summary>
  62. /// 查询是供应商的列表
  63. /// </summary>
  64. /// <param name="pararms"></param>
  65. /// <returns></returns>
  66. [HttpPost]
  67. public ActionResult SupplierList(QueryParamsModel pararms)
  68. {
  69. ConfiguretView configuretView = ConfiguretExtensions.GetConfiguretermsView(pararms);
  70. bool? isSupplier = true;
  71. if (configuretView.Attribute == DropdownList.SELECT_ALL.ToString()) configuretView.Attribute = "";
  72. return base.Json(PublishServices.GetPublishViewGrid(configuretView, isSupplier, null, null, (int)pararms.page, (int)pararms.rows));
  73. }
  74. [HttpPost]
  75. public ActionResult Excel(QueryParamsModel pararms)
  76. {
  77. ConfiguretView configuretView = ConfiguretExtensions.GetConfiguretermsView(null);
  78. //避开全选值
  79. bool? isprivide = null;
  80. bool? ispublish = null;
  81. bool? isprint = null;
  82. if (Request.Form["PrivideDropdown"].ParseStrTo<int>() != null && Request.Form["PrivideDropdown"].ParseStrTo<int>() != DropdownList.SELECT_ALL)
  83. {
  84. isprivide = Request.Form["PrivideDropdown"].ParseStrTo<int>() == 1 ? true : false; ;
  85. }
  86. if (Request.Form["PublishDropdown"].ParseStrTo<int>() != null && Request.Form["PublishDropdown"].ParseStrTo<int>() != DropdownList.SELECT_ALL)
  87. {
  88. ispublish = Request.Form["PublishDropdown"].ParseStrTo<int>() == 1 ? true : false; ;
  89. }
  90. if (Request.Form["PrintDropdown"].ParseStrTo<int>() != null && Request.Form["PrintDropdown"].ParseStrTo<int>() != DropdownList.SELECT_ALL)
  91. {
  92. isprint = Request.Form["PrintDropdown"].ParseStrTo<int>() == 1 ? true : false; ;
  93. }
  94. if (configuretView.Attribute == DropdownList.SELECT_ALL.ToString()) configuretView.Attribute = "";
  95. NpoiExcelHelper neh = new NpoiExcelHelper();
  96. var dt = PublishServices.GetPublishViewExcel(configuretView, isprivide, ispublish, isprint).Select(x => new
  97. {
  98. x.UnitCode,
  99. x.UnitName,
  100. x.ContectUser,
  101. x.Mobile,
  102. x.Phone,
  103. x.Address
  104. }).ToTable();
  105. string[] liststring = { "单位编码", "单位名称", "联系人", "移动电话", "固定电话", "单位地址" };
  106. neh.Export(dt, liststring, "出版单位信息");
  107. return RedirectToAction("MsgShow", "Common", new
  108. {
  109. msg = "导出成功!",
  110. url = Url.Content("~/Publisher/List").AddMenuParameter()
  111. });
  112. }
  113. /// <summary>
  114. /// 编辑页面
  115. /// </summary>
  116. /// <returns></returns>
  117. [HttpGet]
  118. public ActionResult Edit(Guid? publishID)
  119. {
  120. PublisherView publishView;
  121. if (publishID != null && publishID != Guid.Empty)
  122. {
  123. publishView = PublishServices.GetSinglePublish(publishID.Value);
  124. }
  125. else
  126. {
  127. publishView = new PublisherView()
  128. {
  129. PublishID = Guid.Empty
  130. };
  131. }
  132. return View(publishView);
  133. }
  134. /// <summary>
  135. /// 新增
  136. /// </summary>
  137. /// <returns></returns>
  138. [HttpPost]
  139. public ActionResult Edit(PublisherView publisherView)
  140. {
  141. try
  142. {
  143. var user = HttpContext.User as EMIS.Utility.FormValidate.CustomPrincipal;
  144. PublishServices.EditPulish(publisherView, user.UserID);
  145. return Json(new ReturnMessage()
  146. {
  147. IsSuccess = true,
  148. Message = "保存成功!"
  149. });
  150. }
  151. catch (Exception ex)
  152. {
  153. return Json(new ReturnMessage()
  154. {
  155. IsSuccess = false,
  156. Message = "保存失败,原因:" + ex.Message
  157. });
  158. }
  159. }
  160. /// <summary>
  161. /// 删除
  162. /// </summary>
  163. /// <param name="roleID"></param>
  164. /// <returns></returns>
  165. [HttpPost]
  166. public ActionResult Delete(string publishIDs)
  167. {
  168. try
  169. {
  170. var publishIDList = publishIDs.Split(',').Select(x => (Guid)new Guid(x)).ToList();
  171. PublishServices.DeletePublish(publishIDList);
  172. return base.Json("删除成功");
  173. }
  174. catch (Exception ex)
  175. {
  176. string mge = ex.Message;
  177. System.Data.SqlClient.SqlException num = ExceptionHelper.GetSqlException(ex);
  178. if (num != null)
  179. {
  180. if (num.Number == 547)
  181. mge = "请先删除所有关联的数据,如:教材信息等";
  182. }
  183. return base.Json("删除失败,原因:" + mge + "!");
  184. }
  185. }
  186. #region 13.0 出版单位信息导入
  187. [HttpGet]
  188. public ActionResult Import(string errorFile, string operationTips)
  189. {
  190. ViewBag.ErrorFile = errorFile;
  191. if (string.IsNullOrEmpty(operationTips))
  192. {
  193. operationTips = "错误数据下载";
  194. }
  195. ViewBag.operationTips = operationTips;
  196. return View();
  197. }
  198. [HttpPost]
  199. public ActionResult Import(HttpPostedFileBase file)
  200. {
  201. try
  202. {
  203. //1.0 HSSFWorkbook:是操作Excel2003以前(包括2003)的版本,扩展名是.xls
  204. //2.0 XSSFWorkbook:是操作Excel2007的版本,扩展名是.xlsx
  205. var accept = new[] { ".xls", ".xlsx" };
  206. var p = Path.GetExtension(file.FileName);
  207. if (!accept.Contains(p))
  208. {
  209. throw new Exception("只允许上传xls格式的Excel文件!");
  210. }
  211. Dictionary<string, string> cellheader = new Dictionary<string, string>
  212. {
  213. { "UnitCode", "单位编号" },
  214. { "UnitName", "单位名称" },
  215. { "ContectUser", "联系人" },
  216. { "Mobile", "移动电话" },
  217. { "Phone", "固定电话" }, { "Fax", "传真" },
  218. { "Email", "邮箱" },
  219. { "BandName", "开户行名称" },{ "BandCard", "银行帐号" },
  220. { "Ein", "税号" },
  221. { "IsSupplierName", "是否供应商" }, { "IsPulishName", "是否出版单位" },
  222. { "IsPrintName", "是否印刷厂" }, { "Address", "单位地址" },{ "Desc", "备注" }, { "ErrorMessage", "错误信息" }
  223. };
  224. StringBuilder errorMsg = new StringBuilder(); // 错误信息
  225. string sourceWebPath = FileUploadHelper.UploadFile(file);
  226. var sourcePhysicalPath = Server.MapPath(sourceWebPath);
  227. List<PublisherView> errList = new List<PublisherView>();
  228. List<PublisherView> dataList = new List<PublisherView>();
  229. int errCount = 0;
  230. int OkCount = 0;
  231. PublishServices.PublishImport(cellheader, out errList, out errCount, out OkCount, sourcePhysicalPath);
  232. System.IO.File.Delete(sourcePhysicalPath);//删除本地缓存文件
  233. if (errList.Count() > 0)
  234. {
  235. //将异常文件路径显示
  236. string errorWebPath = string.Format("{0}", NpoiExcelHelper.EntityListToExcel2003(cellheader, errList, "出版单位导入失败文件", sourcePhysicalPath));
  237. ViewBag.ErrorFile = errorWebPath;
  238. string Errinfo = string.Format("提示:成功导入{0}条记录,失败{1}条,详情请点击错误数据下载查看。", OkCount < 1 ? 0 : OkCount, errCount < 1 ? 0 : errCount);
  239. ViewBag.operationTips = Errinfo;
  240. // //return RedirectToAction("Import", new { errorFile = errorWebPath, operationTips = Errinfo });
  241. return RedirectToAction("MsgShow", "Common", new
  242. {
  243. WindowID = "none",
  244. msg = "导入失败!",
  245. url = Url.Action("Import").AddMenuParameter() + "&errorFile=" + errorWebPath + "&operationTips=" + Errinfo + "&WindowID=" + Request["WindowID"]
  246. });
  247. }
  248. else
  249. {
  250. return RedirectToAction("MsgShow", "Common", new
  251. {
  252. WindowID = Request["WindowID"],
  253. msg = "导入成功!",
  254. url = Url.Action("List").AddMenuParameter()
  255. });
  256. }
  257. }
  258. catch (Exception ex)
  259. {
  260. return RedirectToAction("MsgShow", "Common", new
  261. {
  262. WindowID = Request["WindowID"],
  263. msg = "导入失败,原因:" + ex.Message,
  264. url = Url.Action("List").AddMenuParameter()
  265. });
  266. }
  267. #endregion
  268. }
  269. }
  270. }