package com.brilliance.isc.common.gmgf.controller;

import com.brilliance.isc.common.doc.TextResultVo;
import com.brilliance.isc.common.gmgf.service.GmgfService;
import com.brilliance.isc.vo.ResponseSet;
import com.brilliance.isc.vo.model.BgdVo;
import com.brilliance.isc.vo.model.GmgfVo;
import io.swagger.annotations.Api;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.util.StringUtils;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

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

@Slf4j
@RestController
@RequestMapping("/gmgf")
@Api("公共组件国贸公服服务")
public class GmgfResource {
    @Autowired(required = false)
    GmgfService gmgfService;

    /**
     * 报关单参考余额查询
     */
    @PostMapping("/queryGdye")
    public ResponseSet<List<BgdVo>> queryGdye(GmgfVo gmgfVo) {
        try {
            return ResponseSet.simpleSuccess(gmgfService.queryGdye(gmgfVo));
        } catch (Exception e) {
            log.error("查询报关单参考余额异常", e);
            return ResponseSet.fail(e.getMessage());
        }
    }

    /**
     * 显示指定报关单信息
     */
    @PostMapping("/queryGdmx")
    public ResponseSet<TextResultVo> queryGdmx(GmgfVo gmgfVo) {
        try {
            return ResponseSet.simpleSuccess(gmgfService.queryGdmx(gmgfVo));
        } catch (Exception e) {
            log.error("查询报关单信息异常", e);
            return ResponseSet.fail(e.getMessage());
        }
    }


    /**
     * 验核报告调阅
     */
    @PostMapping("/queryYzbg")
    public ResponseSet<String> queryYzbg(GmgfVo gmgfVo) {
        try {
            return ResponseSet.simpleSuccess(gmgfService.queryYzbg(gmgfVo));
        } catch (Exception e) {
            log.error("验核报告调阅异常", e);
            return ResponseSet.fail(e.getMessage());
        }
    }


    /**
     * 报关单影像件调阅
     */
    @PostMapping("/queryImage")
    public ResponseSet<String> queryImage(GmgfVo gmgfVo) {
        try {
            return ResponseSet.simpleSuccess(gmgfService.queryImage(gmgfVo));
        } catch (Exception e) {
            log.error("报关单影像件调阅异常", e);
            return ResponseSet.fail(e.getMessage());
        }
    }


    /**
     * 查询验核饱和和关单影像pdf信息
     */
    @PostMapping("/queryPdf")
    public ResponseSet<Map<String, List>> queryPdf(GmgfVo gmgfVo) {
        try {
            return ResponseSet.simpleSuccess(gmgfService.queryPdf(gmgfVo));
        } catch (Exception e) {
            log.error("查询验核饱和和关单影像pdf信息异常", e);
            return ResponseSet.fail(e.getMessage());
        }
    }

    /**
     * 返回Pdf文件流给前端展示
     */
    @PostMapping("/showPdf")
    public ResponseSet<TextResultVo> showPdf(String inr) {
        try {
            if (StringUtils.hasText(inr)) {
                return ResponseSet.simpleSuccess(gmgfService.showPdf(inr));
            } else {
                return ResponseSet.fail("pdf预览终止：查询参数为空");
            }
        } catch (Exception e) {
            log.error("pdf文件预览异常", e);
            return ResponseSet.fail(e.getMessage());
        }
    }
}
