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 WifiUnBindHelper
{
    public readonly static WifiUnBindHelper Instance = new WifiUnBindHelper();
    private WifiUnBindHelper()
    {
    }

    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>("kxs_unbindwifi_list");
                if(!string.IsNullOrEmpty(content))
                {
                    DoSomething(content);
                }
                else
                {
                    Thread.Sleep(60000);
                }
            }
            catch (Exception ex)
            {
                Utils.WriteLog(DateTime.Now.ToString() + "\n" + ex.ToString(), "WIFI解绑异常");
            }
        }
    }

    public void DoSomething(string content)
    {
        string[] data = content.Split(new string[] { "#cut#" }, StringSplitOptions.None);
        JsonData jsonObj = JsonMapper.ToObject(data[1]);
        string posSn = jsonObj["posSn"].ToString();
        WebCMSEntities db = new WebCMSEntities();
        IQueryable<WifiTradeRecord> list = db.WifiTradeRecord.Where(m => m.SnNo == posSn);
        foreach(WifiTradeRecord edit in list)
        {
            edit.DoMonths = edit.Duration + 1;
            edit.Status = -1;
        }
        db.SaveChanges();
        db.Dispose();
    }
}