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

namespace MySystem
{
    /// <summary>
    /// 过期机具预扣费消息推送
    /// </summary>
    public class TimeOutPosSendMessageService
    {
        public readonly static TimeOutPosSendMessageService Instance = new TimeOutPosSendMessageService();
        private TimeOutPosSendMessageService()
        { }

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

        public void doSomething()
        {
            while (true)
            {
                if (DateTime.Now.Hour < 3)
                {
                    try
                    {
                        string check = function.ReadInstance("/TimeOutPosSendMessage/check" + DateTime.Now.ToString("yyyy-MM-dd") + ".txt");
                        if (string.IsNullOrEmpty(check))
                        {
                            function.WritePage("/TimeOutPosSendMessage/", "check" + DateTime.Now.ToString("yyyy-MM-dd") + ".txt", DateTime.Now.ToString("HH:mm:ss"));
                            WebCMSEntities db = new WebCMSEntities();
                            var date = DateTime.Now.AddDays(-55).ToString("yyyy-MM-dd 00:00:00");//过期限制时间
                            var time = DateTime.Parse(date);//过期限制时间

                            DataTable dt = CustomerSqlConn.dtable("SELECT BuyUserId FROM PosMachinesTwo WHERE `Status`>-1 AND BuyUserId>0 AND BindingState=0 AND ActivationState=0 AND ScanQrTrade=0 AND RecycEndDate <'" + date + "' GROUP BY BuyUserId", MysqlConn.SqlConnStr);//消息推送创客
                            var query = db.PosMachinesTwo.Where(m => m.Status > -1 && m.BuyUserId > 0 && m.BindingState == 0 && m.ActivationState == 0 && m.ScanQrTrade == 0 && m.RecycEndDate < time).ToList();//循环过期超过10天机具
                            var brandInfo = db.KqProducts.ToList();
                            foreach (DataRow item in dt.Rows)
                            {
                                int BuyUserId = int.Parse(item["BuyUserId"].ToString());
                                var BrandName = "";
                                var PosSn = "";
                                var SendInfo = "";
                                foreach (var items in brandInfo)
                                {
                                    int BrandId = int.Parse(items.Id.ToString());
                                    BrandName = items.Name.ToString();
                                    var posInfo = query.Where(m => m.BuyUserId == BuyUserId && m.BrandId == BrandId);
                                    foreach (var pos in posInfo)
                                    {
                                        PosSn += BrandName +"："+pos.PosSn + "," + "<br/>";
                                    }
                                }
                                SendInfo = PosSn.TrimEnd(',') + "<br/>";
                                RedisDbconn.Instance.AddList("MsgPersonalQueue", Newtonsoft.Json.JsonConvert.SerializeObject(new MsgPersonal()
                                {
                                    UserId = BuyUserId, //创客
                                    Title = "机具循环过期提醒", //标题
                                    Content = "<div class='f16'>尊敬的创客您好:<br/>您的" + SendInfo + "循环已过期五十五天以上，请在五日内完成激活或通过机具回收回寄该机具。</ div > ",//内容
                                    Summary = "系统检测到您的部分机具可回收，请打开我的-回收机具及时处理！",
                                    CreateDate = DateTime.Now,
                                }));
                            }
                        }
                    }
                    catch (Exception ex)
                    {
                        LogHelper.Instance.WriteLog(DateTime.Now.ToString() + ":" + ex.ToString(), "过期机具预扣费消息推送异常");
                    }
                }
                Thread.Sleep(1000);
            }
        }
    }
}