package com.imdroid.sec_exapi.controller;

import com.imdroid.common.util.GsonUtil;
import com.imdroid.sec_exapi.entity.DeviceQueryResponse;
import com.imdroid.secapi.dto.GnssStatusJoin;
import com.imdroid.secapi.dto.GnssStatusMapper;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RestController;

import javax.servlet.http.HttpServletRequest;
import java.time.format.DateTimeFormatter;
import java.util.List;

@RestController
public class DeviceApi {
    DateTimeFormatter formatter = DateTimeFormatter.ofPattern("yyyy-MM-dd HH:mm:ss");

    @Autowired
    GnssStatusMapper deviceMapper;

    final Logger logger = LoggerFactory.getLogger(DeviceApi.class);

    @GetMapping(value = "/gnss/device" )
    public String query(HttpServletRequest request, String projectName) {
        Integer tenantId = (Integer) request.getAttribute("tenantId");

        List<GnssStatusJoin> deviceList = deviceMapper.queryDeployedByProject(tenantId,projectName);
        DeviceQueryResponse deviceQueryResponse = new DeviceQueryResponse();
        deviceQueryResponse.OK();
            for(GnssStatusJoin device:deviceList){
                deviceQueryResponse.addDevice(device.getDeviceid(),device.getName(),
                        device.getUpdatetime().format(formatter),device.getState());
            }
        String json = GsonUtil.toJson(deviceQueryResponse);
        logger.info(json);

        return json;
    }

}


