123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323 |
- 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<IParameterServices> 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<string> selectedColumnList)
- {
- var mobileExp = new Regex(@"^1\d{10}$");
- var dataCenterCodeTranslateList = DataCenterCodeTranslateRepository.Entities.ToList();
- var lastSynchTime = ParameterServices.Value.GetParameterValue<DateTime>(CF_ParameterType.SynchStudentLastTime);
- var nowTime = DateTime.Now;
- var studentRole = RoleRepository.GetSingle(x => x.CF_StudentRole.RoleID != null, (x => x.CF_StudentRole));
- Expression<Func<LY_XXBZ_JWXT_XSJBXX_VIEW, bool>> studentExp = (x => x.BJDM_USER != null && x.SFZH != null);
- if (collegeID.HasValue || year.HasValue || schoolAreaID.HasValue)
- {
- List<string> classmajorNoList = new List<string>();
- 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<Sys_User>();
- var insertRoleList = new List<Relation2KeyTable>();
- var insertStudentList = new List<CF_Student>();
- var insertStudentProfileList = new List<CF_StudentProfile>();
- var insertStudentContactList = new List<CF_StudentContact>();
- var insertStudentAccountList = new List<CF_StudentAccount>();
- var insertRecruitstudentsList = new List<CF_Recruitstudents>();
- var updateList = new List<Sys_User>();
- var updateStudentList = new List<CF_Student>();
- var updateStudentProfileList = new List<CF_StudentProfile>();
- var updateStudentContactList = new List<CF_StudentContact>();
- 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<Sys_User, Sys_Role>(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);
- }
- }
- }
|