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

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

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

        public void doSomething()
        { 
            while (true)
            {
                string content = RedisDbconn.Instance.RPop<string>("ResetMerchantTradeQueue");
                if (!string.IsNullOrEmpty(content))
                {
                    try
                    {
                        OtherMySqlConn.connstr = Library.ConfigurationManager.AppSettings["SqlConnStr"].ToString();
                        string[] datalist = content.Split(new string[] { "#cut#" }, StringSplitOptions.None);
                        int MerchantId = int.Parse(datalist[0]);
                        DateTime sdate = DateTime.Parse(datalist[1]);
                        DateTime edate = DateTime.Parse(datalist[2]);
                        function.WriteLog(DateTime.Now.ToString(), "重置商户交易额");
                        while (sdate <= edate)
                        {
                            string date = sdate.ToString("yyyy-MM-dd");
                            string TradeDate = date.Replace("-", "");
                            string TradeMonth = TradeDate.Substring(0, 6);
                            string start = date + " 00:00:00";
                            string end = DateTime.Parse(date).AddDays(1).ToString("yyyy-MM-dd") + " 00:00:00";
                            OtherMySqlConn.op("delete from PosMerchantTradeSummay where MerchantId=" + MerchantId + " and TradeMonth='" + TradeMonth + "' and TradeDate='" + TradeDate + "'");
                            OtherMySqlConn.op("insert into PosMerchantTradeSummay (MerchantId,BrandId,TradeDate,TradeMonth,TradeAmount,CreateDate)  select *,now() from (select MerchantId,BrandId,DATE_FORMAT(CreateDate,'%Y%m%d') as TradeDate,DATE_FORMAT(CreateDate,'%Y%m') as TradeMonth,sum(TradeAmount) as TradeAmount from TradeRecord where MerchantId=" + MerchantId + " and CreateDate>='" + start + "' and CreateDate<'" + end + "' group by MerchantId,BrandId,DATE_FORMAT(CreateDate,'%Y%m%d'),TradeDate,DATE_FORMAT(CreateDate,'%Y%m') order by MerchantId,BrandId,DATE_FORMAT(CreateDate,'%Y%m%d')) tb");
                            RedisDbconn.Instance.Clear("PosMerchantAmount:*");
                            RedisDbconn.Instance.Clear("PosMerchantAmount:*:" + date.Replace("-", "").Substring(0, 6)); //商户当月交易
                            RedisDbconn.Instance.Clear("PosMerchantAmount:*:" + date.Replace("-", "")); //商户当日交易
                            sdate = sdate.AddDays(1);
                            Thread.Sleep(200);
                        }
                        function.WriteLog(DateTime.Now.ToString() + "\r\n\r\n", "重置商户交易额");
                    }
                    catch (Exception ex)
                    {
                        function.WriteLog(DateTime.Now.ToString() + ":" + ex.ToString(), "重置商户交易额异常");
                    }
                }
                else
                {
                    Thread.Sleep(5000);
                }
            }
        }
    }
}