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.ComputeClinicalPathwayCostDetailMapper;
import com.kcim.dao.model.ComputeClinicalPathwayCostDetail;
import com.kcim.dao.model.ComputeGroupCostDetail;
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 18:19
 **/
@Repository
public class ComputeClinicalPathwayCostDetailRepository extends ServiceImpl<ComputeClinicalPathwayCostDetailMapper, ComputeClinicalPathwayCostDetail> {
    static  Integer MAX_NUMBER = 500;

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

    }
}
