123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194 |
- using System;
- using System.Threading;
- using System.Linq;
- using System.Data;
- using Library;
- using MySystem.PxcModels;
- using LitJson;
- using System.Collections.Generic;
- namespace MySystem
- {
- public class LeaderApplyCouponsHelper
- {
- public readonly static LeaderApplyCouponsHelper Instance = new LeaderApplyCouponsHelper();
- private LeaderApplyCouponsHelper()
- {
- }
- public void Start()//启动
- {
- Thread thread = new Thread(doSomething);
- thread.IsBackground = true;
- thread.Start();
- }
- public void doSomething()
- {
- while (true)
- {
- string content = RedisDbconn.Instance.RPop<string>("LeaderApplyCouponsQueue");
- if (!string.IsNullOrEmpty(content))
- {
- try
- {
- var Id = int.Parse(content);
- dosomething(Id);
- // WebCMSEntities db = new WebCMSEntities();
- // var LeaderUserId = 0;
- // var queryList = db.LeaderReserveRecord.FirstOrDefault(m => m.Id == Id) ?? new LeaderReserveRecord();
- // var userInfo = db.Users.FirstOrDefault(m => m.Id == queryList.UserId) ?? new Users();
- // if (userInfo.LeaderLevel == 2)
- // {
- // LeaderUserId = queryList.UserId;
- // }
- // else
- // {
- // string[] ParentNavList = userInfo.ParentNav.Trim(',').Replace(",,", ",").Split(',');
- // for (int j = ParentNavList.Length; j > 0; j--)
- // {
- // int userId = int.Parse(ParentNavList[j - 1]);
- // var user = db.Users.FirstOrDefault(m => m.Id == userId) ?? new Users();
- // //如果是小盟主,则查找该小盟主关联的大盟主储蓄金是否充足,是则给券打上关联大盟主标记
- // if (user.LeaderLevel == 2)
- // {
- // LeaderUserId = userId;
- // break;
- // }
- // else
- // {
- // continue;
- // }
- // }
- // }
- // JsonData ApplyList = JsonMapper.ToObject(queryList.SeoTitle);
- // var SnList = "";
- // for (int i = 0; i < ApplyList.Count; i++)
- // {
- // int num = Convert.ToInt32(ApplyList[i]["Num"].ToString());
- // int type = Convert.ToInt32(ApplyList[i]["Type"].ToString());
- // //电签
- // if (type == 1)
- // {
- // var dposCoupons = db.PosCoupons.Where(m => m.QueryCount == 1 && m.UserId == 0 && m.LeaderUserId == 0 && m.IsUse == 0 && m.IsLock == 0 && m.OpId == 0).Take(num).ToList();//电签券
- // foreach (var item in dposCoupons)
- // {
- // var posCoupon = db.PosCoupons.FirstOrDefault(m => m.Id == item.Id) ?? new PosCoupons();
- // posCoupon.UserId = queryList.UserId;
- // posCoupon.LeaderUserId = LeaderUserId;
- // // SnList += posCoupon.ExchangeCode + ",";
- // db.UserSnDelayChange.Add(new UserSnDelayChange()
- // {
- // CreateDate = DateTime.Now,
- // QueryCount = Id,
- // SnNo = posCoupon.ExchangeCode,
- // });
- // }
- // }
- // //大机
- // if (type == 2)
- // {
- // var bposCoupons = db.PosCoupons.Where(m => m.QueryCount == 2 && m.UserId == 0 && m.LeaderUserId == 0 && m.IsUse == 0 && m.IsLock == 0 && m.OpId == 0).Take(num).ToList();//大机券
- // foreach (var items in bposCoupons)
- // {
- // var posCoupon = db.PosCoupons.FirstOrDefault(m => m.Id == items.Id) ?? new PosCoupons();
- // posCoupon.UserId = queryList.UserId;
- // posCoupon.LeaderUserId = LeaderUserId;
- // // SnList += posCoupon.ExchangeCode + ",";
- // db.UserSnDelayChange.Add(new UserSnDelayChange()
- // {
- // CreateDate = DateTime.Now,
- // QueryCount = Id,
- // SnNo = posCoupon.ExchangeCode,
- // });
- // }
- // }
- // db.SaveChanges();
- // }
- // // queryList.SeoDescription = SnList.TrimEnd(',');
- // db.SaveChanges();
- }
- catch (Exception ex)
- {
- function.WriteLog(DateTime.Now.ToString() + ":" + ex.ToString(), "盟主兑换机具券异常");
- }
- }
- else
- {
- Thread.Sleep(5000);
- }
- }
- }
- private void dosomething(int Id)
- {
- WebCMSEntities db = new WebCMSEntities();
- OpModels.WebCMSEntities opdb = new OpModels.WebCMSEntities();
- var query = db.LeaderReserveRecord.FirstOrDefault(m => m.Id == Id && m.Remark == "兑换机具券");
- if(query != null)
- {
- JsonData ApplyList = JsonMapper.ToObject(query.SeoTitle);
- UserRankItem orderUser = PosCouponPrizeService.Instance.GetUserLevel(db, query.UserId);
- string ParentNav = orderUser.ParentNav + "," + orderUser.Id + ",";
- string[] ParentNavList = ParentNav.Trim(',').Replace(",,", ",").Split(',');
- Array.Reverse(ParentNavList);
- int index = 0;
- bool BuyPrizeFlag = false; //购机奖励发放标识
- decimal BuyPrize = 0; //购机奖励
- foreach(string ParentId in ParentNavList)
- {
- UserRankItem parentUser = PosCouponPrizeService.Instance.GetUserLevel(db, int.Parse(ParentId));
- index += 1;
- //给券打大盟主标记
- if(parentUser.LeaderLevel == 2 && index == 1)
- {
- for (int i = 0; i < ApplyList.Count; i++)
- {
- int num = Convert.ToInt32(ApplyList[i]["Num"].ToString());
- int Kind = Convert.ToInt32(ApplyList[i]["Type"].ToString());
- List<int> couponIds = db.PosCoupons.Where(m => m.IsLock == 0 && m.IsUse == 0 && m.UserId == 0 && m.QueryCount == Kind).OrderBy(m => m.Id).Take(num).ToList().Select(m => m.Id).ToList();
- PosCouponPrizeService.Instance.SetPosCouponLeaderFlag(db, parentUser.Id, num / 3, couponIds);
- }
- }
- if(parentUser.OperateLevel > 0 && !BuyPrizeFlag)
- {
- int groupCount = 0;
- for (int i = 0; i < ApplyList.Count; i++)
- {
- int num = Convert.ToInt32(ApplyList[i]["Num"].ToString());
- int Kind = Convert.ToInt32(ApplyList[i]["Type"].ToString());
- groupCount += num / 3;
- }
- if(PosCouponPrizeService.Instance.CheckOpReserve(opdb, groupCount, parentUser.Id))
- {
- //购机奖
- decimal CurBuyPrize = GetBuyPrize(parentUser);
- PosCouponPrizeService.Instance.OpAccount(db, 0, parentUser.Id, CurBuyPrize - BuyPrize, groupCount);
- BuyPrizeFlag = true;
- BuyPrize = CurBuyPrize;
- }
- }
- }
- }
- db.Dispose();
- opdb.Dispose();
- }
- /// <summary>
- /// 获取购机奖金额
- /// </summary>
- /// <param name="user"></param>
- /// <returns></returns>
- public decimal GetBuyPrize(UserRankItem user)
- {
- if(user.OperateLevel == 2) return 60;
- if(user.OperateLevel == 3) return 80;
- if(user.OperateLevel == 4) return 100;
- return 0;
- }
- }
- }
|