CoursematerialServices.cs 21 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437
  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,
  103. out List<CoursematerialView> errdataList, out int? errCount, string sourcePhysicalPath)
  104. {
  105. try
  106. {
  107. StringBuilder errorMsg = new StringBuilder(); // 错误信息
  108. List<CoursematerialView> errList = new List<CoursematerialView>();
  109. // 1.1解析文件,存放到一个List集合里
  110. cellheader.Remove("ErrorMessage");//移除“未导入原因”列(ErrorMessage)
  111. List<CoursematerialView> enlist = NpoiExcelHelper.ExcelToEntityList<CoursematerialView>(cellheader, sourcePhysicalPath, out errorMsg, out errList);
  112. cellheader.Add("ErrorMessage", "未导入原因");
  113. //对List集合进行有效性校验
  114. if (enlist.Count() <= 0)
  115. {
  116. throw new Exception("Excel文件数据为空,请检查。");
  117. }
  118. Regex reg = null; //正则表达式
  119. //DateTime result; //用于返回判断日期字段格式
  120. //bool isListPass = true; //List判断标识
  121. inCount = 0; //导入个数
  122. upCount = 0; //更新个数
  123. errCount = 0; //失败个数
  124. string errorMsgStr = ""; //错误信息
  125. List<EM_Coursematerial> newCoursematerialInList = new List<EM_Coursematerial>();
  126. List<EM_Coursematerial> newCoursematerialUpList = new List<EM_Coursematerial>();
  127. //将循环中相关数据库查询统一查询出来进行匹配(尽量避免在循环中进行数据库查询)
  128. //课程信息
  129. var courseList = CoursematerialDAL.CoursematerialRepository.GetList(x => true).ToList();
  130. //课程代码
  131. var courseCodeList = enlist.Where(x => !string.IsNullOrWhiteSpace(x.CourseCode)).Select(x => x.CourseCode.Trim()).ToList();
  132. //课程名称
  133. var courseNameList = enlist.Where(x => !string.IsNullOrWhiteSpace(x.CourseName)).Select(x => x.CourseName.Trim()).ToList();
  134. //对比后的newCourseList
  135. var newCourseList = courseList.Where(x => courseCodeList.Contains(x.CourseCode) || courseNameList.Contains(x.CourseName)).ToList();
  136. //课程级别
  137. var courseLevelList = IdNameExt.GetDictionaryItem(DictionaryItem.CF_CourseLevel).ToList();
  138. //课程科类
  139. var courseScienceList = IdNameExt.GetDictionaryItem(DictionaryItem.CF_CourseScience).ToList();
  140. //上课类型信息
  141. var classGroupingList = CoursematerialDAL.ClassGroupingRepository.GetList(x => true).ToList();
  142. //上课类型
  143. var classNameList = enlist.Where(x => !string.IsNullOrWhiteSpace(x.ClassName)).Select(x => x.ClassName.Trim()).ToList();
  144. //对比后的newClassGroupingList
  145. var newClassGroupingList = classGroupingList.Where(x => classNameList.Contains(x.ClassName)).ToList();
  146. //是否启用
  147. var generalPurposeList = IdNameExt.GetDictionaryItem(DictionaryItem.CF_GeneralPurpose).ToList();
  148. //循环检测数据列,对各数据列进行验证(必填、字典项验证、数据格式等)
  149. for (int i = 0; i < enlist.Count; i++)
  150. {
  151. CoursematerialView en = enlist[i]; //Excel表数据视图
  152. EM_Coursematerial newCoursematerial = new EM_Coursematerial();
  153. //课程代码
  154. if (string.IsNullOrWhiteSpace(en.CourseCode))
  155. {
  156. errCount++;
  157. errorMsgStr = "课程代码不能为空";
  158. en.ErrorMessage = errorMsgStr;
  159. errList.Add(en);
  160. errorMsg.AppendLine(errorMsgStr);
  161. continue;
  162. }
  163. else
  164. {
  165. reg = new Regex(@"^[0-9a-zA-Z\s?]+$"); //正则表达式(请输入数字或英文字母)
  166. if (!reg.IsMatch(en.CourseCode.Trim()))
  167. {
  168. errCount++;
  169. errorMsgStr = "课程代码格式不正确,请检查(数字或英文字母)";
  170. en.ErrorMessage = errorMsgStr;
  171. errList.Add(en);
  172. errorMsg.AppendLine(errorMsgStr);
  173. continue;
  174. }
  175. else
  176. {
  177. //课程代码
  178. newCoursematerial.CourseCode = en.CourseCode.Trim();
  179. }
  180. }
  181. //课程名称
  182. if (string.IsNullOrWhiteSpace(en.CourseName))
  183. {
  184. errCount++;
  185. errorMsgStr = "课程名称不能为空";
  186. en.ErrorMessage = errorMsgStr;
  187. errList.Add(en);
  188. errorMsg.AppendLine(errorMsgStr);
  189. continue;
  190. }
  191. else
  192. {
  193. //课程名称
  194. newCoursematerial.CourseName = en.CourseName.Trim();
  195. }
  196. //课程简称
  197. if (string.IsNullOrWhiteSpace(en.Abbreviation))
  198. {
  199. //默认
  200. newCoursematerial.Abbreviation = en.CourseName.Trim();
  201. }
  202. else
  203. {
  204. newCoursematerial.Abbreviation = en.Abbreviation.Trim();
  205. }
  206. //英文名称
  207. if (string.IsNullOrWhiteSpace(en.EnglishName))
  208. {
  209. //暂不考虑
  210. }
  211. else
  212. {
  213. newCoursematerial.EnglishName = en.EnglishName.Trim();
  214. }
  215. //课程规范名
  216. if (string.IsNullOrWhiteSpace(en.StandardName))
  217. {
  218. //默认
  219. newCoursematerial.StandardName = en.CourseName.Trim();
  220. }
  221. else
  222. {
  223. newCoursematerial.StandardName = en.StandardName.Trim();
  224. }
  225. //课程版本
  226. if (string.IsNullOrWhiteSpace(en.CourseEdition))
  227. {
  228. //暂不考虑
  229. }
  230. else
  231. {
  232. newCoursematerial.CourseEdition = en.CourseEdition.Trim();
  233. }
  234. //课程级别
  235. if (string.IsNullOrWhiteSpace(en.CourseLevelStr))
  236. {
  237. //暂不考虑
  238. }
  239. else
  240. {
  241. var courseLevel = courseLevelList.Where(x => x.Name == en.CourseLevelStr.Trim()).SingleOrDefault();
  242. if (courseLevel == null)
  243. {
  244. errCount++;
  245. errorMsgStr = "课程级别不存在,请检查";
  246. en.ErrorMessage = errorMsgStr;
  247. errList.Add(en);
  248. errorMsg.AppendLine(errorMsgStr);
  249. continue;
  250. }
  251. else
  252. {
  253. //课程级别
  254. newCoursematerial.CourseLevelID = courseLevel.Value;
  255. }
  256. }
  257. //课程科类
  258. if (string.IsNullOrWhiteSpace(en.CourseScienceStr))
  259. {
  260. //暂不考虑
  261. }
  262. else
  263. {
  264. var courseScience = courseScienceList.Where(x => x.Name == en.CourseScienceStr.Trim()).SingleOrDefault();
  265. if (courseScience == null)
  266. {
  267. errCount++;
  268. errorMsgStr = "课程科类不存在,请检查";
  269. en.ErrorMessage = errorMsgStr;
  270. errList.Add(en);
  271. errorMsg.AppendLine(errorMsgStr);
  272. continue;
  273. }
  274. else
  275. {
  276. //课程科类
  277. newCoursematerial.CourseScienceID = courseScience.Value;
  278. }
  279. }
  280. //上课类型
  281. if (string.IsNullOrWhiteSpace(en.ClassName))
  282. {
  283. //暂不考虑
  284. }
  285. else
  286. {
  287. var classGrouping = newClassGroupingList.Where(x => x.ClassName == en.ClassName.Trim()).SingleOrDefault();
  288. if (classGrouping == null)
  289. {
  290. errCount++;
  291. errorMsgStr = "上课类型不存在,请检查";
  292. en.ErrorMessage = errorMsgStr;
  293. errList.Add(en);
  294. errorMsg.AppendLine(errorMsgStr);
  295. continue;
  296. }
  297. else
  298. {
  299. //上课类型ID
  300. newCoursematerial.ClassGroupingID = classGrouping.ClassGroupingID;
  301. }
  302. }
  303. //课程简介
  304. if (string.IsNullOrWhiteSpace(en.CourseSynopsis))
  305. {
  306. //暂不考虑
  307. }
  308. else
  309. {
  310. newCoursematerial.CourseSynopsis = en.CourseSynopsis.Trim();
  311. }
  312. //预修课程集
  313. if (string.IsNullOrWhiteSpace(en.CourseReserve))
  314. {
  315. //暂不考虑
  316. }
  317. else
  318. {
  319. newCoursematerial.CourseReserve = en.CourseReserve.Trim();
  320. }
  321. //是否启用
  322. if (string.IsNullOrWhiteSpace(en.IsEnableStr))
  323. {
  324. errCount++;
  325. errorMsgStr = "是否启用不能为空";
  326. en.ErrorMessage = errorMsgStr;
  327. errList.Add(en);
  328. errorMsg.AppendLine(errorMsgStr);
  329. continue;
  330. }
  331. else
  332. {
  333. var generalPurpose = generalPurposeList.Where(x => x.Name == en.IsEnableStr.Trim()).SingleOrDefault();
  334. if (generalPurpose == null)
  335. {
  336. errCount++;
  337. errorMsgStr = "是否启用不存在,请检查";
  338. en.ErrorMessage = errorMsgStr;
  339. errList.Add(en);
  340. errorMsg.AppendLine(errorMsgStr);
  341. continue;
  342. }
  343. else
  344. {
  345. //是否启用
  346. newCoursematerial.IsEnable = generalPurpose.Value == (int)CF_GeneralPurpose.IsYes ? true : false;
  347. }
  348. }
  349. //备注
  350. if (string.IsNullOrWhiteSpace(en.Remark))
  351. {
  352. //不考虑
  353. }
  354. else
  355. {
  356. newCoursematerial.Remark = en.Remark.Trim();
  357. }
  358. ////Excel表重复性验证(注:当数据表中没有此记录,但是Excel中有重复数据时的去掉)
  359. //for (int j = i + 1; j < enlist.Count; j++)
  360. //{
  361. // CoursematerialView enA = enlist[j];
  362. // //根据Excel表中的业务主键进行去重(课程代码唯一)
  363. // if (en.No == enA.No && en.Name == enA.Name)
  364. // {
  365. // //用于标识Excel表中的重复记录(由于是批量进行插入数据表)
  366. // }
  367. //}
  368. //数据表重复性验证(课程代码唯一)
  369. var courseCodeVerify = newCourseList.Where(x => x.CourseCode == newCoursematerial.CourseCode).SingleOrDefault();
  370. if (courseCodeVerify == null)
  371. {
  372. //新增
  373. if (!newCoursematerialInList.Any(x => x.CourseCode == newCoursematerial.CourseCode))
  374. {
  375. newCoursematerial.CoursematerialID = Guid.NewGuid();
  376. SetNewStatus(newCoursematerial);
  377. newCoursematerialInList.Add(newCoursematerial);
  378. inCount++;
  379. }
  380. else
  381. {
  382. //Excel表重复性验证
  383. //(注:当数据表中没有此记录,但是Excel中有重复数据,可在此处进行抛出到失败数据文件中,目前暂不考虑)
  384. inCount++;
  385. }
  386. }
  387. else
  388. {
  389. //更新(Excel有重复时,以最后一条记录的更新为准)
  390. //courseCodeVerify.CourseCode = newCoursematerial.CourseCode;
  391. courseCodeVerify.CourseName = newCoursematerial.CourseName;
  392. courseCodeVerify.Abbreviation = newCoursematerial.Abbreviation;
  393. courseCodeVerify.EnglishName = newCoursematerial.EnglishName;
  394. courseCodeVerify.StandardName = newCoursematerial.StandardName;
  395. courseCodeVerify.CourseEdition = newCoursematerial.CourseEdition;
  396. courseCodeVerify.CourseLevelID = newCoursematerial.CourseLevelID;
  397. courseCodeVerify.CourseScienceID = newCoursematerial.CourseScienceID;
  398. courseCodeVerify.ClassGroupingID = newCoursematerial.ClassGroupingID;
  399. courseCodeVerify.CourseSynopsis = newCoursematerial.CourseSynopsis;
  400. courseCodeVerify.CourseReserve = newCoursematerial.CourseReserve;
  401. courseCodeVerify.IsEnable = newCoursematerial.IsEnable;
  402. courseCodeVerify.Remark = newCoursematerial.Remark;
  403. SetModifyStatus(courseCodeVerify);
  404. newCoursematerialUpList.Add(courseCodeVerify);
  405. upCount++;
  406. }
  407. }
  408. //批量插入
  409. UnitOfWork.BulkInsert(newCoursematerialInList);
  410. //批量统一提交更新
  411. if (newCoursematerialUpList != null && newCoursematerialUpList.Count() > 0)
  412. {
  413. UnitOfWork.BatchUpdate(newCoursematerialUpList);
  414. }
  415. errdataList = errList.Distinct().ToList(); //错误列表List
  416. }
  417. catch (Exception)
  418. {
  419. throw;
  420. }
  421. }
  422. }
  423. }