380 lines
11 KiB
C#
380 lines
11 KiB
C#
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----
|
|
|
|
/// <summary>
|
|
/// 服务基类
|
|
/// </summary>
|
|
public abstract class BaseService<TDbContext> : IDisposable where TDbContext : DbContext, new()
|
|
{
|
|
private TDbContext _dbContext;
|
|
|
|
protected BaseService(TDbContext dbContext = null)
|
|
{
|
|
_dbContext = dbContext;
|
|
}
|
|
|
|
protected BaseService(BaseService<TDbContext> baseService)
|
|
{
|
|
if (baseService is null)
|
|
{
|
|
throw new ArgumentNullException(nameof(baseService));
|
|
}
|
|
|
|
_dbContext = baseService.DbContext;
|
|
|
|
OptDate = baseService.OptDate;
|
|
OptUser = baseService.OptUser;
|
|
}
|
|
|
|
/// <summary>
|
|
/// 构造函数
|
|
/// </summary>
|
|
protected BaseService(OptUserInfo userInfo)
|
|
{
|
|
OptUser = userInfo ?? throw new ArgumentNullException(nameof(userInfo));
|
|
}
|
|
|
|
/// <summary>
|
|
/// 特殊情况下继承此构造函数
|
|
/// </summary>
|
|
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;
|
|
|
|
/// <summary>
|
|
/// 操作人ID
|
|
/// </summary>
|
|
public int UserId => OptUser.UserId;
|
|
|
|
/// <summary>
|
|
/// 操作人名称
|
|
/// </summary>
|
|
public string UserName => OptUser.UserName;
|
|
|
|
/// <summary>
|
|
/// 操作时间
|
|
/// </summary>
|
|
protected DateTime OptDate { get; private set; } = DateTime.Now;
|
|
|
|
/// <summary>
|
|
/// 设置输出SQL执行语句
|
|
/// </summary>
|
|
[System.Diagnostics.Conditional("DEBUG")]
|
|
protected void SetDebugSqlLog()
|
|
{
|
|
AppContext.SetSwitch(nameof(SetDebugSqlLog), true);
|
|
}
|
|
|
|
/// <summary>
|
|
/// dbcontext
|
|
/// </summary>
|
|
protected virtual TDbContext DbContext
|
|
{
|
|
get
|
|
{
|
|
if (_dbContext == null)
|
|
{
|
|
_dbContext = new TDbContext();
|
|
if (_dbContext is IOptUserBase optBase)
|
|
{
|
|
optBase.OptUser = OptUser;
|
|
}
|
|
}
|
|
return _dbContext;
|
|
}
|
|
}
|
|
|
|
|
|
/// <summary>
|
|
/// 使用数据保存对象更新数据实体对象
|
|
/// </summary>
|
|
protected static void UpdateChanges<T>(T dbModel, object saveModel, params string[] excludePropertyNames) where T : class
|
|
{
|
|
//_dbContext.Entry(dbModel).CurrentValues.SetValues(saveModel);
|
|
YieldChain.Helpers.ObjectHelper.MapValues(dbModel, saveModel, excludePropertyNames);
|
|
}
|
|
|
|
/// <summary>
|
|
/// 开始一个事务(请使用using方式处理)
|
|
/// </summary>
|
|
protected IDbContextTransaction BeginTransaction()
|
|
{
|
|
return DbContext.Database.BeginTransaction();
|
|
}
|
|
|
|
/// <summary>
|
|
/// 重置数据库上下文(一般用于多线程时切换)
|
|
/// </summary>
|
|
protected void ResetDbContext()
|
|
{
|
|
_dbContext = null;
|
|
}
|
|
|
|
/// <summary>
|
|
/// 释放资源
|
|
/// </summary>
|
|
public void Dispose()
|
|
{
|
|
Dispose(true);
|
|
GC.SuppressFinalize(this);
|
|
}
|
|
|
|
protected virtual void Dispose(bool disposing)
|
|
{
|
|
if (disposing)
|
|
{
|
|
if (_dbContext != null)
|
|
{
|
|
_dbContext.Dispose();
|
|
_dbContext = null;
|
|
}
|
|
}
|
|
}
|
|
|
|
/// <summary>
|
|
/// 设置DBModel的操作人信息
|
|
/// </summary>
|
|
protected void SetDBModelOpt(DBModelWithOperator model)
|
|
{
|
|
model.OptId = UserId;
|
|
model.OptName = UserName;
|
|
model.OptDate = OptDate;
|
|
}
|
|
|
|
/// <summary>
|
|
/// 设置DBModel的创建人信息
|
|
/// </summary>
|
|
protected void SetDBModelCreator(IDBModelWithCreator model)
|
|
{
|
|
model.CreatorId = UserId;
|
|
model.CreatorName = UserName;
|
|
model.CreateDate = OptDate;
|
|
}
|
|
|
|
/// <summary>
|
|
/// 获取用户组
|
|
/// </summary>
|
|
protected string GetUserGroup(string userId)
|
|
{
|
|
return UserBLL.GetUserGroup(userId);
|
|
}
|
|
|
|
/// <summary>
|
|
/// 获取用户组
|
|
/// </summary>
|
|
protected string GetUserGroup(int userId)
|
|
{
|
|
return UserBLL.GetUserGroup(userId);
|
|
}
|
|
|
|
/// <summary>
|
|
/// 移除数据实体集合
|
|
/// </summary>
|
|
protected IEnumerable<TEntity> RemoveEntities<TEntity>(Expression<Func<TEntity, bool>> predicate) where TEntity : class
|
|
{
|
|
var set = DbContext.Set<TEntity>();
|
|
var entities = set.Where(predicate);
|
|
set.RemoveRange(entities);
|
|
return entities;
|
|
}
|
|
|
|
/// <summary>
|
|
/// 更新数据实体集合
|
|
/// </summary>
|
|
protected void UpdateEntity<TEntity>(TEntity entity, object source) where TEntity : class
|
|
{
|
|
DbContext.Entry(entity).CurrentValues.SetValues(source);
|
|
}
|
|
}
|
|
|
|
/// <summary>
|
|
/// 业务服务基类
|
|
/// </summary>
|
|
public abstract class YLBaseService : BaseService<YLContext>
|
|
{
|
|
private UnderlyingDataProvider underlyingDataProvider;
|
|
|
|
/// <summary>
|
|
/// 构造函数
|
|
/// </summary>
|
|
protected YLBaseService(OptUserInfo userInfo) :
|
|
base(userInfo)
|
|
{
|
|
|
|
}
|
|
|
|
/// <summary>
|
|
/// 构造函数
|
|
/// </summary>
|
|
protected YLBaseService(YLBaseService baseService) : base(baseService)
|
|
{
|
|
|
|
}
|
|
|
|
/// <summary>
|
|
/// 特殊情况下继承此构造函数
|
|
/// </summary>
|
|
protected YLBaseService(OptUserInfo optUser, YLContext dbContext) : base(optUser, dbContext)
|
|
{
|
|
|
|
}
|
|
|
|
/// <summary>
|
|
/// 标的数据提供
|
|
/// </summary>
|
|
protected IUnderlyingDataProvider UnderlyingDataProvider => underlyingDataProvider ??= new UnderlyingDataProvider();
|
|
|
|
/// <summary>
|
|
/// 标的数据提供
|
|
/// </summary>
|
|
protected IPriceProvider UnderlyingPriceProvider => underlyingDataProvider ??= new UnderlyingDataProvider();
|
|
|
|
/// <summary>
|
|
/// 获取当前交易日
|
|
/// </summary>
|
|
public virtual DateTime SystemValueDate => valuedateBLL.ValueDate.Date;
|
|
}
|
|
|
|
/// <summary>
|
|
/// 服务上下文基类
|
|
/// </summary>
|
|
public class YLServiceContext : YLBaseService
|
|
{
|
|
public YLServiceContext(OptUserInfo userInfo) : base(userInfo)
|
|
{
|
|
|
|
}
|
|
}
|
|
|
|
/// <summary>
|
|
/// 带有serviceContext的基类
|
|
/// </summary>
|
|
public abstract class YLBaseService<TServiceContext> : YLBaseService
|
|
where TServiceContext : YLServiceContext
|
|
{
|
|
protected readonly TServiceContext _context;
|
|
|
|
protected YLBaseService(TServiceContext context) : base(context)
|
|
{
|
|
_context = context ?? throw new ArgumentNullException(nameof(context));
|
|
}
|
|
}
|
|
|
|
/// <summary>
|
|
/// 客户服务基类
|
|
/// </summary>
|
|
public abstract class ClientBaseService : BaseService<ClientDBContext>
|
|
{
|
|
private YLContext _yldb;
|
|
|
|
/// <summary>
|
|
/// 构造函数
|
|
/// </summary>
|
|
protected ClientBaseService(OptUserInfo userInfo) : base(userInfo)
|
|
{
|
|
|
|
}
|
|
|
|
/// <summary>
|
|
/// 构造函数
|
|
/// </summary>
|
|
protected ClientBaseService(ClientBaseService baseService) : base(baseService)
|
|
{
|
|
|
|
}
|
|
|
|
/// <summary>
|
|
/// 构造函数
|
|
/// </summary>
|
|
protected ClientBaseService(BaseService<ClientDBContext> baseService) : base(baseService)
|
|
{
|
|
|
|
}
|
|
|
|
/// <summary>
|
|
/// ylContext
|
|
/// </summary>
|
|
protected YLContext yldb
|
|
{
|
|
get
|
|
{
|
|
return _yldb ??= DbContextFactory.GetYLDbContext();
|
|
}
|
|
}
|
|
/// <summary>
|
|
/// 获取审批流程进度
|
|
/// </summary>
|
|
/// <param name="approvalprocesses">审批流程</param>
|
|
/// <param name="approvalStatus">审批状态</param>
|
|
/// <param name="aprovalOrderId">审批节点</param>
|
|
/// <param name="processOrderBranch">审批分支</param>
|
|
/// <returns></returns>
|
|
public string GetApprovalStatus(List<approvalprocess> 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;
|
|
|
|
}
|
|
/// <summary>
|
|
/// 获取当前审批进度角色
|
|
/// </summary>
|
|
/// <param name="approvalprocesses"></param>
|
|
/// <param name="aprovalOrderId"></param>
|
|
/// <param name="processOrderBranch"></param>
|
|
/// <returns></returns>
|
|
public int GetApprovalRoleId(List<approvalprocess> 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
|
|
}
|