/// <summary> /// 发起转账单 /// </summary> /// <param name="transferBillType">转账单类型</param> /// <param name="status"></param> /// <param name="fromMemberUserName">发起转账用户Id</param> /// <param name="toMemberUserName">购买人用户名</param> /// <param name="amount">转账金额</param> public void Initiate(TransferBillType transferBillType, TransferBillStatus status, string fromMemberUserName, string toMemberUserName, decimal amount) { var session = DataBase.Session; ITransaction iTransaction = session.BeginTransaction(IsolationLevel.ReadCommitted); try { //创建转账单 var transferBill = new TransferBill { FromMemberUserName = fromMemberUserName, FromAccountType = transferBillType.FromAccount(), ToMemberUserName = toMemberUserName, ToAccountType = transferBillType.ToAccount(), Amount = amount, Status = status, TransferBillType = transferBillType, ExpiredAt = DateTime.Now }; DataBase.Save(transferBill, session); //扣除出账账户余额 var fromMember = GetFromMember(transferBill, session); if (fromMember != null) { DataBase.Update(fromMember, session); } //完成状态的交易单增加入账账户余额 if (transferBill.Status == TransferBillStatus.Completed) { if (transferBill.FromMemberUserName == transferBill.ToMemberUserName) { var propertyName = $"{transferBill.ToAccountType.ToString()}Balance"; var balance = fromMember.GetPropertyValue(propertyName).To <decimal>(); balance += transferBill.Amount; fromMember.SetPropertyValue(propertyName, balance); DataBase.Update(fromMember, session); } else { var toMember = GetToMember(transferBill, session); DataBase.Update(toMember, session); } } iTransaction.Commit(); } catch (Exception ex) { iTransaction.Rollback(); MiddleTier.LogManager.Error(ex); if (ex is PlatformException platformException) { throw platformException; } throw new PlatformException(ErrorCode.SystemError); } finally { session.Close(); } }
//保存表单数据 public ActionResult SaveData(TransferBill TransferBill) { //参数对象可以对应接受数据 TransferBill.MakePerson = Session["UserName"].ToString(); //保存制单人 string result = TransferBillService.SaveData(TransferBill); //保存数据 return(Content(result.ToString())); }
private string AddTransferBill(DataTable dt) { TransferBill bill = DataTableToEntites.GetEntity <TransferBill>(dt.Rows[0]); bill.Record = HandleRecords(dt); TransferBillService Service = new TransferBillService(); Service.SaveData(bill); return(bill.BillCode); }
/// <summary> /// 1.买家发起求购 /// </summary> /// <param name="userName"></param> /// <param name="amount"></param> public void MemberToBuy(string userName, decimal amount) { if (amount % 50 > 0 || amount > MiddleTier.BusinessConfig.ToBuyAmountLimit) { throw new PlatformException(ErrorCode.ToBuyAmountLimitError); } var user = DataBase.Get <Member>(e => e.UserName == userName); if (user == null) { throw new PlatformException(ErrorCode.UserNameIsNotExisted); } if (user.Status != MemberStatus.InUse) { throw new PlatformException(ErrorCode.MustBuyMiningEquipment); } var count = DataBase.Count <TransferBill>(e => e.ToMemberUserName == userName && (e.Status == TransferBillStatus.Original || e.Status == TransferBillStatus.SellerEnsure || e.Status == TransferBillStatus.BuyerEnsure) && e.TransferBillType == TransferBillType.GoldTransfer); if (count >= MiddleTier.BusinessConfig.CurrentNormalToBuyLimitTime && user.MemberType == MemberType.Normal) { throw new PlatformException(ErrorCode.CurrentToBuyTimeLimit); } if (count >= MiddleTier.BusinessConfig.CurrentCallToBuyLimitTime && user.MemberType == MemberType.CallCenter) { throw new PlatformException(ErrorCode.CurrentToBuyTimeLimit); } var existCount = DataBase.Count <TransferBill>(e => e.ToMemberUserName == userName && (e.Status == TransferBillStatus.Original || e.Status == TransferBillStatus.SellerEnsure || e.Status == TransferBillStatus.BuyerEnsure || e.Status == TransferBillStatus.Completed) && e.TransferBillType == TransferBillType.GoldTransfer && e.CreatedAt > DateTime.Now.Date); if ((user.MemberType == MemberType.Normal && existCount >= MiddleTier.BusinessConfig.ToBuyLimitTime) || (user.MemberType == MemberType.CallCenter && existCount >= MiddleTier.BusinessConfig.CallCenterToBuyLimitTime)) { throw new PlatformException(ErrorCode.ToBuyTimeLimit); } //创建转账单 var transferBill = new TransferBill { FromMemberUserName = "", FromAccountType = TransferBillType.GoldTransfer.FromAccount(), ToMemberUserName = userName, ToAccountType = TransferBillType.GoldTransfer.ToAccount(), Amount = amount, Status = TransferBillStatus.Original, TransferBillType = TransferBillType.GoldTransfer, ExpiredAt = DateTime.Now.AddDays(MiddleTier.BusinessConfig.ToBuyExpiredTime), ToBuyAt = DateTime.Now }; DataBase.Save(transferBill); }
//获取表单数据 public ActionResult GetData() { string str = Request.Params["TransferBillId"].ToString(); //如果新单据 没有数据 if (Request["TransferBillId"].ToString() == "") { return(Json(new TransferBill())); //返回一个新建的空对象 } Guid TransferBillId = new Guid(Request["TransferBillId"]); //单据编号 TransferBill bill = TransferBillService.LoadEntities(t => t.Id == TransferBillId).FirstOrDefault(); //获取表单 //return Content(res);//如果是拼接字符串 就用return Content(); 如果是对象 就用 return Json(); return(Json(bill)); }
//获取表单数据 public ActionResult GetData() { string str = Request.Params["TransferBillId"];//单号 //如果新单据 没有数据 if (string.IsNullOrEmpty(str)) { return(Json(new TransferBill()));//返回一个新建的空对象 } //如果有数据 Guid TransferBillId = new Guid(Request["TransferBillId"]); //单据编号 TransferBill bill = TransferBillService.LoadEntities(t => t.Id == TransferBillId).FirstOrDefault(); //获取表单 return(Json(bill)); }
/// <summary> /// 计算求购单的入账金额,免除手续费 /// </summary> /// <param name="transferBill"></param> /// <returns></returns> private decimal GetTransferBillToAmount(TransferBill transferBill) { //Todo 手续费收买家还是卖家 var feePercent = 0.1m; var equipmentCount = DataBase.Count <MemberMiningEquipment>(e => e.MemberUserName == transferBill.FromMemberUserName && e.Type == MiningEquipmentType.Smelter); // if (equipmentCount >= 5) // { // feePercent = 0; // } if (equipmentCount >= 3) { feePercent = 0.05m; } transferBill.ServiceCharge = transferBill.Amount * feePercent; return(transferBill.Amount * (1 - feePercent)); }
private Member GetToMember(TransferBill transferBill, ISession session) { if (transferBill.ToMemberUserName.IsNullOrEmpty()) { return(null); } var toMember = DataBase.Get <Member>(e => e.UserName == transferBill.ToMemberUserName, session); if (toMember == null) { throw new PlatformException(ErrorCode.UserNameIsNotExisted); } var propertyName = $"{transferBill.FromAccountType.ToString()}Balance"; var balance = toMember.GetPropertyValue(propertyName).To <decimal>(); balance += transferBill.Amount; toMember.SetPropertyValue(propertyName, balance); return(toMember); }
//保存表单数据 public ActionResult SaveData() { bool result = true; string res = Request.Params["TransferBill"].ToString(); TransferBill bill = JsonConvert.DeserializeObject <TransferBill>(res); //保存主表数据 if (bill.Id == Guid.Parse("00000000-0000-0000-0000-000000000000")) { bill.Id = Guid.NewGuid(); foreach (var item in bill.Record) { if (item.Id == Guid.Parse("00000000-0000-0000-0000-000000000000")) { item.Id = Guid.NewGuid(); } } TransferBillService.AddEntity(bill); } else { result = TransferBillService.EditEntity(bill); } //删除原子表数据 //List<Record> list = RecordService.LoadEntities(a => a.MainTableId == bill.Id).ToList(); //foreach (Record item in list) //{ // RecordService.DeleteEntity(item); //} //添加子表数据 //foreach (Record record in bill.Record) //{ // if (record.Id == Guid.Parse("00000000-0000-0000-0000-000000000000")) // { // record.Id = Guid.NewGuid();//新生成一个id // record.MainTableId = bill.Id; // RecordService.AddEntity(record); // } //} return(Content(result.ToString())); }
/// <summary> /// 导出入库单 /// </summary> /// <param name="billId"></param> /// <param name="TemplateUrl"></param> /// <returns></returns> private MemoryStream ExportTransferBill(Guid billId, string TemplateUrl) { TransferBill bill = CurrentDBSession.TransferBillDal.LoadEntities(a => a.Id == billId).FirstOrDefault(); if (bill == null) { return(null); } DataTable dt = ExcelHelp.ExcelToDT(TemplateUrl); foreach (var record in bill.Record) { DataRow dr = dt.NewRow(); dr["LBBillCode"] = bill.LBBillCode; dr["LBBillDate"] = bill.LBBillDate; dr["LBFrom"] = bill.LBFrom; dr["LBMoveType"] = bill.LBMoveType; dr["LBRemark"] = bill.LBRemark; dr["MakePerson"] = bill.MakePerson; dr["CreateDate"] = bill.CreateDate; dr["ChargePerson"] = bill.ChargePerson; dr["Warehouse"] = bill.Warehouse; dr["WarehouseId"] = bill.WarehouseId; dr["LoadGoodsType"] = bill.LoadGoodsType; dr["InputType"] = bill.InputType; dr["InputTypeId"] = bill.InputTypeId; dr["BusinessType"] = bill.BusinessType; dr["Remark"] = bill.Remark; dr["Department"] = bill.Department; dr["DepartmentId"] = bill.DepartmentId; dr["Company"] = bill.Company; dr["CompanyId"] = bill.CompanyId; //子表 dr["ItemCode"] = record.ItemCode; dr["ItemLocationId"] = record.ItemLocationId; dr["ItemBatch"] = record.ItemBatch; dr["Count"] = record.Count; dt.Rows.Add(dr); } return(NPIOHelper.RenderToMemory(dt, "sheet1")); }
private Member GetFromMember(TransferBill transferBill, ISession session) { if (transferBill.FromMemberUserName.IsNullOrEmpty()) { return(null); } //1.检查用户 var fromMember = DataBase.Get <Member>(e => e.UserName == transferBill.FromMemberUserName, session); if (fromMember == null) { throw new PlatformException(ErrorCode.UserNameIsNotExisted); } var propertyName = $"{transferBill.FromAccountType.ToString()}Balance"; var balance = fromMember.GetPropertyValue(propertyName).To <decimal>(); if (balance < transferBill.Amount) //出账账户余额不足 { throw new PlatformException(ErrorCode.AccountBalanceNotEnough); } balance -= transferBill.Amount; fromMember.SetPropertyValue(propertyName, balance); return(fromMember); }
/// <summary> /// 激活好友 /// </summary> /// <param name="fromUserName"></param> /// <param name="toUserName"></param> public void ActivePartner(string fromUserName, string toUserName, string name) { var toMember = GetMember(toUserName); if (toMember.Status != MemberStatus.UnActived) { throw new PlatformException(ErrorCode.UserIsActived); } var fromMember = GetMember(fromUserName); var userNames = toMember.Chain.Split(new[] { ChainStr }, StringSplitOptions.RemoveEmptyEntries); if (!userNames.Contains(fromUserName)) { throw new PlatformException(ErrorCode.ErrorInvitRelationship); } if (fromMember.CopperBalance < MiddleTier.BusinessConfig.ActivePartnerNeedAmount) { throw new PlatformException(ErrorCode.AccountBalanceNotEnough); } //2、用户注册后,默认是未激活状态(这个又改回来了),推荐人可以激活他下面的所有用户(往下N代都行),直推从好友列表中激活就可以了。不是直推的让他输入登录名和姓名,匹配上了就可以激活(要判断是否是A下面的用户),激活动作扣除激活人的100$的银,并且往被激活人账户里充100$的银(100这个参数做成可配的)。 冬冬要做个激活的页面。 ISession session = DataBase.Session; ITransaction iTransaction = session.BeginTransaction(IsolationLevel.ReadCommitted); try { fromMember.CopperBalance -= MiddleTier.BusinessConfig.ActivePartnerNeedAmount; toMember.SilverBalance += MiddleTier.BusinessConfig.ActivePartnerNeedAmount; toMember.Status = MemberStatus.Actived; //6、激活成功送钻石一个(给推荐人),每增加一个矿工送一个钻石,选金厂送5个,冶炼厂送10个!钻石以后有用! (这个数字做成可配的) fromMember.DiamondBalance += MiddleTier.BusinessConfig.ActivePartnerRewardDiamondAmount; DataBase.Update(toMember, session); DataBase.Update(fromMember, session); //创建转账单 var transferBill = new TransferBill { FromMemberUserName = fromUserName, FromAccountType = AccountType.Copper, ToMemberUserName = toUserName, ToAccountType = AccountType.Silver, Amount = MiddleTier.BusinessConfig.ActivePartnerNeedAmount, Status = TransferBillStatus.Completed, TransferBillType = TransferBillType.Active, ExpiredAt = DateTime.Now }; DataBase.Save(transferBill, session); var ffMemberIncomeRecord = new MemberIncomeRecord { Amount = MiddleTier.BusinessConfig.ActivePartnerRewardDiamondAmount, MemberUserName = fromMember.UserName, DiamondAmount = MiddleTier.BusinessConfig.ActivePartnerRewardDiamondAmount, CopperAmount = -MiddleTier.BusinessConfig.ActivePartnerNeedAmount, Type = MemberIncomeType.InviteActive, ReferenceMemberUsername = toUserName }; // var fffMemberIncomeRecord = new MemberIncomeRecord // { // Amount = MiddleTier.BusinessConfig.ActivePartnerNeedAmount, // MemberUserName = toUserName, // GoldAmount = MiddleTier.BusinessConfig.ActivePartnerNeedAmount, // Type = MemberIncomeType.InviteActiveCost, // ReferenceMemberUsername = fromUserName // }; DataBase.Save(ffMemberIncomeRecord, session); // DataBase.Save(fffMemberIncomeRecord, session); if (toMember.RecommendUserName.IsNotNullOrEmpty()) { var recommendCount = DataBase.Count <Member>(e => e.RecommendUserName == toMember.RecommendUserName && new [] { MemberStatus.Actived, MemberStatus.InUse, MemberStatus.SealUp }.Contains(e.Status), session); var slagEquipmentCount = DataBase.Count <SlagRefiningEquipment>(e => e.MemberUserName == toMember.RecommendUserName && e.SourceType == SlagRefiningEquipmentSourceType.InvitingAwards, session); if (slagEquipmentCount < MiddleTier.BusinessConfig.SlagRefiningEquipmentCountLimit && (int)(recommendCount / MiddleTier.BusinessConfig.SlagRefiningEquipmentRewardInviteMemberCount) > slagEquipmentCount) //每个推荐人精炼设备上限&&达到发放奖励的推荐人数 { //矿渣提炼炉上限是10台,每推荐10名合伙人送一台,每台可精炼矿渣总量的0.3%黄金,前两台0.5%,每台使用期限一年! DataBase.Save(new SlagRefiningEquipment { MemberUserName = toMember.RecommendUserName, LastRefiningAt = DateTime.Now, Percentage = slagEquipmentCount < MiddleTier.BusinessConfig.SlagRefiningEquipmentTopCount ? MiddleTier.BusinessConfig.SlagRefiningEquipmentTopPercentage : MiddleTier.BusinessConfig.SlagRefiningEquipmentNormalPercentage, SourceType = SlagRefiningEquipmentSourceType.InvitingAwards, ValidityTerm = DateTime.Now.AddDays(MiddleTier.BusinessConfig.SlagRefiningEquipmentUseTermDay) }, session); } } iTransaction.Commit(); } catch (Exception ex) { iTransaction.Rollback(); MiddleTier.LogManager.Error(ex); if (ex is PlatformException platformException) { throw platformException; } throw new PlatformException(ErrorCode.SystemError); } finally { session.Close(); } }
/// <summary> /// 用户精炼 /// </summary> /// <param name="userName"></param> /// <param name="id"></param> public void MemberRefining(string userName, long id) { var member = MiddleTier.MemberManager.GetMember(userName); if (member == null) { throw new PlatformException(ErrorCode.UserNameIsNotExisted); } var slagRefiningEquipment = DataBase.Get <SlagRefiningEquipment>(e => e.Id == id && e.MemberUserName == userName); if (slagRefiningEquipment == null) { throw new PlatformException(ErrorCode.ErrorId); } if (slagRefiningEquipment.LastRefiningAt.Date >= DateTime.Now.Date) { throw new PlatformException(ErrorCode.RefiningTooBusy); } ISession session = DataBase.Session; ITransaction iTransaction = session.BeginTransaction(IsolationLevel.ReadCommitted); try { //比如矿渣余额1000 黄金账户增加金额=1000*精炼百分比(0.3)*精炼数量 //后台设个系数吧,矿渣和黄金的提炼比例。 默认1: 1。比如提炼出1G黄金就扣1G矿渣。 如果1: 2,就是提炼1G黄金,扣2G矿渣。 var amount = member.SlagBalance * slagRefiningEquipment.Percentage / 100; member.GoldBalance += amount; member.SlagBalance -= amount * MiddleTier.BusinessConfig.RefiningConsumeRatio; DataBase.Update(member, session); slagRefiningEquipment.LastRefiningAt = DateTime.Now; DataBase.Update(slagRefiningEquipment, session); //创建转账单 var transferBill = new TransferBill { FromMemberUserName = userName, FromAccountType = AccountType.Slag, ToMemberUserName = userName, ToAccountType = AccountType.Gold, Amount = amount, Status = TransferBillStatus.Completed, TransferBillType = TransferBillType.SlagRefine, ExpiredAt = DateTime.Now, ServiceCharge = amount * MiddleTier.BusinessConfig.RefiningConsumeRatio - amount }; DataBase.Save(transferBill, session); var memberIncomeRecord = new MemberIncomeRecord { Amount = amount, GoldAmount = amount, SlagAmount = -amount * MiddleTier.BusinessConfig.RefiningConsumeRatio, Type = MemberIncomeType.Refining, MemberUserName = userName }; DataBase.Save(memberIncomeRecord, session); iTransaction.Commit(); } catch (Exception ex) { iTransaction.Rollback(); MiddleTier.LogManager.Error(ex); if (ex is PlatformException platformException) { throw platformException; } throw new PlatformException(ErrorCode.SystemError); } finally { session.Close(); } }
/// <summary> /// 取消求购 /// </summary> /// <param name="transferBill"></param> private void CancelToBuy(TransferBill transferBill) { var session = DataBase.Session; ITransaction iTransaction = session.BeginTransaction(IsolationLevel.ReadCommitted); try { var fromMember = DataBase.Get <Member>(e => e.UserName == transferBill.FromMemberUserName, session); var toMember = DataBase.Get <Member>(e => e.UserName == transferBill.ToMemberUserName, session); switch (transferBill.Status) { case TransferBillStatus.Original: break; case TransferBillStatus.SellerEnsure: case TransferBillStatus.BuyerEnsure: if (fromMember == null) { throw new PlatformException(ErrorCode.SystemError); } fromMember.GoldBalance += (transferBill.Amount + transferBill.ServiceCharge); DataBase.Update(fromMember, session); break; case TransferBillStatus.Completed: if (fromMember == null || toMember == null) { throw new PlatformException(ErrorCode.SystemError); } if (toMember.GoldBalance < transferBill.Amount - transferBill.ServiceCharge) { throw new PlatformException(ErrorCode.AccountBalanceNotEnough); } fromMember.GoldBalance += (transferBill.Amount + transferBill.ServiceCharge); toMember.GoldBalance -= transferBill.Amount; if (toMember.GoldBalance < 0) { throw new PlatformException(ErrorCode.AccountBalanceNotEnough); } DataBase.Update(fromMember, session); DataBase.Update(toMember, session); break; default: throw new PlatformException(ErrorCode.ErrorCancelStatus); } transferBill.ExpiredAt = DateTime.Now.AddYears(1); transferBill.Status = TransferBillStatus.Cancelled; DataBase.Update(transferBill, session); iTransaction.Commit(); } catch (Exception ex) { iTransaction.Rollback(); MiddleTier.LogManager.Error(ex); if (ex is PlatformException platformException) { throw platformException; } throw new PlatformException(ErrorCode.SystemError); } finally { session.Close(); } }