package com.emr.controller;

import com.emr.entity.*;
import com.emr.service.Archive_MasterService;
import com.emr.service.TempMasterIDService;
import com.emr.service.Temp_InfoService;
import com.emr.util.ThreadExcelUtils;
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.stereotype.Controller;
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.text.SimpleDateFormat;
import java.util.Date;
import java.util.List;

@Controller
public class TempHandleWzController {
    @Autowired
    private TempMasterIDService tempMasterIDService;

    @Autowired
    private Archive_MasterService archiveMasterService;

    @Autowired
    private Temp_InfoService tempInfoService;

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

    @RequestMapping("toReloadWzPage")
    public String toReloadWzPage() {
        return "reloadWz";
    }

    @RequestMapping("selectByInpNoAndVisitId")
    @ResponseBody
    public List<Archive_Master_Vo> selectByInpNoAndVisitId(Archive_Master_Vo archive_master_vo){
        return this.archiveMasterService.selectByUnfile(archive_master_vo);
    }

    @RequestMapping("reloadWz")
    @ResponseBody
    public int reloadWz(String masterId){
        int rows = 0;
        /*TempMasterID tempMasterID = new TempMasterID();
        tempMasterID.setId(masterId);
        List<TempMasterID> list = new ArrayList<>();
        list.add(tempMasterID);*/
        rows = tempMasterIDService.reloadWz(masterId);
        return rows;
    }


    @ResponseBody
    @RequestMapping(value = "/wzByArchivId")
    public String wzByArchivId(HttpServletRequest request, HttpServletResponse response, Temp_Info tempInfo) {
        response.setCharacterEncoding("utf-8");
        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());
            return result="连接不到完整性接口!";
        }

        return result;
    }


    @ResponseBody
    @RequestMapping(value = "/geWZdetail")
    public OffsetLimitPage inHospitalList(HttpServletRequest request, HttpServletResponse response, Temp_Info tempInfo, Integer offset, Integer limit) {
        OffsetLimitPage result = tempInfoService.selByCol(tempInfo, offset, limit);
        return result;
    }

    @ResponseBody
    @RequestMapping(value = "/exportExcel")
    public void exportExcel(HttpServletResponse response, Temp_Info tempInfo) throws Exception {
        response.setContentType("application/vnd.ms-excel;charset=UTF-8");
        String[] header = {"病案id"};
        String[] fileNames = {"masterId"};
        //文件名
        String fileName = "缺失列表" + new SimpleDateFormat("yyyyMMddHHmmss").format(new Date()) + ".xls";
        //构造excel的数据
        List<Temp_Info> list = tempInfoService.selByCol(tempInfo);
        ThreadExcelUtils utils = new ThreadExcelUtils(fileName, "", header, fileNames);
        String result=utils.exportExcelToFilePath(response,utils.listConvert(list));
    }

    /*@RequestMapping("reloadWzAll")
    @ResponseBody
    public int reloadWzAll(){
        int rows = 0;
        List<TempMasterID> list = tempMasterIDService.selectAllMasterID();
        rows = tempMasterIDService.reloadWz(list);

        *//*TempMasterID t1 = new TempMasterID();
        t1.setId("36cc3c27-447e-48e4-93b9-06b3acc397ca");
        TempMasterID t2 = new TempMasterID();
        t2.setId("a490ca0b-879f-4ae4-9238-33c44e5fda99");
        TempMasterID t3 = new TempMasterID();
        t3.setId("0687f5a2-40f0-48ce-872b-922d8ceba7da");
        TempMasterID t4 = new TempMasterID();
        t4.setId("169044f8-21a2-4420-ad01-c24b19910e3a");
        TempMasterID t5 = new TempMasterID();
        t5.setId("3f3d18cf-6f75-4367-9ada-3684e176c102");
        List<TempMasterID> list = new ArrayList<>();
        list.add(t1);
        list.add(t2);
        list.add(t3);
        list.add(t4);
        list.add(t5);
        rows = tempMasterIDService.reloadWz(list);*//*
        return rows;
    }*/
}
