package com.kcim.dao.repository;

import com.baomidou.mybatisplus.core.conditions.update.UpdateWrapper;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.kcim.common.util.UserContext;
import com.kcim.dao.mapper.OrderCostDetailMapper;
import com.kcim.dao.model.OrderCostDetail;
import org.springframework.stereotype.Repository;

import java.util.ArrayList;
import java.util.List;
import java.util.stream.Collectors;
import java.util.stream.Stream;

/**
 * @program: CostAccount
 * @description:
 * @author: Wang.YS
 * @create: 2023-11-22 11:11
 **/
@Repository
public class OrderCostDetailRepository extends ServiceImpl<OrderCostDetailMapper, OrderCostDetail> {
    public void removeByOrderCostId(List<Integer> integers) {
//        LambdaQueryWrapper<OrderCostDetail> queryWrapper = new LambdaQueryWrapper<>();
//        queryWrapper.eq(OrderCostDetail::getHospId, UserContext.getHospId());
//        queryWrapper.in(OrderCostDetail::getOrderCostId,integers);
//        List<OrderCostDetail> list = this.list(queryWrapper);
//        if(!CollectionUtils.isEmpty(list)){
//            List<Integer> collect = list.stream().map(OrderCostDetail::getId).collect(Collectors.toList());
//            this.removeBatchByIds(collect,500);
//        }

        int limit = countStep(integers.size());
        //方法一：使用流遍历操作
        List<List<Integer>> mgList = new ArrayList<>();
        Stream.iterate(0, n -> n + 1).limit(limit).forEach(i -> {
            mgList.add(integers.stream().skip(i * MAX_NUMBER).limit(MAX_NUMBER).collect(Collectors.toList()));
        });
        for (List<Integer> mg : mgList) {
            UpdateWrapper<OrderCostDetail> updateWrapper = new UpdateWrapper<>();
            updateWrapper.lambda().eq(OrderCostDetail::getDelFlag,0)
                    .eq(OrderCostDetail::getHospId,UserContext.getHospId())
                    .in(OrderCostDetail::getOrderCostId,mg)
                    .set(OrderCostDetail::getDelFlag,1);
//                updateWrapper.set("del_flag",1);
//                updateWrapper.eq("hosp_id", currentUser.getHospId());
//                updateWrapper.eq("del_flag", 0);
//                updateWrapper.in("item_cost_id", integers);
            this.update(updateWrapper);
        }

    }

    static  Integer MAX_NUMBER = 500;

    private static Integer countStep(Integer size) {
        return (size + MAX_NUMBER - 1) / MAX_NUMBER;
    }
}
