/*
 * 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_Message;
import com.ifish.bean.Tbl_Live_Room;
import com.ifish.bean.Tbl_Push_List;
import com.ifish.bean.Tbl_User;
import com.ifish.mapper.Tbl_Live_Room_Mapper;
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;
    @Autowired
    private DeviceHelperI deviceHelper;
    @Autowired
    private UserHelperI userHelperI;
    @Autowired
    private Tbl_Live_Room_Mapper tbl_Live_Room_Mapper;

    /**
     * 根据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;
    }

    /**
     * 根据条件获取用户列表缓存key
     *
     * @param start
     * @param length
     * @param userId
     * @param phoneNumber
     * @param userEmail
     * @param phoneType
     * @param isRegisterGwell
     * @param isRegisterJiguang
     * @param sortField
     * @param sortMode
     * @param nickName
     * @param remarks
     * @param loginTime1
     * @param loginTime2
     * @param createTime1
     * @param createTime2
     * @return
     */
    @Override
    public String getUserListRedisKey(Integer start, Integer length, String userId, String phoneNumber, String userEmail, String phoneType, String isRegisterGwell, String isRegisterJiguang, String sortField, String sortMode, String nickName, String remarks, String loginTime1, String loginTime2, String createTime1, String createTime2) {
        return RedisKey.USER_LIST + userId + "_" + phoneNumber + "_" + userEmail + "_" + phoneType + "_" + isRegisterGwell + "_" + isRegisterJiguang + "_" + sortField + "_" + sortMode + "_" + nickName + "_" + remarks + "_" + loginTime1 + "_" + loginTime2 + "_" + createTime1 + "_" + createTime2 + "_" + start + "_" + length;
    }

    /**
     * 根据条件搜索获取用户列表缓存key
     *
     * @param userId
     * @param phoneNumber
     * @param userEmail
     * @param phoneType
     * @param isRegisterGwell
     * @param isRegisterJiguang
     * @param sortField
     * @param sortMode
     * @param nickName
     * @param remarks
     * @param loginTime1
     * @param loginTime2
     * @param createTime1
     * @param createTime2
     * @return
     */
    @Override
    public String getUserListCountRedisKey(String userId, String phoneNumber, String userEmail, String phoneType, String isRegisterGwell, String isRegisterJiguang, String sortField, String sortMode, String nickName, String remarks, String loginTime1, String loginTime2, String createTime1, String createTime2) {
        return RedisKey.USER_LIST_COUNT + userId + "_" + phoneNumber + "_" + userEmail + "_" + phoneType + "_" + isRegisterGwell + "_" + isRegisterJiguang + "_" + sortField + "_" + sortMode + "_" + nickName + "_" + remarks + "_" + loginTime1 + "_" + loginTime2 + "_" + createTime1 + "_" + createTime2;
    }

    /**
     * 获取用户列表总数
     *
     * @return
     */
    @Override
    public String getAllUserListCountRedisKey() {
        return RedisKey.USER_LIST_COUNT_ALL;
    }

    /**
     * 根据userName获取Tbl_Security_User管理用户的redis缓存key键值
     *
     * @param userName
     * @return
     */
    @Override
    public String getTbl_Security_UserRedisKeyByUserName(String userName) {
        return RedisKey.SECURITY_USER + userName;
    }

    /**
     * 根据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_RoomRedisKeyByCameraId(String cameraId) {
        return RedisKey.LIVEROOM_CAMERAID + cameraId;
    }

    /**
     * 根据用户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;
    }

    /**
     * 获取直播间评论总数的redis缓存key键值
     *
     * @param roomId
     * @return
     */
    @Override
    public String getTbl_Live_MessageCount(Integer roomId) {
        return RedisKey.LIVEMESSAGE_COUNT + roomId;
    }

    /**
     * 根据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;
    }

    /**
     * 根据MD5值获取redis缓存key键值（修改密码用）
     *
     * @param md5
     * @return
     */
    public String getEmailUpdatePasswordRedisKey(String md5) {
        return RedisKey.EMAIL_UPDATEPASSWORD + md5;
    }

    /**
     * 删除redis中某个设备详情Tbl_Device的缓存
     *
     * @param device
     * @return
     */
    @Override
    public void deleteRedisByTbl_Device(Tbl_Device device) {

        try {
            if (device.getDeviceId() != null && device.getDeviceId() != 0 && StringUtils.isBlank(device.getMacAddress())) {
                device = deviceHelper.getDeviceById(device.getDeviceId());
            }
            if (StringUtils.isNotBlank(device.getMacAddress()) && (device.getDeviceId() == null || device.getDeviceId() == 0)) {
                device = deviceHelper.getDeviceByMacAddress(device.getMacAddress());
            }
            if (StringUtils.isNotBlank(device.getCameraId()) && (device.getDeviceId() == null || device.getDeviceId() == 0)) {
                device = deviceHelper.getDeviceByCameraId(device.getCameraId());
            }
            deleteTbl_DeviceInfo(device);
            Tbl_Device_Statistics device_Statistics = deviceHelper.getDeviceStatisticsByDeviceId(device.getDeviceId());
            deleteTbl_Device_StatisticsInfo(device_Statistics);
        } catch (Exception e) {
        }
    }

    public void deleteTbl_DeviceInfo(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) {

        try {
            if (device.getDeviceId() != null && device.getDeviceId() != 0 && StringUtils.isBlank(device.getMacAddress())) {
                device = deviceHelper.getDeviceStatisticsByDeviceId(device.getDeviceId());
            }
            if (StringUtils.isNotBlank(device.getMacAddress()) && (device.getDeviceId() == null || device.getDeviceId() == 0)) {
                device = deviceHelper.getDeviceStatisticsByMacAddress(device.getMacAddress());
            }
            deleteTbl_Device_StatisticsInfo(device);
            Tbl_Device device1 = deviceHelper.getDeviceById(device.getDeviceId());
            deleteTbl_DeviceInfo(device1);
        } catch (Exception e) {
        }
    }

    public void deleteTbl_Device_StatisticsInfo(Tbl_Device_Statistics device) {
        if (device.getDeviceId() != null && device.getDeviceId() > 0) {
            redisHelperI.deleteRedis(getTbl_Device_StatisticsRedisKeyByDeviceId(device.getDeviceId()));
        }
        if (StringUtils.isNotBlank(device.getMacAddress())) {
            redisHelperI.deleteRedis(getTbl_Device_StatisticsRedisKeyByMacAddress(device.getMacAddress()));
        }

    }

    /**
     * 删除redis中某个设备和用户关系Tbl_Device_User的缓存
     *
     * @param tbl_Device_User
     */
    @Override
    public void deleteRedisByTbl_Device_User(Tbl_Device_User tbl_Device_User) {
        deleteTbl_Device_UserInfo(tbl_Device_User);
        try {
            Tbl_Device device1 = deviceHelper.getDeviceById(tbl_Device_User.getDeviceId());
            deleteTbl_DeviceInfo(device1);
            Tbl_Device_Statistics device_Statistics = deviceHelper.getDeviceStatisticsByDeviceId(tbl_Device_User.getDeviceId());
            deleteTbl_Device_StatisticsInfo(device_Statistics);
        } catch (Exception e) {
        }

    }

    public void deleteTbl_Device_UserInfo(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()));
        }
        if (tbl_Device_User.getUserId() != null) {
            redisHelperI.delRedisByTagKey(getListTbl_Device_UserKeyByUserId(tbl_Device_User.getUserId()));
        }
    }

    /**
     * 删除redis中某个用户Tbl_User的缓存
     *
     * @param tbl_User
     */
    @Override
    public void deleteRedisByTbl_User(Tbl_User tbl_User) {
        try {
            if (tbl_User.getUserId() != null && tbl_User.getUserId() > 0) {
                tbl_User = userHelperI.getUserById(tbl_User.getUserId());
            }
        } catch (Exception e) {
        }
        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()));
        }
        redisHelperI.delRedisByTagKey(RedisKey.USER_LIST);
        redisHelperI.delRedisByTagKey(RedisKey.USER_LIST_COUNT);
        redisHelperI.delRedisByTagKey(RedisKey.USER_LIST_COUNT_ALL);
    }

    /**
     * 删除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) && StringUtils.isNotBlank(live_Room.getCameraId())) {
            live_Room = tbl_Live_Room_Mapper.getTbl_Live_RoomByCameraId(live_Room.getCameraId());
        } else if (StringUtils.isBlank(live_Room.getCameraId()) && (live_Room.getRoomId() != null && live_Room.getRoomId() > 0)) {
            live_Room = tbl_Live_Room_Mapper.getTbl_Live_RoomById(live_Room.getRoomId());
        }
        if (live_Room.getRoomId() != null && live_Room.getRoomId() > 0) {
            redisHelperI.deleteRedis(getTbl_Live_RoomRedisKeyByRoomId(live_Room.getRoomId()));
        }
        if (StringUtils.isNotBlank(live_Room.getCameraId())) {
            redisHelperI.deleteRedis(getTbl_Live_RoomRedisKeyByCameraId(live_Room.getCameraId()));
        }
        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());
    }

    /**
     * 删除redis中某个直播间评论列表缓存
     *
     * @param tbl_Live_Message
     */
    @Override
    public void deleteRedisByTbl_Live_Message(Tbl_Live_Message tbl_Live_Message) {
        redisHelperI.delRedisByTagKey(RedisKey.LIVEMESSAGE_ROOMID + tbl_Live_Message.getRoomId());
        redisHelperI.deleteRedis(RedisKey.LIVEMESSAGE_COUNT + tbl_Live_Message.getRoomId());
    }

    /**
     * 后台推送消息列表缓存
     *
     * @param start
     * @param length
     * @param sdkVersion
     * @param phoneNumber
     * @param mac
     * @param phoneType
     * @param pushType
     * @param sortField
     * @param sortMode
     * @return
     */
    @Override
    public String getAdmin_Tbl_Push_List_BySelect(Integer start, Integer length, String sdkVersion, String phoneNumber, String mac, String phoneType, String pushType, String sortField, String sortMode) {
        return RedisKey.PUSHLIST_SELECT + start + "_" + length + "_" + sdkVersion + "_" + phoneNumber + "_" + mac + "_" + phoneType + "_" + pushType + "_" + sortField + "_" + sortMode;
    }

    /**
     * 后台推送消息列表查询数量缓存
     *
     * @param start
     * @param length
     * @param sdkVersion
     * @param phoneNumber
     * @param mac
     * @param phoneType
     * @param pushType
     * @param sortField
     * @param sortMode
     * @return
     */
    @Override
    public String getAdmin_Tbl_Push_List_Count_BySelect(String sdkVersion, String phoneNumber, String mac, String phoneType, String pushType, String sortField, String sortMode) {
        return RedisKey.PUSHLIST_SELECT + "count_" + sdkVersion + "_" + phoneNumber + "_" + mac + "_" + phoneType + "_" + pushType + "_" + sortField + "_" + sortMode;
    }

    /**
     * 后台推送消息列表总数缓存
     *
     * @param start
     * @param length
     * @param sdkVersion
     * @param phoneNumber
     * @param mac
     * @param phoneType
     * @param pushType
     * @param sortField
     * @param sortMode
     * @return
     */
    @Override
    public String getAdmin_Tbl_Push_List_AllCount() {
        return RedisKey.PUSHLIST_SELECT;
    }

    /**
     * 根据条件删除推送列表缓存
     *
     * @param pushId
     */
    @Override
    public void deleteRedisByTbl_Push_List(Tbl_Push_List tbl_Push_List) {
        if (tbl_Push_List.getPushId() != null) {
            redisHelperI.deleteRedis(getTbl_Push_List_RedisByPushId(tbl_Push_List.getPushId()));
        }
        deleteRedisByTbl_Push_ListToAllRedisList();
    }

    /**
     * 删除推送列表类缓存
     */
    @Override
    public void deleteRedisByTbl_Push_ListToAllRedisList() {
        redisHelperI.delRedisByTagKey(RedisKey.PUSHLIST_SELECT);
    }

    /**
     * 删除鱼缸厂信息缓存
     */
    @Override
    public void deleteTbl_Vender_List_RedisKey() {
        redisHelperI.delRedisByTagKey(RedisKey.VENDER_CODE);
    }

}
