/**
 * Copyright (C), 2015-2019
 * Author:   HJL
 * Date:     2019/9/17 11:30
 * Description:定时任务每天4点触发清空PDF文件和txt文件
 */
package com.emr.controller;

import com.emr.entity.Temp_Info;
import com.emr.service.Temp_InfoService;
import com.emr.util.PDFUtils;
import org.apache.cxf.endpoint.Client;
import org.apache.cxf.jaxws.endpoint.dynamic.JaxWsDynamicClientFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.scheduling.annotation.Scheduled;
import org.springframework.stereotype.Component;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.io.File;
import java.io.IOException;
import java.text.SimpleDateFormat;
import java.util.Date;
import java.util.List;

@Component
public class taskController {
    @Autowired
    private Temp_InfoService tempInfoService;

    @Value("${HomepageDictionary}")
    private String HomepageDictionary;
    @Value("${HomepageMethod}")
    private String HomepageMethod;

    @Value("${pdfErrorUrl}")
    private String pdfErrorUrl;

    @Value("${autoPdfUrl}")
    private String autoPdfUrl;

    //@Scheduled(cron="0/1 * *  * * ? ")   //每1秒执行一次
    @Scheduled(cron="${fileDelay}")//每天4点触发cron="0 0 4 * * ?"
    public void task() throws IOException {
        //static\img\pdfFiles\typesPdf
        //String projectPath = System.getProperty("user.dir") + "\\static\\img\\pdfFiles\\typesPdf\\";
        // 项目在容器中实际发布运行的根路径
        //String projectPath = System.getProperty("user.dir");
        //在类中输出System.getProperty("user.dir");显示的是%Tomcat_Home%/bin
       // projectPath=projectPath.replace("bin","webapps\\emr_medical_record\\static\\img\\pdfFiles\\typesPdf\\");

        //System.out.println("projectPath=" + projectPath);
        File outFile1 = new File(autoPdfUrl);
        //如果文件夹不存在则创建
        if (!outFile1.exists() && !outFile1.isDirectory()) {
            outFile1.mkdirs();
        }
        PDFUtils.delAllFile(autoPdfUrl);

        File outFile = new File(pdfErrorUrl);
        //如果文件夹不存在则创建
        if (!outFile.exists() && !outFile.isDirectory()) {
            outFile.mkdirs();
        }
        PDFUtils.delAllFile(pdfErrorUrl);
    }

    //@Scheduled(cron="0 0 3 * * ?")//每天3点触发0 0 3 * * ?
    @Scheduled(cron="${fixedDelay}")
    public void task2() throws IOException {
        Temp_Info tempInfo=new Temp_Info();
        SimpleDateFormat format1 = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
        String result = "";
        int bol=0;
        try {
            //重置缺失校验
            String endpoint = HomepageDictionary;
            String method = HomepageMethod;
            JaxWsDynamicClientFactory factory = JaxWsDynamicClientFactory.newInstance();
            Client client = factory.createClient(endpoint);
            Object[] res = null;

            //根据参数的返回集删除
            bol=tempInfoService.delByMasterID(tempInfo);
            if(bol>=0) {
                bol=0;
                //根据参数查询缺失完整信息
                List<Temp_Info> list = tempInfoService.selByTempMaster(tempInfo);
                if (list != null && list.size() > 0) {
                    String masterID = "";
                    for (int i = 0; i < list.size(); i++) {
                        masterID = list.get(i).getMasterId();
                        if (masterID != null) {
                            tempInfo.setCheckDatetime(format1.format(new Date()));
                            res = client.invoke(method, masterID);
                            result = (String) res[0];
                            //给方法传递参数，并且调用方法
                            //System.out.println("result is " + result);
                            if (!result.equals("完整")) {
                                //缺失保存到数据库TempMasterID
                                tempInfo.setMasterId(masterID);
                                tempInfo.setInpNo(list.get(i).getInpNo());
                                tempInfo.setVisitId(list.get(i).getVisitId());
                                tempInfo.setDischargeDateTime(""+list.get(i).getDischargeDateTime());
                                tempInfo.setCheckedDatetime(format1.format(new Date()));
                                tempInfo.setLockinfo(result);
                                bol+= tempInfoService.insertSelective(tempInfo);
                            }
                        }
                    }
                    if(list.size()==bol){
                        result="已重置完成!";
                    }else if(list.size()>bol){
                        result="部分重置完成!";
                    }
                }else{
                    result="无此记录!";
                }
            }
        } catch (Exception e) {
            System.err.println(e.toString());
            result="连接不到完整性接口!";
        }
        //return result;
    }


}
 

