123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767 |
- 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();
- }
- 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();
- RedisDbconn.Instance.Set("Orders:" + order.Id, order);
- //机具券逻辑
- OrderProduct pro = db.OrderProduct.FirstOrDefault(m => m.OrderId == OrderId);
- if (pro != null)
- {
- if (pro.ProductId == 10 || pro.ProductId == 11 || pro.ProductId == 12 || pro.ProductId == 13)
- {
- order.Status = 2;
- int BuyCount = pro.ProductCount;
- int Kind = 0;
- if (pro.ProductId == 10)
- {
- Kind = 1;
- }
- else if (pro.ProductId == 11)
- {
- Kind = 2;
- }
- else if (pro.ProductId == 12)
- {
- if (pro.NormDetail == "电签POS")
- {
- Kind = 1;
- }
- else if (pro.NormDetail == "大POS")
- {
- Kind = 2;
- }
-
- }
- else if (pro.ProductId == 13)
- {
- if (pro.NormDetail == "300电签POS")
- {
- Kind = 1;
- }
- else if (pro.NormDetail == "200大POS")
- {
- Kind = 2;
- }
- else if (pro.NormDetail == "50大机券+225电签券")
- {
- Kind = 3;
- }
- else if (pro.NormDetail == "100大机券+150电签券")
- {
- Kind = 4;
- }
- else if (pro.NormDetail == "150大机券+75电签券")
- {
- Kind = 5;
- }
- }
- db.SaveChanges();
- string Codes = "";
- if(Kind > 2)
- {
- Dictionary<int, int> couponData = new Dictionary<int, int>();
- if(Kind == 3)
- {
- couponData.Add(1, 225); //电签数量
- couponData.Add(2, 50); //大机数量
- }
- else if(Kind == 4)
- {
- couponData.Add(1, 150); //电签数量
- couponData.Add(2, 100); //大机数量
- }
- else if(Kind == 5)
- {
- couponData.Add(1, 75); //电签数量
- couponData.Add(2, 150); //大机数量
- }
- foreach(int kindNum in couponData.Keys)
- {
- int BuyCountByKind = couponData[kindNum];
- var coupons = db.PosCoupons.Where(m => m.IsLock == 0 && m.IsUse == 0 && m.UserId == 0 && m.QueryCount == kindNum).OrderBy(m => m.Id).Take(BuyCountByKind).ToList();
- foreach (var coupon in coupons)
- {
- 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 == 13)
- {
- item.LeaderUserId = order.UserId;
- }
- Codes += item.ExchangeCode + ",";
- }
- }
- }
- }
- else
- {
- var coupons = db.PosCoupons.Where(m => m.IsLock == 0 && m.IsUse == 0 && m.UserId == 0 && m.QueryCount == Kind).OrderBy(m => m.Id).Take(BuyCount).ToList();
- foreach (var coupon in coupons)
- {
- 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 == 13)
- {
- item.LeaderUserId = order.UserId;
- }
- Codes += item.ExchangeCode + ",";
- }
- }
- }
- order.SnNos = Codes.TrimEnd(',');
- string ChangeRecordNo = "SC" + DateTime.Now.ToString("yyyyMMddHHmmssfff") + function.get_Random(8);
- PosCouponForUser forUser = db.PosCouponForUser.FirstOrDefault(m => m.Id == order.UserId);
- if (forUser == null)
- {
- forUser = db.PosCouponForUser.Add(new PosCouponForUser()
- {
- Id = order.UserId,
- }).Entity;
- db.SaveChanges();
- }
- int BeforeOut = forUser.OutNum;
- int BeforeTotal = forUser.TotalNum;
- int BeforeStock = forUser.StockNum;
- forUser.TotalNum += BuyCount;
- forUser.StockNum += BuyCount;
- int AfterOut = forUser.OutNum;
- int AfterTotal = forUser.TotalNum;
- int AfterStock = forUser.StockNum;
- PosCouponOrders stat = db.PosCouponOrders.Add(new PosCouponOrders()
- {
- QueryCount = Kind,
- CreateDate = DateTime.Now,
- ChangeKind = 1,
- ChangeCount = BuyCount,
- AfterOut = AfterOut,
- AfterTotal = AfterTotal,
- AfterStock = AfterStock,
- BeforeOut = BeforeOut,
- BeforeTotal = BeforeTotal,
- BeforeStock = BeforeStock,
- OrderNo = ChangeRecordNo,
- ToUserId = order.UserId,
- FromUserId = 0,
- }).Entity;
- db.SaveChanges();
- RedisDbconn.Instance.Clear("Orders:" + order.Id);
- }
- //推荐下单奖励
- if (pro.ProductId == 10 || pro.ProductId == 11)
- {
- bool checkPrize = db.UserAccountRecord.Any(m => m.QueryCount == OrderId);
- int SelfBuy = 0;
- OtherMySqlConn.connstr = Library.ConfigurationManager.AppSettings["Pxc1SqlConnStr"].ToString();
- DataTable stat = OtherMySqlConn.dtable("select count(Id) from Orders where Id in (select OrderId from OrderProduct where UserId=" + order.UserId + " and (ProductId=10 or ProductId=11)) and Status>0 and TotalPrice>0");
- if (stat.Rows.Count > 0)
- {
- SelfBuy = int.Parse(function.CheckInt(stat.Rows[0][0].ToString()));
- }
- // OtherMySqlConn.connstr = ;
- function.WriteLog("OrderId:" + OrderId, "推荐下单奖励监控日志");
- function.WriteLog("checkPrize:" + checkPrize, "推荐下单奖励监控日志");
- function.WriteLog("SelfBuy:" + SelfBuy, "推荐下单奖励监控日志");
- if (SelfBuy == 1 && !checkPrize)
- {
- Users user = db.Users.FirstOrDefault(m => m.Id == order.UserId);
- if (user != null)
- {
- int ParentUserId = user.ParentUserId;
- while(ParentUserId > 0)
- {
- int machineCount = db.PosMachinesTwo.Count(m => m.UserId == ParentUserId && m.PosSnType == 0); //判断是否拥有3台购买机
- int ActiveCount = db.PosMachinesTwo.Count(m => m.UserId == ParentUserId && m.ActivationState == 1); //判断是否拥有3台激活机(不限购买或赠送)
- int couponCount = db.PosCoupons.Count(m => m.UserId == ParentUserId && m.IsUse == 0); //判断是否拥有3张券
- function.WriteLog("MakerCode:" + user.MakerCode, "推荐下单奖励监控日志");
- function.WriteLog("machineCount:" + machineCount, "推荐下单奖励监控日志");
- function.WriteLog("ActiveCount:" + ActiveCount, "推荐下单奖励监控日志");
- if (machineCount + ActiveCount + couponCount >= 3)
- {
- function.WriteLog("满足条件", "推荐下单奖励监控日志");
- UserAccount account = db.UserAccount.FirstOrDefault(m => m.Id == ParentUserId);
- if (account == null)
- {
- account = db.UserAccount.Add(new UserAccount()
- {
- Id = ParentUserId,
- UserId = ParentUserId,
- }).Entity;
- db.SaveChanges();
- }
- decimal BeforeTotalAmount = account.TotalAmount; //变更前总金额
- decimal BeforeFreezeAmount = account.FreezeAmount; //变更前冻结金额
- decimal BeforeBalanceAmount = account.BalanceAmount; //变更前余额
- account.BalanceAmount += 100;
- account.TotalAmount += 100;
- 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 = ParentUserId, //创客
- ChangeType = 112, //变动类型
- ChangeAmount = 100, //变更金额
- BeforeTotalAmount = BeforeTotalAmount, //变更前总金额
- AfterTotalAmount = AfterTotalAmount, //变更后总金额
- BeforeFreezeAmount = BeforeFreezeAmount, //变更前冻结金额
- AfterFreezeAmount = AfterFreezeAmount, //变更后冻结金额
- BeforeBalanceAmount = BeforeBalanceAmount, //变更前余额
- AfterBalanceAmount = AfterBalanceAmount, //变更后余额
- QueryCount = OrderId,
- }).Entity;
- db.SaveChanges();
- RedisDbconn.Instance.Set("UserAccount:" + ParentUserId, account);
- //推荐下单上级获得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();
- ParentUserId = 0;
- }
- else
- {
- Users puser = db.Users.FirstOrDefault(m => m.Id == ParentUserId) ?? new Users();
- ParentUserId = puser.ParentUserId;
- }
- }
- }
- }
- function.WriteLog("\r\n\r\n", "推荐下单奖励监控日志");
- }
- }
- }
- }
- public void DoOrder(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();
- RedisDbconn.Instance.Set("Orders:" + order.Id, order);
- //机具券逻辑
- OrderProduct pro = db.OrderProduct.FirstOrDefault(m => m.OrderId == OrderId);
- if (pro != null)
- {
- if (pro.ProductId == 10 || pro.ProductId == 11 || pro.ProductId == 12 || pro.ProductId == 13)
- {
- order.Status = 2;
- db.SaveChanges();
- int BuyCount = pro.ProductCount;
- int Kind = 0;
- if (pro.ProductId == 10)
- {
- Kind = 1;
- }
- else if (pro.ProductId == 11)
- {
- Kind = 2;
- }
- else if (pro.ProductId == 12)
- {
- if (pro.NormDetail == "电签POS")
- {
- Kind = 1;
- }
- else if (pro.NormDetail == "大POS")
- {
- Kind = 2;
- }
- }
- else if (pro.ProductId == 13)
- {
- if (pro.NormDetail == "300电签POS")
- {
- Kind = 1;
- }
- else if (pro.NormDetail == "200大POS")
- {
- Kind = 2;
- }
- else if (pro.NormDetail == "50大机券+225电签券")
- {
- Kind = 3;
- }
- else if (pro.NormDetail == "100大机券+150电签券")
- {
- Kind = 4;
- }
- else if (pro.NormDetail == "150大机券+75电签券")
- {
- Kind = 5;
- }
- }
- string Codes = "";
- if(Kind > 2)
- {
- Dictionary<int, int> couponData = new Dictionary<int, int>();
- if(Kind == 3)
- {
- couponData.Add(1, 225); //电签数量
- couponData.Add(2, 50); //大机数量
- }
- else if(Kind == 4)
- {
- couponData.Add(1, 150); //电签数量
- couponData.Add(2, 100); //大机数量
- }
- else if(Kind == 5)
- {
- couponData.Add(1, 75); //电签数量
- couponData.Add(2, 150); //大机数量
- }
- foreach(int kindNum in couponData.Keys)
- {
- int BuyCountByKind = couponData[kindNum];
- var coupons = db.PosCoupons.Where(m => m.IsLock == 0 && m.IsUse == 0 && m.UserId == 0 && m.QueryCount == kindNum).OrderBy(m => m.Id).Take(BuyCountByKind).ToList();
- foreach (var coupon in coupons)
- {
- 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 == 13)
- {
- item.LeaderUserId = order.UserId;
- }
- Codes += item.ExchangeCode + ",";
- }
- }
- }
- }
- else
- {
- var coupons = db.PosCoupons.Where(m => m.IsLock == 0 && m.IsUse == 0 && m.UserId == 0 && m.QueryCount == Kind).OrderBy(m => m.Id).Take(BuyCount).ToList();
- foreach (var coupon in coupons)
- {
- 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 == 13)
- {
- item.LeaderUserId = order.UserId;
- }
- Codes += item.ExchangeCode + ",";
- }
- }
- }
- order.SnNos = Codes.TrimEnd(',');
- string ChangeRecordNo = "SC" + DateTime.Now.ToString("yyyyMMddHHmmssfff") + function.get_Random(8);
- PosCouponForUser forUser = db.PosCouponForUser.FirstOrDefault(m => m.Id == order.UserId);
- if (forUser == null)
- {
- forUser = db.PosCouponForUser.Add(new PosCouponForUser()
- {
- Id = order.UserId,
- }).Entity;
- db.SaveChanges();
- }
- int BeforeOut = forUser.OutNum;
- int BeforeTotal = forUser.TotalNum;
- int BeforeStock = forUser.StockNum;
- forUser.TotalNum += BuyCount;
- forUser.StockNum += BuyCount;
- int AfterOut = forUser.OutNum;
- int AfterTotal = forUser.TotalNum;
- int AfterStock = forUser.StockNum;
- PosCouponOrders stat = db.PosCouponOrders.Add(new PosCouponOrders()
- {
- QueryCount = Kind,
- CreateDate = DateTime.Now,
- ChangeKind = 1,
- ChangeCount = BuyCount,
- AfterOut = AfterOut,
- AfterTotal = AfterTotal,
- AfterStock = AfterStock,
- BeforeOut = BeforeOut,
- BeforeTotal = BeforeTotal,
- BeforeStock = BeforeStock,
- OrderNo = ChangeRecordNo,
- ToUserId = order.UserId,
- FromUserId = 0,
- }).Entity;
- db.SaveChanges();
- RedisDbconn.Instance.Clear("Orders:" + order.Id);
- }
- //推荐下单奖励
- if (pro.ProductId == 10 || pro.ProductId == 11)
- {
- bool checkPrize = db.UserAccountRecord.Any(m => m.QueryCount == OrderId);
- int SelfBuy = 0;
- OtherMySqlConn.connstr = Library.ConfigurationManager.AppSettings["Pxc1SqlConnStr"].ToString();
- DataTable stat = OtherMySqlConn.dtable("select count(Id) from Orders where Id in (select OrderId from OrderProduct where UserId=" + order.UserId + " and (ProductId=10 or ProductId=11)) and Status>0 and TotalPrice>0");
- if (stat.Rows.Count > 0)
- {
- SelfBuy = int.Parse(function.CheckInt(stat.Rows[0][0].ToString()));
- }
- // OtherMySqlConn.connstr = ;
- function.WriteLog("OrderId:" + OrderId, "推荐下单奖励监控日志");
- function.WriteLog("checkPrize:" + checkPrize, "推荐下单奖励监控日志");
- function.WriteLog("SelfBuy:" + SelfBuy, "推荐下单奖励监控日志");
- if (SelfBuy == 1 && !checkPrize)
- {
- Users user = db.Users.FirstOrDefault(m => m.Id == order.UserId);
- if (user != null)
- {
- int ParentUserId = user.ParentUserId;
- while(ParentUserId > 0)
- {
- int machineCount = db.PosMachinesTwo.Count(m => m.UserId == ParentUserId && m.PosSnType == 0); //判断是否拥有3台购买机
- int ActiveCount = db.PosMachinesTwo.Count(m => m.UserId == ParentUserId && m.ActivationState == 1); //判断是否拥有3台激活机(不限购买或赠送)
- int couponCount = db.PosCoupons.Count(m => m.UserId == ParentUserId && m.IsUse == 0); //判断是否拥有3张券
- function.WriteLog("MakerCode:" + user.MakerCode, "推荐下单奖励监控日志");
- function.WriteLog("machineCount:" + machineCount, "推荐下单奖励监控日志");
- function.WriteLog("ActiveCount:" + ActiveCount, "推荐下单奖励监控日志");
- if (machineCount + ActiveCount + couponCount >= 3)
- {
- function.WriteLog("满足条件", "推荐下单奖励监控日志");
- UserAccount account = db.UserAccount.FirstOrDefault(m => m.Id == ParentUserId);
- if (account == null)
- {
- account = db.UserAccount.Add(new UserAccount()
- {
- Id = ParentUserId,
- UserId = ParentUserId,
- }).Entity;
- db.SaveChanges();
- }
- decimal BeforeTotalAmount = account.TotalAmount; //变更前总金额
- decimal BeforeFreezeAmount = account.FreezeAmount; //变更前冻结金额
- decimal BeforeBalanceAmount = account.BalanceAmount; //变更前余额
- account.BalanceAmount += 100;
- account.TotalAmount += 100;
- 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 = ParentUserId, //创客
- ChangeType = 112, //变动类型
- ChangeAmount = 100, //变更金额
- BeforeTotalAmount = BeforeTotalAmount, //变更前总金额
- AfterTotalAmount = AfterTotalAmount, //变更后总金额
- BeforeFreezeAmount = BeforeFreezeAmount, //变更前冻结金额
- AfterFreezeAmount = AfterFreezeAmount, //变更后冻结金额
- BeforeBalanceAmount = BeforeBalanceAmount, //变更前余额
- AfterBalanceAmount = AfterBalanceAmount, //变更后余额
- QueryCount = OrderId,
- }).Entity;
- db.SaveChanges();
- RedisDbconn.Instance.Set("UserAccount:" + ParentUserId, account);
- //推荐下单上级获得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();
- ParentUserId = 0;
- }
- else
- {
- Users puser = db.Users.FirstOrDefault(m => m.Id == ParentUserId) ?? new Users();
- ParentUserId = puser.ParentUserId;
- }
- }
- }
- }
- function.WriteLog("\r\n\r\n", "推荐下单奖励监控日志");
- }
- }
- }
- }
- public void checkOrder(int OrderId)
- {
- WebCMSEntities db = new WebCMSEntities();
- DoOrder(db, OrderId);
- db.Dispose();
- }
- public void addcoupon(int OrderId)
- {
- WebCMSEntities db = new WebCMSEntities();
- Orders order = db.Orders.FirstOrDefault(m => m.Id == OrderId);
- if (order != null)
- {
- //机具券逻辑
- OrderProduct pro = db.OrderProduct.FirstOrDefault(m => m.OrderId == OrderId);
- if (pro != null)
- {
- if (pro.ProductId == 10 || pro.ProductId == 11 || pro.ProductId == 12 || pro.ProductId == 13)
- {
- int BuyCount = pro.ProductCount;
- int Kind = 0;
- if (pro.ProductId == 10)
- {
- Kind = 1;
- }
- else if (pro.ProductId == 11)
- {
- Kind = 2;
- }
- else if (pro.ProductId == 12)
- {
- if (pro.NormDetail == "电签POS")
- {
- Kind = 1;
- }
- else if (pro.NormDetail == "大POS")
- {
- Kind = 2;
- }
- }
- else if (pro.ProductId == 13)
- {
- if (pro.NormDetail == "电签POS")
- {
- Kind = 1;
- }
- else if (pro.NormDetail == "大POS")
- {
- Kind = 2;
- }
- }
- 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(BuyCount).ToList();
- foreach (var coupon in coupons)
- {
- 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);
- Codes += item.ExchangeCode + ",";
- }
- }
- order.SnNos = Codes.TrimEnd(',');
- string ChangeRecordNo = "SC" + DateTime.Now.ToString("yyyyMMddHHmmssfff") + function.get_Random(8);
- PosCouponForUser forUser = db.PosCouponForUser.FirstOrDefault(m => m.Id == order.UserId);
- if (forUser == null)
- {
- forUser = db.PosCouponForUser.Add(new PosCouponForUser()
- {
- Id = order.UserId,
- }).Entity;
- db.SaveChanges();
- }
- int BeforeOut = forUser.OutNum;
- int BeforeTotal = forUser.TotalNum;
- int BeforeStock = forUser.StockNum;
- forUser.TotalNum += BuyCount;
- forUser.StockNum += BuyCount;
- int AfterOut = forUser.OutNum;
- int AfterTotal = forUser.TotalNum;
- int AfterStock = forUser.StockNum;
- PosCouponOrders stat = db.PosCouponOrders.Add(new PosCouponOrders()
- {
- QueryCount = Kind,
- CreateDate = DateTime.Now,
- ChangeKind = 1,
- ChangeCount = BuyCount,
- AfterOut = AfterOut,
- AfterTotal = AfterTotal,
- AfterStock = AfterStock,
- BeforeOut = BeforeOut,
- BeforeTotal = BeforeTotal,
- BeforeStock = BeforeStock,
- OrderNo = ChangeRecordNo,
- ToUserId = order.UserId,
- FromUserId = 0,
- }).Entity;
- db.SaveChanges();
- RedisDbconn.Instance.Clear("Orders:" + order.Id);
- }
- //推荐下单奖励
- if (pro.ProductId == 10 || pro.ProductId == 11)
- {
- int SelfBuy = db.OrderProduct.Count(m => m.UserId == order.UserId && m.OrderId != OrderId && (m.ProductId == 10 || m.ProductId == 11));
- if (SelfBuy == 1)
- {
- Users user = db.Users.FirstOrDefault(m => m.Id == order.UserId);
- if (user != null)
- {
- int ParentUserId = user.ParentUserId;
- int machineCount = db.PosMachinesTwo.Count(m => m.UserId == ParentUserId && m.PosSnType == 0); //判断是否拥有3台购买机
- int ActiveCount = db.PosMachinesTwo.Count(m => m.UserId == ParentUserId && m.ActivationState == 1); //判断是否拥有3台激活机(不限购买或赠送)
- int couponCount = db.PosCoupons.Count(m => m.UserId == ParentUserId && m.IsUse == 0); //判断是否拥有3张券
- if (machineCount + ActiveCount + couponCount >= 3)
- {
- UserAccount account = db.UserAccount.FirstOrDefault(m => m.Id == ParentUserId);
- if (account == null)
- {
- account = db.UserAccount.Add(new UserAccount()
- {
- Id = ParentUserId,
- UserId = ParentUserId,
- }).Entity;
- db.SaveChanges();
- }
- decimal BeforeTotalAmount = account.TotalAmount; //变更前总金额
- decimal BeforeFreezeAmount = account.FreezeAmount; //变更前冻结金额
- decimal BeforeBalanceAmount = account.BalanceAmount; //变更前余额
- account.BalanceAmount += 100;
- account.TotalAmount += 100;
- 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 = ParentUserId, //创客
- ChangeType = 112, //变动类型
- ChangeAmount = 100, //变更金额
- BeforeTotalAmount = BeforeTotalAmount, //变更前总金额
- AfterTotalAmount = AfterTotalAmount, //变更后总金额
- BeforeFreezeAmount = BeforeFreezeAmount, //变更前冻结金额
- AfterFreezeAmount = AfterFreezeAmount, //变更后冻结金额
- BeforeBalanceAmount = BeforeBalanceAmount, //变更前余额
- AfterBalanceAmount = AfterBalanceAmount, //变更后余额
- }).Entity;
- db.SaveChanges();
- RedisDbconn.Instance.Set("UserAccount:" + ParentUserId, account);
- //推荐下单上级获得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);
- 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();
- }
- }
- }
- }
- }
- }
- db.Dispose();
- }
- }
- }
|