package com.usky.cdi.controller;

import com.usky.cdi.service.impl.BaseDataTransferService;
import com.usky.cdi.service.vo.info.EngineeringBaseVO;
import com.usky.cdi.service.vo.info.FacilityDeviceVO;
import com.usky.cdi.service.vo.info.FloorPlaneVO;
import com.usky.cdi.service.vo.info.ProtectiveUnitVO;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.boot.autoconfigure.condition.ConditionalOnProperty;
import org.springframework.web.bind.annotation.*;

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

/**
 * 基础类数据传输控制器
 * 提供基础类数据上报的接口
 *
 * @author han
 * @date 2025/03/20
 */
@Slf4j
@RestController
@RequestMapping("/api/base")
@ConditionalOnProperty(prefix = "mqtt", value = {"enabled"}, havingValue = "true")
public class BaseDataController {

    @Autowired
    private BaseDataTransferService baseDataTransferService;

    /**
     * 上报人防工程基础信息
     */
    @PostMapping("/engineering")
    public String sendEngineeringBase(@RequestBody EngineeringBaseVO vo) {
        boolean success = baseDataTransferService.sendEngineeringBase(vo);
        return success ? "上报成功" : "上报失败";
    }

    /**
     * 上报防护单元基础信息
     */
    @PostMapping("/protectiveUnit")
    public String sendProtectiveUnit(@RequestBody ProtectiveUnitVO vo) {
        boolean success = baseDataTransferService.sendProtectiveUnit(vo);
        return success ? "上报成功" : "上报失败";
    }

    /**
     * 批量上报防护单元基础信息
     */
    @PostMapping("/protectiveUnits")
    public String batchSendProtectiveUnits(@RequestBody List<ProtectiveUnitVO> units) {
        int successCount = baseDataTransferService.batchSendProtectiveUnits(units);
        return String.format("上报成功 %d/%d", successCount, units.size());
    }

    /**
     * 上报楼层平面图信息
     */
    @PostMapping("/floorPlane")
    public String sendFloorPlane(@RequestBody FloorPlaneVO vo) {
        boolean success = baseDataTransferService.sendFloorPlane(vo);
        return success ? "上报成功" : "上报失败";
    }

    /**
     * 上报智能监管物联设施信息
     */
    @PostMapping("/sensorInfo")
    public String sendSensorInfo(@RequestBody FacilityDeviceVO vo) {
        boolean success = baseDataTransferService.sendSensorInfo(vo);
        return success ? "上报成功" : "上报失败";
    }

    /**
     * 批量上报智能监管物联设施信息
     */
    @GetMapping("/sensorInfos")
    public String batchSendSensorInfos(@RequestParam(value = "tenantId",required = false) Integer tenantId,
                                       @RequestParam(value = "engineeringId") Long engineeringId,
                                       @RequestParam(value = "username") String username,
                                       @RequestParam(value = "password") String password) {
        Map<String, Integer> map = baseDataTransferService.batchSendSensorInfos(tenantId, engineeringId, username, password);
        return String.format("上报成功 %d", map.get("success"));
    }
}

