/*
 * To change this license header, choose License Headers in Project Properties.
 * To change this template file, choose Tools | Templates
 * and open the template in the editor.
 */
package com.ifish.helper;

import com.ifish.bean.Tbl_Device;
import com.ifish.bean.Tbl_Device_Statistics;
import com.ifish.bean.Tbl_Device_User;
import com.ifish.bean.Tbl_Live_Room;
import com.ifish.bean.Tbl_User;
import com.ifish.util.RedisKey;
import org.apache.commons.lang3.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Component;

/**
 *
 * @author Administrator
 */
@Component
public class RedisKeyHelper implements RedisKeyHelperI {
    
    @Autowired
    private RedisHelperI redisHelperI;

    /**
     * 根据phoneNumber获取Tbl_User用户信息的Redis缓存key键值
     *
     * @param userId
     * @return
     */
    @Override
    public String getTbl_UserRedisKeyByPhoneNumber(String phoneNumber) {
        return RedisKey.USER_PHONE + phoneNumber;
    }

    /**
     * 根据token获取tbl_user用户信息的redis缓存key键值
     *
     * @param token
     * @return
     */
    @Override
    public String getTbl_UserRedisKeyByToken(String token) {
        return RedisKey.USER_TOKEN + token;
    }

    /**
     * 根据Email获取Tbl_User用户信息的Redis缓存key键值
     *
     * @param userId
     * @return
     */
    @Override
    public String getTbl_UserRedisKeyByEmail(String email) {
        return RedisKey.USER_EMAIL + email;
    }

    /**
     * 根据userId获取Tbl_User用户信息的Redis缓存key键值
     *
     * @param userId
     * @return
     */
    @Override
    public String getTbl_UserRedisKeyByUserId(Integer userId) {
        return RedisKey.USER_ID_KEY + userId;
    }

    /**
     * 根据mac地址获取Tbl_Device的redis缓存key键值
     *
     * @param macAddress
     * @return
     */
    @Override
    public String getTbl_DeviceRedisKeyByMacAddress(String macAddress) {
        return RedisKey.DEVICE_KEY_MACADDRESS + macAddress;
    }

    /**
     * 根据摄像头Id获取Tbl_Device的redis缓存key键值
     *
     * @param macAddress
     * @return
     */
    @Override
    public String getTbl_CameraRedisKeyByCameraId(String CameraId) {
        return RedisKey.DEVICE_KEY_CAMERAID + CameraId;
    }

    /**
     * 根据Id获取Tbl_Device的redis缓存key键值
     *
     * @param macAddress
     * @return
     */
    @Override
    public String getTbl_DeviceRedisKeyByDeviceId(Integer deviceId) {
        return RedisKey.DEVICE_KEY_DEVICEID + deviceId;
    }

    /**
     * 根据设备ID和用户ID获取Tbl_Device_User的redis缓存key键值
     *
     * @param userId
     * @param deviceId
     * @return
     */
    @Override
    public String getTbl_Device_UserRedisKeyByDeviceIdAndUserId(Integer userId, Integer deviceId) {
        return RedisKey.DEVICE_USER_DEVICEID_AND_USERID + "u_" + userId + "_d_" + deviceId;
    }

    /**
     * 根据设备ID获取Tbl_Device_User(列表)的redis缓存key键值
     *
     * @param userId
     * @param deviceId
     * @return
     */
    @Override
    public String getListTbl_Device_UserKeyByDeviceId(Integer deviceId) {
        return RedisKey.DEVICE_USER_LIST_DEVICEID + deviceId;
    }

    /**
     * 根据用户ID获取Tbl_Device_User(列表)的redis缓存key键值
     *
     * @param userId
     * @param deviceId
     * @return
     */
    @Override
    public String getListTbl_Device_UserKeyByUserId(Integer userId) {
        return RedisKey.DEVICE_USER_LIST_USERID + userId;
    }

    /**
     * 根据mac地址获取Tbl_Device_Statistics的redis缓存key键值
     *
     * @param macAddress
     * @return
     */
    @Override
    public String getTbl_Device_StatisticsRedisKeyByMacAddress(String macAddress) {
        return RedisKey.DEVICE_STATISTICS_MACADDRESS + macAddress;
    }

    /**
     * 根据Id获取Tbl_Device_Statistics的redis缓存key键值
     *
     * @param macAddress
     * @return
     */
    @Override
    public String getTbl_Device_StatisticsRedisKeyByDeviceId(Integer deviceId) {
        return RedisKey.DEVICE_STATISTICS_DEVICEID + deviceId;
    }

    /**
     * 根据typeCode获取Tbl_HardWare_Type的redis缓存key键值
     *
     * @param typeCode
     * @return
     */
    @Override
    public String getTbl_HardWare_TypeRedisKeyByTypeCode(String typeCode) {
        return RedisKey.HARD_WARE_TYPE + typeCode;
    }

    /**
     * 根据brandCode获取Tbl_Vender_list的redis缓存key键值
     *
     * @param brandCode
     * @return
     */
    @Override
    public String getTbl_Vender_ListRedisKeyByCode(String brandCode) {
        return RedisKey.VENDER_CODE + brandCode;
    }

    /**
     * 根据直播间Id获取Tbl_Live_Room的redis缓存key键值
     *
     * @param roomId
     * @return
     */
    @Override
    public String getTbl_Live_RoomRedisKeyByRoomId(Integer roomId) {
        return RedisKey.LIVEROOM_ROOMID + roomId;
    }

    /**
     * 根据用户Id获取Tbl_Live_Room的redis缓存key键值
     *
     * @param roomId
     * @return
     */
    @Override
    public String getTbl_Live_RoomRedisKeyByUserId(Integer User) {
        return RedisKey.LIVEROOM_USERID + User;
    }

    /**
     * 根据分页和排序条件获取直播间列表缓存key键值
     *
     * @param first
     * @param pageSize
     * @param orders
     * @return
     */
    @Override
    public String getListTbl_Live_Room_RedisKey(Integer first, Integer pageSize, String orders) {
        return RedisKey.LIVEROMM_LIST + "f_" + first + "_p_" + pageSize + "_o_" + orders;
    }

    /**
     * 获取所有开启直播间总数量的redis缓存key键值
     *
     * @return
     */
    @Override
    
    public String getTbl_Live_Room_CountRedisKey() {
        return RedisKey.LIVEROOM_COUNT;
    }

    /**
     * 获取直播间评论列表的redis缓存key键值
     *
     * @param roomId
     * @return
     */
    @Override
    public String getTbl_Live_Message(Integer firstResult, Integer pageSize, Integer roomId) {
        return RedisKey.LIVEMESSAGE_ROOMID + roomId + "_f_" + firstResult + "_p_" + pageSize;
    }

    /**
     * 根据pushId获取Tbl_push_list的redis缓存key键值
     *
     * @param pushId
     * @return
     */
    @Override
    public String getTbl_Push_List_RedisByPushId(Integer pushId) {
        return RedisKey.PUSHLIST_PUSHID + pushId;
    }

    /**
     * 根据MD5值获取redis缓存key键值
     *
     * @param md5
     * @return
     */
    @Override
    public String getEmailRedisKey(String md5) {
        return RedisKey.EMAIL_REGIESTER + md5;
    }

    /**
     * 删除redis中某个设备详情Tbl_Device的缓存
     *
     * @param device
     * @return
     */
    @Override
    public void deleteRedisByTbl_Device(Tbl_Device device) {
        if (device.getDeviceId() != null && device.getDeviceId() > 0) {
            redisHelperI.deleteRedis(getTbl_DeviceRedisKeyByDeviceId(device.getDeviceId()));
        }
        if (StringUtils.isNotBlank(device.getMacAddress())) {
            redisHelperI.deleteRedis(getTbl_DeviceRedisKeyByMacAddress(device.getMacAddress()));
        }
        if (StringUtils.isNotBlank(device.getCameraId())) {
            redisHelperI.deleteRedis(getTbl_CameraRedisKeyByCameraId(device.getCameraId()));
        }
    }

    /**
     * 删除redis中某个设备统计信息Tbl_Device_Statistics的缓存
     *
     * @param device
     * @return
     */
    @Override
    public void deleteRedisByTbl_Device_Statistics(Tbl_Device_Statistics device) {
        if (device.getDeviceId() != null && device.getDeviceId() > 0) {
            redisHelperI.deleteRedis(getTbl_DeviceRedisKeyByDeviceId(device.getDeviceId()));
        }
        if (StringUtils.isNotBlank(device.getMacAddress())) {
            redisHelperI.deleteRedis(getTbl_DeviceRedisKeyByMacAddress(device.getMacAddress()));
        }
    }

    /**
     * 删除redis中某个设备和用户关系Tbl_Device_User的缓存
     *
     * @param tbl_Device_User
     */
    @Override
    public void deleteRedisByTbl_Device_User(Tbl_Device_User tbl_Device_User) {
        if (tbl_Device_User.getDeviceId() != null && tbl_Device_User.getUserId() != null && tbl_Device_User.getDeviceId() > 0 && tbl_Device_User.getUserId() > 0) {
            redisHelperI.deleteRedis(getTbl_Device_UserRedisKeyByDeviceIdAndUserId(tbl_Device_User.getUserId(), tbl_Device_User.getDeviceId()));
        }
        if (tbl_Device_User.getDeviceId() != null && tbl_Device_User.getDeviceId() > 0) {
            redisHelperI.deleteRedis(getListTbl_Device_UserKeyByDeviceId(tbl_Device_User.getDeviceId()));
        }
    }

    /**
     * 删除redis中某个用户Tbl_User的缓存
     *
     * @param tbl_User
     */
    @Override
    public void deleteRedisByTbl_User(Tbl_User tbl_User) {
        if (tbl_User.getUserId() != null && tbl_User.getUserId() > 0) {
            redisHelperI.deleteRedis(getTbl_UserRedisKeyByUserId(tbl_User.getUserId()));
        }
        if (StringUtils.isNotBlank(tbl_User.getUserEmail())) {
            redisHelperI.deleteRedis(getTbl_UserRedisKeyByEmail(tbl_User.getUserEmail()));
        }
        if (StringUtils.isNotBlank(tbl_User.getPhoneNumber())) {
            redisHelperI.deleteRedis(getTbl_UserRedisKeyByPhoneNumber(tbl_User.getPhoneNumber()));
        }
        if (StringUtils.isNotBlank(tbl_User.getToken())) {
            redisHelperI.deleteRedis(getTbl_UserRedisKeyByToken(tbl_User.getToken()));
        }
    }

    /**
     * 删除redis中某个直播间Tbl_Live_Room的缓存
     *
     * @param live_Room
     */
    @Override
    public void deleteRedisByTbl_Live_Room(Tbl_Live_Room live_Room) {
        if (live_Room.getRoomId() != null && live_Room.getRoomId() > 0) {
            redisHelperI.deleteRedis(getTbl_Live_RoomRedisKeyByRoomId(live_Room.getRoomId()));
        }
        if (live_Room.getUserId() != null && live_Room.getUserId() > 0) {
            redisHelperI.deleteRedis(getTbl_Live_RoomRedisKeyByUserId(live_Room.getUserId()));
        }
        redisHelperI.deleteRedis(getTbl_Live_Room_CountRedisKey());
        redisHelperI.delRedisByTagKey(RedisKey.LIVEROMM_LIST);
    }

    /**
     * 删除redis中有直播间总数缓存
     */
    @Override
    public void deleteRedisByLiveRoomListCount() {
        redisHelperI.deleteRedis(getTbl_Live_Room_CountRedisKey());
    }
    
}
