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

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


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

        public void StartEverDay()
        {
            while(true)
            {
                if(DateTime.Now.Day == 1 && DateTime.Now.Hour > 0)
                {
                    try
                    {
                        StartEverDayDo();
                    }
                    catch(Exception ex)
                    {
                        LogHelper.Instance.WriteLog(DateTime.Now.ToString() + "\n" + ex.ToString(), "每月统计押金达标数据异常");
                    }
                }
                Thread.Sleep(60000);
            }
        }

        public void StartEverDayDo()
        {
            string Month = DateTime.Now.ToString("yyyyMM");
            string chk = function.ReadInstance("/DepositReturn/" + Month + ".txt");
            if(!string.IsNullOrEmpty(chk))
            {
                return;
            }
            function.WritePage("/DepositReturn/", Month + ".txt", DateTime.Now.ToString());
            WebCMSEntities db = new WebCMSEntities();
            int startid = int.Parse(function.CheckInt(function.ReadInstance("/DepositReturn/" + Month + "MerchantId.txt")));
            bool op = true;
            while(op)
            {
                List<PosMerchantInfo> merchants = db.PosMerchantInfo.Where(m => m.Id > startid && m.ActiveStatus == 1 && m.StandardStatus == 0).OrderBy(m => m.Id).Take(50).ToList();
                if(merchants.Count > 0)
                {
                    foreach(PosMerchantInfo merchant in merchants)
                    {
                        // string start = DateTime.Now.ToString("yyyy-MM-dd HH:mm:ss.fff");
                        int StandardMonths = merchant.StandardMonths;
                        int StandardStatus = merchant.StandardStatus;                    
                        DateTime ActDate = merchant.CreateDate.Value;
                        if(merchant.MerStandardDate == null)
                        {
                            PosMachinesTwo pos = db.PosMachinesTwo.FirstOrDefault(m => m.PosSn == merchant.KqSnNo && m.ActivationState == 1 && m.ActivationTime != null);
                            if(pos != null)
                            {
                                ActDate = pos.ActivationTime.Value;
                            }
                        }
                        else
                        {
                            ActDate = merchant.MerStandardDate.Value;
                        }
                        int MonthNum = StandardMonths;
                        for (int i = StandardMonths + 1; i <= 10; i++)
                        {
                            string TradeMonth = ActDate.AddMonths(i).ToString("yyyyMM");
                            if(int.Parse(TradeMonth) < int.Parse(DateTime.Now.ToString("yyyyMM")))
                            {
                                decimal TradeAmount = 0;
                                bool CheckAmount = db.PosMerchantTradeSummay.Any(m => m.MerchantId == merchant.Id && m.TradeMonth == TradeMonth);
                                if(CheckAmount)
                                {
                                    TradeAmount = db.PosMerchantTradeSummay.Where(m => m.MerchantId == merchant.Id && m.TradeMonth == TradeMonth).Sum(m => m.TradeAmount);
                                }
                                if(TradeAmount >= 10000)
                                {
                                    MonthNum = i;
                                }
                                else
                                {
                                    StandardStatus = -1;
                                    break;
                                }
                            }
                        }
                        if(MonthNum >= 10)
                        {
                            StandardStatus = 4;
                        }
                        PosMerchantInfo edit = db.PosMerchantInfo.FirstOrDefault(m => m.Id == merchant.Id);
                        if(edit != null)
                        {
                            edit.StandardMonths = MonthNum;
                            edit.StandardStatus = StandardStatus;
                            db.SaveChanges();
                        }
                        // string end = DateTime.Now.ToString("yyyy-MM-dd HH:mm:ss.fff");
                        startid = merchant.Id;
                    }
                }
                else
                {
                    op = false;
                }
                function.WritePage("/DepositReturn/", Month + "MerchantId.txt", startid.ToString());
            }
            db.Dispose();
        }
    }
}