|
@@ -0,0 +1,87 @@
|
|
|
+using System;
|
|
|
+using System.Collections.Generic;
|
|
|
+using System.Threading;
|
|
|
+using System.Linq;
|
|
|
+using System.Data;
|
|
|
+using MySystem;
|
|
|
+using MySystem.PxcModels;
|
|
|
+using Library;
|
|
|
+using LitJson;
|
|
|
+
|
|
|
+public class PosCouponExchangeHelper
|
|
|
+{
|
|
|
+ public readonly static PosCouponExchangeHelper Instance = new PosCouponExchangeHelper();
|
|
|
+ private PosCouponExchangeHelper()
|
|
|
+ {
|
|
|
+ }
|
|
|
+
|
|
|
+ public void Start()
|
|
|
+ {
|
|
|
+ Thread th = new Thread(DoWorks);
|
|
|
+ th.IsBackground = true;
|
|
|
+ th.Start();
|
|
|
+ }
|
|
|
+
|
|
|
+ private void DoWorks()
|
|
|
+ {
|
|
|
+ while (true)
|
|
|
+ {
|
|
|
+ try
|
|
|
+ {
|
|
|
+ string content = RedisDbconn.Instance.RPop<string>("PosCouponExchangeQueue");
|
|
|
+ if(!string.IsNullOrEmpty(content))
|
|
|
+ {
|
|
|
+ WebCMSEntities db = new WebCMSEntities();
|
|
|
+ DoSomething(db, content);
|
|
|
+ db.Dispose();
|
|
|
+ }
|
|
|
+ }
|
|
|
+ catch (Exception ex)
|
|
|
+ {
|
|
|
+ function.WriteLog(DateTime.Now.ToString() + "\n" + ex.ToString(), "电签券对大机券异常");
|
|
|
+ }
|
|
|
+ Thread.Sleep(60000);
|
|
|
+ }
|
|
|
+ }
|
|
|
+
|
|
|
+ public void DoSomething(WebCMSEntities db, string content)
|
|
|
+ {
|
|
|
+ JsonData data = JsonMapper.ToObject(content);
|
|
|
+ int UserId = int.Parse(function.CheckInt(data["UserId"].ToString())); //创客Id
|
|
|
+ string CouponCodes = data["CouponCodes"].ToString(); //券列表
|
|
|
+ Dictionary<string, object> Obj = new Dictionary<string, object>();
|
|
|
+ List<string> Codes = CouponCodes.Split(',').ToList();
|
|
|
+ List<int> LeaderUserIds = new List<int>();
|
|
|
+ int BeforeUserId = 0;
|
|
|
+ int AfterUserId = 0;
|
|
|
+ int index = 0;
|
|
|
+ IQueryable<PosCoupons> list = db.PosCoupons.Where(m => Codes.Contains(m.ExchangeCode) && m.UserId == UserId && m.IsLock == 0 && m.IsUse == 0).OrderBy(m => m.CreateDate);
|
|
|
+ foreach(PosCoupons sub in list)
|
|
|
+ {
|
|
|
+ index += 1;
|
|
|
+ if(index % 2 == 1) BeforeUserId = sub.LeaderUserId;
|
|
|
+ if(index % 2 == 0)
|
|
|
+ {
|
|
|
+ AfterUserId = sub.LeaderUserId;
|
|
|
+ if(BeforeUserId > 0 && AfterUserId > 0) LeaderUserIds.Add(AfterUserId);
|
|
|
+ if(BeforeUserId == 0 && AfterUserId > 0) LeaderUserIds.Add(AfterUserId);
|
|
|
+ if(BeforeUserId > 0 && AfterUserId == 0) LeaderUserIds.Add(BeforeUserId);
|
|
|
+ if(BeforeUserId == 0 && AfterUserId == 0) LeaderUserIds.Add(0);
|
|
|
+ BeforeUserId = 0;
|
|
|
+ AfterUserId = 0;
|
|
|
+ }
|
|
|
+ sub.UserId = 0;
|
|
|
+ }
|
|
|
+ db.SaveChanges();
|
|
|
+ int Count = Codes.Count % 2;
|
|
|
+ index = 0;
|
|
|
+ IQueryable<PosCoupons> chklist = db.PosCoupons.Where(m => m.QueryCount == 2 && m.UserId == 0 && m.IsLock == 0 && m.IsUse == 0).Take(Count);
|
|
|
+ foreach(PosCoupons sub in chklist)
|
|
|
+ {
|
|
|
+ index += 1;
|
|
|
+ sub.UserId = UserId;
|
|
|
+ sub.LeaderUserId = LeaderUserIds[index];
|
|
|
+ }
|
|
|
+ db.SaveChanges();
|
|
|
+ }
|
|
|
+}
|