package com.huimv.eartag.service.impl;

import com.alibaba.fastjson.JSON;
import com.alibaba.fastjson.JSONObject;
import com.huimv.eartag.dao.entity.*;
import com.huimv.eartag.utils.BizConst;
import com.huimv.eartag.service.ICacheService;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.data.redis.core.RedisTemplate;
import org.springframework.stereotype.Service;

import java.text.ParseException;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
import java.util.Set;
import java.util.concurrent.TimeUnit;

/**
 * @Project : huimv.shiwan
 * @Package : com.huimv.biosafety.uface.controller
 * @Description : TODO
 * @Version : 1.0
 * @Author : ZhuoNing
 * @Create : 2020-12-25
 **/
@Service
@Slf4j
public class CacheServiceImpl implements ICacheService {
    @Autowired
    private RedisTemplate redisTemplate;
    @Value("${redis.expire.eartag_online_set}")
    private Integer eartagOnlineSetExpire;

    @Override
    public Object getDeviceCodeByChipId(String chipId) {
        String  hashKey = BizConst.CHIP_DEVICE_PREFIX;
        return redisTemplate.opsForHash().get(hashKey,chipId);
    }

    @Override
    public boolean checkEffectiveDeviceSet(String idCode) {
        String setKey = BizConst.SET_ALL_DEVICE_PREFIX;
        return redisTemplate.opsForSet().isMember(setKey,idCode);
    }

    //# 读根据设备编码读取牧场id
    @Override
    public Object getFarmIdByDeviceCode(String deviceCode) {
        Object farmIdObj = redisTemplate.opsForHash().get(BizConst.DEVICE_REGISTER_PREFIX + deviceCode,"farmId");
        return farmIdObj;
    }

    @Override
    public void putDeviceRegister(String deviceCode, EartagDeviceRegisterEntity deviceRegisterEntity) {
        //--更新redis中注册表数据
        String deviceRegHashKey = BizConst.DEVICE_REGISTER_PREFIX + deviceCode;
        //将Entity转为Map
        Map registerMap = RegisterEntityToMap(deviceRegisterEntity);
        redisTemplate.opsForHash().putAll(deviceRegHashKey, registerMap);
    }

    /**
     * @Method : RegisterEntityToMap
     * @Description :
     * @Params : [deviceRegisterEntity]
     * @Return : java.util.Map
     * @Author : ZhuoNing
     * @Date : 2022/2/21
     * @Time : 14:03
     */
    private Map RegisterEntityToMap(EartagDeviceRegisterEntity deviceRegisterEntity) {
        Map map = new HashMap();
        map.put("Id", deviceRegisterEntity.getId());
        map.put("deviceCode", deviceRegisterEntity.getDeviceCode());
        map.put("farmId", deviceRegisterEntity.getFarmId());
        map.put("farmName", deviceRegisterEntity.getFarmName());
        map.put("typeF", deviceRegisterEntity.getTypeF());
        map.put("stage", deviceRegisterEntity.getStage());
        map.put("pigpenId", deviceRegisterEntity.getPigpenId());
        map.put("unitId", deviceRegisterEntity.getUnitId());
        map.put("registerTime", deviceRegisterEntity.getRegisterTime());
        map.put("lastTime", deviceRegisterEntity.getLastTime());
        map.put("activeStatus", deviceRegisterEntity.getActiveStatus());
        map.put("activeTime", deviceRegisterEntity.getActiveTime());
        map.put("deviceStatus", deviceRegisterEntity.getDeviceStatus());
        map.put("deviceAlias", deviceRegisterEntity.getDeviceAlias());
        map.put("location", deviceRegisterEntity.getLocation());
        map.put("remark", deviceRegisterEntity.getRemark());
        return map;
    }

    @Override
    public void putEartagFlowToCache(String earmark, EartagDataEntity dataEntity) {
        Map<String, String> map = new HashMap<>();
        map.put("cmdHeader", dataEntity.getCmdHeader());
        map.put("device", dataEntity.getDevice());
        map.put("earmark", dataEntity.getEarmark());
        map.put("bat", dataEntity.getBat().toString());
        map.put("earTemp", dataEntity.getEarTemp().toString());
        map.put("earTemp1", dataEntity.getEarTemp1().toString());
        map.put("envTemp", dataEntity.getEnvTemp().toString());
        map.put("envTemp1", dataEntity.getEnvTemp1().toString());
        map.put("act", dataEntity.getAct().toString());
        map.put("act1", dataEntity.getAct1().toString());
        map.put("signal1", dataEntity.getSignal1().toString());
        map.put("askTime", dataEntity.getAskTime());
        map.put("other", dataEntity.getOther());
        map.put("addTime", dataEntity.getAddTime().toString());
        //为hash结构设置多个键值对(hmset)
        redisTemplate.opsForHash().putAll(BizConst.EARTAG_FLOW_PREFIX+earmark, map);
    }

    @Override
    public void putEartagRegister(String earmark, EartagEartagRegisterEntity eartagRegisterEntity) {
        Map map = new HashMap();
//        map.put("id", eartagRegisterEntity.getId());
        map.put("earmark",eartagRegisterEntity.getEarmark());
        map.put("firstTime",eartagRegisterEntity.getFirstTime());
        map.put("lastTime",eartagRegisterEntity.getLastTime());
        map.put("firstDevice",eartagRegisterEntity.getFirstDevice());
        map.put("belongDevice",eartagRegisterEntity.getBelongDevice());
        map.put("lastDevice",eartagRegisterEntity.getLastDevice());
        map.put("registerTime",eartagRegisterEntity.getRegisterTime());
        map.put("registerType",eartagRegisterEntity.getRegisterType());
        map.put("remark",eartagRegisterEntity.getRemark());
        map.put("farmId",eartagRegisterEntity.getFarmId());
        map.put("activeStatus",eartagRegisterEntity.getActiveStatus());
        map.put("activeTime",eartagRegisterEntity.getActiveTime());
        map.put("liveStatus",eartagRegisterEntity.getLiveStatus());
        //更新缓存
        String earmarkOnlineKey = BizConst.EARTAG_REGISTER_PREFIX + earmark;
        redisTemplate.opsForHash().putAll(earmarkOnlineKey, map);
    }

    @Override
    public void putEartagTotal(Integer total, String farmId) {
        String valueKey = BizConst.FARM_EARTAG_TOTAL_PREFIX+farmId;
        redisTemplate.opsForValue().set(valueKey,total);
    }

    @Override
    public Object getHashProperty(String hashKey, String fieldName) {
        Object obj = redisTemplate.opsForHash().get(hashKey, fieldName);
        return obj;
    }

    /**
     * @Method      : putDeviceInOnline
     * @Description : 将数据库中的在线数据更新对应的缓存数据中
     * @Params      : [deviceOnlineHashKey, eartagDeviceOnlineEntity]
     * @Return      : void
     *
     * @Author      : ZhuoNing
     * @Date        : 2022/3/1
     * @Time        : 16:07
     */
    @Override
    public void putDeviceInOnline(String deviceCode, EartagDeviceOnlineEntity eartagDeviceOnlineEntity) {
        Map map = new HashMap();
//        map.put("id", eartagDeviceOnlineEntity.getId());
        map.put("deviceCode", eartagDeviceOnlineEntity.getDeviceCode());
        map.put("alias", eartagDeviceOnlineEntity.getAlias());
        map.put("heartbeatTotal", eartagDeviceOnlineEntity.getHeartbeatTotal());
        map.put("envtempTotal", eartagDeviceOnlineEntity.getEnvtempTotal());
        map.put("eartagTotal", eartagDeviceOnlineEntity.getEartagTotal());
        map.put("firstTime", eartagDeviceOnlineEntity.getFirstTime());
        map.put("lastTime", eartagDeviceOnlineEntity.getLastTime());
        map.put("lastEnvtemp", eartagDeviceOnlineEntity.getLastEnvtemp());
        map.put("lastEartag", eartagDeviceOnlineEntity.getLastEartag());
        map.put("addDate", eartagDeviceOnlineEntity.getAddDate());
        map.put("farmId", eartagDeviceOnlineEntity.getFarmId());
        //更新缓存
        String deviceOnlineKey = BizConst.DEVICE_ONLINE_PREFIX + deviceCode;
        redisTemplate.opsForHash().putAll(deviceOnlineKey, map);
//        Map onlineMap = fromOnlineEntityToMap(newDeviceOnlineEntity);
//        redisTemplate.opsForHash().putAll(deviceOnlineHashKey, onlineMap);
    }

    /**
     * @Method      : putEartagOnlineCount
     * @Description :
     * @Params      : [earmark, eartagOnlineEntity]
     * @Return      : void
     *
     * @Author      : ZhuoNing
     * @Date        : 2022/3/3
     * @Time        : 15:29
     */
    @Override
    public void putEartagOnlineCount(String earmark, EartagEartagOnlineEntity eartagOnlineEntity) {
        Map map = new HashMap();
//        map.put("id", eartagRegisterEntity.getId());
        map.put("earmark",eartagOnlineEntity.getEarmark());
        map.put("total",eartagOnlineEntity.getTotal());
        map.put("firstTime",eartagOnlineEntity.getFirstTime());
        map.put("lastTime",eartagOnlineEntity.getLastTime());
        map.put("envTemp",eartagOnlineEntity.getEnvTemp());
        map.put("eartagTemp",eartagOnlineEntity.getEartagTemp());
        map.put("addDate",eartagOnlineEntity.getAddDate());
        map.put("farmId",eartagOnlineEntity.getFarmId());
        //更新缓存
        String earmarkOnlineKey = BizConst.EARTAG_ONLINE_PREFIX + earmark;
        redisTemplate.opsForHash().putAll(earmarkOnlineKey, map);
    }

    @Override
    public void countEartagDeviceOnlineTimes(String earmark, String deviceCode, int total, EartagDeviceEartagCountEntity deviceEartagCountEntity) {
        String hashKey = BizConst.ONLINE_EARMARK_DEVICE_PREFIX+earmark+":"+deviceCode;
//        redisTemplate.opsForHash().increment(hashKey,"total",1);  // Error

//        Integer total = (Integer) getHashKeyProperty(hashKey, "total");
//        putHashKeyProperty(hashKey,"total",++total);
        Map map = getHashEntries(hashKey);
        if(map.size()>0){
            putHashKeyProperty(hashKey,"total",total);
        }else{
            Map newMap = JSON.parseObject(JSONObject.toJSONString(deviceEartagCountEntity),Map.class);
            redisTemplate.opsForHash().putAll(hashKey,newMap);
        }
    }

    @Override
    public Map<Object, Object> getHashEntries(String hashKey) {
//        String hashKey = BizConst.DEVICE_REGISTER_PREFIX+deviceCode;
        Map<Object, Object> entriesMap = redisTemplate.opsForHash().entries(hashKey);
        return entriesMap;
    }

    @Override
    public void putHashKeyProperty(String hashKey, String fieldName, Object value){
        redisTemplate.opsForHash().put(hashKey,fieldName,value);
    }

    @Override
    public boolean isExistEartagOnlineSet(String farmId, String earmark, String todayDateText) {
//        redisTemplate.opsForSet().add(BizConst.SET_FARM_DEVICE_ONLINE_PREFIX + farmId+"_"+todayDateText, earmark);
        String setKey = BizConst.SET_FARM_EARMARK_ONLINE_PREFIX + farmId+":"+todayDateText;
//        System.out.println("## eartag.setKey .isExistEartagOnlineSet >>"+setKey+",earmark>>"+earmark);
        return redisTemplate.opsForSet().isMember(setKey, earmark);
    }

    @Override
    public void putEartagDeviceOnlineCount(String earmark, String deviceCode, String todayDateText, EartagDeviceEartagCountEntity deviceEartagCountEntity){
        //将实体类对象转成Map对象
        Map map = JSON.parseObject(JSONObject.toJSONString(deviceEartagCountEntity),Map.class);
//        Map map2 = (Map)JSONObject.parse(JSONObject.toJSONString(deviceEartagCountEntity));
//        Map map3 = (Map)JSON.parse(JSONObject.toJSONString(deviceEartagCountEntity));
//        Map map4 = (Map)JSON.parseObject(JSONObject.toJSONString(deviceEartagCountEntity));
//        Map<String, Object> map = JSON.parseObject(JSONObject.toJSONString(deviceEartagCountEntity), new TypeReference<Map<String, Object>>(){} );

        //        String hashKey = BizConst.ONLINE_EARMARK_DEVICE_PREFIX+earmark+":"+deviceCode+":"+todayDateText;
        String hashKey = BizConst.ONLINE_EARMARK_DEVICE_PREFIX+earmark+":"+deviceCode;
        redisTemplate.opsForHash().putAll(hashKey,map);
    }

    @Override
    public Integer getDeviceTotal(String farmId) {
        String valueKey = BizConst.FARM_DEVICE_TOTAL_PREFIX+farmId;
//        String value = (String) redisTemplate.opsForValue().get(valueKey);
//        System.out.println("## value>>"+value);
//        redisTemplate.opsForValue().set(valueKey,"50");
        Object obj = redisTemplate.opsForValue().get(valueKey);
        if(obj == null){
            return null;
        }else{
            return Integer.parseInt(String.valueOf(obj));
        }
    }

    @Override
    public void putDeviceTotal(Integer total, String farmId) {
        String valueKey = BizConst.FARM_DEVICE_TOTAL_PREFIX+farmId;
        redisTemplate.opsForValue().set(valueKey,total);
    }

    @Override
    public Integer getDeviceOnlineSum(String farmId, String todayDateText) {
        String setKey = BizConst.SET_FARM_DEVICE_ONLINE_PREFIX + farmId+":"+todayDateText;
        Set<String> resultSet = redisTemplate.opsForSet().members(setKey);
        return resultSet.size();
    }

    @Override
    public Integer getDeviceCancel(String farmId) {
        String valueKey = BizConst.FARM_DEVICE_CANCEL_PREFIX+farmId;
//        String value = (String) redisTemplate.opsForValue().get(valueKey);
        Object obj = redisTemplate.opsForValue().get(valueKey);
        if(obj == null){
            return null;
        }else{
            return Integer.parseInt(String.valueOf(obj));
        }
    }

    @Override
    public void putDeviceCancel(Integer total, String farmId) {
        String valueKey = BizConst.FARM_DEVICE_CANCEL_PREFIX+farmId;
        redisTemplate.opsForValue().set(valueKey,total);
    }

    @Override
    public void flashAllStatusCache(String farmId, EartarFarmAllStatusEntity farmAllStatusEntity) {
        Map allStatusMap = new HashMap();
//        allStatusMap.put("id",farmAllStatusEntity.getId());
        allStatusMap.put("deviceTotal",farmAllStatusEntity.getDeviceTotal());
        allStatusMap.put("deviceOnline",farmAllStatusEntity.getDeviceOnline());
        allStatusMap.put("deviceOffline",farmAllStatusEntity.getDeviceOffline());
        allStatusMap.put("deviceRate",farmAllStatusEntity.getDeviceRate());
        allStatusMap.put("deviceCancel",farmAllStatusEntity.getDeviceCancel());
        allStatusMap.put("eartagTotal",farmAllStatusEntity.getEartagTotal());
        allStatusMap.put("eartagOnline",farmAllStatusEntity.getEartagOnline());
        allStatusMap.put("farmId",farmId);
//        allStatusMap.put("updateTime",duObj.formatDateTime(duObj.getTodayText()));
//        allStatusMap.put("createDate",duObj.getTodayDate());
        allStatusMap.put("updateTime",farmAllStatusEntity.getUpdateTime());
        allStatusMap.put("createDate",farmAllStatusEntity.getCreateDate());

        redisTemplate.opsForHash().putAll(BizConst.FARM_ALL_STATUS_PREFIX+farmId, allStatusMap);
    }

    @Override
    public Integer getEartagTotal(String farmId) {
        String valueKey = BizConst.FARM_EARTAG_TOTAL_PREFIX+farmId;
        Object obj = redisTemplate.opsForValue().get(valueKey);
        if(obj != null){
            return Integer.parseInt(String.valueOf(obj));
        }else{
            return null;
        }
    }

    /**
     * @Method      : getEartagOnlineTotalFromCache
     * @Description : 返回集合缓存的数量；
     * @Params      : [farmId, todayDateText]
     * @Return      : void
     *
     * @Author      : ZhuoNing
     * @Date        : 2022/3/3
     * @Time        : 10:55
     */
    @Override
    public Integer getEartagOnlineTotalFromCache(String farmId, String todayDateText) {
        String setKey = BizConst.SET_FARM_EARMARK_ONLINE_PREFIX + farmId+":"+todayDateText;
        return redisTemplate.opsForSet().members(setKey).size();
    }

    @Override
    public void putEartagToOnlineInSet(String farmId, String earmark, String todayDateText) throws ParseException {
        String setKey = BizConst.SET_FARM_EARMARK_ONLINE_PREFIX + farmId+":"+todayDateText;
//        System.out.println("## eartag.setKey .putEartagToOnlineInSet>>"+setKey+",earmark>>"+earmark);
        redisTemplate.opsForSet().add(setKey, earmark);
        //设置25小时过期
        redisTemplate.expire(setKey,eartagOnlineSetExpire , TimeUnit.HOURS);
//        Set<String> resultSet = redisTemplate.opsForSet().members(setKey);
//        System.out.println("## "+setKey+">>"+resultSet.size()+" >>"+resultSet);
    }

    @Override
    public Object getEartagLastAct(String earmark) {
        String valuekey = BizConst.EARTAG_LAST_ACT_PREFIX+earmark;
        return redisTemplate.opsForValue().get(valuekey);
    }

    @Override
    public void putEartagAct(String earmark, String nowAct) {
        String valuekey = BizConst.EARTAG_LAST_ACT_PREFIX+earmark;
        redisTemplate.opsForValue().set(valuekey,nowAct);
    }

    @Override
    public boolean isExistDeviceOnlineSet(String farmId, String deviceCode, String todayDateText) {
        String setKey = BizConst.SET_FARM_DEVICE_ONLINE_PREFIX + farmId+":"+todayDateText;
        return redisTemplate.opsForSet().isMember(setKey, deviceCode);
    }

    /**
     * @Method      : putDeviceCode
     * @Description : 将新的设备编号加入到缓存当中；
     * @Params      : [farmId, deviceCode]
     * @Return      : void
     *
     * @Author      : ZhuoNing
     * @Date        : 2022/3/1
     * @Time        : 13:42
     */
    @Override
    public void putDeviceToOnlineSet(String farmId, String deviceCode, String todayDateText) {
        String setKey = BizConst.SET_FARM_DEVICE_ONLINE_PREFIX + farmId+":"+todayDateText;
        redisTemplate.opsForSet().add(setKey, deviceCode);
        redisTemplate.expire(setKey,eartagOnlineSetExpire , TimeUnit.HOURS);
    }

    @Override
    public Object getEnvTempThresholdVal(String farmId) {
        String key = BizConst.HASH_ENV_TEMP_TV_PREFIX+farmId;
        String hashKey = "envTemp";
        return redisTemplate.opsForHash().get(key,hashKey);
    }

    @Override
    public void deleteDeviceToOnlineSet(String farmId, String deviceCode, String todayDateText) {
        String setKey = BizConst.SET_FARM_DEVICE_ONLINE_PREFIX + farmId+":"+todayDateText;
//        redisTemplate.opsForSet().add(setKey, deviceCode);
//        redisTemplate.expire(setKey,eartagOnlineSetExpire , TimeUnit.HOURS);
        redisTemplate.opsForSet().remove(setKey,deviceCode);
    }

    @Override
    public Map getFarmAllStatusByFarmId(String farmId) {
        String hashKey = BizConst.FARM_ALL_STATUS_PREFIX+farmId;
        return redisTemplate.opsForHash().entries(hashKey);
    }

    @Override
    public void putFarmAllStatusCache(String farmId, EartarFarmAllStatusEntity farmAllStatusEntity) {
//        Map allStatusMap = new HashMap();
////        allStatusMap.put("id",farmAllStatusEntity.getId());
//        allStatusMap.put("deviceTotal",farmAllStatusEntity.getDeviceTotal());
//        allStatusMap.put("deviceOnline",farmAllStatusEntity.getDeviceOnline());
//        allStatusMap.put("deviceOffline",farmAllStatusEntity.getDeviceOffline());
//        allStatusMap.put("deviceRate",farmAllStatusEntity.getDeviceRate());
//        allStatusMap.put("deviceCancel",farmAllStatusEntity.getDeviceCancel());
//        allStatusMap.put("eartagTotal",farmAllStatusEntity.getEartagTotal());
//        allStatusMap.put("eartagOnline",farmAllStatusEntity.getEartagOnline());
//        allStatusMap.put("farmId",farmId);
////        allStatusMap.put("updateTime",duObj.formatDateTime(duObj.getTodayText()));
////        allStatusMap.put("createDate",duObj.getTodayDate());
//        allStatusMap.put("updateTime",farmAllStatusEntity.getUpdateTime());
//        allStatusMap.put("createDate",farmAllStatusEntity.getCreateDate());
        Map allStatusMap = JSON.parseObject(JSONObject.toJSONString(farmAllStatusEntity),Map.class);
        redisTemplate.opsForHash().putAll(BizConst.FARM_ALL_STATUS_PREFIX+farmId, allStatusMap);
    }

    @Override
    public int getDeviceOnlineTotal(String farmId, String todayDateText) {
        String setKey = BizConst.SET_FARM_DEVICE_ONLINE_PREFIX+farmId+"_"+todayDateText;
        Set<String> resultSet = redisTemplate.opsForSet().members(setKey);
        return resultSet.size();
    }

    @Override
    public int getEartagOnlineTotal(String farmId, String todayDateText) {
        String setKey = BizConst.SET_FARM_EARMARK_ONLINE_PREFIX+farmId+"_"+todayDateText;
        Set<String> resultSet = redisTemplate.opsForSet().members(setKey);
        return resultSet.size();
    }

    @Override
    public void putDeviceTotal_(Integer effectiveDeviceTotal, String farmId) {
        String valueKey = BizConst.FARM_DEVICE_TOTAL_PREFIX+farmId;
        redisTemplate.opsForValue().set(valueKey,effectiveDeviceTotal);
        // TEST
//        Object obj = redisTemplate.opsForValue().get(valueKey);
//        System.out.println("TEST value>>"+obj.toString());
    }

    @Override
    public void putAllDeviceSet(String deviceCode){
        String setKey = BizConst.SET_ALL_DEVICE_PREFIX;
        redisTemplate.opsForSet().add(setKey,deviceCode);
    }

    @Override
    public void putChipIdToCache(String chipId, String deviceCode) {
        System.out.println("## <putChipIdToCache> chipId>>"+chipId);
        System.out.println("## <putChipIdToCache> deviceCode>>"+deviceCode);

        String key = BizConst.CHIP_DEVICE_PREFIX;
        String hashKey = chipId;
        //
        redisTemplate.opsForHash().put(key,hashKey,deviceCode);
//        redisTemplate.expire(key,chipIdExpire , TimeUnit.HOURS);
    }

    @Override
    public void deleteDeviceRegister(String deviceCode) {
        String key = BizConst.DEVICE_REGISTER_PREFIX+deviceCode;
//        System.out.println(">> <deleteDeviceRegister> key>>"+key);
        Object obj = redisTemplate.opsForHash().entries(key);
        if(obj == null){
            log.error("##Error 该key["+key+"]的缓存不存在.");
        }else{
            redisTemplate.delete(key);
//            System.out.println(">> end <deleteDeviceRegister> key>>"+key);
        }
    }

    @Override
    public void deleteDeviceCodeSetCache(String deviceCode) {
        String setKey = BizConst.SET_ALL_DEVICE_PREFIX;
//        System.out.println(">> <deleteDeviceCodeSetCache> setKey "+setKey);
//        redisTemplate.opsForSet().add(setKey,deviceCode);
        redisTemplate.opsForSet().remove(setKey, deviceCode);
//        System.out.println(">> end <deleteDeviceCodeSetCache> setKey "+setKey);
    }

    @Override
    public void deleteOnlineDeviceSet(String farmId, String deviceCode, String todayDateText) {
        String setKey = BizConst.SET_FARM_DEVICE_ONLINE_PREFIX + farmId+":"+todayDateText;
        redisTemplate.opsForSet().remove(setKey,deviceCode);
    }

    @Override
    public void deleteChipIdHashCache(String chipId) {
        String key = BizConst.CHIP_DEVICE_PREFIX;
        String hashKey = chipId;
//        System.out.println(">> <deleteChipIdHashCache> key>>"+key+",hashKey>>"+hashKey);
        Object obj = redisTemplate.opsForHash().get(key, hashKey);
//        System.out.println(">> obj>>"+obj);
        if(obj == null){
            log.error("##Error 该key>>"+key+",hashKey>>"+hashKey+"的缓存不存在.");
        }else{
            redisTemplate.opsForHash().delete(key,hashKey);
        }

//        Object obj = redisTemplate.opsForHash().entries(key);
//        System.out.println(">> obj>>"+obj);
//        if(obj == null){
//            log.error("##Error 该key["+key+"]的缓存不存在.");
//        }else{
//            redisTemplate.delete(key);
//        }
        System.out.println(">> end <deleteChipIdHashCache> key "+key);
    }

    @Override
    public int isExistAllDeviceSet() {
        String setKey = BizConst.SET_ALL_DEVICE_PREFIX;
        Set<String> resultSet = redisTemplate.opsForSet().members(setKey);
        return resultSet.size();
    }

    @Override
    public boolean isExistDeviceCode(String deviceCode) {
        String setKey = BizConst.SET_ALL_DEVICE_PREFIX;
//        log.info("## setKey>>"+setKey+", deviceCode>>"+deviceCode);
        return redisTemplate.opsForSet().isMember(setKey, deviceCode);
    }

    @Override
    public void putAllDeviceSet(List list) {
        String setKey = BizConst.SET_ALL_DEVICE_PREFIX;
        for(int a=0;a<list.size();a++){
            redisTemplate.opsForSet().add(setKey,list.get(a).toString());
        }
        //验证
//        Set<String> resultSet = redisTemplate.opsForSet().members("setKey");
//        System.out.println("# resultSet.toString()>>"+resultSet.toString());
    }
}
