using System; using System.Threading; using System.Linq; using System.Data; using Library; using MySystem.Models; using System.Collections.Generic; namespace MySystem { public class TestHelper { public readonly static TestHelper Instance = new TestHelper(); private TestHelper() { } public void Start()//启动 { Thread thread = new Thread(ImportPostDo); thread.IsBackground = true; thread.Start(); Thread thread2 = new Thread(ImportPost2Do); thread2.IsBackground = true; thread2.Start(); Thread thread3 = new Thread(ImportPost3Do); thread3.IsBackground = true; thread3.Start(); Thread thread4 = new Thread(ImportVirtualCard); thread4.IsBackground = true; thread4.Start(); } public void ImportPostDo() { while (true) { string data = RedisDbconn.Instance.RPop("MakerCouponsQueue:1"); if (!string.IsNullOrEmpty(data)) { int count = int.Parse(function.CheckInt(data)); if (count > 0) { MakerCoupons(1, count); } } else { Thread.Sleep(10000); } } } public void ImportPost2Do() { while (true) { string data = RedisDbconn.Instance.RPop("MakerCouponsQueue:2"); if (!string.IsNullOrEmpty(data)) { int count = int.Parse(function.CheckInt(data)); if (count > 0) { MakerCoupons(2, count); } } else { Thread.Sleep(10000); } } } public void ImportPost3Do() { while (true) { string data = RedisDbconn.Instance.RPop("MakerCouponsQueue:3"); if (!string.IsNullOrEmpty(data)) { int count = int.Parse(function.CheckInt(data)); if (count > 0) { MakerCoupons(3, count); } } else { Thread.Sleep(10000); } } } //生成券,Kind:1-电签券,2-大机券 public void MakerCoupons(int Kind = 1, int count = 0) { try { WebCMSEntities db = new WebCMSEntities(); string StartCode = "0" + Kind + "00000000"; if(db.PosCoupons.Any(m => m.QueryCount == Kind)) { StartCode = db.PosCoupons.Where(m => m.QueryCount == Kind).OrderByDescending(m => m.Id).FirstOrDefault().ExchangeCode; } int QueryCount = Kind; Kind += 1; string title = "0" + Kind.ToString(); int start = int.Parse(function.CheckInt(StartCode.Substring(2).TrimStart('0'))) + 1; if (start > 0) { int end = start + count - 1; for (int i = start; i <= end; i++) { string Code = i.ToString(); for (int j = 0; j < 8 - i.ToString().Length; j++) { Code = "0" + Code; } Code = title + Code; bool check = db.PosCoupons.Any(m => m.ExchangeCode == Code); if (!check) { db.PosCoupons.Add(new PosCoupons() { ExchangeCode = Code, QueryCount = QueryCount, }); db.SaveChanges(); } } } db.Dispose(); } catch (Exception ex) { function.WriteLog(DateTime.Now.ToString() + "\n" + ex.ToString(), "生成兑换券异常"); } } //生成虚拟广电卡SN public void ImportVirtualCard() { while (true) { string data = RedisDbconn.Instance.RPop("VirtualCardQueue"); if (!string.IsNullOrEmpty(data)) { int count = int.Parse(function.CheckInt(data)); if (count > 0) { MakerVirtualCard(count); } } else { Thread.Sleep(10000); } } } public void MakerVirtualCard(int count = 0) { try { string BatchNo = DateTime.Now.ToString("yyyyMMddHHmmss"); WebCMSEntities db = new WebCMSEntities(); string StartCode = "CS00000000000001"; if(db.PosMachinesTwo.Any(m => m.BrandId == 14 && m.PosSn.StartsWith("CS"))) { StartCode = db.PosMachinesTwo.Where(m => m.BrandId == 14 && m.PosSn.StartsWith("CS")).OrderByDescending(m => m.Id).FirstOrDefault().PosSn; } string title = "CS"; int start = int.Parse(function.CheckInt(StartCode.Substring(2).TrimStart('0'))) + 1; if (start > 0) { int end = start + count - 1; for (int i = start; i <= end; i++) { string Code = i.ToString(); for (int j = 0; j < 14 - i.ToString().Length; j++) { Code = "0" + Code; } Code = title + Code; bool check = db.PosMachinesTwo.Any(m => m.PosSn == Code); if (!check) { PosMachinesTwo pos = db.PosMachinesTwo.Add(new PosMachinesTwo() { CreateDate = DateTime.Now, PosSn = Code, //SN编号 BrandId = 14, //产品类型 StoreId = 8664, //所在仓库 DeviceType = "dianqu", //设备类型 BatchNo = BatchNo, DeviceName = "电渠", DeviceKind = "电渠虚拟", SourceStoreId = 8664, //源仓库 CardType = 2, OutBatchNo = "线上", }).Entity; db.SaveChanges(); bool chk = db.MachineForSnNo.Any(m => m.SnNo == Code && m.SnId == pos.Id); if(!chk) { db.MachineForSnNo.Add(new MachineForSnNo() { SnNo = Code, SnId = pos.Id, }); db.SaveChanges(); } } } } db.Dispose(); } catch (Exception ex) { function.WriteLog(DateTime.Now.ToString() + "\n" + ex.ToString(), "生成电渠广电虚拟卡异常"); } } } }