package com.emr.controller.otherManage;

import com.emr.annotation.OptionalLog;
import com.emr.dao.CommomMapper;
import com.emr.dao.approve.Emr_Apply_ApproveMapper;
import com.emr.dao.commomSearch.ScanPathMapper;
import com.emr.dao.emrPrintAndDownLoad.EmrPrintDownloadInfoMapper;
import com.emr.dao.recordLock.Emr_LockMapper;
import com.emr.entity.approve.Emr_Apply_Approve;
import com.emr.util.ExceptionPrintUtil;
import com.emr.vo.EmrPrintOrDownLoadInfo.EmrPrintOrDownLoadInfoVo;
import org.apache.shiro.authz.annotation.RequiresPermissions;
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 java.text.ParseException;
import java.text.SimpleDateFormat;
import java.util.Date;
import java.util.List;

/**
 * @program: emr_record
 * @description
 * @author: wangsiyi
 * @create: 2020-04-23 16:31
 **/
@Controller
@RequestMapping("monitoringCenter/")
public class monitoringCenterController {
    @Autowired
    private Emr_Apply_ApproveMapper applyApproveMapper;
    @Autowired
    private EmrPrintDownloadInfoMapper emrPrintDownloadInfoMapper;
    @Autowired
    private CommomMapper commomMapper;
    @Autowired
    private Emr_LockMapper emrLockMapper;
    @Autowired
    private ScanPathMapper scanPathMapper;
    /**
     * 进入监控中心页面
     * @return
     */
    @OptionalLog(module = "查看",methods="监控中心页面")
    @RequiresPermissions("/monitoringCenter/select")
    @RequestMapping("monitoringCenterList")
    public String monitoringCenter(Model model){
        //正在调阅的数量
        int applyApproveNum = 0;
        //待批病案数
        int applyApproveNullNum = 0;
        EmrPrintOrDownLoadInfoVo vo = new EmrPrintOrDownLoadInfoVo();
        vo.setOperType(Short.valueOf("2"));
        List<EmrPrintOrDownLoadInfoVo> emrPrintOrDownLoadInfoVos = null;
        //数字病案库存数量
        int commomNum = 0;
        //病案锁定数
        int lockNum = 0;
        //病案总盘数
        int phCount = 0;
        //病案图像总数
        int pdfCount = 0;
        try {
            //下载量
            emrPrintOrDownLoadInfoVos = emrPrintDownloadInfoMapper.selectAll(vo);
            commomNum = commomMapper.selectCommomCount();
            lockNum = emrLockMapper.selectCountLocking();
            SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd");
            String nowDate = sdf.format(new Date());
            List<Emr_Apply_Approve> apply_approves = applyApproveMapper.selectCountByApproveState();
            for (Emr_Apply_Approve approve : apply_approves){
                try {
                    if (approve.getEffeTime().compareTo(nowDate) > 0){
                        applyApproveNum += 1;
                    }
                } catch (Exception e) {
                    e.printStackTrace();
                }
            }
            List<Emr_Apply_Approve> applyApproves = applyApproveMapper.selectCountByApproveStateIsNull();
            for (Emr_Apply_Approve approve : applyApproves){
                if (approve.getEffeTime().compareTo(nowDate) > 0){
                    applyApproveNullNum += 1;
                }
            }
            //查询总盘数
            phCount = scanPathMapper.selectCountPh();
            //查询图像总数
            pdfCount = scanPathMapper.selectCountPdf();
        } catch (Exception e) {
            ExceptionPrintUtil.printException(e);
            e.printStackTrace();
        }
        model.addAttribute("applyApproveNum", applyApproveNum);
        model.addAttribute("applyApproveNullNum", applyApproveNullNum);
        model.addAttribute("emrDownloadInfoNum", emrPrintOrDownLoadInfoVos.size());
        model.addAttribute("commomNum", commomNum);
        model.addAttribute("lockNum", lockNum);
        model.addAttribute("phCount", phCount);
        model.addAttribute("pdfCount", pdfCount);
        return "otherManage/monitoringCenter";
    }
}
