package com.zcloud.controller.mapApi.app;

import com.zcloud.controller.base.BaseController;
import com.zcloud.entity.PageData;
import com.zcloud.util.PLSUtil;
import org.springframework.core.io.ByteArrayResource;
import org.springframework.core.io.Resource;
import org.springframework.http.HttpHeaders;
import org.springframework.http.MediaType;
import org.springframework.http.ResponseEntity;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;

@Controller
@RequestMapping("/app/model")
public class AppMapModelController extends BaseController {
    // 服务器地图模型加载
    @RequestMapping(value = "/getModel")
    @ResponseBody
    public ResponseEntity<Resource> getModel() throws Exception {
        PageData pageData = this.getPageData();
        String filename = pageData.getString("filename");
        String CORPINFO_ID = pageData.getString("CORPINFO_ID");
        byte[] gltfFlow = PLSUtil.getGltfFlow(CORPINFO_ID,filename);
        ByteArrayResource resource = new ByteArrayResource(gltfFlow);

        // 获取文件名

        // 构建响应头
        HttpHeaders headers = new HttpHeaders();
        headers.add(HttpHeaders.CONTENT_DISPOSITION, "attachment; filename=");

        // 设置文件类型
        headers.setContentType(MediaType.APPLICATION_OCTET_STREAM);

        // 返回响应实体
        return ResponseEntity.ok()
                .headers(headers)
                .contentLength(resource.contentLength())
                .body(resource);
    }
}
