using System;
using System.Collections.Generic;
using System.Threading;
using System.Linq;
using System.Data;
using MySystem;
using MySystem.Models;
using Library;
using LitJson;

public class ChangePosFeeQueue
{
    public readonly static ChangePosFeeQueue Instance = new ChangePosFeeQueue();
    private ChangePosFeeQueue()
    {
    }

    public void StartEverTime()
    {
        Thread th = new Thread(ChangePosFeeQueueDo);
        th.IsBackground = true;
        th.Start();
    }

    private void ChangePosFeeQueueDo()
    {
        while (true)
        {
            WebCMSEntities db = new WebCMSEntities();
            try
            {
                string data = RedisDbconn.Instance.RPop<string>("ChangePosFeeQueue");
                if (!string.IsNullOrEmpty(data))
                {
                    function.WriteLog("data:" + data, "金控机具服务费调整日志");
                    JsonData Info = JsonMapper.ToObject(data);
                    string PosSnId = Info["PosSnId"].ToString(); //机具Id
                    string BrandId = Info["BrandId"].ToString(); //品牌Id
                    string Deposit = Info["Deposit"].ToString(); //服务费
                    int bId = int.Parse(BrandId);

                    //金控
                    if (bId == 1 || bId == 3)
                    {
                        if (Deposit == "0") Deposit = "0";
                        else if (Deposit == "99") Deposit = "99";
                        else if (Deposit == "199") Deposit = "199";
                        else if (Deposit == "299") Deposit = "299";
                        ChangePosFeeUtil.SetJKDeposit(PosSnId.ToString(), Deposit);
                    }
                    //开店宝
                    if (bId == 2)
                    {
                        if (Deposit == "0") Deposit = "470";
                        else if (Deposit == "99") Deposit = "471";
                        else if (Deposit == "199") Deposit = "472";
                        else if (Deposit == "299") Deposit = "473";
                        ChangePosFeeUtil.SetKDBDeposit(PosSnId.ToString(), Deposit);
                    }
                    //乐刷
                    if (bId == 4 || bId == 5)
                    {
                        if (Deposit == "99") Deposit = "100";
                        else if (Deposit == "199") Deposit = "200";
                        else if (Deposit == "299") Deposit = "300";
                        ChangePosFeeUtil.SetLSDeposit(PosSnId.ToString(), Deposit);
                    }
                    //立刷
                    if (bId == 6)
                    { 
                        if (Deposit == "0") Deposit = "0";
                        else if (Deposit == "99") Deposit = "99";
                        else if (Deposit == "199") Deposit = "199";
                        else if (Deposit == "249") Deposit = "249";
                        ChangePosFeeUtil.SetLISDeposit(PosSnId.ToString(), Deposit);
                    }
                    //盛付通
                    if (bId == 7)
                    { 
                        if (Deposit == "0") Deposit = "0";
                        else if (Deposit == "99") Deposit = "99";
                        else if (Deposit == "199") Deposit = "199";
                        else if (Deposit == "299") Deposit = "299";
                        ChangePosFeeUtil.SetSFTDeposit(PosSnId.ToString(), Deposit);
                    }
                    //海科
                    if (bId == 8 || bId == 9)
                    { 
                        if (Deposit == "99") Deposit = "100";
                        else if (Deposit == "199") Deposit = "200";
                        else if (Deposit == "299") Deposit = "300";
                        ChangePosFeeUtil.SetHKDeposit(PosSnId.ToString(), Deposit);
                    }
                    //联动
                    if (bId == 10 || bId == 11)
                    { 
                        if (Deposit == "0") Deposit = "0";
                        else if (Deposit == "99") Deposit = "99";
                        else if (Deposit == "199") Deposit = "199";
                        else if (Deposit == "299") Deposit = "299";
                        ChangePosFeeUtil.SetLDDeposit(PosSnId.ToString(), Deposit);
                    }
                    //盒易付
                    if (bId == 12 || bId == 13)
                    { 
                        if (Deposit == "0") Deposit = "0";
                        else if (Deposit == "99") Deposit = "99";
                        else if (Deposit == "199") Deposit = "199";
                        else if (Deposit == "299") Deposit = "299";
                        ChangePosFeeUtil.SetHYFDeposit(PosSnId.ToString(), Deposit);
                    }
                    //联客宝
                    if (bId == 15 || bId == 16)
                    { 
                        if (Deposit == "0") Deposit = "0";
                        else if (Deposit == "99") Deposit = "99";
                        else if (Deposit == "199") Deposit = "199";
                        else if (Deposit == "299") Deposit = "299";
                        ChangePosFeeUtil.SetPosPDeposit(PosSnId.ToString(), Deposit);
                    }
                }
                else
                {
                    Thread.Sleep(5000);
                }
            }
            catch (Exception ex)
            {
                function.WriteLog(DateTime.Now.ToString() + "\n" + ex.ToString(), "机具服务费调整线程异常");
            }
            db.Dispose();
        }
    }
}