123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592 |
- 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;
- 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 + 1 >= 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;
- }
- }
- }
- db.SaveChanges();
- if(order.ParentOrderId > 0)
- {
- return;
- }
- OrderProduct pro = db.OrderProduct.FirstOrDefault(m => m.OrderId == OrderId);
- if (pro != null)
- {
- 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 == 30 || pro.ProductId == -2)
- {
- order.Status = 2;
- int BuyCount = pro.ProductCount;
- int Kind = 0;
- if (pro.ProductId == 10 || pro.ProductId == 30)
- {
- 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)
- {
- user.LeaderLevel = 1;
- }
- }
- else if (pro.ProductId == 27)
- {
- Users user = db.Users.FirstOrDefault(m => m.Id == order.UserId && m.LeaderLevel < 2);
- if(user != null)
- {
- user.LeaderLevel = 2;
- }
- }
- if(Kind <= 2 && (pro.ProductId == 10 || pro.ProductId == 11 || pro.ProductId == 30 || 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 == 30 || 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 == 30 || 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)
- {
- //充值盟主储备金
- OpReserve(db, order, order.UserId, order.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)
- {
- bool directPrize = false; //直推奖标记
- bool bigLeaderPrize = false; //大盟主券标记
- bool buyPrize = false; //返100购机奖励标记
- int leaderFlag = 0; //返600备用金标记
- 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, 2, 0, "商城购机");
- //返回到余额
- // OpAccount(db, order, order.UserId, order.TotalPrice / pro.ProductCount, pro.ProductCount);
- }
- else
- {
- //扣减备用金
- OpReserve(db, order, order.UserId, order.TotalPrice, 2, 0, "商城购机");
- //返回到余额
- OpLeaderAccount(db, order, order.UserId, order.TotalPrice / pro.ProductCount, pro.ProductCount);
- }
- }
- //获得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(user.LeaderLevel == 2)
- {
- if(couponIds.Count > 0)
- {
- foreach(int couponId in couponIds)
- {
- PosCoupons coupon = db.PosCoupons.FirstOrDefault(m => m.Id == couponId);
- if(coupon != null)
- {
- coupon.LeaderUserId = user.Id;
- }
- }
- db.SaveChanges();
- }
- }
- else
- {
- int PUserId = user.ParentUserId;
- while(PUserId > 0)
- {
- Users puser = db.Users.FirstOrDefault(m => m.Id == PUserId) ?? new Users();
- if(puser.LeaderLevel == 2)
- {
- UserAccount acc = db.UserAccount.FirstOrDefault(m => m.Id == puser.Id) ?? new UserAccount();
- if(acc.LeaderReserve >= order.TotalPrice)
- {
- //扣减备用金
- OpReserve(db, order, puser.Id, order.TotalPrice, 2, order.UserId, "购机奖励");
- //返回到余额
- OpLeaderAccount(db, order, puser.Id, order.TotalPrice / pro.ProductCount, pro.ProductCount);
- }
- PUserId = 0;
- }
- else
- {
- PUserId = puser.ParentUserId;
- }
- }
- }
- return;
- }
- int ParentUserId = user.ParentUserId;
- // List<int> proids = new List<int>();
- // proids.Add(10);
- // proids.Add(11);
- 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 >= order.TotalPrice && puser.LeaderLevel > leaderFlag)
- {
- //扣减备用金
- OpReserve(db, order, puser.Id, order.TotalPrice, 2, order.UserId, "购机奖励");
- //返回到余额
- OpLeaderAccount(db, order, puser.Id, order.TotalPrice / pro.ProductCount, pro.ProductCount);
- leaderFlag = puser.LeaderLevel;
- }
- //如果是大盟主,则标记大盟主标签
- 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;
- }
- }
- ParentUserId = puser.ParentUserId;
- }
- }
- }
- 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 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 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 == 600)
- {
- 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);
- }
- #endregion
- }
- }
|