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

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

        public void Start()
        {
            Thread th = new Thread(StartDo);
            th.IsBackground = true;
            th.Start();
        }
        public void StartDo()
        {
            while(true)
            {
                try
                {
                    DoSomething();
                }
                catch(Exception ex)
                {
                    function.WriteLog(DateTime.Now.ToString() + "\r\n" + ex.ToString(), "盒力四射加码奖励异常");
                }
                Thread.Sleep(600000);
            }
        }
        public void DoSomething()
        {
            WebCMSEntities db = new WebCMSEntities();
            DateTime online = DateTime.Parse("2024-07-05 00:00:00");
            DateTime end = DateTime.Parse("2025-01-01 00:00:00");
            if(DateTime.Now > end) return;
            List<PosMachinesTwo> posList = db.PosMachinesTwo.Where(m => m.BindingState == 1 && m.ActivationState == 1 && m.BindingTime >= online && (m.BrandId == 12 || m.BrandId == 13) && m.Sort == 0).ToList();
            foreach (PosMachinesTwo pos in posList)
            {
                function.WriteLog(str: "机具号:" + pos.PosSn, "盒力四射加码奖励日志");
                DateTime leftTime = pos.BindingTime.Value.AddDays(7);
                DateTime rightTime = pos.BindingTime.Value.AddDays(30);
                decimal amt = 0;
                DataTable dt = CustomerSqlConn.dtable("select sum(TradeAmount) from PosMerchantTradeSummay where TradeDate>='" + leftTime.ToString("yyyyMMdd") + "' and TradeDate<='" + rightTime.ToString("yyyyMMdd") + "' and MerchantId=" + pos.BindMerchantId + "", MysqlConn.ReadSqlConnStr);
                if(dt.Rows.Count > 0)
                {
                    amt = decimal.Parse(function.CheckNum(dt.Rows[0][0].ToString()));
                }
                if(amt >= 1000)
                {
                    Users user = db.Users.FirstOrDefault(m => m.Id == pos.BuyUserId);
                    if (user != null)
                    {
                        function.WriteLog("机具所属人:" + user.Id, "盒力四射加码奖励日志");
                        int GetUserId = user.Id;
                        AlipayPayBack2Service.Instance.ChangeAccount(db, new Orders() { Id = pos.Id }, GetUserId, 50, 139);
                        PosMachinesTwo edit = db.PosMachinesTwo.FirstOrDefault(m => m.Id == pos.Id);
                        if(edit != null)
                        {
                            edit.Sort = 1;
                            db.SaveChanges();
                        }
                    }
                }
                function.WriteLog("\n\n\n", "盒力四射加码奖励日志");
            }
            db.Dispose();
        }
    }
}