package com.lantone.structure.controller;

import com.lantone.structure.dto.AnalysisDTO;
import com.lantone.structure.dto.RespDTO;
import com.lantone.structure.facade.StructureFacade;
import com.lantone.structure.vo.AnalysisVO;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestBody;
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 java.io.BufferedReader;
import java.io.InputStream;
import java.io.InputStreamReader;

@Api(value = "后结构化接口", tags = { "后结构化接口" })
@RestController
@RequestMapping(value = "/lt/structure")
public class StructureController {

    @Autowired
    private StructureFacade structureFacade;

    @ApiOperation(value = "解析")
    @PostMapping("analysis")
    public RespDTO<AnalysisDTO> analysis(@RequestBody AnalysisVO analysisVO) {
        return RespDTO.onSuc(structureFacade.analysis(analysisVO));
    }

    @ApiOperation(value = "解析(上传文件版)")
    @PostMapping("analysisTest")
    public RespDTO<AnalysisDTO> analysisTest(@RequestParam("file") MultipartFile file, String type) {
        AnalysisVO analysisVO = new AnalysisVO();
        analysisVO.setType(type);
        StringBuffer sbf = new StringBuffer();
        InputStream is = null;
        InputStreamReader ir = null;
        BufferedReader br = null;
        try {
            is = file.getInputStream();
            ir = new InputStreamReader(is);
            br = new BufferedReader(ir);
            String line = null;
            while ((line = br.readLine()) != null) {
                line = line+"\n";
                sbf.append(line);
            }
        } catch (Exception e) {

        } finally {
            try {
                if (br != null) {
                    br.close();
                }
                if (ir != null) {
                    ir.close();
                }
                if (is != null) {
                    is.close();
                }
            } catch (Exception e) {

            }
        }
        analysisVO.setText(sbf.toString());
        return RespDTO.onSuc(structureFacade.analysis(analysisVO));
    }


}