/**
 * Copyright (C), 2015-2019
 * Author:   HJL
 * Date:     2019/7/19 14:39
 * Description:缺陷管理
 */
package com.emr.controller;

import com.emr.entity.*;
import com.emr.service.*;
import com.emr.util.ThreadExcelUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
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
@RequestMapping("vCount")
public class VCountController {
    @Autowired
    private V_CountService v_countService;

    @Autowired
    private Archive_MasterService archiveMasterService;

    @Autowired
    private Emr_DictionaryService emrDictionaryService;

    @RequestMapping(value = "/vCounts")
    public String faults(Model model){
        return "vCountDir/vCountList";
    }

    @ResponseBody
    @RequestMapping(value = "/vCountList")
    public OffsetLimitPage faultList(HttpServletRequest request, HttpServletResponse response, V_Count vCount, Integer offset, Integer limit) {
        OffsetLimitPage result = v_countService.selectPageByClo(vCount,offset, limit);
        return result;
    }

    @ResponseBody
    @RequestMapping(value = "/vCountNumList")
    public List<V_Count> medicalCountDayList(HttpServletResponse response, V_Count vCount) throws Exception {
        //统计列表
        return v_countService.selectByCol(vCount);
    }

    @ResponseBody
    @RequestMapping(value = "/exportExcel")
    public void exportExcel(HttpServletResponse response, V_Count vCount) throws Exception {
        response.setContentType("application/vnd.ms-excel;charset=UTF-8");
        String[] header = {"科室代码","科室名称","出院人数","已归档","未归档","死亡人数","归档率(%)","2日率(%)","3日率(%)","7日率(%)"};
        String[] fileNames = {"deptCode","deptName","outNum","fileNum","unfileNum","deathNum","fileRate","day2Rate","day3Rate","day7Rate"};
        //文件名
        String fileName = "统计数据" + new SimpleDateFormat("yyyyMMddHHmmss").format(new Date()) + ".xls";
        //构造excel的数据
        List<V_Count> list = v_countService.selectByCol(vCount);
        ThreadExcelUtils utils = new ThreadExcelUtils(fileName, "", header, fileNames);
        String result=utils.exportExcelToFilePath(response,utils.listConvert(list));
    }

    @ResponseBody
    @RequestMapping(value = "/exportExcel2")
    public void exportExcel(HttpServletResponse response, Archive_Master_Vo archiveMasterVo) throws Exception {
        response.setContentType("application/vnd.ms-excel;charset=UTF-8");
        String[] header = {"名字,入院科室,入院日期,出院科室,出院日期,主管医生"};
        String[] fileNames = {"name","deptAdmissionTo","admissionDateTime","deptName","dischargeDateTime","doctorInCharge"};
        //文件名
        String fileName = "未归档病历列表" + new SimpleDateFormat("yyyyMMddHHmmss").format(new Date()) + ".xls";
        //构造excel的数据
        List<Archive_Master_Vo> list = archiveMasterService.selectByUnfile(archiveMasterVo);
        Emr_Dictionary dic = new Emr_Dictionary();
        dic.setEffective(1);
        dic.setTypecode("dept_code");
        //科室列表
        List<Emr_Dictionary> dicList = emrDictionaryService.dicByTypeCode(dic);
        for (int i = 0; i < list.size(); i++) {
            //替换科室
            for (int k = 0; k < dicList.size(); k++) {
                String deptName = list.get(i).getDeptName();
                //入院科室dept_admission_to
                String dept2 = list.get(i).getDeptAdmissionTo();
                if ((deptName != null && deptName.equals(dicList.get(k).getCode()))) {
                    //出院科室
                    deptName = deptName.replace(deptName, dicList.get(k).getName());
                    list.get(i).setDeptName(deptName);
                }
                if(dept2 != null && dept2.equals(dicList.get(k).getCode())) {
                    dept2 = dept2.replace(dept2, dicList.get(k).getName());
                    list.get(i).setDeptAdmissionTo(dept2);
                }
            }
        }
        ThreadExcelUtils utils = new ThreadExcelUtils(fileName, "", header, fileNames);
        String result=utils.exportExcelToFilePath(response,utils.listConvert(list));

    }

    /*@ResponseBody
    @RequestMapping(value = "/exportExcel3")
    public void exportExcel3(HttpServletResponse response, Archive_Master_Vo archiveMasterVo) throws Exception {
        String tableThNames = "名字,入院科室,入院日期,出院科室,出院日期,主管医生,推迟天数";
        String fieldCns = "name,deptAdmissionTo,admissionDateTime,deptName,dischargeDateTime,doctorInCharge,cmtNurse";
        //构造excel的数据
        List<Archive_Master> list = archiveMasterService.sel7DayByCol(archiveMasterVo);
        Emr_Dictionary dic = new Emr_Dictionary();
        dic.setEffective(1);
        dic.setTypecode("dept_code");
        //科室列表
        List<Emr_Dictionary> dicList = emrDictionaryService.dicByTypeCode(dic);

        for (int i = 0; i < list.size(); i++) {
            //替换科室
            for (int k = 0; k < dicList.size(); k++) {
                String deptName = list.get(i).getDeptName();
                //入院科室dept_admission_to
                String dept2 = list.get(i).getDeptAdmissionTo();
                 if (deptName != null && deptName.equals(dicList.get(k).getCode())) {
                    //出院科室
                    deptName = deptName.replace(deptName, dicList.get(k).getName());
                    list.get(i).setDeptName(deptName);
                }
                if(dept2 != null && dept2.equals(dicList.get(k).getCode())){
                    //入院科室dept_admission_to
                    dept2 = dept2.replace(dept2, dicList.get(k).getName());
                    list.get(i).setDeptAdmissionTo(dept2);
                }
            }
        }

        //文件名
        String fileName = "超过7天的初审和已归档列表" + new SimpleDateFormat("yyyyMMddHHmmss").format(new Date()) + ".xls";
        //ExportExcelUtil
        ExportExcelUtil exportExcelUtil = new ExportExcelUtil();
        response.setContentType("application/ms-excel;charset=gbk");
        //导出excel的操作
        exportExcelUtil.expordExcel(tableThNames, fieldCns, list, fileName, response);
    }*/

    //获取完整信息
    /*@ResponseBody
    @RequestMapping(value = "/HomepageDic")
    public String HomepageDic(HttpServletRequest request, HttpServletResponse response) {
       String result="";
        try {
            String endpoint = "http://120.27.212.36:9999/filing/services/HomepageDictionary?wsdl";
            //String endpoint = "http://localhost:8080/ca3/services/caSynrochnized?wsdl";

            //直接引用远程的wsdl文件

            //以下都是套路
            Service service = new Service();
            Call call = (Call) service.createCall();
            call.setTargetEndpointAddress(endpoint);

            call.setOperationName(new QName("http://interfaces.ann.com/", "CheckData"));


            //call.setOperationName("CheckData");//WSDL里面描述的接口名称
            call.addParameter("masterId", org.apache.axis.encoding.XMLType.XSD_DATE, javax.xml.rpc.ParameterMode.IN);//接口的参数

            call.setReturnType(org.apache.axis.encoding.XMLType.XSD_STRING);//设置返回类型

            String temp = "02e4ba46662327995711";

             result = (String) call.invoke(new Object[]{temp});

            //给方法传递参数，并且调用方法

            //System.out.println("result is " + result);

        } catch (Exception e) {

            //System.err.println(e.toString());

        }
        return result;
    }*/
}
 

