using Autofac;
using Entity.Ammeter;
using HybirdFrameworkCore.Autofac;
using HybirdFrameworkCore.Autofac.Attribute;
using HybirdFrameworkCore.Job;
using log4net;
using Repository.Ammeter;
using Repository.System;

namespace Service.Job;

/// <summary>
/// 分钟统计
/// </summary>
[Scope]
public class EmeterMinuteJob : AbstractCronJob
{
    private static readonly ILog Log = LogManager.GetLogger(typeof(EmeterMinuteJob));


    public EmeterEnergyRepository EmeterEnergyRepository = AppInfo.Container.Resolve<EmeterEnergyRepository>();

    public EmeterEnergyChangeRepository EmeterEnergyChangeRepository  =
        AppInfo.Container.Resolve<EmeterEnergyChangeRepository>();

    public EmeterMinutesEnergyRepository EmeterMinutesEnergyRepository =
        AppInfo.Container.Resolve<EmeterMinutesEnergyRepository>();


    protected override Task Handle()
    {
        var now = DateTime.Now;
        Log.Info($"EmeterMinuteJob execute start time={now}");
        //查询上一分钟的数据
        string dateTime = now.AddMinutes(-1).ToString("yyyy-MM-dd HH:mm");

        List<EmeterEnergy> emeterEnergies = EmeterEnergyRepository.QueryListByClause(i =>
            dateTime.Equals(i.UploadTime.ToString("yyyy-MM-dd HH:mm")), "upload_time desc");

        List<EmeterEnergyChange> emeterEnergiesChange = EmeterEnergyChangeRepository.QueryListByClause(i =>
            dateTime.Equals(i.UploadTime.ToString("yyyy-MM-dd HH:mm")), "upload_time desc");

        List<EmeterMinutesEnergy> db = new List<EmeterMinutesEnergy>();
        if (emeterEnergies.Count > 0)
        {
            Dictionary<string, List<EmeterEnergy>> dictionary =
                emeterEnergies.GroupBy(i => i.Code).ToDictionary(i => i.Key, i => i.ToList());

            foreach (var keyValuePair in dictionary)
            {
                EmeterMinutesEnergy emeterMinutesEnergy = new EmeterMinutesEnergy();
                emeterMinutesEnergy.Code = keyValuePair.Key;
                emeterMinutesEnergy.Time = dateTime;
                emeterMinutesEnergy.Type = 1;
                var value = keyValuePair.Value;
                var orderByDescending = value.OrderByDescending(i => i.UploadTime).ToList();
                emeterMinutesEnergy.RealTimeValue = orderByDescending[0].Value;
                emeterMinutesEnergy.Value = orderByDescending[0].Value -
                                            orderByDescending[^1].Value;
                db.Add(emeterMinutesEnergy);
            }
        }

        if (emeterEnergiesChange.Count > 0)
        {
            Dictionary<string, List<EmeterEnergyChange>> dictionaryChange =
                emeterEnergiesChange.GroupBy(i => i.Code).ToDictionary(i => i.Key, i => i.ToList());

            foreach (var keyValuePair in dictionaryChange)
            {
                EmeterMinutesEnergy emeterMinutesEnergy = new EmeterMinutesEnergy();
                emeterMinutesEnergy.Code = keyValuePair.Key;
                emeterMinutesEnergy.Time = dateTime;
                emeterMinutesEnergy.Type = 2;
                var value = keyValuePair.Value;
                var orderByDescending = value.OrderByDescending(i => i.UploadTime).ToList();
                emeterMinutesEnergy.RealTimeValue = orderByDescending[0].Value;
                emeterMinutesEnergy.Value = orderByDescending[0].Value -
                                            orderByDescending[^1].Value;
                db.Add(emeterMinutesEnergy);
            }
        }

        if (db.Count > 0)
        {
            var existingData = EmeterMinutesEnergyRepository.QueryListByClause(i =>
                dateTime.Equals(i.Time));
            if (!existingData.Any())
            {
                EmeterMinutesEnergyRepository.Insert(db);
            }
        }

    
        Log.Info($"EmeterMinuteJob execute end ");
        return Task.CompletedTask;
    }

    protected override string Key()
    {
        return "Service.Job.EmeterMinuteJob";
    }

    protected override string Cron()
    {
        return "0 * * * * ? *";
    }
}