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

145 lines
6.3 KiB
C#

using OfficeOpenXml;
using YLErp.Modules.QuotationModule;
namespace YLErp.Web.Controllers
{
public class UnderlyingMainContractController : BaseController
{
readonly YLContext db = new YLContext();
[MyAuthorize("报价管理-报价合约管理")]
public ActionResult Index()
{
var records = new flat_price_quotationBLL().GetContractViewModel();
var viewModel = new UnderlyingMainContractViewModel()
{
Records = records,
EnableClientCalculation = valuedateBLL.EnableClientCalculation,
EnableMobileQuotation = valuedateBLL.EnableMobileQuotation,
EnableMobileVolQuote = valuedateBLL.EnableMobileVolQuote
};
return View(viewModel);
}
/// <summary>
/// 导出申万
/// </summary>
//[ValidateInput(false)]
[HttpPost]
public JsonResult ExportMainContract(string enid)
{
try
{
var contracts = db.underlying_main_contract.Where(u => u.NeedQuote == 1).ToList();
if (contracts != null && contracts.Count > 0)
{
// 报价合约导出的文件路径
var targetPath = Environment.GetFolderPath(Environment.SpecialFolder.DesktopDirectory);
var fileName = Path.Combine(targetPath, "报价合约列表.xlsx");
var newFile = new FileInfo(fileName);
if (newFile.Exists)
{
newFile.Delete();
newFile = new FileInfo(fileName);
}
using (var package = new ExcelPackage(newFile))
{
var row = 1;
var worksheet = package.Workbook.Worksheets.Add("报价合约信息");
// 插入第一行列名
worksheet.Cells[row, 1].Value = "标的品种";
worksheet.Cells[row, 2].Value = "标的代码";
worksheet.Cells[row, 3].Value = "行权价间隔";
worksheet.Cells[row, 4].Value = "最小交易数量";
foreach (var con in contracts)
{
row += 1;
worksheet.Cells[row, 1].Value = con.UnderlyingType;
worksheet.Cells[row, 2].Value = con.UnderlyingCode;
worksheet.Cells[row, 3].Value = con.StrikeInterval;
worksheet.Cells[row, 4].Value = con.MinimumAmount;
}
package.Save();
}
}
else
{
return JsonError("没有需要导出的报价合约");
}
}
catch (Exception e)
{
return JsonError("导出失败: " + e.Message);
}
return Json("导出成功");
}
[HttpPost]
public ActionResult Submit(UnderlyingMainContractViewModel viewModel)
{
try
{
var untypes = viewModel.Records.Select(v => v.UnderlyingType).ToList();
var contracts = db.underlying_main_contract.Where((u => untypes.Contains(u.UnderlyingType))).ToList();
foreach (var model in viewModel.Records)
{
var editcontract = contracts.Where(c => c.UnderlyingType == model.UnderlyingType).ToList();
if (editcontract == null || editcontract.Count == 0)
{
var underlying = new underlying_main_contract
{
OrderId = model.OrderId,
MarketCode = model.MarketCode,
UnderlyingType = model.UnderlyingType,
UnderlyingCode = model.UnderlyingCode,
NeedQuote = model.NeedQuote ? 1 : 0,
MinimumAmount = model.MinimumAmount,
StrikeInterval = model.StrikeInterval,
OptId = CurUser.UserId,
OptDate = DateTime.Now,
LastUpdateTime = DateTime.Now
};
db.underlying_main_contract.Add(underlying);
}
else
{
editcontract.ForEach(e => e.NeedQuote = 0);//补丁,数据重复,先都失效
var firstcontract = editcontract[0];
firstcontract.OrderId = model.OrderId;
firstcontract.MarketCode = model.MarketCode;
firstcontract.UnderlyingType = model.UnderlyingType;
firstcontract.UnderlyingCode = model.UnderlyingCode;
firstcontract.NeedQuote = model.NeedQuote ? 1 : 0;
firstcontract.MinimumAmount = model.MinimumAmount;
firstcontract.StrikeInterval = model.StrikeInterval;
firstcontract.OptId = CurUser.UserId;
firstcontract.OptDate = DateTime.Now;
firstcontract.LastUpdateTime = DateTime.Now;
}
}
var config = db.valuedate.FirstOrDefault(x => x.State == valuedate.当前使用);
config.EnableClientCalculation = viewModel.EnableClientCalculation ? 1 : 0;
config.EnableMobileQuotation = viewModel.EnableMobileQuotation ? 1 : 0;
config.EnableMobileVolQuote = viewModel.EnableMobileVolQuote ? 1 : 0;
db.SaveChanges();
valuedateBLL.ResetValueDate();
// 使缓存无效
underlying_main_contractBLL.IsListOld = true;
return Json("更新成功");
}
catch (Exception ex)
{
LogFactory.GetLogger<UnderlyingMainContractController>().Error("insert underlying_main_contract error", ex);
return JsonError("更新失败");
}
}
}
}