package com.emr.controller;

import com.emr.entity.*;
import com.emr.service.Archive_MasterService;
import com.emr.service.Archive_Master_FollowingService;
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("/medicalHandle")
public class MedicalHandleController {
    @Autowired
    private Archive_MasterService archiveMasterService;
    @Autowired
    private Archive_Master_FollowingService archive_master_followingService;

    @RequestMapping(value = "/medicalHandles")
    public String faults(Model model) {
        return "medicalHandlerDir/medicalHandlerList";
    }

    @ResponseBody
    @RequestMapping(value = "/medicalHandlesList")
    public OffsetLimitPage medicalHandlesList(HttpServletRequest request, HttpServletResponse response, Archive_Master_Vo archiveMasterVo, Integer offset, Integer limit) {
        OffsetLimitPage result = archiveMasterService.selectHandleDetail(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","deptAdmissionTo","admissionDateTime","deptName","dischargeDateTime","handleName","handleTime","followingContent"};
        //文件名
        String fileName = "病案室处理列表" + new SimpleDateFormat("yyyyMMddHHmmss").format(new Date()) + ".xls";
        //构造excel的数据
        List<Archive_Master_Vo> list = archiveMasterService.selectHandleDetail(archiveMasterVo);
        ThreadExcelUtils utils = new ThreadExcelUtils(fileName, "", header, fileNames);
        String result=utils.exportExcelToFilePath(response,utils.listConvert(list));
    }

    @ResponseBody
    @RequestMapping(value = "/getTimeLine")
    public List<Archive_Master_Following> getTimeLine(HttpServletRequest request,String masterId){
        Power_User user = (Power_User) request.getSession().getAttribute("CURRENT_USER");
        Integer roleId = user.getRoleId();
        return archive_master_followingService.selectByMasterId(roleId,masterId);
    }
}
