using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.Mvc;
using System.Text;
using System.IO;
using Bowin.Web.Controls.Mvc;
using Bowin.Common.Utility;
using Bowin.Common.Data;
using Bowin.Common.Exceptions;
using EMIS.Utility;
using EMIS.Entities;
using EMIS.Web.Controls;
using EMIS.ViewModel;
using EMIS.ViewModel.Cultureplan;
using EMIS.CommonLogic.Cultureplan;
namespace EMIS.Web.Controllers.Cultureplan
{
[Authorization]
public class CoursematerialController : Controller
{
public ICoursematerialServices CoursematerialServices { get; set; }
///
/// 课程信息页面
///
///
public ActionResult List()
{
return View();
}
///
/// 课程信息页面查询列表
///
///
///
[HttpPost]
public ActionResult List(QueryParamsModel pararms)
{
ConfiguretView configuretView = ConfiguretExtensions.GetConfiguretermsView(pararms);
//课程级别
var courseLevelID = pararms.getExtraInt("DictionaryCourseLevel") == DropdownList.SELECT_ALL ? null : pararms.getExtraInt("DictionaryCourseLevel");
//课程科类
var courseScienceID = pararms.getExtraInt("DictionaryCourseScience") == DropdownList.SELECT_ALL ? null : pararms.getExtraInt("DictionaryCourseScience");
//上课类型
var classGroupingID = pararms.getExtraGuid("TeachTypeDropdown");
//是否启用
var isEnable = pararms.getExtraInt("DictionaryIsEnable") == DropdownList.SELECT_ALL ? null : pararms.getExtraInt("DictionaryIsEnable");
return base.Json(CoursematerialServices.GetCoursematerialViewGrid(configuretView, courseLevelID, courseScienceID,
classGroupingID, isEnable, (int)pararms.page, (int)pararms.rows));
}
///
/// 课程信息下拉列表
///
///
///
[HttpPost]
public ActionResult ListForDropdown(QueryParamsModel pararms)
{
ConfiguretView configuretView = ConfiguretExtensions.GetConfiguretermsView(pararms);
//课程级别
var courseLevelID = pararms.getExtraInt("DictionaryCourseLevel") == DropdownList.SELECT_ALL ? null : pararms.getExtraInt("DictionaryCourseLevel");
//课程科类
var courseScienceID = pararms.getExtraInt("DictionaryCourseScience") == DropdownList.SELECT_ALL ? null : pararms.getExtraInt("DictionaryCourseScience");
//上课类型
var classGroupingID = pararms.getExtraGuid("TeachTypeDropdown");
//是否启用
var isEnable = pararms.getExtraInt("DictionaryIsEnable") == DropdownList.SELECT_ALL ? null : pararms.getExtraInt("DictionaryIsEnable");
return base.Json(CoursematerialServices.GetCoursematerialViewGrid(configuretView, courseLevelID, courseScienceID,
classGroupingID, isEnable, (int)pararms.page, (int)pararms.rows));
}
///
/// 课程信息下拉列表(启用状态为是的信息)
///
///
///
[HttpPost]
public ActionResult GetIsEnableCoursematerialView(QueryParamsModel pararms)
{
ConfiguretView configuretView = ConfiguretExtensions.GetConfiguretermsView(pararms);
//课程级别
var courseLevelID = pararms.getExtraInt("DictionaryCourseLevel") == DropdownList.SELECT_ALL ? null : pararms.getExtraInt("DictionaryCourseLevel");
//课程科类
var courseScienceID = pararms.getExtraInt("DictionaryCourseScience") == DropdownList.SELECT_ALL ? null : pararms.getExtraInt("DictionaryCourseScience");
//上课类型
var classGroupingID = pararms.getExtraGuid("TeachTypeDropdown");
//是否启用(默认为是)
var isEnable = (int)CF_GeneralPurpose.IsYes;
return base.Json(CoursematerialServices.GetCoursematerialViewGrid(configuretView, courseLevelID, courseScienceID,
classGroupingID, isEnable, (int)pararms.page, (int)pararms.rows));
}
///
/// 课程信息下拉列表(启用状态为是或已使用的信息)
///
///
///
[HttpPost]
public ActionResult GetEnableAndUseCoursematerialView(QueryParamsModel pararms)
{
ConfiguretView configuretView = ConfiguretExtensions.GetConfiguretermsView(pararms);
//课程信息ID
var coursematerialID = Request["coursematerialID"].ParseStrTo();
//是否启用(默认为是)
var isEnable = (int)CF_GeneralPurpose.IsYes;
return base.Json(CoursematerialServices.GetEnableAndUseCoursematerialViewGrid(configuretView, coursematerialID, isEnable, (int)pararms.page, (int)pararms.rows));
}
///
/// 复制新增
///
///
///
public ActionResult CopyAdd(Guid coursematerialID)
{
CoursematerialView coursematerialView = new CoursematerialView();
coursematerialView = CoursematerialServices.GetCoursematerialView(coursematerialID);
if (coursematerialView != null)
{
coursematerialView.Abbreviation = null;
coursematerialView.StandardName = null;
}
return View("Edit", coursematerialView);
}
///
/// 复制新增
///
///
///
[HttpPost]
public ActionResult CopyAdd(CoursematerialView coursematerialView)
{
coursematerialView.CoursematerialID = Guid.Empty;
return this.Edit(coursematerialView);
}
///
/// 编辑
///
///
///
public ActionResult Edit(Guid? coursematerialID)
{
CoursematerialView coursematerialView = new CoursematerialView();
if (coursematerialID.HasValue && coursematerialID != Guid.Empty)
{
coursematerialView = CoursematerialServices.GetCoursematerialView(coursematerialID);
}
else
{
coursematerialView.IsEnable = true;
}
return View(coursematerialView);
}
///
/// 编辑
///
///
///
[HttpPost]
public ActionResult Edit(CoursematerialView coursematerialView)
{
try
{
CoursematerialServices.CoursematerialEdit(coursematerialView);
return Json(new ReturnMessage()
{
IsSuccess = true,
Message = "保存成功。"
});
}
catch (Exception ex)
{
return Json(new ReturnMessage()
{
IsSuccess = false,
Message = "保存失败:" + ex.Message
});
}
}
///
/// 删除
///
///
///
[HttpPost]
public ActionResult Delete(string coursematerialIDs)
{
try
{
List list = coursematerialIDs.Split(',').Where(x => !string.IsNullOrEmpty(x))
.Select(x => (Guid?)new Guid(x)).ToList();
CoursematerialServices.CoursematerialDelete(list);
return base.Json(new ReturnMessage { IsSuccess = true, Message = "删除成功。" });
}
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(new ReturnMessage { IsSuccess = false, Message = "删除失败,原因:" + mge });
}
}
///
/// Excel导出
///
///
[HttpPost]
public ActionResult Excel()
{
NpoiExcelHelper neh = new NpoiExcelHelper();
ConfiguretView configuretView = ConfiguretExtensions.GetConfiguretermsView(null);
//课程级别
var courseLevelID = Request.Form["DictionaryCourseLevel"].ParseStrTo() == DropdownList.SELECT_ALL ? null : Request.Form["DictionaryCourseLevel"].ParseStrTo();
//课程科类
var courseScienceID = Request.Form["DictionaryCourseScience"].ParseStrTo() == DropdownList.SELECT_ALL ? null : Request.Form["DictionaryCourseScience"].ParseStrTo();
//上课类型
var classGroupingID = Request.Form["TeachTypeDropdown"].ParseStrTo();
//是否启用
var isEnable = Request.Form["DictionaryIsEnable"].ParseStrTo() == DropdownList.SELECT_ALL ? null : Request.Form["DictionaryIsEnable"].ParseStrTo();
var dt = CoursematerialServices.GetCoursematerialViewList(configuretView, courseLevelID, courseScienceID, classGroupingID, isEnable)
.Select(x => new
{
x.CourseCode,
x.CourseName,
x.Abbreviation,
x.EnglishName,
x.StandardName,
x.CourseEdition,
x.CourseLevelName,
x.CourseScienceName,
x.ClassName,
x.CourseSynopsis,
x.CourseReserve,
x.IsEnableName,
x.Remark
}).ToTable();
string[] liststring = {
"课程代码", "课程名称", "课程简称", "英文名称", "课程规范名", "课程版本",
"课程级别", "课程科类", "上课类型", "课程简介", "预修课程集", "是否启用", "备注"
};
neh.Export(dt, liststring, "课程信息" + DateTime.Now.ToString("yyyyMMdd"));
return Json(new ReturnMessage()
{
IsSuccess = true,
Message = "导出成功。"
});
}
///
/// Excel导入
///
///
///
///
[HttpGet]
public ActionResult Import(string errorFile, string operationTips)
{
ViewBag.ErrorFile = errorFile;
if (string.IsNullOrEmpty(operationTips))
{
operationTips = "点击查看失败原因...";
}
ViewBag.operationTips = operationTips;
return View();
}
///
/// Excel导入
///
///
///
[HttpPost]
public ActionResult Import(HttpPostedFileBase file)
{
try
{
if (!NpoiExcelHelper.GetIsCompatible(file.FileName))
{
throw new Exception("格式错误,只允许导入xls或xlsx格式的Excel文件。");
}
Dictionary cellheader = new Dictionary
{
{ "CourseCode", "课程代码" },
{ "CourseName", "课程名称" },
{ "Abbreviation", "课程简称" },
{ "EnglishName", "英文名称" },
{ "StandardName", "课程规范名" },
{ "CourseEdition", "课程版本" },
{ "CourseLevelStr", "课程级别" },
{ "CourseScienceStr", "课程科类" },
{ "ClassName", "上课类型" },
{ "CourseSynopsis", "课程简介" },
{ "CourseReserve", "预修课程集" },
{ "IsEnableStr", "是否启用" },
{ "Remark", "备注" },
{ "ErrorMessage", "未导入原因" }
};
StringBuilder errorMsg = new StringBuilder(); // 错误信息
string sourceWebPath = FileUploadHelper.UploadFile(file);
var sourcePhysicalPath = Server.MapPath(sourceWebPath);
List errList = new List();
List dataList = new List();
int? inCount = 0; //导入个数
int? upCount = 0; //更新个数
int? errCount = 0; //失败个数
//导入
CoursematerialServices.CoursematerialImport(cellheader, out inCount, out upCount, out errList, out errCount, 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}条课程信息更新成功,{2}条课程信息导入失败,点击查看。",
inCount, upCount, errCount);
ViewBag.operationTips = Errinfo;
return RedirectToAction("MsgShow", "Common", new
{
WindowID = "none",
msg = Errinfo,
url = Url.Action("Import").AddMenuParameter() + "&errorFile=" + errorWebPath + "&operationTips="
+ Errinfo + "&WindowID=" + Request["WindowID"]
});
}
else
{
string successInfo = string.Format("提示:{0}条课程信息导入成功,{1}条课程信息更新成功。", inCount, upCount);
return RedirectToAction("MsgShow", "Common", new
{
WindowID = Request["WindowID"],
msg = successInfo,
url = Url.Action("List").AddMenuParameter()
});
}
}
catch (Exception ex)
{
return RedirectToAction("MsgShow", "Common", new
{
WindowID = "none",
msg = "导入失败,原因:" + ex.Message,
url = Url.Action("Import").AddMenuParameter() + "&WindowID=" + Request["WindowID"]
});
}
}
}
}