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

namespace MySystem
{
    /// <summary>
    /// 过期兑换机具循环截止时间超过15天激活扣费退还
    /// </summary>
    public class TimeOutPosChargeReturnService
    {
        public readonly static TimeOutPosChargeReturnService Instance = new TimeOutPosChargeReturnService();
        private TimeOutPosChargeReturnService()
        { }

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

        public void doSomething()
        {
            while (true)
            {
                if (DateTime.Now.Hour < 9)
                {
                    try
                    {
                        string check = function.ReadInstance("/TimeOutPosChargeReturn/check" + DateTime.Now.ToString("yyyy-MM-dd") + ".txt");
                        if (string.IsNullOrEmpty(check))
                        {
                            function.WritePage("/TimeOutPosChargeReturn/", "check" + DateTime.Now.ToString("yyyy-MM-dd") + ".txt", DateTime.Now.ToString("HH:mm:ss"));
                            WebCMSEntities db = new WebCMSEntities();
                            DataTable dt = CustomerSqlConn.dtable("SELECT a.Id Id FROM ToChargeBackRecord a LEFT JOIN PosMachinesTwo b ON a.Remark=b.PosSn WHERE a.`Status`=1 AND b.PosSnType=0  AND b.BindingState=1 AND b.ActivationState=1", MysqlConn.SqlConnStr);//循环过期超过15天激活机具退费创客
                            foreach (DataRow item in dt.Rows)
                            {
                                int Id = int.Parse(item["Id"].ToString());
                                var toChargeBackRecord = db.ToChargeBackRecord.FirstOrDefault(m => m.Id == Id && m.Status == 1) ?? new ToChargeBackRecord();
                                if (toChargeBackRecord.Id > 0)
                                {
                                    var userAccount = db.UserAccount.FirstOrDefault(m => m.Id == toChargeBackRecord.UserId) ?? new UserAccount();
                                    var userAccountRecord = db.UserAccountRecord.Add(new UserAccountRecord
                                    {
                                        CreateDate = DateTime.Now,
                                        UserId = toChargeBackRecord.UserId,
                                        BeforeBalanceAmount = userAccount.BalanceAmount,
                                        AfterBalanceAmount = userAccount.BalanceAmount + toChargeBackRecord.ChargeAmount,
                                        ChangeAmount = toChargeBackRecord.ChargeAmount,
                                        ChangeType = 126,//过期兑换机具超过15天激活扣费退还
                                        Remark = "机具货款退还",

                                    }).Entity;
                                    toChargeBackRecord.Status = 2;//过期兑换机具循环截止时间超过15天激活扣费退还标识
                                    userAccount.BalanceAmount += toChargeBackRecord.ChargeAmount;
                                    db.SaveChanges();
                                }
                            }
                        }
                    }
                    catch (Exception ex)
                    {
                        function.WriteLog(DateTime.Now.ToString() + ":" + ex.ToString(), "过期兑换机具超过15天激活扣费退还");
                    }
                }
                Thread.Sleep(1000);
            }
        }
    }
}