using System; using System.Collections.Generic; using System.Linq; using MySystem.SpModels; using Library; using LitJson; using System.Threading; namespace MySystem { public class SycnSpTradeService { public readonly static SycnSpTradeService Instance = new SycnSpTradeService(); private SycnSpTradeService() { } public void Start() { Thread th = new Thread(StartDo); th.IsBackground = true; th.Start(); } public void StartDo() { while (true) { try { WebCMSEntities spdb = new WebCMSEntities(); PxcModels.WebCMSEntities db = new PxcModels.WebCMSEntities(); DateTime start = DateTime.Now.AddDays(-10); int StartId = int.Parse(function.CheckInt(function.ReadInstance("/SycnSp/TradeRecordId.txt"))); IQueryable trades = spdb.TradeRecord.Where(m => m.Id >= StartId && m.CreateDate >= start && m.Status == 1).OrderBy(m => m.CreateDate); foreach (TradeRecord trade in trades.ToList()) { bool op = true; if (trade.SerEntryMode == "N" && trade.ProductType == "1") //金控押金获取 { if (trade.TradeAmount == 9900 || trade.TradeAmount == 19900 || trade.TradeAmount == 29900) { op = false; PxcModels.MachineForSnNo posFor = db.MachineForSnNo.FirstOrDefault(m => m.SnNo == trade.TradeSnNo) ?? new PxcModels.MachineForSnNo(); PxcModels.PosMachinesTwo pos = db.PosMachinesTwo.FirstOrDefault(m => m.Id == posFor.SnId); if (pos != null) { pos.SeoKeyword = trade.TradeAmount.ToString("f2"); // pos.IsPurchase = 0; db.SaveChanges(); } TradeRecord edit = spdb.TradeRecord.FirstOrDefault(m => m.Id == trade.Id); if (edit != null) { edit.Status = 2; } spdb.SaveChanges(); } } else if (trade.SerEntryMode == "1" && trade.ProductType == "10") //联动押金获取 { if (trade.TradeAmount == 99 || trade.TradeAmount == 199 || trade.TradeAmount == 299) { op = false; PxcModels.MachineForSnNo posFor = db.MachineForSnNo.FirstOrDefault(m => m.SnNo == trade.TradeSnNo) ?? new PxcModels.MachineForSnNo(); PxcModels.PosMachinesTwo pos = db.PosMachinesTwo.FirstOrDefault(m => m.Id == posFor.SnId); if (pos != null) { pos.SeoKeyword = trade.TradeAmount.ToString("f2"); db.SaveChanges(); } TradeRecord edit = spdb.TradeRecord.FirstOrDefault(m => m.Id == trade.Id); if (edit != null) { edit.Status = 2; } spdb.SaveChanges(); } } if (trade.DiscountRateFlag == "True") { op = false; TradeRecord edit = spdb.TradeRecord.FirstOrDefault(m => m.Id == trade.Id); if (edit != null) { edit.Status = 2; } spdb.SaveChanges(); } if(trade.ProductType == "7" && trade.TradeStatus != "00") { op = false; TradeRecord edit = spdb.TradeRecord.FirstOrDefault(m => m.Id == trade.Id); if (edit != null) { edit.Status = 2; } spdb.SaveChanges(); } if (op) { PxcModels.MachineForMerNo posFor = db.MachineForMerNo.FirstOrDefault(m => m.MerNo == trade.MerNo) ?? new PxcModels.MachineForMerNo(); PxcModels.PosMachinesTwo pos = db.PosMachinesTwo.FirstOrDefault(m => m.Id == posFor.SnId); if (pos != null) { if (pos.BindMerchantId > 0) { PxcModels.Users user = db.Users.FirstOrDefault(m => m.Id == pos.UserId) ?? new PxcModels.Users(); int TopUserId = 0; if (!string.IsNullOrEmpty(user.ParentNav)) { TopUserId = int.Parse(user.ParentNav.Trim(',').Replace(",,", ",").Split(',')[0]); } bool check = db.SpOrderNos.Any(m => m.OrderNo == trade.TradeSerialNo); if (!check) { decimal TradeAmount = trade.TradeAmount; int BankCardType = -1; int QrPayFlag = 0; int VipFlag = 0; int PayType = 0; if (pos.BrandId == 1 || pos.BrandId == 3) { TradeAmount = TradeAmount / 100; BankCardType = int.Parse(trade.BankCardType); if (trade.TradeType == "02") QrPayFlag = 1; else if (trade.TradeType == "NQ") PayType = 1; if (trade.SettleMethod == "E") VipFlag = 1; } else if (pos.BrandId == 2) { if (trade.TradeType == "DEBIT_BY_CARD") { BankCardType = 0; } else { BankCardType = 1; } if (trade.TradeType == "CLOUD_PAY") QrPayFlag = 1; } else if (pos.BrandId == 4 || pos.BrandId == 5) { TradeAmount = TradeAmount / 100; if (trade.BankCardType == "100") { BankCardType = 1; } else if (trade.BankCardType == "200") { BankCardType = 0; } if (trade.TradeType == "200") QrPayFlag = 1; if (trade.Field1 == "1") VipFlag = 1; } else if (pos.BrandId == 6) { TradeAmount = TradeAmount / 100; if (trade.BankCardType == "01" || trade.BankCardType == "11") { BankCardType = 0; } else { BankCardType = 1; } } else if (pos.BrandId == 7) { TradeAmount = TradeAmount / 100; if (trade.BankCardType == "OA" || trade.BankCardType == "CC" || trade.BankCardType == "SCC") { BankCardType = 1; } else { BankCardType = 0; } } else if (pos.BrandId == 8 || pos.BrandId == 9) { TradeAmount = TradeAmount / 100; if (trade.BankCardType == "100") { BankCardType = 1; } else if (trade.BankCardType == "200") { BankCardType = 0; } if (trade.TradeType == "200") QrPayFlag = 1; if (trade.Field1 == "1") VipFlag = 1; } else if (pos.BrandId == 10 || pos.BrandId == 11) { if (trade.BankCardType == "01") { BankCardType = 1; } else if (trade.BankCardType == "00") { BankCardType = 0; } if (trade.TradeType == "F010") QrPayFlag = 1; PxcModels.PosMerchantInfo merchant = db.PosMerchantInfo.FirstOrDefault(m => m.Id == pos.BindMerchantId); if(merchant != null) { if(string.IsNullOrEmpty(merchant.MerIdcardNo) && !string.IsNullOrEmpty(trade.Field2)) { merchant.MerIdcardNo = trade.Field2.ToUpper(); //商户身份证号 } if(!string.IsNullOrEmpty(trade.Field1)) { merchant.MerchantMobile = trade.Field1; //商户手机号 } if(!string.IsNullOrEmpty(merchant.MerIdcardNo)) { string startNo = merchant.MerIdcardNo.Substring(0, 6); string endNo = merchant.MerIdcardNo.Substring(merchant.MerIdcardNo.Length - 4, 4).ToUpper(); string Name = merchant.MerchantName; Name = Name.Replace("*", ""); function.WriteLog(DateTime.Now.ToString() + "-----startNo:" + startNo + ",endNo:" + endNo + ",Name:" + Name, "监控机具是否互斥"); bool chk = db.PosMerchantInfo.Any(m => m.MerIdcardNo.StartsWith(startNo) && m.MerIdcardNo.EndsWith(endNo) && m.MerchantName.Contains(Name)); if(chk) { pos.IsFirst = 0; } } db.SaveChanges(); } } int BrandId = pos.BrandId; // int BrandId = int.Parse(trade.ProductType); // if (BrandId == 1 && trade.MerNo.StartsWith("M900")) // { // BrandId = 3; // } // if (BrandId == 4 && trade.DeviceType == "200") // { // BrandId = 5; // } decimal SourceTradeAmount = TradeAmount; if (pos.Sort == 0 && pos.BrandId == 7) { decimal deposit = decimal.Parse(function.CheckNum(pos.SeoKeyword)); if (deposit > 0) { TradeAmount -= deposit; pos.Sort = 1; } } decimal CheckMoney = 1000; int CheckDays = 30; if (pos.BrandId == 6) { CheckMoney = 5000; } //联动 if(BankCardType == 1 && trade.TradeType == "M015" && (pos.BrandId == 10 || pos.BrandId == 11)) { pos.CreditTrade += SourceTradeAmount; } //金控 else if((BankCardType == 1 || BankCardType == 2) && (pos.BrandId == 1 || pos.BrandId == 3)) { pos.CreditTrade += SourceTradeAmount; } //开店宝 else if (BankCardType == 1 && pos.BrandId == 2) { pos.CreditTrade += SourceTradeAmount; } //乐刷or海科 else if (BankCardType == 1 && (trade.TradeType == "100" || trade.TradeType == "200") && (pos.BrandId == 4 || pos.BrandId == 5 || pos.BrandId == 8 || pos.BrandId == 9)) { pos.CreditTrade += SourceTradeAmount; } //立刷 else if(BankCardType == 1 && trade.Field2 == "0104" && pos.BrandId == 6) { pos.CreditTrade += SourceTradeAmount; } //盛付通 else if(BankCardType == 1 && trade.TradeType == "PUR" && pos.BrandId == 7) { pos.CreditTrade += SourceTradeAmount; } else if (BankCardType == 0) { pos.DebitCardTrade += TradeAmount; } //判断激活条件并激活 if (pos.CreditTrade >= CheckMoney && pos.ActivationState == 0 && pos.TransferTime < pos.BindingTime && pos.BindingTime > DateTime.Now.AddDays(-CheckDays)) { pos.ActivationState = 1; pos.ActivationTime = DateTime.Now; PxcModels.PosMerchantInfo merchant = db.PosMerchantInfo.FirstOrDefault(m => m.Id == pos.BindMerchantId); if (merchant != null) { merchant.ActiveStatus = 1; merchant.MerStandardDate = DateTime.Now; db.SaveChanges(); // 推荐王逻辑(激活) string SendData = "{\"Kind\":\"3\",\"Data\":{\"UserId\":\"" + pos.BuyUserId + "\",\"PosId\":\"" + pos.Id + "\",\"TradeMonth\":\"" + DateTime.Now.ToString("yyyyMM") + "\"}}"; RedisDbconn.Instance.AddList("RecommendActStatQueue", SendData); //发放开机奖励 if(pos.IsFirst == 1) { RedisDbconn.Instance.AddList("OpenRewardQueue", pos.Id.ToString()); } } //发放大盟主奖励 if(pos.LeaderUserId > 0) { RedisDbconn.Instance.AddList("LeaderPrizeQueue", pos.Id); } //发放运营中心奖励 RedisDbconn.Instance.AddList("OperatePrizeQueue", pos.Id); AlipayPayBack2Service.Instance.ActReserveBack(pos.OpId, pos.OpReserve1, pos.OpReserve2, pos.OpReserve3); } if (TradeAmount > 0) { ulong MerHelpFlag = 0; decimal DepositAmt = decimal.Parse(function.CheckNum(pos.SeoKeyword)); if (pos.DownFeeFlag == 1) { MerHelpFlag = 2; } else if (DepositAmt > 0 && pos.IsFirst == 1) { string DayString = RedisDbconn.Instance.Get("pobjrule:" + pos.BrandId + ":HelpPolicy:Days"); int Days = int.Parse(DayString); //天数 if (pos.BindingTime != null) { if (pos.BindingTime.Value.AddDays(Days) > DateTime.Now) { MerHelpFlag = 1; } } } if((pos.BrandId == 10 || pos.BrandId == 11) && trade.TradeType == "T015" && MerHelpFlag == 1) { MerHelpFlag = 0; } int CapFlag = 0; if(BankCardType == 0 && TradeAmount >= 1666.67M) { CapFlag = 1; } string HelpProfitFlag = ""; bool checkHelpProfit = db.HelpProfitMerchantForUser.Any(m => m.MerchantId == pos.BindMerchantId); if(checkHelpProfit) { HelpProfitFlag = "HelpProfit"; } db.TradeRecord.Add(new PxcModels.TradeRecord() { ParentNav = user.ParentNav, CreateDate = trade.CreateDate, UpdateDate = trade.UpdateDate, RecordNo = trade.TradeSerialNo, //单号 UserId = pos.UserId, //创客 MerchantId = pos.BindMerchantId, //商户 MerNo = trade.MerNo, //渠道商户编号 MerHelpFlag = MerHelpFlag, //1扶持周,0稳定期,2盈利期 HelpMonthCount = 0, //扶持第几个月 MerBuddyType = user.MerchantType, //商户创客类型 SnNo = trade.TradeSnNo, //渠道SN号 TradeSerialNo = trade.ChannelSerial, //交易流水号 TradeAmount = TradeAmount, //交易金额 BankCardType = BankCardType, //银行卡类型 QrPayFlag = QrPayFlag, //云闪付标识 CapFlag = CapFlag, //借记卡是否封顶 VipFlag = VipFlag, //会员标记 PayType = PayType, //支付方式 BrandId = BrandId, //品牌 Remark = trade.Remark, //备注 TopUserId = TopUserId, //顶级创客 MerUserId = pos.UserId, //商户直属创客 SeoTitle = HelpProfitFlag, Version = MerHelpFlag == 2 ? 1 : 0, ActStatus = pos.ActivationState, }); } // 立刷60天内刷满10000,再返40 if (pos.BrandId == 6 && decimal.Parse(function.CheckNum(pos.SeoKeyword)) > 0 && pos.ActivationState == 1 && pos.CreditTrade >= 10000 && pos.BindingTime > DateTime.Now.AddDays(-60)) { // PxcModels.PosMerchantInfo merchant = db.PosMerchantInfo.FirstOrDefault(m => m.Id == pos.BindMerchantId && m.Sort == 1); PxcModels.PosMerchantInfo merchant = db.PosMerchantInfo.FirstOrDefault(m => m.Id == pos.BindMerchantId); if(merchant != null) { PxcModels.Users getUser = db.Users.FirstOrDefault(m => m.Id == pos.BuyUserId) ?? new PxcModels.Users(); int BuyTopUserId = 0; string ParentNav = getUser.ParentNav; if (!string.IsNullOrEmpty(ParentNav)) { string[] ParentNavList = ParentNav.Trim(',').Replace(",,", ",").Split(','); if (ParentNavList.Length > 1) { BuyTopUserId = int.Parse(ParentNavList[1]); } else if (ParentNavList.Length == 1) { BuyTopUserId = int.Parse(ParentNavList[0]); } } StatService.Instance.doActiveReward(db, merchant, pos, getUser.Id, getUser.ParentNav, BuyTopUserId, 40, 12); } } db.SpOrderNos.Add(new PxcModels.SpOrderNos() { OrderNo = trade.TradeSerialNo }); db.SaveChanges(); // 推荐王逻辑(交易) if(TradeAmount > 0) { string Send = "{\"Kind\":\"4\",\"Data\":{\"PosId\":\"" + pos.Id + "\",\"TradeMonth\":\"" + trade.CreateDate.Value.ToString("yyyyMM") + "\",\"TradeAmount\":\"" + TradeAmount + "\"}}"; RedisDbconn.Instance.AddList("RecommendActStatQueue", Send); } } TradeRecord edit = spdb.TradeRecord.FirstOrDefault(m => m.Id == trade.Id); if (edit != null) { edit.Status = 2; } spdb.SaveChanges(); } } } } spdb.Dispose(); db.Dispose(); } catch (Exception ex) { function.WriteLog(DateTime.Now.ToString() + "\n" + ex.ToString(), "同步SP交易数据到MAIN异常"); } Thread.Sleep(1000); } } } }