package com.lantone.qc.dbanaly.controller;

import com.google.common.collect.Lists;
import com.lantone.qc.dbanaly.facade.changx.ChangxXmlDataAnalysisFacade;
import com.lantone.qc.dbanaly.facade.comsis.ModuleHelper;
import com.lantone.qc.dbanaly.facade.comsis.RedisFacade;
import com.lantone.qc.dbanaly.facade.shaoyf.ShaoyfXmlDataAnalysisFacade;
import com.lantone.qc.dbanaly.facade.taizhou.TaiZhouXmlDataAnalysisFacade;
import com.lantone.qc.dbanaly.util.RedisUtil;
import com.lantone.qc.dbanaly.vo.AnalyzeXmlVO;
import com.lantone.qc.dbanaly.vo.CrfCacheRemoveVO;
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;

import java.util.List;

/**
 * @ClassName : QCController
 * @Description : 质控总入口
 * @Author : 楼辉荣
 * @Date: 2020-03-03 19:57
 */
@Slf4j
@Api(value = "数据对接接口", tags = { "数据对接接口" })
@RestController
@RequestMapping(value = "/db")
public class DbController {
    @Autowired
    private ModuleHelper moduleHelper;
    @Autowired
    private ChangxXmlDataAnalysisFacade changxXmlDataAnalysisFacade;
    @Autowired
    private TaiZhouXmlDataAnalysisFacade taiZhouXmlDataAnalysisFacade;
    @Autowired
    private ShaoyfXmlDataAnalysisFacade shaoyfXmlDataAnalysisFacade;
    @Autowired
    private RedisFacade redisFacade;

    @ApiOperation(value = "模板信息加载")
    @PostMapping("loadModuleInfo")
    public Response<Boolean> loadModuleInfo() {
        Response response = new Response();
        response.setData(moduleHelper.loadModuleInfo());
        return response;
    }

    @ApiOperation(value = "xml数据分析")
    @PostMapping("analyzeXml")
    public Response<Boolean> analyzeXml(@RequestBody AnalyzeXmlVO analyzeXmlVO) {
        Response response = new Response();
        List<String> msgList = Lists.newArrayList();
        Long hospitalId = analyzeXmlVO.getHospitalId();
        if (hospitalId == null) {
            msgList.addAll(changxXmlDataAnalysisFacade.analyzeXml(analyzeXmlVO.getModeIds()));
            msgList.addAll(shaoyfXmlDataAnalysisFacade.analyzeXml(analyzeXmlVO.getModeIds()));
            msgList.addAll(taiZhouXmlDataAnalysisFacade.analyzeXml(analyzeXmlVO.getModeIds()));
        } else if (hospitalId == 1) {
            msgList = changxXmlDataAnalysisFacade.analyzeXml(analyzeXmlVO.getModeIds());
        } else if (hospitalId == 2) {
            msgList = shaoyfXmlDataAnalysisFacade.analyzeXml(analyzeXmlVO.getModeIds());
        } else if (hospitalId == 3) {
            msgList = taiZhouXmlDataAnalysisFacade.analyzeXml(analyzeXmlVO.getModeIds());
        }
        response.setData(msgList);
        return response;
    }

    @ApiOperation(value = "crf缓存清理")
    @PostMapping("crfCacheRemove")
    public Response<Boolean> crfCacheRemove(@RequestBody CrfCacheRemoveVO crfCacheRemoveVO) {
        Response response = new Response();
        response.setData(redisFacade.crfCacheRemove(crfCacheRemoveVO));
        return response;
    }

}