package com.fastbee.data.controller.aaScreenAgricultural.deviceRealTimeData;

import com.baomidou.mybatisplus.extension.conditions.query.LambdaQueryChainWrapper;
import com.fastbee.common.core.domain.AjaxResult;
import com.fastbee.deviceData.api.yingshiyun.service.YingshiyunService;
import com.fastbee.deviceInfo.domain.DeviceInformationMonitor;
import com.fastbee.deviceInfo.mapper.DeviceInformationMonitorMapper;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import java.util.List;
import java.util.Map;

/**
 * @author mijiupro
 */
@RestController
@RequestMapping("/seedlingGrowth")
public class DeviceRealtimedataSeedlingGrowthController {

    @Autowired
    private DeviceInformationMonitorMapper monitorMapper ;

    @Autowired
    private YingshiyunService yingshiyunService;

    /**
     * 获取苗情监测监控设备列表
     */
    @GetMapping("/monitorDeviceList")
    public AjaxResult getMonitorDeviceList() {
        List<DeviceInformationMonitor> list = new LambdaQueryChainWrapper<>(monitorMapper)
                .select(DeviceInformationMonitor::getId,
                        DeviceInformationMonitor::getName,
                        DeviceInformationMonitor::getDeviceEncoding,
                        DeviceInformationMonitor::getLatitude,
                        DeviceInformationMonitor::getLongitude)
                .list();
        //获取监控播放地址
        list.forEach(this::setPlayUrl);
        return AjaxResult.success(list);
    }

    /**
     * 获取监控播放地址
      * @param monitor
     */
   public void setPlayUrl(DeviceInformationMonitor monitor) {
       Map<String, Object> videoPlayMap = yingshiyunService.getVideoPlayUrl(monitor.getDeviceEncoding());
       if (videoPlayMap.containsKey("playUrl")) {
           Object playUrl = videoPlayMap.get("playUrl");
           monitor.setPlayUrl(playUrl.toString());
       }
       if( videoPlayMap.containsKey("accessToken")){
           Object accessToken = videoPlayMap.get("accessToken");
           monitor.setAccessToken(accessToken.toString());
       }
   }
}