public CashOrderDTO ToDTO(CashOrderEntity entity) { CashOrderDTO dto = new CashOrderDTO(); dto.BuyId = entity.BuyId; dto.SellId = entity.SellId; dto.BuyUserId = entity.BuyUserId; dto.SellUserId = entity.SellUserId; dto.BuyerCode = entity.BuyOrder.Buyer.Mobile; dto.SellerCode = entity.SellOrder.Seller.Mobile; dto.OrderCode = entity.OrderCode; dto.Number = entity.Number; dto.Price = entity.Price; dto.Amount = entity.Amount; dto.PayStateType = entity.PayStateType; dto.PayStateName = entity.PayStateType.GetEnumName <CashOrderPayStateEnums>(); dto.PayTime = entity.PayTime; dto.ConfirmStateType = entity.ConfirmStateType; dto.ConfirmStateName = entity.ConfirmStateType.GetEnumName <CashOrderConfirmStateEnums>(); dto.ConfirmTime = entity.ConfirmTime; dto.StateType = entity.StateType; dto.StateName = entity.StateType.GetEnumName <CashOrderStateEnums>(); return(dto); }
public async Task <long> AddAsync(long buyId, long sellId, long buyUserId, long sellUserId, string orderCode, int number, decimal price, decimal amount, int payStateType, int confirmStateType, int stateType) { using (MyDbContext dbc = new MyDbContext()) { CashOrderEntity log = new CashOrderEntity(); log.BuyId = buyId; log.SellId = sellId; log.BuyUserId = buyUserId; log.SellUserId = sellUserId; log.OrderCode = orderCode; log.Number = number; log.Price = price; log.Amount = amount; log.PayStateType = payStateType; log.ConfirmStateType = confirmStateType; log.StateType = stateType; dbc.CashOrders.Add(log); await dbc.SaveChangesAsync(); return(log.Id); } }
public async Task <long> BuyFromAsync(long userId, long buyerUid, long sellId, int buyNum) { decimal balanceAmount = 0, payAmount = 0; using (MyDbContext dbc = new MyDbContext()) { var sellEntity = await dbc.GetAll <CashSellEntity>().SingleOrDefaultAsync(w => w.Id == sellId); if (sellEntity == null) { return(-1); } if (sellEntity.Seller.Id == userId) { return(-8); //不能买入自己挂卖的订单 } if (sellEntity.BalanceNum < buyNum) { return(-2);//买入数量不能大于交易数量 } payAmount = sellEntity.Price * buyNum; //获取商城账户余额 var shopBalance = await shopApiService.GetBalanceAsync(buyerUid); if (shopBalance < payAmount) { return(-5);//账户余额不足 } using (var scope = dbc.Database.BeginTransaction()) { try { #region 买入订单、交易订单 string orderCode = Common.CommonHelper.CreateNo(); CashBuyEntity buy = new CashBuyEntity(); buy.UserId = userId; buy.OrderCode = orderCode; buy.Number = buyNum; buy.Price = sellEntity.Price; buy.Amount = buyNum * sellEntity.Price; buy.Charge = 0; buy.BalanceNum = buyNum; buy.StateType = 1; buy.BuyType = 0;//BuyType 0:买入,1:求购 buy.CurrencyType = sellEntity.CurrencyType; dbc.CashBuys.Add(buy); await dbc.SaveChangesAsync(); CashOrderEntity order = new CashOrderEntity(); order.BuyId = buy.Id; order.SellId = sellId; order.BuyUserId = userId; order.SellUserId = sellEntity.UserId; order.OrderCode = orderCode; order.Number = buyNum; order.Price = sellEntity.Price; order.Amount = buyNum * sellEntity.Price; order.PayStateType = 1; order.PayTime = DateTime.Now; order.ConfirmStateType = 1; order.ConfirmTime = DateTime.Now; order.StateType = 1; dbc.CashOrders.Add(order); sellEntity.BalanceNum -= buyNum; if (sellEntity.BalanceNum == 0) { sellEntity.StateType = (int)CashSellStateEnums.已完成; } #endregion //买方 增加积分 #region 买方 增加积分 var userBuyEntity = await dbc.GetAll <UserEntity>().SingleOrDefaultAsync(w => w.Id == userId); var userSellEntity = await dbc.GetAll <UserEntity>().AsNoTracking().SingleOrDefaultAsync(w => w.Id == sellEntity.UserId); if (sellEntity.CurrencyType == 1)//A积分 { userBuyEntity.BonusAmount += buyNum; balanceAmount = userBuyEntity.BonusAmount; } else if (sellEntity.CurrencyType == 2)//B积分 { userBuyEntity.Amount += buyNum; balanceAmount = userBuyEntity.Amount; } else { scope.Rollback(); return(-4); } int JournalTypeId = 1; await journalService.AddAsync(userId, buyNum, JournalTypeId, sellEntity.CurrencyType, balanceAmount, "交易买入", 1); #endregion await dbc.SaveChangesAsync(); //买方扣除账户余额 //修改商城账户余额,type 0为减少,1为增加 var payResult = await shopApiService.SetBalanceAsync(buyerUid, 0, payAmount, sellEntity.CurrencyType); if (!payResult) { return(-6);//支付出错 } //卖方增加账户余额 //修改商城账户余额,type 0为减少,1为增加 var getResult = await shopApiService.SetBalanceAsync(userSellEntity.ShopUID, 1, payAmount, sellEntity.CurrencyType); if (!getResult) { return(-7);//卖方账户增加出错 } scope.Commit(); return(order.Id); } catch (Exception ex) { scope.Rollback(); return(-3); } } } }
/// <summary> /// 出售给买方 /// </summary> /// <param name="userId"></param> /// <param name="buyId"></param> /// <param name="sellNum"></param> /// <returns></returns> public async Task <long> SellToAsync(long userId, long buyId, int sellNum) { decimal balanceAmount = 0, payAmount = 0; using (MyDbContext dbc = new MyDbContext()) { var buyEntity = await dbc.GetAll <CashBuyEntity>().SingleOrDefaultAsync(w => w.Id == buyId); if (buyEntity == null) { return(-1); } if (buyEntity.Buyer.Id == userId) { return(-8); //不能出售给自己挂买的订单 } if (buyEntity.BalanceNum < sellNum) { return(-2);//出售数量不能大于交易数量 } using (var scope = dbc.Database.BeginTransaction()) { try { string orderCode = Common.CommonHelper.CreateNo(); CashSellEntity sell = new CashSellEntity(); sell.UserId = userId; sell.OrderCode = orderCode; sell.Number = sellNum; sell.Price = buyEntity.Price; sell.Amount = sellNum * buyEntity.Price; sell.Charge = 0; sell.BalanceNum = 0; sell.StateType = 1; sell.SellType = 0; //SellType 0:卖出,1:挂卖 sell.CurrencyType = buyEntity.CurrencyType; dbc.CashSells.Add(sell); await dbc.SaveChangesAsync(); CashOrderEntity order = new CashOrderEntity(); order.BuyId = buyId; order.SellId = sell.Id; order.BuyUserId = buyEntity.UserId; order.SellUserId = userId; order.OrderCode = orderCode; order.Number = sellNum; order.Price = buyEntity.Price; order.Amount = sellNum * buyEntity.Price; order.PayStateType = 1; order.PayTime = DateTime.Now; order.ConfirmStateType = 1; order.ConfirmTime = DateTime.Now; order.StateType = 1; dbc.CashOrders.Add(order); buyEntity.BalanceNum -= sellNum; if (buyEntity.BalanceNum == 0) { buyEntity.StateType = 1; } //卖方 扣除积分 var userSellEntity = await dbc.GetAll <UserEntity>().SingleOrDefaultAsync(w => w.Id == userId); if (buyEntity.CurrencyType == 1)//A积分 { userSellEntity.BonusAmount -= sellNum; balanceAmount = userSellEntity.BonusAmount; } else if (buyEntity.CurrencyType == 2)//B积分 { userSellEntity.Amount -= sellNum; balanceAmount = userSellEntity.Amount; } else { scope.Rollback(); return(-4); } int JournalTypeId = (int)JournalTypeEnum.交易卖出; await journalService.AddAsync(userSellEntity.Id, sellNum, JournalTypeId, buyEntity.CurrencyType, balanceAmount, "交易卖出", 0); //买方 增加积分 var userBuyEntity = await dbc.GetAll <UserEntity>().SingleOrDefaultAsync(w => w.Id == buyEntity.UserId); if (buyEntity.CurrencyType == 1)//A积分 { userBuyEntity.BonusAmount += sellNum; } else if (buyEntity.CurrencyType == 2)//B积分 { userBuyEntity.Amount += sellNum; } else { scope.Rollback(); return(-4); } JournalTypeId = (int)JournalTypeEnum.交易买入; await journalService.AddAsync(userId, sellNum, JournalTypeId, buyEntity.CurrencyType, balanceAmount, "交易买入", 1); //卖方增加账户余额 //修改商城账户余额,type 0为减少,1为增加 payAmount = buyEntity.Price * sellNum; var getResult = await shopApiService.SetBalanceAsync(userSellEntity.ShopUID, 1, payAmount, buyEntity.CurrencyType); if (!getResult) { return(-7);//卖方账户增加出错 } await dbc.SaveChangesAsync(); scope.Commit(); return(sell.Id); } catch (Exception ex) { scope.Rollback(); return(-3); } } } }