using System; using System.Collections.Generic; using System.Linq; using System.Text; using System.Threading.Tasks; using Winner.Balance.Facade; using Winner.Balance.GlobalCurrency; using Winner.DCTS.DataAccess; using Winner.DCTS.Entities; using Winner.DCTS.Entities.enums; using Winner.Framework.Core.Facade; using Winner.Framework.Utils; namespace Winner.DCTS.Facade { public class MatchTransfer : FacadeBase { private eBankTransaction EBankTransaction { get; set; } private Tdct_Match daMatch { get; set; } public bool DoTransfer(Tdct_Match match) { daMatch = match; //开启事务 BeginTransaction(); //关联事务 EBankTransaction= Transfer.GeteBankTransaction(); //出售者把币转到购买者 if (!SellToBuy()) { Rollback(); //回滚事务 UpdateFail(this.PromptInfo.MessageStack); return false; } //购买者把钱转到出售者 + 手续费 if (!BuyToSell()) { Rollback(); UpdateFail(this.PromptInfo.MessageStack); return false; } daMatch.TransferId =this.EBankTransaction; //撮合成功时修改撮合信息表状态 if (!UpdateSuccess()) { Rollback(); return false; } //解除代币以及人名币锁定 if (!UnFreeze()) { Rollback(); return false; } //同步代币动态交易信息 if (!SyncDynamice()) { Rollback(); return false; } Commit(); return true; } //出售者把币转到购买者 private bool SellToBuy() { BeginTransaction(); Currency currency =new Currency(CurrencyType.RMB, daMatch.BuyCount); UserPurse sellPurse = UserPurseProvider.Get(daMatch.SellUserId, daMatch.SellCurrency); UserPurse buyPurse = UserPurseProvider.Get(daMatch.BuyUserId, daMatch.SellCurrency); Transfer transfer =new Transfer(EBankTransaction); transfer.ReferenceTransactionFrom(this.Transaction); //如果买和卖都是同一个人则需要用转到系统用户再转入 if (daMatch.SellUserId== daMatch.BuyUserId) { //系统用户钱包 UserPurse transitPurse= UserPurseProvider.Get(AppConfig.TransitUserId, daMatch.SellCurrency); if (!transfer.DoTransferAndUnFreeze(sellPurse, transitPurse, currency, (int)TransferReson.出售者转入购买者, "出售货币中转出账", daMatch.SellFreezeId, "购买货币中转入账")) { Rollback(); Alert("出售者转中转钱包失败!", transfer.PromptInfo); return false; } if (!transfer.DoTransfer(transitPurse, buyPurse, currency, (int)TransferReson.出售者转入购买者, "出售货币中转出账", "购买货币中转入账")) { Rollback(); Alert("中转者转购买者失败!", transfer.PromptInfo); return false; } } else { if (!transfer.DoTransferAndUnFreeze(sellPurse, buyPurse, currency, (int)TransferReson.出售者转入购买者, "出售货币出账", daMatch.SellFreezeId, "购买货币入账")) { Rollback(); Alert("出售者转购买者失败!", transfer.PromptInfo); return false; } } Commit(); return true; } //购买者把钱转到出售者 + 手续费 private bool BuyToSell() { BeginTransaction(); Currency currency =new Currency(CurrencyType.RMB, daMatch.Amount); UserPurse buyPurse = UserPurseProvider.Get(daMatch.BuyUserId, daMatch.BuyCurrency); UserPurse sellPurse = UserPurseProvider.Get(daMatch.SellUserId, daMatch.BuyCurrency); //收取手续费 Transfer transfer =new Transfer(EBankTransaction); transfer.ReferenceTransactionFrom(this.Transaction); //如果买和卖都是同一个人则需要用转到系统用户再转入 if (daMatch.BuyUserId== daMatch.SellUserId) { UserPurse transitPurse = UserPurseProvider.Get(AppConfig.TransitUserId, daMatch.BuyCurrency); if (!transfer.DoTransferAndUnFreeze(buyPurse, transitPurse, currency, (int)TransferReson.购买者转入出售者, "购买货币中转付款", daMatch.BuyFreezeId, "出售货币中转入账")) { Rollback(); Alert("购买者转中转者失败!", transfer.PromptInfo); return false; } if (!transfer.DoTransfer(transitPurse, sellPurse, currency, (int)TransferReson.购买者转入出售者, "购买货币中转付款", "出售货币中转入账")) { Rollback(); Alert("中转者转出售者失败!", transfer.PromptInfo); return false; } } else { if (!transfer.DoTransferAndUnFreeze(buyPurse, sellPurse, currency, (int)TransferReson.购买者转入出售者, "购买货币付款", daMatch.BuyFreezeId, "出售货币入账")) { Rollback(); Alert("购买者转出售者失败!", transfer.PromptInfo); return false; } } if (daMatch.Fee <=0) { Commit(); return true; } UserPurse feePurse = UserPurseProvider.Get(AppConfig.SystemUserId, daMatch.BuyCurrency); Currency feeCurrency =new Currency(feePurse.CurrencyType, daMatch.Fee); if (!transfer.DoTransfer(sellPurse, feePurse, feeCurrency, (int)TransferReson.收取出售者手续费, "收取出售者手续费")) { Rollback(); Alert("收取出售者手续费失败!", transfer.PromptInfo); return false; } Commit(); return true; } //修改撮合成功信息 private bool UpdateSuccess() { daMatch.ReferenceTransactionFrom(this.Transaction); if (!daMatch.UpdateTransferSuccess(daMatch.MatchId, daMatch.TransferId.Value)) { Alert("修改撮合历史成功时发现失败", daMatch.PromptInfo); return false; } return true; } //修改撮合失败信息 private bool UpdateFail(string remarks) { Tdct_Match match =new Tdct_Match(); if (!daMatch.UpdateTransferFail(daMatch.MatchId, remarks)) { Alert("修改撮合历史成功时发现失败", daMatch.PromptInfo); return false; } return true; } //解除挂单的代币锁定 public bool UnFreeze() { Tdct_EntrustCollection daEntrustColl =new Tdct_EntrustCollection(); daEntrustColl.ReferenceTransactionFrom(this.Transaction); daEntrustColl.ListWaitRevokeRedundant(daMatch.SellEntrust, daMatch.BuyEntrust); Log.Info("解冻冗余[{0}笔]:", daEntrustColl.Count); if (daEntrustColl.Count <=0) { return true; } foreach (Tdct_Entrust item in daEntrustColl) { Log.Info("[{0}]解冻就绪", item.Id); decimal amount=item.EAmount - item.TAmount; Currency currency =new Currency(CurrencyType.RMB, amount); Log.Info("[{0}]委托剩余解冻{1}金额", item.Id, amount); UserPurse userPurse = UserPurseProvider.Get(item.UserId, item.TCurrency); userPurse.ReferenceTransactionFrom(this.Transaction); if (!userPurse.UnFreeze(item.FreezeId, currency)) { Alert(userPurse.PromptInfo); Log.Info("[{0}]解冻失败:{1}", item.Id, userPurse.PromptInfo.MessageStack); return false; } Log.Info("[{0}]解冻成功", item.Id); } return true; } //同步代币动态信息 private bool SyncDynamice() { Tdct_Daily_Dynamics daDynamice =new Tdct_Daily_Dynamics(); daDynamice.ReferenceTransactionFrom(this.Transaction); if (daDynamice.SelectByDate(daMatch.SellCurrency, daMatch.BuyCurrency)) { if (!daDynamice.UpdateNewSycn(daDynamice.Id, daMatch.Price, daMatch.BuyCount)) { Alert("同步每日动态失败"); return false; } return true; } daDynamice.BuyCurrencyId = daMatch.BuyCurrency; daDynamice.SellCurrencyId = daMatch.SellCurrency; daDynamice.LastTime = DateTime.Now; daDynamice.MaxPrice = daMatch.Price; daDynamice.MinPrice = daMatch.Price; daDynamice.NewPrice = daMatch.Price; daDynamice.StartPrice = daMatch.Price; daDynamice.TCount = daMatch.BuyCount; if (!daDynamice.Insert()) { Alert("保存每日动态数据失败!", daDynamice.PromptInfo); return false; } return true; } } }
注册有任何问题请添加 微信:MVIP619 拉你进入群
打开微信扫一扫
添加客服
进入交流群
发表评论