package com.hdkj.lt.si.feign;

import cn.hutool.core.bean.BeanUtil;
import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.hdkj.lt.feign.IFileApiClient;
import com.hdkj.lt.feign.ILineSvgFileApiClient;
import com.hdkj.lt.modle.FileRequest;
import com.hdkj.lt.modle.dto.file.LineSvgFileBase;
import com.hdkj.lt.modle.dto.file.LineSvgFileGetDTO;
import com.hdkj.lt.modle.dto.svg.StationSvgGenerationDTO;
import com.hdkj.lt.modle.dto.svg.SvgGenerationDTO;
import com.hdkj.lt.modle.po.FhzgDistMappingFilePO;
import com.hdkj.lt.modle.vo.svg.LineFileInfoVO;
import com.hdkj.lt.modle.vo.svg.StationFileInfoVO;
import com.hdkj.lt.si.service.svg.IDistMappingFileService;
import com.hdkj.lt.si.service.svg.ISvgGenerationService;
import com.hdkj.hussar.ApiResponse;
import lombok.RequiredArgsConstructor;
import lombok.extern.slf4j.Slf4j;
import org.apache.commons.lang3.ObjectUtils;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RestController;

import javax.validation.Valid;
import java.util.List;

/**
 * @Description
 * @Author wcx
 * @Date 2025/6/3 15:27
 * @Version 1.0
 */

@Slf4j
@RestController
@RequiredArgsConstructor
public class LineSvgFileApiClient implements ILineSvgFileApiClient {

    private final IFileApiClient fileApiClient;
    private final ISvgGenerationService svgGenerationService;
    private final IDistMappingFileService distMappingFileService;

    /**
     * 获取异动线路信息
     */
    @PostMapping(ILineSvgFileApiClient.API_MAPPING_SERVICE_LINE_GET)
    public ApiResponse<List<LineSvgFileGetDTO>> getLineByVersion(@Valid @RequestBody LineSvgFileBase lineSvgFile) {
        LambdaQueryWrapper<FhzgDistMappingFilePO> queryWrapper = new LambdaQueryWrapper<FhzgDistMappingFilePO>()
                .eq(FhzgDistMappingFilePO::getTopoVersion, lineSvgFile.getTopoVersion())
                .eq(FhzgDistMappingFilePO::getFileType, lineSvgFile.getFileType());
        List<FhzgDistMappingFilePO> distMappingFile = distMappingFileService.list(queryWrapper);
        List<LineSvgFileGetDTO> lineSvgFileGet = BeanUtil.copyToList(distMappingFile, LineSvgFileGetDTO.class);
        // 获取文件信息
        return ApiResponse.success(lineSvgFileGet);
    }

    /**
     * 获取文件流信息
     */
    @PostMapping(ILineSvgFileApiClient.API_MAPPING_SERVICE_LINE_FILE_GET)
    public ApiResponse<byte[]> getLineFileInfo(@Valid @RequestBody LineSvgFileGetDTO lineSvgFile) {
        LambdaQueryWrapper<FhzgDistMappingFilePO> queryWrapper = new LambdaQueryWrapper<FhzgDistMappingFilePO>()
                .eq(FhzgDistMappingFilePO::getLineId, lineSvgFile.getLineId())
                .eq(FhzgDistMappingFilePO::getFileType, lineSvgFile.getFileType()).last(" limit 1");
        FhzgDistMappingFilePO distMappingFile = distMappingFileService.getOne(queryWrapper);
        if (ObjectUtils.isNotEmpty(distMappingFile)) {
            FileRequest request = FileRequest.builder().fileId(distMappingFile.getFileId()).build();
            byte[] fileData = fileApiClient.getFileById(request).getData();
            return ApiResponse.success(fileData);
        }
        // 获取文件信息
        return ApiResponse.fail(String.format("文件获取失败：线路ID【%s】", lineSvgFile.getLineId()));
    }

    /**
     * 保存动态SVG文件-线路、场景
     */
    @PostMapping(ILineSvgFileApiClient.API_MAPPING_SERVICE_LINE_FILE_SAVE)
    public ApiResponse<List<LineFileInfoVO>> lineDynamicSvgSave(@Valid @RequestBody SvgGenerationDTO svgGeneration) {
        return svgGenerationService.getAndSaveSvgGenerationByFeeder(svgGeneration);
    }

    /**
     * 保存动态SVG成图-变电站、场景
     */
    @PostMapping(ILineSvgFileApiClient.API_MAPPING_SERVICE_STATION_FILE_SAVE)
    public ApiResponse<List<StationFileInfoVO>> stationDynamicSvgSave(@Valid @RequestBody StationSvgGenerationDTO stationSvgDTO) {
        return svgGenerationService.getAndSaveSvgGenerationByStation(stationSvgDTO);
    }
}
