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 PosSn = Info["PosSn"].ToString(); //机具Id
                    string BrandId = Info["BrandId"].ToString(); //品牌Id
                    string Deposit = Info["Deposit"].ToString(); //服务费
                    string UserId = Info["UserId"].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(PosSn, 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(PosSn, 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(PosSn, Deposit, BrandId);
                    }
                    //立刷
                    if (bId == 6)
                    { 
                        if (Deposit == "0") Deposit = "0";
                        else if (Deposit == "99") Deposit = "99";
                        else if (Deposit == "199") Deposit = "199";
                        else if (Deposit == "299") Deposit = "299";
                        ChangePosFeeUtil.SetLISDeposit(PosSn, Deposit, BrandId);
                    }
                    //立刷微电签
                    if (bId == 22 || bId == 33)
                    { 
                        if (Deposit == "0") Deposit = "0";
                        else if (Deposit == "99") Deposit = "99";
                        else if (Deposit == "199") Deposit = "199";
                        else if (Deposit == "299") Deposit = "299";
                        ChangePosFeeUtil.SetLISDeposit(PosSn, Deposit, BrandId);
                    }
                    //盛付通
                    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(PosSn, 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(PosSn, Deposit, BrandId);
                    }
                    //联动
                    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(PosSn, Deposit, BrandId);
                    }
                    //联动掌中宝
                    if (bId == 34)
                    { 
                        if (Deposit == "0") Deposit = "0";
                        else if (Deposit == "99") Deposit = "1";
                        else if (Deposit == "199") Deposit = "2";
                        else if (Deposit == "299") Deposit = "3";
                        ChangePosFeeUtil.SetLDZZBDeposit(PosSn, 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(PosSn, Deposit, UserId);
                    }
                    //联客宝
                    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(PosSn, Deposit);
                    }
                    //拉卡拉
                    if (bId == 30 || bId == 31)
                    { 
                        if (Deposit == "0") Deposit = "0";
                        else if (Deposit == "99") Deposit = "99";
                        else if (Deposit == "199") Deposit = "199";
                        else if (Deposit == "299") Deposit = "299";
                        ChangePosFeeUtil.SetLKLDeposit(PosSn, Deposit, bId);
                    }
                }
                else
                {
                    Thread.Sleep(5000);
                }
            }
            catch (Exception ex)
            {
                function.WriteLog(DateTime.Now.ToString() + "\n" + ex.ToString(), "机具服务费调整线程异常");
            }
            db.Dispose();
        }
    }
}