TeachingMaterialPoolServices.cs 30 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620
  1. using System;
  2. using System.Collections.Generic;
  3. using System.Linq;
  4. using System.Data.Entity;
  5. using System.Text;
  6. using EMIS.DataLogic.Common.TeachingMaterial;
  7. using EMIS.ViewModel.TeachingMaterial;
  8. using Bowin.Common.Linq.Entity;
  9. using EMIS.ViewModel;
  10. using EMIS.DataLogic.Repositories;
  11. using EMIS.Entities;
  12. using EMIS.Utility.FormValidate;
  13. using System.Data;
  14. using Bowin.Common.Utility;
  15. using EMIS.ViewModel.CacheManage;
  16. using Bowin.Common.Linq;
  17. using System.Linq.Expressions;
  18. using EMIS.ViewModel.Cultureplan;
  19. using System.Text.RegularExpressions;
  20. namespace EMIS.CommonLogic.TeachingMaterial
  21. {
  22. public class TeachingMaterialPoolServices : BaseServices, ITeachingMaterialPoolServices
  23. {
  24. #region --0.0 定义--
  25. public TeachingMaterialPoolDAL TeachingMaterialPoolDAL { get; set; }
  26. public TeachingMaterialPoolRepository TeachingMaterialPoolRepository { get; set; }
  27. public UserRepository UserRepository { get; set; }
  28. public CoursematerialRepository CoursematerialRepository { get; set; }
  29. public DictionaryItemRepository DictionaryItemRepository { get; set; }
  30. public PublishRepository PublishRepository { get; set; }
  31. #endregion
  32. #region 1.0 获取教材总库信息列表
  33. /// <summary>
  34. /// 获取教材总库信息列表
  35. /// </summary>
  36. /// <param name="exp"></param>
  37. /// <returns></returns>
  38. public IGridResultSet<TeachingMaterialPoolView> GetTeachingMaterialPoolViewGrid(ConfiguretView configuretView, Guid? publishID, Guid? coursematerialID, bool? isLate, int? teachingMaterialType, int pageIndex, int pageSize)
  39. {
  40. Expression<Func<CF_TeachingMaterialPool, bool>> exp = (x => true);
  41. if (publishID != null && publishID != Guid.Empty)
  42. {
  43. exp = exp.And(x => x.PublishID == publishID);
  44. }
  45. if (coursematerialID != null && coursematerialID != Guid.Empty)
  46. {
  47. exp = exp.And(x => x.EM_Coursematerial.Any(w => w.CoursematerialID == coursematerialID));
  48. }
  49. if (isLate != null)
  50. {
  51. exp = exp.And(x => x.IsLate == isLate);
  52. }
  53. if (teachingMaterialType != null && teachingMaterialType > 0)
  54. {
  55. exp = exp.And(x => x.TeachingMaterialTypeID == teachingMaterialType);
  56. }
  57. var query = TeachingMaterialPoolDAL.GetTeachingMaterialPoolGridView(exp);
  58. if (!string.IsNullOrEmpty(configuretView.ConditionValue))
  59. return query.DynamicWhere(configuretView.Attribute, configuretView.Condition, configuretView.ConditionValue).OrderBy(x => x.TeachingMaterialCode.Length).ThenBy(x => x.TeachingMaterialCode).ToGridResultSet<TeachingMaterialPoolView>(pageIndex, pageSize);//.OrderBy(x => x.TeachingMaterialTypeID).OrderByDescending(x => x.TeachingMaterialName)
  60. //.OrderBy(x => x.TeachingMaterialCode)
  61. return query.OrderBy(x => x.TeachingMaterialCode.Length).ThenBy(x => x.TeachingMaterialCode).ToGridResultSet<TeachingMaterialPoolView>(pageIndex, pageSize);
  62. }
  63. #endregion
  64. #region 2.0 编辑保存教材信息
  65. /// <summary>
  66. /// 编辑教材总库
  67. /// </summary>
  68. /// <param name="publisherView"></param>
  69. public void EditTeachingMaterialPool(TeachingMaterialPoolView teachingMaterialPoolView, Guid createUserID, IList<CoursematerialView> coursematerialList)
  70. {
  71. try
  72. {
  73. var coursematerialIDList = coursematerialList.Select(x => x.CoursematerialID).ToList();
  74. var coursematerialEntityList = CoursematerialRepository.GetList(x => coursematerialIDList.Contains(x.CoursematerialID)).ToList();
  75. if (teachingMaterialPoolView.PublishTime == null || teachingMaterialPoolView.PublishTime == "")
  76. {
  77. throw new Exception("版本时间不能为空");
  78. }
  79. if (teachingMaterialPoolView.TeachingMaterialCode.Length > 10)
  80. {
  81. throw new Exception("教材编号不能超过十位数,请重新输入");
  82. }
  83. if (this.TeachingMaterialPoolRepository.GetList(x => x.TeachingMaterialCode == teachingMaterialPoolView.TeachingMaterialCode
  84. && x.TeachingMaterialPoolID != teachingMaterialPoolView.TeachingMaterialPoolID).Count() > 0)
  85. {
  86. throw new Exception("教材编号已经存在,请重新输入!");
  87. }
  88. if (this.TeachingMaterialPoolRepository.GetList(x => x.ISBN == teachingMaterialPoolView.ISBN
  89. && x.TeachingMaterialPoolID != teachingMaterialPoolView.TeachingMaterialPoolID).Count() > 0)
  90. {
  91. throw new Exception("ISBN已经存在,请重新输入!");
  92. }
  93. Regex datestr = new Regex("^[0-9]{4}年[0-9]{1,2}月[0-9]{1,3}版[0-9]{1,3}次$");
  94. if (!datestr.IsMatch(teachingMaterialPoolView.PublishTime))//时间格式验证
  95. {
  96. throw new Exception("版本时间格式不正确,应为 xxxx年xx月xx版xx次,请重新输入!");
  97. }
  98. if (teachingMaterialPoolView.TeachingMaterialPoolID != null && teachingMaterialPoolView.TeachingMaterialPoolID != Guid.Empty)
  99. {
  100. CF_TeachingMaterialPool teachingMaterialPool = TeachingMaterialPoolRepository
  101. .GetSingle(x => x.TeachingMaterialPoolID == teachingMaterialPoolView.TeachingMaterialPoolID, (x => x.EM_Coursematerial));
  102. teachingMaterialPool.TeachingMaterialCode = teachingMaterialPoolView.TeachingMaterialCode;
  103. teachingMaterialPool.TeachingMaterialName = teachingMaterialPoolView.TeachingMaterialName;
  104. teachingMaterialPool.EM_Coursematerial = new HashSet<EM_Coursematerial>();
  105. teachingMaterialPool.EM_Coursematerial = new HashSet<EM_Coursematerial>(coursematerialEntityList);
  106. teachingMaterialPool.ISBN = teachingMaterialPoolView.ISBN;
  107. teachingMaterialPool.TeachingMaterialShorName = teachingMaterialPoolView.TeachingMaterialShorName;
  108. teachingMaterialPool.TeachingMaterialTypeID = teachingMaterialPoolView.TeachingMaterialTypeID == null ? -1 : teachingMaterialPoolView.TeachingMaterialTypeID;
  109. teachingMaterialPool.WnningLevel = teachingMaterialPoolView.WnningLevel == null ? -1 : teachingMaterialPoolView.WnningLevel;
  110. teachingMaterialPool.PublishTime = teachingMaterialPoolView.PublishTime;
  111. teachingMaterialPool.PublishID = teachingMaterialPoolView.PublishID;
  112. teachingMaterialPool.Author = teachingMaterialPoolView.Author;
  113. teachingMaterialPool.Price = teachingMaterialPoolView.Price;
  114. teachingMaterialPool.IsLate = teachingMaterialPoolView.IsLate;
  115. teachingMaterialPool.ModifyUserID = createUserID;
  116. teachingMaterialPool.ModifyTime = DateTime.Now;
  117. teachingMaterialPool.Desc = teachingMaterialPoolView.Desc;
  118. teachingMaterialPool.MinInventory = teachingMaterialPoolView.MinInventory;
  119. TeachingMaterialPoolRepository.UnitOfWork.Update(teachingMaterialPool);
  120. TeachingMaterialPoolRepository.UnitOfWork.Commit();
  121. }
  122. else
  123. {
  124. CF_TeachingMaterialPool teachingMaterialPool = new CF_TeachingMaterialPool()
  125. {
  126. TeachingMaterialPoolID = Guid.NewGuid(),
  127. WnningLevel = teachingMaterialPoolView.WnningLevel == null ? -1 : teachingMaterialPoolView.WnningLevel,
  128. PublishID = teachingMaterialPoolView.PublishID,
  129. Author = teachingMaterialPoolView.Author,
  130. ISBN = teachingMaterialPoolView.ISBN,
  131. IsLate = teachingMaterialPoolView.IsLate,
  132. PublishTime = teachingMaterialPoolView.PublishTime,
  133. TeachingMaterialCode = teachingMaterialPoolView.TeachingMaterialCode,
  134. TeachingMaterialName = teachingMaterialPoolView.TeachingMaterialName,
  135. TeachingMaterialShorName = teachingMaterialPoolView.TeachingMaterialShorName == null ? teachingMaterialPoolView.TeachingMaterialName : teachingMaterialPoolView.TeachingMaterialShorName,
  136. TeachingMaterialTypeID = teachingMaterialPoolView.TeachingMaterialTypeID == null ? -1 : teachingMaterialPoolView.TeachingMaterialTypeID,
  137. Price = teachingMaterialPoolView.Price,
  138. EM_Coursematerial = new HashSet<EM_Coursematerial>(coursematerialEntityList),
  139. Desc = teachingMaterialPoolView.Desc,
  140. CreateTime = DateTime.Now,
  141. CreateUserID = createUserID,
  142. MinInventory = 1,//最小库存量默认1
  143. ModifyUserID = createUserID,
  144. ModifyTime = DateTime.Now
  145. };
  146. TeachingMaterialPoolRepository.UnitOfWork.Add(teachingMaterialPool);
  147. TeachingMaterialPoolRepository.UnitOfWork.Commit();
  148. }
  149. }
  150. catch (Exception ex)
  151. {
  152. throw ex;
  153. }
  154. }
  155. #endregion
  156. #region 3.0 删除教材总库信息
  157. /// <summary>
  158. /// 删除教材总库信息
  159. /// </summary>
  160. /// <param name="publishID"></param>
  161. public void DeleteTeachingMaterialPool(List<Guid> TeachingMaterialPoolIDs)
  162. {
  163. try
  164. {
  165. TeachingMaterialPoolRepository.UnitOfWork.Delete<CF_TeachingMaterialPool>(x => TeachingMaterialPoolIDs.Contains(x.TeachingMaterialPoolID));
  166. }
  167. catch (Exception ex)
  168. {
  169. throw ex;
  170. }
  171. }
  172. #endregion
  173. #region 4.0 获取教材总库单位
  174. /// <summary>
  175. /// 获取教材总库单位
  176. /// </summary>
  177. /// <returns></returns>
  178. public TeachingMaterialPoolView GetSingleTeachingMaterialPool(Guid TeachingMaterialPoolID)
  179. {
  180. return TeachingMaterialPoolDAL.GetSingleTeachingMaterialPool(TeachingMaterialPoolID);
  181. }
  182. #endregion
  183. #region 5.0 获取教材总库息列表导出
  184. /// <summary>
  185. /// 获取教材总库息列表导出
  186. /// </summary>
  187. /// <param name="exp"></param>
  188. /// <returns></returns>
  189. public IList<TeachingMaterialPoolView> GetTeachingMaterialPoolViewExcel(ConfiguretView configuretView, Guid? publishID, Guid? coursematerialID, bool? isLate, int? teachingMaterialType)
  190. {
  191. Expression<Func<CF_TeachingMaterialPool, bool>> exp = (x => true);
  192. if (publishID != null && publishID != Guid.Empty)
  193. {
  194. exp = exp.And(x => x.PublishID == publishID);
  195. }
  196. if (coursematerialID != null && coursematerialID != Guid.Empty)
  197. {
  198. exp = exp.And(x => x.EM_Coursematerial.Any(w => w.CoursematerialID == coursematerialID));
  199. }
  200. if (isLate != null)
  201. {
  202. exp = exp.And(x => x.IsLate == isLate);
  203. }
  204. if (teachingMaterialType != null && teachingMaterialType > 0)
  205. {
  206. exp = exp.And(x => x.TeachingMaterialTypeID == teachingMaterialType);
  207. }
  208. var query = TeachingMaterialPoolDAL.GetTeachingMaterialPoolGridView(exp);
  209. if (!string.IsNullOrEmpty(configuretView.ConditionValue))
  210. return query.DynamicWhere(configuretView.Attribute, configuretView.Condition, configuretView.ConditionValue).OrderBy(x => x.TeachingMaterialTypeID).OrderByDescending(x => x.TeachingMaterialName).ToList();
  211. return query.OrderBy(x => x.TeachingMaterialCode).ToList();
  212. }
  213. #endregion
  214. #region 获取教材对应的课程资料
  215. public List<CoursematerialView> GetTeachingMaterialCoursematerialList(Guid? teachingMaterialPoolID)
  216. {
  217. if (teachingMaterialPoolID.HasValue)
  218. {
  219. var coursematerialViewList = TeachingMaterialPoolDAL.GetTeachingMaterialPoolCoursematerialView(x => x.TeachingMaterialPoolID == teachingMaterialPoolID);
  220. return coursematerialViewList.ToList();
  221. }
  222. else
  223. {
  224. return new List<CoursematerialView>();
  225. }
  226. }
  227. #endregion
  228. #region 6.0 获取教材总库信息列表(用于学生征订)
  229. /// <summary>
  230. /// 获取教材总库信息列表(用于学生征订)
  231. /// </summary>
  232. /// <param name="exp"></param>
  233. /// <returns></returns>
  234. public IGridResultSet<TeachingMaterialPoolView> GetTeachingMaterialPoolForOrder(ConfiguretView configuretView, Guid? publishID, string isbn, Guid? coursematerialID, string teachingMaterialName, string ischeck, int pageIndex, int pageSize)
  235. {
  236. Expression<Func<CF_TeachingMaterialPool, bool>> exp = x => true;
  237. exp = exp.And(x => x.IsLate != true);
  238. if (ischeck == "true" && coursematerialID != null && coursematerialID != Guid.Empty)
  239. {
  240. exp = exp.And(x => x.EM_Coursematerial.Any(y => y.CoursematerialID == coursematerialID));
  241. }
  242. var query = TeachingMaterialPoolDAL.GetTeachingMaterialPoolForOrder(exp, coursematerialID);
  243. if (publishID != null && publishID != Guid.Empty)
  244. {
  245. query = query.Where(x => x.PublishID == publishID);
  246. }
  247. if (isbn != null && isbn != "")// && !isbn.IsEmpty()
  248. {
  249. query = query.Where(x => x.ISBN.Contains(isbn));
  250. }
  251. if (teachingMaterialName != null && teachingMaterialName != "")
  252. {
  253. query = query.Where(x => x.TeachingMaterialName.Contains(teachingMaterialName));
  254. }
  255. //暂时不根据征订的课程筛选教材
  256. //if (coursematerialID != null && coursematerialID != Guid.Empty)
  257. //{
  258. // query = query.where(x => x.coursematerialid == coursematerialid);
  259. //}
  260. if (!string.IsNullOrEmpty(configuretView.ConditionValue))
  261. return query.DynamicWhere(configuretView.Attribute, configuretView.Condition, configuretView.ConditionValue)
  262. .OrderBy(x => x.TeachingMaterialName).ToGridResultSet<TeachingMaterialPoolView>(pageIndex, pageSize);
  263. return query.OrderBy(x => x.OrderID)
  264. .ThenBy(x => x.TeachingMaterialName).ToGridResultSet<TeachingMaterialPoolView>(pageIndex, pageSize);
  265. }
  266. #endregion
  267. #region 7.0 编辑保存教材库存信息
  268. /// <summary>
  269. /// 编辑教材库存
  270. /// </summary>
  271. /// <param name="publisherView"></param>
  272. public void EditTeachingMateriaInventory(TeachingMaterialPoolView teachingMaterialPoolView, Guid createUserID)
  273. {
  274. try
  275. {
  276. if (teachingMaterialPoolView.TeachingMaterialPoolID != null && teachingMaterialPoolView.TeachingMaterialPoolID != Guid.Empty)
  277. {
  278. CF_TeachingMaterialPool teachingMaterialPool = TeachingMaterialPoolRepository.GetSingle(x => x.TeachingMaterialPoolID == teachingMaterialPoolView.TeachingMaterialPoolID);
  279. teachingMaterialPool.MinInventory = teachingMaterialPoolView.MinInventory;
  280. TeachingMaterialPoolRepository.UnitOfWork.Update(teachingMaterialPool);
  281. TeachingMaterialPoolRepository.UnitOfWork.Commit();
  282. }
  283. }
  284. catch (Exception ex)
  285. {
  286. throw ex;
  287. }
  288. }
  289. #endregion
  290. public bool IsExistCode(string code)
  291. {
  292. bool IsExist = false;
  293. var Endt = TeachingMaterialPoolRepository.GetSingle(x => x.TeachingMaterialCode == code);
  294. if (Endt != null)
  295. {
  296. IsExist = true;
  297. }
  298. return IsExist;
  299. }
  300. public bool IsExistISBN(string ISBN)
  301. {
  302. bool IsExist = false;
  303. var Endt = TeachingMaterialPoolRepository.GetSingle(x => x.ISBN == ISBN);
  304. if (Endt != null)
  305. {
  306. IsExist = true;
  307. }
  308. return IsExist;
  309. }
  310. public IGridResultSet<StudentTeachingMaterialView> GetStudentTeachingMaterialViewGrid(ConfiguretView configuretView, int pageIndex, int pageSize)
  311. {
  312. var user = CustomPrincipal.Current;
  313. var query = TeachingMaterialPoolDAL.GetStudentTeachingMaterialView(x => x.UserID == user.UserID);
  314. if (!string.IsNullOrEmpty(configuretView.ConditionValue))
  315. query = query.DynamicWhere(configuretView.Attribute, configuretView.Condition, configuretView.ConditionValue);
  316. return query.OrderBy(x => x.SchoolYearValue).ThenBy(x => x.CourseName).ToGridResultSet<StudentTeachingMaterialView>(pageIndex, pageSize);
  317. }
  318. public IList<StudentTeachingMaterialView> GetStudentTeachingMaterialViewList(ConfiguretView configuretView)
  319. {
  320. var user = CustomPrincipal.Current;
  321. var query = TeachingMaterialPoolDAL.GetStudentTeachingMaterialView(x => x.UserID == user.UserID);
  322. if (!string.IsNullOrEmpty(configuretView.ConditionValue))
  323. query = query.DynamicWhere(configuretView.Attribute, configuretView.Condition, configuretView.ConditionValue);
  324. return query.OrderBy(x => x.SchoolYearValue).ThenBy(x => x.CourseName).ToList();
  325. }
  326. public void TeachingMaterialPoolImport(Dictionary<string, string> cellheader, out int OkCount, out List<TeachingMaterialPoolImportView> errdataList, out int successCount, string sourcePhysicalPath)
  327. {
  328. StringBuilder errorMsg = new StringBuilder(); // 错误信息
  329. List<TeachingMaterialPoolImportView> errList = new List<TeachingMaterialPoolImportView>();
  330. #region 1.1解析文件,存放到一个List集合里
  331. cellheader.Remove("ErrorMessage");//去除异常列、导入操作不需要
  332. // 1.1解析文件,存放到一个List集合里
  333. List<TeachingMaterialPoolImportView> enlist =
  334. NpoiExcelHelper.ExcelToEntityList<TeachingMaterialPoolImportView>(cellheader, sourcePhysicalPath, out errorMsg, out errList);
  335. cellheader.Add("ErrorMessage", "错误信息");//还原字典项
  336. #endregion
  337. #region 1.2 将Excel数据写入数据库中
  338. #region 1.2.1 对List集合进行有效性校验
  339. if (enlist.Count() <= 0)
  340. {
  341. throw new Exception("请填写Excel模板信息数据。");
  342. }
  343. #region 1.2.1.1检测必填项是否必填
  344. for (int i = 0; i < enlist.Count; i++)
  345. {
  346. TeachingMaterialPoolImportView en = enlist[i];
  347. string errorMsgStr = "第" + (i + 1) + "行数据检测异常:";
  348. bool isHaveNoInputValue = false; // 是否含有未输入项
  349. if (string.IsNullOrEmpty(en.TeachingMaterialName))
  350. {
  351. errorMsgStr += "教材名称不能为空;";
  352. en.ErrorMessage = errorMsgStr;
  353. isHaveNoInputValue = true;
  354. }
  355. if (string.IsNullOrEmpty(en.CoursematerialName))
  356. {
  357. errorMsgStr += "课程名称不能为空;";
  358. en.ErrorMessage = errorMsgStr;
  359. isHaveNoInputValue = true;
  360. }
  361. if (string.IsNullOrEmpty(en.TeachingMaterialCode))
  362. {
  363. errorMsgStr += "教材编号不能为空;";
  364. en.ErrorMessage = errorMsgStr;
  365. isHaveNoInputValue = true;
  366. }
  367. if (string.IsNullOrEmpty(en.ISBN))
  368. {
  369. errorMsgStr += "ISBN不能为空;";
  370. en.ErrorMessage = errorMsgStr;
  371. isHaveNoInputValue = true;
  372. }
  373. Regex datestr = new Regex("^[0-9]{4}年[0-9]{1,2}月[0-9]{1,3}版[0-9]{1,3}次$");
  374. if (string.IsNullOrEmpty(en.PublishTime))
  375. {
  376. errorMsgStr += "版本时间不能为空;";
  377. en.ErrorMessage = errorMsgStr;
  378. isHaveNoInputValue = true;
  379. }
  380. else if (!datestr.IsMatch(en.PublishTime))//时间格式验证
  381. {
  382. errorMsgStr += "版本时间格式不正确,应为 xxxx年xx月xx版xx次,请重新输入!";
  383. en.ErrorMessage = errorMsgStr;
  384. isHaveNoInputValue = true;
  385. }
  386. if (string.IsNullOrEmpty(en.PublishName))
  387. {
  388. errorMsgStr += "出版单位不能为空;";
  389. en.ErrorMessage = errorMsgStr;
  390. isHaveNoInputValue = true;
  391. }
  392. else
  393. {
  394. if (!PublishRepository.Entities.Any(x => x.UnitName == en.PublishName))
  395. {
  396. errorMsgStr += "该出版单位不存在,请添加后再导入;";
  397. en.ErrorMessage = errorMsgStr;
  398. isHaveNoInputValue = true;
  399. }
  400. }
  401. if (string.IsNullOrEmpty(en.Author))
  402. {
  403. errorMsgStr += "作者不能为空;";
  404. en.ErrorMessage = errorMsgStr;
  405. isHaveNoInputValue = true;
  406. }
  407. if (en.Price == null)
  408. {
  409. errorMsgStr += "单价列不能为空;";
  410. en.ErrorMessage = errorMsgStr;
  411. isHaveNoInputValue = true;
  412. }
  413. #region 1.2.1.2 检测(启用状态课程名称唯一性、)
  414. //if (CoursematerialRepository.Entities.Where(x => x.IsEnable == true && x.CourseName == en.CoursematerialName).Count() > 1)
  415. //{
  416. // errorMsgStr += "该课程名称列对应的课程编码存在多条;";
  417. // en.ErrorMessage = errorMsgStr;
  418. // isHaveNoInputValue = true;
  419. //}
  420. var coursematerialNames = en.CoursematerialName.Split(',').Where(x => !string.IsNullOrEmpty(x)).ToList();
  421. if (CoursematerialRepository.Entities.Where(x => x.IsEnable == true && coursematerialNames.Contains(x.CourseName)).Count() != coursematerialNames.Count)
  422. {
  423. errorMsgStr += "部分对应的课程不存在或未启用,请添加后再导入;";
  424. en.ErrorMessage = errorMsgStr;
  425. isHaveNoInputValue = true;
  426. }
  427. if (TeachingMaterialPoolRepository.Entities.Any(x => x.TeachingMaterialCode == en.TeachingMaterialCode))
  428. {
  429. errorMsgStr += "教材编号已存在;";
  430. en.ErrorMessage = errorMsgStr;
  431. isHaveNoInputValue = true;
  432. }
  433. if (TeachingMaterialPoolRepository.Entities.Any(x => x.ISBN == en.ISBN))
  434. {
  435. errorMsgStr += "ISBN已存在;";
  436. en.ErrorMessage = errorMsgStr;
  437. isHaveNoInputValue = true;
  438. }
  439. if (!string.IsNullOrEmpty(en.WnningLevelName))
  440. {
  441. if (!IdNameExt.GetDictionaryItem(DictionaryItem.CF_WnningLevel.ToString())
  442. .Any(x => x.Name == en.WnningLevelName))
  443. {
  444. errorMsgStr += "获奖等级不存在;";
  445. en.ErrorMessage = errorMsgStr;
  446. isHaveNoInputValue = true;
  447. }
  448. }
  449. if (!string.IsNullOrEmpty(en.TeachingMaterialTypeName))
  450. {
  451. if (!IdNameExt.GetDictionaryItem(DictionaryItem.CF_TeachingMaterialType.ToString())
  452. .Any(x => x.Name == en.TeachingMaterialTypeName))
  453. {
  454. errorMsgStr += "该教材类型不存在,请添加后再导入;";
  455. en.ErrorMessage = errorMsgStr;
  456. isHaveNoInputValue = true;
  457. }
  458. }
  459. #endregion
  460. if (isHaveNoInputValue) // 若必填项有值未填
  461. {
  462. en.IsExcelVaildateOK = false;
  463. en.ErrorMessage = errorMsgStr;
  464. errList.Add(en);
  465. errorMsg.AppendLine(errorMsgStr);
  466. }
  467. }
  468. #endregion
  469. // TODO:其他检测
  470. #region 1.2.1.3 循环写入验证成功的数据
  471. List<CF_TeachingMaterialPool> tpList = new List<CF_TeachingMaterialPool>();
  472. var coursematerialNameList = enlist.SelectMany(x => x.CoursematerialName.Split(',').Where(w => !string.IsNullOrEmpty(w)).ToList()).Distinct().ToList();
  473. var coursematerialEntityList = CoursematerialRepository.GetList(x => x.IsEnable == true
  474. && coursematerialNameList.Contains(x.CourseName)).ToList();
  475. var publishNameList = enlist.Select(x => x.PublishName).Distinct().ToList();
  476. var publishEntityList = PublishRepository.GetList(x => publishNameList.Contains(x.UnitName)).ToList();
  477. for (int i = 0; i < enlist.Count; i++)
  478. {
  479. TeachingMaterialPoolImportView enA = enlist[i];
  480. if (enA.IsExcelVaildateOK == false) // 上面验证不通过,不进行此步验证
  481. {
  482. continue;
  483. }
  484. var curUser = EMIS.Utility.FormValidate.CustomPrincipal.Current;
  485. CF_Publish publishEntity = publishEntityList.Where(x => x.UnitName == enA.PublishName).FirstOrDefault();
  486. var curCoursematerialNameList = enA.CoursematerialName.Split('.').Where(w => !string.IsNullOrEmpty(w)).ToList();
  487. //EM_Coursematerial coursematerialEntity = CoursematerialRepository.Entities.Where(x => x.IsEnable == true && x.CourseName == enA.CoursematerialName).FirstOrDefault();
  488. CF_TeachingMaterialPool teachingMaterialPool = new CF_TeachingMaterialPool()
  489. {
  490. TeachingMaterialPoolID = Guid.NewGuid(),
  491. WnningLevel = enA.WnningLevelName == null ? -1 : IdNameExt.GetDictionaryItem(DictionaryItem.CF_WnningLevel.ToString())
  492. .Where(x => x.Name == enA.WnningLevelName).FirstOrDefault().Value,
  493. PublishID = publishEntity.PublishID,
  494. Author = enA.Author.Trim(),
  495. ISBN = enA.ISBN.Trim(),
  496. IsLate = false,
  497. PublishTime = enA.PublishTime,
  498. TeachingMaterialCode = enA.TeachingMaterialCode,
  499. TeachingMaterialName = enA.TeachingMaterialName,
  500. TeachingMaterialShorName = enA.TeachingMaterialName,
  501. TeachingMaterialTypeID = enA.TeachingMaterialTypeName == null ? -1 : IdNameExt.GetDictionaryItem(DictionaryItem.CF_TeachingMaterialType.ToString())
  502. .Where(x => x.Name == enA.TeachingMaterialTypeName)
  503. .FirstOrDefault().Value,
  504. Price = enA.Price == null ? 0 : enA.Price,
  505. EM_Coursematerial = new HashSet<EM_Coursematerial>(coursematerialEntityList.Where(x => curCoursematerialNameList.Contains(x.CourseName)).ToList()),
  506. Desc = enA.Desc,
  507. CreateTime = DateTime.Now,
  508. CreateUserID = curUser.UserID,
  509. MinInventory = 1,//最小库存量默认1
  510. ModifyUserID = curUser.UserID,
  511. ModifyTime = DateTime.Now
  512. };
  513. tpList.Add(teachingMaterialPool);
  514. }
  515. #endregion
  516. UnitOfWork.BulkInsert(tpList);//统一写入
  517. #endregion
  518. #endregion
  519. #region 1.3 返回各项数据值
  520. successCount = enlist.Distinct().Count() - errList.Distinct().Count();//共条数减去失败条数
  521. errdataList = errList.Distinct().ToList();
  522. OkCount = errList.Distinct().Count();
  523. #endregion
  524. }
  525. public string GetLastTeachingMaterialCode()
  526. {
  527. List<string> codeList = TeachingMaterialPoolRepository.Entities.Select(x => x.TeachingMaterialCode).ToList();
  528. string pattern = "^[0-9]*$";
  529. int newcodeInt = 1;
  530. List<string> newcodeList = new List<string>();
  531. foreach (string code in codeList)
  532. {
  533. if (Regex.IsMatch(code, pattern))
  534. {
  535. newcodeList.Add(code);
  536. }
  537. }
  538. newcodeList.OrderByDescending(x => x.ToString().Length).ThenByDescending(x => x.ToString());
  539. string newcode = newcodeList.OrderByDescending(x => x.ToString().Length).ThenByDescending(x => x.ToString()).FirstOrDefault();
  540. if (newcode != "" && newcode != String.Empty && newcode != null)
  541. {
  542. newcodeInt = int.Parse(newcode) + 1;
  543. }
  544. return (newcodeInt).ToString();
  545. }
  546. }
  547. }