301 lines
9.6 KiB
C#
301 lines
9.6 KiB
C#
using Qdp.Pricing.Base.Utilities;
|
|
using YLErp.BLL.Calculation;
|
|
using YLErp.Modules.CalculationModule;
|
|
using YLErp.QdpModule;
|
|
|
|
namespace YLErp.Web.Controllers
|
|
{
|
|
public class CalendarController : BaseController
|
|
{
|
|
readonly YLContext db = new YLContext();
|
|
|
|
/// <summary>
|
|
/// 默认判断chn中的日期是否是假日
|
|
/// </summary>
|
|
public JsonResult IsHoliday(DateTime date)
|
|
{
|
|
return Json(QdpCalendarHelper.IsHoliday(date));
|
|
}
|
|
|
|
public JsonResult GetNonHoliday(DateTime date)
|
|
{
|
|
return Json(QdpCalendarHelper.GetNonHoliday(date));
|
|
}
|
|
|
|
public JsonResult GetNonHolidayDefore(DateTime date)
|
|
{
|
|
return Json(valuedateBLL.GetNonHolidayDefore(date));
|
|
}
|
|
|
|
public JsonResult GetNonHolidayShiftDay(DateTime from, int shiftDays)
|
|
{
|
|
var date = QdpCalendarHelper.BizDayShift(from, shiftDays);
|
|
return JsonSuccess("", date);
|
|
}
|
|
|
|
/// <summary>
|
|
/// 获取有效天数
|
|
/// </summary>
|
|
public JsonResult GetDay(DateTime from, DateTime to)
|
|
{
|
|
var dayCount = QdpCalendarHelper.GetWorkDayCount(from, to);
|
|
dayCount = dayCount + 1;
|
|
|
|
TimeSpan span = to - from;
|
|
int businessDays = span.Days + 1;
|
|
int fullWeekCount = businessDays / 7;
|
|
if (businessDays > fullWeekCount * 7)
|
|
{
|
|
int firstDayOfWeek = from.DayOfWeek == DayOfWeek.Sunday ? 7 : (int)from.DayOfWeek;
|
|
int lastDayOfWeek = to.DayOfWeek == DayOfWeek.Sunday ? 7 : (int)to.DayOfWeek;
|
|
if (lastDayOfWeek < firstDayOfWeek)
|
|
lastDayOfWeek += 7;
|
|
if (firstDayOfWeek <= 6)
|
|
{
|
|
if (lastDayOfWeek >= 7)
|
|
businessDays -= 2;
|
|
else if (lastDayOfWeek >= 6)
|
|
businessDays -= 1;
|
|
}
|
|
else if (firstDayOfWeek <= 7 && lastDayOfWeek >= 7)
|
|
{
|
|
businessDays -= 1;
|
|
}
|
|
}
|
|
businessDays -= fullWeekCount + fullWeekCount;
|
|
|
|
return JsonSuccess("", new { TradingDays = dayCount, Weekdays = businessDays, });
|
|
}
|
|
|
|
public JsonResult GetWorkDays(DateTime from, DateTime to)
|
|
{
|
|
var dayCount = QdpCalendarHelper.GetWorkDayCount(from, to);
|
|
return JsonSuccess("", dayCount);
|
|
}
|
|
|
|
/// <summary>
|
|
/// 获取有效天数(含小数)
|
|
/// </summary>
|
|
public JsonResult GetWorkDaysWithDecimal(DateTime from, DateTime to, underlying_manager underlying)
|
|
{
|
|
var dayCount = QdpCalendarHelper.GetWorkDayCount(from, to);
|
|
var variety = VarietyBLL.GetAllvarietyModel().FirstOrDefault(v => v.id == underlying.UnderlyingTypeId);
|
|
return JsonSuccess("", dayCount + QdpHelper.GetAddDays(DateTime.Now, variety != null && variety.HasNightMarket));
|
|
}
|
|
|
|
/// <summary>
|
|
/// 获取按照244,365等日期分类的qdp中的工作日
|
|
/// </summary>
|
|
/// <param name="from">交易日期</param>
|
|
/// <param name="to">到期日期</param>
|
|
public JsonResult DaysInPeriod(DateTime from, DateTime to, int varietyid = 0, SettlementTypeEnum settlementType = SettlementTypeEnum.ClosePrice)
|
|
{
|
|
if (PS.Config.Is润和)
|
|
{
|
|
from = DateTime.Today;
|
|
}
|
|
var ttmDays = 0d;
|
|
if (PS.Config.Is厦门象屿 && settlementType == SettlementTypeEnum.ReferencePrice)
|
|
{
|
|
ttmDays = TradeCalcHelper.CalculateTTMDaysForXiangYu(from, to, varietyid,
|
|
PS.Config.ErpElement.PrecisionOfMinuteInQuote || to == valuedateBLL.ValueDate);
|
|
}
|
|
else
|
|
{
|
|
ttmDays = TradeCalcHelper.CalculateTTMDays(from, to, varietyid,
|
|
PS.Config.ErpElement.PrecisionOfMinuteInQuote || to == valuedateBLL.ValueDate);
|
|
}
|
|
return JsonSuccessData(ttmDays);
|
|
}
|
|
|
|
public JsonResult AnnualizeFactor(DateTime from, DateTime to)
|
|
{
|
|
//和DaysInPeriod保持一致,先计算出精确的ttm
|
|
var ttmDays = TradeCalcHelper.CalculateTTMDays(from, to, 0, PS.Config.ErpElement.PrecisionOfMinuteInQuote);
|
|
return JsonSuccessData(QdpHelper.AnnualizeFactor(ttmDays, CalculatorHelper.GetTradeDayCount().ToDayCountImpl()));
|
|
}
|
|
|
|
/// <summary>
|
|
/// 获取假日日期
|
|
/// </summary>
|
|
/// <returns></returns>
|
|
[MyAuthorizeIgnore]
|
|
public JsonResult GetCalendars()
|
|
{
|
|
return Json(CalendarBLL.GetRecentCalendarModel());
|
|
}
|
|
|
|
/// <summary>
|
|
/// 获取假日日期
|
|
/// </summary>
|
|
/// <returns></returns>
|
|
[MyAuthorizeIgnore]
|
|
public JsonResult GetCalendarsV2()
|
|
{
|
|
if (PS.Config.ErpElement.SupportMultiCalendar)
|
|
{
|
|
return Json(new { version = "多日历" });
|
|
}
|
|
return Json(CalendarBLL.GetRecentCalendarModel());
|
|
}
|
|
|
|
public ActionResult CalendarYearEdit(string enid)
|
|
{
|
|
var id = DecryptInt(enid);
|
|
var calendar = db.calendar.Find(id);
|
|
return View(calendar);
|
|
}
|
|
|
|
public JsonResult SaveYearCalendar(CalendarReq calendar)
|
|
{
|
|
var caBLL = new CalendarBLL();
|
|
caBLL.UpdateSingle(calendar.id, calendar);
|
|
return JsonSuccess("保存日历成功", calendar);
|
|
}
|
|
|
|
public async Task<JsonResult> SaveUploadCalendar(CalendarReq calendar)
|
|
{
|
|
var id = 0;
|
|
if (!string.IsNullOrEmpty(calendar.EncryptId))
|
|
{
|
|
id = DecryptInt(calendar.EncryptId);
|
|
}
|
|
await Savecalendar(id);
|
|
return JsonSuccess("上传保存日历成功", calendar);
|
|
}
|
|
|
|
[MyAuthorize("基础参数管理-交易日历")]
|
|
public ActionResult CalendarList()
|
|
{
|
|
return View();
|
|
}
|
|
|
|
[HttpPost]
|
|
public JsonResult CalendarQuery(CalendarReq req)
|
|
{
|
|
var bll = new CalendarBLL();
|
|
var sList = bll.SearchList(req);
|
|
foreach (var item in sList.rows)
|
|
{
|
|
item.WorkingDays = item.GetWorkingDays();
|
|
item.HolidayJson = null;
|
|
}
|
|
return Json(sList);
|
|
}
|
|
|
|
public ActionResult CalendarView(string enid)
|
|
{
|
|
var intid = DecryptInt(enid);
|
|
var r = db.calendar.Find(intid);
|
|
return View(r);
|
|
}
|
|
|
|
public ActionResult CalendarEdit(string enid)
|
|
{
|
|
var isAdd = string.IsNullOrEmpty(enid) || enid == "0";
|
|
if (isAdd)
|
|
{
|
|
return View(new calendar());
|
|
}
|
|
var intid = DecryptInt(enid);
|
|
var r = db.calendar.Find(intid);
|
|
return View(r);
|
|
}
|
|
|
|
public ActionResult CalendarUpload(string enid)
|
|
{
|
|
calendar r = null;
|
|
var isAdd = string.IsNullOrEmpty(enid) || enid == "0";
|
|
if (isAdd)
|
|
{
|
|
return View(r);
|
|
}
|
|
|
|
var intid = DecryptInt(enid);
|
|
r = db.calendar.Find(intid);
|
|
|
|
return View(r);
|
|
}
|
|
|
|
[HttpPost]
|
|
public JsonResult CalendarEditJson(CalendarReq req)
|
|
{
|
|
var id = 0;
|
|
if (!string.IsNullOrEmpty(req.EncryptId))
|
|
{
|
|
id = DecryptInt(req.EncryptId);
|
|
}
|
|
var r = Savecalendar(id);
|
|
return JsonSuccess("更新成功", r);
|
|
}
|
|
|
|
[HttpPost]
|
|
public async Task<ActionResult> CalendarEdit(CalendarReq req)
|
|
{
|
|
var id = DecryptInt(req.EncryptId);
|
|
var r = await Savecalendar(id);
|
|
return Redirect("~/calendar/calendarView/?id=" + r.EncryptId);
|
|
}
|
|
|
|
private async Task<calendar> Savecalendar(int? id)
|
|
{
|
|
calendar r;
|
|
var isAddNew = id == null || id == 0;
|
|
if (isAddNew)
|
|
{
|
|
r = new calendar
|
|
{
|
|
OptId = CurUser.UserId,
|
|
OptName = CurUser.UserName,
|
|
CreateDate = DateTime.Now
|
|
};
|
|
|
|
db.calendar.Add(r);
|
|
}
|
|
else
|
|
{
|
|
r = db.calendar.Find(id);
|
|
}
|
|
|
|
await TryUpdateModelAsync(r);
|
|
|
|
if (string.IsNullOrEmpty(r.Country))
|
|
{
|
|
throw new ServiceException("日历名称 必须填写");
|
|
}
|
|
|
|
if (!r.Country.All(n => (n >= '0' && n <= '9') || (n >= 'a' && n <= 'z') || (n >= 'A' && n <= 'Z')))
|
|
{
|
|
throw new ServiceException("日历名称 只能由字母、数字组成");
|
|
}
|
|
|
|
if (string.IsNullOrEmpty(r.HolidayJson))
|
|
{
|
|
r.HolidayJson = "[]";
|
|
}
|
|
|
|
db.SaveChanges();
|
|
|
|
CalendarBLL.IsListOld = true;
|
|
CalendarBLL.ResetCalendarForQdp();
|
|
return r;
|
|
}
|
|
|
|
[HttpPost]
|
|
public JsonResult Deletecalendar(string id)
|
|
{
|
|
var intid = DecryptInt(id);
|
|
var r = db.calendar.Find(intid);
|
|
if (r == null)
|
|
{
|
|
return JsonError("找不到日历");
|
|
}
|
|
db.calendar.Remove(r);
|
|
db.SaveChanges();
|
|
CalendarBLL.IsListOld = true;
|
|
CalendarBLL.ResetCalendarForQdp();
|
|
return JsonSuccess("删除成功");
|
|
}
|
|
}
|
|
}
|