ClassmajorSynchServices.cs 6.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104
  1. using System;
  2. using System.Collections.Generic;
  3. using System.Linq;
  4. using System.Text;
  5. using System.Transactions;
  6. using Bowin.Common.Linq.Entity;
  7. using EMIS.CommonLogic.Specialtyclass;
  8. using EMIS.CommonLogic.SystemServices;
  9. using EMIS.DataLogic.DataCenterSynch;
  10. using EMIS.Entities;
  11. using EMIS.ViewModel;
  12. namespace EMIS.CommonLogic.DataCenterSynch
  13. {
  14. public class ClassmajorSynchServices : ClassmajorServices, IClassmajorSynchServices
  15. {
  16. public Lazy<IParameterServices> ParameterServices { get; set; }
  17. public ClassmajorSynchDAL ClassmajorSynchDAL { get; set; }
  18. public void Synchr()
  19. {
  20. var lastSynchTime = ParameterServices.Value.GetParameterValue<DateTime>(CF_ParameterType.SynchClassmajorLastTime);
  21. var nowTime = DateTime.Now;
  22. var allClassmajorList = ClassmajorSynchDAL.XXBZ_JWXT_BJXX_VIEWRepository.GetList(x => x.TIMESTAMPS >= lastSynchTime || lastSynchTime == null).ToList();
  23. var allFacultyCodeList = allClassmajorList.Select(x => x.USER_ZYDM.Trim()).Distinct().ToList();
  24. var allClassmajorNoList = allClassmajorList.Select(x => x.BJDM.Trim()).Distinct().ToList();
  25. var dbFacultyList = this.ClassmajorDAL.FacultymajorRepository.GetList(x => allFacultyCodeList.Contains(x.Code)).ToList();
  26. var allGradeList = (from all in allClassmajorList
  27. from faculty in dbFacultyList.Where(x => x.Code == all.USER_ZYDM.Trim())
  28. group new { all, faculty } by new { faculty.FacultymajorID, SchoolyearID = (!string.IsNullOrEmpty(all.NJ) ? (int?)Convert.ToInt32(all.NJ) : null) } into g
  29. select g.Key).ToList();
  30. var dbGradeList = this.ClassmajorDAL.GrademajorRepository.Entities.SelectByKeys(allGradeList);
  31. var dbClassmajorList = this.ClassmajorDAL.ClassmajorRepository.GetList(x => allClassmajorNoList.Contains(x.No)).ToList();
  32. var insertGradeList = (from all in allGradeList
  33. from faculty in dbFacultyList.Where(x => x.FacultymajorID == all.FacultymajorID)
  34. from db in dbGradeList.Where(x => x.FacultymajorID == faculty.FacultymajorID && x.SchoolyearID == all.SchoolyearID).DefaultIfEmpty()
  35. where db == null
  36. select new CF_Grademajor
  37. {
  38. GrademajorID = Guid.NewGuid(),
  39. FacultymajorID = faculty.FacultymajorID,
  40. Code = faculty.Code + all.SchoolyearID.ToString(),
  41. Name = all.SchoolyearID.ToString() + faculty.Name,
  42. SchoolyearID = all.SchoolyearID,
  43. RecordStatus = (int)SYS_STATUS.USABLE,
  44. CreateTime = nowTime,
  45. ModifyTime = nowTime
  46. }).ToList();
  47. var allDbGradeList = dbGradeList.Concat(insertGradeList).ToList();
  48. var insertClassmajorList = (from all in allClassmajorList
  49. from faculty in dbFacultyList.Where(x => x.Code == all.USER_ZYDM.Trim())
  50. from grade in allDbGradeList.Where(x => x.FacultymajorID == faculty.FacultymajorID && x.SchoolyearID == (!string.IsNullOrEmpty(all.NJ) ? (int?)Convert.ToInt32(all.NJ) : null))
  51. from db in ClassmajorDAL.ClassmajorRepository.Entities.Where(x => x.No == all.BJDM.Trim()).DefaultIfEmpty()
  52. where db == null
  53. select new CF_Classmajor
  54. {
  55. ClassmajorID = Guid.NewGuid(),
  56. GrademajorID = grade.GrademajorID,
  57. No = all.BJDM.Trim(),
  58. Name = all.BJMC.Trim(),
  59. Abbreviation = "",
  60. RecordStatus = (int)SYS_STATUS.USABLE,
  61. CreateTime = nowTime,
  62. ModifyTime = nowTime
  63. }).ToList();
  64. var updateList = (from all in allClassmajorList
  65. from faculty in dbFacultyList.Where(x => x.Code == all.USER_ZYDM.Trim())
  66. from grade in allDbGradeList.Where(x => x.FacultymajorID == faculty.FacultymajorID && x.SchoolyearID == (!string.IsNullOrEmpty(all.NJ) ? (int?)Convert.ToInt32(all.NJ) : null))
  67. from db in ClassmajorDAL.ClassmajorRepository.Entities.Where(x => x.No == all.BJDM.Trim())
  68. select new CF_Classmajor
  69. {
  70. ClassmajorID = db.ClassmajorID,
  71. GrademajorID = grade.GrademajorID,
  72. No = all.BJDM.Trim(),
  73. Name = all.BJMC.Trim(),
  74. Abbreviation = db.Abbreviation,
  75. EnglishName = db.EnglishName,
  76. ClassNum = db.ClassNum,
  77. UserID = db.UserID,
  78. AssistantUserID = db.AssistantUserID,
  79. Fixedclassroom = db.Fixedclassroom,
  80. Remark = db.Remark,
  81. RecordStatus = db.RecordStatus,
  82. CreateUserID = db.CreateUserID,
  83. CreateTime = db.CreateTime,
  84. ModifyUserID = db.ModifyUserID,
  85. ModifyTime = nowTime
  86. }).ToList();
  87. using (var scope = new TransactionScope(TransactionScopeOption.RequiresNew, new TransactionOptions { IsolationLevel = IsolationLevel.Serializable }))
  88. {
  89. UnitOfWork.BulkInsert(insertGradeList);
  90. UnitOfWork.BulkInsert(insertClassmajorList);
  91. UnitOfWork.BatchUpdate(updateList);
  92. scope.Complete();
  93. }
  94. ParameterServices.Value.SaveTo(CF_ParameterType.SynchClassmajorLastTime, nowTime);
  95. }
  96. }
  97. }