using System; using System.Collections.Generic; using System.Linq; using System.Linq.Expressions; using Bowin.Common.Linq; using Bowin.Common.Linq.Entity; using EMIS.Entities; using EMIS.ViewModel; using EMIS.DataLogic.ExaminationApply; using EMIS.ViewModel.ExaminationApply; using EMIS.ViewModel.Students; using EMIS.DataLogic.Common.Students; using Spire.Barcode; using System.Drawing; using EMIS.Utility; using System.Web; using System.IO; using System.Text; using Bowin.Common.Utility; using System.Web.Mvc; using EMIS.DataLogic.ExamManage; using System.Data; using EMIS.ViewModel.SystemView; using EMIS.CommonLogic.SystemServices; namespace EMIS.CommonLogic.ExaminationApply { public class ExaminationSubjectServices : BaseServices, IExaminationSubjectServices { public ExaminationSubjectDAL ExaminationSubjectDAL { get; set; } public StudentsDAL studentsDAL { get; set; } public ProjectFeeDAL ProjectFeeDAL { get; set; } public ExaminationBatchDAL ExaminationBatchDAL { get; set; } public Lazy SerialNumberServices { get; set; } public IGridResultSet GetStudentListViewGrid(ConfiguretView examinationSubjectView, Guid? schoolYearID, Guid? examinationBatchID, Guid? examinationTypeID, Guid? examinationProjectID, int? campusID, Guid? collegeID, int? year, Guid? classmajorID, int? recordStatus, int? isConfirm, int pageIndex, int pageSize) { Expression> erexp = x => true; Expression> epexp = x => true; Expression> ebpexp = x => true; Expression> stuexp = x => true; if (schoolYearID.HasValue) { ebpexp = ebpexp.And(x => x.EX_ExaminationBatch.SchoolyearID == schoolYearID); } if (examinationBatchID.HasValue) { ebpexp = ebpexp.And(x => x.ExaminationBatchID == examinationBatchID); } if (examinationTypeID.HasValue) { epexp = epexp.And(x => x.ExaminationTypeID == examinationTypeID); } if (examinationProjectID.HasValue) { epexp = epexp.And(x => x.ExaminationProjectID == examinationProjectID); } if (campusID.HasValue) { stuexp = stuexp.And(x => x.CF_Classmajor.CF_Grademajor.SchoolAreaID == campusID); } if (collegeID.HasValue) { stuexp = stuexp.And(x => x.CF_Classmajor.CF_Grademajor.CF_Facultymajor.CollegeID == collegeID); } if (year.HasValue) { stuexp = stuexp.And(x => x.CF_Classmajor.CF_Grademajor.SchoolyearID == year); } if (classmajorID.HasValue) { stuexp = stuexp.And(x => x.ClassmajorID == classmajorID); } if (recordStatus.HasValue) { erexp = erexp.And(x => x.RecordStatus == recordStatus); } var q = ExaminationSubjectDAL.GetStudentListView(erexp, epexp, ebpexp, stuexp); if (isConfirm.HasValue) { q = q.Where(x => isConfirm == 1 ? (x.ExaminationRegistrationConfirmID != null) : (x.ExaminationRegistrationConfirmID == null)); } if (!string.IsNullOrEmpty(examinationSubjectView.ConditionValue) && !string.IsNullOrEmpty(examinationSubjectView.Attribute)) q = q.DynamicWhere(examinationSubjectView.Attribute, examinationSubjectView.Condition, examinationSubjectView.ConditionValue); return q.OrderBy(x => x.ExaminationTypeName).ThenBy(x => x.ExaminationProjectName).ThenBy(x => x.FeeTypeID).ThenBy(x => x.UserName).ToGridResultSet(pageIndex, pageSize); } public IGridResultSet GetMyexamListView(ConfiguretView examinationSubjectView,Guid UserID, int pageIndex, int pageSize) { Expression> erexp = x => true; Expression> epexp = x => true; Expression> ebpexp = x => true; Expression> stuexp = x => true; erexp = erexp.And(x => x.UserID == UserID); var q = ExaminationSubjectDAL.GetStudentListView(erexp, epexp, ebpexp, stuexp); if (!string.IsNullOrEmpty(examinationSubjectView.ConditionValue) && !string.IsNullOrEmpty(examinationSubjectView.Attribute)) q = q.DynamicWhere(examinationSubjectView.Attribute, examinationSubjectView.Condition, examinationSubjectView.ConditionValue); return q.OrderByDescending(x => x.SchoolyearCode).ThenBy(x => x.ExaminationProjectName).ThenBy(x => x.ExaminationTypeName).ThenBy(x => x.FeeTypeID). ThenBy(x => x.ClassmajorName).ThenByDescending(x => x.StudentNo).ToGridResultSet(pageIndex, pageSize); } public List GetStudentListViewForExport(ConfiguretView examinationSubjectView, Guid? schoolYearID, Guid? examinationBatchID, Guid? examinationTypeID, Guid? examinationProjectID, int? campusID, Guid? collegeID, int? year, Guid? classmajorID, int? recordStatus, int? isConfirm, List idList) { Expression> erexp = x => true; Expression> epexp = x => true; Expression> ebpexp = x => true; Expression> stuexp = x => true; if (schoolYearID.HasValue) { ebpexp = ebpexp.And(x => x.EX_ExaminationBatch.SchoolyearID == schoolYearID); } if (examinationBatchID.HasValue) { ebpexp = ebpexp.And(x => x.ExaminationBatchID == examinationBatchID); } if (examinationTypeID.HasValue) { epexp = epexp.And(x => x.ExaminationTypeID == examinationTypeID); } if (examinationProjectID.HasValue) { epexp = epexp.And(x => x.ExaminationProjectID == examinationProjectID); } if (campusID.HasValue) { stuexp = stuexp.And(x => x.CF_Classmajor.CF_Grademajor.SchoolAreaID == campusID); } if (collegeID.HasValue) { stuexp = stuexp.And(x => x.CF_Classmajor.CF_Grademajor.CF_Facultymajor.CollegeID == collegeID); } if (year.HasValue) { stuexp = stuexp.And(x => x.CF_Classmajor.CF_Grademajor.SchoolyearID == year); } if (classmajorID.HasValue) { stuexp = stuexp.And(x => x.ClassmajorID == classmajorID); } if (recordStatus.HasValue) { erexp = erexp.And(x => x.RecordStatus == recordStatus); } if (idList.Count > 0) { erexp = erexp.And(x => idList.Contains(x.ExaminationRegistrationID)); } var q = ExaminationSubjectDAL.GetStudentListView(erexp, epexp, ebpexp, stuexp); if (isConfirm.HasValue) { q = q.Where(x => isConfirm == 1 ? (x.ExaminationRegistrationConfirmID != null) : (x.ExaminationRegistrationConfirmID == null)); } if (!string.IsNullOrEmpty(examinationSubjectView.ConditionValue) && !string.IsNullOrEmpty(examinationSubjectView.Attribute)) q = q.DynamicWhere(examinationSubjectView.Attribute, examinationSubjectView.Condition, examinationSubjectView.ConditionValue); return q.OrderByDescending(x => x.SchoolyearCode).ThenBy(x => x.ExaminationProjectName).ThenBy(x => x.ExaminationTypeName).ThenBy(x => x.FeeTypeID). ThenBy(x => x.ClassmajorName).ThenByDescending(x => x.StudentNo).ToList(); } public StudentListView GetStudentListView(Guid? examinationRegistrationID) { try { var studentListView = ExaminationSubjectDAL.GetStudentListView(x => x.ExaminationRegistrationID == examinationRegistrationID, x => true, x => true, x => true) .SingleOrDefault(); return studentListView; } catch (Exception ex) { throw new Exception(ex.Message); } } public void AddSave(StudentListView studentListView, List studentsViewList) { try { var ExaminationProjectFeeType = ExaminationSubjectDAL.examinationProjectFeeTypeRepository.GetSingle(x => x.ExaminationProjectFeeID == studentListView.ExaminationProjectFeeID && x.FeeTypeID == studentListView.FeeTypeID); if (ExaminationProjectFeeType == null) { throw new Exception("找不到收费标准对应的收费项,请先设置。"); } List RegistrationList = new List(); var ExaminationBatchProject = ExaminationSubjectDAL.examinationBatchProjectRepository.GetList(x => x.ExaminationProjectID == studentListView.ExaminationProjectID && x.ExaminationBatchID == studentListView.ExaminationBatchID).FirstOrDefault(); var userIDList = studentsViewList.Select(x => x.UserID).ToList(); var dbstudentRegistration = ExaminationSubjectDAL.examinationRegistrationRepository.GetList(x => x.ExaminationProjectFeeTypeID == ExaminationProjectFeeType.ExaminationProjectFeeTypeID && x.ExaminationBatchProjectID == ExaminationBatchProject.ExaminationBatchProjectID && userIDList.Contains(x.UserID.Value) && x.RecordStatus != (int)EX_ExaminationRegistrationStatus.Refunded).FirstOrDefault(); if (dbstudentRegistration != null) { throw new Exception("添加的学生中有已报名的学生。"); } foreach (var student in studentsViewList) { EX_ExaminationRegistration examinationRegistration = new EX_ExaminationRegistration(); examinationRegistration.ExaminationBatchProjectID = ExaminationBatchProject.ExaminationBatchProjectID; examinationRegistration.ExaminationProjectFeeTypeID = ExaminationProjectFeeType.ExaminationProjectFeeTypeID; examinationRegistration.ExaminationRegistrationConfirmID = studentListView.ExaminationRegistrationConfirmID; //examinationRegistration.SchoolyearID = studentListView.SchoolyearID; examinationRegistration.Fee = ExaminationProjectFeeType.Fee; examinationRegistration.Remark = studentListView.Remark; examinationRegistration.ExaminationRegistrationID = Guid.NewGuid(); examinationRegistration.UserID = student.UserID; this.SetNewStatus(examinationRegistration); RegistrationList.Add(examinationRegistration); } UnitOfWork.BulkInsert(RegistrationList); } catch (Exception) { throw; } } public void EditSave(StudentListView studentListView) { var ExaminationProjectFeeType = ExaminationSubjectDAL.examinationProjectFeeTypeRepository.GetSingle(x => x.ExaminationProjectFeeID == studentListView.ExaminationProjectFeeID && x.FeeTypeID == studentListView.FeeTypeID); if (ExaminationProjectFeeType == null) { throw new Exception("找不到收费标准对应的收费项,请先设置。"); } var dbstudentRegistration = ExaminationSubjectDAL.examinationRegistrationRepository.GetSingle(x => x.ExaminationRegistrationID == studentListView.ExaminationRegistrationID); if (dbstudentRegistration == null) { throw new Exception("该报名信息不存在。"); } else { dbstudentRegistration.ExaminationProjectFeeTypeID = ExaminationProjectFeeType.ExaminationProjectFeeTypeID; dbstudentRegistration.Fee = ExaminationProjectFeeType.Fee; dbstudentRegistration.Remark = studentListView.Remark; } UnitOfWork.Commit(); } public IGridResultSet GetStudentViewList(ConfiguretView examinationSubjectView, Guid? examinationRegistrationID, int pageIndex, int pageSize) { Expression> filter = x => true; var inSchoolStatusIDList = studentsDAL.InSchoolSettingRepository.GetList(x => x.IsSelected == true).Select(w => w.InSchoolStatusID).ToList(); filter = filter.And(x => inSchoolStatusIDList.Contains(x.InSchoolStatusID)); var query = ExaminationSubjectDAL.GetStudentListView(x => x.ExaminationRegistrationID == examinationRegistrationID, x => true, x => true, x => true); return query.OrderByDescending(x => x.StudentNo).ToGridResultSet(pageIndex, pageSize); } public IGridResultSet GetStudentListForSelect(ConfiguretView configuretView, Guid? examinationRegistrationID, Guid? examinationBatchID, Guid? examinationProjectID, int? campusID, Guid? collegeID, Guid? grademajorID, int? year, int? standardID, Guid? classmajorID, int pageIndex, int pageSize) { var examinationBatchProject = ExaminationSubjectDAL.examinationBatchProjectRepository.Entities.Where(x => x.ExaminationBatchID == examinationBatchID && x.ExaminationProjectID == examinationProjectID).FirstOrDefault(); List studentUserIDList = new List(); if (examinationBatchProject != null) { var dbStudentList = ExaminationSubjectDAL.examinationRegistrationRepository.Entities.Where(x => x.ExaminationBatchProjectID == examinationBatchProject.ExaminationBatchProjectID && x.RecordStatus != (int)EX_ExaminationRegistrationStatus.Refunded).ToList(); studentUserIDList = dbStudentList.Select(x => x.UserID).ToList(); } Expression> exp = x => true; if (campusID.HasValue) { exp = exp.And(x => x.CF_Classmajor.CF_Grademajor.SchoolAreaID == campusID); } if (collegeID.HasValue) { exp = exp.And(x => x.CF_Classmajor.CF_Grademajor.CF_Facultymajor.CollegeID == collegeID); } if (grademajorID.HasValue) { exp = exp.And(x => x.CF_Classmajor.GrademajorID == grademajorID); } if(year.HasValue) { exp = exp.And(x => x.CF_Classmajor.CF_Grademajor.SchoolyearID == year); } if (standardID.HasValue) { exp = exp.And(x => x.CF_Classmajor.CF_Grademajor.CF_Facultymajor.StandardID == standardID); } if (classmajorID.HasValue) { exp = exp.And(x => x.ClassmajorID == classmajorID); } //if (studentUserIDList.Count > 0) //{ // exp = exp.And(x => !studentUserIDList.Contains(x.UserID)); //} var query = studentsDAL.GetInSchoolStudentViewQueryable(exp); if (!string.IsNullOrEmpty(configuretView.ConditionValue) && !string.IsNullOrEmpty(configuretView.Attribute)) query = query.DynamicWhere(configuretView.Attribute, configuretView.Condition, configuretView.ConditionValue); var result = query.ToList(); if (studentUserIDList.Count > 0) { result = query.ToList().Where(x => !studentUserIDList.Contains(x.UserID)).ToList(); } return result.AsQueryable().OrderBy(x => x.CollegeName).ThenByDescending(x => x.Years).ThenBy(x => x.ClassmajorName).ThenBy(x => x.UserName).ToGridResultSet(pageIndex, pageSize); //return null; } public void Delete(List examinationRegistrationIDLsit) { try { var dbExaminationRegistrationList = ExaminationSubjectDAL.GetStudentListView(x => examinationRegistrationIDLsit.Contains(x.ExaminationRegistrationID), x => true, x => true, x => true).ToList(); if (dbExaminationRegistrationList.Any(x => x.RecordStatus > (int)EX_ExaminationRegistrationStatus.ForPay)) { throw new Exception("不是“待缴费”状态的数据不能删除"); } if (dbExaminationRegistrationList.Any(x => x.ExaminationRegistrationConfirmID != null)) { throw new Exception("已确认的数据不能删除"); } UnitOfWork.Delete(x => examinationRegistrationIDLsit.Contains(x.ExaminationRegistrationID)); } catch (Exception ex) { throw ex; } } public void ChangeFee(List examinationRegistrationIDLsit, decimal? Fee, string Remark) { try { var examinationRegistrationList = ExaminationSubjectDAL.examinationRegistrationRepository.GetList(x => examinationRegistrationIDLsit.Contains(x.ExaminationRegistrationID)).ToList(); examinationRegistrationList.ForEach(x => { if (x.RecordStatus != (int)EX_ExaminationRegistrationStatus.ForPay) { throw new Exception("只有待缴费的数据才能调整费用"); } }); examinationRegistrationList.ForEach(x => { x.Fee = Fee; x.Remark = Remark; }); UnitOfWork.BatchUpdate(examinationRegistrationList); } catch (Exception) { throw; } } public Guid? Confirm(List examinationRegistrationIDLsit, string filePathUrl) { try { var dbExaminationRegistrationList = ExaminationSubjectDAL.GetStudentListView(x => examinationRegistrationIDLsit.Contains(x.ExaminationRegistrationID), x => true, x => true, x => true).ToList(); if (dbExaminationRegistrationList.Any(x => x.RecordStatus != (int)EX_ExaminationRegistrationStatus.ForPay)) { throw new Exception("只能选择待缴费状态的数据进行打包"); } if (dbExaminationRegistrationList.Any(x => x.ExaminationRegistrationConfirmID != null)) { throw new Exception("选择的数据存在已确认数据,如需要重新打包请先取消打包"); } //var confirmNo = DateTime.Now.ToString("yyyyMMdd"); //var dbRegistrationList = ExaminationSubjectDAL.examinationRegistrationRepository.GetList(x => x.ExaminationRegistrationConfirmID != null).ToList(); //var confirmIDList = dbRegistrationList.Select(x => x.ExaminationRegistrationConfirmID).ToList(); //var dbConfirmList = ExaminationSubjectDAL.examinationRegistrationConfirmRepository.GetList(x => confirmIDList.Contains(x.ExaminationRegistrationConfirmID)); //var todayNoList = dbConfirmList.Where(x => x.No.Contains(confirmNo)).Select(x => x.No).ToList(); //var maxNo = 0; //if (todayNoList.Count > 0) //{ // foreach (var noStr in todayNoList) // { // var no = int.Parse(noStr.Substring(noStr.Length - 4, 4)); // if (no > maxNo) // { // maxNo = no; // } // } //} //maxNo = maxNo + 1; //confirmNo = confirmNo + maxNo.ToString("0000"); var confirmNo = SerialNumberServices.Value.SetDialySN(Const.EX_REGISTCONFIRM_NO_PREFIX, 4) .Replace(Const.EX_REGISTCONFIRM_NO_PREFIX, DateTime.Today.ToString("yyyyMMdd")); //string filePath = Const.LOCAL_SETTING_UPLOAD_FILEPATH; //string physicalPath = HttpContext.Current.Server.MapPath(filePath); EX_ExaminationRegistrationConfirm confirm = new EX_ExaminationRegistrationConfirm(); confirm.ExaminationRegistrationConfirmID = Guid.NewGuid(); confirm.No = confirmNo; confirm.BarcodeUrl = GenerateBarCodeByConfirm(confirmNo, filePathUrl); SetNewStatus(confirm); var examinationRegistrationList = ExaminationSubjectDAL.examinationRegistrationRepository.GetList(x => examinationRegistrationIDLsit.Contains(x.ExaminationRegistrationID)).ToList(); examinationRegistrationList.ForEach(x => { x.ExaminationRegistrationConfirmID = confirm.ExaminationRegistrationConfirmID; }); UnitOfWork.Add(confirm); UnitOfWork.Commit(); UnitOfWork.BatchUpdate(examinationRegistrationList); return confirm.ExaminationRegistrationConfirmID; } catch (Exception) { throw; } } public void CancelConfirm(List examinationRegistrationIDLsit) { try { var registrationList = ExaminationSubjectDAL.examinationRegistrationRepository.GetList(x => examinationRegistrationIDLsit.Contains(x.ExaminationRegistrationID)).ToList(); if (registrationList.Where(x => x.RecordStatus == (int)EX_ExaminationRegistrationStatus.Paid || x.RecordStatus == (int)EX_ExaminationRegistrationStatus.Refunding).Count() > 0) { throw new Exception("选中的数据有已缴费状态的,无法取消确认"); } var confirmIDList = registrationList.Select(x => x.ExaminationRegistrationConfirmID).ToList(); var cancelRegistrationList = ExaminationSubjectDAL.examinationRegistrationRepository.GetList(x => confirmIDList.Contains(x.ExaminationRegistrationConfirmID)).ToList(); cancelRegistrationList.ForEach(x => x.ExaminationRegistrationConfirmID = null); UnitOfWork.BatchUpdate(cancelRegistrationList); UnitOfWork.Delete(x => confirmIDList.Contains(x.ExaminationRegistrationConfirmID)); } catch (Exception) { throw; } } public IList GetStudentViewListByIDList(IList registrationIDList) { return ExaminationSubjectDAL.GetStudentQueryableByRegistration(x => registrationIDList.Contains(x.ExaminationRegistrationID)).ToList(); } public IList GetStudentListByCondition(ConfiguretView configuretView, Guid? schoolYearID, Guid? examinationBatchID, Guid? examinationTypeID, Guid? examinationProjectID, int? campusID, Guid? collegeID, int? year, Guid? classmajorID, int? recordStatus, int? isConfirm) { Expression> erexp = x => true; Expression> epexp = x => true; Expression> ebpexp = x => true; Expression> stuexp = x => true; if (schoolYearID.HasValue) { ebpexp = ebpexp.And(x => x.EX_ExaminationBatch.SchoolyearID == schoolYearID); } if (examinationBatchID.HasValue) { ebpexp = ebpexp.And(x => x.ExaminationBatchID == examinationBatchID); } if (examinationTypeID.HasValue) { epexp = epexp.And(x => x.ExaminationTypeID == examinationTypeID); } if (examinationProjectID.HasValue) { epexp = epexp.And(x => x.ExaminationProjectID == examinationProjectID); } if (campusID.HasValue) { stuexp = stuexp.And(x => x.CF_Classmajor.CF_Grademajor.SchoolAreaID == campusID); } if (collegeID.HasValue) { stuexp = stuexp.And(x => x.CF_Classmajor.CF_Grademajor.CF_Facultymajor.CollegeID == collegeID); } if (year.HasValue) { stuexp = stuexp.And(x => x.CF_Classmajor.CF_Grademajor.SchoolyearID == year); } if (classmajorID.HasValue) { stuexp = stuexp.And(x => x.ClassmajorID == classmajorID); } if (recordStatus.HasValue) { erexp = erexp.And(x => x.RecordStatus == recordStatus); } var q = ExaminationSubjectDAL.GetStudentListView(erexp, epexp, ebpexp, stuexp); if (!string.IsNullOrEmpty(configuretView.ConditionValue)) q = q.DynamicWhere(configuretView.Attribute, configuretView.Condition, configuretView.ConditionValue); var idList = q.Select(x => x.ExaminationRegistrationID).ToList(); return ExaminationSubjectDAL.GetStudentQueryableByRegistration(x => idList.Contains(x.ExaminationRegistrationID)).ToList(); } public string GenerateBarCodeByConfirm(string confirmNo, string filePathUrl) { BarcodeSettings bs = new BarcodeSettings() { Data = confirmNo, ShowCheckSumChar = false, TopTextColor = Color.Red, ShowTopText = false, ShowTextOnBottom = true }; BarcodeSettings.ApplyKey("JY1YX-2SPIQ-9H8DV-H2IPO-3RPI2"); //Generate the barcode based on the this.barCodeControl1 BarCodeGenerator generator = new BarCodeGenerator(bs); Image barcode = generator.GenerateImage(); string filePath = Const.LOCAL_SETTING_UPLOAD_FILEPATH.EndsWith("\\") ? Const.LOCAL_SETTING_UPLOAD_FILEPATH : Const.LOCAL_SETTING_UPLOAD_FILEPATH + "\\"; filePath = filePath + Const.LOCAL_SETTING_BARCODEPATH + "\\"; string physicalPath = HttpContext.Current.Server.MapPath(filePath); if (!Directory.Exists(physicalPath)) { Directory.CreateDirectory(physicalPath); } string url = HttpContext.Current.Request.Url.Scheme + "://" + HttpContext.Current.Request.Url.Authority; string savePath = url + filePathUrl + "/" + Const.LOCAL_SETTING_BARCODEPATH + "/" + confirmNo + ".png"; string tagetPhysicalPath = (physicalPath.EndsWith("\\") ? physicalPath : physicalPath + "\\") + confirmNo + ".png"; //save the barcode as an image barcode.Save(@tagetPhysicalPath); return savePath; } public ExcelImportResult StudentImport(Dictionary cellheader, out int OkCount, out List errdataList, out int ErrCount, string sourcePhysicalPath, Guid? schoolYearID, Guid? examinationBatchID, Guid? examinationTypeID, Guid? examinationProjectID, Guid? examinationProjectFeeID, int? feeTypeID) { //获取要报考的批次项目和项目收费标准对应收费项 var examinationBatchProject = ExaminationSubjectDAL.examinationBatchProjectRepository.Entities.Where(x => x.ExaminationBatchID == examinationBatchID && x.ExaminationProjectID == examinationProjectID).FirstOrDefault(); //var examinationProjectFeeType = ExaminationSubjectDAL.examinationProjectFeeTypeRepository.Entities.Where(x => x.ExaminationProjectFeeID == examinationProjectFeeID && x.FeeTypeID == feeTypeID).FirstOrDefault(); //获取要报考的批次项目和项目收费标准对应收费项对应的已报名名单 var studentRegistrationList = ExaminationSubjectDAL.examinationRegistrationRepository.Entities.Where(x => x.ExaminationBatchProjectID == examinationBatchProject.ExaminationBatchProjectID).ToList(); StringBuilder errorMsg = new StringBuilder(); // 错误信息 var result = new ExcelImportResult(); var excel = new NpoiExcelHelper(); var errorTable = new DataTable(); List errList = new List(); cellheader.Remove("ErrorMessage");//去除异常列、导入操作不需要 // 1.1解析文件,存放到一个List集合里 List enlist = NpoiExcelHelper.ExcelToEntityList(cellheader, sourcePhysicalPath, out errorMsg, out errList); Expression> filter = x => true; List insertList = new List(); var inSchoolStatusIDList = studentsDAL.InSchoolSettingRepository.GetList(x => x.IsSelected == true).Select(w => w.InSchoolStatusID).ToList(); //filter = filter.And(x => inSchoolStatusIDList.Contains(x.InSchoolStatusID));获取所有学生,不排除是否在校 var studentList = studentsDAL.GetInSchoolStudentViewQueryable(filter).ToList(); cellheader.Add("ErrorMessage", "错误信息");//还原字典项 cellheader.ForEach(x => errorTable.Columns.Add(x.Value)); if (enlist.Count() <= 0) { throw new Exception("请填写Excel模板信息数据。"); } for (int i = 0; i < enlist.Count(); i++) { string errorMsgStr = "第" + (i + 2) + "行数据检测异常:"; StudentListView en = enlist[i]; bool isHaveNoInputValue = false; en.IsExcelVaildateOK = true; #region 检查是否有未填的值 if (string.IsNullOrEmpty(en.IDNumber)) { errorMsgStr += "证件号不能为空;"; isHaveNoInputValue = true; } #endregion #region 检查数据是否存在 if (!studentList.Any(x => x.IDNumber == en.IDNumber)) { errorMsgStr += "学生不存在;"; isHaveNoInputValue = true; } else { var student = studentList.FirstOrDefault(x => x.IDNumber == en.IDNumber); if (studentRegistrationList.Any(x => x.UserID == student.UserID)) { errorMsgStr += "学生已报名该科目;"; isHaveNoInputValue = true; } } #endregion if (isHaveNoInputValue) { en.IsExcelVaildateOK = false; en.ErrorMessage = errorMsgStr; errList.Add(en); } else { //enlist[i].IsExcelVaildateOK = true; var student = studentList.FirstOrDefault(x => x.IDNumber == en.IDNumber); if(student != null) { insertList.Add(student); } } } ErrCount = errList.Distinct().Count(); errdataList = errList.Distinct().ToList(); OkCount = enlist.Distinct().Count() - errList.Distinct().Count(); result.SuccessCount = OkCount; result.ErrorCount = ErrCount; if (ErrCount > 0) { errList.ForEach(x => errorTable.Rows.Add(x)); result.IsAllSuccess = false; var errorCellHeader = cellheader; result.Data = insertList; result.ErrorFilePath = this.WriteErrorFile(excel, cellheader.Values, errorTable); result.Message = string.Format("导入失败,详情请点击错误数据下载查看。"); } else { result.IsAllSuccess = true; result.Data = insertList; } return result; } private string WriteErrorFile(NpoiExcelHelper excel, IEnumerable errorCellheader, DataTable errorTable) { var errorExcelBuff = excel.ExportMemoryStream(errorTable, "", errorCellheader.ToArray()); var logicalPath = Const.LOCAL_SETTING_UPLOAD_FILEPATH + "/" + Guid.NewGuid().ToString() + ".xls"; var physicalPath = HttpContext.Current.Server.MapPath(logicalPath); var fs = File.Create(physicalPath); fs.Write(errorExcelBuff, 0, errorExcelBuff.Length); fs.Flush(); fs.Close(); return UrlHelper.GenerateContentUrl(logicalPath, HttpContext.Current.Request.RequestContext.HttpContext); } /// /// 获取项目下拉(根据批次,考试类型) /// /// /// /// /// /// /// public IGridResultSet GetExaminationProjectViewGridForComboGrid(ConfiguretView configuretView, Guid? examinationBatchID, Guid? examinationTypeID, int pageIndex, int pageSize) { Expression> expCampus = (x => x.RecordStatus > (int)SYS_STATUS.UNUSABLE); if (examinationBatchID.HasValue) { expCampus = expCampus.And(x => x.EX_ExaminationBatchProject.Any(y => y.ExaminationBatchID == examinationBatchID)); } if (examinationTypeID.HasValue) { expCampus = expCampus.And(x => x.ExaminationTypeID == examinationTypeID); } var query = ProjectFeeDAL.GetProectListViewGridForSelect(expCampus).Distinct(); if (!string.IsNullOrEmpty(configuretView.ConditionValue)) { query = query.DynamicWhere(configuretView.Attribute, configuretView.Condition, configuretView.ConditionValue.Trim()); } return query.OrderBy(x => x.ExaminationProjectID).ThenBy(x => x.ExaminationLevelID) .ToGridResultSet(pageIndex, pageSize); } /// /// 获取项目收费标准下拉(根据项目) /// /// /// /// /// /// public IGridResultSet GetProjectFeeTypeViewForComboGrid(ConfiguretView configuretView, Guid? examinationBatchID, Guid? examinationProjectID, int pageIndex, int pageSize) { Expression> expebpc = (x => x.RecordStatus > (int)SYS_STATUS.UNUSABLE); var ExaminationBatchProject = ExaminationSubjectDAL.examinationBatchProjectRepository.Entities.Where(x => x.ExaminationBatchID == examinationBatchID && x.ExaminationProjectID == examinationProjectID).FirstOrDefault(); var curUser = EMIS.Utility.FormValidate.CustomPrincipal.Current; if (ExaminationBatchProject != null) { expebpc = expebpc.And(x => x.ExaminationBatchProjectID == ExaminationBatchProject.ExaminationBatchProjectID); } if (curUser != null && curUser.IsStudent) { var student = studentsDAL.GetStudentQueryable(x => x.UserID == curUser.UserID, x => true, x => true).FirstOrDefault(); expebpc = expebpc.And(x => x.SchoolyearID == student.Years && x.StandardID == student.StandardID); } var query = ProjectFeeDAL.GerProjectFeeByProjectAndYearStandard(expebpc); if (!string.IsNullOrEmpty(configuretView.ConditionValue)) { query = query.DynamicWhere(configuretView.Attribute, configuretView.Condition, configuretView.ConditionValue.Trim()); } return query.Distinct().OrderBy(x => x.ExaminationProjectID).ToGridResultSet(pageIndex, pageSize); } public IGridResultSet GetProjectFeeTypeViewByBaseForComboGrid(ConfiguretView configuretView, Guid? examinationBatchID, Guid? examinationProjectID, int pageIndex, int pageSize) { Expression> expFee = (x => x.RecordStatus > (int)SYS_STATUS.UNUSABLE); var ExaminationBatchProject = ExaminationSubjectDAL.examinationBatchProjectRepository.Entities.Where(x => x.ExaminationBatchID == examinationBatchID && x.ExaminationProjectID == examinationProjectID).FirstOrDefault(); var curUser = EMIS.Utility.FormValidate.CustomPrincipal.Current; if (examinationProjectID.HasValue) { expFee = expFee.And(x => x.ExaminationProjectID == examinationProjectID); } var query = ProjectFeeDAL.GetProjectFee(expFee); if (!string.IsNullOrEmpty(configuretView.ConditionValue)) { query = query.DynamicWhere(configuretView.Attribute, configuretView.Condition, configuretView.ConditionValue.Trim()); } return query.OrderBy(x => x.ProjectName).ThenBy(x => x.Name).ToGridResultSet(pageIndex, pageSize); } /// /// 获取收费项下拉(根据项目收费标准) /// /// /// /// /// /// public IGridResultSet GetFeeTypeViewForComboGrid(ConfiguretView configuretView, Guid? examinationProjectFeeID, int pageIndex, int pageSize) { var examinationProjectFeeTypeList = ExaminationSubjectDAL.examinationProjectFeeTypeRepository.GetList(x => x.ExaminationProjectFeeID == examinationProjectFeeID && x.RecordStatus > (int)SYS_STATUS.UNUSABLE); List feeTypeID = examinationProjectFeeTypeList.Select(x => x.FeeTypeID).Distinct().ToList(); var query = from a in ExaminationSubjectDAL.dictionaryItemRepository.GetList(x => x.DictionaryCode == "EX_ExaminationProjectFeeType" && feeTypeID.Contains(x.Value)) from epftr in ExaminationSubjectDAL.examinationProjectFeeTypeRepository.Entities.Where(x => x.ExaminationProjectFeeID == examinationProjectFeeID && x.RecordStatus > (int)SYS_STATUS.UNUSABLE && x.FeeTypeID == a.Value).DefaultIfEmpty() select new DictionaryItemView { DictionaryItemID = a.DictionaryItemID, DictionaryCode = a.DictionaryCode, Code = a.Code, DictionaryName = a.Sys_Dictionary.Name, Value = a.Value, IsEditable = a.IsEditable.Value, IsVisible = a.RecordStatus > 0, Name = a.Name + "(" + epftr.Fee.ToString() + ")", OrderNo = a.OrderNo, RecordStatus = a.RecordStatus, }; return query.Distinct().OrderBy(x => x.Value).ToGridResultSet(pageIndex, pageSize); } /// /// 获取批次下拉(根据学年学期) /// /// /// /// /// /// public IGridResultSet GetExaminationBatchViewForComboGrid(ConfiguretView configuretView, Guid? schoolyearID, int pageIndex, int pageSize) { Expression> expCampus = (x => x.RecordStatus > (int)EMIS.ViewModel.SYS_STATUS.UNUSABLE); if (schoolyearID.HasValue) { expCampus = expCampus.And(x => x.SchoolyearID == schoolyearID); } var query = ExaminationBatchDAL.GetBatchList(expCampus); if (!string.IsNullOrEmpty(configuretView.ConditionValue)) { query = query.DynamicWhere(configuretView.Attribute, configuretView.Condition, configuretView.ConditionValue.Trim()); } return query.OrderBy(x => x.Name).ToGridResultSet(pageIndex, pageSize); } public List GetStudentListForExportViewGrid(ConfiguretView examinationSubjectView, Guid? schoolYearID, Guid? examinationBatchID, Guid? examinationTypeID, Guid? examinationProjectID, int? campusID, Guid? collegeID, int? year, Guid? classmajorID, int? recordStatus, int? isConfirm, List ids) { Expression> erexp = x => true; Expression> epexp = x => true; Expression> ebpexp = x => true; Expression> stuexp = x => true; if (schoolYearID.HasValue) { ebpexp = ebpexp.And(x => x.EX_ExaminationBatch.SchoolyearID == schoolYearID); } if (examinationBatchID.HasValue) { ebpexp = ebpexp.And(x => x.ExaminationBatchID == examinationBatchID); } if (examinationTypeID.HasValue) { epexp = epexp.And(x => x.ExaminationTypeID == examinationTypeID); } if (examinationProjectID.HasValue) { epexp = epexp.And(x => x.ExaminationProjectID == examinationProjectID); } if (campusID.HasValue) { stuexp = stuexp.And(x => x.CF_Classmajor.CF_Grademajor.SchoolAreaID == campusID); } if (collegeID.HasValue) { stuexp = stuexp.And(x => x.CF_Classmajor.CF_Grademajor.CF_Facultymajor.CollegeID == collegeID); } if (year.HasValue) { stuexp = stuexp.And(x => x.CF_Classmajor.CF_Grademajor.SchoolyearID == year); } if (classmajorID.HasValue) { stuexp = stuexp.And(x => x.ClassmajorID == classmajorID); } if (recordStatus.HasValue) { erexp = erexp.And(x => x.RecordStatus == recordStatus); } var q = ExaminationSubjectDAL.GetStudentListForExport(erexp, epexp, ebpexp, stuexp); if (isConfirm.HasValue) { q = q.Where(x => isConfirm == 1 ? (x.ExaminationRegistrationConfirmID != null) : (x.ExaminationRegistrationConfirmID == null)); } if (!string.IsNullOrEmpty(examinationSubjectView.ConditionValue) && !string.IsNullOrEmpty(examinationSubjectView.Attribute)) q = q.DynamicWhere(examinationSubjectView.Attribute, examinationSubjectView.Condition, examinationSubjectView.ConditionValue); return q.OrderByDescending(x => x.Year).ThenBy(x => x.CollegeID).ThenBy(x => x.StandardID).ThenBy(x => x.StudentNo).ToList(); } public List AddStudentsCheck(List IDList, Guid? schoolYearID, Guid? examinationBatchID, Guid? examinationTypeID, Guid? examinationProjectID, Guid? examinationProjectFeeID, int? feeTypeID) { try { //获取要报考的批次项目和项目收费标准对应收费项 var examinationBatchProject = ExaminationSubjectDAL.examinationBatchProjectRepository.Entities.Where(x => x.ExaminationBatchID == examinationBatchID && x.ExaminationProjectID == examinationProjectID).FirstOrDefault(); //var examinationProjectFeeType = ExaminationSubjectDAL.examinationProjectFeeTypeRepository.Entities.Where(x => x.ExaminationProjectFeeID == examinationProjectFeeID && x.FeeTypeID == feeTypeID).FirstOrDefault(); //获取要报考的批次项目和项目收费标准对应收费项对应的已报名名单 var studentRegistrationList = ExaminationSubjectDAL.examinationRegistrationRepository.Entities.Where(x => x.ExaminationBatchProjectID == examinationBatchProject.ExaminationBatchProjectID && x.RecordStatus != (int)EX_ExaminationRegistrationStatus.Refunded).ToList(); var userIDList = studentRegistrationList.Select(x => x.UserID).ToList(); //获取学生 Expression> filter = x => true; filter = filter.And(x => IDList.Contains(x.IDNumber)); var studentList = studentsDAL.GetInSchoolStudentViewQueryable(filter).ToList(); studentList = studentList.Where(x => !userIDList.Contains(x.UserID)).ToList(); return studentList; } catch (Exception) { throw; } } } }