public ActionResult Import(Models.DormTeacher.Import vm) { vm.ImportList = new List <Dto.DormTeacher.Import>(); if (ModelState.IsValid) { var file = Request.Files[nameof(vm.UploadFile)]; var fileSave = System.IO.Path.GetTempFileName(); file.SaveAs(fileSave); using (var db = new XkSystem.Models.DbContext()) { #region 数据 if (Code.Common.GetFileType(file.FileName) != Code.FileType.Excel) { ModelState.AddModelError("", "上传的文件不是正确的EXCLE文件!"); return(View(vm)); } var dt = Code.NpoiHelper.ExcelToDataTable(fileSave, System.IO.Path.GetExtension(file.FileName), string.Empty); if (dt == null) { ModelState.AddModelError("", "无法读取上传的文件,请检查文件格式是否正确!"); return(View(vm)); } var tbList = new List <string>() { "排序", "宿舍名称", "宿舍楼", "容纳人数", "宿管教职工号", "宿管姓名" }; var Text = string.Empty; foreach (var a in tbList) { if (dt.Columns.Contains(a.ToString()) == false) { Text += a + ","; } } if (string.IsNullOrEmpty(Text) == false) { ModelState.AddModelError("", "上传的EXCEL内容与预期不一致,缺少对应的字段:" + Text); return(View(vm)); } foreach (System.Data.DataRow dr in dt.Rows) { var dto = new Dto.DormTeacher.Import() { No = dr["排序"].ConvertToString(), RoomName = dr["宿舍名称"].ConvertToString(), BuildName = dr["宿舍楼"].ConvertToString(), MaxCount = dr["容纳人数"].ConvertToString(), TeacherCode = dr["宿管教职工号"].ConvertToString(), TeacherName = dr["宿管姓名"].ConvertToString() }; if (vm.ImportList.Where(d => d.No == dto.No && d.RoomName == dto.RoomName && d.BuildName == dto.BuildName && d.TeacherCode == dto.TeacherCode && d.TeacherName == dto.TeacherName && d.MaxCount == dto.MaxCount).Any() == false) { vm.ImportList.Add(dto); } } vm.ImportList.RemoveAll(d => string.IsNullOrEmpty(d.RoomName) && string.IsNullOrEmpty(d.BuildName) && string.IsNullOrEmpty(d.No) && string.IsNullOrEmpty(d.TeacherCode) && string.IsNullOrEmpty(d.TeacherName) && string.IsNullOrEmpty(d.MaxCount)); if (vm.ImportList.Count == 0) { ModelState.AddModelError("", "未读取到任何有效数据!"); return(View(vm)); } #endregion var RoomList = db.Table <Basis.Entity.tbRoom>().Include(d => d.tbBuild).ToList(); var BuildList = db.Table <Basis.Entity.tbBuild>().ToList(); var dormTeacherList = db.Table <Dorm.Entity.tbDormTeacher>() .Include(d => d.tbRoom) .Include(d => d.tbRoom.tbBuild) .Include(d => d.tbTeacher).ToList(); var teacherList = db.Table <Teacher.Entity.tbTeacher>().ToList(); #region 验证数据格式是否正确 foreach (var item in vm.ImportList) { if (string.IsNullOrEmpty(item.RoomName)) { item.Error += "宿舍名称不能为空!"; } if (string.IsNullOrEmpty(item.TeacherCode) || string.IsNullOrEmpty(item.TeacherName)) { item.Error += "教师编码和教师名称不能为空!"; } if (string.IsNullOrEmpty(item.BuildName)) { item.Error += "宿舍楼不能为空!"; } if (BuildList.Where(d => d.BuildName == item.BuildName).Count() == 0) { item.Error += "宿舍楼不存在;"; } if (vm.IsUpdate) { //if (dormTeacherList.Where(d => d.tbTeacher.TeacherCode == item.TeacherCode // && d.tbTeacher.TeacherName == item.TeacherName // && d.tbRoom.RoomName == item.RoomName // && d.tbRoom.tbBuild.BuildName == item.BuildName).Any()) //{ // item.Error += "该条数据重复!"; //} } else { if (dormTeacherList.Where(d => d.tbTeacher.TeacherCode == item.TeacherCode && d.tbRoom.RoomName == item.RoomName).Any()) { item.Error += "系统中已存在该记录!"; } } if (teacherList.Where(d => d.TeacherCode == item.TeacherCode && d.TeacherName == item.TeacherName).Any() == false) { item.Error += "该教职工不存在!"; } int num = 0; if (!string.IsNullOrEmpty(item.No)) { if (!int.TryParse(item.No, out num) || num < 0) { item.Error += "容纳人数必须是正整数!"; } } } if (vm.ImportList.Where(d => string.IsNullOrEmpty(d.Error) == false).Count() > 0) { vm.ImportList.RemoveAll(d => string.IsNullOrEmpty(d.Error)); return(View(vm)); } #endregion #region 数据导入,判断关键字,存在的数据做更新,不存在的做新增 List <Dorm.Entity.tbDormTeacher> tbDormTeacherList = new List <Dorm.Entity.tbDormTeacher>(); foreach (var item in vm.ImportList) { if (dormTeacherList.Where(d => d.tbRoom.RoomName == item.RoomName && d.tbRoom.tbBuild.BuildName == item.BuildName).Count() > 0) { if (vm.IsUpdate) { var tb = dormTeacherList.Where(d => d.tbRoom.RoomName == item.RoomName && d.tbRoom.tbBuild.BuildName == item.BuildName).FirstOrDefault(); tb.No = item.No.ConvertToInt(); tb.tbTeacher = teacherList.Where(d => d.TeacherCode == item.TeacherCode && d.TeacherName == item.TeacherName).FirstOrDefault(); } } else { var tb = new Dorm.Entity.tbDormTeacher() { No = item.No.ConvertToInt(), tbRoom = RoomList.Where(d => d.RoomName == item.RoomName && d.tbBuild.BuildName == item.BuildName).FirstOrDefault(), tbTeacher = teacherList.Where(d => d.TeacherCode == item.TeacherCode && d.TeacherName == item.TeacherName).FirstOrDefault() }; tbDormTeacherList.Add(tb); } } #endregion if (tbDormTeacherList.Count > 0) { db.Set <Dorm.Entity.tbDormTeacher>().AddRange(tbDormTeacherList); } if (db.SaveChanges() > 0) { vm.ImportList.RemoveAll(d => string.IsNullOrEmpty(d.Error)); XkSystem.Areas.Sys.Controllers.SysUserLogController.Insert("导入宿舍信息"); vm.Status = true; } } } return(View(vm)); }
public ActionResult Import() { var vm = new Models.DormTeacher.Import(); return(View(vm)); }