package com.kcim.dao.repository;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.kcim.common.util.UserContext;
import com.kcim.dao.mapper.ComputeDiseaseCostMapper;
import com.kcim.dao.model.ComputeDiseaseCost;
import com.kcim.dao.model.dto.ComputeDiseaseCostPageDto;
import org.springframework.stereotype.Repository;
import org.springframework.util.CollectionUtils;

import java.util.ArrayList;
import java.util.List;
import java.util.stream.Collectors;

/**
 * @program: CostAccount
 * @description: 病种分组计算数据库交互
 * @author: Wang.YS
 * @create: 2023-10-31 10:53
 **/
@Repository
public class ComputeDiseaseCostRepository extends ServiceImpl<ComputeDiseaseCostMapper, ComputeDiseaseCost> {

    public List<Integer> removeByComputeDate(String computeDate) {
        LambdaQueryWrapper<ComputeDiseaseCost> queryWrapper = new LambdaQueryWrapper<>();
        queryWrapper.eq(ComputeDiseaseCost::getHospId, UserContext.getHospId());
        queryWrapper.eq(ComputeDiseaseCost::getComputeDate,computeDate);
        List<ComputeDiseaseCost> list = this.list(queryWrapper);
        if(!CollectionUtils.isEmpty(list)){
            List<Integer> collect = list.stream().map(ComputeDiseaseCost::getId).collect(Collectors.toList());
            this.removeBatchByIds(collect,500);
            return collect;
        }
        return new ArrayList<>();
    }


    public Page<ComputeDiseaseCostPageDto> getByPage(Integer current, Integer pageSize, String computeDate, String departmentName, String diseaseName) {
        Page<ComputeDiseaseCostPageDto> page = new Page<>(current,pageSize);
        List<ComputeDiseaseCostPageDto> byPage = this.baseMapper.getByPage(page, computeDate, departmentName, diseaseName, UserContext.getHospId());
        if(!CollectionUtils.isEmpty(byPage)){
            page.setRecords(byPage);
        }
        return page;
    }
}
