using JiShe.CollectBus.IotSystems.Records;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using JiShe.CollectBus.Common.Consts;
using JiShe.CollectBus.EnergySystems.TableViews;
using JiShe.CollectBus.FreeSql;
using Volo.Abp.Domain.Repositories;
using JiShe.CollectBus.IotSystems.PrepayModel;
using JiShe.CollectBus.Ammeters;
using JiShe.CollectBus.Common.BuildSendDatas;
using JiShe.CollectBus.Common.Enums;
using JiShe.CollectBus.Common.Helpers;

namespace JiShe.CollectBus.EnergySystem
{
    public class CacheAppService : CollectBusAppService, ICacheAppService
    {
        public async Task SetHashByKey()
        {
            var data = await SqlProvider.Instance.Change(DbEnum.EnergyDB).Select<V_FocusAmmeter>().ToListAsync();

            var groupData = data.GroupBy(a => $"{a.FocusAreaCode}{a.FocusAddress}").ToList();

            foreach (var group in groupData)
            {
                await FreeRedisProvider.Instance.HSetAsync($"{RedisConst.CacheAmmeterFocusKey}:{group.Key}", group.ToDictionary(a => $"{a.ID}_{a.Address}", b => b));
            }
        }
    }
}
