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

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

    public List<ComputePatientCostGroupByDepartmentDto> getPatientCostDepartmentGroup(String computeDate){
        return this.baseMapper.getListGroupByDepartment(computeDate,UserContext.getHospId());
    }
}
