Esempio n. 1
0
        public void DistributeRewards(FiiiPayRewardMessage message, int fiiicoinId)
        {
            MerchantAccountDAC maDAC = new MerchantAccountDAC();
            var merchant             = maDAC.GetByUsername(message.Account);

            if (merchant == null)
            {
                throw new CommonException(10000, $"无效的商家名:{message.Account}");
            }

            InviteRecord invitor = GetInvitor(merchant.Id);

            if (invitor == null)
            {
                throw new CommonException(10000, "没有邀请人");
            }

            var invitedList = new InviteRecordDAC().GetFiiiPosRecordsByInvitorId(invitor.InviterAccountId, InviteType.Fiiipos);
            var rewardRate  = GetRewardPercentage(invitedList == null ? 0 : invitedList.Count);

            if (rewardRate == 0)
            {
                throw new CommonException(10000, "没有达到奖励条件");
            }

            decimal t            = (decimal)Math.Pow(10, -8);
            long    nTotalReward = (long)Math.Floor(message.Reward * rewardRate);

            if (nTotalReward == 0)
            {
                throw new CommonException(10000, "奖励金额为0");
            }
            decimal  rewardAmount = nTotalReward * t;
            DateTime dtNow        = DateTime.UtcNow;

            if (message.CurrentDate > 0)
            {
                dtNow = new DateTime(1970, 1, 1, 0, 0, 0, DateTimeKind.Utc).AddMilliseconds(message.CurrentDate);
            }
            var distributeRecord = new RewardDistributeRecords
            {
                UserAccountId     = invitor.InviterAccountId,
                MerchantAccountId = merchant.Id,
                SN             = message.SN,
                OriginalReward = message.Reward,
                Percentage     = rewardRate,
                ActualReward   = rewardAmount,
                Timestamp      = dtNow
            };

            long    profitId;
            decimal oldBalance    = 0;
            var     invitorWallet = uwDAC.GetByAccountId(invitor.InviterAccountId, fiiicoinId);

            using (var scope = new TransactionScope(TransactionScopeOption.Required, new TimeSpan(0, 0, 1, 30)))
            {
                if (invitorWallet == null)
                {
                    invitorWallet = CreateWallet(invitor.InviterAccountId, fiiicoinId, rewardAmount);
                }
                else
                {
                    oldBalance = invitorWallet.Balance;
                    uwDAC.Increase(invitorWallet.Id, rewardAmount);
                }

                var profitDetail = new ProfitDetail
                {
                    InvitationId = invitor.Id,
                    CryptoAmount = rewardAmount,
                    AccountId    = invitor.InviterAccountId,
                    Status       = InviteStatusType.IssuedActive,
                    Type         = ProfitType.InvitePiiiPos,
                    OrderNo      = CreateOrderno(),
                    Timestamp    = dtNow,
                    CryptoId     = invitorWallet.CryptoId,
                    CryptoCode   = "FIII"
                };
                profitId = pfDAC.Insert(profitDetail);

                uwsDAC.Insert(new UserWalletStatement
                {
                    WalletId      = invitorWallet.Id,
                    Action        = UserWalletStatementAction.Reward,
                    Amount        = rewardAmount,
                    Balance       = oldBalance + rewardAmount,
                    FrozenAmount  = 0,
                    FrozenBalance = invitorWallet.FrozenBalance,
                    Timestamp     = dtNow
                });

                transDAC.Insert(new UserTransaction
                {
                    AccountId    = invitor.InviterAccountId,
                    Amount       = rewardAmount,
                    CryptoCode   = "FIII",
                    CryptoId     = fiiicoinId,
                    DetailId     = profitId.ToString(),
                    Id           = Guid.NewGuid(),
                    MerchantName = string.Empty,
                    OrderNo      = profitDetail.OrderNo,
                    Status       = 2,
                    Timestamp    = dtNow,
                    Type         = UserTransactionType.Profit
                });

                distributeRecord.ProfitId = profitId;
                rdrDAC.Insert(distributeRecord);

                scope.Complete();
            }

            if (profitId > 0)
            {
                try
                {
                    MessagePushService.PubUserInviteSuccessed(profitId, 0);
                }
                catch (Exception ex)
                {
                    throw new CommonException(10000, ex.Message);
                }
            }
        }
Esempio n. 2
0
        public void DistributeRewards(FiiiPayRewardMessage message, DateTime dt)
        {
            if (fiiicoinId == 0)
            {
                var fiiicoin = new CryptocurrencyDAC().GetByCode("FIII");
                fiiicoinId = fiiicoin.Id;
            }
            MerchantAccountDAC maDAC = new MerchantAccountDAC();
            var merchant             = maDAC.GetByUsername(message.Account);

            if (merchant == null)
            {
                throw new CommonException(10000, $"无效的商家名:{message.Account}");
            }

            InviteRecord invitor = GetInvitor(merchant.Id);

            if (invitor == null)
            {
                throw new CommonException(10000, "没有邀请人");
            }

            var invitedList = new InviteRecordDAC().GetFiiiPosRecordsByInvitorId(invitor.InviterAccountId, InviteType.Fiiipos);
            var rewardRate  = GetRewardPercentage(invitedList == null ? 0 : invitedList.Count);

            if (rewardRate == 0)
            {
                throw new CommonException(10000, "没有达到奖励条件");
            }

            decimal t            = (decimal)Math.Pow(10, -8);
            long    nTotalReward = (long)Math.Floor(message.ActualReward * rewardRate);

            if (nTotalReward == 0)
            {
                throw new CommonException(10000, "奖励金额为0");
            }
            decimal rewardAmount     = nTotalReward * t;
            var     distributeRecord = new RewardDistributeRecords
            {
                UserAccountId     = invitor.InviterAccountId,
                MerchantAccountId = merchant.Id,
                SN             = message.SN,
                OriginalReward = message.ActualReward,
                Percentage     = rewardRate,
                ActualReward   = rewardAmount,
                Timestamp      = dt
            };

            long    profitId      = 0;
            decimal oldBalance    = 0;
            var     invitorWallet = uwDAC.GetByAccountId(invitor.InviterAccountId, fiiicoinId);

            using (var scope = new TransactionScope(TransactionScopeOption.Required, new TimeSpan(0, 0, 1, 30)))
            {
                if (invitorWallet == null)
                {
                    invitorWallet = CreateWallet(invitor.InviterAccountId, fiiicoinId, rewardAmount);
                }
                else
                {
                    oldBalance = invitorWallet.Balance;
                    uwDAC.Increase(invitorWallet.Id, rewardAmount);
                }
                profitId = pfDAC.Insert(new ProfitDetail()
                {
                    InvitationId = invitor.Id,
                    CryptoAmount = rewardAmount,
                    AccountId    = invitor.InviterAccountId,
                    Status       = InviteStatusType.IssuedActive,
                    Type         = ProfitType.InvitePiiiPos,
                    OrderNo      = CreateOrderno(),
                    Timestamp    = dt,
                    CryptoId     = invitorWallet.CryptoId
                });
                uwsDAC.Insert(new UserWalletStatement
                {
                    WalletId      = invitorWallet.Id,
                    Action        = UserWalletStatementAction.Reward,
                    Amount        = rewardAmount,
                    Balance       = oldBalance + rewardAmount,
                    FrozenAmount  = 0,
                    FrozenBalance = invitorWallet.FrozenBalance,
                    Timestamp     = dt,
                    Remark        = "System"
                });

                distributeRecord.ProfitId = profitId;
                rdrDAC.Insert(distributeRecord);

                new TempDataDAC().MessageComplated(message.Id);

                scope.Complete();
            }
        }