/*
 * 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.entity.Camera;
import com.ifish.entity.CameraUser;
import com.ifish.mapper.Tbl_Camera_Mapper;
import com.ifish.mapper.Tbl_Camera_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 CameraUserHelper implements CameraUserHelperI {

    @Autowired
    private Tbl_Camera_User_Mapper tbl_Camera_User_Mapper;

    @Autowired
    private Tbl_Camera_Mapper tbl_Camera_Mapper;

    @Autowired
    private RedisHelperI redisHelperI;

    /**
     * 根据用户Id获取用户绑定的摄像头
     *
     * @param userId
     * @return
     */
    public List<CameraUser> getCameraUserListByUserId(Integer userId) throws Exception {

        List<CameraUser> cameraUserList = null;
        String key = RedisKey.CameraUser_key + userId;
        String redisString = redisHelperI.getRedis(key);
        if (StringUtils.isNotBlank(redisString)) {
            cameraUserList = (List<CameraUser>) StringUtil.JsonToList(redisString, CameraUser.class);
        } else {
            cameraUserList = tbl_Camera_User_Mapper.getCameraUserListByUserId(userId);
            if (cameraUserList != null && cameraUserList.size() > 0) {
                redisHelperI.setRedis(key, StringUtil.ObjectToJson(cameraUserList));
            }
        }
        return cameraUserList;
    }

    /**
     * 根据摄像头Id获取摄像头信息
     *
     * @param cameraId
     * @return
     * @throws Exception
     */
    public Camera getCameraById(String cameraId) throws Exception {
        Camera camera = null;
        String key = RedisKey.CameraUser_key + cameraId;
        String redisString = redisHelperI.getRedis(key);
        if (StringUtils.isNotBlank(redisString)) {
            camera = (Camera) StringUtil.JsonToBean(redisString, Camera.class);
        } else {
            camera = tbl_Camera_Mapper.getCameraById(cameraId);
            if (camera != null && StringUtils.isNotBlank(camera.getCameraId())) {
                redisHelperI.setRedis(key, StringUtil.ObjectToJson(camera));
            }
        }
        return camera;
    }

}
