using YLErp.Modules.EodModule; namespace YLErp.Web.Controllers { public class CurrencyController : BaseController { [MyAuthorize("基础参数管理-设置货币")] public ActionResult setCurrency() { var currencies = DbContextFactory.GetYLDbContext().currency.ToList(); return View(currencies); } [MyAuthorize("基础参数管理-设置货币")] public ActionResult currencyEdit(string currencyCode) { if (string.IsNullOrWhiteSpace(currencyCode)) { return View(new Currency { StartDate = DateTime.Today }); } var dbCur = DbContextFactory.GetYLDbContext().currency.Where(O => O.CurrencyCode == currencyCode).FirstOrDefault(); if (dbCur == null) { throw new ServiceException($"数据库中未找到{currencyCode}"); } if (dbCur.StartDate.HasValue && dbCur.StartDate.Value.Year < 2010) { dbCur.StartDate = new DateTime(2010, 1, 1); } return View(dbCur); } [MyAuthorize("基础参数管理-设置货币")] public JsonResult currencyDelete(string currencyCode) { if (string.IsNullOrWhiteSpace(currencyCode)) { throw new ServiceException("货币代码为空"); } var db = DbContextFactory.GetYLDbContext(); var dbCur = db.currency.Where(O => O.CurrencyCode == currencyCode).FirstOrDefault(); if (dbCur == null) { throw new ServiceException($"数据库中未找到{currencyCode}"); } //验证CurrencyCode if (new EodCurrencyRateService(CurUser).HadCurrencyUsed(currencyCode)) { throw new ServiceException($"{currencyCode}已在使用,无法被删除"); } db.currency.Remove(dbCur); db.SaveChanges(); return JsonSuccess("已删除"); } [MyAuthorize("基础参数管理-设置货币")] public JsonResult currencyEditJson(Currency currency) { if (currency == null || string.IsNullOrWhiteSpace(currency.CurrencyCode) || string.IsNullOrWhiteSpace(currency.CurrencyName)) { throw new ServiceException("货币信息不全!"); } var db = DbContextFactory.GetYLDbContext(); var dbCur = db.currency.Where(O => O.CurrencyCode == currency.CurrencyCode).FirstOrDefault(); if (dbCur == null) { db.currency.Add(currency); } else { dbCur.CurrencyName = currency.CurrencyName; dbCur.CurrencyUnit = currency.CurrencyUnit; dbCur.CurrencySymbol = currency.CurrencySymbol; dbCur.StartDate = currency.StartDate; } db.SaveChanges(); return JsonSuccess("操作成功"); } public static List getList() { using (var db = DbContextFactory.GetYLDbContext()) { var list = new List(); var currencyCodes = db.currency.Select(o => o.CurrencyCode); foreach (var item in currencyCodes) { list.Add(new SelectListItem { Value = item, Text = item }); } return list; } } public static List getCashInCashOutList() { var list = new CurrencyController().getSelectCurrencyList(); return list; } public JsonResult getCurrencyList(DateTime? happenDate = null) { var list = getSelectCurrencyList(happenDate); return JsonSuccessData(list); } private List getSelectCurrencyList(DateTime? happenDate = null) { var defaultValues = new string[] { "CNY", "USD" }; var list = new List(); var db = DbContextFactory.GetYLDbContext(); var currencyCodes = db.currency.Select(o => o.CurrencyCode); if (happenDate != null) { currencyCodes = db.currency.Where(x => x.StartDate < happenDate).Select(o => o.CurrencyCode); } if (PS.Config.Company == Configuration.CompanyEnum.中金) { currencyCodes = currencyCodes.Where(o => defaultValues.Contains(o)); } foreach (var item in currencyCodes) { list.Add(new SelectListItem { Value = item, Text = item }); } return list; } } }