package com.sptg.iot.biz.controller;

import com.baomidou.mybatisplus.plugins.Page;
import com.sptg.common.core.beans.ResultBean;
import com.sptg.common.core.beans.ResultEnum;
import com.sptg.iot.api.beans.ApiResultBean;
import com.sptg.iot.api.dto.IotApiLogDto;
import com.sptg.iot.api.entity.IotApiLog;
import com.sptg.iot.api.feigns.IotApiLogFeign;
import com.sptg.iot.biz.service.IotApiLogServiceInterface;
import lombok.RequiredArgsConstructor;
import lombok.extern.slf4j.Slf4j;
import org.springframework.web.bind.annotation.*;

import java.util.Map;

/**
 * @author zwx
 * @create 2020/10/21 0:47
 */
@Slf4j
@RestController
@RequiredArgsConstructor
public class IotApiLogController implements IotApiLogFeign {

    private final IotApiLogServiceInterface iotApiLogServiceInterface;

    @Override
    @PostMapping(value = "/iot/apiLog/record")
    public ResultBean<String> recordIotApiLog(@RequestBody Map<String, Object> map) {
        String apiMethod = (String) map.get("apiMethod");
        String reqData = (String) map.get("reqData");
        ApiResultBean apiResultBean = new ApiResultBean();
        apiResultBean.setCode((Integer) map.get("code"));
        apiResultBean.setMsg((String) map.get("msg"));
        this.iotApiLogServiceInterface.recordIotApiLog(apiMethod, null, null, reqData, apiResultBean);
        return new ResultBean<>(ResultEnum.SUCCESS);
    }

    @Override
    @PostMapping(value = "/iot/apiLog/pagingQuery")
    public ResultBean<Page<IotApiLog>> pagingQuery(@RequestBody IotApiLogDto iotApiLogDto) {
        return this.iotApiLogServiceInterface.pagingQuery(iotApiLogDto);
    }

    @Override
    @GetMapping(value = "/iot/apiLog/getApiLog/{id}")
    public IotApiLog getIotApiLogById(@PathVariable(value = "id") Long id) {
        return this.iotApiLogServiceInterface.getIotApiLogById(id);
    }
}
