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.ComputeClinicalPathwayCostMapper;
import com.kcim.dao.model.ComputeClinicalPathwayCost;
import com.kcim.dao.model.dto.ComputeClinicalPathwayCostPageDto;
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 18:19
 **/
@Repository
public class ComputeClinicalPathwayCostRepository extends ServiceImpl<ComputeClinicalPathwayCostMapper, ComputeClinicalPathwayCost> {
    public List<Integer> removeByComputeDate(String computeDate) {
        LambdaQueryWrapper<ComputeClinicalPathwayCost> queryWrapper = new LambdaQueryWrapper<>();
        queryWrapper.eq(ComputeClinicalPathwayCost::getHospId, UserContext.getHospId());
        queryWrapper.eq(ComputeClinicalPathwayCost::getComputeDate,computeDate);
        List<ComputeClinicalPathwayCost> list = this.list(queryWrapper);
        if(!CollectionUtils.isEmpty(list)){
            List<Integer> collect = list.stream().map(ComputeClinicalPathwayCost::getId).collect(Collectors.toList());
            this.removeBatchByIds(collect,500);
            return collect;
        }
        return new ArrayList<>();
    }

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