123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161 |
- using System;
- using System.Collections.Generic;
- using System.Linq;
- using MySystem.SpModels;
- using Library;
- using LitJson;
- using System.Threading;
- namespace MySystem
- {
- public class SycnSpBindService
- {
- public readonly static SycnSpBindService Instance = new SycnSpBindService();
- private SycnSpBindService()
- { }
- 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(-5);
- var Binds = spdb.BindRecord.Select(m => new { m.Id, m.CreateTime, m.Status, m.MerNo, m.MerSnNo, m.Field1 }).Where(m => m.CreateTime >= start && m.Status == 1).OrderByDescending(m => m.Id).ToList();
- foreach (var Bind in Binds)
- {
- var tran = db.Database.BeginTransaction();
- try
- {
- if(Bind.Field1 == "解绑")
- {
- string PosSn = Bind.MerSnNo;
- string MerNo = Bind.MerNo;
- PxcModels.MachineForSnNo forSnNo = db.MachineForSnNo.FirstOrDefault(m => m.SnNo == PosSn);
- PxcModels.MachineForMerNo forMerNo = db.MachineForMerNo.FirstOrDefault(m => m.MerNo == MerNo);
- PxcModels.PosMachinesTwo pos = db.PosMachinesTwo.FirstOrDefault(m => m.Id == forSnNo.SnId);
- if (pos.ActivationState == 0 && pos.BuyUserId > 0 && pos.BindingState == 0)
- {
- if (forMerNo != null)
- {
- db.MachineForMerNo.Remove(forMerNo);
- db.SaveChanges();
- }
- db.MachineUnBind.Add(new PxcModels.MachineUnBind()
- {
- CreateDate = DateTime.Now,
- MerchantId = pos.BindMerchantId,
- AuditDate = DateTime.Now,
- AuditDesc = "金控推送解绑",
- AuditStatus = 1,
- SnNo = pos.PosSn,
- BrandId = pos.BrandId,
- UserId = pos.BuyUserId,
- ApplyNo = "U" + DateTime.Now.ToString("yyyyMMddHHmmssfff") + function.get_Random(8),
- });
- pos.BindMerchantId = 0;
- pos.BindingState = 0;
- pos.BindingTime = DateTime.Parse("1900-01-01");
- pos.UserId = pos.BuyUserId;
- string IdBrand = pos.BuyUserId + "_" + pos.BrandId;
- PxcModels.UserMachineData userData = db.UserMachineData.FirstOrDefault(m => m.IdBrand == IdBrand);
- if (userData == null)
- {
- userData = db.UserMachineData.Add(new PxcModels.UserMachineData()
- {
- IdBrand = IdBrand,
- }).Entity;
- db.SaveChanges();
- }
- userData.BindCount -= 1;
- userData.UnBindCount += 1;
- db.SaveChanges();
- BindRecord edit = spdb.BindRecord.FirstOrDefault(m => m.Id == Bind.Id);
- if (edit != null)
- {
- edit.Status = 2;
- spdb.SaveChanges();
- }
- }
- }
- else
- {
- PxcModels.MachineForSnNo posFor = db.MachineForSnNo.FirstOrDefault(m => m.SnNo == Bind.MerSnNo) ?? new PxcModels.MachineForSnNo();
- PxcModels.PosMachinesTwo pos = db.PosMachinesTwo.FirstOrDefault(m => m.Id == posFor.SnId);
- if (pos != null)
- {
- pos.BindingState = 1;
- pos.BindingTime = Bind.CreateTime;
- pos.Status = 0;
- PxcModels.MachineForMerNo merFor = db.MachineForMerNo.FirstOrDefault(m => m.MerNo == Bind.MerNo);
- if (merFor == null)
- {
- merFor = db.MachineForMerNo.Add(new PxcModels.MachineForMerNo()
- {
- MerNo = Bind.MerNo,
- SnId = pos.Id,
- }).Entity;
- }
- else
- {
- merFor.SnId = pos.Id;
- }
- db.SaveChanges();
- RedisDbconn.Instance.Set("PosMachinesTwo", pos);
- BindRecord edit = spdb.BindRecord.FirstOrDefault(m => m.Id == Bind.Id);
- if (edit != null)
- {
- edit.Status = 2;
- spdb.SaveChanges();
- }
- string IdBrand = pos.UserId + "_" + pos.BrandId;
- PxcModels.UserMachineData MachineData = db.UserMachineData.FirstOrDefault(m => m.IdBrand == IdBrand);
- if (MachineData == null)
- {
- MachineData = db.UserMachineData.Add(new PxcModels.UserMachineData()
- {
- IdBrand = IdBrand,
- }).Entity;
- db.SaveChanges();
- }
- MachineData.BindCount += 1;
- MachineData.UnBindCount -= 1;
- db.SaveChanges();
- RedisDbconn.Instance.Set("UserMachineData:" + IdBrand, MachineData);
- }
- }
- tran.Commit();
- }
- catch (Exception ex)
- {
- tran.Rollback();
- function.WriteLog(DateTime.Now.ToString() + "\n" + ex.ToString() + "\n" + Bind.Id, "同步SP绑定数据到MAIN异常");
- }
- tran.Dispose();
- }
- spdb.SaveChanges();
- spdb.Dispose();
- db.SaveChanges();
- db.Dispose();
- }
- catch (Exception ex)
- {
- function.WriteLog(DateTime.Now.ToString() + "\n" + ex.ToString(), "同步SP绑定数据到MAIN异常");
- }
- Thread.Sleep(1000);
- }
- }
- }
- }
|