using System; using System.Collections.Generic; using System.Linq; using System.Text; using System.Transactions; using Bowin.Common.Linq.Entity; using EMIS.CommonLogic.Specialtyclass; using EMIS.CommonLogic.SystemServices; using EMIS.DataLogic.DataCenterSynch; using EMIS.Entities; using EMIS.ViewModel; namespace EMIS.CommonLogic.DataCenterSynch { public class ClassmajorSynchServices : ClassmajorServices, IClassmajorSynchServices { public Lazy ParameterServices { get; set; } public ClassmajorSynchDAL ClassmajorSynchDAL { get; set; } public void Synchr() { var lastSynchTime = ParameterServices.Value.GetParameterValue(CF_ParameterType.SynchClassmajorLastTime); var nowTime = DateTime.Now; var allClassmajorList = ClassmajorSynchDAL.XXBZ_JWXT_BJXX_VIEWRepository.GetList(x => x.TIMESTAMPS >= lastSynchTime || lastSynchTime == null).ToList(); var allFacultyCodeList = allClassmajorList.Select(x => x.USER_ZYDM.Trim()).Distinct().ToList(); var allClassmajorNoList = allClassmajorList.Select(x => x.BJDM.Trim()).Distinct().ToList(); var dbFacultyList = this.ClassmajorDAL.FacultymajorRepository.GetList(x => allFacultyCodeList.Contains(x.Code)).ToList(); var allGradeList = (from all in allClassmajorList from faculty in dbFacultyList.Where(x => x.Code == all.USER_ZYDM.Trim()) group new { all, faculty } by new { faculty.FacultymajorID, SchoolyearID = (!string.IsNullOrEmpty(all.NJ) ? (int?)Convert.ToInt32(all.NJ) : null) } into g select g.Key).ToList(); var dbGradeList = this.ClassmajorDAL.GrademajorRepository.Entities.SelectByKeys(allGradeList); var dbClassmajorList = this.ClassmajorDAL.ClassmajorRepository.GetList(x => allClassmajorNoList.Contains(x.No)).ToList(); var insertGradeList = (from all in allGradeList from faculty in dbFacultyList.Where(x => x.FacultymajorID == all.FacultymajorID) from db in dbGradeList.Where(x => x.FacultymajorID == faculty.FacultymajorID && x.SchoolyearID == all.SchoolyearID).DefaultIfEmpty() where db == null select new CF_Grademajor { GrademajorID = Guid.NewGuid(), FacultymajorID = faculty.FacultymajorID, Code = faculty.Code + all.SchoolyearID.ToString(), Name = all.SchoolyearID.ToString() + faculty.Name, SchoolyearID = all.SchoolyearID, RecordStatus = (int)SYS_STATUS.USABLE, CreateTime = nowTime, ModifyTime = nowTime }).ToList(); var allDbGradeList = dbGradeList.Concat(insertGradeList).ToList(); var insertClassmajorList = (from all in allClassmajorList from faculty in dbFacultyList.Where(x => x.Code == all.USER_ZYDM.Trim()) from grade in allDbGradeList.Where(x => x.FacultymajorID == faculty.FacultymajorID && x.SchoolyearID == (!string.IsNullOrEmpty(all.NJ) ? (int?)Convert.ToInt32(all.NJ) : null)) from db in ClassmajorDAL.ClassmajorRepository.Entities.Where(x => x.No == all.BJDM.Trim()).DefaultIfEmpty() where db == null select new CF_Classmajor { ClassmajorID = Guid.NewGuid(), GrademajorID = grade.GrademajorID, No = all.BJDM.Trim(), Name = all.BJMC.Trim(), Abbreviation = "", RecordStatus = (int)SYS_STATUS.USABLE, CreateTime = nowTime, ModifyTime = nowTime }).ToList(); var updateList = (from all in allClassmajorList from faculty in dbFacultyList.Where(x => x.Code == all.USER_ZYDM.Trim()) from grade in allDbGradeList.Where(x => x.FacultymajorID == faculty.FacultymajorID && x.SchoolyearID == (!string.IsNullOrEmpty(all.NJ) ? (int?)Convert.ToInt32(all.NJ) : null)) from db in ClassmajorDAL.ClassmajorRepository.Entities.Where(x => x.No == all.BJDM.Trim()) select new CF_Classmajor { ClassmajorID = db.ClassmajorID, GrademajorID = grade.GrademajorID, No = all.BJDM.Trim(), Name = all.BJMC.Trim(), Abbreviation = db.Abbreviation, EnglishName = db.EnglishName, ClassNum = db.ClassNum, UserID = db.UserID, AssistantUserID = db.AssistantUserID, Fixedclassroom = db.Fixedclassroom, Remark = db.Remark, RecordStatus = db.RecordStatus, CreateUserID = db.CreateUserID, CreateTime = db.CreateTime, ModifyUserID = db.ModifyUserID, ModifyTime = nowTime }).ToList(); using (var scope = new TransactionScope(TransactionScopeOption.RequiresNew, new TransactionOptions { IsolationLevel = IsolationLevel.Serializable })) { UnitOfWork.BulkInsert(insertGradeList); UnitOfWork.BulkInsert(insertClassmajorList); UnitOfWork.BatchUpdate(updateList); scope.Complete(); } ParameterServices.Value.SaveTo(CF_ParameterType.SynchClassmajorLastTime, nowTime); } } }