//public bool IsValid(string name, string password) //{ // password = Senlan2.Weixin.Services.EncryptService.Encode(password); // using(Senlan2.Weixin.Models.Senlan2Entities ctx = new Models.Senlan2Entities()) // { // int count = ctx.Users.Count(u => u.LoginName == name && u.Password == password); // if (count > 0) // return true; // return false; // } //} public User IsBind(string openId) { using (DBEntity.SenLan2Entities ctx = new DBEntity.SenLan2Entities()) { return ctx.Users.Where(o => o.IsDeleted == false && o.WeixinOpenId == openId).FirstOrDefault(); } }
public override string ContractNoGenerator(DateTime signDate, int contractType, int commodityId, int businessPartnerId, int tradeType) { //使用目前的生成规则 关联公司代码 + 金属 + p/s + 年月日 + 序号 using (var ctx = new SenLan2Entities()) { string no; string datetime = signDate.ToString("yyyyMMdd"); BusinessPartner businessPartner = ctx.BusinessPartners.Where(b => b.Id == businessPartnerId).FirstOrDefault(); Commodity commodity = ctx.Commodities.Where(c => c.Id == commodityId).FirstOrDefault(); DateTime startTime = Convert.ToDateTime(signDate.ToShortDateString()); DateTime endTime = startTime.AddDays(1); int contractCount = ctx.Contracts.Where(o => o.SignDate >= startTime && o.SignDate < endTime && o.ContractType == contractType && o.InternalCustomerId == businessPartnerId).Count(); contractCount++; if (contractType == (int)ContractType.Purchase) { no = businessPartner.Code + "-" + "P" + datetime + "-" + contractCount.ToString(CultureInfo.InvariantCulture).PadLeft(3, '0'); } else { no = businessPartner.Code + "-" + "S" + datetime + "-" + contractCount.ToString(CultureInfo.InvariantCulture).PadLeft(3, '0'); } return no; } //return base.ContractNoGenerator(signDate, contractType, commodityId, businessPartnerId, tradeType); }
private string GetInvoiceNoByQuotaId(int quotaId, int invoiceType, string invoiceStr) { using (var ctx = new SenLan2Entities()) { Quota quota = ctx.Quotas.Where(q => q.Id == quotaId).FirstOrDefault(); string quotaNo = quota.QuotaNo; string invoiceNo = quotaNo.Replace('-', '.'); invoiceNo += (invoiceStr != string.Empty ? "." : ""); int nextInvoicesCount = 0; if (invoiceType == (int)CommercialInvoiceType.Provisional) { nextInvoicesCount = ctx.CommercialInvoices.Where(ci => ci.QuotaId == quotaId && ci.InvoiceType == (int)CommercialInvoiceType.Provisional).Count() + 1; } else { nextInvoicesCount = ctx.CommercialInvoices.Where(ci => ci.QuotaId == quotaId && (ci.InvoiceType == (int)CommercialInvoiceType.FinalCommercial || ci.InvoiceType == (int)CommercialInvoiceType.Final)).Count() + 1; } if (nextInvoicesCount > 1) { invoiceNo += invoiceStr + nextInvoicesCount; } else { invoiceNo += invoiceStr; } return invoiceNo; } }
public User GetUserByOpenId(string openId) { using (DBEntity.SenLan2Entities ctx = new DBEntity.SenLan2Entities()) { return ctx.Users.FirstOrDefault(o => o.WeixinOpenId == openId); } }
//临时发票号 public virtual string ProvisionalInvoiceNoGenerator(int quotaId) { using (var ctx = new SenLan2Entities()) { Quota quota = ctx.Quotas.Where(q => q.Id == quotaId).FirstOrDefault(); return quota.QuotaNo + "-Provisional"; } }
public static void WriteLog(string tableCode, string actionCode, int objectId, int userId, int? approveStageId) { try { using (var ctx = new SenLan2Entities(userId)) { // Add new log var l = new Log(); var doc = ctx.Documents.FirstOrDefault(o => o.TableCode == tableCode && !o.IsDeleted); if (doc == null) { throw new FaultException<ServerErr>(new ServerErr(ErrCode.DocumentNotFound), new FaultReason(ErrorMsgManager.GetErrMsg(ErrCode.DocumentNotFound))); } l.DocumentId = doc.Id; var logAction = ctx.LogActions.FirstOrDefault(o => o.Code == actionCode && !o.IsDeleted); if (logAction == null) { throw new FaultException<ServerErr>(new ServerErr(ErrCode.LogActionNotFound), new FaultReason(ErrorMsgManager.GetErrMsg(ErrCode.LogActionNotFound))); } l.LogActionId = logAction.Id; l.LogUserId = userId; if(approveStageId != null && approveStageId > 0) { l.ApprovalStageId = approveStageId; } l.ObjectId = objectId; l.LogTime = DateTime.Now; ctx.Logs.AddObject(l); ctx.SaveChanges(); // Add Log Messages var lrs = ctx.LogRegistrations.Where( o => o.DocumentId == doc.Id && o.LogActionId == logAction.Id && !o.IsDeleted).ToList(); foreach (var lr in lrs) { var lm = new LogMessage {LogId = l.Id, LogRegistrationId = lr.Id, UserId = lr.UserId, IsRead = false}; ctx.LogMessages.AddObject(lm); } ctx.SaveChanges(); } } catch (OptimisticConcurrencyException) { throw new FaultException(ErrCode.OptimisticConcurrencyErr.ToString()); } }
/// <summary> /// 计算期现正套的保本基差的方法是: /// (已点价批次以及期货头寸的总盈亏+未点价采购批次的升贴水×数量-未点价销售批次的升贴水×数量)/现货批次的敞口数量 /// </summary> /// <param name="hg"></param> /// <param name="ctx"></param> public void Handle(HedgeGroup hg, SenLan2Entities ctx) { hg.StopLossSpread = null; //LME PnL var lmeLines = ctx.HedgeLineLMEPositions.Include("LMEPosition").Include("LMEPosition.Commodity") .Where(o => o.HedgeGroupId == hg.Id && !o.IsDeleted) .ToList(); decimal lmePnL = LMEPositionHelper.CalcPositionPnL(lmeLines)*(hg.Rate ?? 1) - lmeLines.Sum(o => o.AssignedCommission) * (hg.Rate ?? 1); //SHFE PnL var shfeLines = ctx.HedgeLineSHFEPositions.Include("SHFEPosition").Include("SHFEPosition.Commodity") .Where(o => o.HedgeGroupId == hg.Id && !o.IsDeleted) .ToList(); decimal shfePnL = SHFEPositionHelper.CalcPositionPnL(shfeLines) - shfeLines.Sum(o => o.AssignedCommission); decimal totalPnL = (lmePnL + shfePnL); //用于算基差,所以要加负号 //Quota PnL var quotaLines = ctx.HedgeLineQuotas.Include("Quota") .Include("Quota.Contract") .Include("Quota.Pricings") .Include("Quota.Currency") .Where(o => o.HedgeGroupId == hg.Id && !o.IsDeleted).ToList() .Select(o => o.Quota).ToList(); var exposure = (decimal)quotaLines.Sum(o => o.Quantity * o.Contract.ContractTypeValue); if (exposure != 0) { foreach (var q in quotaLines) { decimal rate = 1; if (q.Currency.Code != "CNY") rate = hg.Rate ?? 1; if (q.PricingStatus == (int)PricingStatus.Complete) { totalPnL += (q.FinalPrice * q.Quantity * q.Contract.ContractTypeValue ?? 0) * rate; } else { var pricings = q.Pricings.Where(o => !o.IsDeleted).ToList(); var unpricedQty = (decimal)(q.Quantity - pricings.Sum(o => o.PricingQuantity)); totalPnL += (decimal)pricings.Sum(o => o.PricingQuantity * o.FinalPrice) * q.Contract.ContractTypeValue * rate; totalPnL += (unpricedQty * q.Premium * q.Contract.ContractTypeValue * rate) ?? 0; } } hg.StopLossSpread = totalPnL / exposure; } }
public static IPricingHandler GetPricingHandler(Quota quota, SenLan2Entities ctx) { switch (quota.PricingType) { case (int)PricingType.Fixed: return new FixPricingHandler(quota, ctx); case (int)PricingType.Manual: return new ManualPricingHandler(quota, ctx); case (int)PricingType.Average: return new AveragePricingHandler(quota,ctx); default: return null; } }
public string UnBind(string openId) { using (DBEntity.SenLan2Entities ctx = new DBEntity.SenLan2Entities()) { User u = ctx.Users.Where(o => o.IsDeleted == false && o.WeixinOpenId == openId).FirstOrDefault(); if (u == null) return "解绑定失败,请重试!"; u.WeixinOpenId = null; List<WeixinAlert> alerts = ctx.WeixinAlerts.Where(a => a.UserId == u.Id).ToList(); foreach (WeixinAlert alert in alerts) { ctx.DeleteObject(alert); } ctx.SaveChanges(); return string.Empty; } }
//批次号 public virtual string QuotaNoGenerator(int contractId) { //contract no + 序号 using (var ctx = new SenLan2Entities()) { Contract contract = ctx.Contracts.Where(o => o.Id == contractId).FirstOrDefault(); if (contract.TradeType == (int)TradeType.ShortDomesticTrade || contract.TradeType == (int)TradeType.ShortForeignTrade) { return contract.ContractNo; } int quotaCount = ctx.Quotas.Include("Contract").Where(o => o.ContractId == contractId && o.Contract.ContractType == contract.ContractType && o.Contract.TradeType == contract.TradeType).Count(); quotaCount++; return contract.ContractNo + "/" + quotaCount.ToString(CultureInfo.InvariantCulture).PadLeft(3, '0'); } }
public override string ContractNoGenerator(DateTime signDate, int contractType, int commodityId, int businessPartnerId, int tradeType) { //生成规则:公司代码 + 金属 + YYMMDD + D/L + 序号 //D是短单,L是长单 using (var ctx = new SenLan2Entities()) { string no; string tradeTypeValue = string.Empty; string datetime = signDate.ToString("yyMMdd"); BusinessPartner businessPartner = ctx.BusinessPartners.Where(b => b.Id == businessPartnerId).FirstOrDefault(); Commodity commodity = ctx.Commodities.Where(c => c.Id == commodityId).FirstOrDefault(); DateTime startTime = Convert.ToDateTime(signDate.ToShortDateString()); DateTime endTime = startTime.AddDays(1); if (tradeType == (int)TradeType.ShortDomesticTrade || tradeType == (int)TradeType.ShortForeignTrade) { tradeTypeValue = "D"; } else if(tradeType == (int)TradeType.LongDomesticTrade || tradeType == (int)TradeType.LongForeignTrade) { tradeTypeValue = "L"; } int contractCount = ctx.Contracts.Where(o => o.SignDate >= startTime && o.SignDate < endTime && o.ContractType == contractType).Count(); contractCount++; if (contractType == (int)ContractType.Purchase) { no = businessPartner.Code + "-" + commodity.Code + "-"+ datetime + tradeTypeValue + contractCount.ToString(CultureInfo.InvariantCulture).PadLeft(3, '0') + "-" + "P"; } else { no = businessPartner.Code + "-" + commodity.Code + "-" + datetime + tradeTypeValue + contractCount.ToString(CultureInfo.InvariantCulture).PadLeft(3, '0') + "-" + "S"; } return no; } }
public string Bind(string name, string password, string openId) { password = Utility.Misc.Encrypt.Encode(password); using (DBEntity.SenLan2Entities ctx = new DBEntity.SenLan2Entities()) { User u = ctx.Users.Where(o => o.IsDeleted == false && o.LoginName == name && o.Password == password).FirstOrDefault(); if (u == null) return "账号或者密码错误,请重试!"; if(string.IsNullOrEmpty(u.WeixinOpenId) == false) { return "系统账号" + u.LoginName + "已经绑定微信账号, 请解绑定后再进行新系统账号的绑定!"; } if(ctx.Users.Count(o => o.IsDeleted == false && o.WeixinOpenId == openId) > 0) { return "该微信账号已经绑定系统账号,请解绑定后再进行新系统账号的绑定!"; } u.WeixinOpenId = openId; ctx.SaveChanges(); return string.Empty; } }
public static Currency GetCurrencyByPricingBasis(PricingBasis pb) { using (var ctx = new SenLan2Entities()) { switch ((int)pb) { case (int)PricingBasis.LME3M: return ctx.Currencies.FirstOrDefault(c => c.Code == "USD"); case (int)PricingBasis.LMECash: return ctx.Currencies.FirstOrDefault(c => c.Code == "USD"); case (int)PricingBasis.SGESettlement: return ctx.Currencies.FirstOrDefault(c => c.Code == "CNY"); case (int)PricingBasis.SHFE01: case (int)PricingBasis.SHFE02: case (int)PricingBasis.SHFE03: case (int)PricingBasis.SHFE04: case (int)PricingBasis.SHFE05: case (int)PricingBasis.SHFE06: case (int)PricingBasis.SHFE07: case (int)PricingBasis.SHFE08: case (int)PricingBasis.SHFE09: case (int)PricingBasis.SHFE10: case (int)PricingBasis.SHFE11: case (int)PricingBasis.SHFE12: return ctx.Currencies.FirstOrDefault(c => c.Code == "CNY"); case (int)PricingBasis.SHX: return ctx.Currencies.FirstOrDefault(c => c.Code == "CNY"); case (int)PricingBasis.SHY: return ctx.Currencies.FirstOrDefault(c => c.Code == "CNY"); case (int)PricingBasis.PCJ: return ctx.Currencies.FirstOrDefault(c => c.Code == "CNY"); case (int)PricingBasis.NC: return ctx.Currencies.FirstOrDefault(c => c.Code == "CNY"); default: return null; } } }
public ManualPricingHandler(Quota quota, SenLan2Entities ctx) : base(quota, ctx) { }
public BasePricingHandler(Quota quota, SenLan2Entities ctx) { Quota = quota; CTX = ctx; }
public AveragePricingHandler(Quota quota, SenLan2Entities ctx) :base(quota, ctx) { }
public void Handle(HedgeGroup hg, SenLan2Entities ctx) { hg.StopLossSpread = null; var lmeLines = ctx.HedgeLineLMEPositions.Include("LMEPosition").Include("LMEPosition.Commodity") .Where(o => o.HedgeGroupId == hg.Id && !o.IsDeleted) .ToList(); var shfeLines = ctx.HedgeLineSHFEPositions.Include("SHFEPosition").Include("SHFEPosition.Commodity") .Where(o => o.HedgeGroupId == hg.Id && !o.IsDeleted) .ToList(); if (lmeLines.Count > 0) { var groups = lmeLines.GroupBy(o => o.LMEPosition.PromptDate); decimal exposure = 0; foreach (var @group in groups) { var list = @group.ToList(); var tmp = Math.Round( (decimal) list.Sum( o => o.AssignedLotAmount*o.LMEPosition.TradeDirectionValue* o.LMEPosition.Commodity.LMEQtyPerHand), RoundRules.QUANTITY, MidpointRounding.AwayFromZero); if (tmp != 0) { exposure = Math.Abs(tmp); break; } } if (exposure != 0) { var pnl = -lmeLines.Sum(o => o.AssignedLotAmount * o.LMEPosition.AgentPrice * o.LMEPosition.TradeDirectionValue * o.LMEPosition.Commodity.LMEQtyPerHand) - lmeLines.Sum(o => o.AssignedCommission); hg.StopLossSpread = pnl / exposure; } } else if (shfeLines.Count > 0) { var groups = shfeLines.GroupBy(o => o.SHFEPosition.PromptDate); decimal exposure = 0; foreach (var @group in groups) { var list = @group.ToList(); var tmp = Math.Round( (decimal)list.Sum( o => o.AssignedLotAmount * o.SHFEPosition.OpenCloseValue * o.SHFEPosition.Commodity.SHFEQtyPerHand), RoundRules.QUANTITY, MidpointRounding.AwayFromZero); if (tmp != 0) { exposure = Math.Abs(tmp); break; } } if (exposure != 0) { var pnl = -shfeLines.Sum( o => o.AssignedLotAmount * o.SHFEPosition.Price * o.SHFEPosition.TradeDirectionValue * o.SHFEPosition.Commodity.SHFEQtyPerHand) - shfeLines.Sum(o => o.AssignedCommission); hg.StopLossSpread = pnl / exposure; } } }