package com.kcim.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.kcim.vo.CodeAndNameVO;
import com.kcim.dao.mapper.AllocationQueryMapper;
import com.kcim.service.AllocationQueryService;
import org.springframework.stereotype.Service;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;

import com.kcim.dao.model.AllocationQuery;

import java.math.BigDecimal;
import java.util.List;
import java.util.stream.Collectors;


@Service("allocationQueryService")
public class AllocationQueryServiceImpl extends ServiceImpl<AllocationQueryMapper, AllocationQuery> implements AllocationQueryService {


    /**
     * 这个月总金额
     *
     * @param dateYear
     * @param month
     * @param hospId
     * @return
     */
    @Override
    public BigDecimal getTotalMoney(int dateYear, int month, Long hospId) {
        return baseMapper.getTotalMoney(dateYear, month, hospId);
    }

    @Override
    public List<CodeAndNameVO> getRespCodeAndName(Long hospId, int dateYear, int month) {
        List<CodeAndNameVO> respCodeAndName = baseMapper.getRespCodeAndName(hospId, dateYear, month);
        return respCodeAndName.stream().distinct().collect(Collectors.toList());
    }

    @Override
    public List<CodeAndNameVO> getAccountCodeAndName(Long hospId, int dateYear, int month) {
        List<CodeAndNameVO> respCodeAndName = baseMapper.getAccountCodeAndName(hospId, dateYear, month);
        return respCodeAndName.stream().distinct().collect(Collectors.toList());
    }

    @Override
    public BigDecimal getCountByRespAndAccounts(Long hospId, int dateYear, int month, String code, List<String> accountCodes) {
        return baseMapper.getCountByRespAndAccounts(hospId, dateYear, month, code, accountCodes);
    }

    @Override
    public BigDecimal getTotalByAccountAndResps(Long hospId, int dateYear, int month, String code, List<String> respCodes) {
        return baseMapper.getTotalByAccountAndResps(hospId, dateYear, month, code, respCodes);
    }

    @Override
    public BigDecimal getTotalByAccountAndRespCode(Long hospId, int dateYear, int month, String accountCode, String responsibilityCode) {
        return baseMapper.getTotalByAccountAndRespCode(hospId, dateYear, month, accountCode, responsibilityCode);
    }

    /**
     * 获取这个月的分摊数据
     *
     * @param hospId 医院id
     * @param year   年
     * @param month  月
     * @return 分摊数据查询
     */
    @Override
    public List<AllocationQuery> getAllByDate(Long hospId, int year, int month) {
        List<AllocationQuery> list = this.list(
                new LambdaQueryWrapper<AllocationQuery>()
                        .eq(AllocationQuery::getDateYear, year)
                        .eq(AllocationQuery::getDateMonth, month)
                        .eq(AllocationQuery::getHospId, hospId)
        );
        return list;
    }

    @Override
    public List<AllocationQuery> getByDate(int year, int month, Long hospId, List<Long> levelSorts) {
        return this.list(
                new LambdaQueryWrapper<AllocationQuery>()
                        .eq(AllocationQuery::getDateYear, year)
                        .eq(AllocationQuery::getDateMonth, month)
                        .eq(AllocationQuery::getHospId, hospId)
                        .in(AllocationQuery::getShareLevelId, levelSorts)
        );
    }

    @Override
    public List<AllocationQuery> getByDateAndResp(int year, int month, Long hospId, String responsibilityCode) {
        return this.list(
                new LambdaQueryWrapper<AllocationQuery>()
                        .eq(AllocationQuery::getDateYear, year)
                        .eq(AllocationQuery::getDateMonth, month)
                        .eq(AllocationQuery::getHospId, hospId)
                        .eq(AllocationQuery::getTargetResponsibilityCode, responsibilityCode)
        );
    }

    @Override
    public List<AllocationQuery> getByDateRespn(int year, int month, Long hospId, List<Long> shareLevelId, String responsibilityCode) {
        return this.list(
                new LambdaQueryWrapper<AllocationQuery>()
                        .eq(AllocationQuery::getDateYear, year)
                        .eq(AllocationQuery::getDateMonth, month)
                        .eq(AllocationQuery::getHospId, hospId)
                        .eq(AllocationQuery::getTargetResponsibilityCode, responsibilityCode)
                        .in(AllocationQuery::getShareLevelId, shareLevelId)
        );
    }
}