예제 #1
0
        /// <summary>
        /// 获取表单
        /// </summary>
        /// <param name="levelId"></param>
        /// <param name="userId"></param>
        /// <returns></returns>
        public UserLevelActiveForm GetActiveForm(long levelId, long userId)
        {
            #region 安全验证

            var userLevel = Resolve <IUserLevelService>().GetSingle(r => r.Id == levelId);
            if (userLevel == null)
            {
                throw new ValidException("用户级别数据不存在");
            }

            if (userLevel.Status == UserLevelStatus.Activated)
            {
                throw new ValidException("用户级别数据已激活");
            }
            var userLevelRecord = Resolve <IUserLevelRecordService>().GetSingle(r => r.LevelId == userLevel.Id);
            if (userLevelRecord == null)
            {
                throw new ValidException("用户级别数据不存在");
            }

            if (!(userLevel.UserId == userId || userLevel.ParentId == userId))
            {
                throw new ValidException("您无权查看,该数据信息");
            }
            var userLevelConfigList = Resolve <IAutoConfigService>().GetList <UserLevelConfig>();
            var userLevelConfig     = userLevelConfigList.FirstOrDefault(r => r.Id == userLevel.ConfigId);
            if (userLevelConfig == null || userLevelConfig.Status != Status.Normal)
            {
                throw new ValidException($"消费套餐不存在");
            }
            var moneyAccount = Resolve <IAccountService>().GetAccount(userId, Currency.Cny);

            if (moneyAccount == null)
            {
                throw new ValidException($"资产账号不存在");
            }
            if (userLevel.Status == UserLevelStatus.Activated)
            {
                throw new ValidException("用户级别数据已激活");
            }

            #endregion 安全验证

            UserLevelActiveForm activeForm = userLevelRecord.MapTo <UserLevelActiveForm>();
            activeForm.LevelName    = $"{userLevelConfig.Name}(金额:{userLevelConfig.Price})";
            activeForm.Mobile       = userLevel.Mobile;
            activeForm.ActiveUserId = userLevel.UserId;
            activeForm.IdCard       = userLevel.IdCard;
            activeForm.Country      = userLevel.Country.GetDisplayName();
            activeForm.Id           = userLevel.Id;

            activeForm.ReductMoney = $"<b style='color:red; font-size:1.2rem'>{userLevelConfig.Price}</b> 现金资产余额:{moneyAccount.Amount}";

            activeForm.Name = userLevel.Name;

            return(activeForm);
        }
예제 #2
0
        /// <summary>
        /// 激活用户
        /// </summary>
        /// <param name="activeForm"></param>
        /// <returns></returns>
        public ServiceResult Active(UserLevelActiveForm activeForm, long userId)
        {
            #region 安全验证

            var user = Resolve <IUserService>().GetNomarlUser(userId);
            if (user == null)
            {
                return(ServiceResult.Failure("您的账号不存在或被冻结"));
            }
            var activeUser = Resolve <IUserService>().GetSingle(r => r.Id == activeForm.ActiveUserId);
            if (activeUser == null)
            {
                return(ServiceResult.Failure("被激活用户不存在"));
            }

            if (activeUser.Status != Status.Deleted)
            {
                return(ServiceResult.Failure("被激活用户状态不正确"));
            }
            var userLevel = Resolve <IUserLevelService>().GetSingle(r => r.Id == activeForm.Id);
            if (userLevel == null)
            {
                return(ServiceResult.Failure("用户级别数据不存在"));
            }
            var userLevelRecord = Resolve <IUserLevelRecordService>().GetSingle(r => r.LevelId == userLevel.Id);
            if (userLevelRecord == null)
            {
                return(ServiceResult.Failure("用户级别数据不存在"));
            }
            if (userLevel.Status == UserLevelStatus.Activated)
            {
                return(ServiceResult.Failure("用户级别数据已激活"));
            }
            if (userLevelRecord.Status == UserLevelStatus.Activated)
            {
                return(ServiceResult.Failure("用户级别记录数据已激活"));
            }
            if (!(userLevel.UserId == userId || userLevel.ParentId == userId))
            {
                return(ServiceResult.Failure("您无权查看,该数据信息"));
            }
            var userLevelConfigList = Resolve <IAutoConfigService>().GetList <UserLevelConfig>();
            var userLevelConfig     = userLevelConfigList.FirstOrDefault(r => r.Id == userLevel.ConfigId);
            if (userLevelConfig == null || userLevelConfig.Status != Status.Normal)
            {
                return(ServiceResult.Failure($"消费套餐不存在"));
            }

            if (userLevelConfig.Price.EqualsDigits(userLevelRecord.Price))
            {
                return(ServiceResult.Failure($"套餐金额校验不通过"));
            }

            // 现金账户
            var cnyAccount = Resolve <IAccountService>().GetAccount(userId, Currency.Cny);

            if (cnyAccount == null)
            {
                return(ServiceResult.Failure($"资产账号不存在"));
            }

            if (userLevelConfig.Price > cnyAccount.Amount)
            {
                return(ServiceResult.Failure($"现金账号余额不足请充值"));
            }

            // 配送的通宝权益账号,和货币类型对应
            var tongBaoAccount = Resolve <IAccountService>()
                                 .GetAccount(activeUser.Id, Guid.Parse("e97ccd1e-1478-49bd-bfc7-e73a5d699756"));
            if (tongBaoAccount == null)
            {
                return(ServiceResult.Failure($"通宝权益账号不存在"));
            }

            if (tongBaoAccount.Amount > 0)
            {
                return(ServiceResult.Failure($"被激活用户财务账号不合理"));
            }

            #endregion 安全验证

            #region 登录用户现金资产减少

            //转账用户金额减少
            cnyAccount.Amount = cnyAccount.Amount - userLevelRecord.Price;
            // 登录用户 人民币减少账单
            var cnyAccountBill = new Bill {
                UserId      = user.Id,
                AfterAmount = cnyAccount.Amount,
                Amount      = userLevelRecord.Price,
                OtherUserId = activeUser.Id,
                Type        = BillActionType.FenRun,
                MoneyTypeId = cnyAccount.MoneyTypeId,
                Flow        = AccountFlow.Spending,
                CreateTime  = DateTime.Now,
                Intro       =
                    $@"{user.GetUserName()}激活{activeUser.GetUserName()}套餐{userLevelConfig.Name},支出金额:{userLevelRecord.Price} "
            };

            #endregion 登录用户现金资产减少

            #region 被激活用户配送通宝

            tongBaoAccount.Amount = userLevelRecord.Price;
            var tongBaoAccountBill = new Bill {
                UserId      = activeUser.Id,
                AfterAmount = tongBaoAccount.Amount,
                Amount      = userLevelRecord.Price,
                OtherUserId = user.Id,
                Type        = BillActionType.FenRun,
                MoneyTypeId = tongBaoAccount.MoneyTypeId,
                Flow        = AccountFlow.Income,
                CreateTime  = DateTime.Now,
                Intro       =
                    $@"{user.GetUserName()}激活{activeUser.GetUserName()}套餐{userLevelConfig.Name},增加金额:{userLevelRecord.Price} "
            };

            #endregion 被激活用户配送通宝

            var context = Repository <IBillRepository>().RepositoryContext;
            context.BeginTransaction();
            try {
                // 激活用户状态修改
                activeUser.Status = Status.Normal;
                Resolve <IUserService>().Update(activeUser);

                // 用户级别数据修改
                userLevel.Status = UserLevelStatus.Activated;
                Resolve <IUserLevelService>().Update(userLevel);

                // 用户级别记录数据修改
                userLevelRecord.Status = UserLevelStatus.Activated;
                Resolve <IUserLevelRecordService>().Update(userLevelRecord);

                // 登录用户现金资产减少
                Resolve <IAccountService>().Update(cnyAccount);
                Resolve <IBillService>().Add(cnyAccountBill);

                // 激活用户赠送通宝资产
                Resolve <IAccountService>().Update(tongBaoAccount);
                Resolve <IBillService>().Add(tongBaoAccountBill);

                context.SaveChanges();
                context.CommitTransaction();
            } catch (Exception ex) {
                context.RollbackTransaction();
                return(ServiceResult.Failure("激活失败:" + ex.Message));
            } finally {
                context.DisposeTransaction();
            }
            return(ServiceResult.Success);;
        }