package com.kcim.dao.repository;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
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.ComputeDiseaseCostDetailMapper;
import com.kcim.dao.model.ComputeDiseaseCostDetail;
import com.kcim.dao.model.ComputePatientCostDetail;
import org.springframework.stereotype.Repository;
import org.springframework.util.CollectionUtils;

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-10-31 10:54
 **/
@Repository
public class ComputeDiseaseCostDetailRepository extends ServiceImpl<ComputeDiseaseCostDetailMapper, ComputeDiseaseCostDetail> {
    static  Integer MAX_NUMBER = 500;

    private static Integer countStep(Integer size) {
        return (size + MAX_NUMBER - 1) / MAX_NUMBER;
    }
    public void removeByDiseaseCostId(List<Integer> integers) {
//        LambdaQueryWrapper<ComputeDiseaseCostDetail> queryWrapper = new LambdaQueryWrapper<>();
//        queryWrapper.eq(ComputeDiseaseCostDetail::getHospId, UserContext.getHospId());
//        queryWrapper.in(ComputeDiseaseCostDetail::getDiseaseCostId,integers);
//        List<ComputeDiseaseCostDetail> list = this.list(queryWrapper);
//        if(!CollectionUtils.isEmpty(list)){
//            List<Integer> collect = list.stream().map(ComputeDiseaseCostDetail::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<ComputeDiseaseCostDetail> updateWrapper = new UpdateWrapper<>();
            updateWrapper.lambda().eq(ComputeDiseaseCostDetail::getDelFlag,0)
                    .eq(ComputeDiseaseCostDetail::getHospId,UserContext.getHospId())
                    .in(ComputeDiseaseCostDetail::getDiseaseCostId,mg)
                    .set(ComputeDiseaseCostDetail::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);
        }
    }
}
