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

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

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

    private void DoWorks()
    {
        while (true)
        {
            try
            {
                string content = RedisDbconn.Instance.RPop<string>("UserMonthFeeQueue");
                if(!string.IsNullOrEmpty(content))
                {
                    int uid = int.Parse(content);
                    WebCMSEntities db = new WebCMSEntities();
                    DoSomething(db, uid);
                    db.Dispose();
                    RedisDbconn.Instance.AddList("PosExpiredPayQueue", content);
                }
                else
                {
                    Thread.Sleep(60000);
                }
            }
            catch (Exception ex)
            {
                function.WriteLog(DateTime.Now.ToString() + "\n" + ex.ToString(), "创客每月月费扣款异常");
            }
            function.WriteLog(DateTime.Now.ToString("yyyy-MM-dd HH:mm:ss.fff") + "\n\n", "创客每月月费扣款日志");
        }
    }

    public void DoSomething(WebCMSEntities db, int UserId)
    {
        DateTime Start = DateTime.Parse(DateTime.Now.ToString("yyyy-MM") + "-01 00:00:00");
        DateTime WithoutStart = DateTime.Parse("2023-02-20 00:00:00"); //收支明细服务费记录开始时间
        UserAccount User = db.UserAccount.FirstOrDefault(m => m.Id == UserId && m.BalanceAmount > 0);
        if(User != null)
        {
            bool check = db.UserAccountRecord.Any(m => m.UserId == User.Id && m.CreateDate >= Start && m.CreateDate >= WithoutStart && m.ChangeType == 125);
            if(!check)
            {
                if(User.BalanceAmount > 10)
                {
                    AlipayPayBack2Service.Instance.ChangeAccount(db, new Orders(), User.Id, -10, 125);
                }
                else
                {
                    AlipayPayBack2Service.Instance.ChangeAccount(db, new Orders(), User.Id, -User.BalanceAmount, 125);
                }
            }
        }
    }

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


    private void DoWorks2()
    {
        while (true)
        {
            try
            {
                if(DateTime.Now.Hour == 1 && DateTime.Now < DateTime.Parse("2023-02-20 00:00:00"))
                {
                    string check = function.ReadInstance("/UserMonthFee/" + DateTime.Now.ToString("yyyy-MM-dd") + ".txt");
                    if(string.IsNullOrEmpty(check))
                    {
                        function.WritePage("/UserMonthFee/", DateTime.Now.ToString("yyyy-MM-dd") + ".txt", DateTime.Now.ToString("HH:mm:ss"));
                        WebCMSEntities db = new WebCMSEntities();
                        DoSomething2(db);
                        db.Dispose();
                    }
                }
            }
            catch (Exception ex)
            {
                function.WriteLog(DateTime.Now.ToString() + "\n" + ex.ToString(), "创客每月月费补扣款异常");
            }
            function.WriteLog(DateTime.Now.ToString("yyyy-MM-dd HH:mm:ss.fff") + "\n\n", "创客每月月费补扣款日志");
            Thread.Sleep(60000);
        }
    }

    public void DoSomething2(WebCMSEntities db)
    {
        DateTime Start = DateTime.Parse("2023-01-19 00:00:00"); //收支明细服务费记录开始时间
        // DateTime ExpireDate = DateTime.Parse(DateTime.Now.ToString("yyyy-MM") + "-01 00:00:00").AddDays(-90); //创客认证超过90天比对时间

        // //超过90天创客ID集合
        // List<int> UserIds = db.Users.Where(m => m.AuthFlag == 1 && m.AuthDate < ExpireDate).ToList().Select(m => m.Id).ToList();

        List<int> UserIds = new List<int>();
        UserIds.Add(311);

        //已扣费创客ID集合  
        List<int> UserAccountRecordIds = db.UserAccountRecord.Where(m => UserIds.Contains(m.UserId) && m.CreateDate >= Start && m.ChangeType == 125).ToList().Select(m => m.UserId).ToList();

        //应该扣除而未扣的创客集合 
        var Users = db.UserAccount.Select(m => new { m.Id, m.BalanceAmount }).Where(m => !UserAccountRecordIds.Contains(m.Id) && UserIds.Contains(m.Id) && m.BalanceAmount >= 10).ToList();
        foreach(var User in Users)
        {
            AlipayPayBack2Service.Instance.ChangeAccount(db, new Orders(), User.Id, -10, 125); //执行扣费
        }
    }
}