package com.kcim.service.impl;

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

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


@Service("allocationQueryService")
public class AllocationQueryServiceImpl extends BaseBatchServiceImpl<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);
    }

    @Override
    public List<AllocationQuery> getAcountAccounts(Long hospId, int dateYear, int month) {
        return baseMapper.getAcountAccounts(hospId, dateYear, month);
    }

    @Override
    public List<AllocationQuery> getRespAcountAccounts(Long hospId, int dateYear, int month) {
        return baseMapper.getRespAcountAccounts(hospId, dateYear, month);
    }

    @Override
    public List<AllocationQuery> getRespOriginAcountAccounts(Long hospId, int dateYear, int month) {
        return baseMapper.getRespOriginAcountAccounts(hospId, dateYear, month);
    }

    @Override
    public void batchInsertAllocationQuery(List<AllocationQuery> allocationQueryList) {
         this.batchInsert(allocationQueryList);
    }

    /**
     * 获取这个月的分摊数据
     *
     * @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;
    }

    /**
     * 获取指定类型的成本数据
     * @param hospId
     * @param year
     * @param month
     * @param originType 来源类型:1.分摊前，2.分摊后
     * @return
     */
    @Override
    public List<AllocationQuery> getCostByDate(Long hospId, int year, int month, int originType) {
        List<AllocationQuery> list = this.list(
                new LambdaQueryWrapper<AllocationQuery>()
                        .eq(AllocationQuery::getDateYear, year)
                        .eq(AllocationQuery::getDateMonth, month)
                        .eq(AllocationQuery::getHospId, hospId)
                        .eq(AllocationQuery::getOriginType, originType)
        );
        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)
        );
    }
}