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.ComputeItemCostMapper;
import com.kcim.dao.model.ComputeItemCost;
import com.kcim.dao.model.dto.ComputeItemCostPageDto;
import com.kcim.dao.model.dto.ComputeItemGroupByOrder;
import com.kcim.dao.model.dto.ComputeItemGroupByVisitNoDto;
import com.kcim.vo.SessionUserVO;
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-25 14:37
 **/
@Repository
public class ComputeItemCostRepository extends ServiceImpl<ComputeItemCostMapper, ComputeItemCost> {
    public List<Integer> removeByComputeDate(String computeDate, SessionUserVO currentUser) {
        LambdaQueryWrapper<ComputeItemCost> queryWrapper = new LambdaQueryWrapper<>();
        queryWrapper.eq(ComputeItemCost::getHospId, currentUser.getHospId());
        queryWrapper.eq(ComputeItemCost::getComputeDate,computeDate);
        List<ComputeItemCost> list = this.list(queryWrapper);
        if(!CollectionUtils.isEmpty(list)){
            List<Integer> collect = list.stream().map(ComputeItemCost::getId).collect(Collectors.toList());
            this.removeBatchByIds(collect,500);
            return collect;
        }
        return new ArrayList<>();
    }

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

    public List<ComputeItemGroupByVisitNoDto> getByComputeDateGroupByVisitNo(String computeDate){
        return this.baseMapper.getListGroupByVisitNo(computeDate,UserContext.getHospId());

    }
    public List<ComputeItemGroupByOrder> getByComputeDateGroupByOrder(String computeDate){
        return this.baseMapper.getListGroupByOrder(computeDate,UserContext.getHospId());

    }
}
