using HandyControl.Controls;
using HKCardOUT.Helper;
using HKCardOUT.Logic.Model;
using HKControl;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using UHFHelper;
using XExten.Advance.LinqFramework;

namespace HKCardOUT.Logic.Service
{
    public class HKCore
    {
        List<SaleLog> Commom(SaleLog input)
        {
            if (DataBus.StartDevice)
                if (!Main.GetInstance.GetIsSwipe(input.Location.AsInt())) return null;
            var Old = DbContext.Context.Queryable<SaleLog>()
                .Where(t => t.CardNo == input.CardNo)
                .Where(t => t.CreateTime.Year == DateTime.Now.Year)
                .Where(t => t.CreateTime.Month == DateTime.Now.Month)
                .Where(t => t.CreateTime.Day == DateTime.Now.Day).ToList();
            if (DataBus.Cancel)
                if (Old.Count > 3) return null;
            return Old;
        }

        bool GetCardStutas(string CardNo)
        {
           var data = DbContext.Context.Queryable<CardStutas>().First(t => t.CardNo == CardNo);
            if (data == null) return false;
            if (data.Stutas != 1) return false;
            return true;
        }
        public static void AddCard(CardStutas input) 
        {
            DbContext.Context.Insertable(input).CallEntityMethod(t => t.Create()).ExecuteCommand();
        }

        /// <summary>
        /// 早上消费
        /// </summary>
        /// <param name="input"></param>
        /// <returns></returns>
        public bool DeviceSaleAM(SaleLog input)
        {
            try
            {
                if(!GetCardStutas(input.CardNo)) return false;
                var Old = Commom(input);
                if (Old == null) return false;
                var AM = DataBus.Times.AM.Split(",");
                var S = DateTime.Parse(AM[0]);
                var E = DateTime.Parse(AM[1]);
                var AMDay = Old.Where(t => t.CreateTime >=S  && t.CreateTime < E)
                    .Where(t => t.CreateTime.Year == DateTime.Now.Year)
                    .Where(t => t.CreateTime.Month == DateTime.Now.Month)
                    .Where(t => t.CreateTime.Day == DateTime.Now.Day)
                    .Count();
                HKLog.HKLogImport.WriteInfo($"卡号{input.CardNo}上午消费次数：{AMDay}");

                if (AMDay < DataBus.Count)
                {
                    var entity = DbContext.Context.Insertable(input).CallEntityMethod(t => t.Create()).ExecuteReturnEntity();
                    if (entity.Id != Guid.Empty)
                    {
                        ////有网络直接同步
                        //if (DataBus.NetWordState)
                        //{
                        //    //先出餐后推数据
                        //    if (DataBus.StartDevice)
                        //    {
                        //        Main.GetInstance.Start(input.Location.AsInt());
                        //        UHF_RS485_Helper.GetInstance().OpenBeep(input.Location.AsInt());
                        //    }
                        //    var res = RemoteService.SyncSaleLog(input.CardNo, input.Location);
                        //    if (res)
                        //        DbContext.Context.Updateable<SaleLog>().SetColumns(t => t.IsSync == true).Where(t => t.Id == entity.Id).ExecuteCommand();
                        //    return res;
                        //}
                        //else
                        //{

                        //}
                        if (DataBus.StartDevice)
                        {
                            Main.GetInstance.Start(input.Location.AsInt());
                            UHF_RS485_Helper.GetInstance().OpenBeep(input.Location.AsInt());
                        }
                        return true;
                    }
                }
                return false;
            }
            catch (Exception ex)
            {
                HKLog.HKLogImport.WriteError(ex);
                return false;
            }

        }
        /// <summary>
        /// 中午消费
        /// </summary>
        /// <param name="input"></param>
        /// <param name="Old"></param>
        /// <returns></returns>
        public bool DeviceSalePM(SaleLog input)
        {
            try
            {
                if (!GetCardStutas(input.CardNo)) return false;
                var Old = Commom(input);
                if (Old == null) return false;
                var PM = DataBus.Times.PM.Split(",");
                var S = DateTime.Parse(PM[0]);
                var E = DateTime.Parse(PM[1]);
                var PMDay = Old.Where(t => t.CreateTime >= S && t.CreateTime < E).
                      Where(t => t.CreateTime.Year == DateTime.Now.Year)
                      .Where(t => t.CreateTime.Month == DateTime.Now.Month)
                      .Where(t => t.CreateTime.Day == DateTime.Now.Day)
                      .Count();
                HKLog.HKLogImport.WriteInfo($"卡号{input.CardNo}中午消费次数：{PMDay}");
                if (PMDay < DataBus.Count)
                {
                    var entity = DbContext.Context.Insertable(input).CallEntityMethod(t => t.Create()).ExecuteReturnEntity();
                    if (entity.Id != Guid.Empty)
                    {
                        ////有网络直接同步
                        //if (DataBus.NetWordState)
                        //{
                        //    //先出餐后推数据
                        //    if (DataBus.StartDevice)
                        //    {
                        //        Main.GetInstance.Start(input.Location.AsInt());
                        //        UHF_RS485_Helper.GetInstance().OpenBeep(input.Location.AsInt());
                        //    }
                        //    var res = RemoteService.SyncSaleLog(input.CardNo, input.Location);
                        //    if (res)
                        //        DbContext.Context.Updateable<SaleLog>().SetColumns(t => t.IsSync == true).Where(t => t.Id == entity.Id).ExecuteCommand();
                        //    return res;
                        //}
                        //else
                        //{
                            if (DataBus.StartDevice)
                            {
                                Main.GetInstance.Start(input.Location.AsInt());
                                UHF_RS485_Helper.GetInstance().OpenBeep(input.Location.AsInt());
                            }
                            return true;
                        //}
                    }
                }
                return false;
            }
            catch (Exception ex)
            {
                HKLog.HKLogImport.WriteError(ex);
                return false;
            }

        }
        /// <summary>
        /// 晚上
        /// </summary>
        /// <param name="input"></param>
        /// <param name="Old"></param>
        /// <returns></returns>
        public bool DeviceSaleAT(SaleLog input)
        {
            try
            {
                if (!GetCardStutas(input.CardNo)) return false;
                var Old = Commom(input);
                if (Old == null) return false;
                var AT = DataBus.Times.AT.Split(",");
                var S = DateTime.Parse(AT[0]);
                var E = DateTime.Parse(AT[1]);
                var ATDay = Old.Where(t => t.CreateTime >= S && t.CreateTime <E)
                    .Where(t => t.CreateTime.Year == DateTime.Now.Year)
                    .Where(t => t.CreateTime.Month == DateTime.Now.Month)
                    .Where(t => t.CreateTime.Day == DateTime.Now.Day)
                    .Count();
                HKLog.HKLogImport.WriteInfo($"卡号{input.CardNo}下午消费次数：{ATDay}");
                if (ATDay < DataBus.Count)
                {
                    var entity = DbContext.Context.Insertable(input).CallEntityMethod(t => t.Create()).ExecuteReturnEntity();
                    if (entity.Id != Guid.Empty)
                    {
                        ////有网络直接同步
                        //if (DataBus.NetWordState)
                        //{
                        //    //先出餐后推数据
                        //    if (DataBus.StartDevice)
                        //    {
                        //        Main.GetInstance.Start(input.Location.AsInt());
                        //        UHF_RS485_Helper.GetInstance().OpenBeep(input.Location.AsInt());
                        //    }
                        //    var res = RemoteService.SyncSaleLog(input.CardNo, input.Location);
                        //    if (res)
                        //        DbContext.Context.Updateable<SaleLog>().SetColumns(t => t.IsSync == true).Where(t => t.Id == entity.Id).ExecuteCommand();
                        //    return res;
                        //}
                        //else
                        //{
                            if (DataBus.StartDevice)
                            {
                                Main.GetInstance.Start(input.Location.AsInt());
                                UHF_RS485_Helper.GetInstance().OpenBeep(input.Location.AsInt());
                            }
                            return true;
                        //}
                    }
                }
                return false;
            }
            catch (Exception ex)
            {
                HKLog.HKLogImport.WriteError(ex);
                return false;
            }
        }
        /// <summary>
        /// 获取当天消费前100条展示
        /// </summary>
        /// <returns></returns>
        public List<SaleLog> PullDaySaleLog()
        {
            var Begin = DateTime.Parse(DateTime.Now.ToString("yyyy-MM-dd 00:00:00"));
            var End = DateTime.Parse(DateTime.Now.ToString("yyyy-MM-dd 23:59:59"));
            var data = DbContext.Context.Queryable<SaleLog>()
                   .Where(t => t.CreateTime >= Begin)
                   .Where(t => t.CreateTime < End)
                   .OrderByDescending(t => t.CreateTime).ToPageList(0, 100);
            return data.GroupBy(t => t.CardNo).Select(t => new SaleLog
            {
                CardNo = t.Key,
                Location = String.Join(",", t.Select(m => m.Location)),
                Count = t.Count()
            }).ToList();
        }
    }
}
