/**
 * Copyright (C), 2015-2019
 * Author:   HJL
 * Date:     2019/8/8 12:46
 * Description:
 */
package com.emr.controller;

import com.emr.entity.*;
import com.emr.service.Archive_MasterService;
import com.emr.service.Emr_Fault_DetailService;
import com.emr.service.Emr_Fault_TypeService;
import com.emr.util.ThreadExcelUtils;
import org.apache.shiro.SecurityUtils;
import org.apache.shiro.session.Session;
import org.apache.shiro.subject.Subject;
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.io.FileInputStream;
import java.io.IOException;
import java.io.OutputStream;
import java.text.SimpleDateFormat;
import java.util.Date;
import java.util.List;

@Controller
@RequestMapping("commCtr")
public class commitController {

    @Autowired
    private Archive_MasterService archiveMasterService;

    @Autowired
    private Emr_Fault_TypeService emrFaultTypeService;

    @Autowired
    private Emr_Fault_DetailService emrFaultDetailService;

    @RequestMapping(value = "/commList")
    public String faults(Model model) {
        return "commitDir/commitList";
    }

    @ResponseBody
    @RequestMapping(value = "/commitList")
    public OffsetLimitPage commitList(HttpServletRequest request, HttpServletResponse response, Archive_Master_Vo archiveMasterVo, Integer offset, Integer limit) {
        OffsetLimitPage result = archiveMasterService.selectByCmtCol(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 = {"病历清单id","住院号","住院次数","名字","性别","身份证","出院科室","出院日期","主管医生","状态","医生是否提交","医生","医生提交日期","护士是否提交","护士","护士提交日期","是否死亡"};
        String[] fileNames = {"id,inpNo","visitId","name","sex","idNo","deptName","dischargeDateTime","doctorInCharge","archivestate","cmtDoctor","doctorName","cmtDoctorDate","cmtNurse","nurseName","cmtDurseDate","deathFlag"};
        //文件名
        String fileName = "科主任审核" + new SimpleDateFormat("yyyyMMddHHmmss").format(new Date()) + ".xls";
        //构造excel的数据
        List<Archive_Master_Vo> list = archiveMasterService.selectByCmtCol(archiveMasterVo);
        ThreadExcelUtils utils = new ThreadExcelUtils(fileName, "", header, fileNames);
        String result=utils.exportExcelToFilePath(response,utils.listConvert(list));

    }


    @ResponseBody
    @RequestMapping(value = "/getFaultType")
    public List<Emr_Fault_Type> getFaultType(HttpServletRequest request, HttpServletResponse response, Emr_Fault_Type emrFaultType) {
        List<Emr_Fault_Type> result = emrFaultTypeService.selectByCol(emrFaultType);
        return result;
    }


    @ResponseBody
    @RequestMapping(value = "/getDetailByArchId")
    public Emr_Fault_Detail getDetailByArchId(HttpServletRequest request, HttpServletResponse response, Emr_Fault_Detail emrFaultDetail) {
        Emr_Fault_Detail result = emrFaultDetailService.selectByArchiveDetailId(emrFaultDetail);
        return result;
    }

    @ResponseBody
    @RequestMapping(value = "/updateDetailByArchivId")
    public int updateDetailByArchivId(HttpServletRequest request, HttpServletResponse response, Emr_Fault_Detail emrFaultDetail) {
        response.setCharacterEncoding("utf-8");
        //修改病案归档状态：复审退回
        Archive_Master archiveMaster = new Archive_Master();
        archiveMaster.setId(emrFaultDetail.getArchiveDetailId());
        archiveMaster.setArchivestate("主任退回");
        archiveMaster.setCmtDoctor(0);
        archiveMaster.setCmtNurse(0);
        archiveMaster.setDoctorName("");
        archiveMaster.setNurseName("");
        archiveMaster.setCmtNurseDate("");
        archiveMaster.setCmtDoctorDate("");
        int bol = archiveMasterService.updateByClo(archiveMaster);

        //修改复审内容
        if (bol == 1) {
            // 从session获取用户名
            Power_User user = (Power_User) request.getSession().getAttribute("CURRENT_USER");
            String username = user.getUserName();//获取前面登录的用户名

            //参数输入
            SimpleDateFormat format1 = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
            String nowTime = format1.format(new Date());
            emrFaultDetail.setUpdater(username);
            emrFaultDetail.setUpdateTime(nowTime);
            //修改复审内容
            //1、查询出该病案的创建时间最近的缺陷记录
            //Emr_Fault_Detail entity= emrFaultDetailService.selectByArchiveDetailId(emrFaultDetail);
            //emrFaultDetail.setId(entity.getId());
            //2、修改缺陷记录
            bol = emrFaultDetailService.updateCloByPrimaryKey(emrFaultDetail);
        }

        return bol;
    }

    @ResponseBody
    @RequestMapping(value = "/updateStateByArchivId")
    public int updateStateByArchivId(HttpServletRequest request, HttpServletResponse response, Emr_Fault_Detail emrFaultDetail) {
        response.setCharacterEncoding("utf-8");
        //修改病案归档状态
        Archive_Master archiveMaster = new Archive_Master();
        archiveMaster.setId(emrFaultDetail.getArchiveDetailId());
        archiveMaster.setArchivestate("已归档");
        int bol =0;
//            int bol = archiveMasterService.updateByClo(archiveMaster);
//
//            //添加初审内容
//            if (bol == 1) {
//                // 从session获取用户名
//                Subject currentUser = SecurityUtils.getSubject();
//                Session session = currentUser.getSession();
//                String username = (String) session.getAttribute("userSession");//获取前面登录的用户名
//
//                SimpleDateFormat format1 = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
//                String nowTime = format1.format(new Date());
//
//                //参数输入
//                emrFaultDetail.setCreater(username);
//                emrFaultDetail.setCreateTime(nowTime);
//                emrFaultDetail.setState("未召回");
//                bol = emrFaultDetailService.insertSel(emrFaultDetail);
//            }

        return bol;
    }

    @ResponseBody
    @RequestMapping(value = "/bbSelPage")
    public OffsetLimitPage bbSelPage(HttpServletRequest request, HttpServletResponse response, Archive_Master_Vo archiveMasterVo, Integer offset, Integer limit) {
        OffsetLimitPage result = archiveMasterService.bbSel(archiveMasterVo, offset, limit);
        return result;
    }

    //当前科室归档情况报表
    @ResponseBody
    @RequestMapping(value = "/exportBbSelExcel")
    public void exportBbSelExcel(HttpServletResponse response, Archive_Master_Vo archiveMasterVo) throws Exception {
        response.setContentType("application/vnd.ms-excel;charset=UTF-8");
        String[] header = {"科室编码","科室名称","出院人数","已初审病历数","未初审病历数","超期未初审病历数"};
        String[] fileNames = {"dept_name","name","checkDoctor","lockinfo","inpNo","visitId"};
        //文件名
        String fileName = "当前科室归档情况报表" + new SimpleDateFormat("yyyyMMddHHmmss").format(new Date()) + ".xls";
        //构造excel的数据
        List<Archive_Master> list = archiveMasterService.bbSel(archiveMasterVo);
        ThreadExcelUtils utils = new ThreadExcelUtils(fileName, "", header, fileNames);
        String result=utils.exportExcelToFilePath(response,utils.listConvert(list));
    }


    @ResponseBody
    @RequestMapping(value = "/bbSelPage2")
    public OffsetLimitPage bbSelPage2(HttpServletRequest request, HttpServletResponse response, Archive_Master_Vo archiveMasterVo, Integer offset, Integer limit) {
        OffsetLimitPage result = archiveMasterService.bbSel2(archiveMasterVo, offset, limit);
        return result;
    }


    @ResponseBody
    @RequestMapping(value = "/exportBbSel2Excel")
    public void exportBbSel2Excel(HttpServletResponse response, Archive_Master_Vo archiveMasterVo) throws Exception {
        response.setContentType("application/vnd.ms-excel;charset=UTF-8");
        String[] header = {"病历清单id","住院号","住院次数","名字","科室编码","科室名称","主管医生","状态","入院日期","出院日期","超期天数"};
        String[] fileNames = {"id","inpNo","visitId","name","deptName","doctorName","doctorInCharge","archivestate","admissionDateTime","dischargeDateTime","nurseName"};
        //文件名
        String fileName = "当前科室超期病历详情" + new SimpleDateFormat("yyyyMMddHHmmss").format(new Date()) + ".xls";
        //构造excel的数据
        List<Archive_Master> list = archiveMasterService.bbSel2(archiveMasterVo);
        ThreadExcelUtils utils = new ThreadExcelUtils(fileName, "", header, fileNames);
        String result=utils.exportExcelToFilePath(response,utils.listConvert(list));
    }


    @RequestMapping("getImg2")
    public void getImg2(HttpServletRequest request, HttpServletResponse response, String path) throws IOException {
        FileInputStream fis = null;
        OutputStream os = null;
        try {
            fis = new FileInputStream(path);
            os = response.getOutputStream();
            int count = 0;
            byte[] buffer = new byte[1024 * 8];
            while ((count = fis.read(buffer)) != -1) {
                os.write(buffer, 0, count);
                os.flush();
            }
        } catch (Exception e) {
            e.printStackTrace();
        } finally {
            try {
                fis.close();
                os.close();
            } catch (IOException e) {
                e.printStackTrace();
            }
        }
    }




}
 

