Files
zszq-trs/YLErpWeb/Controllers/BasketUnderlyingController.cs
T
2024-05-09 14:06:26 +08:00

116 lines
4.4 KiB
C#

using YLErp.Modules.UnderlyingModule;
namespace YLErp.Web.Controllers
{
public class BasketUnderlyingController : BaseController
{
public ActionResult BasketUnderlyingSetting()
{
return View();
}
public ActionResult PopBasketUnderlyingSettingView()
{
return View();
}
public ActionResult EditBasketUnderlyingSettingView()
{
return View();
}
/// <summary>
/// 导入篮子标的
/// </summary>
public JsonResult UploadBasketData(IFormFile file)
{
try
{
if (file == null || string.IsNullOrWhiteSpace(file.FileName))
{
return JsonError("未获取上传文件");
}
BasketUnderlyingDto result = null;
var fileExtension = Path.GetExtension(file.FileName);
using (var readStream = file.OpenReadStream())
{
result = new BasketUnderlyingService(CurUser).ImportBasketDatasFromExcel(readStream);
return JsonSuccess("导入成功", result);
}
}
catch (Exception ex)
{
LogFactory.GetLogger("导入篮子标的").Error("导入组合标的出错", ex);
return JsonError(ex.Message);
}
}
public JsonResult GetBasketUnderlyingByName(string name)
{
if (name.IsNullOrWhiteSpace())
{
return JsonError("请传入别名");
}
var su = new BasketUnderlyingService(CurUser).GetBasketUnderlyingByName(name);
if (su == null)
{
return JsonError("没有相关组合标的");
}
return JsonSuccess("", su);
}
// GET: Basket__underlying
public JsonResult SaveBasketUnderlying(BasketUnderlyingDto req)
{
if (req == null)
{
return JsonError("保存的对象不能为空");
}
req.OptId = CurUser.UserId;
req.OptName = CurUser.UserName;
req.OptDate = DateTime.Now;
new BasketUnderlyingService(CurUser).SaveBasketUnderlying(req);
return JsonSuccess("保存成功", req.OptId);
}
[HttpPost]
public ActionResult SaveBasketUnderlyingSettingEdit(BasketUnderlyingDto req)
{
var existUnderlying = yldb.underlying_manager.FirstOrDefault(u => u.UnderlyingCode == req.UnderlyingCode);
if (existUnderlying == null)
{
return JsonError("系统中没有相关的标的");
}
if (req.Items.Any(n => string.IsNullOrWhiteSpace(n.code)))
{
return JsonError("篮子标的列表中不应存在空白标的");
}
var useWhiteCode = new StockBlackWhiteService(CurUser).GetStockBlackWhiteList(Configuration.Enums.LimitRangeEnum.Swap, out var Codes);
// 篮子标的到期日
DateTime? MaturityDate = DateTime.MaxValue;
foreach (var item in req.Items)
{
var um = DataCacheProvider.GetUnderlyingDataSource().GetData(item.code);
if (ConsGlobal.InstrumentType.IsStock(um.UnderlyingInstrumentType) && Codes != null && useWhiteCode == Codes.Contains(item.code))
{
return JsonError($"标的代码:{item.code} {(useWhiteCode ? "存在于黑名单中" : "不在白名单中")}");
}
var Date = DataCacheProvider.GetUnderlyingDataSource().GetData(item.code).MaturityDate;
MaturityDate = Date < MaturityDate ? Date : MaturityDate;
item.Price = null;
}
existUnderlying.MaturityDate = MaturityDate == DateTime.MaxValue ? null : MaturityDate;
existUnderlying.SubData = JsonHelper.Stringify(req.Items);
var removeHisList = yldb.UnderlyingHisData.Where(O => O.UnderlyingCode == existUnderlying.UnderlyingCode);
yldb.UnderlyingHisData.RemoveRange(removeHisList);
var removePriceList = yldb.eod_stock_price.Where(O => O.UnderlyingCode == existUnderlying.UnderlyingCode);
yldb.eod_stock_price.RemoveRange(removePriceList);
yldb.SaveChanges();
return Json("修改成功!");
}
}
}