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

namespace MySystem
{
    public class SycnMerchantTradeService
    {
        public readonly static SycnMerchantTradeService Instance = new SycnMerchantTradeService();
        private SycnMerchantTradeService()
        { }

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

        public void StartDo()
        {
            while (true)
            {
                try
                {
                    string content = RedisDbconn.Instance.RPop<string>("SycnMerchantTradeQueue");
                    if(!string.IsNullOrEmpty(content))
                    {
                        function.WriteLog("content:" + content, "同步商户交易额");
                        JsonData jsonObj = JsonMapper.ToObject(content);
                        int OldPosId = int.Parse(function.CheckInt(jsonObj["OldPosId"].ToString()));
                        int NewPosId = int.Parse(function.CheckInt(jsonObj["NewPosId"].ToString()));
                        if(OldPosId != NewPosId)
                        {
                            CustomerSqlConn.op("update PosMerchantTradeSummay set MerchantId=" + NewPosId + " where MerchantId=" + OldPosId, MysqlConn.SqlConnStr);
                        }
                        Thread.Sleep(500);
                    }
                    else
                    {
                        Thread.Sleep(60000);
                    }
                }
                catch (Exception ex)
                {
                    function.WriteLog(DateTime.Now.ToString() + "\r\n" + ex.ToString(), "同步商户交易额异常");
                    Thread.Sleep(300000);
                }
            }
        }
    }
}