package com.yeejoin.amos.api.openapi.controller;

import com.yeejoin.amos.api.openapi.service.XiAnDataDockServiceImpl;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import org.apache.commons.io.FilenameUtils;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.RestController;
import org.springframework.web.multipart.MultipartFile;
import org.typroject.tyboot.core.foundation.enumeration.UserType;
import org.typroject.tyboot.core.restful.doc.TycloudOperation;
import org.typroject.tyboot.core.restful.utils.ResponseHelper;


@RestController
@Api(tags = "西安数据对接 API")
@RequestMapping(value = "/xi-an")
public class XiAnDataDockController {

    private final XiAnDataDockServiceImpl xiAnDataDockService;

    public XiAnDataDockController(XiAnDataDockServiceImpl xiAnDataDockService) {
        this.xiAnDataDockService = xiAnDataDockService;
    }

    /**
     * 批量导入设备数据的接口
     *
     * @param file 上传的文件
     * @return ResponseModel 封装的响应数据
     */
    @TycloudOperation(ApiLevel = UserType.AGENCY, needAuth = false)
    @PostMapping(value = "/importData")
    @ApiOperation(httpMethod = "POST", value = "设备批量导入", notes = "导入多个设备的数据文件")
    public Object importPressureData(@RequestParam("file") MultipartFile file) {
        // 校验文件是否为空
        if (file.isEmpty()) {
            return ResponseHelper.buildResponse("文件不能为空");
        }

        // 校验文件类型为 Excel
        String contentType = file.getContentType();
        String fileExtension = FilenameUtils.getExtension(file.getOriginalFilename());

        // 校验 Excel 文件的 MIME type 和扩展名
        if (!("application/vnd.ms-excel".equals(contentType) ||
                "application/vnd.openxmlformats-officedocument.spreadsheetml.sheet".equals(contentType)) ||
                !("xls".equalsIgnoreCase(fileExtension) || "xlsx".equalsIgnoreCase(fileExtension))) {
            return ResponseHelper.buildResponse("文件类型必须是 Excel 文件");
        }
        return xiAnDataDockService.importEquipmentData(file);
    }
}

