package com.sky.ioc.service.security.impl;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.sky.ioc.config.RestTemplateConfig;
import com.sky.ioc.entity.domain.security.SecurityDevice;
import com.sky.ioc.mapper.security.SecurityDeviceMapper;
import com.sky.ioc.service.security.SecurityDeviceService;
import com.sky.ioc.tool.ReturnMsg;
import lombok.extern.slf4j.Slf4j;
import org.springframework.http.ResponseEntity;
import org.springframework.stereotype.Service;
import org.springframework.web.client.RestTemplate;

import javax.annotation.Resource;
import java.util.ArrayList;
import java.util.LinkedHashMap;
import java.util.List;
import java.util.Map;

@Slf4j
@Service
public class SecurityDeviceServiceImpl implements SecurityDeviceService {

    @Resource
    RestTemplateConfig restTemplateConfig;

    @Resource
    private SecurityDeviceMapper securityDeviceMapper;

    final static String SECURITY_CAMERA_URL = "/api/safety/Hikvision/camera/list?pageNum=1&pageSize=1000";
    final static String SECURITY_CAMERA_API_INFO_URL = "/api/safety/Hikvision/api/apiInfo";

    @Override
    public ReturnMsg syncAllCameras() {
        RestTemplate restTemplate = restTemplateConfig.build();
        String url = restTemplateConfig.addBaseUrl(SECURITY_CAMERA_URL);
        // 发送GET请求
        ResponseEntity<Map> responseEntity = restTemplate.getForEntity(url, Map.class);
        // 获取响应对象里的 body 对象
        Map<String, Object> body = responseEntity.getBody();
        // 获取状态码
        Integer code = (Integer)body.get("code");
        // 获取响应信息
        String message = (String)body.get("msg");
        if(code==200){
            ArrayList list = (ArrayList) body.get("rows");
            if(list!=null&&list.size()>0){
                securityDeviceMapper.delete(null);
                for (int i=0;i<list.size();i++){
                    LinkedHashMap json = (LinkedHashMap) list.get(i);
                    SecurityDevice device = new SecurityDevice();

                    device.setThirdId(json.get("id").toString());
                    device.setDeviceName((String) json.get("cameraName"));
                    device.setDeviceId((String) json.get("cameraIndexCode"));
                    device.setDeviceType(1);
                    device.setDeviceLocation((String) json.get("cameraName"));
                    device.setDeviceAge(0);
                    device.setCompanyId("0");
                    device.setUserId("");
                    device.setDeviceGroup("");
                    device.setStatus(1);
                    securityDeviceMapper.insert(device);
                }
            }
            return ReturnMsg.ok();
        }else{
            log.info("获取摄像头列表数据："+message);
            return ReturnMsg.fail(message);
        }
    }

    @Override
    public ReturnMsg getAllCameras() {
        LambdaQueryWrapper<SecurityDevice> queryWrapper = new LambdaQueryWrapper<>();
        queryWrapper.eq(SecurityDevice::getDeviceType, "1");
        List<SecurityDevice> securityDevices = securityDeviceMapper.selectList(queryWrapper);
        return ReturnMsg.ok(securityDevices);
    }

    @Override
    public ReturnMsg getCameraApiInfo() {
        RestTemplate restTemplate = restTemplateConfig.build();
        String url = restTemplateConfig.addBaseUrl(SECURITY_CAMERA_API_INFO_URL);
        // 发送GET请求
        ResponseEntity<Map> responseEntity = restTemplate.getForEntity(url, Map.class);
        // 获取响应对象里的 body 对象
        Map<String, Object> body = responseEntity.getBody();
        // 获取状态码
        Integer code = (Integer)body.get("code");
        // 获取响应信息
        String message = (String)body.get("msg");
        if(code==200){
            return ReturnMsg.ok(body.get("data"));
        }else{
            log.info("获取摄像头api信息："+message);
            return ReturnMsg.fail(message);
        }
    }

}
