CoursematerialServices.cs 21 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436
  1. using System;
  2. using System.Collections.Generic;
  3. using System.Linq;
  4. using System.Text;
  5. using System.Text.RegularExpressions;
  6. using Bowin.Common.Utility;
  7. using EMIS.Entities;
  8. using EMIS.ViewModel;
  9. using EMIS.ViewModel.CacheManage;
  10. using EMIS.ViewModel.Cultureplan;
  11. namespace EMIS.ExtensionLogic.ServiceLogic.CultureplanManage
  12. {
  13. public class CoursematerialServices : EMIS.CommonLogic.Cultureplan.CoursematerialServices
  14. {
  15. /// <summary>
  16. /// 编辑(新增、修改)
  17. /// 注:业务主键为课程代码唯一(河北工业大学,相同课程名称不同课程代码,用于区分专业基础课、专业课)
  18. /// </summary>
  19. /// <param name="coursematerialView"></param>
  20. public override void CoursematerialEdit(CoursematerialView coursematerialView)
  21. {
  22. try
  23. {
  24. //查询数据库进行验证
  25. var courseCodeVerify = CoursematerialDAL.CoursematerialRepository
  26. .GetList(x => x.CoursematerialID != coursematerialView.CoursematerialID
  27. && x.CourseCode == coursematerialView.CourseCode).SingleOrDefault();
  28. if (courseCodeVerify == null)
  29. {
  30. //数据有误验证
  31. if (coursematerialView.CoursematerialID != Guid.Empty)
  32. {
  33. var course = CoursematerialDAL.CoursematerialRepository
  34. .GetList(x => x.CoursematerialID == coursematerialView.CoursematerialID).SingleOrDefault();
  35. if (course == null)
  36. {
  37. throw new Exception("数据有误,请核查。");
  38. }
  39. else
  40. {
  41. //表示修改
  42. course.CourseCode = coursematerialView.CourseCode;
  43. course.CourseName = coursematerialView.CourseName;
  44. course.Abbreviation = coursematerialView.Abbreviation;
  45. course.EnglishName = coursematerialView.EnglishName;
  46. course.StandardName = coursematerialView.StandardName;
  47. course.CourseEdition = coursematerialView.CourseEdition;
  48. course.CourseLevelID = coursematerialView.CourseLevelID;
  49. course.CourseScienceID = coursematerialView.CourseScienceID;
  50. course.ClassGroupingID = coursematerialView.ClassGroupingID;
  51. course.CourseSynopsis = coursematerialView.CourseSynopsis;
  52. course.CourseReserve = coursematerialView.CourseReserve;
  53. course.IsEnable = coursematerialView.IsEnable;
  54. course.Remark = coursematerialView.Remark;
  55. SetModifyStatus(course);
  56. }
  57. }
  58. else
  59. {
  60. //表示新增
  61. var newCourse = new EM_Coursematerial();
  62. newCourse.CoursematerialID = Guid.NewGuid();
  63. newCourse.CourseCode = coursematerialView.CourseCode;
  64. newCourse.CourseName = coursematerialView.CourseName;
  65. newCourse.Abbreviation = coursematerialView.Abbreviation;
  66. newCourse.EnglishName = coursematerialView.EnglishName;
  67. newCourse.StandardName = coursematerialView.StandardName;
  68. newCourse.CourseEdition = coursematerialView.CourseEdition;
  69. newCourse.CourseLevelID = coursematerialView.CourseLevelID;
  70. newCourse.CourseScienceID = coursematerialView.CourseScienceID;
  71. newCourse.ClassGroupingID = coursematerialView.ClassGroupingID;
  72. newCourse.CourseSynopsis = coursematerialView.CourseSynopsis;
  73. newCourse.CourseReserve = coursematerialView.CourseReserve;
  74. newCourse.IsEnable = coursematerialView.IsEnable;
  75. newCourse.Remark = coursematerialView.Remark;
  76. SetNewStatus(newCourse);
  77. UnitOfWork.Add(newCourse);
  78. }
  79. //事务提交
  80. UnitOfWork.Commit();
  81. }
  82. else
  83. {
  84. throw new Exception("已存在相同的课程代码,请核查。");
  85. }
  86. }
  87. catch (Exception ex)
  88. {
  89. throw new Exception(ex.Message);
  90. }
  91. }
  92. /// <summary>
  93. /// 课程信息导入
  94. /// 注:业务主键为课程代码唯一(河北工业大学,相同课程名称不同课程代码,用于区分专业基础课、专业课)
  95. /// </summary>
  96. /// <param name="cellheader"></param>
  97. /// <param name="inCount"></param>
  98. /// <param name="upCount"></param>
  99. /// <param name="errdataList"></param>
  100. /// <param name="errCount"></param>
  101. /// <param name="sourcePhysicalPath"></param>
  102. public override void CoursematerialImport(Dictionary<string, string> cellheader, out int? inCount, out int? upCount, out List<CoursematerialView> errdataList, out int? errCount, string sourcePhysicalPath)
  103. {
  104. try
  105. {
  106. StringBuilder errorMsg = new StringBuilder(); // 错误信息
  107. List<CoursematerialView> errList = new List<CoursematerialView>();
  108. // 1.1解析文件,存放到一个List集合里
  109. cellheader.Remove("ErrorMessage");//移除“未导入原因”列(ErrorMessage)
  110. List<CoursematerialView> enlist = NpoiExcelHelper.ExcelToEntityList<CoursematerialView>(cellheader, sourcePhysicalPath, out errorMsg, out errList);
  111. cellheader.Add("ErrorMessage", "未导入原因");
  112. //对List集合进行有效性校验
  113. if (enlist.Count() <= 0)
  114. {
  115. throw new Exception("Excel文件数据为空,请检查。");
  116. }
  117. Regex reg = null; //正则表达式
  118. //DateTime result; //用于返回判断日期字段格式
  119. //bool isListPass = true; //List判断标识
  120. inCount = 0; //导入个数
  121. upCount = 0; //更新个数
  122. errCount = 0; //失败个数
  123. string errorMsgStr = ""; //错误信息
  124. List<EM_Coursematerial> newCoursematerialInList = new List<EM_Coursematerial>();
  125. List<EM_Coursematerial> newCoursematerialUpList = new List<EM_Coursematerial>();
  126. //将循环中相关数据库查询统一查询出来进行匹配(尽量避免在循环中进行数据库查询)
  127. //课程信息
  128. var courseList = CoursematerialDAL.CoursematerialRepository.GetList(x => true).ToList();
  129. //课程代码
  130. var courseCodeList = enlist.Where(x => !string.IsNullOrWhiteSpace(x.CourseCode)).Select(x => x.CourseCode.Trim()).ToList();
  131. //课程名称
  132. var courseNameList = enlist.Where(x => !string.IsNullOrWhiteSpace(x.CourseName)).Select(x => x.CourseName.Trim()).ToList();
  133. //对比后的newCourseList
  134. var newCourseList = courseList.Where(x => courseCodeList.Contains(x.CourseCode) || courseNameList.Contains(x.CourseName)).ToList();
  135. //课程级别
  136. var courseLevelList = IdNameExt.GetDictionaryItem(DictionaryItem.CF_CourseLevel).ToList();
  137. //课程科类
  138. var courseScienceList = IdNameExt.GetDictionaryItem(DictionaryItem.CF_CourseScience).ToList();
  139. //上课类型信息
  140. var classGroupingList = CoursematerialDAL.ClassGroupingRepository.GetList(x => true).ToList();
  141. //上课类型
  142. var classNameList = enlist.Where(x => !string.IsNullOrWhiteSpace(x.ClassName)).Select(x => x.ClassName.Trim()).ToList();
  143. //对比后的newClassGroupingList
  144. var newClassGroupingList = classGroupingList.Where(x => classNameList.Contains(x.ClassName)).ToList();
  145. //是否启用
  146. var generalPurposeList = IdNameExt.GetDictionaryItem(DictionaryItem.CF_GeneralPurpose).ToList();
  147. //循环检测数据列,对各数据列进行验证(必填、字典项验证、数据格式等)
  148. for (int i = 0; i < enlist.Count; i++)
  149. {
  150. CoursematerialView en = enlist[i]; //Excel表数据视图
  151. EM_Coursematerial newCoursematerial = new EM_Coursematerial();
  152. //课程代码
  153. if (string.IsNullOrWhiteSpace(en.CourseCode))
  154. {
  155. errCount++;
  156. errorMsgStr = "课程代码不能为空";
  157. en.ErrorMessage = errorMsgStr;
  158. errList.Add(en);
  159. errorMsg.AppendLine(errorMsgStr);
  160. continue;
  161. }
  162. else
  163. {
  164. reg = new Regex(@"^[0-9a-zA-Z\s?]+$"); //正则表达式(请输入数字或英文字母)
  165. if (!reg.IsMatch(en.CourseCode.Trim()))
  166. {
  167. errCount++;
  168. errorMsgStr = "课程代码格式不正确,请检查(数字或英文字母)";
  169. en.ErrorMessage = errorMsgStr;
  170. errList.Add(en);
  171. errorMsg.AppendLine(errorMsgStr);
  172. continue;
  173. }
  174. else
  175. {
  176. //课程代码
  177. newCoursematerial.CourseCode = en.CourseCode.Trim();
  178. }
  179. }
  180. //课程名称
  181. if (string.IsNullOrWhiteSpace(en.CourseName))
  182. {
  183. errCount++;
  184. errorMsgStr = "课程名称不能为空";
  185. en.ErrorMessage = errorMsgStr;
  186. errList.Add(en);
  187. errorMsg.AppendLine(errorMsgStr);
  188. continue;
  189. }
  190. else
  191. {
  192. //课程名称
  193. newCoursematerial.CourseName = en.CourseName.Trim();
  194. }
  195. //课程简称
  196. if (string.IsNullOrWhiteSpace(en.Abbreviation))
  197. {
  198. //默认
  199. newCoursematerial.Abbreviation = en.CourseName.Trim();
  200. }
  201. else
  202. {
  203. newCoursematerial.Abbreviation = en.Abbreviation.Trim();
  204. }
  205. //英文名称
  206. if (string.IsNullOrWhiteSpace(en.EnglishName))
  207. {
  208. //暂不考虑
  209. }
  210. else
  211. {
  212. newCoursematerial.EnglishName = en.EnglishName.Trim();
  213. }
  214. //课程规范名
  215. if (string.IsNullOrWhiteSpace(en.StandardName))
  216. {
  217. //默认
  218. newCoursematerial.StandardName = en.CourseName.Trim();
  219. }
  220. else
  221. {
  222. newCoursematerial.StandardName = en.StandardName.Trim();
  223. }
  224. //课程版本
  225. if (string.IsNullOrWhiteSpace(en.CourseEdition))
  226. {
  227. //暂不考虑
  228. }
  229. else
  230. {
  231. newCoursematerial.CourseEdition = en.CourseEdition.Trim();
  232. }
  233. //课程级别
  234. if (string.IsNullOrWhiteSpace(en.CourseLevelStr))
  235. {
  236. //暂不考虑
  237. }
  238. else
  239. {
  240. var courseLevel = courseLevelList.Where(x => x.Name == en.CourseLevelStr.Trim()).SingleOrDefault();
  241. if (courseLevel == null)
  242. {
  243. errCount++;
  244. errorMsgStr = "课程级别不存在,请检查";
  245. en.ErrorMessage = errorMsgStr;
  246. errList.Add(en);
  247. errorMsg.AppendLine(errorMsgStr);
  248. continue;
  249. }
  250. else
  251. {
  252. //课程级别
  253. newCoursematerial.CourseLevelID = courseLevel.Value;
  254. }
  255. }
  256. //课程科类
  257. if (string.IsNullOrWhiteSpace(en.CourseScienceStr))
  258. {
  259. //暂不考虑
  260. }
  261. else
  262. {
  263. var courseScience = courseScienceList.Where(x => x.Name == en.CourseScienceStr.Trim()).SingleOrDefault();
  264. if (courseScience == null)
  265. {
  266. errCount++;
  267. errorMsgStr = "课程科类不存在,请检查";
  268. en.ErrorMessage = errorMsgStr;
  269. errList.Add(en);
  270. errorMsg.AppendLine(errorMsgStr);
  271. continue;
  272. }
  273. else
  274. {
  275. //课程科类
  276. newCoursematerial.CourseScienceID = courseScience.Value;
  277. }
  278. }
  279. //上课类型
  280. if (string.IsNullOrWhiteSpace(en.ClassName))
  281. {
  282. //暂不考虑
  283. }
  284. else
  285. {
  286. var classGrouping = newClassGroupingList.Where(x => x.ClassName == en.ClassName.Trim()).SingleOrDefault();
  287. if (classGrouping == null)
  288. {
  289. errCount++;
  290. errorMsgStr = "上课类型不存在,请检查";
  291. en.ErrorMessage = errorMsgStr;
  292. errList.Add(en);
  293. errorMsg.AppendLine(errorMsgStr);
  294. continue;
  295. }
  296. else
  297. {
  298. //上课类型ID
  299. newCoursematerial.ClassGroupingID = classGrouping.ClassGroupingID;
  300. }
  301. }
  302. //课程简介
  303. if (string.IsNullOrWhiteSpace(en.CourseSynopsis))
  304. {
  305. //暂不考虑
  306. }
  307. else
  308. {
  309. newCoursematerial.CourseSynopsis = en.CourseSynopsis.Trim();
  310. }
  311. //预修课程集
  312. if (string.IsNullOrWhiteSpace(en.CourseReserve))
  313. {
  314. //暂不考虑
  315. }
  316. else
  317. {
  318. newCoursematerial.CourseReserve = en.CourseReserve.Trim();
  319. }
  320. //是否启用
  321. if (string.IsNullOrWhiteSpace(en.IsEnableStr))
  322. {
  323. errCount++;
  324. errorMsgStr = "是否启用不能为空";
  325. en.ErrorMessage = errorMsgStr;
  326. errList.Add(en);
  327. errorMsg.AppendLine(errorMsgStr);
  328. continue;
  329. }
  330. else
  331. {
  332. var generalPurpose = generalPurposeList.Where(x => x.Name == en.IsEnableStr.Trim()).SingleOrDefault();
  333. if (generalPurpose == null)
  334. {
  335. errCount++;
  336. errorMsgStr = "是否启用不存在,请检查";
  337. en.ErrorMessage = errorMsgStr;
  338. errList.Add(en);
  339. errorMsg.AppendLine(errorMsgStr);
  340. continue;
  341. }
  342. else
  343. {
  344. //是否启用
  345. newCoursematerial.IsEnable = generalPurpose.Value == (int)CF_GeneralPurpose.IsYes ? true : false;
  346. }
  347. }
  348. //备注
  349. if (string.IsNullOrWhiteSpace(en.Remark))
  350. {
  351. //不考虑
  352. }
  353. else
  354. {
  355. newCoursematerial.Remark = en.Remark.Trim();
  356. }
  357. ////Excel表重复性验证(注:当数据表中没有此记录,但是Excel中有重复数据时的去掉)
  358. //for (int j = i + 1; j < enlist.Count; j++)
  359. //{
  360. // CoursematerialView enA = enlist[j];
  361. // //根据Excel表中的业务主键进行去重(课程代码唯一)
  362. // if (en.No == enA.No && en.Name == enA.Name)
  363. // {
  364. // //用于标识Excel表中的重复记录(由于是批量进行插入数据表)
  365. // }
  366. //}
  367. //数据表重复性验证(课程代码唯一)
  368. var courseCodeVerify = newCourseList.Where(x => x.CourseCode == newCoursematerial.CourseCode).SingleOrDefault();
  369. if (courseCodeVerify == null)
  370. {
  371. //新增
  372. if (!newCoursematerialInList.Any(x => x.CourseCode == newCoursematerial.CourseCode))
  373. {
  374. newCoursematerial.CoursematerialID = Guid.NewGuid();
  375. SetNewStatus(newCoursematerial);
  376. newCoursematerialInList.Add(newCoursematerial);
  377. inCount++;
  378. }
  379. else
  380. {
  381. //Excel表重复性验证
  382. //(注:当数据表中没有此记录,但是Excel中有重复数据,可在此处进行抛出到失败数据文件中,目前暂不考虑)
  383. inCount++;
  384. }
  385. }
  386. else
  387. {
  388. //更新(Excel有重复时,以最后一条记录的更新为准)
  389. //courseCodeVerify.CourseCode = newCoursematerial.CourseCode;
  390. courseCodeVerify.CourseName = newCoursematerial.CourseName;
  391. courseCodeVerify.Abbreviation = newCoursematerial.Abbreviation;
  392. courseCodeVerify.EnglishName = newCoursematerial.EnglishName;
  393. courseCodeVerify.StandardName = newCoursematerial.StandardName;
  394. courseCodeVerify.CourseEdition = newCoursematerial.CourseEdition;
  395. courseCodeVerify.CourseLevelID = newCoursematerial.CourseLevelID;
  396. courseCodeVerify.CourseScienceID = newCoursematerial.CourseScienceID;
  397. courseCodeVerify.ClassGroupingID = newCoursematerial.ClassGroupingID;
  398. courseCodeVerify.CourseSynopsis = newCoursematerial.CourseSynopsis;
  399. courseCodeVerify.CourseReserve = newCoursematerial.CourseReserve;
  400. courseCodeVerify.IsEnable = newCoursematerial.IsEnable;
  401. courseCodeVerify.Remark = newCoursematerial.Remark;
  402. SetModifyStatus(courseCodeVerify);
  403. newCoursematerialUpList.Add(courseCodeVerify);
  404. upCount++;
  405. }
  406. }
  407. //批量插入
  408. UnitOfWork.BulkInsert(newCoursematerialInList);
  409. //批量统一提交更新
  410. if (newCoursematerialUpList != null && newCoursematerialUpList.Count() > 0)
  411. {
  412. UnitOfWork.BatchUpdate(newCoursematerialUpList);
  413. }
  414. errdataList = errList.Distinct().ToList(); //错误列表List
  415. }
  416. catch (Exception)
  417. {
  418. throw;
  419. }
  420. }
  421. }
  422. }