/*
 * 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.DeviceUserBean;
import com.ifish.entity.Device;
import com.ifish.entity.DeviceCamera;
import com.ifish.mapper.Tbl_Device_Mapper;
import com.ifish.mapper.Tbl_Device_User_Mapper;
import com.ifish.util.RedisKey;
import com.ifish.util.StringUtil;
import java.util.List;
import org.apache.commons.lang3.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Component;

/**
 *
 * @author Administrator
 */
@Component
public class DeviceUserHelper implements DeviceUserHelperI {

    /**
     * 所有tbl_device_user表的操作方法接口
     */
    @Autowired
    private Tbl_Device_User_Mapper tbl_Device_User_Mapper;

    /**
     * redis缓存服务器方法接口
     */
    @Autowired
    private RedisHelperI redisHelperI;

    /**
     * 所有tbl_device表的操作方法接口
     */
    @Autowired
    private Tbl_Device_Mapper tbl_Device_Mapper;

    @Override
    public List<DeviceUserBean> getDeviceUsersByUserId(Integer userId) throws Exception {
        List<DeviceUserBean> deviceUserList = null;
        String key = RedisKey.DeviceUser_Key + userId;
        String redisString = redisHelperI.getRedis(key);
        if (StringUtils.isNotBlank(redisString)) {
            deviceUserList = (List<DeviceUserBean>) StringUtil.JsonToList(redisString, DeviceUserBean.class);
        } else {
            deviceUserList = tbl_Device_User_Mapper.getDeviceUsersByUserId(userId);
            if (deviceUserList != null && deviceUserList.size() > 0) {
                redisHelperI.setRedis(key, StringUtil.ObjectToJson(deviceUserList));
            }
        }
        return deviceUserList;
    }

    @Override
    public List<DeviceCamera> getDeviceCameraListByDeviceId(Integer deviceId) throws Exception {
        List<DeviceCamera> deviceCameraList = null;
        String key = RedisKey.DeviceCamera_key + deviceId;
        String redisString = redisHelperI.getRedis(key);
        if (StringUtils.isNotBlank(redisString)) {
            deviceCameraList = (List<DeviceCamera>) StringUtil.JsonToList(redisString, DeviceCamera.class);
        } else {
            deviceCameraList = tbl_Device_User_Mapper.getDeviceCameraListByDeviceId(deviceId);
            if (deviceCameraList != null && deviceCameraList.size() > 0) {
                redisHelperI.setRedis(key, StringUtil.ObjectToJson(deviceCameraList));
            }
        }
        return deviceCameraList;
    }

    @Override
    public Device getDeviceById(Integer deviceId) throws Exception {
        Device device = null;
        String key = RedisKey.Device_key + deviceId;
        String redisString = redisHelperI.getRedis(key);
        if (StringUtils.isNotBlank(redisString)) {
            device = (Device) StringUtil.JsonToBean(redisString, Device.class);
        } else {
            device = tbl_Device_Mapper.getDeviceById(deviceId);
            if (device != null && device.getDeviceId() > 0) {
                redisHelperI.setRedis(key, StringUtil.ObjectToJson(device));
            }
        }
        return device;
    }
}
