using Masuit.Tools;
using Masuit.Tools.Models;
using Microsoft.AspNetCore.Mvc;
using Microsoft.AspNetCore.Mvc.RazorPages;
using PVDEMCS.Common;
using PVDEMCS.Common.Constant;
using PVDEMCS.Services;
using PVDEMCS.Services.Models;
using System;

namespace PVDEMCS.Controllers
{
    /// <summary>
    /// 设备数据记录与统计
    /// </summary>
    [ApiController]
    [Route("[controller]/[action]")]
    public class EquipmentRecordController : Controller
    {
        private IEquipmentRecordService _equipmentRecordService;
        private IEquipmentService _equipmentService;

        public EquipmentRecordController(IEquipmentRecordService equipmentRecordService, IEquipmentService equipmentService)
        {
            this._equipmentRecordService = equipmentRecordService;
            this._equipmentService = equipmentService;
        }

        #region 设备状态记录

        /// <summary>
        /// 获取设备状态记录分页列表（分钟）
        /// </summary>
        /// <param name="equipmentId">设备Id</param>
        /// <param name="equipmentName">设备名称</param>
        /// <param name="equipmentCode">设备编号</param>
        /// <param name="equipmentType">设备类型</param>
        /// <param name="begTime">开始时间</param>
        /// <param name="endTime">结束时间</param>
        /// <param name="page">当前页</param>
        /// <param name="size">页大小</param>
        /// <returns></returns>
        [HttpGet]
        public Result<PagedList<EquipmentRecord>> GetEquipmentRecordPageList(string equipmentId, string equipmentName, string equipmentCode, string equipmentType, DateTime? begTime, DateTime? endTime, int page, int size = 20)
        {
            var result = _equipmentRecordService.GetEquipmentRecordPageList(equipmentId, equipmentName, equipmentCode, equipmentType, begTime, endTime, page, size);
            return result;
        }

        /// <summary>
        /// 获取设备状态记录列表（分钟）
        /// </summary>
        /// <param name="equipmentId">设备Id</param>
        /// <param name="equipmentName">设备名称</param>
        /// <param name="equipmentCode">设备编号</param>
        /// <param name="equipmentType">设备类型</param>
        /// <param name="begTime">开始时间</param>
        /// <param name="endTime">结束时间</param>
        /// <returns></returns>
        [HttpGet]
        public Result<List<EquipmentRecord>> GetEquipmentRecordList(string equipmentId, string equipmentName, string equipmentCode, string equipmentType, DateTime? begTime, DateTime? endTime)
        {
            var result = _equipmentRecordService.GetEquipmentRecordList(equipmentId, equipmentName, equipmentCode, equipmentType, begTime, endTime);
            return result;
        }

        /// <summary>
        /// 添加或更新设备状态记录
        /// 1.如果设备最新记录状态没有改变则更新结束时间
        /// 2.如果没有记录则添加记录
        /// </summary>
        /// <param name="equipmentId">设备Id</param>
        /// <param name="state">设备状态，运行:Run，待机:Stop，报警:Alarm</param>
        /// <param name="dateTime">记录时间</param>
        /// <returns></returns>
        //[HttpPost]
        //public Result AddUpdateEquipmentRecord(string equipmentId, string state, DateTime dateTime)
        //{
        //    var result = _equipmentRecordService.AddUpdateEquipmentRecord(equipmentId, state, dateTime);
        //    return result;
        //}

        ///// <summary>
        ///// 进行设备状态记录统计（日统计）
        ///// </summary>
        ///// <param name="date"></param>
        ///// <returns></returns>
        //[HttpPost]
        //public Result RunEquipmentRecordTotal(DateTime date)
        //{
        //    var result = _equipmentRecordService.RunEquipmentRecordTotal(date);
        //    return result;
        //}

        #endregion

        #region 设备记录统计

        /// <summary>
        /// 获取设备状态记录统计（日）分页列表（分钟）
        /// </summary>
        /// <param name="equipmentName">设备名称</param>
        /// <param name="equipmentCode">设备编号</param>
        /// <param name="equipmentType">设备类型</param>
        /// <param name="begTime">开始时间</param>
        /// <param name="endTime">结束时间</param>
        /// <param name="page">当前页</param>
        /// <param name="size">页大小</param>
        [HttpGet]
        public Result<PagedList<EquipmentRecordDayTotal>> GetEquipmentRecordDayTotalPageList(string equipmentName, string equipmentCode, string equipmentType, DateTime? begTime, DateTime? endTime, int page, int size = 20)
        {
            var result = _equipmentRecordService.GetEquipmentRecordDayTotalPageList(equipmentName, equipmentCode, equipmentType, begTime, endTime, page, size);
            return result;
        }

        /// <summary>
        /// 获取设备状态记录统计（日）列表（分钟）
        /// </summary>
        /// <param name="equipmentName">设备名称</param>
        /// <param name="equipmentCode">设备编号</param>
        /// <param name="equipmentType">设备类型</param>
        /// <param name="begTime">开始时间</param>
        /// <param name="endTime">结束时间</param>
        [HttpGet]
        public Result<List<EquipmentRecordDayTotal>> GetEquipmentRecordDayTotalList(string equipmentName, string equipmentCode, string equipmentType, DateTime? begTime, DateTime? endTime)
        {
            var result = _equipmentRecordService.GetEquipmentRecordDayTotalList(equipmentName, equipmentCode, equipmentType, begTime, endTime);
            return result;
        }

        /// <summary>
        /// 获取设备状态记录统计分页列表（小时）
        /// </summary>
        /// <param name="equipmentName">设备名称</param>
        /// <param name="equipmentCode">设备编号</param>
        /// <param name="equipmentType">设备类型</param>
        /// <param name="begTime">开始时间</param>
        /// <param name="endTime">结束时间</param>
        /// <param name="page">当前页</param>
        /// <param name="size">页大小</param>
        [HttpGet]
        public Result<PagedList<EquipmentRecordTotal>> GetEquipmentRecordTotalPageList(string equipmentName, string equipmentCode, string equipmentType, DateTime? begTime, DateTime? endTime, int page, int size = 20)
        {
            var result = _equipmentRecordService.GetEquipmentRecordTotalPageList(equipmentName, equipmentCode, equipmentType, begTime, endTime, page, size);
            if (result.IsSuccess)
            {
                foreach (var item in result.Content.Data)
                {
                    item.TotalAlarmTime = Math.Round(item.TotalAlarmTime / 60, 2);
                    item.TotalStopTime = Math.Round(item.TotalStopTime / 60, 2);
                    item.TotalRunningTime = Math.Round(item.TotalRunningTime / 60, 2);
                }
            }
            return result;
        }

        /// <summary>
        /// 获取设备状态记录统计列表（小时）
        /// </summary>
        /// <param name="equipmentName">设备名称</param>
        /// <param name="equipmentCode">设备编号</param>
        /// <param name="equipmentType">设备类型</param>
        /// <param name="begTime">开始时间</param>
        /// <param name="endTime">结束时间</param>
        [HttpGet]
        public Result<List<EquipmentRecordTotal>> GetEquipmentRecordTotalList(string equipmentName, string equipmentCode, string equipmentType, DateTime? begTime, DateTime? endTime)
        {
            var result = _equipmentRecordService.GetEquipmentRecordTotalList(equipmentName, equipmentCode, equipmentType, begTime, endTime);
            if (result.IsSuccess)
            {
                foreach (var item in result.Content)
                {
                    item.TotalAlarmTime = Math.Round(item.TotalAlarmTime / 60, 2);
                    item.TotalStopTime = Math.Round(item.TotalStopTime / 60, 2);
                    item.TotalRunningTime = Math.Round(item.TotalRunningTime / 60, 2);
                }
            }
            return result;
        }

        /// <summary>
        /// 根据设备Id获取记录统计（小时）
        /// </summary>
        /// <param name="equipmentId">设备Id</param>
        /// <returns></returns>
        [HttpGet]
        public Result<EquipmentRecordTotal> GetEquipmentRecordTotalDetail(string equipmentId)
        {
            var result = _equipmentRecordService.GetEquipmentRecordTotalDetail(equipmentId);
            if (result.IsSuccess && !result.Content.IsNullOrEmpty())
            {
                result.Content.TotalAlarmTime = Math.Round(result.Content.TotalAlarmTime / 60, 2);
                result.Content.TotalStopTime = Math.Round(result.Content.TotalStopTime / 60, 2);
                result.Content.TotalRunningTime = Math.Round(result.Content.TotalRunningTime / 60, 2);
            }
            return result;
        }

        /// <summary>
        /// 获取设备OEE信息
        /// </summary>
        /// <param name="equipmentId">设备Id</param>
        /// <param name="begDate">开始时间</param>
        /// <param name="endDate">结束时间</param>
        /// <returns></returns>
        [HttpGet]
        public Result<EquipmentOEETotal> GetEquipmentOEEMonthTotal(string equipmentId, DateTime begDate, DateTime endDate)
        {
            var result = _equipmentRecordService.GetEquipmentOEEMonthTotal(equipmentId, begDate, endDate);
            if (result.IsSuccess && !result.Content.IsNullOrEmpty())
            {
                result.Content.TotalAlarmTime = Math.Round(result.Content.TotalAlarmTime / 60, 2);
                result.Content.TotalStopTime = Math.Round(result.Content.TotalStopTime / 60, 2);
                result.Content.TotalRunningTime = Math.Round(result.Content.TotalRunningTime / 60, 2);
            }
            return result;
        }

        /// <summary>
        /// 获取设备总览
        /// </summary>
        /// <returns></returns>
        [HttpGet]
        public Result<List<EquipmentStateView>> GetEquipmentStateView()
        {
            var equipments = _equipmentService.GetEquipmentList();
            var ret = new List<EquipmentStateView>();
            //获取所有设备状态汇总
            var allTotal = equipments.Content.GroupBy(f => f.State).Select(f => new
            {
                State = f.Key,
                count = f.Count()
            }).ToList();
            var equipmentStateView = new EquipmentStateView();
            equipmentStateView.EquipmentType = "All";
            var alarm = allTotal.Where(f => f.State == EquipmentState.Alarm).FirstOrDefault();
            if (!alarm.IsNullOrEmpty())
            {
                equipmentStateView.Alarm = alarm.count;
            }
            var run = allTotal.Where(f => f.State == EquipmentState.Run).FirstOrDefault();
            if (!run.IsNullOrEmpty())
            {
                equipmentStateView.Run = run.count;
            }
            var stop = allTotal.Where(f => f.State == EquipmentState.Stop).FirstOrDefault();
            if (!stop.IsNullOrEmpty())
            {
                equipmentStateView.Stop = stop.count;
            }
            ret.Add(equipmentStateView);
            //根据设备类型获取状态汇总
            var equimentTypeTotal = equipments.Content.GroupBy(f => new { f.EquipmentType, f.State }).Select(f => new
            {
                equipmentType = f.Key.EquipmentType,
                State = f.Key.State,
                count = f.Count()
            }).ToList();
            var equimentTypes = new string[] { EquipmentType.Homegrown, EquipmentType.Import };
            foreach (var type in equimentTypes)
            {
                equipmentStateView = new EquipmentStateView();
                equipmentStateView.EquipmentType = type;
                if (equimentTypeTotal.Count > 0)
                {
                    var alarmType = equimentTypeTotal.Where(f => f.State == EquipmentState.Alarm && f.equipmentType == type).FirstOrDefault();
                    if (!alarmType.IsNullOrEmpty())
                    {
                        equipmentStateView.Alarm = alarmType.count;
                    }
                    var runType = equimentTypeTotal.Where(f => f.State == EquipmentState.Run && f.equipmentType == type).FirstOrDefault();
                    if (!runType.IsNullOrEmpty())
                    {
                        equipmentStateView.Run = runType.count;
                    }
                    var stopType = equimentTypeTotal.Where(f => f.State == EquipmentState.Stop && f.equipmentType == type).FirstOrDefault();
                    if (!stopType.IsNullOrEmpty())
                    {
                        equipmentStateView.Stop = stopType.count;
                    }
                }
                ret.Add(equipmentStateView);
            }

            return new Result<List<EquipmentStateView>>(ret);
        }

        /// <summary>
        /// 获取报警设备列表
        /// </summary>
        /// <returns></returns>
        [HttpGet]
        public Result<List<EquipmentInfo>> GetEquipmentAlarmList()
        {
            var equipments = _equipmentService.GetEquipmentList();
            var alarmList = equipments.Content.Where(f => f.State == EquipmentState.Alarm).ToList();
            return new Result<List<EquipmentInfo>>(alarmList);
        }

        /// <summary>
        /// 获取设备状态记录月统计（小时）
        /// </summary>
        /// <returns></returns>
        [HttpGet]
        public Result<List<EquipmentRecordStateMonthTotal>> GetEquipmentRecordStateMonthTotal()
        {
            var result = _equipmentRecordService.GetEquipmentRecordStateMonthTotal();
            if (result.IsSuccess)
            {
                foreach (var item in result.Content)
                {
                    item.TotalAlarmTime = Math.Round(item.TotalAlarmTime / 60, 2);
                    item.TotalStopTime = Math.Round(item.TotalStopTime / 60, 2);
                    item.TotalRunningTime = Math.Round(item.TotalRunningTime / 60, 2);
                }
            }
            return result;
        }

        /// <summary>
        /// 获取设备记录开炉次数月统计
        /// </summary>
        /// <returns></returns>
        [HttpGet]
        public Result<List<EquipmentRecordFurnaceMonthTotal>> GetEquipmentRecordFurnaceMonthTotal()
        {
            var result = _equipmentRecordService.GetEquipmentRecordFurnaceMonthTotal();
            return result;
        }

        /// <summary>
        /// 获取设备记录OEE月统计
        /// </summary>
        /// <returns></returns>
        [HttpGet]
        public Result<List<EquipmentOEEMonthTotal>> GetEquipmentRecordOEEMonthTotal()
        {
            var result = _equipmentRecordService.GetEquipmentRecordOEEMonthTotal();
            return result;
        }

        #endregion
    }
}
