package thyyxxk.webserver.service.reports;

import com.alibaba.fastjson.JSON;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import thyyxxk.webserver.config.exception.ExceptionEnum;
import thyyxxk.webserver.dao.his.reports.HuiZhenTongJiDao;
import thyyxxk.webserver.entity.ResultVo;
import thyyxxk.webserver.entity.casefrontsheet.JieShouHuiZhenPojo;
import thyyxxk.webserver.entity.datamodify.GetDropdownBox;
import thyyxxk.webserver.utils.DateUtil;
import thyyxxk.webserver.utils.ExcelUtil;
import thyyxxk.webserver.utils.ResultVoUtil;
import thyyxxk.webserver.utils.StringUtil;

import javax.servlet.http.HttpServletResponse;
import java.util.List;

/**
 * <p>
 * 描述： 会诊统计
 * </p>
 *
 * @author xc
 * @date 2021-07-30 10:03
 */
@Service
@Slf4j
public class HuiZhenTongJiService {

    private final HuiZhenTongJiDao dao;

    @Autowired
    public HuiZhenTongJiService(HuiZhenTongJiDao dao) {
        this.dao = dao;
    }

    /**
     * @return 获取科室
     */
    public ResultVo<List<GetDropdownBox>> getDept() {
        return ResultVoUtil.success(dao.getDept());
    }

    /**
     * 分装 page 分页 用来做 导出excel
     *
     * @param param 查询条件
     * @return 返回数据
     */
    public IPage<JieShouHuiZhenPojo> getHuiZhenTongJiPage(JieShouHuiZhenPojo param) {
        IPage<JieShouHuiZhenPojo> page = new Page<>(param.getCurrentPage(), param.getPageSize());
        return dao.getHuiZhenTongJi(page, param.getStartTime(), param.getEndTime(), param.getDeptCode(), param.getStatusFlag());
    }

    /**
     * 查询 会诊记录的数据
     *
     * @param param 查询条件 时间范围 和科室
     * @return 返回
     */
    public ResultVo<IPage<JieShouHuiZhenPojo>> getHuiZhenData(JieShouHuiZhenPojo param) {
        if (StringUtil.isBlank(param.getStartTime()) && StringUtil.isBlank(param.getEndTime())) {
            return ResultVoUtil.fail(ExceptionEnum.NULL_POINTER, "时间范围不能为空");
        }
        IPage<JieShouHuiZhenPojo> page = getHuiZhenTongJiPage(param);
        return ResultVoUtil.success(page);
    }

    /**
     * 导出会诊信息
     *
     * @param response 返回文件流
     * @param param    查询的条件
     */
    public void excel(HttpServletResponse response, JieShouHuiZhenPojo param) {
        log.info("导出会诊统计excel --> {}", JSON.toJSONString(param));
        List<JieShouHuiZhenPojo> list = getHuiZhenTongJiPage(param).getRecords();
        String[] title = {"住院号", "住院次数", "申请人", "申请科室", "申请次数", "申请会诊日期", "会诊科室", "会诊医生", "会诊状态"};
        String[][] content = new String[list.size()][];
        for (int i = 0; i < list.size(); i++) {
            content[i] = new String[title.length];
            JieShouHuiZhenPojo pojo = list.get(i);
            content[i][0] = pojo.getInpatientNo();
            content[i][1] = pojo.getAdmissTimes().toString();
            content[i][2] = pojo.getInputName();
            content[i][3] = pojo.getDeptCode();
            content[i][4] = pojo.getReqTimes().toString();
            content[i][5] = DateUtil.formatDatetime(pojo.getReqDate(), DateUtil.DEFAULT_PATTERN);
            content[i][6] = pojo.getReqDept1();
            content[i][7] = pojo.getHzDoctor();
            content[i][8] = pojo.getStatusFlag() == 1 ? "未会诊" : "已会诊";
        }
        //传三个参数 一个是 固定的 response ，excel的头部信息，excel的内容
        ExcelUtil.exportExcel(response, title, content);
    }
}
