package com.njcn.gather.tool.parsepqdif.controller;

import com.njcn.common.pojo.annotation.OperateInfo;
import com.njcn.common.pojo.enums.common.LogEnum;
import com.njcn.common.pojo.enums.response.CommonResponseEnum;
import com.njcn.common.pojo.response.HttpResult;
import com.njcn.common.utils.LogUtil;
import com.njcn.gather.tool.parsepqdif.pojo.vo.PqdifParseResponse;
import com.njcn.gather.tool.parsepqdif.pojo.vo.PqdifRecognizeResultVO;
import com.njcn.gather.tool.parsepqdif.service.ParsePqdifService;
import com.njcn.web.controller.BaseController;
import com.njcn.web.utils.HttpResultUtil;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiImplicitParam;
import io.swagger.annotations.ApiOperation;
import lombok.RequiredArgsConstructor;
import lombok.extern.slf4j.Slf4j;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestPart;
import org.springframework.web.bind.annotation.RestController;
import org.springframework.web.multipart.MultipartFile;

/**
 * PQDIF 解析接口入口。
 */
@Slf4j
@Api(tags = "PQDIF解析")
@RestController
@RequestMapping("/api/parse-pqdif")
@RequiredArgsConstructor
public class ParsePqdifController extends BaseController {

    private final ParsePqdifService parsePqdifService;

    @OperateInfo(info = LogEnum.BUSINESS_COMMON)
    @ApiOperation("上传 PQDIF 文件并解析")
    @ApiImplicitParam(name = "pqdifFile", value = "PQDIF文件", required = true, dataType = "__file", paramType = "form")
    @PostMapping(value = "/parse", consumes = {"multipart/form-data"})
    public HttpResult<PqdifParseResponse> parse(@RequestPart("pqdifFile") MultipartFile pqdifFile) {
        String methodDescribe = getMethodDescribe("parse");
        LogUtil.njcnDebug(log, "{}，PQDIF解析入口，fileName={}",
                methodDescribe, pqdifFile == null ? null : pqdifFile.getOriginalFilename());
        PqdifParseResponse result = parsePqdifService.parse(pqdifFile);
        return HttpResultUtil.assembleCommonResponseResult(CommonResponseEnum.SUCCESS, result, methodDescribe);
    }

    @OperateInfo(info = LogEnum.BUSINESS_COMMON)
    @ApiOperation("上传 PQDIF 文件并解析为可识别数据")
    @ApiImplicitParam(name = "pqdifFile", value = "PQDIF文件", required = true, dataType = "__file", paramType = "form")
    @PostMapping(value = "/parse-recognizable", consumes = {"multipart/form-data"})
    public HttpResult<PqdifRecognizeResultVO> parseRecognizable(@RequestPart("pqdifFile") MultipartFile pqdifFile) {
        String methodDescribe = getMethodDescribe("parseRecognizable");
        LogUtil.njcnDebug(log, "{}，PQDIF二次解析入口，fileName={}",
                methodDescribe, pqdifFile == null ? null : pqdifFile.getOriginalFilename());
        PqdifParseResponse parseResponse = parsePqdifService.parse(pqdifFile);
        PqdifRecognizeResultVO result = parsePqdifService.parseRecognizable(parseResponse);
        return HttpResultUtil.assembleCommonResponseResult(CommonResponseEnum.SUCCESS, result, methodDescribe);
    }
}
