123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393 |
- using System;
- using System.Collections.Generic;
- using System.Linq;
- using System.Text;
- using EMIS.DataLogic.Common.TeachingMaterial;
- using Bowin.Common.Linq.Entity;
- using EMIS.ViewModel;
- using EMIS.ViewModel.TeachingMaterial;
- using EMIS.Entities;
- using EMIS.DataLogic.Repositories;
- using Bowin.Common.Utility;
- using EMIS.ViewModel.CacheManage;
- using System.Text.RegularExpressions;
- namespace EMIS.CommonLogic.TeachingMaterial
- {
- public class PublishServices : BaseServices, IPublishServices
- {
- #region --0.0 定义--
- public PublishDAL PublishDAL { get; set; }
- public PublishRepository PublishRepository { get; set; }
- #endregion
- #region 1.0 查询出版单位信息
- /// <summary>
- /// 获取出版单位信息列表
- /// </summary>
- /// <param name="exp"></param>
- /// <returns></returns>
- public IGridResultSet<PublisherView> GetPublishViewGrid(ConfiguretView configuretView, bool? isSupplier, bool? isPulish, bool? isPrint, int pageIndex, int pageSize)
- {
- var query = PublishDAL.GetPulishGridView(x => true);
- if (isSupplier.HasValue)
- query = query.Where(x => x.IsSupplier == isSupplier);
- if (isPrint.HasValue)
- query = query.Where(x => x.IsPrint == isPrint);
- if (isPulish.HasValue)
- query = query.Where(x => x.IsPulish == isPulish);
- if (!string.IsNullOrEmpty(configuretView.ConditionValue))
- return query.DynamicWhere(configuretView.Attribute, configuretView.Condition, configuretView.ConditionValue).OrderBy(x => x.UnitCode.Length).ThenBy(x => x.UnitCode).ToGridResultSet<PublisherView>(pageIndex, pageSize);
- return query.OrderBy(x => x.UnitCode.Length).ThenBy(x => x.UnitCode).ToGridResultSet<PublisherView>(pageIndex, pageSize);
- }
- #endregion
- #region 2.0 编辑保存出版单位信息
- /// <summary>
- /// 编辑出单社
- /// </summary>
- /// <param name="publisherView"></param>
- public void EditPulish(PublisherView publisherView, Guid createUserID)
- {
- try
- {
- if (this.PublishRepository.GetList(x => x.UnitCode == publisherView.UnitCode
- && x.PublishID != publisherView.PublishID).Count() > 0)
- {
- throw new Exception("出版单位编号已经存在,请重新输入!");
- }
- if (this.PublishRepository.GetList(x => x.UnitName == publisherView.UnitName
- && x.PublishID != publisherView.PublishID).Count() > 0)
- {
- throw new Exception("出版单位已经存在,请重新输入!");
- }
- if (publisherView.PublishID != null && publisherView.PublishID != Guid.Empty)
- {
- CF_Publish publish = PublishRepository.GetSingle(x => x.PublishID == publisherView.PublishID);
- publish.Fax = publisherView.Fax;
- publish.Address = publisherView.Address;
- publish.BandCard = publisherView.BandCard;
- publish.BandName = publisherView.BandName;
- publish.ContectUser = publisherView.ContectUser;
- publish.Ein = publisherView.Ein;
- publish.Email = publisherView.Email;
- publish.IsSupplier = publisherView.IsSupplier;
- publish.IsPulish = publisherView.IsPulish;
- publish.IsPrint = publisherView.IsPrint;
- publish.UnitShortName = publisherView.UnitShortName;
- publish.UnitCode = publisherView.UnitCode;
- publish.UnitName = publisherView.UnitName;
- publish.Mobile = publisherView.Mobile;
- publish.Phone = publisherView.Phone;
- publish.Desc = publisherView.Desc;
- PublishRepository.UnitOfWork.Update(publish);
- PublishRepository.UnitOfWork.Commit();
- }
- else
- {
- CF_Publish publish = new CF_Publish()
- {
- PublishID = Guid.NewGuid(),
- Fax = publisherView.Fax,
- Address = publisherView.Address,
- BandCard = publisherView.BandCard,
- BandName = publisherView.BandName,
- ContectUser = publisherView.ContectUser,
- Ein = publisherView.Ein,
- Email = publisherView.Email,
- IsSupplier = publisherView.IsSupplier,
- IsPulish = publisherView.IsPulish,
- IsPrint = publisherView.IsPrint,
- UnitShortName = publisherView.UnitShortName == null ? publisherView.UnitName : publisherView.UnitShortName,
- UnitCode = publisherView.UnitCode,
- UnitName = publisherView.UnitName,
- Mobile = publisherView.Mobile,
- Phone = publisherView.Phone,
- Desc = publisherView.Desc,
- CreateUserID = createUserID,
- CreateTime = DateTime.Now
- };
- PublishRepository.UnitOfWork.Add(publish);
- PublishRepository.UnitOfWork.Commit();
- }
- }
- catch (Exception ex)
- {
- throw ex;
- }
- }
- #endregion
- #region 3.0 删除出版单位信息
- /// <summary>
- /// 删除出版单位信息
- /// </summary>
- /// <param name="publishID"></param>
- public void DeletePublish(List<Guid> publishIDs)
- {
- try
- {
- PublishRepository.UnitOfWork.Delete<CF_Publish>(x => publishIDs.Contains(x.PublishID));
- }
- catch (Exception ex)
- {
- throw ex;
- }
- }
- #endregion
- #region 4.0 获取出版单位单位
- /// <summary>
- /// 获取出版单位单位
- /// </summary>
- /// <returns></returns>
- public PublisherView GetSinglePublish(Guid publishID)
- {
- return PublishDAL.GetSinglePublish(publishID);
- }
- #endregion
- #region 5.0 获取出版单位信息列表
- /// <summary>
- /// 获取出版单位信息列表
- /// </summary>
- /// <param name="exp"></param>
- /// <returns></returns>
- public IList<PublisherView> GetPublishViewExcel(ConfiguretView configuretView, bool? isSupplier, bool? isPulish, bool? isPrint)
- {
- var query = PublishDAL.GetPulishGridView(x => true);
- if (isSupplier.HasValue)
- query = query.Where(x => x.IsSupplier == isSupplier);
- if (isPrint.HasValue)
- query = query.Where(x => x.IsPrint == isPrint);
- if (isPulish.HasValue)
- query = query.Where(x => x.IsPulish == isPulish);
- if (!string.IsNullOrEmpty(configuretView.ConditionValue))
- return query.DynamicWhere(configuretView.Attribute, configuretView.Condition, configuretView.ConditionValue).OrderByDescending(x => x.UnitName).ToList();
- return query.OrderByDescending(x => x.UnitName).ToList();
- }
- #endregion
- #region 6.0 获取所有出版单位信息-无条件模式
- /// <summary>
- /// 获取所有出版单位信息-无条件模式
- /// </summary>
- /// <returns></returns>
- public IList<PublisherView> GetAllPublish()
- {
- return PublishDAL.GetPulishGridView(x => true).ToList();
- }
- #endregion
- #region 7.0 验证出版单位单位编码是否存在
- public bool IsExistUnitCode(string unitCode)
- {
- bool IsExist = false;
- var Endt = PublishRepository.GetSingle(x => x.UnitCode == unitCode);
- if (Endt != null)
- {
- IsExist = true;
- }
- return IsExist;
- }
- #endregion
- #region 8.0 出版单位信息导入
- public void PublishImport(Dictionary<string, string> cellheader, out List<PublisherView> errdataList, out int ErrCount, out int OkCount, string sourcePhysicalPath)
- {
- StringBuilder errorMsg = new StringBuilder(); // 错误信息
- List<PublisherView> errList = new List<PublisherView>();
- #region 1.1解析文件,存放到一个List集合里
- cellheader.Remove("ErrorMessage");//去除异常列、导入操作不需要
- // 1.1解析文件,存放到一个List集合里
- List<PublisherView> enlist =
- NpoiExcelHelper.ExcelToEntityList<PublisherView>(cellheader, sourcePhysicalPath, out errorMsg, out errList);
- cellheader.Add("ErrorMessage", "错误信息");//还原字典项
- #endregion
- #region 1.2 将Excel数据写入数据库中
- #region 1.2.1 对List集合进行有效性校验
- #region 1.2.1.1检测必填项是否必填
- if (enlist.Count() <= 0)
- {
- throw new Exception("请填写Excel模板信息数据。");
- }
- for (int i = 0; i < enlist.Count; i++)
- {
- PublisherView en = enlist[i];
- string errorMsgStr = "第" + (i + 1) + "行数据检测异常:";
- bool isHaveNoInputValue = false; // 是否含有未输入项
- if (string.IsNullOrEmpty(en.UnitCode))
- {
- errorMsgStr += "单位编号不能为空;";
- en.ErrorMessage = errorMsgStr;
- isHaveNoInputValue = true;
- }
- if (string.IsNullOrEmpty(en.UnitName))
- {
- errorMsgStr += "单位名称不能为空;";
- en.ErrorMessage = errorMsgStr;
- isHaveNoInputValue = true;
- }
- if (!string.IsNullOrEmpty(en.UnitCode))
- {
- if (PublishRepository.Entities.Any(x => x.UnitCode == en.UnitCode))
- {
- errorMsgStr += "单位编号已存在;";
- en.ErrorMessage = errorMsgStr;
- isHaveNoInputValue = true;
- }
- }
- if (!string.IsNullOrEmpty(en.UnitName))
- {
- if (PublishRepository.Entities.Any(x => x.UnitName == en.UnitName))
- {
- errorMsgStr += "单位名称已存在;";
- en.ErrorMessage = errorMsgStr;
- isHaveNoInputValue = true;
- }
- }
- if (!string.IsNullOrEmpty(en.IsPrintName))
- {
- if (!(IdNameExt.GetDictionaryItem(DictionaryItem.CF_YesOrNoStatus.ToString())
- .Any(x => x.Name == en.IsPrintName)))
- {
- errorMsgStr += "是否印刷厂不存在;";
- en.ErrorMessage = errorMsgStr;
- isHaveNoInputValue = true;
- }
- }
- if (!string.IsNullOrEmpty(en.IsPulishName))
- {
- if (!(IdNameExt.GetDictionaryItem(DictionaryItem.CF_YesOrNoStatus.ToString())
- .Any(x => x.Name == en.IsPulishName)))
- {
- errorMsgStr += "是否出版单位不存在;";
- en.ErrorMessage = errorMsgStr;
- isHaveNoInputValue = true;
- }
- }
- if (!string.IsNullOrEmpty(en.IsSupplierName))
- {
- if (!(IdNameExt.GetDictionaryItem(DictionaryItem.CF_YesOrNoStatus.ToString())
- .Any(x => x.Name == en.IsSupplierName)))
- {
- errorMsgStr += "是否供应商不存在;";
- en.ErrorMessage = errorMsgStr;
- isHaveNoInputValue = true;
- }
- }
- Regex reg = new Regex(@"^[\w-]+(\.[\w-]+)*@([\w-]+\.)+[a-zA-Z]+$");
- Regex regMobile = new Regex(@"^1[3|4|5|7|8]\d{9}$");
- if (!string.IsNullOrEmpty(en.Email))
- {
- if (!reg.IsMatch(en.Email))
- {
- errorMsgStr += "邮箱格式不正确;";
- en.ErrorMessage = errorMsgStr;
- isHaveNoInputValue = true;
- }
- }
- if (!string.IsNullOrEmpty(en.Mobile))
- {
- if (!regMobile.IsMatch(en.Mobile))
- {
- errorMsgStr += "手机号码不正确;";
- en.ErrorMessage = errorMsgStr;
- isHaveNoInputValue = true;
- }
- }
- if (isHaveNoInputValue) // 若必填项有值未填
- {
- en.IsExcelVaildateOK = false;
- en.ErrorMessage = errorMsgStr;
- errList.Add(en);
- errorMsg.AppendLine(errorMsgStr);
- }
- }
- #endregion
- // TODO:其他检测
- #region 1.2.1.3 循环写入验证成功的数据
- List<CF_Publish> publishList = new List<CF_Publish>();
- for (int i = 0; i < enlist.Count; i++)
- {
- PublisherView enA = enlist[i];
- if (enA.IsExcelVaildateOK == false) // 上面验证不通过,不进行此步验证
- {
- continue;
- }
- var curUser = EMIS.Utility.FormValidate.CustomPrincipal.Current;
- CF_Publish publish = new CF_Publish()
- {
- PublishID = Guid.NewGuid(),
- Fax = enA.Fax,
- Address = enA.Address,
- BandCard = enA.BandCard,
- BandName = enA.BandName,
- ContectUser = enA.ContectUser,
- Ein = enA.Ein,
- Email = enA.Email,
- IsSupplier = enA.IsSupplierName == "是" ? true : false,
- IsPulish = enA.IsPulishName == "是" ? true : false,
- IsPrint = enA.IsPrintName == "是" ? true : false,
- UnitShortName = enA.UnitName,
- UnitCode = enA.UnitCode,
- UnitName = enA.UnitName,
- Mobile = enA.Mobile,
- Phone = enA.Phone,
- Desc = enA.Desc,
- CreateUserID = curUser.UserID,
- CreateTime = DateTime.Now
- };
- publishList.Add(publish);
- }
- #endregion
- UnitOfWork.BulkInsert(publishList);//统一写入
- #endregion
- #endregion
- #region 1.3 返回各项数据值
- OkCount = enlist.Distinct().Count() - errList.Distinct().Count();//共条数减去失败条数
- errdataList = errList.Distinct().OrderBy(x => x.UnitCode).ToList();
- ErrCount = errList.Distinct().Count();
- #endregion
- }
- #endregion
- }
- }
|