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.ComputePatientCostDetailMapper;
import com.kcim.dao.model.ComputeItemCostDetail;
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-30 16:18
 **/
@Repository
public class ComputePatientCostDetailRepository extends ServiceImpl<ComputePatientCostDetailMapper, ComputePatientCostDetail> {
    static  Integer MAX_NUMBER = 500;

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