using BaseOUDAL;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Text.RegularExpressions;
using System.Threading.Tasks;
using YLErp.DBModels;
namespace YLErp.Modules.SwapModule
{
public class SwapCapitalAccountService : YLBaseService
{
public SwapCapitalAccountService(OptUserInfo optUser) : base(optUser)
{
}
public SwapCapitalAccountService(YLBaseService baseService) : base(baseService)
{
}
///
/// 查询资金账号
///
public SearchListResult SearchList(SwapCapitalAccountQueryRequest req)
{
var predicate = PredicateBuilder.Create(n => 1==1);
if (req.ClientId>0)
{
predicate = predicate.And(n=>n.ClientId==req.ClientId);
}
if (!string.IsNullOrEmpty(req.TradeNumber))
{
predicate = predicate.And(n => n.SwapTradeNo.Contains(req.TradeNumber.Trim()));
}
if (!string.IsNullOrEmpty(req.FundAccount))
{
predicate = predicate.And(n => n.FundAccount.Contains(req.FundAccount.Trim()));
}
var query = DbContext.swap_fund_account.Where(predicate);
if (string.IsNullOrEmpty(req.sidx))
{
req.sidx = "id";
req.sord = "asc";
}
var retListResult = query.ToSearchList(req);
return retListResult;
}
///
/// 保存资金账号
///
///
public void SaveCapitalAccount(swap_fund_account req)
{
CheckValid(req);
swap_fund_account capital = DbContext.swap_fund_account.Find(req.id);
if (capital==null)
{
capital = new swap_fund_account();
}
capital.SwapTradeNo = req.SwapTradeNo;
capital.SwapTradeId = req.SwapTradeId;
capital.Status = req.Status;
capital.ClientId = req.ClientId;
capital.ClientName = req.ClientName;
capital.FundAccount = req.FundAccount;
if (req.id == 0)
{
DbContext.swap_fund_account.Add(capital);
}
DbContext.SaveChanges();
}
///
/// 删除场内资金账号
///
///
///
public void DeleteCapitalAccount(int id)
{
var capitalAccount= DbContext.swap_fund_account.Find(id);
if (capitalAccount==null)
{
throw new ServiceException("未找到该场内资金账号记录");
}
DbContext.swap_fund_account.Remove(capitalAccount);
DbContext.SaveChanges();
}
///
/// 查看场内资金账号
///
///
///
public swap_fund_account GetCapitalAccount(int id)
{
var capitalAccount = DbContext.swap_fund_account.Find(id);
if (capitalAccount == null)
{
throw new ServiceException("未找到该场内资金账号记录");
}
return capitalAccount;
}
///
/// 根据场内资金账号获取详情
///
///
///
public swap_fund_account GetCapitalAccountByFoundAccount(string foundAccount)
{
return DbContext.swap_fund_account.FirstOrDefault(x=>x.FundAccount== foundAccount&&x.Status== (int)SwapFoundAccountStatusEnum.正常);
}
///
/// 必填项校验
///
///
///
private void CheckRequired(swap_fund_account req)
{
if (req.ClientId == 0)
{
throw new ServiceException("交易对手方不能为空");
}
if (req.SwapTradeId == 0)
{
throw new ServiceException("互换编码不能为空");
}
if (string.IsNullOrEmpty(req.FundAccount)&&string.IsNullOrEmpty(req.FundAccount.Trim()))
{
throw new ServiceException("资金账号不能为空");
}
req.FundAccount=req.FundAccount.Trim();
//var uPattern = "^[0 - 9]{ 6,20}$";
//Regex regex = new Regex(uPattern);
//if (regex.IsMatch(req.FundAccount))
//{
// throw new ServiceException("资金账号应为20位以内的数字");
//}
}
///
/// 数据有效性校验
///
///
private void CheckValid(swap_fund_account req)
{
CheckRequired(req);
var predicate = PredicateBuilder.Create(n => n.FundAccount == req.FundAccount&&n.Status== (int)SwapFoundAccountStatusEnum.正常);
if (req.id > 0)
{
predicate = predicate.And(n => n.id != req.id);
}
if (DbContext.swap_fund_account.Any(predicate))
{
throw new ServiceException("已经存在正常状态的资金账号");
}
}
}
}