StudentSynchServices.cs 16 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323
  1. using System;
  2. using System.Collections.Generic;
  3. using System.Linq;
  4. using System.Linq.Expressions;
  5. using System.Text;
  6. using System.Text.RegularExpressions;
  7. using System.Threading;
  8. using System.Transactions;
  9. using Bowin.Common.Linq;
  10. using Bowin.Common.Utility;
  11. using EMIS.CommonLogic.Students;
  12. using EMIS.CommonLogic.SystemServices;
  13. using EMIS.DataLogic;
  14. using EMIS.DataLogic.DataCenterSynch;
  15. using EMIS.DataLogic.Repositories;
  16. using EMIS.DataLogic.SystemSetting;
  17. using EMIS.Entities;
  18. using EMIS.Entities.DataCenter;
  19. using EMIS.Entities.LYDataCenter;
  20. using EMIS.ViewModel;
  21. namespace EMIS.CommonLogic.DataCenterSynch
  22. {
  23. public class StudentSynchServices : StudentsServices, IStudentSynchServices
  24. {
  25. public Lazy<IParameterServices> ParameterServices { get; set; }
  26. public DataCenterCodeTranslateRepository DataCenterCodeTranslateRepository { get; set; }
  27. public StudentSynchDAL StudentSynchDAL { get; set; }
  28. public DictionaryItemDAL DictionaryItemDAL { get; set; }
  29. public RoleRepository RoleRepository { get; set; }
  30. public void Synchr()
  31. {
  32. SynchrByCondition(null, null, null, null);
  33. }
  34. public void SynchrByCondition(Guid? collegeID, int? year, int? schoolAreaID, IList<string> selectedColumnList)
  35. {
  36. var mobileExp = new Regex(@"^1\d{10}$");
  37. var dataCenterCodeTranslateList = DataCenterCodeTranslateRepository.Entities.ToList();
  38. var lastSynchTime = ParameterServices.Value.GetParameterValue<DateTime>(CF_ParameterType.SynchStudentLastTime);
  39. var nowTime = DateTime.Now;
  40. var studentRole = RoleRepository.GetSingle(x => x.CF_StudentRole.RoleID != null, (x => x.CF_StudentRole));
  41. Expression<Func<LY_XXBZ_JWXT_XSJBXX_VIEW, bool>> studentExp = (x => x.BJDM_USER != null && x.SFZH != null);
  42. if (collegeID.HasValue || year.HasValue || schoolAreaID.HasValue)
  43. {
  44. List<string> classmajorNoList = new List<string>();
  45. var gradeMajorQuery = this.StudentsDAL.Value.GrademajorRepository.Entities;
  46. if (collegeID.HasValue)
  47. {
  48. gradeMajorQuery = gradeMajorQuery.Where(x => x.CF_Facultymajor.CollegeID == collegeID);
  49. }
  50. if (year.HasValue)
  51. {
  52. gradeMajorQuery = gradeMajorQuery.Where(x => x.SchoolyearID == year);
  53. }
  54. if (schoolAreaID.HasValue)
  55. {
  56. gradeMajorQuery = gradeMajorQuery.Where(x => x.SchoolAreaID == schoolAreaID);
  57. }
  58. var classmajorNoQuery = gradeMajorQuery.SelectMany(x => x.CF_Classmajor).Select(x => x.No);
  59. classmajorNoList = classmajorNoQuery.ToList();
  60. studentExp = studentExp.And(x => classmajorNoList.Contains(x.BJDM_USER));
  61. }
  62. var allStudentList = StudentSynchDAL.XXBZ_JWXT_XSJBXX_VIEWRepository.GetList(studentExp).ToList();
  63. var duplicateStudentList = allStudentList.Where(x => allStudentList.GroupBy(w => w.SFZH).Where(w => w.Count() > 1).Select(w => w.Key).Contains(x.SFZH)).ToList();
  64. allStudentList = allStudentList.Where(x => !duplicateStudentList.Select(w => w.SFZH).Contains(x.SFZH)).ToList();
  65. var allStudentIdNumberList = allStudentList.Select(x => x.SFZH.Trim()).Distinct().ToList();
  66. var allClassmajorNoList = allStudentList.Select(x => x.BJDM_USER.Trim()).ToList();
  67. var dbClassmajorList = this.ClassmajorRepository.Value.GetList(x => allClassmajorNoList.Contains(x.No)).ToList();
  68. var dbStudentList = this.StudentsDAL.Value.StudentRepository.GetList(x => allStudentIdNumberList.Contains(x.Sys_User.LoginID),
  69. (x => x.Sys_User), (x => x.CF_StudentProfile), (x => x.CF_StudentContact)).ToList();
  70. var sexList = dataCenterCodeTranslateList.Where(x => x.CodeType == "XBM").ToList();
  71. var nationList = this.DictionaryItemDAL.DictionaryItemRepository.GetList(x => x.DictionaryCode == typeof(CF_Nation).Name).ToList(); ;
  72. var idtypeList = dataCenterCodeTranslateList.Where(x => x.CodeType == "SFZJLXM").ToList();
  73. #region 获取要添加和修改的记录,涉及表多,展开较长……
  74. var insertList = new List<Sys_User>();
  75. var insertRoleList = new List<Relation2KeyTable>();
  76. var insertStudentList = new List<CF_Student>();
  77. var insertStudentProfileList = new List<CF_StudentProfile>();
  78. var insertStudentContactList = new List<CF_StudentContact>();
  79. var insertStudentAccountList = new List<CF_StudentAccount>();
  80. var insertRecruitstudentsList = new List<CF_Recruitstudents>();
  81. var updateList = new List<Sys_User>();
  82. var updateStudentList = new List<CF_Student>();
  83. var updateStudentProfileList = new List<CF_StudentProfile>();
  84. var updateStudentContactList = new List<CF_StudentContact>();
  85. foreach (var all in allStudentList)
  86. {
  87. var XH = all.XH.Trim();
  88. var SZBH = all.BJDM_USER.Trim();
  89. var XBM = (all.XB ?? "").Trim();
  90. var MZM = (all.MZ ?? "").Trim();
  91. var XM = (all.XM ?? "").Trim();
  92. var SFZJH = (all.SFZH ?? "").Trim();
  93. var CSRQ = all.CSRQ;
  94. var JG = (all.JG ?? "").Trim();
  95. if (SFZJH == "")
  96. {
  97. continue;
  98. }
  99. var db = dbStudentList.FirstOrDefault(s => s.Sys_User.LoginID == SFZJH);
  100. var classmajor = dbClassmajorList.FirstOrDefault(c => c.No == SZBH);
  101. var sex = sexList.FirstOrDefault(c => c.Code == XBM);
  102. var nation = nationList.FirstOrDefault(c => c.Name == MZM);
  103. var XB = sex != null ? sex.Value : null;
  104. var SFZJLX = (int)CF_CertificatesType.IdCrad;
  105. var MZ = nation != null ? nation.Value : null;
  106. var studentTypeID = (studentRole != null ? studentRole.CF_StudentRole.StudentType : null);
  107. string DH = "";
  108. string Mobile = "";
  109. string TXDZ = "";
  110. string DZYX = "";
  111. DH = (all.LXDH ?? "").Trim();
  112. TXDZ = (all.LXDZ ?? "").Trim();
  113. DZYX = (all.EMAIL ?? "").Trim();
  114. Mobile = (all.MOBILE ?? "").Trim();
  115. var userID = Guid.NewGuid();
  116. if (db == null)
  117. {
  118. if (classmajor != null && !insertList.Any(s => s.LoginID == SFZJH))
  119. {
  120. var user = new Sys_User
  121. {
  122. UserID = userID,
  123. LoginID = SFZJH,
  124. Password = "888888".MD5(),
  125. Name = XM,
  126. RecordStatus = (int)SYS_STATUS.USABLE,
  127. CreateTime = nowTime,
  128. ModifyTime = nowTime
  129. };
  130. var student = new CF_Student
  131. {
  132. UserID = userID,
  133. ClassmajorID = classmajor.ClassmajorID,
  134. Sex = XB,
  135. CertificatesType = SFZJLX,
  136. IDNumber = SFZJH,
  137. StudentType = studentTypeID,
  138. InSchoolStatusID = (int)CF_InschoolStatus.InSchool,
  139. StudentStatus = (int)CF_STUDENTSTATUS.registration,
  140. StudentCardNo = XH,
  141. RecordStatus = (int)SYS_STATUS.USABLE,
  142. CreateTime = nowTime,
  143. ModifyTime = nowTime
  144. };
  145. var profile = new CF_StudentProfile
  146. {
  147. UserID = userID,
  148. BirthDate = CSRQ,
  149. Place = JG,
  150. Nation = MZ,
  151. RecordStatus = (int)SYS_STATUS.USABLE,
  152. CreateTime = nowTime,
  153. ModifyTime = nowTime
  154. };
  155. var studentContact = new CF_StudentContact
  156. {
  157. UserID = userID,
  158. Telephone = DH,
  159. Mobile = Mobile,
  160. Address = TXDZ,
  161. Email = DZYX,
  162. RecordStatus = (int)SYS_STATUS.USABLE,
  163. CreateTime = nowTime,
  164. ModifyTime = nowTime
  165. };
  166. var account = new CF_StudentAccount
  167. {
  168. UserID = userID,
  169. RecordStatus = (int)SYS_STATUS.USABLE,
  170. CreateTime = nowTime,
  171. ModifyTime = nowTime
  172. };
  173. var recruit = new CF_Recruitstudents
  174. {
  175. UserID = userID,
  176. RecordStatus = (int)SYS_STATUS.USABLE,
  177. CreateTime = nowTime,
  178. ModifyTime = nowTime
  179. };
  180. insertList.Add(user);
  181. if (studentRole != null)
  182. {
  183. insertRoleList.Add(new Relation2KeyTable { Key1 = userID, Key2 = studentRole.RoleID });
  184. }
  185. insertStudentList.Add(student);
  186. insertStudentProfileList.Add(profile);
  187. insertStudentContactList.Add(studentContact);
  188. insertStudentAccountList.Add(account);
  189. insertRecruitstudentsList.Add(recruit);
  190. }
  191. }
  192. else
  193. {
  194. var user = new Sys_User
  195. {
  196. UserID = db.UserID,
  197. LoginID = selectedColumnList.Contains("SFZJH") ? SFZJH : db.Sys_User.LoginID,
  198. Password = db.Sys_User.Password,
  199. Name = selectedColumnList.Contains("XM") ? XM : db.Sys_User.Name,
  200. RecordStatus = db.RecordStatus,
  201. CreateUserID = db.CreateUserID,
  202. CreateTime = db.CreateTime,
  203. ModifyUserID = db.ModifyUserID,
  204. ModifyTime = nowTime
  205. };
  206. var student = new CF_Student
  207. {
  208. UserID = db.UserID,
  209. ClassmajorID = selectedColumnList.Contains("SZBH") ? (classmajor != null ? classmajor.ClassmajorID : db.ClassmajorID) : db.ClassmajorID,
  210. Sex = selectedColumnList.Contains("XBM") ? XB : db.Sex,
  211. CertificatesType = selectedColumnList.Contains("SFZJLXM") ? SFZJLX : db.CertificatesType,
  212. IDNumber = selectedColumnList.Contains("SFZJH") ? SFZJH : db.IDNumber,
  213. StudentType = db.StudentType,
  214. InSchoolStatusID = db.InSchoolStatusID,
  215. StudentStatus = db.StudentStatus,
  216. CultureModel = db.CultureModel,
  217. IsProofread = db.IsProofread,
  218. IsDreamProject = db.IsDreamProject,
  219. StudentCardNo = selectedColumnList.Contains("XH") ? XH : db.StudentCardNo,
  220. PlanningGraduateDate = db.PlanningGraduateDate,
  221. GraduateDate = db.GraduateDate,
  222. GraduateCardNo = db.GraduateCardNo,
  223. ReplaceGraduateNo = db.ReplaceGraduateNo,
  224. DegreeStatus = db.DegreeStatus,
  225. PhotoUrl = db.PhotoUrl,
  226. Career = db.Career
  227. };
  228. var profile = new CF_StudentProfile
  229. {
  230. UserID = db.UserID,
  231. UsedName = db.CF_StudentProfile.UsedName,
  232. DirectorName = db.CF_StudentProfile.DirectorName,
  233. BirthDate = selectedColumnList.Contains("CSRQ") ? CSRQ : db.CF_StudentProfile.BirthDate,
  234. Country = db.CF_StudentProfile.Country,
  235. Politics = db.CF_StudentProfile.Politics,
  236. LiteracyLevels = db.CF_StudentProfile.LiteracyLevels,
  237. Place = selectedColumnList.Contains("JG") ? JG : db.CF_StudentProfile.Place,
  238. Nation = selectedColumnList.Contains("MZM") ? MZ : db.CF_StudentProfile.Nation,
  239. Healthy = db.CF_StudentProfile.Healthy,
  240. BloodGroup = db.CF_StudentProfile.BloodGroup,
  241. BornPlace = db.CF_StudentProfile.BornPlace,
  242. Specialty = db.CF_StudentProfile.Specialty,
  243. Height = db.CF_StudentProfile.Height,
  244. Weight = db.CF_StudentProfile.Weight,
  245. Remarks = db.CF_StudentProfile.Remarks,
  246. GraduationPictureUrl = db.CF_StudentProfile.GraduationPictureUrl,
  247. RecordStatus = db.CF_StudentProfile.RecordStatus,
  248. CreateUserID = db.CF_StudentProfile.CreateUserID,
  249. CreateTime = db.CF_StudentProfile.CreateTime,
  250. ModifyUserID = db.CF_StudentProfile.ModifyUserID,
  251. ModifyTime = nowTime
  252. };
  253. var studentContact = new CF_StudentContact
  254. {
  255. UserID = db.UserID,
  256. Email = selectedColumnList.Contains("DZYX") ? DZYX : db.CF_StudentContact.Email,
  257. QQ = db.CF_StudentContact.QQ,
  258. Mobile = selectedColumnList.Contains("DH") ? Mobile : db.CF_StudentContact.Mobile,
  259. Telephone = selectedColumnList.Contains("DH") ? DH : db.CF_StudentContact.Telephone,
  260. MicroMsgNo = db.CF_StudentContact.MicroMsgNo,
  261. Zipcode = db.CF_StudentContact.Zipcode,
  262. Address = selectedColumnList.Contains("TXDZ") ? TXDZ : db.CF_StudentContact.Address,
  263. WorkUnit = db.CF_StudentContact.WorkUnit,
  264. HomeAddress = db.CF_StudentContact.HomeAddress,
  265. Recipient = db.CF_StudentContact.Recipient,
  266. Dormitory = db.CF_StudentContact.Dormitory,
  267. RecordStatus = db.CF_StudentContact.RecordStatus,
  268. CreateUserID = db.CF_StudentContact.CreateUserID,
  269. CreateTime = db.CF_StudentContact.CreateTime,
  270. ModifyUserID = db.CF_StudentContact.ModifyUserID,
  271. ModifyTime = nowTime
  272. };
  273. updateList.Add(user);
  274. updateStudentList.Add(student);
  275. updateStudentProfileList.Add(profile);
  276. updateStudentContactList.Add(studentContact);
  277. }
  278. }
  279. #endregion
  280. using (var scope = new TransactionScope(TransactionScopeOption.RequiresNew, new TransactionOptions { IsolationLevel = IsolationLevel.Serializable }))
  281. {
  282. UnitOfWork.BulkInsert(insertList);
  283. UnitOfWork.BulkInsert<Sys_User, Sys_Role>(insertRoleList);
  284. UnitOfWork.BulkInsert(insertStudentList);
  285. UnitOfWork.BulkInsert(insertStudentProfileList);
  286. UnitOfWork.BulkInsert(insertStudentContactList);
  287. UnitOfWork.BulkInsert(insertStudentAccountList);
  288. UnitOfWork.BulkInsert(insertRecruitstudentsList);
  289. UnitOfWork.BatchUpdate(updateList);
  290. UnitOfWork.BatchUpdate(updateStudentList);
  291. UnitOfWork.BatchUpdate(updateStudentProfileList);
  292. UnitOfWork.BatchUpdate(updateStudentContactList);
  293. scope.Complete();
  294. }
  295. ParameterServices.Value.SaveTo(CF_ParameterType.SynchStudentLastTime, nowTime);
  296. }
  297. }
  298. }