package com.kcim.dao.repository;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.kcim.common.util.UserContext;
import com.kcim.dao.mapper.ComputeCheckResultMapper;
import com.kcim.dao.model.ComputeCheckResult;
import com.kcim.vo.ComputeProjectCostPageDto;
import org.springframework.stereotype.Repository;
import org.springframework.util.CollectionUtils;
import org.springframework.util.StringUtils;

import java.util.Date;
import java.util.List;
import java.util.stream.Collectors;

/**
 * @program: CostAccount
 * @description:
 * @author: Wang.YS
 * @create: 2024-03-21 15:21
 **/
@Repository
public class ComputeCheckResultRepository extends ServiceImpl<ComputeCheckResultMapper, ComputeCheckResult> {

    public Page<ComputeCheckResult> getByPage(Integer current, Integer pageSize, String computeDate, String resultType) {
        Page<ComputeCheckResult> page = new Page<>(current,pageSize);
        LambdaQueryWrapper<ComputeCheckResult> queryWrapper = new LambdaQueryWrapper<>();
        queryWrapper.eq(ComputeCheckResult::getComputeDate,computeDate);
        queryWrapper.eq(ComputeCheckResult::getHospId,UserContext.getHospId());
        if(!StringUtils.isEmpty(resultType)){
            queryWrapper.eq(ComputeCheckResult::getResultType,resultType);
        }
        return this.page(page, queryWrapper);

    }

    public void removeByComputeDate(String computeDate) {
        LambdaQueryWrapper<ComputeCheckResult> queryWrapper = new LambdaQueryWrapper<>();
        queryWrapper.eq(ComputeCheckResult::getComputeDate,computeDate);
        queryWrapper.eq(ComputeCheckResult::getHospId,UserContext.getHospId());
        List<ComputeCheckResult> list = this.list(queryWrapper);
        if(!CollectionUtils.isEmpty(list)){
            for (ComputeCheckResult computeCheckResult : list) {
                computeCheckResult.setDeleteTime(new Date());
                computeCheckResult.setDeleteUser(String.valueOf(UserContext.getCurrentUser().getId()));
            }
            this.updateBatchById(list);
            List<Integer> collect = list.stream().map(ComputeCheckResult::getId).collect(Collectors.toList());
            this.removeByIds(collect);
        }

    }
}
