using System; using System.Collections.Generic; using System.Linq; using System.Data.Entity; using System.Text; using EMIS.DataLogic.Common.TeachingMaterial; using EMIS.ViewModel.TeachingMaterial; using Bowin.Common.Linq.Entity; using EMIS.ViewModel; using EMIS.DataLogic.Repositories; using EMIS.Entities; using EMIS.Utility.FormValidate; using System.Data; using Bowin.Common.Utility; using EMIS.ViewModel.CacheManage; using Bowin.Common.Linq; using System.Linq.Expressions; using EMIS.ViewModel.Cultureplan; using System.Text.RegularExpressions; namespace EMIS.CommonLogic.TeachingMaterial { public class TeachingMaterialPoolServices : BaseServices, ITeachingMaterialPoolServices { #region --0.0 定义-- public TeachingMaterialPoolDAL TeachingMaterialPoolDAL { get; set; } public TeachingMaterialPoolRepository TeachingMaterialPoolRepository { get; set; } public UserRepository UserRepository { get; set; } public CoursematerialRepository CoursematerialRepository { get; set; } public DictionaryItemRepository DictionaryItemRepository { get; set; } public PublishRepository PublishRepository { get; set; } #endregion #region 1.0 获取教材总库信息列表 /// /// 获取教材总库信息列表 /// /// /// public IGridResultSet GetTeachingMaterialPoolViewGrid(ConfiguretView configuretView, Guid? publishID, Guid? coursematerialID, bool? isLate, int? teachingMaterialType, int pageIndex, int pageSize) { Expression> exp = (x => true); if (publishID != null && publishID != Guid.Empty) { exp = exp.And(x => x.PublishID == publishID); } if (coursematerialID != null && coursematerialID != Guid.Empty) { exp = exp.And(x => x.EM_Coursematerial.Any(w => w.CoursematerialID == coursematerialID)); } if (isLate != null) { exp = exp.And(x => x.IsLate == isLate); } if (teachingMaterialType != null && teachingMaterialType > 0) { exp = exp.And(x => x.TeachingMaterialTypeID == teachingMaterialType); } var query = TeachingMaterialPoolDAL.GetTeachingMaterialPoolGridView(exp); if (!string.IsNullOrEmpty(configuretView.ConditionValue)) return query.DynamicWhere(configuretView.Attribute, configuretView.Condition, configuretView.ConditionValue).OrderBy(x => x.TeachingMaterialCode.Length).ThenBy(x => x.TeachingMaterialCode).ToGridResultSet(pageIndex, pageSize);//.OrderBy(x => x.TeachingMaterialTypeID).OrderByDescending(x => x.TeachingMaterialName) //.OrderBy(x => x.TeachingMaterialCode) return query.OrderBy(x => x.TeachingMaterialCode.Length).ThenBy(x => x.TeachingMaterialCode).ToGridResultSet(pageIndex, pageSize); } #endregion #region 2.0 编辑保存教材信息 /// /// 编辑教材总库 /// /// public void EditTeachingMaterialPool(TeachingMaterialPoolView teachingMaterialPoolView, Guid createUserID, IList coursematerialList) { try { var coursematerialIDList = coursematerialList.Select(x => x.CoursematerialID).ToList(); var coursematerialEntityList = CoursematerialRepository.GetList(x => coursematerialIDList.Contains(x.CoursematerialID)).ToList(); if (teachingMaterialPoolView.PublishTime == null || teachingMaterialPoolView.PublishTime == "") { throw new Exception("版本时间不能为空"); } if (teachingMaterialPoolView.TeachingMaterialCode.Length > 10) { throw new Exception("教材编号不能超过十位数,请重新输入"); } if (this.TeachingMaterialPoolRepository.GetList(x => x.TeachingMaterialCode == teachingMaterialPoolView.TeachingMaterialCode && x.TeachingMaterialPoolID != teachingMaterialPoolView.TeachingMaterialPoolID).Count() > 0) { throw new Exception("教材编号已经存在,请重新输入!"); } if (this.TeachingMaterialPoolRepository.GetList(x => x.ISBN == teachingMaterialPoolView.ISBN && x.TeachingMaterialPoolID != teachingMaterialPoolView.TeachingMaterialPoolID).Count() > 0) { throw new Exception("ISBN已经存在,请重新输入!"); } Regex datestr = new Regex("^[0-9]{4}年[0-9]{1,2}月[0-9]{1,3}版[0-9]{1,3}次$"); if (!datestr.IsMatch(teachingMaterialPoolView.PublishTime))//时间格式验证 { throw new Exception("版本时间格式不正确,应为 xxxx年xx月xx版xx次,请重新输入!"); } if (teachingMaterialPoolView.TeachingMaterialPoolID != null && teachingMaterialPoolView.TeachingMaterialPoolID != Guid.Empty) { CF_TeachingMaterialPool teachingMaterialPool = TeachingMaterialPoolRepository .GetSingle(x => x.TeachingMaterialPoolID == teachingMaterialPoolView.TeachingMaterialPoolID, (x => x.EM_Coursematerial)); teachingMaterialPool.TeachingMaterialCode = teachingMaterialPoolView.TeachingMaterialCode; teachingMaterialPool.TeachingMaterialName = teachingMaterialPoolView.TeachingMaterialName; teachingMaterialPool.EM_Coursematerial = new HashSet(); teachingMaterialPool.EM_Coursematerial = new HashSet(coursematerialEntityList); teachingMaterialPool.ISBN = teachingMaterialPoolView.ISBN; teachingMaterialPool.TeachingMaterialShorName = teachingMaterialPoolView.TeachingMaterialShorName; teachingMaterialPool.TeachingMaterialTypeID = teachingMaterialPoolView.TeachingMaterialTypeID == null ? -1 : teachingMaterialPoolView.TeachingMaterialTypeID; teachingMaterialPool.WnningLevel = teachingMaterialPoolView.WnningLevel == null ? -1 : teachingMaterialPoolView.WnningLevel; teachingMaterialPool.PublishTime = teachingMaterialPoolView.PublishTime; teachingMaterialPool.PublishID = teachingMaterialPoolView.PublishID; teachingMaterialPool.Author = teachingMaterialPoolView.Author; teachingMaterialPool.Price = teachingMaterialPoolView.Price; teachingMaterialPool.IsLate = teachingMaterialPoolView.IsLate; teachingMaterialPool.ModifyUserID = createUserID; teachingMaterialPool.ModifyTime = DateTime.Now; teachingMaterialPool.Desc = teachingMaterialPoolView.Desc; teachingMaterialPool.MinInventory = teachingMaterialPoolView.MinInventory; TeachingMaterialPoolRepository.UnitOfWork.Update(teachingMaterialPool); TeachingMaterialPoolRepository.UnitOfWork.Commit(); } else { CF_TeachingMaterialPool teachingMaterialPool = new CF_TeachingMaterialPool() { TeachingMaterialPoolID = Guid.NewGuid(), WnningLevel = teachingMaterialPoolView.WnningLevel == null ? -1 : teachingMaterialPoolView.WnningLevel, PublishID = teachingMaterialPoolView.PublishID, Author = teachingMaterialPoolView.Author, ISBN = teachingMaterialPoolView.ISBN, IsLate = teachingMaterialPoolView.IsLate, PublishTime = teachingMaterialPoolView.PublishTime, TeachingMaterialCode = teachingMaterialPoolView.TeachingMaterialCode, TeachingMaterialName = teachingMaterialPoolView.TeachingMaterialName, TeachingMaterialShorName = teachingMaterialPoolView.TeachingMaterialShorName == null ? teachingMaterialPoolView.TeachingMaterialName : teachingMaterialPoolView.TeachingMaterialShorName, TeachingMaterialTypeID = teachingMaterialPoolView.TeachingMaterialTypeID == null ? -1 : teachingMaterialPoolView.TeachingMaterialTypeID, Price = teachingMaterialPoolView.Price, EM_Coursematerial = new HashSet(coursematerialEntityList), Desc = teachingMaterialPoolView.Desc, CreateTime = DateTime.Now, CreateUserID = createUserID, MinInventory = 1,//最小库存量默认1 ModifyUserID = createUserID, ModifyTime = DateTime.Now }; TeachingMaterialPoolRepository.UnitOfWork.Add(teachingMaterialPool); TeachingMaterialPoolRepository.UnitOfWork.Commit(); } } catch (Exception ex) { throw ex; } } #endregion #region 3.0 删除教材总库信息 /// /// 删除教材总库信息 /// /// public void DeleteTeachingMaterialPool(List TeachingMaterialPoolIDs) { try { TeachingMaterialPoolRepository.UnitOfWork.Delete(x => TeachingMaterialPoolIDs.Contains(x.TeachingMaterialPoolID)); } catch (Exception ex) { throw ex; } } #endregion #region 4.0 获取教材总库单位 /// /// 获取教材总库单位 /// /// public TeachingMaterialPoolView GetSingleTeachingMaterialPool(Guid TeachingMaterialPoolID) { return TeachingMaterialPoolDAL.GetSingleTeachingMaterialPool(TeachingMaterialPoolID); } #endregion #region 5.0 获取教材总库息列表导出 /// /// 获取教材总库息列表导出 /// /// /// public IList GetTeachingMaterialPoolViewExcel(ConfiguretView configuretView, Guid? publishID, Guid? coursematerialID, bool? isLate, int? teachingMaterialType) { Expression> exp = (x => true); if (publishID != null && publishID != Guid.Empty) { exp = exp.And(x => x.PublishID == publishID); } if (coursematerialID != null && coursematerialID != Guid.Empty) { exp = exp.And(x => x.EM_Coursematerial.Any(w => w.CoursematerialID == coursematerialID)); } if (isLate != null) { exp = exp.And(x => x.IsLate == isLate); } if (teachingMaterialType != null && teachingMaterialType > 0) { exp = exp.And(x => x.TeachingMaterialTypeID == teachingMaterialType); } var query = TeachingMaterialPoolDAL.GetTeachingMaterialPoolGridView(exp); if (!string.IsNullOrEmpty(configuretView.ConditionValue)) return query.DynamicWhere(configuretView.Attribute, configuretView.Condition, configuretView.ConditionValue).OrderBy(x => x.TeachingMaterialTypeID).OrderByDescending(x => x.TeachingMaterialName).ToList(); return query.OrderBy(x => x.TeachingMaterialCode).ToList(); } #endregion #region 获取教材对应的课程资料 public List GetTeachingMaterialCoursematerialList(Guid? teachingMaterialPoolID) { if (teachingMaterialPoolID.HasValue) { var coursematerialViewList = TeachingMaterialPoolDAL.GetTeachingMaterialPoolCoursematerialView(x => x.TeachingMaterialPoolID == teachingMaterialPoolID); return coursematerialViewList.ToList(); } else { return new List(); } } #endregion #region 6.0 获取教材总库信息列表(用于学生征订) /// /// 获取教材总库信息列表(用于学生征订) /// /// /// public IGridResultSet GetTeachingMaterialPoolForOrder(ConfiguretView configuretView, Guid? publishID, string isbn, Guid? coursematerialID, string teachingMaterialName, string ischeck, int pageIndex, int pageSize) { Expression> exp = x => true; exp = exp.And(x => x.IsLate != true); if (ischeck == "true" && coursematerialID != null && coursematerialID != Guid.Empty) { exp = exp.And(x => x.EM_Coursematerial.Any(y => y.CoursematerialID == coursematerialID)); } var query = TeachingMaterialPoolDAL.GetTeachingMaterialPoolForOrder(exp, coursematerialID); if (publishID != null && publishID != Guid.Empty) { query = query.Where(x => x.PublishID == publishID); } if (isbn != null && isbn != "")// && !isbn.IsEmpty() { query = query.Where(x => x.ISBN.Contains(isbn)); } if (teachingMaterialName != null && teachingMaterialName != "") { query = query.Where(x => x.TeachingMaterialName.Contains(teachingMaterialName)); } //暂时不根据征订的课程筛选教材 //if (coursematerialID != null && coursematerialID != Guid.Empty) //{ // query = query.where(x => x.coursematerialid == coursematerialid); //} if (!string.IsNullOrEmpty(configuretView.ConditionValue)) return query.DynamicWhere(configuretView.Attribute, configuretView.Condition, configuretView.ConditionValue) .OrderBy(x => x.TeachingMaterialName).ToGridResultSet(pageIndex, pageSize); return query.OrderBy(x => x.OrderID) .ThenBy(x => x.TeachingMaterialName).ToGridResultSet(pageIndex, pageSize); } #endregion #region 7.0 编辑保存教材库存信息 /// /// 编辑教材库存 /// /// public void EditTeachingMateriaInventory(TeachingMaterialPoolView teachingMaterialPoolView, Guid createUserID) { try { if (teachingMaterialPoolView.TeachingMaterialPoolID != null && teachingMaterialPoolView.TeachingMaterialPoolID != Guid.Empty) { CF_TeachingMaterialPool teachingMaterialPool = TeachingMaterialPoolRepository.GetSingle(x => x.TeachingMaterialPoolID == teachingMaterialPoolView.TeachingMaterialPoolID); teachingMaterialPool.MinInventory = teachingMaterialPoolView.MinInventory; TeachingMaterialPoolRepository.UnitOfWork.Update(teachingMaterialPool); TeachingMaterialPoolRepository.UnitOfWork.Commit(); } } catch (Exception ex) { throw ex; } } #endregion public bool IsExistCode(string code) { bool IsExist = false; var Endt = TeachingMaterialPoolRepository.GetSingle(x => x.TeachingMaterialCode == code); if (Endt != null) { IsExist = true; } return IsExist; } public bool IsExistISBN(string ISBN) { bool IsExist = false; var Endt = TeachingMaterialPoolRepository.GetSingle(x => x.ISBN == ISBN); if (Endt != null) { IsExist = true; } return IsExist; } public IGridResultSet GetStudentTeachingMaterialViewGrid(ConfiguretView configuretView, int pageIndex, int pageSize) { var user = CustomPrincipal.Current; var query = TeachingMaterialPoolDAL.GetStudentTeachingMaterialView(x => x.UserID == user.UserID); if (!string.IsNullOrEmpty(configuretView.ConditionValue)) query = query.DynamicWhere(configuretView.Attribute, configuretView.Condition, configuretView.ConditionValue); return query.OrderBy(x => x.SchoolYearValue).ThenBy(x => x.CourseName).ToGridResultSet(pageIndex, pageSize); } public IList GetStudentTeachingMaterialViewList(ConfiguretView configuretView) { var user = CustomPrincipal.Current; var query = TeachingMaterialPoolDAL.GetStudentTeachingMaterialView(x => x.UserID == user.UserID); if (!string.IsNullOrEmpty(configuretView.ConditionValue)) query = query.DynamicWhere(configuretView.Attribute, configuretView.Condition, configuretView.ConditionValue); return query.OrderBy(x => x.SchoolYearValue).ThenBy(x => x.CourseName).ToList(); } public void TeachingMaterialPoolImport(Dictionary cellheader, out int OkCount, out List errdataList, out int successCount, string sourcePhysicalPath) { StringBuilder errorMsg = new StringBuilder(); // 错误信息 List errList = new List(); #region 1.1解析文件,存放到一个List集合里 cellheader.Remove("ErrorMessage");//去除异常列、导入操作不需要 // 1.1解析文件,存放到一个List集合里 List enlist = NpoiExcelHelper.ExcelToEntityList(cellheader, sourcePhysicalPath, out errorMsg, out errList); cellheader.Add("ErrorMessage", "错误信息");//还原字典项 #endregion #region 1.2 将Excel数据写入数据库中 #region 1.2.1 对List集合进行有效性校验 if (enlist.Count() <= 0) { throw new Exception("请填写Excel模板信息数据。"); } #region 1.2.1.1检测必填项是否必填 for (int i = 0; i < enlist.Count; i++) { TeachingMaterialPoolImportView en = enlist[i]; string errorMsgStr = "第" + (i + 1) + "行数据检测异常:"; bool isHaveNoInputValue = false; // 是否含有未输入项 if (string.IsNullOrEmpty(en.TeachingMaterialName)) { errorMsgStr += "教材名称不能为空;"; en.ErrorMessage = errorMsgStr; isHaveNoInputValue = true; } if (string.IsNullOrEmpty(en.CoursematerialName)) { errorMsgStr += "课程名称不能为空;"; en.ErrorMessage = errorMsgStr; isHaveNoInputValue = true; } if (string.IsNullOrEmpty(en.TeachingMaterialCode)) { errorMsgStr += "教材编号不能为空;"; en.ErrorMessage = errorMsgStr; isHaveNoInputValue = true; } if (string.IsNullOrEmpty(en.ISBN)) { errorMsgStr += "ISBN不能为空;"; en.ErrorMessage = errorMsgStr; isHaveNoInputValue = true; } Regex datestr = new Regex("^[0-9]{4}年[0-9]{1,2}月[0-9]{1,3}版[0-9]{1,3}次$"); if (string.IsNullOrEmpty(en.PublishTime)) { errorMsgStr += "版本时间不能为空;"; en.ErrorMessage = errorMsgStr; isHaveNoInputValue = true; } else if (!datestr.IsMatch(en.PublishTime))//时间格式验证 { errorMsgStr += "版本时间格式不正确,应为 xxxx年xx月xx版xx次,请重新输入!"; en.ErrorMessage = errorMsgStr; isHaveNoInputValue = true; } if (string.IsNullOrEmpty(en.PublishName)) { errorMsgStr += "出版单位不能为空;"; en.ErrorMessage = errorMsgStr; isHaveNoInputValue = true; } else { if (!PublishRepository.Entities.Any(x => x.UnitName == en.PublishName)) { errorMsgStr += "该出版单位不存在,请添加后再导入;"; en.ErrorMessage = errorMsgStr; isHaveNoInputValue = true; } } if (string.IsNullOrEmpty(en.Author)) { errorMsgStr += "作者不能为空;"; en.ErrorMessage = errorMsgStr; isHaveNoInputValue = true; } if (en.Price == null) { errorMsgStr += "单价列不能为空;"; en.ErrorMessage = errorMsgStr; isHaveNoInputValue = true; } #region 1.2.1.2 检测(启用状态课程名称唯一性、) //if (CoursematerialRepository.Entities.Where(x => x.IsEnable == true && x.CourseName == en.CoursematerialName).Count() > 1) //{ // errorMsgStr += "该课程名称列对应的课程编码存在多条;"; // en.ErrorMessage = errorMsgStr; // isHaveNoInputValue = true; //} var coursematerialNames = en.CoursematerialName.Split(',').Where(x => !string.IsNullOrEmpty(x)).ToList(); if (CoursematerialRepository.Entities.Where(x => x.IsEnable == true && coursematerialNames.Contains(x.CourseName)).Count() != coursematerialNames.Count) { errorMsgStr += "部分对应的课程不存在或未启用,请添加后再导入;"; en.ErrorMessage = errorMsgStr; isHaveNoInputValue = true; } if (TeachingMaterialPoolRepository.Entities.Any(x => x.TeachingMaterialCode == en.TeachingMaterialCode)) { errorMsgStr += "教材编号已存在;"; en.ErrorMessage = errorMsgStr; isHaveNoInputValue = true; } if (TeachingMaterialPoolRepository.Entities.Any(x => x.ISBN == en.ISBN)) { errorMsgStr += "ISBN已存在;"; en.ErrorMessage = errorMsgStr; isHaveNoInputValue = true; } if (!string.IsNullOrEmpty(en.WnningLevelName)) { if (!IdNameExt.GetDictionaryItem(DictionaryItem.CF_WnningLevel.ToString()) .Any(x => x.Name == en.WnningLevelName)) { errorMsgStr += "获奖等级不存在;"; en.ErrorMessage = errorMsgStr; isHaveNoInputValue = true; } } if (!string.IsNullOrEmpty(en.TeachingMaterialTypeName)) { if (!IdNameExt.GetDictionaryItem(DictionaryItem.CF_TeachingMaterialType.ToString()) .Any(x => x.Name == en.TeachingMaterialTypeName)) { errorMsgStr += "该教材类型不存在,请添加后再导入;"; en.ErrorMessage = errorMsgStr; isHaveNoInputValue = true; } } #endregion if (isHaveNoInputValue) // 若必填项有值未填 { en.IsExcelVaildateOK = false; en.ErrorMessage = errorMsgStr; errList.Add(en); errorMsg.AppendLine(errorMsgStr); } } #endregion // TODO:其他检测 #region 1.2.1.3 循环写入验证成功的数据 List tpList = new List(); var coursematerialNameList = enlist.SelectMany(x => x.CoursematerialName.Split(',').Where(w => !string.IsNullOrEmpty(w)).ToList()).Distinct().ToList(); var coursematerialEntityList = CoursematerialRepository.GetList(x => x.IsEnable == true && coursematerialNameList.Contains(x.CourseName)).ToList(); var publishNameList = enlist.Select(x => x.PublishName).Distinct().ToList(); var publishEntityList = PublishRepository.GetList(x => publishNameList.Contains(x.UnitName)).ToList(); for (int i = 0; i < enlist.Count; i++) { TeachingMaterialPoolImportView enA = enlist[i]; if (enA.IsExcelVaildateOK == false) // 上面验证不通过,不进行此步验证 { continue; } var curUser = EMIS.Utility.FormValidate.CustomPrincipal.Current; CF_Publish publishEntity = publishEntityList.Where(x => x.UnitName == enA.PublishName).FirstOrDefault(); var curCoursematerialNameList = enA.CoursematerialName.Split('.').Where(w => !string.IsNullOrEmpty(w)).ToList(); //EM_Coursematerial coursematerialEntity = CoursematerialRepository.Entities.Where(x => x.IsEnable == true && x.CourseName == enA.CoursematerialName).FirstOrDefault(); CF_TeachingMaterialPool teachingMaterialPool = new CF_TeachingMaterialPool() { TeachingMaterialPoolID = Guid.NewGuid(), WnningLevel = enA.WnningLevelName == null ? -1 : IdNameExt.GetDictionaryItem(DictionaryItem.CF_WnningLevel.ToString()) .Where(x => x.Name == enA.WnningLevelName).FirstOrDefault().Value, PublishID = publishEntity.PublishID, Author = enA.Author.Trim(), ISBN = enA.ISBN.Trim(), IsLate = false, PublishTime = enA.PublishTime, TeachingMaterialCode = enA.TeachingMaterialCode, TeachingMaterialName = enA.TeachingMaterialName, TeachingMaterialShorName = enA.TeachingMaterialName, TeachingMaterialTypeID = enA.TeachingMaterialTypeName == null ? -1 : IdNameExt.GetDictionaryItem(DictionaryItem.CF_TeachingMaterialType.ToString()) .Where(x => x.Name == enA.TeachingMaterialTypeName) .FirstOrDefault().Value, Price = enA.Price == null ? 0 : enA.Price, EM_Coursematerial = new HashSet(coursematerialEntityList.Where(x => curCoursematerialNameList.Contains(x.CourseName)).ToList()), Desc = enA.Desc, CreateTime = DateTime.Now, CreateUserID = curUser.UserID, MinInventory = 1,//最小库存量默认1 ModifyUserID = curUser.UserID, ModifyTime = DateTime.Now }; tpList.Add(teachingMaterialPool); } #endregion UnitOfWork.BulkInsert(tpList);//统一写入 #endregion #endregion #region 1.3 返回各项数据值 successCount = enlist.Distinct().Count() - errList.Distinct().Count();//共条数减去失败条数 errdataList = errList.Distinct().ToList(); OkCount = errList.Distinct().Count(); #endregion } public string GetLastTeachingMaterialCode() { List codeList = TeachingMaterialPoolRepository.Entities.Select(x => x.TeachingMaterialCode).ToList(); string pattern = "^[0-9]*$"; int newcodeInt = 1; List newcodeList = new List(); foreach (string code in codeList) { if (Regex.IsMatch(code, pattern)) { newcodeList.Add(code); } } newcodeList.OrderByDescending(x => x.ToString().Length).ThenByDescending(x => x.ToString()); string newcode = newcodeList.OrderByDescending(x => x.ToString().Length).ThenByDescending(x => x.ToString()).FirstOrDefault(); if (newcode != "" && newcode != String.Empty && newcode != null) { newcodeInt = int.Parse(newcode) + 1; } return (newcodeInt).ToString(); } } }