228 lines
8.8 KiB
C#
228 lines
8.8 KiB
C#
|
|
using Microsoft.AspNetCore.Authorization;
|
|
using YLErp.Cache;
|
|
using YLErp.Modules.MarginModule;
|
|
|
|
namespace YLErp.Web.Controllers
|
|
{
|
|
public class ClientMarginConfigController : BaseController
|
|
{
|
|
private IYLCache _cache;
|
|
public ClientMarginConfigController(IYLCache cache)
|
|
{
|
|
_cache = cache;
|
|
}
|
|
[MyAuthorize("互换簿记预设-互换预付金率维护")]
|
|
public ActionResult ClientMarginConfigList()
|
|
{
|
|
return View();
|
|
}
|
|
|
|
[MyAuthorize("互换簿记预设-互换预付金率修改")]
|
|
public ActionResult ClientMarginConfigEdit(string enid)
|
|
{
|
|
var id = DecryptInt(enid);
|
|
var fixedTerms = ConsMarginTerm.AllowedTerms.ToArray();
|
|
if (id == 0)
|
|
{
|
|
// 新增时,创建空的DTO记录
|
|
var emptyDetails = new List<ClientMarginDetailDto>();
|
|
|
|
for (int i = 0; i < fixedTerms.Length; i++)
|
|
{
|
|
emptyDetails.Add(new ClientMarginDetailDto
|
|
{
|
|
bond_term = fixedTerms[i],
|
|
init_rate = null,
|
|
maintain_rate = null,
|
|
swap_days = null
|
|
});
|
|
}
|
|
ViewBag.Details = emptyDetails;
|
|
return View(new client_margin_config() { value_date = DateTime.Now });
|
|
}
|
|
var marginConfig = yldb.clientMarginConfig.Find(id);
|
|
|
|
// 获取详情数据并转换为DTO
|
|
var details = yldb.clientMarginDetail.Where(d => d.config_id == id).ToList();
|
|
var detailDtos = new List<ClientMarginDetailDto>();
|
|
|
|
// 确保返回5条数据,按固定顺序
|
|
for (int i = 0; i < fixedTerms.Count(); i++)
|
|
{
|
|
var term = fixedTerms[i];
|
|
var existingDetail = details.FirstOrDefault(d => d.bond_term == term);
|
|
if (existingDetail != null)
|
|
{
|
|
detailDtos.Add(new ClientMarginDetailDto
|
|
{
|
|
bond_term = existingDetail.bond_term,
|
|
init_rate = existingDetail.init_rate * 100, // 转换为百分比显示
|
|
maintain_rate = existingDetail.maintain_rate * 100, // 转换为百分比显示
|
|
swap_days = existingDetail.swap_days
|
|
});
|
|
}
|
|
else
|
|
{
|
|
detailDtos.Add(new ClientMarginDetailDto
|
|
{
|
|
bond_term = term,
|
|
init_rate = null,
|
|
maintain_rate = null,
|
|
swap_days = null
|
|
});
|
|
}
|
|
}
|
|
|
|
ViewBag.Details = detailDtos;
|
|
|
|
return View(marginConfig);
|
|
}
|
|
|
|
|
|
|
|
[MyAuthorize("互换簿记预设-互换预付金率修改")]
|
|
public JsonResult ClientMarginConfigDelete(string enid)
|
|
{
|
|
var id = DecryptInt(enid);
|
|
var marginConfig = yldb.clientMarginConfig.Find(id);
|
|
if (marginConfig == null)
|
|
{
|
|
throw new ServiceException("数据库中未找到");
|
|
}
|
|
|
|
// 删除详情记录
|
|
var details = yldb.clientMarginDetail.Where(d => d.config_id == id).ToList();
|
|
yldb.clientMarginDetail.RemoveRange(details);
|
|
|
|
// 删除主记录
|
|
yldb.clientMarginConfig.Remove(marginConfig);
|
|
yldb.SaveChanges();
|
|
if (_cache!=null)
|
|
{
|
|
// 删除缓存ClientMarginRate开头的key
|
|
_cache.BatchDelete("ClientMarginRate:*");
|
|
}
|
|
return JsonSuccess("已删除");
|
|
}
|
|
|
|
[MyAuthorize("互换簿记预设-互换预付金率修改")]
|
|
public JsonResult ClientMarginConfigEditJson(client_margin_config marginConfig, List<ClientMarginDetailDto> details)
|
|
{
|
|
// 将DTO转换为数据库实体,处理必填和可选逻辑
|
|
var detailEntities = new List<client_margin_detail>();
|
|
var hasDefaultTerm = false;
|
|
|
|
if (details != null)
|
|
{
|
|
foreach (var dto in details)
|
|
{
|
|
// 默认期限(空字符串)必填
|
|
if (string.IsNullOrEmpty(dto.bond_term))
|
|
{
|
|
if (!dto.init_rate.HasValue || !dto.maintain_rate.HasValue)
|
|
{
|
|
return JsonError("默认期限的初始保证金率和维持保证金率必须填写");
|
|
}
|
|
hasDefaultTerm = true;
|
|
|
|
detailEntities.Add(new client_margin_detail
|
|
{
|
|
bond_term = "",
|
|
init_rate = dto.init_rate.Value / 100, // 转换百分比
|
|
maintain_rate = dto.maintain_rate.Value / 100, // 转换百分比
|
|
swap_days = dto.swap_days ?? 0
|
|
});
|
|
}
|
|
else
|
|
{
|
|
// 其他期限非必填,但如果填写了任何字段就需要验证
|
|
if (dto.init_rate.HasValue || dto.maintain_rate.HasValue || dto.swap_days.HasValue)
|
|
{
|
|
// 如果swap_days有值,则init_rate和maintain_rate必须有值
|
|
if (dto.swap_days.HasValue && (!dto.init_rate.HasValue || !dto.maintain_rate.HasValue))
|
|
{
|
|
return JsonError($"期限\"{dto.bond_term}\":当互换默认天数有值时,初始保证金率和维持保证金率必须填写");
|
|
}
|
|
|
|
detailEntities.Add(new client_margin_detail
|
|
{
|
|
bond_term = dto.bond_term,
|
|
init_rate = (dto.init_rate ?? 0) / 100, // 转换百分比
|
|
maintain_rate = (dto.maintain_rate ?? 0) / 100, // 转换百分比
|
|
swap_days = dto.swap_days ?? 0
|
|
});
|
|
}
|
|
}
|
|
}
|
|
}
|
|
|
|
// 验证默认期限必须填写
|
|
if (!hasDefaultTerm)
|
|
{
|
|
return JsonError("默认期限的保证金配置信息必须填写");
|
|
}
|
|
|
|
new ClientMarginConfigService(CurUser).SaveMarginConfigWithDetails(marginConfig, detailEntities);
|
|
if (_cache != null)
|
|
{
|
|
// 删除缓存ClientMarginRate开头的key
|
|
_cache.BatchDelete("ClientMarginRate:*");
|
|
}
|
|
return JsonSuccess("已修改");
|
|
}
|
|
|
|
[HttpPost]
|
|
public JsonResult ClientMarginConfigQuery(ClientMarginConfigReq req)
|
|
{
|
|
var sList = new ClientMarginConfigService(CurUser).SearchList(req);
|
|
GetExtendInfo(sList);
|
|
return JsonSuccess("",sList);
|
|
}
|
|
|
|
[HttpPost]
|
|
public JsonResult GetMarginConfigDetails(int configId)
|
|
{
|
|
var details = yldb.clientMarginDetail.Where(d => d.config_id == configId).ToList();
|
|
// 转换为百分比显示
|
|
foreach (var detail in details)
|
|
{
|
|
detail.init_rate *= 100;
|
|
detail.maintain_rate *= 100;
|
|
}
|
|
return Json(details);
|
|
}
|
|
/// <summary>
|
|
/// 获取适用金率
|
|
/// </summary>
|
|
/// <param name="req"></param>
|
|
/// <returns></returns>
|
|
[HttpPost]
|
|
[AllowAnonymous]
|
|
public JsonResult GetApplicableMarginRate([FromBody]GetClientMarginDetailReq req)
|
|
{
|
|
var detail= YLErp.Modules.UnderlyingModule.UnderlyingHelper.GetApplicableMarginRate(req.clientId, req.underlyingCode, req.valueDate);
|
|
return JsonSuccess("",detail);
|
|
}
|
|
|
|
private void GetExtendInfo(IEnumerable<ClientMarginConfigDto> marginConfigs)
|
|
{
|
|
foreach (var item in marginConfigs)
|
|
{
|
|
if (item.client_id != 0)
|
|
{
|
|
var client = DataCacheProvider.GetClientDataSource().GetData(item.client_id);
|
|
if (client != null)
|
|
{
|
|
item.client_name = client.Name;
|
|
}
|
|
}
|
|
|
|
// 百分比转换已在服务层处理,此处不需要重复转换
|
|
}
|
|
}
|
|
|
|
|
|
}
|
|
}
|