package com.diagbot.service.impl;

import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.diagbot.dto.*;
import com.diagbot.entity.QcresultInfo;
import com.diagbot.mapper.QcresultInfoMapper;
import com.diagbot.service.QcresultInfoService;
import com.diagbot.vo.*;
import org.apache.commons.collections4.CollectionUtils;
import org.apache.ibatis.annotations.Param;
import org.springframework.stereotype.Service;

import java.text.DecimalFormat;
import java.util.*;

/**
 * <p>
 * 质控评分结果信息
每次评分增加一条信息，前面所有评分is_deleted全部设置为Y 服务实现类
 * </p>
 *
 * @author zhoutg
 * @since 2020-04-13
 */
@Service
public class QcresultInfoServiceImpl extends ServiceImpl<QcresultInfoMapper, QcresultInfo> implements QcresultInfoService {

    /**
     * 筛选质控记录
     *
     * @param qcresultFilterVO
     * @return
     */
    @Override
    public int getQcresultSelectively(QcresultFilterVO qcresultFilterVO) {
        return baseMapper.getQcresultSelectively(qcresultFilterVO);
    }

    /**
     * 质控病历数统计
     *
     * @param qcresultFilterVO
     * @return
     */
    @Override
    public int resultCount(QcresultFilterVO qcresultFilterVO) {
        return baseMapper.resultCount(qcresultFilterVO);
    }


    /**
     * 按科室统计各等级病历百分比
     *
     * @param filterVO
     * @return
     */
    @Override
    public List<QcResultPercentDTO> levelPercentGroupByDept(FilterVO filterVO) {
        return baseMapper.levelPercentGroupByDept(filterVO);
    }

    /**
     * 按模块统计缺陷数
     *
     * @param filterVO
     * @return
     */
    @Override
    public List<NumDTO> entryCountGroupByCase(FilterVO filterVO) {
        return baseMapper.entryCountGroupByCase(filterVO);
    }

    /**
     * 条目缺陷分组统计
     *
     * @param filterVO
     * @return
     */
    @Override
    public List<NumDTO> entryCountGroupByEntry(FilterVO filterVO) {
        return baseMapper.entryCountGroupByEntry(filterVO);
    }

    /**
     * 质控平均分按科室统计
     *
     * @param filterVO
     * @return
     */
    @Override
    public List<AverageStatisticsDTO> getAverageScore(FilterVO filterVO) {
        return baseMapper.getAverageScore(filterVO);
    }

    /**
     * 按科室统计质控病历数
     *
     * @param qcresultFilterVO
     * @return
     */
    @Override
    public List<NumDTO> resultCountByDept(QcresultFilterVO qcresultFilterVO) {
        return baseMapper.resultCountByDept(qcresultFilterVO);
    }

    @Override
    public List<Map<String, Object>> resultMrCountByDept(QcresultFilterVO qcresultFilterVO) {
        return baseMapper.resultMrCountByDept(qcresultFilterVO);
    }

    /**
     * 各模块缺陷占比-按科室
     *
     * @param filterVO
     * @return
     */
    @Override
    public List<DeptNumDTO> entryCountGroupByCaseAndDept(FilterVO filterVO) {
        return baseMapper.entryCountGroupByCaseAndDept(filterVO);
    }

    /**
     * 条目缺陷占比-按科室
     *
     * @param filterVO
     * @return
     */
    @Override
    public List<DeptNumDTO> entryCountGroupByEntryAndDept(FilterVO filterVO) {
        return baseMapper.entryCountGroupByEntryAndDept(filterVO);
    }

    /**
     * 各模块缺陷占比（分页）
     *
     * @param filterPageVO
     * @return
     */
    @Override
    public IPage<NumDTO> entryCountGroupByCasePage(@Param("filterPageVO") FilterPageVO filterPageVO) {
        return baseMapper.entryCountGroupByCasePage(filterPageVO);
    }

    /**
     * 缺陷详情（分页）
     *
     * @param filterPageVO
     * @return
     */
    @Override
    public IPage<EntryNumGroupDTO> entryCountGroupByEntryPage(@Param("filterPageVO") FilterPageVO filterPageVO) {
        return baseMapper.entryCountGroupByEntryPage(filterPageVO);
    }

    /**
     * 各科室质控平均分（分页）
     *
     * @param filterPageVO
     * @return
     */
    @Override
    public IPage<AverageStatisticsDTO> getAverageScoreByDeptPage(@Param("filterPageVO") FilterPageVO filterPageVO) {
        return baseMapper.getAverageScoreByDeptPage(filterPageVO);
    }

    /**
     * 各科室甲级病历占比排行（分页）
     *
     * @param filterPageVO
     * @return
     */
    @Override
    public IPage<QcResultPercentDTO> levelPercentGroupByDeptPage(@Param("filterPageVO") FilterPageVO filterPageVO) {
        return baseMapper.levelPercentGroupByDeptPage(filterPageVO);
    }

    /**
     * 病历等级占比
     *
     * @param filterVO
     * @return
     */
    @Override
    public List<LevelPercentDTO> levelPercentByDept(FilterVO filterVO) {
        return baseMapper.levelPercentByDept(filterVO);
    }

    /**
     * 各模块缺陷占比-科室（分页）
     *
     * @param filterPageByDeptVO
     * @return
     */
    @Override
    public IPage<NumDTO> entryCountGroupByCaseAndDeptPage(@Param("filterPageByDeptVO") FilterPageByDeptVO filterPageByDeptVO) {
        return baseMapper.entryCountGroupByCaseAndDeptPage(filterPageByDeptVO);
    }

    /**
     * 缺陷详情-科室（分页）
     *
     * @param filterPageByDeptVO
     * @return
     */
    @Override
    public IPage<EntryNumGroupDTO> entryCountGroupByEntryAndDeptPage(@Param("filterPageByDeptVO") FilterPageByDeptVO filterPageByDeptVO) {
        return baseMapper.entryCountGroupByEntryAndDeptPage(filterPageByDeptVO);
    }

    /**
     * 条目缺陷占比（内页）
     *
     * @return
     */
    @Override
    public IPage<EntryNumDTO> entryGroupByEntryInnerPage(@Param("filterPageVO") FilterPageVO filterPageVO) {
        //基础数据
        IPage<EntryNumDTO> entryNumDTOIPage = baseMapper.entryGroupByEntryInnerPage(filterPageVO);
        List<EntryNumDTO> records = entryNumDTOIPage.getRecords();
        if (CollectionUtils.isEmpty(records)) {
            return entryNumDTOIPage;
        }
        String hospitalId = filterPageVO.getHospitalId();
        //缺陷总数
        Integer sumInteget = baseMapper.entryGroupByEntrySum(filterPageVO.getHospitalId(), filterPageVO.getIsPlacefile(),
                filterPageVO.getStartDate(), filterPageVO.getEndDate());
        float nsum = 0;
        Set<Long> ids = new HashSet<Long>();
        for (EntryNumDTO record : records) {
            ids.add(record.getId());
            int num = record.getNum().intValue();
            if (sumInteget != null && sumInteget != 0) {
                int sum = sumInteget.intValue();
                nsum = (float) num / sum;
            }
            DecimalFormat df = new DecimalFormat("0.00");
            String percent = df.format(nsum * 100);
            record.setPercentStr(percent + "%");
            record.setTotleNum(sumInteget);
        }
        //缺陷分值
        String isPlacefile = filterPageVO.getIsPlacefile();
        List<EntryNumDTO> entryScore = new ArrayList<>();
        entryScore = baseMapper.entryGroupByEntryScore(hospitalId, isPlacefile, ids);
        for (EntryNumDTO record : records) {
            for (EntryNumDTO entryNumDTO : entryScore) {
                if (record.getId().equals(entryNumDTO.getId())) {
                    record.setScore(entryNumDTO.getScore());
                }
            }
        }
        return entryNumDTOIPage;
    }

    /**
     * 条目缺陷占比-科室（内页）
     *
     * @param filterPageByDeptVO
     * @return
     */
    @Override
    public IPage<DeptEntryNumDTO> entryGroupByEntryAndDeptInnerPage(@Param("filterPageByDeptVO") FilterPageByDeptVO filterPageByDeptVO) {
        return baseMapper.entryGroupByEntryAndDeptInnerPage(filterPageByDeptVO);
    }

    /**
     * 各科室质控平均分（首页）-根据内外科系统统计
     *
     * @param filterVO
     * @return
     */
    @Override
    public List<AverageStatisticsDTO> getAverageScoreByDeptClass(FilterVO filterVO) {
        return baseMapper.getAverageScoreByDeptClass(filterVO);
    }

    /**
     * 单项否决缺陷占比
     *
     * @param filterVO
     * @return
     */
    @Override
    public List<EntryNumDTO> entryRejectPercent(FilterVO filterVO) {
        return baseMapper.entryRejectPercent(filterVO);
    }

    /**
     * 各科室甲/乙/丙级病历占比
     *
     * @param filterVO
     * @return
     */
    @Override
    public List<DeptNumDTO> qcResultLevelPercent(FilterVO filterVO) {
        return baseMapper.qcResultLevelPercent(filterVO);
    }

    /**
     * 历史评分
     * @param historyAnalyzePageVO
     * @return
     */
    @Override
    public IPage<QcCasesHistoryDTO> getMedQcresultResult(HistoryAnalyzePageVO historyAnalyzePageVO){
        return baseMapper.getMedQcresultResult(historyAnalyzePageVO);
    }
    /**
     * 病历扣分大于等于5占比
     *
     * @param filterVO
     * @return
     */
    @Override
    public List<DeptNumDTO> deductGT5ByDept(FilterVO filterVO) {
        return baseMapper.deductGT5ByDept(filterVO);
    }
}
