12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697989910010110210310410510610710810911011111211311411511611711811912012112212312412512612712812913013113213313413513613713813914014114214314414514614714814915015115215315415515615715815916016116216316416516616716816917017117217317417517617717817918018118218318418518618718818919019119219319419519619719819920020120220320420520620720820921021121221321421521621721821922022122222322422522622722822923023123223323423523623723823924024124224324424524624724824925025125225325425525625725825926026126226326426526626726826927027127227327427527627727827928028128228328428528628728828929029129229329429529629729829930030130230330430530630730830931031131231331431531631731831932032132232332432532632732832933033133233333433533633733833934034134234334434534634734834935035135235335435535635735835936036136236336436536636736836937037137237337437537637737837938038138238338438538638738838939039139239339439539639739839940040140240340440540640740840941041141241341441541641741841942042142242342442542642742842943043143243343443543643743843944044144244344444544644744844945045145245345445545645745845946046146246346446546646746846947047147247347447547647747847948048148248348448548648748848949049149249349449549649749849950050150250350450550650750850951051151251351451551651751851952052152252352452552652752852953053153253353453553653753853954054154254354454554654754854955055155255355455555655755855956056156256356456556656756856957057157257357457557657757857958058158258358458558658758858959059159259359459559659759859960060160260360460560660760860961061161261361461561661761861962062162262362462562662762862963063163263363463563663763863964064164264364464564664764864965065165265365465565665765865966066166266366466566666766866967067167267367467567667767867968068168268368468568668768868969069169269369469569669769869970070170270370470570670770870971071171271371471571671771871972072172272372472572672772872973073173273373473573673773873974074174274374474574674774874975075175275375475575675775875976076176276376476576676776876977077177277377477577677777877978078178278378478578678778878979079179279379479579679779879980080180280380480580680780880981081181281381481581681781881982082182282382482582682782882983083183283383483583683783883984084184284384484584684784884985085185285385485585685785885986086186286386486586686786886987087187287387487587687787887988088188288388488588688788888989089189289389489589689789889990090190290390490590690790890991091191291391491591691791891992092192292392492592692792892993093193293393493593693793893994094194294394494594694794894995095195295395495595695795895996096196296396496596696796896997097197297397497597697797897998098198298398498598698798898999099199299399499599699799899910001001100210031004100510061007100810091010101110121013101410151016101710181019102010211022102310241025102610271028102910301031103210331034103510361037103810391040104110421043104410451046104710481049105010511052105310541055105610571058105910601061106210631064106510661067106810691070107110721073107410751076107710781079108010811082108310841085108610871088108910901091109210931094109510961097109810991100110111021103110411051106110711081109111011111112111311141115111611171118111911201121112211231124112511261127 |
- using System;
- using System.Collections.Generic;
- using System.Linq;
- using System.Data;
- using MySystem.PxcModels;
- using Library;
- using LitJson;
- namespace MySystem
- {
- public class AlipayPayBack2Service
- {
- public readonly static AlipayPayBack2Service Instance = new AlipayPayBack2Service();
- private AlipayPayBack2Service()
- { }
- public void Start(JobMqMsg jobInfo)
- {
- string content = "";
- try
- {
- dosomething();
- // string Msg = "success";
- // jobInfo.Status = Msg == "success" ? 1 : 0;
- // jobInfo.Msg = Msg == "success" ? "执行完成" : Msg;
- // RabbitMQClient.Instance.SendMsg(Newtonsoft.Json.JsonConvert.SerializeObject(jobInfo), "PublicBack");
- }
- catch (Exception ex)
- {
- if (!string.IsNullOrEmpty(content))
- {
- Dictionary<string, string> data = new Dictionary<string, string>();
- data.Add("ErrTime", DateTime.Now.ToString("yyyy-MM-dd HH:mm:ss"));
- data.Add("ErrMsg", ex.ToString());
- function.WriteLog(Newtonsoft.Json.JsonConvert.SerializeObject(data), "public_err");
- }
- else
- {
- function.WriteLog(DateTime.Now.ToString() + ":" + ex.ToString(), "商城订单支付回调异常");
- }
- }
- }
-
- private void dosomething()
- {
- bool op = true;
- while (op)
- {
- string content = RedisDbconn.Instance.RPop<string>("PayCallBack2");
- if (!string.IsNullOrEmpty(content))
- {
- sloveAlipayCallBack(content);
- }
- else
- {
- op = false;
- }
- }
- }
- public void sloveAlipayCallBack(string content)
- {
- JsonData jsonObj = JsonMapper.ToObject(content);
- string OrderNo = jsonObj["out_trade_no"].ToString();
- string TradeNo = jsonObj["transaction_id"].ToString();
- decimal TotalFee = decimal.Parse(function.CheckNum(jsonObj["total_fee"].ToString()));
- WebCMSEntities db = new WebCMSEntities();
- OrderForNo forNo = db.OrderForNo.FirstOrDefault(m => m.OrderNo == OrderNo);
- if (forNo != null)
- {
- string[] ids = forNo.OrderIds.Split(',');
- foreach (string idString in ids)
- {
- int OrderId = int.Parse(idString);
- DoOrderV2(db, OrderId);
- }
- }
- db.Dispose();
- }
- #region 新订单流程
- public void DoOrderV2(WebCMSEntities db, int OrderId)
- {
- Orders order = db.Orders.FirstOrDefault(m => m.Id == OrderId && m.Status <= 0);
- if (order != null)
- {
- order.Status = 1;
- order.PayDate = DateTime.Now;
- order.PayStatus = 1;
- db.SaveChanges();
- if(order.ParentOrderId > 0)
- {
- int total = db.Orders.Count(m => m.ParentOrderId == order.ParentOrderId);
- int paycount = db.Orders.Count(m => m.ParentOrderId == order.ParentOrderId && m.Status > 0);
- if(paycount >= total)
- {
- // order = db.Orders.FirstOrDefault(m => m.Id == order.ParentOrderId && m.Status == 0);
- // if (order != null)
- // {
- // order.Status = 2;
- // order.PayDate = DateTime.Now;
- // order.PayStatus = 1;
- // OrderId = order.Id;
- // }
- DoOrderV2(db, order.ParentOrderId);
- }
- return;
- }
- OrderProduct pro = db.OrderProduct.FirstOrDefault(m => m.OrderId == OrderId);
- if (pro != null)
- {
- order.ProductId = pro.ProductId;
- db.SaveChanges();
- if(order.ErpMode > 0)
- {
- pro.ProductId = order.ProductId;
- }
- List<int> couponIds = new List<int>();
- if (pro.ProductId == 10 || pro.ProductId == 11 || pro.ProductId == 27 || pro.ProductId == 28 || pro.ProductId == 34 || pro.ProductId == -2)
- {
- order.Status = 2;
- int BuyCount = pro.ProductCount;
- int Kind = 0;
- int BeforeLeaderLevel = 0;
- if (pro.ProductId == 10 || pro.ProductId == 34)
- {
- Kind = 1;
- }
- else if (pro.ProductId == 11 || pro.ProductId == -2)
- {
- Kind = 2;
- }
- else if (pro.ProductId == 28)
- {
- Users user = db.Users.FirstOrDefault(m => m.Id == order.UserId && m.LeaderLevel == 0);
- if(user != null)
- {
- BeforeLeaderLevel = user.LeaderLevel;
- user.LeaderLevel = 1;
- }
- }
- else if (pro.ProductId == 27)
- {
- Users user = db.Users.FirstOrDefault(m => m.Id == order.UserId && m.LeaderLevel < 2);
- if(user != null)
- {
- BeforeLeaderLevel = user.LeaderLevel;
- user.LeaderLevel = 2;
- }
- }
- if(Kind <= 2 && (pro.ProductId == 10 || pro.ProductId == 11 || pro.ProductId == 34 || pro.ProductId == -2))
- {
- // 购买600一组机具券,返券
- int CouponCount = 0;
- if(Kind == 1)
- {
- CouponCount = 3 * pro.ProductCount;
- }
- else if(Kind == 2)
- {
- CouponCount = 2 * pro.ProductCount;
- }
- string Codes = "";
- var coupons = db.PosCoupons.Where(m => m.IsLock == 0 && m.IsUse == 0 && m.UserId == 0 && m.QueryCount == Kind).OrderBy(m => m.Id).Take(CouponCount).ToList();
- int RecordId = 0;
- if(coupons.Count > 0 && (pro.ProductId == 34 || pro.ProductId == -2))
- {
- var adds = db.HelpProfitExchange.Add(new HelpProfitExchange()
- {
- CreateDate = DateTime.Now, //创建时间
- UserId = order.UserId,
- ExchangeCount = coupons.Count,
- }).Entity;
- db.SaveChanges();
- RecordId = adds.Id;
- }
- foreach (var coupon in coupons) // TODO: 数量多的话,会慢
- {
- PosCoupons item = db.PosCoupons.FirstOrDefault(m => m.Id == coupon.Id);
- if (item != null)
- {
- item.CreateDate = DateTime.Now;
- item.UserId = order.UserId;
- item.UpdateDate = DateTime.Now.AddDays(180);
- if(pro.ProductId == 34 || pro.ProductId == -2)
- {
- item.HelpProfitFlag = 1;
- db.HelpProfitExchangeDetail.Add(new HelpProfitExchangeDetail()
- {
- CreateDate = DateTime.Now, //创建时间
- UserId = order.UserId,
- PosCouponId = item.Id,
- ExchangeCode = item.ExchangeCode,
- RecordId = RecordId,
- });
- }
- Codes += item.ExchangeCode + ",";
- couponIds.Add(coupon.Id);
- }
- }
- order.SnNos = Codes.TrimEnd(',');
- }
- db.SaveChanges();
- if (pro.ProductId == 27 || pro.ProductId == 28)
- {
- //充值盟主储备金
- decimal TotalPrice = 10000;
- if(pro.ProductId == 27)
- {
- TotalPrice = 40000;
- }
- OpReserve(db, order, order.UserId, TotalPrice, 1);
- Users user = db.Users.FirstOrDefault(m => m.Id == order.UserId) ?? new Users();
- if(pro.ProductId == 27)
- {
- OperateReserveBackFor(db, user.Id, 40000);
- //预设大盟主职级
- LeaderPreUserLevel(db, order.UserId, 2);
- decimal Prize = decimal.Parse(function.CheckNum(PublicFunction.GetPublicParam(db, "BigLeaderPrize")));
- if(Prize > 0 && BeforeLeaderLevel < 2 && user.UserType == 0)
- {
- LeaderRecommendPrize(db, order, user.Id, Prize, 122);
- }
- }
- }
- if (pro.ProductId == 28) //购买小盟主,给上级大盟主返储备金
- {
- bool check = LeaderBack(db, order);
- Users user = db.Users.FirstOrDefault(m => m.Id == order.UserId) ?? new Users();
- if (check) OperateReserveBackFor(db, user.Id, 10000);
- //预设小盟主职级
- LeaderPreUserLevel(db, order.UserId, 1);
- decimal Prize = decimal.Parse(function.CheckNum(PublicFunction.GetPublicParam(db, "SmallLeaderPrize")));
- if(Prize > 0 && BeforeLeaderLevel < 1 && user.UserType == 0)
- {
- LeaderRecommendPrize(db, order, user.Id, Prize, 123);
- }
- }
- }
- // 购买盟主储蓄金
- if (pro.ProductId == 39 || pro.ProductId == 40)
- {
- order.Status = 2;
- //充值盟主储备金
- decimal TotalPrice = 10000;
- if(pro.ProductId == 39)
- {
- TotalPrice = 40000;
- }
- OpReserve(db, order, order.UserId, TotalPrice, 1);
- }
- //推荐下单奖励
- if (pro.ProductId == 10 || pro.ProductId == 11)
- {
- bool checkPrize = db.UserAccountRecord.Any(m => m.QueryCount == OrderId && m.ChangeType == 112);
- function.WriteLog("OrderId:" + OrderId, "推荐下单奖励监控日志");
- function.WriteLog("checkPrize:" + checkPrize, "推荐下单奖励监控日志");
- if (checkPrize)
- {
- Users user = db.Users.FirstOrDefault(m => m.Id == order.UserId);
- if (user != null)
- {
- if(user.LeaderLevel > 0 || user.UserType == 1) //盟主或运营中心
- {
- //获得100元奖励
- OpAccount(db, order, order.UserId, 100, pro.ProductCount);
- }
- }
- }
- else
- {
- Users user = db.Users.FirstOrDefault(m => m.Id == order.UserId);
- if (user != null)
- {
- bool directPrize = false; //直推奖标记
- bool bigLeaderPrize = false; //大盟主券标记
- bool operateFlag = false; //运营中心标记
- bool buyPrize = false; //返100购机奖励标记或返100购机奖励标记-运营中心
- int leaderFlag = 0; //返600备用金标记
- if(user.LeaderLevel > 0 || user.UserType == 1) //盟主或运营中心
- {
- if(user.LeaderLevel > 0)
- {
- UserAccount acccount = db.UserAccount.FirstOrDefault(m => m.Id == order.UserId) ?? new UserAccount();
- if(acccount.LeaderReserve >= order.TotalPrice)
- {
- if(order.PayMode == 4)
- {
- //扣减备用金
- OpReserve(db, order, order.UserId, order.TotalPrice, 0, 0, "商城购机(储备金支付)");
- }
- }
- }
- //获得100元奖励
- OpAccount(db, order, order.UserId, 100, pro.ProductCount);
- //推荐奖励
- DirectPrize(db, order.Id, order.UserId, pro.ProductCount);
- //推荐下单上级获得30天的机具循环天数(盟主自己得)
- var posList = db.PosMachinesTwo.Select(m => new { m.Id, m.UserId, m.BindingState, m.RecycEndDate }).Where(m => m.UserId == order.UserId && m.BindingState == 0 && m.RecycEndDate != null).ToList();
- foreach (var subPos in posList)
- {
- PosMachinesTwo pos = db.PosMachinesTwo.FirstOrDefault(m => m.Id == subPos.Id);
- if (pos != null)
- {
- pos.RecycEndDate = pos.RecycEndDate.Value.AddDays(30);
- }
- }
- db.SaveChanges();
- if(order.PayMode == 4 && user.LeaderLevel > 0) //使用盟主储蓄金,并且是盟主
- {
- if(user.UserType == 1)
- {
- if(couponIds.Count > 0)
- {
- UserAccount acccount = db.UserAccount.FirstOrDefault(m => m.Id == user.Id) ?? new UserAccount();
- foreach(int couponId in couponIds)
- {
- PosCoupons coupon = db.PosCoupons.FirstOrDefault(m => m.Id == couponId);
- if(coupon != null)
- {
- if(acccount.LeaderReserve >= 400 * pro.ProductCount)
- {
- coupon.LeaderUserId = user.Id;
- }
- coupon.OpId = user.Id;
- }
- }
- }
- db.SaveChanges();
- }
- else
- {
- //寻找最近运营中心额度大于0的运营中心
- int PUserId = user.Id;
- bool OperateFlag = true;
- bool OncePrizeFlag1 = true;
- while(PUserId > 0)
- {
- Users puser = db.Users.FirstOrDefault(m => m.Id == PUserId);
- if(puser != null)
- {
- if(puser.UserType == 1 && OperateFlag == true) //运营中心
- {
- if(couponIds.Count > 0)
- {
- UserAccount acccount = db.UserAccount.FirstOrDefault(m => m.Id == order.UserId) ?? new UserAccount();
- foreach(int couponId in couponIds)
- {
- PosCoupons coupon = db.PosCoupons.FirstOrDefault(m => m.Id == couponId);
- if(coupon != null)
- {
- if(acccount.LeaderReserve >= 400 * pro.ProductCount)
- {
- coupon.LeaderUserId = user.Id;
- }
- coupon.OpId = puser.Id;
- }
- }
- db.SaveChanges();
- if(acccount.LeaderReserve >= 400 * pro.ProductCount && OncePrizeFlag1)
- {
- //扣减备用金
- OpReserve(db, order, order.UserId, 400 * pro.ProductCount, 2, 0, "商城购机");
- //返回到余额
- OpLeaderAccount(db, order, order.UserId, 400, pro.ProductCount);
- OncePrizeFlag1 = false;
- }
- OperateFlag = false;
- }
- }
- PUserId = puser.ParentUserId;
- }
- else
- {
- PUserId = 0;
- }
- }
- }
- }
- else if(order.PayMode == 4 && user.UserType == 1) //使用盟主储蓄金,并且是运营中心
- {
- if(couponIds.Count > 0)
- {
- foreach(int couponId in couponIds)
- {
- PosCoupons coupon = db.PosCoupons.FirstOrDefault(m => m.Id == couponId);
- if(coupon != null)
- {
- coupon.OpId = user.Id;
- }
- }
- }
- db.SaveChanges();
- //寻找最近储蓄金充足的大盟主
- int PUserId = user.Id;
- bool LeaderFlag = true;
- bool OncePrizeFlag2 = true;
- while(PUserId > 0)
- {
- Users puser = db.Users.FirstOrDefault(m => m.Id == PUserId);
- if(puser != null)
- {
- if(puser.LeaderLevel == 2 && LeaderFlag == true) //大盟主
- {
- if(couponIds.Count > 0)
- {
- UserAccount pacccount = db.UserAccount.FirstOrDefault(m => m.Id == puser.Id) ?? new UserAccount();
- foreach(int couponId in couponIds)
- {
- PosCoupons coupon = db.PosCoupons.FirstOrDefault(m => m.Id == couponId);
- if(coupon != null && pacccount.LeaderReserve >= 400 * pro.ProductCount)
- {
- coupon.LeaderUserId = puser.Id;
- }
- }
- db.SaveChanges();
- UserAccount acccount = db.UserAccount.FirstOrDefault(m => m.Id == order.UserId) ?? new UserAccount();
- if(acccount.LeaderReserve >= 400 * pro.ProductCount && OncePrizeFlag2)
- {
- //扣减备用金
- OpReserve(db, order, order.UserId, 400 * pro.ProductCount, 2, 0, "商城购机");
- //返回到余额
- OpLeaderAccount(db, order, order.UserId, 400, pro.ProductCount);
- OncePrizeFlag2 = false;
- }
- LeaderFlag = false;
- }
- }
- PUserId = puser.ParentUserId;
- }
- else
- {
- PUserId = 0;
- }
- }
- }
- else
- {
- //寻找最近储蓄金充足的大盟主及最近运营中心额度大于0的运营中心(含自身)
- int PUserId = user.Id;
- int LeaderFlag = 0;
- bool OperateFlag = true;
- bool OncePrizeFlag3 = true;
- while(PUserId > 0)
- {
- Users puser = db.Users.FirstOrDefault(m => m.Id == PUserId);
- if(puser != null)
- {
- if(puser.LeaderLevel > 0 && puser.LeaderLevel > LeaderFlag && LeaderFlag < 2) //大盟主
- {
- if(couponIds.Count > 0)
- {
- if(puser.LeaderLevel == 2)
- {
- UserAccount pacccount = db.UserAccount.FirstOrDefault(m => m.Id == puser.Id) ?? new UserAccount();
- foreach(int couponId in couponIds)
- {
- PosCoupons coupon = db.PosCoupons.FirstOrDefault(m => m.Id == couponId);
- if(coupon != null && pacccount.LeaderReserve >= 400 * pro.ProductCount)
- {
- coupon.LeaderUserId = puser.Id;
- }
- }
- db.SaveChanges();
- }
- UserAccount acccount = db.UserAccount.FirstOrDefault(m => m.Id == puser.Id) ?? new UserAccount();
- if(acccount.LeaderReserve >= 400 * pro.ProductCount && OncePrizeFlag3)
- {
- //扣减备用金
- OpReserve(db, order, puser.Id, 400 * pro.ProductCount, 2, 0, "商城购机");
- //返回到余额
- OpLeaderAccount(db, order, puser.Id, 400, pro.ProductCount);
- LeaderFlag = puser.LeaderLevel;
- OncePrizeFlag3 = false;
- }
- }
- }
- if(puser.UserType == 1 && OperateFlag == true) //运营中心
- {
- if(couponIds.Count > 0)
- {
- foreach(int couponId in couponIds)
- {
- PosCoupons coupon = db.PosCoupons.FirstOrDefault(m => m.Id == couponId);
- if(coupon != null)
- {
- coupon.OpId = puser.Id;
- }
- }
- db.SaveChanges();
- OperateFlag = false;
- }
- }
- PUserId = puser.ParentUserId;
- }
- else
- {
- PUserId = 0;
- }
- }
- //运营中心返额度(下单人自己是运营中心)
- // OperateReserveBackFor(db, order.UserId, order.TotalPrice);
- }
- return;
- }
- int ParentUserId = user.ParentUserId;
- // List<int> proids = new List<int>();
- // proids.Add(10);
- // proids.Add(11);
- // 普通创客购买600一组的机具券
- bool OncePrizeFlag4 = true;
- while(ParentUserId > 0)
- {
- Users puser = db.Users.FirstOrDefault(m => m.Id == ParentUserId) ?? new Users();
- int machineCount = db.PosMachinesTwo.Count(m => m.BuyUserId == ParentUserId && m.PosSnType == 0 && m.ActivationState == 0); //判断是否拥有3台兑换机
- int ActiveCount = db.PosMachinesTwo.Count(m => m.BuyUserId == ParentUserId && m.ActivationState == 1); //判断是否拥有3台激活机(不限购买或赠送)
- int couponCount = db.PosCoupons.Count(m => m.UserId == ParentUserId && m.IsUse == 0); //判断是否拥有3张券
- function.WriteLog("MakerCode:" + puser.MakerCode, "推荐下单奖励监控日志");
- function.WriteLog("machineCount:" + machineCount, "推荐下单奖励监控日志");
- function.WriteLog("ActiveCount:" + ActiveCount, "推荐下单奖励监控日志");
- if ((machineCount + ActiveCount + couponCount >= 3 || puser.LeaderLevel > 0) && !directPrize)
- {
- // function.WriteLog("满足条件", "推荐下单奖励监控日志");
- // if(puser.LeaderLevel == 0) // 非盟主直推奖励,每个创客第一次下单,上级可得
- // {
- // List<int> oids = new List<int>();
- // var orderpros = db.OrderProduct.Select(m => new { m.OrderId,m.UserId,m.ProductId }).Where(m => m.UserId == order.UserId && proids.Contains(m.ProductId)).ToList();
- // foreach(var orderpro in orderpros)
- // {
- // oids.Add(orderpro.OrderId);
- // }
- // bool check = db.Orders.Any(m => oids.Contains(m.Id) && m.Status > 0);
- // if(!check)
- // {
- // DirectPrize(db, order.Id, ParentUserId, pro.ProductCount);
- // directPrize = true;
- // }
- // }
- // else
- // {
- // 盟主直推奖励,可以每次下单获得
- DirectPrize(db, order.Id, ParentUserId, pro.ProductCount);
- directPrize = true;
- // }
- //推荐下单上级获得30天的机具循环天数
- var posList = db.PosMachinesTwo.Select(m => new { m.Id, m.UserId, m.BindingState, m.RecycEndDate }).Where(m => m.UserId == ParentUserId && m.BindingState == 0 && m.RecycEndDate != null).ToList();
- foreach (var subPos in posList)
- {
- PosMachinesTwo pos = db.PosMachinesTwo.FirstOrDefault(m => m.Id == subPos.Id);
- if (pos != null)
- {
- pos.RecycEndDate = pos.RecycEndDate.Value.AddDays(30);
- }
- }
- db.SaveChanges();
- }
- if(puser.LeaderLevel > 0)
- {
- UserAccount acccount = db.UserAccount.FirstOrDefault(m => m.Id == puser.Id) ?? new UserAccount();
- if(acccount.LeaderReserve >= order.TotalPrice && !buyPrize)
- {
- //购机奖励
- OpAccount(db, order, puser.Id, 100, pro.ProductCount);
- buyPrize = true;
- }
- if(acccount.LeaderReserve >= 400 * pro.ProductCount && puser.LeaderLevel > leaderFlag && leaderFlag < 2 && OncePrizeFlag4)
- {
- //扣减备用金
- OpReserve(db, order, puser.Id, 400 * pro.ProductCount, 2, order.UserId, "购机奖励");
- //返回到余额
- OpLeaderAccount(db, order, puser.Id, 400, pro.ProductCount);
- leaderFlag = puser.LeaderLevel;
- OncePrizeFlag4 = false;
- }
- //如果是大盟主,则标记大盟主标签
- if(couponIds.Count > 0 && puser.LeaderLevel == 2 && acccount.LeaderReserve >= order.TotalPrice && !bigLeaderPrize)
- {
- foreach(int couponId in couponIds)
- {
- PosCoupons coupon = db.PosCoupons.FirstOrDefault(m => m.Id == couponId);
- if(coupon != null)
- {
- coupon.LeaderUserId = puser.Id;
- }
- }
- db.SaveChanges();
- bigLeaderPrize = true;
- }
- }
- if(puser.UserType == 1)
- {
- UserAccount acccount = db.UserAccount.FirstOrDefault(m => m.Id == puser.Id) ?? new UserAccount();
- if(!buyPrize)
- {
- //购机奖励
- ChangeAccount(db, order, puser.Id, 100 * pro.ProductCount, 120);
- buyPrize = true;
- }
- //标记标签
- if(couponIds.Count > 0 && !operateFlag)
- {
- foreach(int couponId in couponIds)
- {
- PosCoupons coupon = db.PosCoupons.FirstOrDefault(m => m.Id == couponId);
- if(coupon != null)
- {
- coupon.OpId= puser.Id;
- }
- }
- db.SaveChanges();
- operateFlag = true;
- }
- }
- ParentUserId = puser.ParentUserId;
- }
- //运营中心返额度
- // OperateReserveBackFor(db, order.UserId, order.TotalPrice);
- }
- }
- function.WriteLog("\r\n\r\n", "推荐下单奖励监控日志");
- }
- //推荐王
- if(pro.ProductId == 29)
- {
- RecommendMethod(order.UserId, order.CreateDate.Value.ToString("yyyyMM"));
- }
- //购买分仓临时额度
- if(pro.ProductId == 30 || pro.ProductId == 31 || pro.ProductId == 32)
- {
- string SendData = "{\"Kind\":\"1\",\"Data\":{\"OrderId\":\"" + order.Id + "\"}}";
- RedisDbconn.Instance.AddList("StoreApplyQueue", SendData);
- }
- }
- else
- {
- if(order.ProductId > 0 && order.ErpMode > 0)
- {
- //盟主储备金-升级/购买 ErpMode:1-升级,2-购买
- if(order.ErpMode > 0)
- {
- Users user = db.Users.FirstOrDefault(m => m.Id == order.UserId);
- if(user != null)
- {
- if(order.ErpMode == 1)
- {
- user.LeaderLevel = 2;
- db.SaveChanges();
- }
- OpReserve(db, order, order.UserId, order.TotalPrice, 1);
- }
- }
- }
- }
- }
- }
- //小盟主购买逻辑
- public bool LeaderBack(WebCMSEntities db, Orders order)
- {
- int LeaderUserId = order.UserId;
- int level = 0;
- bool result = true;
- while(LeaderUserId > 0)
- {
- level += 1;
- Users user = db.Users.FirstOrDefault(m => m.Id == LeaderUserId);
- if(user != null)
- {
- if(user.LeaderLevel == 2 && level > 1)
- {
- UserAccount account = db.UserAccount.FirstOrDefault(m => m.Id == LeaderUserId);
- if(account != null)
- {
- decimal TotalPrice = 10000;
- if(account.LeaderReserve >= TotalPrice)
- {
- OpReserve(db, order, LeaderUserId, TotalPrice, 2, 0, "推荐小盟主");
- OpLeaderAccount(db, order, LeaderUserId, TotalPrice);
- LeaderUserId = 0;
- result = false;
- }
- else
- {
- LeaderUserId = user.ParentUserId;
- // decimal LeaderReserve = account.LeaderReserve;
- // OpReserve(db, order, LeaderUserId, LeaderReserve, 2, 0, "推荐小盟主");
- // OpLeaderAccount(db, order, LeaderUserId, LeaderReserve);
- }
- }
- }
- else
- {
- LeaderUserId = user.ParentUserId;
- }
- }
- else
- {
- LeaderUserId = 0;
- }
- }
- return result;
- }
- //操作储备金
- public void OpReserve(WebCMSEntities db, Orders order, 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 = order.Id,
- Remark = Remark,
- AfterAmt = AfterAmount,
- BeforeAmt = BeforeAmount,
- ChangeAmt = Money,
- TradeDate = DateTime.Now.ToString("yyyyMMdd"),
- TradeMonth = DateTime.Now.ToString("yyyyMM"),
- UserId = UserId,
- SourceUserId = SourceUserId,
- }).Entity;
- db.SaveChanges();
- }
- //操作余额
- public void OpAccount(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 == 600)
- {
- ChangeType = 117;
- }
- else if(Money == 100)
- {
- ChangeType = 118;
- }
- 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 = order.Id,
- }).Entity;
- db.SaveChanges();
- }
- public void ChangeAccount(WebCMSEntities db, Orders order, int UserId, decimal Money, int ChangeType = 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 BeforeTotalAmount = account.TotalAmount; //变更前总金额
- decimal BeforeFreezeAmount = account.FreezeAmount; //变更前冻结金额
- decimal BeforeBalanceAmount = account.BalanceAmount; //变更前余额
- account.BalanceAmount += Money;
- if(Money > 0)
- {
- account.TotalAmount += Money;
- }
- 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 = Math.Abs(Money), //变更金额
- BeforeTotalAmount = BeforeTotalAmount, //变更前总金额
- AfterTotalAmount = AfterTotalAmount, //变更后总金额
- BeforeFreezeAmount = BeforeFreezeAmount, //变更前冻结金额
- AfterFreezeAmount = AfterFreezeAmount, //变更后冻结金额
- BeforeBalanceAmount = BeforeBalanceAmount, //变更前余额
- AfterBalanceAmount = AfterBalanceAmount, //变更后余额
- QueryCount = order.Id,
- Remark = Remark,
- }).Entity;
- db.SaveChanges();
- }
- //操作盟主可提现余额
- 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,
- }).Entity;
- db.SaveChanges();
- }
- 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();
- }
- // 推荐王逻辑(下单)
- public void RecommendMethod(int UserId, string TradeMonth)
- {
- string SendData = "{\"Kind\":\"1\",\"Data\":{\"UserId\":\"" + UserId + "\",\"TradeMonth\":\"" + TradeMonth + "\"}}";
- RedisDbconn.Instance.AddList("RecommendActStatQueue", SendData);
- }
- //操作运营中心额度
- public void OperateReserveBackFor(WebCMSEntities maindb, int UserId, decimal Money)
- {
- OpModels.WebCMSEntities db = new OpModels.WebCMSEntities();
- while(UserId > 0)
- {
- Users user = maindb.Users.FirstOrDefault(m => m.Id == UserId);
- if(user != null)
- {
- bool sys = db.SysAdmin.Any(m => m.UserId == UserId);
- if(sys)
- {
- OpModels.UserAccount account = db.UserAccount.FirstOrDefault(m => m.Id == UserId) ?? new OpModels.UserAccount();
- if(account.TotalAmt <= 0)
- {
- return;
- }
- if(account.TotalAmt < Money)
- {
- Money = account.TotalAmt;
- }
- if(Money > 0)
- {
- OperateAmountChange(db, UserId, Money, 2, 1, "商城购机");
- OperateAmountChange(db, UserId, Money, 1, 2, "商城购机");
- }
- UserId = 0;
- }
- else
- {
- UserId = user.ParentUserId;
- }
- }
- else
- {
- UserId = 0;
- }
- }
- db.Dispose();
- }
- public void OperateReserveBack(int UserId, decimal Money)
- {
- OpModels.WebCMSEntities db = new OpModels.WebCMSEntities();
- OpModels.UserAccount account = db.UserAccount.FirstOrDefault(m => m.Id == UserId) ?? new OpModels.UserAccount();
- if(account.TotalAmt <= 0)
- {
- return;
- }
- if(account.TotalAmt < Money)
- {
- Money = account.TotalAmt;
- }
- if(Money > 0)
- {
- OperateAmountChange(db, UserId, Money, 2, 1, "商城购机");
- OperateAmountChange(db, UserId, Money, 1, 2, "商城购机");
- }
- db.Dispose();
- }
- public void ActReserveBack(int UserId, decimal OpReserve1, decimal OpReserve2, decimal OpReserve3)
- {
- OpModels.WebCMSEntities db = new OpModels.WebCMSEntities();
- if(OpReserve1 > 0) OperateAmountChange(db, UserId, OpReserve1, 1, 1, "机具激活", true);
- if(OpReserve2 > 0) OperateAmountChange(db, UserId, OpReserve2, 1, 2, "机具激活", true);
- if(OpReserve3 > 0) OperateAmountChange(db, UserId, OpReserve3, 1, 3, "机具激活", true);
- db.Dispose();
- }
- 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)
- {
- // BeforeAmount = account.TotalAmt;
- account.TotalAmt += Money;
- // AfterAmount = account.TotalAmt;
- }
- else if(AmountType == 2)
- {
- // BeforeAmount = account.ValidForGetAmount;
- account.ValidForGetAmount += Money;
- // AfterAmount = account.ValidForGetAmount;
- }
- else
- {
- // BeforeAmount = account.ValidAmount;
- account.ValidAmount += Money;
- // AfterAmount = account.ValidAmount;
- }
- }
- else
- {
- if(AmountType == 1)
- {
- // BeforeAmount = account.TotalAmt;
- account.TotalAmt -= Money;
- // AfterAmount = account.TotalAmt;
- }
- else if(AmountType == 2)
- {
- // BeforeAmount = account.ValidForGetAmount;
- account.ValidForGetAmount -= Money;
- // AfterAmount = account.ValidForGetAmount;
- }
- else
- {
- // BeforeAmount = account.ValidAmount;
- account.ValidAmount -= Money;
- // AfterAmount = account.ValidAmount;
- }
- }
- 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,
- BeforeAmount = BeforeAmount,
- UseAmount = Money,
- UserId = UserId,
- SeoDescription = Remark,
- Version = AmountType,
- }).Entity;
- }
- db.SaveChanges();
- }
- #endregion
- #region 盟主推荐奖励
- public void LeaderRecommendPrize(WebCMSEntities db, Orders order, int UserId, decimal Money, int ChangeType)
- {
- int level = 0;
- while(UserId > 0)
- {
- level += 1;
- Users user = db.Users.FirstOrDefault(m => m.Id == UserId);
- if(user != null)
- {
- if((user.UserType == 1 || user.LeaderLevel > 0) && level > 1)
- {
- ChangeAccount(db, order, UserId, Money, ChangeType);
- UserId = 0;
- }
- else
- {
- UserId = user.ParentUserId;
- }
- }
- else
- {
- UserId = 0;
- }
- }
- db.Dispose();
- }
- #endregion
- #region 购买盟主预设职级
- public void LeaderPreUserLevel(WebCMSEntities db, int UserId, int LeaderKind)
- {
- int Month = LeaderKind == 1 ? 6 : 12;
- int Rank = LeaderKind == 1 ? 5 : 7;
- UserRankWhite check = db.UserRankWhite.FirstOrDefault(m => m.Id == UserId);
- if(check == null)
- {
- db.UserRankWhite.Add(new UserRankWhite()
- {
- CreateDate = DateTime.Now, //设置时间
- UpdateDate = DateTime.Now.AddMonths(Month), //过期时间
- Rank = Rank,
- UserId = UserId, //用户
- Id = UserId,
- });
- }
- else
- {
- if(check.Rank < Rank)
- {
- check.Rank = Rank;
- check.UpdateDate = DateTime.Now.AddMonths(Month);
- }
- else if(check.Rank == Rank && check.UpdateDate < DateTime.Now.AddMonths(Month))
- {
- check.UpdateDate = DateTime.Now.AddMonths(Month);
- }
- }
- db.SaveChanges();
- }
- #endregion
- }
- }
|