package org.jeecg.modules.api.controller;

import com.alibaba.fastjson.JSONObject;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import lombok.extern.slf4j.Slf4j;
import org.springframework.http.HttpStatus;
import org.springframework.http.ResponseEntity;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import java.util.HashMap;
import java.util.Map;

@Api(tags="A轮对受电弓数采接口")
@RestController
@RequestMapping("/sdgInterface/dataImport")
@Slf4j
public class DataInterfaceController {
//    @Autowired
//    private IDataImportService dataImportService;

    /**
     * 统一数据上传接口
     * 接收来自各个子系统的检测数据，并将其存储到统一数据库中。
     *
     * @param jsonData 包含检测数据的JSON对象
     * @return 包含上传结果的响应实体
     */
    @ApiOperation(value="统一数据上传接口", notes="统一数据上传接口")
    @PostMapping("/upload")
    public ResponseEntity<Map<String, String>> uploadDetectionData(@RequestBody JSONObject jsonData) {
        Map<String, String> response = new HashMap<>();
        try {
            System.out.println(JSONObject.toJSONString(jsonData));
            // 参数校验 (例如，检查checkModule和checkTime是否存在)
//            if (jsonData == null || !jsonData.containsKey("data") || !jsonData.getJSONObject("data").containsKey("checkModule") || !jsonData.getJSONObject("data").containsKey("checkTime")) {
//                response.put("status", "error");
//                response.put("message", "Invalid request body: Missing essential 'data' fields.");
//                return new ResponseEntity<>(response, HttpStatus.BAD_REQUEST);
//            }

//            dataImportService.importDetectionData(jsonData);
            response.put("status", "success");
            response.put("message", "Detection data uploaded successfully.");
            return new ResponseEntity<>(response, HttpStatus.OK);
        } catch (IllegalArgumentException e) {
            response.put("status", "error");
            response.put("message", "Data validation failed: " + e.getMessage());
            return new ResponseEntity<>(response, HttpStatus.BAD_REQUEST);
        } catch (Exception e) {
            // 捕获所有其他未知异常，记录日志并返回通用错误信息
            System.err.println("Error uploading detection data: " + e.getMessage());
            e.printStackTrace(); // 在生产环境中应使用日志框架
            response.put("status", "error");
            response.put("message", "An internal server error occurred: " + e.getMessage());
            return new ResponseEntity<>(response, HttpStatus.INTERNAL_SERVER_ERROR);
        }
    }
}
