package com.huijy.management.service.impl;

import java.util.List;
import com.huijy.common.utils.DateUtils;
import com.huijy.common.utils.ip.PingIpUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import com.huijy.management.mapper.CameraMapper;
import com.huijy.management.domain.Camera;
import com.huijy.management.service.ICameraService;

/**
 * 摄像头管理Service业务层处理
 *
 * @author luobo
 * @date 2021-10-03
 */
@Service
public class CameraServiceImpl implements ICameraService
{
    @Autowired
    private CameraMapper cameraMapper;

    /**
     * 查询摄像头管理
     *
     * @param cameraId 摄像头管理主键
     * @return 摄像头管理
     */
    @Override
    public Camera selectCameraByCameraId(Long cameraId)
    {
        return cameraMapper.selectCameraByCameraId(cameraId);
    }

    /**
     * 查询摄像头管理列表
     *
     * @param camera 摄像头管理
     * @return 摄像头管理
     */
    @Override
    public List<Camera> selectCameraList(Camera camera)
    {
        return cameraMapper.selectCameraList(camera);
    }

    /**
     * 新增摄像头管理
     *
     * @param camera 摄像头管理
     * @return 结果
     */
    @Override
    public int insertCamera(Camera camera)
    {
        camera.setCreateTime(DateUtils.getNowDate());
        return cameraMapper.insertCamera(camera);
    }

    /**
     * 修改摄像头管理
     *
     * @param camera 摄像头管理
     * @return 结果
     */
    @Override
    public int updateCamera(Camera camera)
    {
        camera.setUpdateTime(DateUtils.getNowDate());
        return cameraMapper.updateCamera(camera);
    }

    /**
     * 批量删除摄像头管理
     *
     * @param cameraIds 需要删除的摄像头管理主键
     * @return 结果
     */
    @Override
    public int deleteCameraByCameraIds(Long[] cameraIds)
    {
        return cameraMapper.deleteCameraByCameraIds(cameraIds);
    }

    /**
     * 删除摄像头管理信息
     *
     * @param cameraId 摄像头管理主键
     * @return 结果
     */
    @Override
    public int deleteCameraByCameraId(Long cameraId)
    {
        return cameraMapper.deleteCameraByCameraId(cameraId);
    }

    @Override
    public void checkAllCameraIsOnline() {
        List<Camera> list = this.selectCameraList(new Camera());
        for (Camera item:list){
           String ip = item.getIpAddres();
           boolean b = PingIpUtils.ping(ip,2,1000);
           if (b){
               item.setOnlineState(1);
           }else{
               item.setOnlineState(0);
           }
           updateCamera(item);
        }
    }

    @Override
    public Boolean checkCameraIsOnline(Long cameraId) {
        Camera entity = this.selectCameraByCameraId(cameraId);
        if (entity!=null){
            String ip = entity.getIpAddres();
            boolean b = PingIpUtils.ping(ip,2,1000);
            if (b){
                entity.setOnlineState(1);
            }else{
                entity.setOnlineState(0);
            }
            updateCamera(entity);
            return b;
        }
        return false;
    }
}
