package com.yeejoin.amos.boot.module.jg.biz.controller;

import com.yeejoin.amos.boot.biz.common.controller.BaseController;
import com.yeejoin.amos.boot.module.jg.biz.service.impl.DataDockServiceImpl;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import org.apache.commons.io.FilenameUtils;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.*;
import org.springframework.web.multipart.MultipartFile;
import org.typroject.tyboot.component.event.RestEventTrigger;
import org.typroject.tyboot.core.foundation.enumeration.UserType;
import org.typroject.tyboot.core.restful.doc.TycloudOperation;
import org.typroject.tyboot.core.restful.utils.ResponseHelper;
import org.typroject.tyboot.core.restful.utils.ResponseModel;

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

@Validated
@RestController
@Api(tags = "数据对接 API")
@RequestMapping(value = "/dataDock")
public class DataDockController extends BaseController {

    private final DataDockServiceImpl dataDockService;

    public DataDockController(DataDockServiceImpl dataDockService) {
        this.dataDockService = dataDockService;
    }

    /**
     * 西安数据对接-批量导入设备数据的接口
     *
     * @param equLists 设备数据集合
     * @return result
     */
    @TycloudOperation(ApiLevel = UserType.AGENCY)
    @PostMapping(value = "/xi-an/saveEquipmentData")
    @ApiOperation(httpMethod = "POST", value = "西安数据对接-设备批量导入", notes = "西安数据对接-导入多个设备的数据文件")
    public ResponseModel<?> saveEquipmentData(@RequestBody List<Map<String, Object>> equLists) {
        return ResponseHelper.buildResponse(dataDockService.xiAnSaveEquipmentData(equLists, getSelectedOrgInfo()));
    }

    @TycloudOperation(ApiLevel = UserType.AGENCY)
    @PostMapping(value = "/importData")
    @ApiOperation(httpMethod = "POST", value = "八大类历史设备导入", notes = "八大类历史设备导入")
    public Object importData(@RequestParam String remark, @RequestParam 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 ResponseHelper.buildResponse(dataDockService.dataCheckAndImportEquipmentData(remark, file, getSelectedOrgInfo()));
    }

    /**
     * 历史-台套类设备导入字段校验
     * @param file 导入文件
     * @return 导入数据
     */
    @TycloudOperation(ApiLevel = UserType.AGENCY)
    @PostMapping(value = "/checkHisSetEquipImportData")
    @ApiOperation(httpMethod = "POST", value = "历史-台套类设备导入字段校验", notes = "历史-台套类设备导入字段校验")
    public Object checkSetEquipImportData(@RequestParam 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 ResponseHelper.buildResponse(dataDockService.checkSetEquipImportData("his", file));
    }

    /**
     * 新设备台套类设备导入字段校验
     * @param file 导入文件
     * @return 导入数据
     */
    @TycloudOperation(ApiLevel = UserType.AGENCY)
    @PostMapping(value = "/checkNewSetEquipImportData")
    @ApiOperation(httpMethod = "POST", value = "新设备台套类设备导入字段校验", notes = "新设备台套类设备导入字段校验")
    public Object checkNewSetEquipImportData(@RequestParam 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 ResponseHelper.buildResponse(dataDockService.checkSetEquipImportData("new", file));
    }

    /**
     * 新设备台套类设备导入字段校验
     * @param file 导入文件
     * @return 导入数据
     */
    @TycloudOperation(ApiLevel = UserType.AGENCY)
    @PostMapping(value = "/checkBlackSetEquipImportData")
    @ApiOperation(httpMethod = "POST", value = "新设备台套类设备导入字段校验", notes = "新设备台套类设备导入字段校验")
    public Object checkBlackSetEquipImportData(@RequestParam 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 ResponseHelper.buildResponse(dataDockService.checkSetEquipImportData("black", file));
    }

    /**
     * 台套类设备，气瓶，新管道都使用此方法
     **/
    @TycloudOperation(ApiLevel = UserType.AGENCY)
    @PostMapping(value = "/saveSetEquipImportData")
    @ApiOperation(httpMethod = "POST", value = "台套类设备导入字段保存", notes = "台套类设备导入字段保存")
    @RestEventTrigger(value = "operateLogRestEventHandler")
    public ResponseModel<?> saveEquipImportData(@RequestBody Map<String, Object> paramMap) {
        return ResponseHelper.buildResponse(dataDockService.saveEquipImportData(paramMap, getSelectedOrgInfo()));
    }

    /**
     * 工业管道批量检查
     **/
    @TycloudOperation(ApiLevel = UserType.AGENCY)
    @PostMapping(value = "/checkGYGDData")
    @ApiOperation(httpMethod = "POST", value = "工业管道批量检查", notes = "工业管道批量检查")
    @RestEventTrigger(value = "operateLogRestEventHandler")
    public ResponseModel<?> checkGYGDData(@RequestPart("file") MultipartFile multipartFile) throws Exception {
        return ResponseHelper.buildResponse(dataDockService.checkGDData(multipartFile, true));
    }

    /**
     * 工业 、公用、长输管道批量保存
     **/
    @TycloudOperation(ApiLevel = UserType.AGENCY)
    @PostMapping(value = "/savePipingData")
    @ApiOperation(httpMethod = "POST", value = "工业 、公用、长输管道批量保存", notes = "工业 、公用、长输管道批量保存")
    @RestEventTrigger(value = "operateLogRestEventHandler")
    public ResponseModel<?> savePipingData(@RequestBody Map<String, Object> paramMap) {
        return ResponseHelper.buildResponse(dataDockService.savePipingData(paramMap, getSelectedOrgInfo()));
    }

    /**
     * 管道设备表省市区缺失字段补充
     **/
    @TycloudOperation(ApiLevel = UserType.AGENCY)
    @GetMapping(value = "/updatePipingUseInfo")
    @ApiOperation(httpMethod = "GET", value = "管道设备表省市区缺失字段补充", notes = "管道设备表省市区缺失字段补充")
    @RestEventTrigger(value = "updatePipingUseInfo")
    public ResponseModel<String> updatePipingUseInfo() {
        return ResponseHelper.buildResponse(dataDockService.updatePipingUseInfo());
    }

    /**
     * 公用、长输道批量检查
     **/
    @TycloudOperation(ApiLevel = UserType.AGENCY)
    @PostMapping(value = "/checkGYCSGDData")
    @ApiOperation(httpMethod = "POST", value = "公用、长输管道批量检查", notes = "公用、长输管道批量检查")
    @RestEventTrigger(value = "operateLogRestEventHandler")
    public ResponseModel<?> checkGYCSGDData(@RequestPart("file") MultipartFile multipartFile) throws Exception {
        return ResponseHelper.buildResponse(dataDockService.checkGDData(multipartFile, false));
    }

    /**
     * 西安电梯历史设备以及业务数据批量导入
     *
     * @param equLists 电梯历史设备以及业务数据
     * @return result
     */
    @TycloudOperation(ApiLevel = UserType.AGENCY)
    @PostMapping(value = "/xi-an/saveElevatorData")
    @ApiOperation(httpMethod = "POST", value = "西安电梯历史设备以及业务数据批量导入", notes = "西安电梯历史设备以及业务数据批量导入")
    public ResponseModel<?> saveElevatorData(@RequestBody List<Map<String, Object>> equLists) {
        return ResponseHelper.buildResponse(dataDockService.saveElevatorData(equLists));
    }
}

