PublishServices.cs 15 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393
  1. using System;
  2. using System.Collections.Generic;
  3. using System.Linq;
  4. using System.Text;
  5. using EMIS.DataLogic.Common.TeachingMaterial;
  6. using Bowin.Common.Linq.Entity;
  7. using EMIS.ViewModel;
  8. using EMIS.ViewModel.TeachingMaterial;
  9. using EMIS.Entities;
  10. using EMIS.DataLogic.Repositories;
  11. using Bowin.Common.Utility;
  12. using EMIS.ViewModel.CacheManage;
  13. using System.Text.RegularExpressions;
  14. namespace EMIS.CommonLogic.TeachingMaterial
  15. {
  16. public class PublishServices : BaseServices, IPublishServices
  17. {
  18. #region --0.0 定义--
  19. public PublishDAL PublishDAL { get; set; }
  20. public PublishRepository PublishRepository { get; set; }
  21. #endregion
  22. #region 1.0 查询出版单位信息
  23. /// <summary>
  24. /// 获取出版单位信息列表
  25. /// </summary>
  26. /// <param name="exp"></param>
  27. /// <returns></returns>
  28. public IGridResultSet<PublisherView> GetPublishViewGrid(ConfiguretView configuretView, bool? isSupplier, bool? isPulish, bool? isPrint, int pageIndex, int pageSize)
  29. {
  30. var query = PublishDAL.GetPulishGridView(x => true);
  31. if (isSupplier.HasValue)
  32. query = query.Where(x => x.IsSupplier == isSupplier);
  33. if (isPrint.HasValue)
  34. query = query.Where(x => x.IsPrint == isPrint);
  35. if (isPulish.HasValue)
  36. query = query.Where(x => x.IsPulish == isPulish);
  37. if (!string.IsNullOrEmpty(configuretView.ConditionValue))
  38. return query.DynamicWhere(configuretView.Attribute, configuretView.Condition, configuretView.ConditionValue).OrderBy(x => x.UnitCode.Length).ThenBy(x => x.UnitCode).ToGridResultSet<PublisherView>(pageIndex, pageSize);
  39. return query.OrderBy(x => x.UnitCode.Length).ThenBy(x => x.UnitCode).ToGridResultSet<PublisherView>(pageIndex, pageSize);
  40. }
  41. #endregion
  42. #region 2.0 编辑保存出版单位信息
  43. /// <summary>
  44. /// 编辑出单社
  45. /// </summary>
  46. /// <param name="publisherView"></param>
  47. public void EditPulish(PublisherView publisherView, Guid createUserID)
  48. {
  49. try
  50. {
  51. if (this.PublishRepository.GetList(x => x.UnitCode == publisherView.UnitCode
  52. && x.PublishID != publisherView.PublishID).Count() > 0)
  53. {
  54. throw new Exception("出版单位编号已经存在,请重新输入!");
  55. }
  56. if (this.PublishRepository.GetList(x => x.UnitName == publisherView.UnitName
  57. && x.PublishID != publisherView.PublishID).Count() > 0)
  58. {
  59. throw new Exception("出版单位已经存在,请重新输入!");
  60. }
  61. if (publisherView.PublishID != null && publisherView.PublishID != Guid.Empty)
  62. {
  63. CF_Publish publish = PublishRepository.GetSingle(x => x.PublishID == publisherView.PublishID);
  64. publish.Fax = publisherView.Fax;
  65. publish.Address = publisherView.Address;
  66. publish.BandCard = publisherView.BandCard;
  67. publish.BandName = publisherView.BandName;
  68. publish.ContectUser = publisherView.ContectUser;
  69. publish.Ein = publisherView.Ein;
  70. publish.Email = publisherView.Email;
  71. publish.IsSupplier = publisherView.IsSupplier;
  72. publish.IsPulish = publisherView.IsPulish;
  73. publish.IsPrint = publisherView.IsPrint;
  74. publish.UnitShortName = publisherView.UnitShortName;
  75. publish.UnitCode = publisherView.UnitCode;
  76. publish.UnitName = publisherView.UnitName;
  77. publish.Mobile = publisherView.Mobile;
  78. publish.Phone = publisherView.Phone;
  79. publish.Desc = publisherView.Desc;
  80. PublishRepository.UnitOfWork.Update(publish);
  81. PublishRepository.UnitOfWork.Commit();
  82. }
  83. else
  84. {
  85. CF_Publish publish = new CF_Publish()
  86. {
  87. PublishID = Guid.NewGuid(),
  88. Fax = publisherView.Fax,
  89. Address = publisherView.Address,
  90. BandCard = publisherView.BandCard,
  91. BandName = publisherView.BandName,
  92. ContectUser = publisherView.ContectUser,
  93. Ein = publisherView.Ein,
  94. Email = publisherView.Email,
  95. IsSupplier = publisherView.IsSupplier,
  96. IsPulish = publisherView.IsPulish,
  97. IsPrint = publisherView.IsPrint,
  98. UnitShortName = publisherView.UnitShortName == null ? publisherView.UnitName : publisherView.UnitShortName,
  99. UnitCode = publisherView.UnitCode,
  100. UnitName = publisherView.UnitName,
  101. Mobile = publisherView.Mobile,
  102. Phone = publisherView.Phone,
  103. Desc = publisherView.Desc,
  104. CreateUserID = createUserID,
  105. CreateTime = DateTime.Now
  106. };
  107. PublishRepository.UnitOfWork.Add(publish);
  108. PublishRepository.UnitOfWork.Commit();
  109. }
  110. }
  111. catch (Exception ex)
  112. {
  113. throw ex;
  114. }
  115. }
  116. #endregion
  117. #region 3.0 删除出版单位信息
  118. /// <summary>
  119. /// 删除出版单位信息
  120. /// </summary>
  121. /// <param name="publishID"></param>
  122. public void DeletePublish(List<Guid> publishIDs)
  123. {
  124. try
  125. {
  126. PublishRepository.UnitOfWork.Delete<CF_Publish>(x => publishIDs.Contains(x.PublishID));
  127. }
  128. catch (Exception ex)
  129. {
  130. throw ex;
  131. }
  132. }
  133. #endregion
  134. #region 4.0 获取出版单位单位
  135. /// <summary>
  136. /// 获取出版单位单位
  137. /// </summary>
  138. /// <returns></returns>
  139. public PublisherView GetSinglePublish(Guid publishID)
  140. {
  141. return PublishDAL.GetSinglePublish(publishID);
  142. }
  143. #endregion
  144. #region 5.0 获取出版单位信息列表
  145. /// <summary>
  146. /// 获取出版单位信息列表
  147. /// </summary>
  148. /// <param name="exp"></param>
  149. /// <returns></returns>
  150. public IList<PublisherView> GetPublishViewExcel(ConfiguretView configuretView, bool? isSupplier, bool? isPulish, bool? isPrint)
  151. {
  152. var query = PublishDAL.GetPulishGridView(x => true);
  153. if (isSupplier.HasValue)
  154. query = query.Where(x => x.IsSupplier == isSupplier);
  155. if (isPrint.HasValue)
  156. query = query.Where(x => x.IsPrint == isPrint);
  157. if (isPulish.HasValue)
  158. query = query.Where(x => x.IsPulish == isPulish);
  159. if (!string.IsNullOrEmpty(configuretView.ConditionValue))
  160. return query.DynamicWhere(configuretView.Attribute, configuretView.Condition, configuretView.ConditionValue).OrderByDescending(x => x.UnitName).ToList();
  161. return query.OrderByDescending(x => x.UnitName).ToList();
  162. }
  163. #endregion
  164. #region 6.0 获取所有出版单位信息-无条件模式
  165. /// <summary>
  166. /// 获取所有出版单位信息-无条件模式
  167. /// </summary>
  168. /// <returns></returns>
  169. public IList<PublisherView> GetAllPublish()
  170. {
  171. return PublishDAL.GetPulishGridView(x => true).ToList();
  172. }
  173. #endregion
  174. #region 7.0 验证出版单位单位编码是否存在
  175. public bool IsExistUnitCode(string unitCode)
  176. {
  177. bool IsExist = false;
  178. var Endt = PublishRepository.GetSingle(x => x.UnitCode == unitCode);
  179. if (Endt != null)
  180. {
  181. IsExist = true;
  182. }
  183. return IsExist;
  184. }
  185. #endregion
  186. #region 8.0 出版单位信息导入
  187. public void PublishImport(Dictionary<string, string> cellheader, out List<PublisherView> errdataList, out int ErrCount, out int OkCount, string sourcePhysicalPath)
  188. {
  189. StringBuilder errorMsg = new StringBuilder(); // 错误信息
  190. List<PublisherView> errList = new List<PublisherView>();
  191. #region 1.1解析文件,存放到一个List集合里
  192. cellheader.Remove("ErrorMessage");//去除异常列、导入操作不需要
  193. // 1.1解析文件,存放到一个List集合里
  194. List<PublisherView> enlist =
  195. NpoiExcelHelper.ExcelToEntityList<PublisherView>(cellheader, sourcePhysicalPath, out errorMsg, out errList);
  196. cellheader.Add("ErrorMessage", "错误信息");//还原字典项
  197. #endregion
  198. #region 1.2 将Excel数据写入数据库中
  199. #region 1.2.1 对List集合进行有效性校验
  200. #region 1.2.1.1检测必填项是否必填
  201. if (enlist.Count() <= 0)
  202. {
  203. throw new Exception("请填写Excel模板信息数据。");
  204. }
  205. for (int i = 0; i < enlist.Count; i++)
  206. {
  207. PublisherView en = enlist[i];
  208. string errorMsgStr = "第" + (i + 1) + "行数据检测异常:";
  209. bool isHaveNoInputValue = false; // 是否含有未输入项
  210. if (string.IsNullOrEmpty(en.UnitCode))
  211. {
  212. errorMsgStr += "单位编号不能为空;";
  213. en.ErrorMessage = errorMsgStr;
  214. isHaveNoInputValue = true;
  215. }
  216. if (string.IsNullOrEmpty(en.UnitName))
  217. {
  218. errorMsgStr += "单位名称不能为空;";
  219. en.ErrorMessage = errorMsgStr;
  220. isHaveNoInputValue = true;
  221. }
  222. if (!string.IsNullOrEmpty(en.UnitCode))
  223. {
  224. if (PublishRepository.Entities.Any(x => x.UnitCode == en.UnitCode))
  225. {
  226. errorMsgStr += "单位编号已存在;";
  227. en.ErrorMessage = errorMsgStr;
  228. isHaveNoInputValue = true;
  229. }
  230. }
  231. if (!string.IsNullOrEmpty(en.UnitName))
  232. {
  233. if (PublishRepository.Entities.Any(x => x.UnitName == en.UnitName))
  234. {
  235. errorMsgStr += "单位名称已存在;";
  236. en.ErrorMessage = errorMsgStr;
  237. isHaveNoInputValue = true;
  238. }
  239. }
  240. if (!string.IsNullOrEmpty(en.IsPrintName))
  241. {
  242. if (!(IdNameExt.GetDictionaryItem(DictionaryItem.CF_YesOrNoStatus.ToString())
  243. .Any(x => x.Name == en.IsPrintName)))
  244. {
  245. errorMsgStr += "是否印刷厂不存在;";
  246. en.ErrorMessage = errorMsgStr;
  247. isHaveNoInputValue = true;
  248. }
  249. }
  250. if (!string.IsNullOrEmpty(en.IsPulishName))
  251. {
  252. if (!(IdNameExt.GetDictionaryItem(DictionaryItem.CF_YesOrNoStatus.ToString())
  253. .Any(x => x.Name == en.IsPulishName)))
  254. {
  255. errorMsgStr += "是否出版单位不存在;";
  256. en.ErrorMessage = errorMsgStr;
  257. isHaveNoInputValue = true;
  258. }
  259. }
  260. if (!string.IsNullOrEmpty(en.IsSupplierName))
  261. {
  262. if (!(IdNameExt.GetDictionaryItem(DictionaryItem.CF_YesOrNoStatus.ToString())
  263. .Any(x => x.Name == en.IsSupplierName)))
  264. {
  265. errorMsgStr += "是否供应商不存在;";
  266. en.ErrorMessage = errorMsgStr;
  267. isHaveNoInputValue = true;
  268. }
  269. }
  270. Regex reg = new Regex(@"^[\w-]+(\.[\w-]+)*@([\w-]+\.)+[a-zA-Z]+$");
  271. Regex regMobile = new Regex(@"^1[3|4|5|7|8]\d{9}$");
  272. if (!string.IsNullOrEmpty(en.Email))
  273. {
  274. if (!reg.IsMatch(en.Email))
  275. {
  276. errorMsgStr += "邮箱格式不正确;";
  277. en.ErrorMessage = errorMsgStr;
  278. isHaveNoInputValue = true;
  279. }
  280. }
  281. if (!string.IsNullOrEmpty(en.Mobile))
  282. {
  283. if (!regMobile.IsMatch(en.Mobile))
  284. {
  285. errorMsgStr += "手机号码不正确;";
  286. en.ErrorMessage = errorMsgStr;
  287. isHaveNoInputValue = true;
  288. }
  289. }
  290. if (isHaveNoInputValue) // 若必填项有值未填
  291. {
  292. en.IsExcelVaildateOK = false;
  293. en.ErrorMessage = errorMsgStr;
  294. errList.Add(en);
  295. errorMsg.AppendLine(errorMsgStr);
  296. }
  297. }
  298. #endregion
  299. // TODO:其他检测
  300. #region 1.2.1.3 循环写入验证成功的数据
  301. List<CF_Publish> publishList = new List<CF_Publish>();
  302. for (int i = 0; i < enlist.Count; i++)
  303. {
  304. PublisherView enA = enlist[i];
  305. if (enA.IsExcelVaildateOK == false) // 上面验证不通过,不进行此步验证
  306. {
  307. continue;
  308. }
  309. var curUser = EMIS.Utility.FormValidate.CustomPrincipal.Current;
  310. CF_Publish publish = new CF_Publish()
  311. {
  312. PublishID = Guid.NewGuid(),
  313. Fax = enA.Fax,
  314. Address = enA.Address,
  315. BandCard = enA.BandCard,
  316. BandName = enA.BandName,
  317. ContectUser = enA.ContectUser,
  318. Ein = enA.Ein,
  319. Email = enA.Email,
  320. IsSupplier = enA.IsSupplierName == "是" ? true : false,
  321. IsPulish = enA.IsPulishName == "是" ? true : false,
  322. IsPrint = enA.IsPrintName == "是" ? true : false,
  323. UnitShortName = enA.UnitName,
  324. UnitCode = enA.UnitCode,
  325. UnitName = enA.UnitName,
  326. Mobile = enA.Mobile,
  327. Phone = enA.Phone,
  328. Desc = enA.Desc,
  329. CreateUserID = curUser.UserID,
  330. CreateTime = DateTime.Now
  331. };
  332. publishList.Add(publish);
  333. }
  334. #endregion
  335. UnitOfWork.BulkInsert(publishList);//统一写入
  336. #endregion
  337. #endregion
  338. #region 1.3 返回各项数据值
  339. OkCount = enlist.Distinct().Count() - errList.Distinct().Count();//共条数减去失败条数
  340. errdataList = errList.Distinct().OrderBy(x => x.UnitCode).ToList();
  341. ErrCount = errList.Distinct().Count();
  342. #endregion
  343. }
  344. #endregion
  345. }
  346. }