/// <summary> /// 创建用户资产账户 /// </summary> /// <param name="userId">会员Id</param> /// <param name="config">The configuration.</param> public ServiceResult CreateAccount(long userId, MoneyTypeConfig config) { var result = ServiceResult.Success; if (config == null) { return(ServiceResult.FailedWithMessage("货币类型不存在")); } if (config.Id.IsNull()) { return(ServiceResult.FailedWithMessage("货币类型不合法")); } if (config.Status != Status.Normal) { return(ServiceResult.FailedWithMessage("货币类型状态不正常,不能添加")); } var account = new Account { UserId = userId, MoneyTypeId = config.Id }; account.Token = GetToken(userId, config); Add(account); return(result); }
/// <summary> /// 减少账户金额 /// </summary> /// <param name="user">用户</param> /// <param name="typeConfig"></param> /// <param name="amount"></param> /// <param name="Intro"></param> public ServiceResult Reduce(User user, MoneyTypeConfig typeConfig, decimal amount, string Intro) { var result = ServiceResult.Success; if (user == null) { return(ServiceResult.FailedWithMessage("操作用户不存在")); } if (amount <= 0) { return(ServiceResult.FailedWithMessage("金额不能为0")); } var userAcount = Resolve <IAccountService>().GetAccount(user.Id, typeConfig.Id); var account = Resolve <IAccountService>().GetAccount(user.Id, typeConfig.Id); if (account == null) { return(ServiceResult.FailedWithMessage("操作账户不存在")); } if (typeConfig.Status != Status.Normal) { return(ServiceResult.FailedWithMessage("操作币种状态不正常")); } if (userAcount.Amount < amount) { return(ServiceResult.FailedWithMessage("账户余额不足,扣除失败")); } userAcount.Amount = userAcount.Amount - amount; var userBill = CreateBill(userAcount, -amount, BillActionType.SystemReduce, Intro); var context = Repository <IBillRepository>().RepositoryContext; context.BeginTransaction(); try { Resolve <IAccountService>().Update(userAcount); Add(userBill); context.SaveChanges(); context.CommitTransaction(); } catch (Exception ex) { context.RollbackTransaction(); return(ServiceResult.FailedWithMessage("更新失败:" + ex.Message)); } finally { context.DisposeTransaction(); } return(result); }
/// <summary> /// 创建分润参数 /// </summary> /// <param name="shareAamount"></param> /// <param name="triggerUser">触发用户</param> /// <param name="shareUser"></param> /// <param name="moneyType">分润金额到账账户类型</param> /// <param name="config">分润维度配置数据</param> /// <param name="parameter">分润触发通用参数</param> /// <param name="shareLevel"></param> public static FenRunResultParameter Create(decimal shareAamount, User triggerUser, User shareUser, MoneyTypeConfig moneyType, ShareBaseConfig config, TaskParameter parameter, long shareLevel = 0) { parameter.TryGetValue("OrderId", out long orderId); parameter.TryGetValue("OrderSerial", out string orderSerial); var Order = new InvoiceOrder { Id = orderId, Serial = orderSerial, Amount = shareAamount }; //替换分润描述 var summary = config.TemplateRule.LoggerTemplate.Replace("{OrderUserName}", triggerUser.UserName) .Replace("{ShareUserNickName}", triggerUser.Name).Replace("{ShareUserRealName}", triggerUser.Name) .Replace("{GainerUserName}", shareUser.UserName).Replace("{GainerNickName}", shareUser.Name) .Replace("{GainerRealName}", shareUser.Name) .Replace("{OrderSerial}", orderSerial).Replace("{AccountName}", moneyType.Name) .Replace("{ShareUserAmount}", triggerUser.ToString()) .Replace("{DividendAmount}", shareAamount.ToString()); return(new FenRunResultParameter { Amount = shareAamount, ModuleName = "招商奖", //读取配置的的名称,未实现 //分润记录信息 ShareStatus = FenRunStatus.Success, ShareLevel = shareLevel, UserRemark = string.Empty, Summary = summary, ExtraDate = "额外数据", //模块信息 ModuleConfigId = config.Id, //BonusId = config.BonusId, ModuleTypeName = config.GetType().Name, TriggerType = config.TriggerType, //触发用户 TriggerGradeId = config.ShareUser.ShareUserGradeId, TriggerUserTypeId = config.ShareUser.ShareUserTypeId, TriggerUserId = triggerUser.Id, OrderUserName = triggerUser.GetUserName(), //获得分润的用户 ReceiveUserId = shareUser.Id, ReceiveUserName = shareUser.GetUserName(), //订单信息 Order = Order, //财务信息 MoneyTypeId = moneyType.Id, BillStatus = BillStatus.Success }); }
public void Increase_User_MoneyTypeConfig_Decimal_String_test() { Users.Entities.User user = null; MoneyTypeConfig typeConfig = null; //var amount = 0; //var Intro = ""; //var result = Service<IBillService>().Increase(user, typeConfig, amount, Intro); //Assert.NotNull(result); }
private string ReplaceTemplate(string template, User shareUser, MoneyTypeConfig ruleMoneyType, decimal ruleAmount) { return(template.Replace("{OrderUserName}", base.OrderUser.GetUserName()) .Replace("{ShareUserName}", shareUser.GetUserName()) .Replace("{AccountName}", ruleMoneyType.Name) .Replace("{OrderId}", ShareTrade.EntityId) .Replace("{OrderPrice}", base.ShareTrade.Amount.ToString("F2")) .Replace("{ShareAmount}", ruleAmount.ToString("F2"))); }
public void Reduce_User_MoneyTypeConfig_Decimal_String_test() { Users.Entities.User user = null; MoneyTypeConfig typeConfig = null; var amount = 0; var Intro = ""; var result = Resolve <IBillService>().Reduce(user, typeConfig, amount, Intro); Assert.NotNull(result); }
/// <summary> /// 冻结账户金额 /// </summary> /// <param name="user">用户</param> /// <param name="typeConfig"></param> /// <param name="amount"></param> /// <param name="Intro"></param> public ServiceResult Treeze(User user, MoneyTypeConfig typeConfig, decimal amount, string Intro) { var result = ServiceResult.Success; if (user == null) { return(ServiceResult.Failure("操作用户不存在")); } var userAcount = Resolve <IAccountService>() .GetSingle(r => r.UserId == user.Id && r.MoneyTypeId == typeConfig.Id); if (userAcount == null) { return(ServiceResult.Failure("操作账户不存在")); } if (typeConfig.Status != Status.Normal) { return(ServiceResult.Failure("操作币种状态不正常")); } if (userAcount.Amount < amount) { return(ServiceResult.Failure("冻结金额不足")); } if (amount <= 0) { return(ServiceResult.Failure("冻结金额不能为0")); } userAcount.Amount = userAcount.Amount - amount; userAcount.FreezeAmount = userAcount.FreezeAmount + amount; var userBill = CreateBill(userAcount, -amount, BillActionType.Treeze, Intro); var context = Repository <IBillRepository>().RepositoryContext; context.BeginTransaction(); try { Resolve <IAccountService>().Update(userAcount); Add(userBill); context.SaveChanges(); context.CommitTransaction(); } catch (Exception ex) { context.RollbackTransaction(); return(ServiceResult.Failure("更新失败:" + ex.Message)); } finally { context.DisposeTransaction(); } return(result); }
/// <summary> /// 冻结单条记录,没有使用事物 /// </summary> /// <param name="user">用户</param> /// <param name="typeConfig"></param> /// <param name="amount"></param> /// <param name="Intro"></param> public ServiceResult TreezeSingle(User user, MoneyTypeConfig typeConfig, decimal amount, string Intro) { var result = ServiceResult.Success; if (user == null) { return(ServiceResult.Failure("操作用户不存在")); } var userAcount = Resolve <IAccountService>() .GetSingle(r => r.UserId == user.Id && r.MoneyTypeId == typeConfig.Id); if (userAcount == null) { return(ServiceResult.Failure("操作账户不存在")); } if (typeConfig.Status != Status.Normal) { return(ServiceResult.Failure("操作币种状态不正常")); } if (userAcount.Amount < amount) { return(ServiceResult.Failure("冻结金额不足")); } if (amount <= 0) { return(ServiceResult.Failure("冻结金额不能为0")); } userAcount.Amount = userAcount.Amount - amount; userAcount.FreezeAmount = userAcount.FreezeAmount + amount; var userBill = Resolve <IBillService>().CreateBill(userAcount, -amount, BillActionType.Treeze, Intro); if (!Resolve <IAccountService>().Update(userAcount)) { return(ServiceResult.Failure("冻结失败")); } if (!Add(userBill)) { return(ServiceResult.Failure("账单记录添加失败")); } return(result); }
public ServiceResult Transfer(User user, User targetUser, MoneyTypeConfig typeConfig, MoneyTypeConfig targetTypeConfig, decimal amount) { var transferConfigs = Resolve <IAutoConfigService>().GetList <TransferConfig>(); var config = transferConfigs.FirstOrDefault(r => r.OutMoneyTypeId == typeConfig.Id && r.InMoneyTypeId == targetTypeConfig.Id); if (config == null) { return(ServiceResult.Failure("该转账类型不存,请在转账配置中进行设置")); } return(Transfer(user, targetUser, config, amount)); }
public string GetToken(long userId, MoneyTypeConfig config) { if (config == null) { return(null); } var begin = Convert.ToInt32(config.Currency).ToString(); // 3位数 if (config.Currency == Currency.Cny) { begin = RandomHelper.Number(11, 99) + "0"; } if (begin.Length > 3) { begin = begin.Substring(0, 3); } if (begin.Length == 1) { begin += RandomHelper.Number(10, 99).ToString(); } if (begin.Length == 2) { begin += RandomHelper.Number(0, 9).ToString(); } var lastMoneyId = config.Id.ToString().Replace("-", ""); var userIdToken = userId.ToString().ToMd5HashString(); var middle = RandomHelper.RandomString(19); // 25位数 var token = begin + middle + lastMoneyId.Substring(3, 2).ToLower() + userIdToken.Substring(5, 1).ToUpper() + RandomHelper.RandomString(2) + lastMoneyId.Substring(10, 2).ToUpper() + RandomHelper.RandomString(2) + userIdToken.Substring(10, 1).ToLower() + RandomHelper.RandomString(2); return(token); }
/// <summary> /// </summary> /// <param name="user">用户</param> /// <param name="typeConfig"></param> /// <param name="amount"></param> /// <param name="Intro"></param> public ServiceResult Increase(User user, MoneyTypeConfig typeConfig, decimal amount, string Intro) { if (user == null) { return(ServiceResult.Failure("操作用户不存在")); } var result = ServiceResult.Success; var userAcount = Resolve <IAccountService>().GetAccount(user.Id, typeConfig.Id); if (userAcount == null) { Resolve <IAccountService>().CreateAccount(user.Id, typeConfig); } userAcount = Resolve <IAccountService>().GetAccount(user.Id, typeConfig.Id); var account = Resolve <IAccountService>().GetAccount(user.Id, typeConfig.Id); if (account == null) { return(ServiceResult.Failure("操作账户不存在")); } if (typeConfig.Status != Status.Normal) { return(ServiceResult.Failure("操作币种状态不正常")); } if (amount <= 0) { return(ServiceResult.Failure("金额不能为0")); } //string Intro = $@"管理员增加{user.GetUserName()}用户的{userAcount.Currency.GetDisplayName()},增加金额为{amount}"; userAcount.Amount = userAcount.Amount + amount; userAcount.HistoryAmount = userAcount.HistoryAmount + amount; var userBill = CreateBill(userAcount, amount, BillActionType.PeopleIncrease, Intro); var context = Repository <IBillRepository>().RepositoryContext; context.BeginTransaction(); try { Resolve <IAccountService>().Update(userAcount); Add(userBill); // 如果类型为成长值,则添加成长值队列 if (typeConfig.Currency == Currency.UpgradePoints) { var taskQueue = new TaskQueue { UserId = user.Id, ModuleId = TaskQueueModuleId.UserUpgradeByUpgradePoints // 自动升级维度的guid,固定不变 }; Resolve <ITaskQueueService>().Add(taskQueue); } context.SaveChanges(); context.CommitTransaction(); } catch (Exception ex) { context.RollbackTransaction(); return(ServiceResult.Failure("更新失败:" + ex.Message)); } finally { context.DisposeTransaction(); } return(result); }
public void SetDefault() { var list = Ioc.Resolve <IAutoConfigService>().GetList <PriceStyleConfig>(); var moneyTypelist = Ioc.Resolve <IAutoConfigService>().GetList <MoneyTypeConfig>(); if (moneyTypelist.Count == 0) { new MoneyTypeConfig().SetDefault(); } if (list.Count == 0) { var configs = new List <PriceStyleConfig>(); var config = new PriceStyleConfig(); MoneyTypeConfig mt = null; moneyTypelist = Ioc.Resolve <IAutoConfigService>().GetList <MoneyTypeConfig>(); foreach (PriceStyle item in Enum.GetValues(typeof(PriceStyle))) { if (Convert.ToInt32(item) >= 0) { config = new PriceStyleConfig { //Intro = item.GetDisplayName(), // MaxCashRate = 1.00m, MinCashRate = 0.50m, Name = item.GetDisplayName() }; if (item == PriceStyle.Customer) { config.Id = Guid.NewGuid(); } else { config.Id = item.GetFieldAttribute().GuidId.ToGuid(); } switch (item) { case PriceStyle.CashAndCredit: //现金+授信 config.PriceStyle = PriceStyle.CashAndCredit; config.Status = Status.Normal; mt = moneyTypelist.FirstOrDefault(p => p.Currency == Currency.Voucher); config.MinCashRate = 0.5m; break; case PriceStyle.CashAndPoint: //现金+积分 config.PriceStyle = PriceStyle.CashAndPoint; mt = moneyTypelist.FirstOrDefault(p => p.Currency == Currency.Point); config.MinCashRate = 0.5m; break; case PriceStyle.CashAndVirtual: //现金+虚拟币 config.PriceStyle = PriceStyle.CashAndVirtual; mt = moneyTypelist.FirstOrDefault(p => p.Currency == Currency.Virtual); config.MinCashRate = 0.5m; break; case PriceStyle.CashProduct: //全部由现金购买 config.MinCashRate = 1; config.PriceStyle = PriceStyle.CashProduct; //全部现金购买的商品 MoneyTypeId 为空 config.SortOrder = 1; config.MinCashRate = 1; mt = moneyTypelist.FirstOrDefault(p => p.Currency == Currency.Cny); break; case PriceStyle.CreditProduct: //全部由授信购买 config.PriceStyle = PriceStyle.CreditProduct; config.MinCashRate = 0; config.Status = Status.Freeze; mt = moneyTypelist.FirstOrDefault(p => p.Currency == Currency.Credit); break; case PriceStyle.Customer: config.PriceStyle = PriceStyle.Customer; mt = moneyTypelist.FirstOrDefault(p => p.Currency == Currency.Custom); break; case PriceStyle.PointProduct: //积分购买 config.PriceStyle = PriceStyle.PointProduct; mt = moneyTypelist.FirstOrDefault(p => p.Currency == Currency.Point); config.MinCashRate = 0; break; case PriceStyle.VirtualProduct: //全部由虚拟币购买 mt = moneyTypelist.FirstOrDefault(p => p.Currency == Currency.Virtual); config.PriceStyle = PriceStyle.VirtualProduct; config.MinCashRate = 0; break; case PriceStyle.ShopAmount: // 消费额 mt = moneyTypelist.FirstOrDefault(p => p.Currency == Currency.Withdrawal); config.PriceStyle = PriceStyle.ShopAmount; config.Status = Status.Normal; config.MinCashRate = 0; break; } if (mt == null) { continue; } config.MoneyTypeId = mt.Id; if (Convert.ToInt32(item) > 1000) { config.Status = Status.Freeze; } configs.Add(config); } } var autoConfig = new AutoConfig { Type = config.GetType().FullName, LastUpdated = DateTime.Now, Value = JsonConvert.SerializeObject(configs) }; Ioc.Resolve <IAutoConfigService>().AddOrUpdate(autoConfig); } }
/// <summary> /// 抽奖所中 /// 如果抽中储值1,优惠券4,打折券2,兑换券3则直接存储在用户账户,同时记录到中奖记录表 /// 需要返回一个中奖项,用户剩余可抽奖次数 /// </summary> /// <param name="drawInput"></param> /// <returns></returns> public ServiceResult Draw(DrawInput drawInput) { if (drawInput.DrawCount <= 0) { return(ServiceResult.FailedWithMessage("抽奖次数已用完!")); } if (drawInput.Id == null) { return(ServiceResult.FailedWithMessage("获取数据为空!")); } var model = GetSingle(ObjectId.Parse(drawInput.Id), drawInput.Key); if (model == null) { return(ServiceResult.FailedWithMessage("获取数据为空!")); } var list = model.Awards; var rateDic = new Dictionary <Guid, int>(); list.ForEach(a => { if (a.Count > 0) { rateDic.Add(a.AwardId, (int)a.Rate * 100); } }); var resultsDic = Lottery(rateDic); //获取抽奖结果 var results = ""; foreach (var d in resultsDic) { results = d.Key.ToString(); } if (!string.IsNullOrEmpty(results)) { var winList = model.Awards.Where(a => a.AwardId == results.ToGuid()); var awardWin = winList.FirstOrDefault(u => u.AwardId == results.ToGuid()); var updateResult = true; //中奖后要减去对应的奖品数量 if (awardWin.Type != HudongAwardType.None) //如果中奖项为:未中奖,则不用更改数据 { var awardList = new List <HudongAward>(); foreach (var award in model.Awards) { var awardModel = new HudongAward(); if (award.AwardId == results.ToGuid()) { awardModel.Count = award.Count - 1; } else { awardModel.Count = award.Count; } awardModel.AwardId = award.AwardId; awardModel.img = award.img; awardModel.Grade = award.Grade; awardModel.Rate = award.Rate; awardModel.Type = award.Type; awardModel.worth = award.worth; awardList.Add(awardModel); } model.Awards = awardList; updateResult = Update(model); } //更改中奖数量 if (updateResult) { //根据中奖的awardID查询对应model var drawType = awardWin.Type.Value(); var intro = awardWin.Intro.IsNullOrEmpty() ? "" : awardWin.Intro; var worth = awardWin.worth; var count = awardWin.Count; //每次抽奖后需要一条抽奖记录 var recordModel = new HudongRecord(); recordModel.UserId = drawInput.UserId; recordModel.HuDongType = HuDongEnums.BigWheel; recordModel.Intro = intro; recordModel.HuDongStatus = AwardStatus.NotExchange; recordModel.Grade = awardWin.Grade; recordModel.HuDongActivityType = awardWin.Type; var moneyTypes = new MoneyTypeConfig(); var moneyType = MoneyTypeConfig.Credit; if (drawType == HudongAwardType.StoreValue.Value()) //储值对应人民币 { moneyTypes.Id = MoneyTypeConfig.CNY; recordModel.HuDongStatus = AwardStatus.Exchage; } else if (drawType == HudongAwardType.Discount.Value()) //赠送消费额 { moneyTypes.Id = MoneyTypeConfig.ShopAmount; recordModel.HuDongStatus = AwardStatus.Exchage; } else if (drawType == HudongAwardType.Integral.Value()) //积分 { moneyTypes.Id = MoneyTypeConfig.Point; recordModel.HuDongStatus = AwardStatus.Exchage; } else if (drawType == HudongAwardType.Coupon.Value()) //优惠券 { moneyTypes.Id = MoneyTypeConfig.Preferential; recordModel.HuDongStatus = AwardStatus.Exchage; } var recordResult = Resolve <IHudongRecordService>().AddRecord(recordModel); //抽奖记录 if (recordResult.Succeeded && awardWin.Type != HudongAwardType.None && recordModel.HuDongStatus == AwardStatus.Exchage) { var user = Resolve <IUserService>().GetSingle(drawInput.UserId); Resolve <IBillService>().Increase(user, moneyTypes, worth, "大转盘抽奖中奖获得"); } } } return(ServiceResult.SuccessWithObject(results)); }