123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621 |
- using System;
- using System.Collections.Generic;
- using System.Linq;
- using System.Data;
- using System.Threading;
- using MySystem.PxcModels;
- using Library;
- using LitJson;
- namespace MySystem
- {
- public class PosCouponPrizeService
- {
- public readonly static PosCouponPrizeService Instance = new PosCouponPrizeService();
- private PosCouponPrizeService()
- { }
- public void Start()
- {
- Thread th = new Thread(ready);
- th.IsBackground = true;
- th.Start();
- }
-
- private void ready()
- {
- bool op = true;
- while (op)
- {
- string content = RedisDbconn.Instance.RPop<string>("PosCouponPrizeQueue");
- if (!string.IsNullOrEmpty(content))
- {
- try
- {
- dosomething(int.Parse(function.CheckInt(content)));
- }
- catch(Exception ex)
- {
- function.WriteLog(DateTime.Now.ToString() + "\n" + ex.ToString(), "商城下单机具券发奖异常");
- }
- }
- else
- {
- Thread.Sleep(2000);
- }
- }
- }
- public void dosomething(int OrderId)
- {
- WebCMSEntities db = new WebCMSEntities();
- OpModels.WebCMSEntities opdb = new OpModels.WebCMSEntities();
- Orders order = db.Orders.FirstOrDefault(m => m.Id == OrderId);
- if(order != null)
- {
- List<int> ChkIds = new List<int>(); //机具券商品Id集合
- ChkIds.Add(10);
- ChkIds.Add(11);
- ChkIds.Add(77);
- ChkIds.Add(78);
- ChkIds.Add(79);
- //判断是否是指定商品
- if(ChkIds.Contains(order.ProductId))
- {
- int Kind = 0;
- if (order.ProductId == 10)
- {
- Kind = 1;
- }
- else if (order.ProductId == 11)
- {
- Kind = 2;
- }
- int CouponCount = 0;
- if(Kind == 1)
- {
- CouponCount = 3 * order.BuyCount;
- }
- else if(Kind == 2)
- {
- CouponCount = 2 * order.BuyCount;
- }
- UserRankItem orderUser = GetUserLevel(db, order.UserId);
- if(orderUser.UserLevel == 0)
- {
- PreUserLevel(db, order.UserId, 1, DateTime.Parse("2025-01-01 00:00:00"));
- }
- string ParentNav = orderUser.ParentNav + "," + orderUser.Id + ",";
- string[] ParentNavList = ParentNav.Trim(',').Replace(",,", ",").Split(',');
- Array.Reverse(ParentNavList);
- int index = 0;
- bool DirectPrizeFlag = false; //推荐奖励发放标识
- bool BackAccountFlag = false; //备用金返余额标识
- bool BigLeaderFlag = false; //大盟主标记
- decimal BuyPrize = 0; //购机奖励
- foreach(string ParentId in ParentNavList)
- {
- UserRankItem parentUser = GetUserLevel(db, int.Parse(ParentId));
- index += 1;
- if(index == 1 && (orderUser.LeaderLevel > 0 || orderUser.OperateLevel > 0) && !DirectPrizeFlag)
- {
- DirectPrize(db, order.Id, order.UserId, order.BuyCount);
- DirectPrizeFlag = true;
- }
- if(index > 1)
- {
- if(Utils.Instance.IsStandardUser(parentUser.Id) && !DirectPrizeFlag)
- {
- DirectPrize(db, order.Id, parentUser.Id, order.BuyCount);
- DirectPrizeFlag = true;
- }
- }
- if(parentUser.AgoLeaderLevel > 0 && CheckLeaderReserve(db, order.BuyCount, parentUser.Id))
- {
- //购机奖
- if(parentUser.LeaderLevel > 0)
- {
- decimal CurBuyPrize = GetBuyPrize(parentUser);
- decimal GetPrize = CurBuyPrize - BuyPrize;
- if(GetPrize > 0)
- {
- int ChangeType = 118;
- if(parentUser.OperateLevel == 1)
- {
- ChangeType = 120;
- }
- if(parentUser.OperateLevel > 1)
- {
- ChangeType = 128;
- }
- OpAccount(db, order.Id, parentUser.Id, CurBuyPrize - BuyPrize, order.BuyCount, ChangeType);
- BuyPrize = CurBuyPrize;
- }
- }
- if(!BackAccountFlag)
- {
- BackAccountFlag = true;
- //扣减备用金
- OpReserve(db, order.Id, parentUser.Id, GetWithdrawMoney(parentUser) * order.BuyCount, 2, order.UserId, "购机奖励");
- //返回到余额
- OpLeaderAccount(db, order, parentUser.Id, GetWithdrawMoney(parentUser), order.BuyCount);
- }
- //给券打大盟主标记
- if(parentUser.LeaderLevel == 2 && !BigLeaderFlag)
- {
- BigLeaderFlag = true;
- if(!string.IsNullOrEmpty(order.SnNos))
- {
- List<string> codes = order.SnNos.Split(',').ToList();
- List<int> couponIds = db.PosCoupons.Where(m => codes.Contains(m.ExchangeCode)).OrderBy(m => m.Id).Take(CouponCount).ToList().Select(m => m.Id).ToList();
- SetPosCouponLeaderFlag(db, parentUser.Id, order.BuyCount, couponIds);
- }
- }
- }
- else if(parentUser.OperateLevel > 0 && CheckOpReserve(opdb, order.BuyCount, parentUser.Id))
- {
- //购机奖
- decimal CurBuyPrize = GetBuyPrize(parentUser);
- decimal GetPrize = CurBuyPrize - BuyPrize;
- if(GetPrize > 0)
- {
- int ChangeType = 120;
- if(parentUser.OperateLevel > 1)
- {
- ChangeType = 128;
- }
- OpAccount(db, order.Id, parentUser.Id, GetPrize, order.BuyCount, ChangeType);
- BuyPrize = CurBuyPrize;
- }
- if(!BackAccountFlag)
- {
- BackAccountFlag = true;
- //扣减备用金
- OperateAmountChange(opdb, parentUser.Id, GetWithdrawMoney(parentUser) * order.BuyCount, 2, 1, "商城购机", true);
- //返回到余额
- OperateAmountChange(opdb, parentUser.Id, GetWithdrawMoney(parentUser) * order.BuyCount, 1, 2, "商城购机", true);
- }
- }
- }
- }
- }
- db.Dispose();
- opdb.Dispose();
- }
- /// <summary>
- /// 获取创客各种等级
- /// </summary>
- /// <param name="db"></param>
- /// <param name="UserId"></param>
- /// <returns></returns>
- public UserRankItem GetUserLevel(WebCMSEntities db, int UserId)
- {
- UserRankItem dic = new UserRankItem();
- DateTime now = DateTime.Now;
- Users user = db.Users.FirstOrDefault(m => m.Id == UserId && m.AuthFlag == 1 && m.Status > -1) ?? new Users();
- UserRankWhite userWhite = db.UserRankWhite.FirstOrDefault(m => m.Id == UserId && m.UpdateDate > now) ?? new UserRankWhite();
- Leaders leader = db.Leaders.FirstOrDefault(m => m.Id == UserId) ?? new Leaders();
- dic.UserLevel = userWhite.Rank > user.UserLevel ? userWhite.Rank : user.UserLevel;
- if(leader.ExpiredDate > now)
- {
- dic.LeaderLevel = leader.LeaderLevel;
- }
- dic.AgoLeaderLevel = leader.LeaderLevel;
- dic.OperateLevel = user.UserType;
- dic.UserType = user.UserType;
- dic.MerchantType = user.MerchantType;
- dic.Id = user.Id;
- dic.MakerCode = user.MakerCode;
- dic.RealName = user.RealName;
- dic.ParentUserId = user.ParentUserId;
- dic.ParentNav = user.ParentNav;
- return dic;
- }
- /// <summary>
- /// 直推奖励
- /// </summary>
- /// <param name="db"></param>
- /// <param name="OrderId"></param>
- /// <param name="UserId"></param>
- /// <param name="Count"></param>
- public void DirectPrize(WebCMSEntities db, int OrderId, int UserId, int Count = 1)
- {
- UserAccount account = db.UserAccount.FirstOrDefault(m => m.Id == UserId);
- if (account == null)
- {
- account = db.UserAccount.Add(new UserAccount()
- {
- Id = UserId,
- UserId = UserId,
- }).Entity;
- db.SaveChanges();
- }
- decimal BeforeTotalAmount = account.TotalAmount; //变更前总金额
- decimal BeforeFreezeAmount = account.FreezeAmount; //变更前冻结金额
- decimal BeforeBalanceAmount = account.BalanceAmount; //变更前余额
- account.BalanceAmount += 100 * Count;
- account.TotalAmount += 100 * Count;
- decimal AfterTotalAmount = account.TotalAmount; //变更后总金额
- decimal AfterFreezeAmount = account.FreezeAmount; //变更后冻结金额
- decimal AfterBalanceAmount = account.BalanceAmount; //变更后余额
- UserAccountRecord userAccountRecord = db.UserAccountRecord.Add(new UserAccountRecord()
- {
- CreateDate = DateTime.Now,
- UpdateDate = DateTime.Now,
- UserId = UserId, //创客
- ChangeType = 112, //变动类型
- ChangeAmount = 100 * Count, //变更金额
- BeforeTotalAmount = BeforeTotalAmount, //变更前总金额
- AfterTotalAmount = AfterTotalAmount, //变更后总金额
- BeforeFreezeAmount = BeforeFreezeAmount, //变更前冻结金额
- AfterFreezeAmount = AfterFreezeAmount, //变更后冻结金额
- BeforeBalanceAmount = BeforeBalanceAmount, //变更前余额
- AfterBalanceAmount = AfterBalanceAmount, //变更后余额
- QueryCount = OrderId,
- }).Entity;
- db.SaveChanges();
- }
-
- /// <summary>
- /// 判断盟主储蓄金足够
- /// </summary>
- /// <param name="db"></param>
- /// <param name="Count"></param>
- /// <param name="UserId"></param>
- /// <returns></returns>
- public bool CheckLeaderReserve(WebCMSEntities db, int Count, int UserId)
- {
- UserAccount acccount = db.UserAccount.FirstOrDefault(m => m.Id == UserId) ?? new UserAccount();
- if(acccount.LeaderReserve >= 400 * Count)
- {
- return true;
- }
- return false;
- }
- /// <summary>
- /// 判断运营中心/合伙人额度足够
- /// </summary>
- /// <param name="db"></param>
- /// <param name="Count"></param>
- /// <param name="UserId"></param>
- /// <returns></returns>
- public bool CheckOpReserve(OpModels.WebCMSEntities db, int Count, int UserId)
- {
- OpModels.UserAccount account = db.UserAccount.FirstOrDefault(m => m.Id == UserId) ?? new OpModels.UserAccount();
- if(account.TotalAmt >= 400 * Count)
- {
- return true;
- }
- return false;
- }
- public bool CheckOpReserve(OpModels.WebCMSEntities db, decimal Amt, int UserId)
- {
- OpModels.UserAccount account = db.UserAccount.FirstOrDefault(m => m.Id == UserId) ?? new OpModels.UserAccount();
- if(account.TotalAmt >= Amt)
- {
- return true;
- }
- return false;
- }
- /// <summary>
- /// 扣减/增加盟主储蓄金
- /// </summary>
- /// <param name="db"></param>
- /// <param name="OrderId"></param>
- /// <param name="UserId"></param>
- /// <param name="Money"></param>
- /// <param name="ChangeType"></param>
- /// <param name="SourceUserId"></param>
- /// <param name="Remark"></param>
- public void OpReserve(WebCMSEntities db, int OrderId, int UserId, decimal Money, int ChangeType, int SourceUserId = 0, string Remark = "储备金购买")
- {
- UserAccount account = db.UserAccount.FirstOrDefault(m => m.Id == UserId);
- if (account == null)
- {
- account = db.UserAccount.Add(new UserAccount()
- {
- Id = UserId,
- UserId = UserId,
- }).Entity;
- db.SaveChanges();
- }
- decimal BeforeAmount = account.LeaderReserve; //变更前总金额
- if(ChangeType == 1)
- {
- account.LeaderReserve += Money;
- }
- else
- {
- account.LeaderReserve -= Money;
- }
- decimal AfterAmount = account.LeaderReserve; //变更后总金额
- LeaderReserveRecord add = db.LeaderReserveRecord.Add(new LeaderReserveRecord()
- {
- CreateDate = DateTime.Now,
- ChangeType = ChangeType,
- OrderId = OrderId,
- Remark = Remark,
- AfterAmt = AfterAmount,
- BeforeAmt = BeforeAmount,
- ChangeAmt = Money,
- TradeDate = DateTime.Now.ToString("yyyyMMdd"),
- TradeMonth = DateTime.Now.ToString("yyyyMM"),
- UserId = UserId,
- SourceUserId = SourceUserId,
- Sort = GetLeaderReserveRecordType(Remark),
- }).Entity;
- db.SaveChanges();
- }
- private int GetLeaderReserveRecordType(string Remark)
- {
- Dictionary<int, string> data = new Dictionary<int, string>();
- data.Add(11, "兑换机具券,机具券兑换,系统增加(盟主储蓄金),系统扣减(盟主储蓄金),储备金购买");
- data.Add(12, "系统增加(可提现余额),系统扣减(可提现余额)");
- data.Add(13, "推荐大盟主,推荐小盟主,商城购机,购机奖励");
- var item = data.FirstOrDefault(m => m.Value.Contains(Remark));
- if(item.Key > 0)
- {
- return item.Key;
- }
- return 0;
- }
- /// <summary>
- /// 操作盟主可提现余额
- /// </summary>
- /// <param name="db"></param>
- /// <param name="order"></param>
- /// <param name="UserId"></param>
- /// <param name="Money"></param>
- /// <param name="Count"></param>
- /// <param name="Kind"></param>
- public void OpLeaderAccount(WebCMSEntities db, Orders order, int UserId, decimal Money, int Count = 1)
- {
- UserAccount account = db.UserAccount.FirstOrDefault(m => m.Id == UserId);
- if (account == null)
- {
- account = db.UserAccount.Add(new UserAccount()
- {
- Id = UserId,
- UserId = UserId,
- }).Entity;
- db.SaveChanges();
- }
- int ChangeType = 0;
- if(Money == 400)
- {
- ChangeType = 117;
- }
- decimal BeforeLeaderBalanceAmount = account.LeaderBalanceAmount; //变更前余额
- account.LeaderBalanceAmount += Money * Count;
- decimal AfterLeaderBalanceAmount = account.LeaderBalanceAmount; //变更后余额
- LeaderAccountRecord leaderAccountRecord = db.LeaderAccountRecord.Add(new LeaderAccountRecord()
- {
- CreateDate = DateTime.Now,
- UpdateDate = DateTime.Now,
- UserId = UserId, //创客
- ChangeType = ChangeType, //变动类型
- ChangeAmount = Money * Count, //变更金额
- BeforeBalanceAmount = BeforeLeaderBalanceAmount, //变更前余额
- AfterBalanceAmount = AfterLeaderBalanceAmount, //变更后余额
- QueryCount = order.Id,
- Sort = order.UserId,
- }).Entity;
- db.SaveChanges();
- }
- /// <summary>
- /// 操作余额
- /// </summary>
- /// <param name="db"></param>
- /// <param name="order"></param>
- /// <param name="UserId"></param>
- /// <param name="Money"></param>
- /// <param name="Count"></param>
- public void OpAccount(WebCMSEntities db, int OrderId, int UserId, decimal Money, int Count = 1, int ChangeType = 0, int Kind = 0)
- {
- UserAccount account = db.UserAccount.FirstOrDefault(m => m.Id == UserId);
- if (account == null)
- {
- account = db.UserAccount.Add(new UserAccount()
- {
- Id = UserId,
- UserId = UserId,
- }).Entity;
- db.SaveChanges();
- }
- decimal BeforeTotalAmount = account.TotalAmount; //变更前总金额
- decimal BeforeFreezeAmount = account.FreezeAmount; //变更前冻结金额
- decimal BeforeBalanceAmount = account.BalanceAmount; //变更前余额
- account.BalanceAmount += Money * Count;
- account.TotalAmount += Money * Count;
- decimal AfterTotalAmount = account.TotalAmount; //变更后总金额
- decimal AfterFreezeAmount = account.FreezeAmount; //变更后冻结金额
- decimal AfterBalanceAmount = account.BalanceAmount; //变更后余额
- UserAccountRecord userAccountRecord = db.UserAccountRecord.Add(new UserAccountRecord()
- {
- CreateDate = DateTime.Now,
- UpdateDate = DateTime.Now,
- UserId = UserId, //创客
- ChangeType = ChangeType, //变动类型
- ChangeAmount = Money * Count, //变更金额
- BeforeTotalAmount = BeforeTotalAmount, //变更前总金额
- AfterTotalAmount = AfterTotalAmount, //变更后总金额
- BeforeFreezeAmount = BeforeFreezeAmount, //变更前冻结金额
- AfterFreezeAmount = AfterFreezeAmount, //变更后冻结金额
- BeforeBalanceAmount = BeforeBalanceAmount, //变更前余额
- AfterBalanceAmount = AfterBalanceAmount, //变更后余额
- QueryCount = OrderId,
- Kind = Kind,
- }).Entity;
- db.SaveChanges();
- }
- /// <summary>
- /// 操作运营中心/合伙人额度(TotalAmt未使用额度,ValidForGetAmount可提现额度)
- /// </summary>
- /// <param name="db"></param>
- /// <param name="UserId"></param>
- /// <param name="Money"></param>
- /// <param name="OperateType">(1增加,2减少)</param>
- /// <param name="AmountType">(1未使用额度,2可提现额度)</param>
- /// <param name="Remark"></param>
- /// <param name="record"></param>
- public void OperateAmountChange(OpModels.WebCMSEntities db, int UserId, decimal Money, int OperateType, int AmountType, string Remark = "", bool record = false)
- {
- OpModels.UserAccount account = db.UserAccount.FirstOrDefault(m => m.Id == UserId);
- if (account == null)
- {
- account = db.UserAccount.Add(new OpModels.UserAccount()
- {
- Id = UserId,
- UserId = UserId,
- }).Entity;
- db.SaveChanges();
- }
- decimal BeforeAmount = account.ValidAmount + account.TotalAmt + account.ValidForGetAmount; //变更前总金额
- if(OperateType == 1)
- {
- if(AmountType == 1)
- {
- account.TotalAmt += Money;
- }
- else if(AmountType == 2)
- {
- account.ValidForGetAmount += Money;
- }
- else
- {
- account.ValidAmount += Money;
- }
- }
- else
- {
- if(AmountType == 1)
- {
- account.TotalAmt -= Money;
- }
- else if(AmountType == 2)
- {
- account.ValidForGetAmount -= Money;
- }
- else
- {
- account.ValidAmount -= Money;
- }
- }
- decimal AfterAmount = account.ValidAmount + account.TotalAmt + account.ValidForGetAmount; //变更后总金额
- if(record)
- {
- OpModels.AmountRecord add = db.AmountRecord.Add(new OpModels.AmountRecord()
- {
- CreateDate = DateTime.Now,
- UpdateDate = DateTime.Now,
- OperateType = OperateType,
- AfterAmount = AfterAmount,
- AfterValidForGetAmount = account.ValidForGetAmount,
- AfterTotalAmt = account.TotalAmt,
- AfterValidAmount = account.ValidAmount,
- BeforeAmount = BeforeAmount,
- UseAmount = Money,
- UserId = UserId,
- SeoDescription = Remark,
- Version = AmountType,
- }).Entity;
- }
- db.SaveChanges();
- }
- /// <summary>
- /// 给券设置大盟主标记
- /// </summary>
- /// <param name="db"></param>
- /// <param name="LeaderUserId"></param>
- /// <param name="Count"></param>
- /// <param name="couponIds"></param>
- public int SetPosCouponLeaderFlag(WebCMSEntities db, int LeaderUserId, int Count, List<int> couponIds)
- {
- int SetCount = 0;
- UserAccount pacccount = db.UserAccount.FirstOrDefault(m => m.Id == LeaderUserId) ?? new UserAccount();
- foreach(int couponId in couponIds)
- {
- PosCoupons coupon = db.PosCoupons.FirstOrDefault(m => m.Id == couponId);
- if(coupon != null && pacccount.LeaderReserve >= 400 * Count)
- {
- coupon.LeaderUserId = LeaderUserId;
- SetCount += 1;
- }
- }
- db.SaveChanges();
- return SetCount;
- }
- /// <summary>
- /// 获取购机奖金额
- /// </summary>
- /// <param name="user"></param>
- /// <returns></returns>
- public decimal GetBuyPrize(UserRankItem user)
- {
- if(user.OperateLevel == 1) return 100;
- if(user.OperateLevel == 2) return 160;
- if(user.OperateLevel == 3) return 180;
- if(user.OperateLevel == 4) return 200;
- if(user.LeaderLevel > 0) return 100;
- return 0;
- }
- /// <summary>
- /// 获取可提现金额
- /// </summary>
- /// <param name="user"></param>
- /// <returns></returns>
- public decimal GetWithdrawMoney(UserRankItem user)
- {
- if(user.OperateLevel == 1) return 400;
- // if(user.OperateLevel == 2) return 340;
- // if(user.OperateLevel == 3) return 320;
- // if(user.OperateLevel == 4) return 300;
- if(user.OperateLevel == 2) return 400;
- if(user.OperateLevel == 3) return 400;
- if(user.OperateLevel == 4) return 400;
- if(user.AgoLeaderLevel > 0) return 400;
- return 0;
- }
- /// <summary>
- /// 预设职级
- /// </summary>
- /// <param name="db"></param>
- /// <param name="UserId">创客Id</param>
- /// <param name="Rank">职级</param>
- /// <param name="ExpiredDate">过期时间</param>
- public void PreUserLevel(WebCMSEntities db, int UserId, int Rank, DateTime ExpiredDate)
- {
- UserRankWhite rank = db.UserRankWhite.FirstOrDefault(m => m.Id == UserId);
- if(rank == null)
- {
- rank = db.UserRankWhite.Add(new UserRankWhite()
- {
- CreateDate = DateTime.Now, //设置时间
- UserId = UserId, //用户
- Id = UserId,
- }).Entity;
- db.SaveChanges();
- }
- rank.Rank = Rank;
- rank.UpdateDate = ExpiredDate;
- db.SaveChanges();
- }
- }
- }
|