package com.diagbot.aggregate;

import com.diagbot.dto.NumDTO;
import com.diagbot.dto.QcResultPercentDTO;
import com.diagbot.facade.BehospitalInfoFacade;
import com.diagbot.facade.QcresultInfoFacade;
import com.diagbot.util.BeanUtil;
import com.diagbot.util.ListUtil;
import com.diagbot.vo.FilterVO;
import com.diagbot.vo.QcresultFilterVO;
import com.google.common.collect.Lists;
import io.github.lvyahui8.spring.annotation.DataConsumer;
import io.github.lvyahui8.spring.annotation.DataProvider;
import io.github.lvyahui8.spring.annotation.InvokeParameter;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Component;

import java.math.BigDecimal;
import java.math.RoundingMode;
import java.text.DecimalFormat;
import java.util.LinkedHashMap;
import java.util.List;
import java.util.Map;
import java.util.stream.Collectors;

/**
 * @Description:病历相关统计
 * @Author:zhaops
 * @time: 2020/5/6 17:21
 */
@Component
public class MrStatisticsAggregate {

    @Autowired
    private BehospitalInfoFacade behospitalInfoFacade;
    @Autowired
    private QcresultInfoFacade qcresultInfoFacade;

    @DataProvider("setAllMr")
    public Map<String, Object> setAllDept(
            @InvokeParameter("filterVO") FilterVO filterVO,
            @DataConsumer("getLevelResultDept") List<QcResultPercentDTO> levelResults,
            @DataConsumer("leaveHosCount") Map<String, Object> leaveHosMap,
            @DataConsumer("mrCount") List<NumDTO> mrCountList) {
        Map<String, Object> retMap = new LinkedHashMap<>();
        retMap.put("各科室甲级病历占比", Lists.newLinkedList());
        retMap.put("病历数统计", Lists.newLinkedList());
        retMap.put("出院人数统计", new LinkedHashMap<>());
        if (ListUtil.isNotEmpty(levelResults)) {
            retMap.put("各科室甲级病历占比", levelResults);
        }
        if (leaveHosMap != null) {
            retMap.put("出院人数统计", leaveHosMap);
        }
        if (ListUtil.isNotEmpty(mrCountList)) {
            retMap.put("病历数统计", mrCountList);
        }
        return retMap;
    }

    /**
     * 各科室甲级病历占比
     *
     * @param filterVO
     * @return
     */
    @DataProvider("getLevelResultDept")
    public List<QcResultPercentDTO> getLevelResultDept(@InvokeParameter("filterVO") FilterVO filterVO) {
        List<QcResultPercentDTO> qcResultPercentList = qcresultInfoFacade.levelPercentGroupByDept(filterVO);
        Integer limitCount = filterVO.getLimitCount();
        qcResultPercentList = qcResultPercentList
                .stream()
                .limit(limitCount)
                .collect(Collectors.toList());
        return qcResultPercentList;
    }

    /**
     * 出院人数统计
     *
     * @param filterVO
     * @return
     */
    @DataProvider("leaveHosCount")
    public Map<String, Object> leaveHosCount(@InvokeParameter("filterVO") FilterVO filterVO) {
        Map<String, Object> retMap = new LinkedHashMap<>();
        retMap.put("总人数", 0);
        retMap.put("死亡人数", 0);
        retMap.put("新生儿人数", 0);
        retMap.put("手术病人数", 0);
        retMap.put("非医嘱离院病人数", 0);
        retMap.put("31日再入院病人数", 0);
        Integer totleNum = behospitalInfoFacade.leaveHosCount(filterVO);
        Integer deathNum = behospitalInfoFacade.deathCount(filterVO);
        Integer newBornNum = behospitalInfoFacade.newBornCount(filterVO);
        Integer operationNum = behospitalInfoFacade.operationCount(filterVO);
        Integer nonAdviceNum = behospitalInfoFacade.nonAdviceCount(filterVO);
        Integer reBehospitalNum = behospitalInfoFacade.get31DaysBehospitalCount(filterVO);
        retMap.put("总人数", totleNum);
        retMap.put("死亡人数", deathNum);
        retMap.put("新生儿人数", newBornNum);
        retMap.put("手术病人数", operationNum);
        retMap.put("非医嘱离院病人数", nonAdviceNum);
        retMap.put("31日再入院病人数", reBehospitalNum);
        return retMap;
    }

    /**
     * 质控病历统计
     *
     * @return
     */
    @DataProvider("mrCount")
    public List<NumDTO> mrCount(@InvokeParameter("filterVO") FilterVO filterVO) {
        DecimalFormat df = new DecimalFormat("#0.00");
        List<NumDTO> retList = Lists.newLinkedList();
        QcresultFilterVO qcresultFilterVO = new QcresultFilterVO();
        BeanUtil.copyProperties(filterVO, qcresultFilterVO);

        int totleNum = qcresultInfoFacade.resultCount(qcresultFilterVO);
        if (totleNum == 0) {
            return retList;
        }
        qcresultFilterVO.setLevel("甲");
        int firstLevelNum = qcresultInfoFacade.resultCount(qcresultFilterVO);
        qcresultFilterVO.setLevel("乙");
        int secondLevelNum = qcresultInfoFacade.resultCount(qcresultFilterVO);
        qcresultFilterVO.setLevel("丙");
        int thirdLevelNum = qcresultInfoFacade.resultCount(qcresultFilterVO);
        NumDTO totleNumDTO = new NumDTO();
        totleNumDTO.setName("累计质控病历数");
        totleNumDTO.setTotleNum(totleNum);
        totleNumDTO.setNum(totleNum);
        NumDTO firstLevelNumDTO = new NumDTO();
        firstLevelNumDTO.setName("甲级病历");
        firstLevelNumDTO.setNum(firstLevelNum);
        firstLevelNumDTO.setTotleNum(totleNum);
        Double firstPercent = BigDecimal.valueOf(firstLevelNum)
                .divide(BigDecimal.valueOf(totleNum), 4, RoundingMode.HALF_UP)
                .doubleValue();
        String firstPercentStr = df.format(BigDecimal.valueOf(firstPercent).multiply(BigDecimal.valueOf(100))) + "%";
        firstLevelNumDTO.setPercent(firstPercent);
        firstLevelNumDTO.setPercentStr(firstPercentStr);
        NumDTO secondLevelNumDTO = new NumDTO();
        secondLevelNumDTO.setName("乙级病历");
        secondLevelNumDTO.setNum(secondLevelNum);
        secondLevelNumDTO.setTotleNum(totleNum);
        Double secondPercent = BigDecimal.valueOf(secondLevelNum)
                .divide(BigDecimal.valueOf(totleNum), 4, RoundingMode.HALF_UP)
                .doubleValue();
        String secondPercentStr = df.format(BigDecimal.valueOf(secondPercent).multiply(BigDecimal.valueOf(100))) + "%";
        secondLevelNumDTO.setPercent(secondPercent);
        secondLevelNumDTO.setPercentStr(secondPercentStr);
        NumDTO thirdLevelNumDTO = new NumDTO();
        thirdLevelNumDTO.setName("丙级病历");
        thirdLevelNumDTO.setNum(thirdLevelNum);
        thirdLevelNumDTO.setTotleNum(totleNum);
        Double thirdPercent = BigDecimal.valueOf(thirdLevelNum)
                .divide(BigDecimal.valueOf(totleNum), 4, RoundingMode.HALF_UP)
                .doubleValue();
        String thirdPercentStr = df.format(BigDecimal.valueOf(thirdPercent).multiply(BigDecimal.valueOf(100))) + "%";
        thirdLevelNumDTO.setPercent(thirdPercent);
        thirdLevelNumDTO.setPercentStr(thirdPercentStr);
        retList.add(totleNumDTO);
        retList.add(firstLevelNumDTO);
        retList.add(secondLevelNumDTO);
        retList.add(thirdLevelNumDTO);
        return retList;
    }
}