using System; using System.Collections.Generic; using System.Linq; using System.Text; using System.Transactions; using EMIS.CommonLogic.Specialtyclass; using EMIS.CommonLogic.SystemServices; using EMIS.CommonLogic.SystemSetting; using EMIS.DataLogic.DataCenterSynch; using EMIS.DataLogic.SystemSetting; using EMIS.Entities; using EMIS.ViewModel; namespace EMIS.CommonLogic.DataCenterSynch { public class FacultySynchServices : FacultymajorServices, IFacultySynchServices { public Lazy ParameterServices { get; set; } public DictionaryItemDAL DictionaryItemDAL { get; set; } public FacultySynchDAL FacultySynchDAL { get; set; } public void Synchr() { var lastSynchTime = ParameterServices.Value.GetParameterValue(CF_ParameterType.SynchFacultyLastTime); var nowTime = DateTime.Now; var allFacultyList = FacultySynchDAL.XXBZ_JWXT_ZYXX_VIEWRepository.GetList(x => x.TIMESTAMPS >= lastSynchTime || lastSynchTime == null).ToList(); var allFacultyCodeList = allFacultyList.Select(x => x.USER_DM.Trim()).Distinct().ToList(); var allCollegeNoList = allFacultyList.Select(x => x.SSYXB_USERDM.Trim()).Distinct().ToList(); var dbFacultyList = this.FacultymajorDAL.FacultymajorRepository.GetList(x => allFacultyCodeList.Contains(x.Code)).ToList(); var dbCollegeList = this.FacultymajorDAL.CollegeRepository.GetList(x => allCollegeNoList.Contains(x.No)).ToList(); var standardDictionaryCode = typeof(CF_Standard).Name; var dbDictionaryList = this.DictionaryItemDAL.DictionaryItemRepository.GetList(x => x.DictionaryCode == standardDictionaryCode && allFacultyCodeList.Contains(x.Code)).ToList(); var insertDictionaryList = ( from all in allFacultyList from db in dbDictionaryList.Where(x => x.Code == all.USER_DM.Trim()).DefaultIfEmpty() where db == null select new Sys_DictionaryItem { DictionaryItemID = Guid.NewGuid(), DictionaryCode = standardDictionaryCode, Code = all.USER_DM.Trim(), Name = all.ZWMC.Trim(), Value = Convert.ToInt32(all.USER_DM.Trim()), RecordStatus = (int)SYS_STATUS.USABLE, OrderNo = Convert.ToInt16(all.USER_DM.Trim()), IsEditable = true } ).ToList(); var updateDictionaryList = ( from all in allFacultyList from db in dbDictionaryList.Where(x => x.Code == all.USER_DM.Trim()) select new Sys_DictionaryItem { DictionaryItemID = db.DictionaryItemID, DictionaryCode = db.DictionaryCode, Code = all.USER_DM.Trim(), Name = all.ZWMC.Trim(), Value = Convert.ToInt32(all.USER_DM.Trim()), RecordStatus = db.RecordStatus, OrderNo = Convert.ToInt16(all.USER_DM.Trim()), IsEditable = db.IsEditable } ).ToList(); var allDictionaryList = insertDictionaryList.Concat(updateDictionaryList).ToList(); var insertList = (from all in allFacultyList from college in dbCollegeList.Where(x => x.No == all.SSYXB_USERDM.Trim()) from dic in allDictionaryList.Where(x => x.Code == all.USER_DM.Trim()) from db in dbFacultyList.Where(x => x.Code == all.USER_DM.Trim()).DefaultIfEmpty() where db == null select new CF_Facultymajor { FacultymajorID = Guid.NewGuid(), CollegeID = college.CollegeID, Code = all.USER_DM.Trim(), Name = all.ZWMC.Trim(), Abbreviation = null, EnglishName = all.YWMC?.Trim(), StandardID = dic.Value, RecordStatus = (int)SYS_STATUS.USABLE, CreateTime = nowTime, ModifyTime = nowTime }).ToList(); var updateList = (from all in allFacultyList from college in dbCollegeList.Where(x => x.No == all.SSYXB_USERDM.Trim()) from dic in allDictionaryList.Where(x => x.Code == all.USER_DM.Trim()) from db in dbFacultyList.Where(x => x.Code == all.USER_DM.Trim()) select new CF_Facultymajor { FacultymajorID = db.FacultymajorID, CollegeID = college.CollegeID, Code = all.USER_DM.Trim(), Name = all.ZWMC.Trim(), Abbreviation = db.Abbreviation, EnglishName = all.YWMC?.Trim(), StandardID = dic.Value, EducationID = db.EducationID, LearningformID = db.LearningformID, LearnSystem = db.LearnSystem, ScienceclassID = db.ScienceclassID, LearningstyleID = db.LearningstyleID, LearnPositionID = db.LearnPositionID, TeacherIdentification = db.TeacherIdentification, SetTime = db.SetTime, Remark = db.Remark, RecordStatus = (int)SYS_STATUS.USABLE, 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(insertDictionaryList); UnitOfWork.BatchUpdate(updateDictionaryList); UnitOfWork.BulkInsert(insertList); UnitOfWork.BatchUpdate(updateList); scope.Complete(); } ParameterServices.Value.SaveTo(CF_ParameterType.SynchFacultyLastTime, nowTime); } } }