123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111 |
- 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();
- }
- public void ImportPostDo()
- {
- while (true)
- {
- string data = RedisDbconn.Instance.RPop<string>("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<string>("MakerCouponsQueue:2");
- if (!string.IsNullOrEmpty(data))
- {
- int count = int.Parse(function.CheckInt(data));
- if (count > 0)
- {
- MakerCoupons(2, count);
- }
- }
- else
- {
- Thread.Sleep(10000);
- }
- }
- }
- //生成券,Kind:1-电签券,2-大机券
- public void MakerCoupons(int Kind = 1, int count = 0)
- {
- try
- {
- WebCMSEntities db = new WebCMSEntities();
- string 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(), "生成兑换券异常");
- }
- }
- }
- }
|