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.OrderCostMapper;
import com.kcim.dao.model.ComputeGroupCost;
import com.kcim.dao.model.OrderCost;
import com.kcim.dao.model.dto.ComputeGroupCostPageDto;
import com.kcim.dao.model.dto.ComputeOrderCostPageDto;
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-11-22 11:10
 **/
@Repository
public class OrderCostRepository extends ServiceImpl<OrderCostMapper, OrderCost> {
    public List<Integer> removeByComputeDate(String computeDate) {
        LambdaQueryWrapper<OrderCost> queryWrapper = new LambdaQueryWrapper<>();
        queryWrapper.eq(OrderCost::getHospId, UserContext.getHospId());
        queryWrapper.eq(OrderCost::getComputeDate,computeDate);
        List<OrderCost> list = this.list(queryWrapper);
        if(!CollectionUtils.isEmpty(list)){
            List<Integer> collect = list.stream().map(OrderCost::getId).collect(Collectors.toList());
            this.removeBatchByIds(collect,500);
            return collect;
        }
        return new ArrayList<>();
    }

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