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 查询出版单位信息
///
/// 获取出版单位信息列表
///
///
///
public IGridResultSet 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(pageIndex, pageSize);
return query.OrderBy(x => x.UnitCode.Length).ThenBy(x => x.UnitCode).ToGridResultSet(pageIndex, pageSize);
}
#endregion
#region 2.0 编辑保存出版单位信息
///
/// 编辑出单社
///
///
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 删除出版单位信息
///
/// 删除出版单位信息
///
///
public void DeletePublish(List publishIDs)
{
try
{
PublishRepository.UnitOfWork.Delete(x => publishIDs.Contains(x.PublishID));
}
catch (Exception ex)
{
throw ex;
}
}
#endregion
#region 4.0 获取出版单位单位
///
/// 获取出版单位单位
///
///
public PublisherView GetSinglePublish(Guid publishID)
{
return PublishDAL.GetSinglePublish(publishID);
}
#endregion
#region 5.0 获取出版单位信息列表
///
/// 获取出版单位信息列表
///
///
///
public IList 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 获取所有出版单位信息-无条件模式
///
/// 获取所有出版单位信息-无条件模式
///
///
public IList 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 cellheader, out List errdataList, out int ErrCount, out int OkCount, string sourcePhysicalPath)
{
StringBuilder errorMsg = new StringBuilder(); // 错误信息
List errList = new List();
#region 1.1解析文件,存放到一个List集合里
cellheader.Remove("ErrorMessage");//去除异常列、导入操作不需要
// 1.1解析文件,存放到一个List集合里
List enlist =
NpoiExcelHelper.ExcelToEntityList(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 publishList = new List();
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
}
}