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.ComputeItemCostDetailMapper;
import com.kcim.dao.model.ComputeItemCostDetail;
import com.kcim.dao.model.ImportPatientItem;
import com.kcim.vo.SessionUserVO;
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-25 14:38
 **/
@Repository
public class ComputeItemCostDetailRepository extends ServiceImpl<ComputeItemCostDetailMapper, ComputeItemCostDetail> {
    static  Integer MAX_NUMBER = 500;

    private static Integer countStep(Integer size) {
        return (size + MAX_NUMBER - 1) / MAX_NUMBER;
    }
    public void removeByItemCostId(List<Integer> costIds, SessionUserVO currentUser) {
        if(!CollectionUtils.isEmpty(costIds)){
            int limit = countStep(costIds.size());
            //方法一：使用流遍历操作
            List<List<Integer>> mglist = new ArrayList<>();
            Stream.iterate(0, n -> n + 1).limit(limit).forEach(i -> {
                mglist.add(costIds.stream().skip(i * MAX_NUMBER).limit(MAX_NUMBER).collect(Collectors.toList()));
            });
            for (List<Integer> integers : mglist) {
                UpdateWrapper<ComputeItemCostDetail> updateWrapper = new UpdateWrapper<>();
                updateWrapper.lambda().eq(ComputeItemCostDetail::getDelFlag,0)
                                .eq(ComputeItemCostDetail::getHospId,currentUser.getHospId())
                        .in(ComputeItemCostDetail::getItemCostId,integers)
                                                .set(ComputeItemCostDetail::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);
            }
        }

//
//
//        update compute_item_cost_detail set del_flag = 1
//        where item_cost_id in(select id from compute_item_cost where del_flag=0 and compute_date='2023-11')
//
//
//
//
//        LambdaQueryWrapper<ComputeItemCostDetail> queryWrapper = new LambdaQueryWrapper<>();
//        queryWrapper.eq(ComputeItemCostDetail::getHospId, UserConcurrentUsertext.getHospId());
//        queryWrapper.in(ComputeItemCostDetail::getItemCostId,costIds);
//        List<ComputeItemCostDetail> list = this.list(queryWrapper);
//        if(!CollectionUtils.isEmpty(list)){
////            list.forEach(computeItemCost -> {
////                computeItemCost.setDeleteUser(String.valueOf(UserContext.getCurrentUser().getId()));
////                computeItemCost.setDeleteTime(new Date());
////            });
////            this.updateBatchById(list,100);
//            List<Integer> collect = list.stream().map(ComputeItemCostDetail::getId).collect(Collectors.toList());
//            this.removeBatchByIds(collect,500);
//        }
    }
}
