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

namespace PVDEMCS.Services.Impl
{
    /// <summary>
    /// 设备数据记录与统计 服务层 实现
    /// </summary>
    internal class EquipmentRecordService : IEquipmentRecordService
    {
        private readonly IEquipmentRecordRepository _equipmentRecordRepository;
        public EquipmentRecordService(IEquipmentRecordRepository equipmentRecordRepository)
        {
            this._equipmentRecordRepository = equipmentRecordRepository;
        }

        #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>
        public Result<PagedList<EquipmentRecord>> GetEquipmentRecordPageList(string equipmentId, string equipmentName, string equipmentCode, string equipmentType, DateTime? begTime, DateTime? endTime, int page, int size)
        {
            var result = this._equipmentRecordRepository.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>
        public Result<List<EquipmentRecord>> GetEquipmentRecordList(string equipmentId, string equipmentName, string equipmentCode, string equipmentType, DateTime? begTime, DateTime? endTime)
        {
            var result = this._equipmentRecordRepository.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>
        /// <returns></returns>
        public Result AddUpdateEquipmentRecord(string equipmentId, string state, DateTime dateTime)
        {
            var result = this._equipmentRecordRepository.AddUpdateEquipmentRecord(equipmentId, state, dateTime);
            return result;
        }

        /// <summary>
        /// 批量添加或更新设备状态记录 
        /// 1.如果设备最新记录状态没有改变则更新结束时间
        /// 2.如果没有记录则添加记录
        /// </summary>
        /// <returns></returns>
        public Result AddUpdateEquipmentRecordBatch(List<EquipmentRecordUpdateModel> items) 
        {
            var result = this._equipmentRecordRepository.AddUpdateEquipmentRecordBatch(items);
            return result;
        }

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

        /// <summary>
        /// 进行设备状态记录统计
        /// </summary>
        /// <returns></returns>
        public Result RunEquipmentRecordTotal()
        {
            var result = this._equipmentRecordRepository.RunEquipmentRecordTotal();
            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>
        public Result<PagedList<EquipmentRecordDayTotal>> GetEquipmentRecordDayTotalPageList(string equipmentName, string equipmentCode, string equipmentType, DateTime? begTime, DateTime? endTime, int page, int size)
        {
            var result = this._equipmentRecordRepository.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>
        public Result<List<EquipmentRecordDayTotal>> GetEquipmentRecordDayTotalList(string equipmentName, string equipmentCode, string equipmentType, DateTime? begTime, DateTime? endTime)
        {
            var result = this._equipmentRecordRepository.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>
        public Result<PagedList<EquipmentRecordTotal>> GetEquipmentRecordTotalPageList(string equipmentName, string equipmentCode, string equipmentType, DateTime? begTime, DateTime? endTime, int page, int size)
        {
            var result = this._equipmentRecordRepository.GetEquipmentRecordTotalPageList(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>
        public Result<List<EquipmentRecordTotal>> GetEquipmentRecordTotalList(string equipmentName, string equipmentCode, string equipmentType, DateTime? begTime, DateTime? endTime)
        {
            var result = this._equipmentRecordRepository.GetEquipmentRecordTotalList(equipmentName, equipmentCode, equipmentType, begTime, endTime);
            return result;
        }


        /// <summary>
        /// 根据设备Id获取记录统计
        /// </summary>
        /// <param name="equipmentId">设备Id</param>
        /// <returns></returns>
        public Result<EquipmentRecordTotal> GetEquipmentRecordTotalDetail(string equipmentId)
        {
            var result = new Result<EquipmentRecordTotal>();

            var value = this._equipmentRecordRepository.GetEquipmentRecordTotalDetail(equipmentId);
            if (!value.IsSuccess)
            {
                result.Message = value.Message;
                return result;
            }
            result.Content = ModelTools.PubClone<EquipmentRecordTotalEntity, EquipmentRecordTotal>.Trans(value.Content);

            return result;
        }

        /// <summary>
        /// 获取设备状态记录月统计
        /// </summary>
        /// <returns></returns>
        public Result<List<EquipmentRecordStateMonthTotal>> GetEquipmentRecordStateMonthTotal()
        {
            var result = this._equipmentRecordRepository.GetEquipmentRecordStateMonthTotal();
            return result;
        }

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

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

        /// <summary>
        /// 获取设备OEE信息
        /// </summary>
        /// <param name="equipmentId">设备Id</param>
        /// <param name="begDate">开始时间</param>
        /// <param name="endDate">结束时间</param>
        /// <returns></returns>
        public Result<EquipmentOEETotal> GetEquipmentOEEMonthTotal(string equipmentId, DateTime begDate, DateTime endDate) 
        {
            var result = this._equipmentRecordRepository.GetEquipmentOEEMonthTotal(equipmentId, begDate, endDate);
            return result;
        }

        #endregion
    }
}
