package com.emr.controller;

import com.emr.entity.Archive_Master_Vo;
import com.emr.entity.OffsetLimitPage;
import com.emr.service.ScanInfoService;
import com.emr.util.DatelUtils;
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("/scanInfo")
public class ScanListController {
    @Autowired
    private ScanInfoService scanInfoService;

    /*
     * 扫描上传记录统计
     * */
    @RequestMapping(value = "/scanCount")
    public String scanCount(Model model) {
        return "scanDir/scanCountList";
    }

    /*
     * 复印记录统计分页
     * */
    @ResponseBody
    @RequestMapping(value = "/scanCountPage")
    public OffsetLimitPage scanCountPage(HttpServletRequest request, HttpServletResponse response, Archive_Master_Vo archiveMasterVo, Integer offset, Integer limit)throws Exception {
        archiveMasterVo= DatelUtils.setMaterVoFun(archiveMasterVo);
        OffsetLimitPage result = (OffsetLimitPage) scanInfoService.scanCountPage(archiveMasterVo, offset, limit);
        return result;
    }

    @ResponseBody
    @RequestMapping(value = "/exportCountExcel")
    public void exportCountExcel(HttpServletResponse response, Archive_Master_Vo archiveMasterVo) throws Exception {
        response.setContentType("application/vnd.ms-excel;charset=UTF-8");
        String[] header = {"扫描人","扫描日期","扫描次数","扫描份数"};
        String[] fileNames = {"creater","createTime","numb","content"};
        //文件名
        String fileName = "扫描上传记录统计" + new SimpleDateFormat("yyyyMMddHHmmss").format(new Date()) + ".xls";
        //构造excel的数据beHospList
        archiveMasterVo=DatelUtils.setMaterVoFun(archiveMasterVo);
        List<Archive_Master_Vo> list = scanInfoService.scanCountList(archiveMasterVo);
        ThreadExcelUtils utils = new ThreadExcelUtils(fileName, "", header, fileNames);
        String result=utils.exportExcelToFilePath(response,utils.listConvert(list));
    }


    /*
     * 扫描上传记录明细
     * */
    @RequestMapping(value = "/scanDetailList")
    public String scanDetailList(Model model) {
        return "scanDir/scanDetailList";
    }

    /*
     * 复印记录统计明细
     * */
    @ResponseBody
    @RequestMapping(value = "/scanDetailPage")
    public OffsetLimitPage scanDetailPage(HttpServletRequest request, HttpServletResponse response, Archive_Master_Vo archiveMasterVo, Integer offset, Integer limit)throws Exception {
        archiveMasterVo= DatelUtils.setMaterVoFun(archiveMasterVo);
        OffsetLimitPage result = (OffsetLimitPage) scanInfoService.scanDetailPage(archiveMasterVo, offset, limit);
        return result;
    }

    @ResponseBody
    @RequestMapping(value = "/exportExcel")
    public void exportExcel(HttpServletResponse response, Archive_Master_Vo archiveMasterVo) throws Exception {
        response.setContentType("application/vnd.ms-excel;charset=UTF-8");
       /* String[] header = {"记帐号","住院号","住院次数","患者姓名","性别","入院日期","出院日期","出院科室","主管医生","扫描人","文件名","扫描日期"};
        String[] fileNames = {"patientId","inpNo","visitId","name","sex","admissionDateTime","dischargeDateTime","deptName","doctorInCharge","creater","content","createTime"};*/
        String[] header = {"记帐号","住院号","住院次数","患者姓名","出院日期","扫描人","文件名","扫描日期"};
        String[] fileNames = {"patientId","inpNo","visitId","name","dischargeDateTime","creater","content","createTime"};
        //文件名
        String fileName = "扫描上传记录明细" + new SimpleDateFormat("yyyyMMddHHmmss").format(new Date()) + ".xls";
        //构造excel的数据beHospList
        archiveMasterVo=DatelUtils.setMaterVoFun(archiveMasterVo);
        List<Archive_Master_Vo> list = scanInfoService.scanDetailList(archiveMasterVo);
        ThreadExcelUtils utils = new ThreadExcelUtils(fileName, "", header, fileNames);
        String result=utils.exportExcelToFilePath(response,utils.listConvert(list));
    }
}
