package com.emr.controller;

import com.alibaba.fastjson.JSONObject;
import com.emr.dao.MedicalRecordHomepageCacheMapper;
import com.emr.entity.Archive_Master_Vo;
import com.emr.entity.MedicalRecordHomepageCache;
import com.emr.entity.MedicalRecordHomepageCacheExample;
import com.emr.entity.OffsetLimitPage;
import com.emr.service.Archive_MasterService;
import com.emr.util.ThreadExcelUtils;
import org.apache.commons.lang3.StringUtils;
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("/medicalOverdue")
public class medicalOverdueController {
    @Autowired
    private Archive_MasterService archiveMasterService;

    @Autowired
    private MedicalRecordHomepageCacheMapper medicalRecordHomepageCacheMapper;

    @RequestMapping("/medicalOverdues")
    public String medicalOverdues(Model model){
        return "medicalOverdueDir/medicalOverdueList";
    }

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

    /*@ResponseBody
    @RequestMapping(value = "/test")
    public String test(Archive_Master_Vo archiveMasterVo) {
        List<Archive_Master_Vo> list = archiveMasterService.selectOverdue(archiveMasterVo);
        System.out.println(list.toString());
        return list.toString();
    }*/

    @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 = {"inpNo","visitId","name","deptAdmissionTo","deptName","dischargeDateTime","createTime","firstInstance","doctorInCharge","overdue","score"};
        //文件名
        String fileName = "首次签收超期列表" + new SimpleDateFormat("yyyyMMddHHmmss").format(new Date()) + ".xls";
        //构造excel的数据overdueDays
        List<Archive_Master_Vo> list = archiveMasterService.selectOverdue(archiveMasterVo);

        List<MedicalRecordHomepageCache> medicalRecordHomepageCacheList = null;

        if(archiveMasterVo.getOverdueDays()!=null && archiveMasterVo.getOverdueDays()!="") {
            if (list != null && list.size() > 0) {
                for (int i = 0; i < list.size(); i++) {
                    if (list.get(i).getOverdue() != null && list.get(i).getOverdue() != "" ) {
                        int score = Integer.parseInt(list.get(i).getOverdue()) - Integer.parseInt(archiveMasterVo.getOverdueDays());
                        list.get(i).setScore("" + score);
                    }
                    //2021-09-17
                    String doctorInChargeInHomePage = "";

                    //根据masterId查出该条首页缓存表中content内容
                    MedicalRecordHomepageCacheExample medicalRecordHomepageCacheExample = new MedicalRecordHomepageCacheExample();
                    MedicalRecordHomepageCacheExample.Criteria criteria = medicalRecordHomepageCacheExample.createCriteria();
                    if (!StringUtils.isEmpty(list.get(i).getId())){
                        criteria.andMasterIdEqualTo(list.get(i).getId());
                    }
                    medicalRecordHomepageCacheList = medicalRecordHomepageCacheMapper.selectByExample(medicalRecordHomepageCacheExample);

                    if (medicalRecordHomepageCacheList.size()>0){
                        //查出content
                        String content = medicalRecordHomepageCacheList.get(0).getContent();
                        //根据content内容获取首页中主治医生name
                        doctorInChargeInHomePage = JSONObject.parseObject(content).getJSONObject("commonTable").getString("yZY054");
                    }
                    //重新赋值主管医生
                    if (doctorInChargeInHomePage != null && !doctorInChargeInHomePage.equals("")){
                        list.get(i).setDoctorInCharge(doctorInChargeInHomePage);
                    }else {
                        list.get(i).setDoctorInCharge("首页尚未填写主治医生！");
                    }
                    //2021-09-17
                }
            }
        }
        ThreadExcelUtils utils = new ThreadExcelUtils(fileName, "", header, fileNames);
        String result=utils.exportExcelToFilePath(response,utils.listConvert(list));
    }
}
