using GalaSoft.MvvmLight;
using GalaSoft.MvvmLight.Command;
using Microsoft.Extensions.DependencyInjection;
using NPOI.SS.Formula.Functions;
using SlnMesnac.Business;
using SlnMesnac.Model.domain;
using SlnMesnac.Model.dto;
using SlnMesnac.WPF.MessageTips;
using SlnMesnac.WPF.Model;
using SlnMesnac.WPF.Templates;
using SqlSugar;
using System;
using System.Collections.Generic;
using System.Collections.ObjectModel;
using System.Diagnostics;
using System.Drawing.Printing;
using System.IO;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
//using static DevExpress.Drawing.Printing.Internal.DXPageSizeInfo;


#region << 版 本 注 释 >>
/*--------------------------------------------------------------------
* 版权所有 (c) 2024 WenJY 保留所有权利。
* CLR版本：4.0.30319.42000
* 机器名称：LAPTOP-E0N2L34V
* 命名空间：SlnMesnac.WPF.ViewModel
* 唯一标识：14008fcc-0a31-4f1e-bc80-9f9ea84d3de5
*
* 创建者：WenJY
* 电子邮箱：wenjy@mesnac.com
* 创建时间：2024-04-10 16:18:57
* 版本：V1.0.0
* 描述：
*
*--------------------------------------------------------------------
* 修改人：
* 时间：
* 修改说明：
*
* 版本：V1.0.0
*--------------------------------------------------------------------*/
#endregion << 版 本 注 释 >>
namespace SlnMesnac.WPF.ViewModel
{
    /// <summary>
    /// 生产统计ViewModel
    /// </summary>
    public class MachineStopHistoryViewModel : ViewModelBase
    {
       private readonly ISqlSugarClient? sqlSugarClient;

        public PagenationModel<T> PagenationModel { get; set; }

        private Dictionary<int,string> machineNameDic = new Dictionary<int, string>();
        public MachineStopHistoryViewModel()
        {
            PagenationModel = new PagenationModel<T>()
            {
             
                FindPreCommand = new RelayCommand(OnPreCommand),
                FindNextCommand = new RelayCommand(OnNextCommand),
                FindTargetCommand = new RelayCommand(OnGoCommand)
            };

            sqlSugarClient = App.ServiceProvider.GetService<ISqlSugarClient>();

            InitList();
           
        }

      

        #region 事件定义
        private void OnPreCommand()
        {
            if (PagenationModel.Current == 1) return;
            // 处理前一页逻辑
            PagenationModel.Current = PagenationModel.Current - 1;
            Task.Run(async () => await RefreshWcsTaskDataGrid(PagenationModel.Current, PagenationModel.PageSize));

        }

        private void OnNextCommand()
        {
            // 处理下一页逻辑
            PagenationModel.Current = PagenationModel.Current + 1;
            Task.Run(async () => await RefreshWcsTaskDataGrid(PagenationModel.Current, PagenationModel.PageSize));
        }

        private void OnGoCommand()
        {
            // 处理跳转到指定页逻辑
            // 处理下一页逻辑

            Task.Run(async () => await RefreshWcsTaskDataGrid(PagenationModel.Current, PagenationModel.PageSize));
        }
        #endregion

        private async void InitList()
        {
            machineNameDic.Add(1016, "3楼拆包机");
            machineNameDic.Add(1017, "2楼磁选机");
            machineNameDic.Add(1018, "2楼螺旋机");
            machineNameDic.Add(1019, "2楼烘干机");
            machineNameDic.Add(1020, "2楼除尘机");
            machineNameDic.Add(1021, "2楼喷码机");
            PagenationModel.Total =  await getListTotal();
            PagenationModel.computePage();
            await RefreshWcsTaskDataGrid(PagenationModel.Current, 20);
        }
        private async Task<int> getListTotal()
        {
            List<int> deviceIds = new List<int> { 1016, 1017, 1018, 1019, 1020, 1021 };
            int aa  = await sqlSugarClient.AsTenant().GetConnection("mes").Queryable<DmsRecordShutDown>().Where(x => deviceIds.Contains(x.DeviceId)).CountAsync();
            return aa;
        }



        #region 参数定义

        /// <summary>
        /// 停机记录DataGrid
        /// </summary>
        private ObservableCollection<DmsShutDownModel> dmsShutDownModelDataGrid = new ObservableCollection<DmsShutDownModel>();

        public ObservableCollection<DmsShutDownModel> DmsShutDownModelDataGrid
        {
            get { return dmsShutDownModelDataGrid; }
            set { dmsShutDownModelDataGrid = value; RaisePropertyChanged(() => DmsShutDownModelDataGrid); }

        }
        #endregion



        /// <summary>
        /// 刷新停机记录信息
        /// </summary>
        /// <param name="palletTasks"></param>
        private async Task RefreshWcsTaskDataGrid(int pageNumber=1,int pageSize=0)
        {
        
            try
            {
                PagenationModel.computePage();
                List<int> deviceIds = new List<int> { 1016, 1017, 1018, 1019, 1020, 1021 };
                List<DmsRecordShutDown> list = await sqlSugarClient.AsTenant().GetConnection("mes").Queryable<DmsRecordShutDown>().Where(x=>deviceIds.Contains(x.DeviceId)).OrderByDescending(x=>x.ShutBeginTime).ToPageListAsync(pageNumber,pageSize);

                 await App.Current.Dispatcher.BeginInvoke((Action)(() =>
                 {
                     dmsShutDownModelDataGrid.Clear();
                     int startId = (pageNumber-1)*pageSize+1;
                     foreach (var item in list)
                     {
                         DmsShutDownModel model =  new DmsShutDownModel();
                         model.id = startId++;
                         model.deviceName = machineNameDic[item.DeviceId];
                         model.shutReason = item.ShutReason??"未知"; 
                         model.shutBeginTime = item.ShutBeginTime==null?"":item.ShutBeginTime.Value.ToString("yyyy-MM-dd HH:mm:ss");
                         model.shutEndTime = item.ShutEndTime == null?"":item.ShutEndTime.Value.ToString("yyyy-MM-dd HH:mm:ss");
                         model.totalTime  = item.ShutEndTime == null ? "" : (item.ShutEndTime.Value - item.ShutBeginTime.Value).TotalMinutes.ToString("F2");
                         dmsShutDownModelDataGrid.Add(model);
                     }
                    
                 }));
                
               
            }catch(Exception ex)
            {
                Msg.MsgShow($"刷新停机记录信息{ex.Message}", 2, 3);
              
            }

        }

 
    }
}
