package com.ann.demo.controller;

import com.ann.demo.entity.constant.InterfaceName;
import com.ann.demo.entity.filing.ArchiveDetail;
import com.ann.demo.entity.filing.ArchiveDetail20201218;
import com.ann.demo.entity.filing.MessageScanning;
import com.ann.demo.entity.filing.MessageSubordinate;
import com.ann.demo.entity.filing.dto.MessageDto;
import com.ann.demo.entity.interfaceEntity.ArchiveMaster;
import com.ann.demo.entity.interfaceEntity.EMRData;
import com.ann.demo.repository.*;
import com.ann.demo.service.AnalysisService;
import com.ann.demo.service.ArchiveMasterService;
import com.ann.demo.utils.XMLUtils;
import org.apache.cxf.endpoint.Client;
import org.apache.cxf.jaxws.endpoint.dynamic.JaxWsDynamicClientFactory;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;

import java.io.File;
import java.util.Date;
import java.util.List;
import java.util.Objects;
import java.util.Optional;

/**
 * @Author: LeiJiaXin
 * @Date: 2019/7/11 18:47
 */
@Controller
@RequestMapping("/")
public class TestController {

    @Autowired
    AnalysisService analysisService;

    @Autowired
    private ArchiveDetail20201218Repository archiveDetailRepository;

    @Autowired
    MessageSubordinateRepository messageSubordinateRepository;

    @Autowired
    ArchiveMasterRepository archiveMasterRepository;

    @Autowired
    private MessageScanningRepository messageScanningRepository;

    private static final Logger logger = LoggerFactory.getLogger(AnalysisService.class);

    @RequestMapping("/demo")
    public String aa() {
        return "demo";
    }

    @RequestMapping("/haha")
    public String cc(InterfaceForm interfaceForm) {
        JaxWsDynamicClientFactory dcf = JaxWsDynamicClientFactory.newInstance();
        Client client = dcf.createClient(interfaceForm.getUrl());
        Object[] objects = new Object[0];
        try {
            // invoke("方法名",参数1,参数2,参数3....);
            if (Objects.equals(interfaceForm.getInterfaceName(), "ReceiveDict") ||
                    Objects.equals(interfaceForm.getInterfaceName(), "InpSummary") ||
                    Objects.equals(interfaceForm.getInterfaceName(), "handNumbnessReport") ||
                    Objects.equals(interfaceForm.getInterfaceName(), InterfaceName.ICU_REPORT) ||
                    Objects.equals(interfaceForm.getInterfaceName(), InterfaceName.BLOODAPPLY_REPORT) ||
                    Objects.equals(interfaceForm.getInterfaceName(), "DoctorData") ||
                    Objects.equals(interfaceForm.getInterfaceName(), "NurseData") ||
                    Objects.equals(interfaceForm.getInterfaceName(), "ScanningFile")) {
                objects = client.invoke(interfaceForm.getInterfaceName(), interfaceForm.getContent());
            } else {
                objects = client.invoke(interfaceForm.getInterfaceName(), interfaceForm.getParameter(), interfaceForm.getContent());
            }
            System.out.println(objects[0]);
        } catch (Exception e) {
            e.printStackTrace();
        }
        return "redirect:/demo";
    }

//    @ResponseBody
//    @RequestMapping("/cc")
//    public String cc1(){
//        String name = "";
//        //补救写入
//        List<ArchiveDetail20201218> all = archiveDetailRepository.getAllBySource("嘉时扫描系统");
//        for (ArchiveDetail20201218 archiveDetail:
//                all) {
//            ArchiveMaster archiveMaster = archiveMasterRepository.findArchiveMaster(archiveDetail.getMasterID());
//            name = "Z:/project_js/docus_scanningfile_20201216/2020/12/"+archiveMaster.getInpNo()+"_" + archiveMaster.getVisitId()+"/"+archiveDetail.getId()+".pdf";
//
//            File localFile = new File(name);
//            if (localFile.exists()) {
//                archiveDetail.setPdfPath(name);
//                archiveDetailRepository.save(archiveDetail);
//            }
//        }
//        return name;
//    }

    @ResponseBody
    @RequestMapping("/cc")
    public String cc1(){
        List<MessageScanning> all = messageScanningRepository.findAllMessageScanning();
        int i =0;
        //补救写入
        for (MessageScanning messageScanning:
                all) {
            ArchiveMaster archiveMaster = null;
            Integer status = 3;
            MessageDto messageDto = new MessageDto();

            String outContent = "失败",remark = null;
            try {
                boolean flag = false;
                messageDto = XMLUtils.xpathToBean(messageScanning.getContent(), EMRData.class, InterfaceName.SCANNING_FILE);
                if (messageDto != null) {
                    archiveMaster = archiveMasterRepository.findByInpNoAndVisitIdAndIsValid(messageDto.getInpNo(),messageDto.getVisitId(),0);
                    if(archiveMaster != null){
                        remark = analysisService.handlePdfFile(messageDto,archiveMaster);
                        if(remark == null){
                            status =1;
                            flag = true;
                        }
                    }else{
                        remark = "没有患者信息";
                    }
                } else {
                    remark = "传过来的消息无法解析";
                }
                outContent = AnalysisService.getReturnResult(flag, remark);
                i = i++;
                System.out.println(i);
            }catch (Exception e){
                remark = e.toString();
                logger.error("出错咯！错误信息为{}，以及错误行数为:{}" ,e,e.getStackTrace()[0]);
            }
            messageScanning.setRemark(remark);
            messageScanning.setStatus(status);
            messageScanning.setCreateTime(new Date());
            messageScanningRepository.save(messageScanning);
        }
        return "hh";
    }
}
