451 lines
18 KiB
C#
451 lines
18 KiB
C#
using BaseOUDAL;
|
|
using YLErp.BLL;
|
|
|
|
namespace YLErp.Modules.ClientModule
|
|
{
|
|
/// <summary>
|
|
/// creditBLL
|
|
/// </summary>
|
|
public class ClientCreditService : YLBaseService
|
|
{
|
|
public ClientCreditService(OptUserInfo userInfo) : base(userInfo)
|
|
{
|
|
}
|
|
|
|
/// <summary>
|
|
/// 查询credit
|
|
/// </summary>
|
|
public SearchListResult<CreditDto> SearchList(ClientCreditReq req)
|
|
{
|
|
var db = new YLContext();
|
|
|
|
|
|
var creditOpenProcessOrder = db.approvalprocess.Where(x => x.processType == "CreditProcess").Count();
|
|
var query = from source in DbContext.credit.Where(a => req.CurUserClientIds.Contains(a.ClientId ?? 0))
|
|
join process in DbContext.approvalprocess.Where(a => a.processType == "CreditProcess") on source.ProcessOrderId equals process.order into pro
|
|
from proce in pro.DefaultIfEmpty()
|
|
select new CreditDto
|
|
{
|
|
id = source.id,
|
|
Credit = source.Credit,
|
|
PFECredit = source.PFECredit,
|
|
IMCredit = source.IMCredit,
|
|
AuditCredit = source.AuditCredit,
|
|
StockEqvNotional = source.StockEqvNotional,
|
|
AuditStockEqvNotional = source.AuditStockEqvNotional,
|
|
DeductStockEqvNotional = source.DeductStockEqvNotional,
|
|
ClientId = source.ClientId,
|
|
ClientName = source.ClientName,
|
|
CreditStartDate = source.CreditStartDate,
|
|
CreditDeadLine = source.CreditDeadLine,
|
|
ClientNumber = source.ClientNumber,
|
|
ProcessStatusOriginal = source.ProcessStatus,
|
|
ProcessStatus = source.ProcessStatus == "审批中" ? source.ProcessStatus + " 流程" + (source.ProcessOrderId - 1) + "/" + creditOpenProcessOrder : source.ProcessStatus,
|
|
ProcessRoleId = proce.roleId,
|
|
ProcessRoleName = "",
|
|
ProcessOptDate = source.ProcessOptDate,
|
|
CreatorId = source.CreatorId,
|
|
CreatorName = source.CreatorName,
|
|
CreateDate = source.CreateDate,
|
|
OptId = source.OptId,
|
|
OptName = source.OptName,
|
|
OptDate = source.OptDate,
|
|
VarietyId = source.VarietyId,
|
|
VarietyName = source.VarietyName,
|
|
Type = source.Type,
|
|
RoleId = source.RoleId,
|
|
RoleName = source.RoleName,
|
|
BuySellRight = source.BuySellRight,
|
|
IsAll = source.IsAll,
|
|
IsVariety = source.IsVariety,
|
|
QualificationRating = source.QualificationRating,
|
|
IsWhitelist = source.IsWhitelist,
|
|
Comments = source.Comments,
|
|
MarginLimit = source.MarginLimit
|
|
};
|
|
|
|
if (!string.IsNullOrEmpty(req.VarietyId))
|
|
{
|
|
List<string> vars = req.VarietyId.Split(',').ToList();
|
|
vars.ForEach(v =>
|
|
{
|
|
query = query.Where(d => !(d.Type == CreditTable.RoleType || (d.Type == CreditTable.ClientType && d.IsVariety == true))
|
|
|| ((d.Type == CreditTable.RoleType || d.IsWhitelist == 1) && ("," + d.VarietyId + ",").Contains("," + v + ","))
|
|
|| (d.IsWhitelist == 0 && !("," + d.VarietyId + ",").Contains("," + v + ",")));
|
|
});
|
|
}
|
|
|
|
if (req.ClientIds.Any())
|
|
{
|
|
query = query.Where(d => req.ClientIds.Contains(d.ClientId ?? 0));
|
|
}
|
|
|
|
if (req.RoleId != null)
|
|
{
|
|
query = query.Where(d => d.RoleId == req.RoleId);
|
|
}
|
|
|
|
if (!string.IsNullOrEmpty(req.RoleName))
|
|
{
|
|
query = query.Where(d => d.RoleName.Contains(req.RoleName));
|
|
}
|
|
|
|
if (!string.IsNullOrEmpty(req.Type))
|
|
{
|
|
query = query.Where(d => d.Type == req.Type);
|
|
}
|
|
|
|
if (req.Credit != null)
|
|
{
|
|
query = query.Where(d => d.Credit == req.Credit);
|
|
}
|
|
|
|
if (req.ClientId != null)
|
|
{
|
|
query = query.Where(d => d.ClientId == req.ClientId);
|
|
}
|
|
|
|
if (!string.IsNullOrEmpty(req.ClientName))
|
|
{
|
|
query = query.Where(d => d.ClientName.Contains(req.ClientName));
|
|
}
|
|
|
|
if (!string.IsNullOrWhiteSpace(req.ProcessStatuses))
|
|
{
|
|
query = query.Where(d => req.ProcessStatusList.Contains(d.ProcessStatusOriginal));
|
|
}
|
|
if (req.CreditDeadLineStart != DateTime.MinValue)
|
|
{
|
|
query = query.Where(d => d.CreditDeadLine >= req.CreditDeadLineStart);
|
|
}
|
|
|
|
if (req.CreditDeadLineEnd != DateTime.MinValue)
|
|
{
|
|
DateTime CreditDeadLineTemp = req.CreditDeadLineEnd.AddDays(1);
|
|
query = query.Where(d => d.CreditDeadLine < CreditDeadLineTemp);
|
|
}
|
|
if (!string.IsNullOrEmpty(req.ClientNumber))
|
|
{
|
|
query = query.Where(d => d.ClientNumber.Contains(req.ClientNumber));
|
|
}
|
|
|
|
if (!string.IsNullOrEmpty(req.VarietyName))
|
|
{
|
|
query = query.Where(d => !(d.Type == CreditTable.RoleType || (d.Type == CreditTable.ClientType && d.IsVariety == true))
|
|
|| ((d.Type == CreditTable.RoleType || d.IsWhitelist == 1) && d.VarietyName.Contains(req.VarietyName)) || (d.IsWhitelist == 0 && !d.VarietyName.Contains(req.VarietyName)));
|
|
}
|
|
|
|
if (string.IsNullOrEmpty(req.sidx))
|
|
{
|
|
req.sidx = "id";
|
|
req.sord = "desc";
|
|
}
|
|
var DbErpBaseContext = new ErpBaseContext();
|
|
var retListResult = query.ToSearchList(req);
|
|
var listRoles = DbErpBaseContext.Roles.ToList();
|
|
foreach (var creditLinq in retListResult.rows)
|
|
{
|
|
if (creditLinq.ProcessRoleId != null)
|
|
{
|
|
creditLinq.ProcessRoleName =
|
|
listRoles.FirstOrDefault(s => s.Id == creditLinq.ProcessRoleId).Name;
|
|
}
|
|
}
|
|
return retListResult;
|
|
}
|
|
|
|
/// <summary>
|
|
/// 查询credit processlog
|
|
/// </summary>
|
|
public SearchListResult<ProcesslogViewModel> SearchCreditList(ProcesslogReq req)
|
|
{
|
|
var predicate = ProcesslogReqUtil.CreatePredicate("CreditProcess", req);
|
|
var query = from processlog in DbContext.CreditProcessLog.Where(predicate)
|
|
join credit in DbContext.credit on processlog.TypeId equals credit.id into t_credit
|
|
from credit in t_credit.DefaultIfEmpty()
|
|
select new ProcesslogViewModel
|
|
{
|
|
id = processlog.id,
|
|
TypeId = processlog.TypeId,
|
|
ClientName = credit.ClientName,
|
|
ProcessType = processlog.ProcessType,
|
|
ProcessStatus = processlog.ProcessStatus,
|
|
Log = processlog.Log,
|
|
OptId = processlog.OptId,
|
|
//OptName = systemUsersD.LoginName,
|
|
CreateTime = processlog.CreateTime
|
|
};
|
|
|
|
|
|
if (string.IsNullOrEmpty(req.sidx))
|
|
{
|
|
req.sidx = "id";
|
|
req.sord = "desc";
|
|
}
|
|
var retListResult = query.ToSearchList(req);
|
|
|
|
foreach (var row in retListResult.rows)
|
|
{
|
|
row.OptName = UserBLL.GetById(row.OptId ?? 0)?.Name;//OTC-8651
|
|
}
|
|
|
|
return retListResult;
|
|
}
|
|
|
|
/// <summary>
|
|
/// creditController.SubmitProcess
|
|
/// </summary>
|
|
public string SubmitProcess(int creditId)
|
|
{
|
|
try
|
|
{
|
|
var credit = DbContext.credit.FirstOrDefault(s => s.id == creditId);
|
|
if (credit == null)
|
|
{
|
|
throw new ServiceException("请先保存授信基本信息");
|
|
}
|
|
if (PS.Config.ClientElement.IsCreditLimit &&
|
|
!DbContextFactory.GetClientDbContext(OptUser).client.Any(n => n.id == credit.ClientId && n.IsCreditOn == 1))
|
|
{
|
|
throw new ServiceException("请确认客户的授信是打开的");
|
|
}
|
|
////授信审批通过时校验授权交易类型
|
|
//if (!new CreditTableService(OptUser).derivativesInvestmentVarietiesCheck(credit))
|
|
//{
|
|
// throw new ServiceException("客户必须为同业交易商或AAA级以上银行,才可以对互换进行授信!");
|
|
//}
|
|
|
|
var processList = DbContext.approvalprocess.Where(s => s.processType == "CreditProcess").ToList();//判断有没有设置授信审批流程
|
|
|
|
//未设置流程 直接设置成授信状态
|
|
if (processList.Count == 0)
|
|
{
|
|
credit.ProcessStatus = "已审批";
|
|
credit.ProcessOrderId = -2;
|
|
credit.ProcessOptDate = DateTime.Now;
|
|
CreditLog(credit.id, "通过审批", "未设置审批流程,直接通过");
|
|
DbContext.SaveChanges();
|
|
return "已审批";
|
|
}
|
|
if (credit.ProcessStatus == "已审批")
|
|
{
|
|
return "已审批,无需再次提交";
|
|
}
|
|
|
|
if (credit.ProcessStatus == "审批中")
|
|
{
|
|
return "正在审批中";
|
|
}
|
|
|
|
if (credit.ProcessStatus == "已拒绝")
|
|
{
|
|
credit.ProcessStatus = "审批中";
|
|
credit.ProcessOrderId = 1;
|
|
credit.ProcessOptDate = DateTime.Now;
|
|
CreditLog(credit.id, "重新提交审批");
|
|
DbContext.SaveChanges();
|
|
return "已重新提交授信审批";
|
|
}
|
|
|
|
int orderId = credit.ProcessOrderId;
|
|
credit.ProcessStatus = "审批中";
|
|
credit.ProcessOrderId = 1;
|
|
credit.ProcessOptDate = DateTime.Now;
|
|
CreditLog(credit.id, "提交审批");
|
|
DbContext.SaveChanges();
|
|
|
|
return "已提交,正在审批中";
|
|
}
|
|
catch (Exception ex)
|
|
{
|
|
LogFactory.GetLogger("提交授信审批").Error(ex);
|
|
throw new ServiceException(ex.Message, ex);
|
|
}
|
|
}
|
|
|
|
#region----UpdateProcessCreditLog-----
|
|
|
|
/// <summary>
|
|
/// 客户授信审批
|
|
/// </summary>
|
|
public string UpdateProcessCreditLog(int creditId, string status, string text)
|
|
{
|
|
if (string.IsNullOrEmpty(status))
|
|
{
|
|
throw new ServiceException("status参数传入有误");
|
|
}
|
|
|
|
if (creditId < 1)
|
|
{
|
|
throw new ServiceException("审批失败,参数传入有误");
|
|
}
|
|
|
|
var credit = DbContext.credit.Find(creditId);
|
|
|
|
if (credit == null)
|
|
{
|
|
throw new ServiceException("审批失败,系统中没有该授信记录");
|
|
}
|
|
|
|
try
|
|
{
|
|
if (status == "pass")
|
|
{
|
|
////授信审批通过时校验授权交易类型
|
|
//if (!new CreditTableService(OptUser).derivativesInvestmentVarietiesCheck(credit)) {
|
|
// throw new ServiceException("客户必须为同业交易商或AAA级以上银行,才可以对互换进行授信!");
|
|
//}
|
|
|
|
// //检查是否存在对应的资信评级
|
|
// var clientrating = db.Client_Rating.Where(x => !x.IsDeleted && x.ProcessStatus == "已审批" && x.ClientId == credit.ClientId
|
|
//&& credit.CreditStartDate >= x.RatingStartDate).OrderByDescending(x => x.RatingDeadLine).ThenByDescending(x=>x.ProcessOptDate).FirstOrDefault();
|
|
// if (clientrating == null)
|
|
// {
|
|
// throw new ServcieException($"审批失败,请为客户设置从日期:{credit.CreditStartDate.Value.ToString("yyyy-MM-dd")}开始的资信评级");
|
|
// }
|
|
// else
|
|
// {
|
|
// if (credit.CreditDeadLine > clientrating.RatingDeadLine)
|
|
// {
|
|
// throw new ServcieException($"审批失败,客户的本次授信截止日期({credit.CreditDeadLine.Value.ToString("yyyy-MM-dd")})不能超过该客户的对应资信评级记录的截止日({clientrating.RatingDeadLine.Value.ToString("yyyy-MM-dd")})");
|
|
// }
|
|
// }
|
|
|
|
if (new ClientRatingService(OptUser).IsCreditOverTheCreditLine(credit))
|
|
{
|
|
throw new ServiceException("审批失败,客户授信申请额度不能超过资信等级额度");
|
|
}
|
|
|
|
var count = DbContext.approvalprocess.Where(s => s.processType == "CreditProcess").Count();
|
|
|
|
if (count == 0)
|
|
{
|
|
return SetCreditOpen(credit, text);
|
|
}
|
|
|
|
var processOrderId = credit.ProcessOrderId + 1;
|
|
if (processOrderId > count)
|
|
{
|
|
return SetCreditOpen(credit, text);
|
|
}
|
|
|
|
//继续审批流转
|
|
credit.ProcessOrderId = processOrderId;
|
|
//client.ProcessStatus = "审批中";
|
|
credit.ProcessOptDate = OptDate;
|
|
credit.OptId = UserId;
|
|
credit.OptName = UserName;
|
|
credit.OptDate = OptDate;
|
|
|
|
DbContext.CreditProcessLog.Add(new Processlog
|
|
{
|
|
TypeId = creditId,
|
|
ProcessType = "CreditProcess",
|
|
ProcessStatus = "通过审批",
|
|
Log = text,
|
|
OptId = UserId,
|
|
CreateTime = OptDate
|
|
});
|
|
CreditLog(creditId, "通过审批", text);
|
|
DbContext.SaveChanges();
|
|
|
|
return "提交成功";
|
|
}
|
|
else if (status == "reject")
|
|
{
|
|
credit.ProcessOrderId = -1;
|
|
credit.ProcessStatus = "已拒绝";
|
|
credit.ProcessOptDate = OptDate;
|
|
credit.OptId = UserId;
|
|
credit.OptName = UserName;
|
|
credit.OptDate = OptDate;
|
|
|
|
DbContext.CreditProcessLog.Add(new Processlog
|
|
{
|
|
TypeId = creditId,
|
|
ProcessType = "CreditProcess",
|
|
ProcessStatus = "审批拒绝",
|
|
Log = text,
|
|
OptId = UserId,
|
|
CreateTime = OptDate
|
|
});
|
|
CreditLog(creditId, "审批拒绝", text);
|
|
DbContext.SaveChanges();
|
|
|
|
return "提交成功";
|
|
}
|
|
|
|
throw new ServiceException("status参数不支持:" + status);
|
|
}
|
|
catch (ServiceException)
|
|
{
|
|
throw;
|
|
}
|
|
catch (Exception ex)
|
|
{
|
|
LogFactory.GetLogger<ClientProcessLogService>().Error("提交授信审批", ex);
|
|
throw new ServiceException(ex.Message, ex);
|
|
}
|
|
}
|
|
|
|
private string SetCreditOpen(CreditTable credit, string text)
|
|
{
|
|
//设置成授信审批状态
|
|
credit.ProcessOrderId = -2;
|
|
credit.ProcessStatus = "已审批";
|
|
credit.ProcessOptDate = OptDate;
|
|
credit.OptId = UserId;
|
|
credit.OptName = UserName;
|
|
credit.OptDate = OptDate;
|
|
|
|
DbContext.CreditProcessLog.Add(new Processlog
|
|
{
|
|
TypeId = credit.id,
|
|
ProcessType = "CreditProcess",
|
|
ProcessStatus = "已审批",
|
|
Log = text,
|
|
OptId = UserId,
|
|
CreateTime = OptDate
|
|
});
|
|
CreditLog(credit.id, "通过审批", text);
|
|
DbContext.SaveChanges();
|
|
|
|
return "提交成功";
|
|
}
|
|
|
|
/// <summary>
|
|
/// 授信日志
|
|
/// </summary>
|
|
public void CreditLog(int CreditId, string OptType, string Changes = null)
|
|
{
|
|
DbContext.credit_log.Add(new CreditLog
|
|
{
|
|
CreditId = CreditId,
|
|
OptType = OptType,
|
|
OptId = UserId,
|
|
Changes = Changes,
|
|
OptName = UserName,
|
|
OptDate = DateTime.Now
|
|
}); ;
|
|
DbContext.SaveChanges();
|
|
}
|
|
|
|
#endregion
|
|
|
|
/// <summary>
|
|
/// 授信是否可用于收益互换
|
|
/// </summary>
|
|
/// <param name="client"></param>
|
|
/// <returns></returns>
|
|
public static bool creditCanApplySwap(Client client)
|
|
{
|
|
//授信是否可用于授信
|
|
if (client.BusinessType != null && ((client.BusinessType.Contains("银行") && "AAA".Equals(client.BankCreditRating)) || (client.BusinessType.Contains("证券") && client.SamePeer == 0)))
|
|
{
|
|
return true;
|
|
}
|
|
return false;
|
|
}
|
|
}
|
|
}
|