package com.lantone.qc.kernel.web.controller;

import com.lantone.qc.kernel.analysis.QCAnalysis;
import com.lantone.qc.pub.exception.AIException;
import com.lantone.qc.pub.exception.CatalogueException;
import com.lantone.qc.pub.exception.TransException;
import com.lantone.qc.pub.model.OutputInfo;
import com.lantone.qc.pub.model.vo.QueryVo;
import com.lantone.qc.pub.res.Response;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import lombok.extern.slf4j.Slf4j;
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.RestController;

/**
 * @ClassName : QCController
 * @Description : 质控总入口
 * @Author : 楼辉荣
 * @Date: 2020-03-03 19:57
 */
@Slf4j
@Api(value = "质控服务接口", tags = { "质控服务接口" })
@RestController
@RequestMapping(value = "analyse")
public class QCController {

    @Autowired
    private QCAnalysis qCAnalysis;

    @ApiOperation(value = "质控外部接口，需要token信息", notes = "")
    @PostMapping("rec")
    public Response<OutputInfo> extract(@RequestBody QueryVo queryVo) {
        log.error(queryVo.getBehospitalInfo().getBehospitalCode() + "-----开始质控");
        long t1 = System.currentTimeMillis();
        Response response = new Response();
        try {
            response.setData(qCAnalysis.anlysis(queryVo));
        } catch (AIException aie) {
            log.error(aie.getMessage(), aie);
            response.setRet(-1);
            response.setMsg(aie.getMessage());
        } catch (TransException trae) {
            log.error(trae.getMessage(), trae);
            response.setRet(-2);
            response.setMsg(trae.getMessage());
        } catch (CatalogueException ce) {
            log.error(ce.getMessage(), ce);
            response.setRet(-3);
            response.setMsg(ce.getMessage());
        } catch (Exception e) {
            log.error(e.getMessage(), e);
            response.setRet(-9);
            response.setMsg("质控出错：" + e.getMessage());
        }
        long t2 = System.currentTimeMillis();
        log.error(queryVo.getBehospitalInfo().getBehospitalCode() + "-----质控结束  耗时：" + (t2 - t1));
        return response;
    }

}