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.ComputeGroupCostMapper;
import com.kcim.dao.model.ComputeGroupCost;
import com.kcim.dao.model.dto.ComputeGroupCostPageDto;
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: DRG/DIP成本计算主表数据库交互
 * @author: Wang.YS
 * @create: 2023-10-31 16:21
 **/
@Repository
public class ComputeGroupCostRepository extends ServiceImpl<ComputeGroupCostMapper, ComputeGroupCost> {
    public List<Integer> removeByComputeDate(String computeDate) {
        LambdaQueryWrapper<ComputeGroupCost> queryWrapper = new LambdaQueryWrapper<>();
        queryWrapper.eq(ComputeGroupCost::getHospId, UserContext.getHospId());
        queryWrapper.eq(ComputeGroupCost::getComputeDate,computeDate);
        List<ComputeGroupCost> list = this.list(queryWrapper);
        if(!CollectionUtils.isEmpty(list)){
            List<Integer> collect = list.stream().map(ComputeGroupCost::getId).collect(Collectors.toList());
            this.removeBatchByIds(collect,500);
            return collect;
        }
        return new ArrayList<>();
    }


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