package com.dji.sample.manage.controller;

import com.dji.sample.manage.model.dto.DeviceDTO;
import com.dji.sample.manage.model.dto.DeviceOprLogsDTO;
import com.dji.sample.manage.model.param.DeviceOprLogsQueryParam;
import com.dji.sample.manage.service.IDeviceOprLogsService;
import com.dji.sample.manage.service.IDeviceService;
import com.dji.sdk.common.HttpResultResponse;
import com.dji.sdk.common.PaginationData;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import java.util.Optional;

/**
 * @author hqjiang
 * @version 1.0
 * @date 2024/6/26
 */
@RestController
@Slf4j
@RequestMapping("${url.manage.prefix}${url.manage.version}/oprlogs")
public class DeviceOprLogsController {

    @Autowired
    private IDeviceOprLogsService deviceOprLogsService;

    @Autowired
    private IDeviceService deviceService;

    /**
     * 查询工作空间下设备操作日志
     * @param workspaceId
     * @param param
     * @return
     */
    @GetMapping("/{workspace_id}/devices/logs")
    public HttpResultResponse getDeviceOprLogs(DeviceOprLogsQueryParam param, @PathVariable("workspace_id") String workspaceId) {
        PaginationData<DeviceOprLogsDTO> data = deviceOprLogsService.getDeviceOprLogs(workspaceId,param);
        //添加子设备
        if(data.getList() != null && !data.getList().isEmpty()) {
            for(DeviceOprLogsDTO logsDTO : data.getList()) {
                Optional<DeviceDTO> deviceOpt = deviceService.getDeviceBySn(logsDTO.getDeviceSn());
                if(deviceOpt.isPresent()) {
                    logsDTO.setChildren(deviceService.setChild(deviceOpt.get()).getChildren());
                }
            }
        }
        return HttpResultResponse.success(data);
    }

}
