package com.bonus.core.device.manage.service;

import cn.hutool.core.map.MapUtil;
import cn.hutool.core.util.ObjectUtil;
import com.alibaba.fastjson.JSON;
import com.alibaba.fastjson.JSONObject;
import com.baomidou.mybatisplus.core.conditions.Wrapper;
import com.baomidou.mybatisplus.core.conditions.update.LambdaUpdateWrapper;
import com.baomidou.mybatisplus.core.toolkit.Wrappers;
import com.bonus.core.common.utils.SysUtil;
import com.bonus.core.device.manage.mapper.DeviceMetadataMapper;
import com.bonus.core.device.manage.model.DeviceMetadata;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.lang.invoke.SerializedLambda;
import java.util.Iterator;
import java.util.List;
import java.util.Map;
import java.util.function.Function;
import java.util.stream.Collectors;

@Service
public class DeviceMetadataService {
   @Autowired
   private DeviceMetadataMapper deviceMetadataMapper;
   private static final String CACHE_PREFIX = "yst:";
   private static final String CACHE_KEY = "device-mata-data";
   private static final long CACHE_SECONDS = 3600L;

//   private List<DeviceMetadata> listDeviceMetadataCacheable(Long deviceId) {
//      String cacheKey = this.getCacheKey(deviceId);
//      Object cacheObj = RedisUtil.getObj(cacheKey);
//      if (cacheObj instanceof List) {
//         return (List)cacheObj;
//      } else {
//         List<DeviceMetadata> selectList = this.deviceMetadataMapper.selectList((Wrapper)Wrappers.lambdaQuery(DeviceMetadata.class).eq(DeviceMetadata::getDeviceId, deviceId));
//         RedisUtil.setObj(cacheKey, selectList, 3600L);
//         return selectList;
//      }
//   }
//
//   public Map<String, Object> listMapDeviceMetadataCacheable(Long deviceId) {
//      Map<String, Object> map = MapUtil.newHashMap();
//      List<DeviceMetadata> deviceMetadataList = this.listDeviceMetadataCacheable(deviceId);
//      Iterator var4 = deviceMetadataList.iterator();
//
//      while(var4.hasNext()) {
//         DeviceMetadata deviceMetadata = (DeviceMetadata)var4.next();
//         map.put(deviceMetadata.getModelKey(), deviceMetadata.getModelValue());
//      }
//
//      return map;
//   }

   public Map<Long, Map> listDeviceMetadataBYDeviceIdList(List<Long> deviceIdList) {
      List<DeviceMetadata> selectList = this.deviceMetadataMapper.selectList(Wrappers.lambdaQuery(DeviceMetadata.class)
              .in(DeviceMetadata::getDeviceId, deviceIdList));
      Map<Long, List<DeviceMetadata>> longListMap = selectList.stream().collect(Collectors
              .groupingBy(DeviceMetadata::getDeviceId));
      Map<Long, Map> longMapMap = MapUtil.newHashMap();
      Iterator var5 = longListMap.keySet().iterator();

      while(var5.hasNext()) {
         Long key = (Long)var5.next();
         Map<String, Object> map = MapUtil.newHashMap();
         List<DeviceMetadata> deviceMetadataList = longListMap.get(key);
         Iterator var9 = deviceMetadataList.iterator();

         while(var9.hasNext()) {
            DeviceMetadata deviceMetadata = (DeviceMetadata)var9.next();
            if (ObjectUtil.equal(deviceMetadata.getModelKey(), "imageMap")) {
               List<String> list = JSON.parseArray(deviceMetadata.getModelValue(), String.class);
               List<String> listadd = list.stream().map((s) -> {
                  return SysUtil.getCutFileUrl(s);
               }).collect(Collectors.toList());
               map.put(deviceMetadata.getModelKey(), JSONObject.toJSON(listadd).toString());
            } else {
               map.put(deviceMetadata.getModelKey(), deviceMetadata.getModelValue());
            }
         }

         longMapMap.put(key, map);
      }

      return longMapMap;
   }

//   public void saveDeviceMetadata(List<DeviceMetadata> deviceMetadataList) {
//      Map<String, DeviceMetadata> metadataMap = (Map)this.deviceMetadataMapper.selectList(Wrappers.lambdaQuery(DeviceMetadata.class)).stream().collect(Collectors.toMap(DeviceMetadata::uniqueKey, Function.identity()));
//      Iterator var3 = deviceMetadataList.iterator();
//
//      while(var3.hasNext()) {
//         DeviceMetadata newDeviceMetadata = (DeviceMetadata)var3.next();
//         DeviceMetadata oldMetadata = (DeviceMetadata)metadataMap.get(newDeviceMetadata.uniqueKey());
//         if (ObjectUtil.isNull(oldMetadata)) {
//            newDeviceMetadata.setMetadataId(Id.next());
//            this.deviceMetadataMapper.insert(newDeviceMetadata);
//         } else {
//            this.deviceMetadataMapper.update((Object)null, (Wrapper)((LambdaUpdateWrapper)((LambdaUpdateWrapper)Wrappers.lambdaUpdate(DeviceMetadata.class).eq(DeviceMetadata::getMetadataId, oldMetadata.getMetadataId())).set(DeviceMetadata::getModelValue, newDeviceMetadata.getModelValue())).set(DeviceMetadata::getModelKeyRemark, newDeviceMetadata.getModelKeyRemark()));
//         }
//      }
//
//      this.clearCache();
//   }
//
//   private String getCacheKey(Long deviceId) {
//      Long var10000 = TenantContextHolder.getTenantId();
//      return "yst:" + var10000 + ":device-mata-data:" + deviceId;
//   }
//
//   private void clearCache() {
//      RedisUtil.deleteByPattern("yst:" + TenantContextHolder.getTenantId() + ":device-mata-data:*");
//   }
//
//   public void deleteByDevice(Long deviceId) {
//      if (ObjectUtil.isNotEmpty(deviceId)) {
//         this.deviceMetadataMapper.delete((Wrapper)Wrappers.lambdaQuery(DeviceMetadata.class).eq(DeviceMetadata::getDeviceId, deviceId));
//         RedisUtil.delete(this.getCacheKey(deviceId));
//      }
//
//   }
}
