using BaseOUDAL;
using Microsoft.EntityFrameworkCore.Storage;
using Qdp.Pricing.Library.Base.Utilities;
using System.Data;
using System.Linq.Expressions;
using YLErp.Abstract;
using YLErp.Abstract.DataProviders;
using YLErp.BLL;
using YLErp.DBModels;
using YLErp.Model;
using YLErp.Modules.DataProviderModule;
namespace YLErp.Modules
{
#region----BaseService----
///
/// 服务基类
///
public abstract class BaseService : IDisposable where TDbContext : DbContext, new()
{
private TDbContext _dbContext;
protected BaseService(TDbContext dbContext = null)
{
_dbContext = dbContext;
}
protected BaseService(BaseService baseService)
{
if (baseService is null)
{
throw new ArgumentNullException(nameof(baseService));
}
_dbContext = baseService.DbContext;
OptDate = baseService.OptDate;
OptUser = baseService.OptUser;
}
///
/// 构造函数
///
protected BaseService(OptUserInfo userInfo)
{
OptUser = userInfo ?? throw new ArgumentNullException(nameof(userInfo));
}
///
/// 特殊情况下继承此构造函数
///
protected BaseService(OptUserInfo userInfo, TDbContext dbContext) : this(dbContext)
{
OptUser = userInfo ?? throw new ArgumentNullException(nameof(userInfo));
}
public OptUserInfo OptUser { get; protected set; }
public OptUserInfo UserInfo => OptUser;
///
/// 操作人ID
///
public int UserId => OptUser.UserId;
///
/// 操作人名称
///
public string UserName => OptUser.UserName;
///
/// 操作时间
///
protected DateTime OptDate { get; private set; } = DateTime.Now;
///
/// 设置输出SQL执行语句
///
[System.Diagnostics.Conditional("DEBUG")]
protected void SetDebugSqlLog()
{
AppContext.SetSwitch(nameof(SetDebugSqlLog), true);
}
///
/// dbcontext
///
protected virtual TDbContext DbContext
{
get
{
if (_dbContext == null)
{
_dbContext = new TDbContext();
if (_dbContext is IOptUserBase optBase)
{
optBase.OptUser = OptUser;
}
}
return _dbContext;
}
}
///
/// 使用数据保存对象更新数据实体对象
///
protected static void UpdateChanges(T dbModel, object saveModel, params string[] excludePropertyNames) where T : class
{
//_dbContext.Entry(dbModel).CurrentValues.SetValues(saveModel);
YieldChain.Helpers.ObjectHelper.MapValues(dbModel, saveModel, excludePropertyNames);
}
///
/// 开始一个事务(请使用using方式处理)
///
protected IDbContextTransaction BeginTransaction()
{
return DbContext.Database.BeginTransaction();
}
///
/// 重置数据库上下文(一般用于多线程时切换)
///
protected void ResetDbContext()
{
_dbContext = null;
}
///
/// 释放资源
///
public void Dispose()
{
Dispose(true);
GC.SuppressFinalize(this);
}
protected virtual void Dispose(bool disposing)
{
if (disposing)
{
if (_dbContext != null)
{
_dbContext.Dispose();
_dbContext = null;
}
}
}
///
/// 设置DBModel的操作人信息
///
protected void SetDBModelOpt(DBModelWithOperator model)
{
model.OptId = UserId;
model.OptName = UserName;
model.OptDate = OptDate;
}
///
/// 设置DBModel的创建人信息
///
protected void SetDBModelCreator(IDBModelWithCreator model)
{
model.CreatorId = UserId;
model.CreatorName = UserName;
model.CreateDate = OptDate;
}
///
/// 获取用户组
///
protected string GetUserGroup(string userId)
{
return UserBLL.GetUserGroup(userId);
}
///
/// 获取用户组
///
protected string GetUserGroup(int userId)
{
return UserBLL.GetUserGroup(userId);
}
///
/// 移除数据实体集合
///
protected IEnumerable RemoveEntities(Expression> predicate) where TEntity : class
{
var set = DbContext.Set();
var entities = set.Where(predicate);
set.RemoveRange(entities);
return entities;
}
///
/// 更新数据实体集合
///
protected void UpdateEntity(TEntity entity, object source) where TEntity : class
{
DbContext.Entry(entity).CurrentValues.SetValues(source);
}
}
///
/// 业务服务基类
///
public abstract class YLBaseService : BaseService
{
private UnderlyingDataProvider underlyingDataProvider;
///
/// 构造函数
///
protected YLBaseService(OptUserInfo userInfo) :
base(userInfo)
{
}
///
/// 构造函数
///
protected YLBaseService(YLBaseService baseService) : base(baseService)
{
}
///
/// 特殊情况下继承此构造函数
///
protected YLBaseService(OptUserInfo optUser, YLContext dbContext) : base(optUser, dbContext)
{
}
///
/// 标的数据提供
///
protected IUnderlyingDataProvider UnderlyingDataProvider => underlyingDataProvider ??= new UnderlyingDataProvider();
///
/// 标的数据提供
///
protected IPriceProvider UnderlyingPriceProvider => underlyingDataProvider ??= new UnderlyingDataProvider();
///
/// 获取当前交易日
///
public virtual DateTime SystemValueDate => valuedateBLL.ValueDate.Date;
}
///
/// 服务上下文基类
///
public class YLServiceContext : YLBaseService
{
public YLServiceContext(OptUserInfo userInfo) : base(userInfo)
{
}
}
///
/// 带有serviceContext的基类
///
public abstract class YLBaseService : YLBaseService
where TServiceContext : YLServiceContext
{
protected readonly TServiceContext _context;
protected YLBaseService(TServiceContext context) : base(context)
{
_context = context ?? throw new ArgumentNullException(nameof(context));
}
}
///
/// 客户服务基类
///
public abstract class ClientBaseService : BaseService
{
private YLContext _yldb;
///
/// 构造函数
///
protected ClientBaseService(OptUserInfo userInfo) : base(userInfo)
{
}
///
/// 构造函数
///
protected ClientBaseService(ClientBaseService baseService) : base(baseService)
{
}
///
/// 构造函数
///
protected ClientBaseService(BaseService baseService) : base(baseService)
{
}
///
/// ylContext
///
protected YLContext yldb
{
get
{
return _yldb ??= DbContextFactory.GetYLDbContext();
}
}
///
/// 获取审批流程进度
///
/// 审批流程
/// 审批状态
/// 审批节点
/// 审批分支
///
public string GetApprovalStatus(List approvalprocesses, string approvalStatus, int aprovalOrderId, int processOrderBranch)
{
var branch = approvalprocesses.FirstOrDefault(x => x.approvalGroupId != 0);//审批流程有分支情况
var firstBranch = approvalprocesses.Where(x => (x.node == 1 && x.approvalGroupId == 0) || x.node == 0);//分支一总流程
var secondBranch = approvalprocesses.Where(x => (x.node == 2 && x.approvalGroupId == 0) || x.node == 0);//分支二总流程
var firstCount = firstBranch.Count();
var secondCount = secondBranch.Count();
var totalOpenProcessOrder = approvalprocesses.Count;
var processOrder = aprovalOrderId;
if (branch != null)
{
if (processOrderBranch == 1)
{
processOrder = firstBranch.ToArray().FirstIndexOf(x => x.order == processOrder);
totalOpenProcessOrder = firstCount;
}
else
{
processOrder = secondBranch.ToArray().FirstIndexOf(x => x.order == processOrder);
totalOpenProcessOrder = secondCount;
}
}
else
{
processOrder = aprovalOrderId - 1;
}
return approvalStatus + " 流程" + processOrder + "/" + totalOpenProcessOrder;
}
///
/// 获取当前审批进度角色
///
///
///
///
///
public int GetApprovalRoleId(List approvalprocesses, int aprovalOrderId, int processOrderBranch)
{
var curProcess = approvalprocesses.FirstOrDefault(x => x.order == aprovalOrderId && (x.node == processOrderBranch || x.node == 0) && x.approvalGroupId == 0);
if (curProcess != null)
{
return curProcess.roleId;
}
return 0;
}
}
#endregion
}