using System; using System.Collections.Generic; using System.Linq; using System.Linq.Expressions; using System.Text; using System.Text.RegularExpressions; using System.Threading; using System.Transactions; using Bowin.Common.Linq; using Bowin.Common.Utility; using EMIS.CommonLogic.Students; using EMIS.CommonLogic.SystemServices; using EMIS.DataLogic; using EMIS.DataLogic.DataCenterSynch; using EMIS.DataLogic.Repositories; using EMIS.DataLogic.SystemSetting; using EMIS.Entities; using EMIS.Entities.DataCenter; using EMIS.Entities.LYDataCenter; using EMIS.ViewModel; namespace EMIS.CommonLogic.DataCenterSynch { public class StudentSynchServices : StudentsServices, IStudentSynchServices { public Lazy ParameterServices { get; set; } public DataCenterCodeTranslateRepository DataCenterCodeTranslateRepository { get; set; } public StudentSynchDAL StudentSynchDAL { get; set; } public DictionaryItemDAL DictionaryItemDAL { get; set; } public RoleRepository RoleRepository { get; set; } public void Synchr() { SynchrByCondition(null, null, null, null); } public void SynchrByCondition(Guid? collegeID, int? year, int? schoolAreaID, IList selectedColumnList) { var mobileExp = new Regex(@"^1\d{10}$"); var dataCenterCodeTranslateList = DataCenterCodeTranslateRepository.Entities.ToList(); var lastSynchTime = ParameterServices.Value.GetParameterValue(CF_ParameterType.SynchStudentLastTime); var nowTime = DateTime.Now; var studentRole = RoleRepository.GetSingle(x => x.CF_StudentRole.RoleID != null, (x => x.CF_StudentRole)); Expression> studentExp = (x => x.BJDM_USER != null && x.SFZH != null); if (collegeID.HasValue || year.HasValue || schoolAreaID.HasValue) { List classmajorNoList = new List(); var gradeMajorQuery = this.StudentsDAL.Value.GrademajorRepository.Entities; if (collegeID.HasValue) { gradeMajorQuery = gradeMajorQuery.Where(x => x.CF_Facultymajor.CollegeID == collegeID); } if (year.HasValue) { gradeMajorQuery = gradeMajorQuery.Where(x => x.SchoolyearID == year); } if (schoolAreaID.HasValue) { gradeMajorQuery = gradeMajorQuery.Where(x => x.SchoolAreaID == schoolAreaID); } var classmajorNoQuery = gradeMajorQuery.SelectMany(x => x.CF_Classmajor).Select(x => x.No); classmajorNoList = classmajorNoQuery.ToList(); studentExp = studentExp.And(x => classmajorNoList.Contains(x.BJDM_USER)); } var allStudentList = StudentSynchDAL.XXBZ_JWXT_XSJBXX_VIEWRepository.GetList(studentExp).ToList(); var duplicateStudentList = allStudentList.Where(x => allStudentList.GroupBy(w => w.SFZH).Where(w => w.Count() > 1).Select(w => w.Key).Contains(x.SFZH)).ToList(); allStudentList = allStudentList.Where(x => !duplicateStudentList.Select(w => w.SFZH).Contains(x.SFZH)).ToList(); var allStudentIdNumberList = allStudentList.Select(x => x.SFZH.Trim()).Distinct().ToList(); var allClassmajorNoList = allStudentList.Select(x => x.BJDM_USER.Trim()).ToList(); var dbClassmajorList = this.ClassmajorRepository.Value.GetList(x => allClassmajorNoList.Contains(x.No)).ToList(); var dbStudentList = this.StudentsDAL.Value.StudentRepository.GetList(x => allStudentIdNumberList.Contains(x.Sys_User.LoginID), (x => x.Sys_User), (x => x.CF_StudentProfile), (x => x.CF_StudentContact)).ToList(); var sexList = dataCenterCodeTranslateList.Where(x => x.CodeType == "XBM").ToList(); var nationList = this.DictionaryItemDAL.DictionaryItemRepository.GetList(x => x.DictionaryCode == typeof(CF_Nation).Name).ToList(); ; var idtypeList = dataCenterCodeTranslateList.Where(x => x.CodeType == "SFZJLXM").ToList(); #region 获取要添加和修改的记录,涉及表多,展开较长…… var insertList = new List(); var insertRoleList = new List(); var insertStudentList = new List(); var insertStudentProfileList = new List(); var insertStudentContactList = new List(); var insertStudentAccountList = new List(); var insertRecruitstudentsList = new List(); var updateList = new List(); var updateStudentList = new List(); var updateStudentProfileList = new List(); var updateStudentContactList = new List(); foreach (var all in allStudentList) { var XH = all.XH.Trim(); var SZBH = all.BJDM_USER.Trim(); var XBM = (all.XB ?? "").Trim(); var MZM = (all.MZ ?? "").Trim(); var XM = (all.XM ?? "").Trim(); var SFZJH = (all.SFZH ?? "").Trim(); var CSRQ = all.CSRQ; var JG = (all.JG ?? "").Trim(); if (SFZJH == "") { continue; } var db = dbStudentList.FirstOrDefault(s => s.Sys_User.LoginID == SFZJH); var classmajor = dbClassmajorList.FirstOrDefault(c => c.No == SZBH); var sex = sexList.FirstOrDefault(c => c.Code == XBM); var nation = nationList.FirstOrDefault(c => c.Name == MZM); var XB = sex != null ? sex.Value : null; var SFZJLX = (int)CF_CertificatesType.IdCrad; var MZ = nation != null ? nation.Value : null; var studentTypeID = (studentRole != null ? studentRole.CF_StudentRole.StudentType : null); string DH = ""; string Mobile = ""; string TXDZ = ""; string DZYX = ""; DH = (all.LXDH ?? "").Trim(); TXDZ = (all.LXDZ ?? "").Trim(); DZYX = (all.EMAIL ?? "").Trim(); Mobile = (all.MOBILE ?? "").Trim(); var userID = Guid.NewGuid(); if (db == null) { if (classmajor != null && !insertList.Any(s => s.LoginID == SFZJH)) { var user = new Sys_User { UserID = userID, LoginID = SFZJH, Password = "888888".MD5(), Name = XM, RecordStatus = (int)SYS_STATUS.USABLE, CreateTime = nowTime, ModifyTime = nowTime }; var student = new CF_Student { UserID = userID, ClassmajorID = classmajor.ClassmajorID, Sex = XB, CertificatesType = SFZJLX, IDNumber = SFZJH, StudentType = studentTypeID, InSchoolStatusID = (int)CF_InschoolStatus.InSchool, StudentStatus = (int)CF_STUDENTSTATUS.registration, StudentCardNo = XH, RecordStatus = (int)SYS_STATUS.USABLE, CreateTime = nowTime, ModifyTime = nowTime }; var profile = new CF_StudentProfile { UserID = userID, BirthDate = CSRQ, Place = JG, Nation = MZ, RecordStatus = (int)SYS_STATUS.USABLE, CreateTime = nowTime, ModifyTime = nowTime }; var studentContact = new CF_StudentContact { UserID = userID, Telephone = DH, Mobile = Mobile, Address = TXDZ, Email = DZYX, RecordStatus = (int)SYS_STATUS.USABLE, CreateTime = nowTime, ModifyTime = nowTime }; var account = new CF_StudentAccount { UserID = userID, RecordStatus = (int)SYS_STATUS.USABLE, CreateTime = nowTime, ModifyTime = nowTime }; var recruit = new CF_Recruitstudents { UserID = userID, RecordStatus = (int)SYS_STATUS.USABLE, CreateTime = nowTime, ModifyTime = nowTime }; insertList.Add(user); if (studentRole != null) { insertRoleList.Add(new Relation2KeyTable { Key1 = userID, Key2 = studentRole.RoleID }); } insertStudentList.Add(student); insertStudentProfileList.Add(profile); insertStudentContactList.Add(studentContact); insertStudentAccountList.Add(account); insertRecruitstudentsList.Add(recruit); } } else { var user = new Sys_User { UserID = db.UserID, LoginID = selectedColumnList.Contains("SFZJH") ? SFZJH : db.Sys_User.LoginID, Password = db.Sys_User.Password, Name = selectedColumnList.Contains("XM") ? XM : db.Sys_User.Name, RecordStatus = db.RecordStatus, CreateUserID = db.CreateUserID, CreateTime = db.CreateTime, ModifyUserID = db.ModifyUserID, ModifyTime = nowTime }; var student = new CF_Student { UserID = db.UserID, ClassmajorID = selectedColumnList.Contains("SZBH") ? (classmajor != null ? classmajor.ClassmajorID : db.ClassmajorID) : db.ClassmajorID, Sex = selectedColumnList.Contains("XBM") ? XB : db.Sex, CertificatesType = selectedColumnList.Contains("SFZJLXM") ? SFZJLX : db.CertificatesType, IDNumber = selectedColumnList.Contains("SFZJH") ? SFZJH : db.IDNumber, StudentType = db.StudentType, InSchoolStatusID = db.InSchoolStatusID, StudentStatus = db.StudentStatus, CultureModel = db.CultureModel, IsProofread = db.IsProofread, IsDreamProject = db.IsDreamProject, StudentCardNo = selectedColumnList.Contains("XH") ? XH : db.StudentCardNo, PlanningGraduateDate = db.PlanningGraduateDate, GraduateDate = db.GraduateDate, GraduateCardNo = db.GraduateCardNo, ReplaceGraduateNo = db.ReplaceGraduateNo, DegreeStatus = db.DegreeStatus, PhotoUrl = db.PhotoUrl, Career = db.Career }; var profile = new CF_StudentProfile { UserID = db.UserID, UsedName = db.CF_StudentProfile.UsedName, DirectorName = db.CF_StudentProfile.DirectorName, BirthDate = selectedColumnList.Contains("CSRQ") ? CSRQ : db.CF_StudentProfile.BirthDate, Country = db.CF_StudentProfile.Country, Politics = db.CF_StudentProfile.Politics, LiteracyLevels = db.CF_StudentProfile.LiteracyLevels, Place = selectedColumnList.Contains("JG") ? JG : db.CF_StudentProfile.Place, Nation = selectedColumnList.Contains("MZM") ? MZ : db.CF_StudentProfile.Nation, Healthy = db.CF_StudentProfile.Healthy, BloodGroup = db.CF_StudentProfile.BloodGroup, BornPlace = db.CF_StudentProfile.BornPlace, Specialty = db.CF_StudentProfile.Specialty, Height = db.CF_StudentProfile.Height, Weight = db.CF_StudentProfile.Weight, Remarks = db.CF_StudentProfile.Remarks, GraduationPictureUrl = db.CF_StudentProfile.GraduationPictureUrl, RecordStatus = db.CF_StudentProfile.RecordStatus, CreateUserID = db.CF_StudentProfile.CreateUserID, CreateTime = db.CF_StudentProfile.CreateTime, ModifyUserID = db.CF_StudentProfile.ModifyUserID, ModifyTime = nowTime }; var studentContact = new CF_StudentContact { UserID = db.UserID, Email = selectedColumnList.Contains("DZYX") ? DZYX : db.CF_StudentContact.Email, QQ = db.CF_StudentContact.QQ, Mobile = selectedColumnList.Contains("DH") ? Mobile : db.CF_StudentContact.Mobile, Telephone = selectedColumnList.Contains("DH") ? DH : db.CF_StudentContact.Telephone, MicroMsgNo = db.CF_StudentContact.MicroMsgNo, Zipcode = db.CF_StudentContact.Zipcode, Address = selectedColumnList.Contains("TXDZ") ? TXDZ : db.CF_StudentContact.Address, WorkUnit = db.CF_StudentContact.WorkUnit, HomeAddress = db.CF_StudentContact.HomeAddress, Recipient = db.CF_StudentContact.Recipient, Dormitory = db.CF_StudentContact.Dormitory, RecordStatus = db.CF_StudentContact.RecordStatus, CreateUserID = db.CF_StudentContact.CreateUserID, CreateTime = db.CF_StudentContact.CreateTime, ModifyUserID = db.CF_StudentContact.ModifyUserID, ModifyTime = nowTime }; updateList.Add(user); updateStudentList.Add(student); updateStudentProfileList.Add(profile); updateStudentContactList.Add(studentContact); } } #endregion using (var scope = new TransactionScope(TransactionScopeOption.RequiresNew, new TransactionOptions { IsolationLevel = IsolationLevel.Serializable })) { UnitOfWork.BulkInsert(insertList); UnitOfWork.BulkInsert(insertRoleList); UnitOfWork.BulkInsert(insertStudentList); UnitOfWork.BulkInsert(insertStudentProfileList); UnitOfWork.BulkInsert(insertStudentContactList); UnitOfWork.BulkInsert(insertStudentAccountList); UnitOfWork.BulkInsert(insertRecruitstudentsList); UnitOfWork.BatchUpdate(updateList); UnitOfWork.BatchUpdate(updateStudentList); UnitOfWork.BatchUpdate(updateStudentProfileList); UnitOfWork.BatchUpdate(updateStudentContactList); scope.Complete(); } ParameterServices.Value.SaveTo(CF_ParameterType.SynchStudentLastTime, nowTime); } } }