package com.yunzhupaas.flowable.service.impl;

import cn.hutool.core.collection.CollectionUtil;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.yunzhupaas.base.service.SuperServiceImpl;
import com.yunzhupaas.flowable.entity.RevokeEntity;
import com.yunzhupaas.flowable.mapper.RevokeMapper;
import com.yunzhupaas.flowable.service.RevokeService;
import com.yunzhupaas.util.UserProvider;
import org.springframework.stereotype.Service;

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

/**
 * 类的描述
 *
 * @author YUNZHUPAASYUNZHUPAAS开发组
 * @version 5.0.x
 * @since 2024/8/24 13:40
 */
@Service
public class RevokeServiceImpl extends SuperServiceImpl<RevokeMapper, RevokeEntity> implements RevokeService {
    @Override
    public RevokeEntity getRevokeTask(String revokeTaskId) {
        QueryWrapper<RevokeEntity> queryWrapper = new QueryWrapper<>();
        queryWrapper.lambda().eq(RevokeEntity::getRevokeTaskId, revokeTaskId);
        return this.getOne(queryWrapper);
    }

    @Override
    public Boolean checkExist(String taskId) {
        QueryWrapper<RevokeEntity> queryWrapper = new QueryWrapper<>();
        queryWrapper.lambda().eq(RevokeEntity::getTaskId, taskId).isNull(RevokeEntity::getDeleteMark);
        return this.count(queryWrapper) == 0;
    }

    @Override
    public void deleteRevoke(String revokeTaskId) {
        RevokeEntity revokeEntity = this.getRevokeTask(revokeTaskId);
        if (null != revokeEntity) {
            revokeEntity.setDeleteMark(-1);
            revokeEntity.setDeleteTime(new Date());
            revokeEntity.setDeleteUserId(UserProvider.getLoginUserId());
            this.updateById(revokeEntity);
        }
    }

    @Override
    public List<String> getByTaskId(List<String> ids) {
        if (CollectionUtil.isEmpty(ids)) {
            return new ArrayList<>();
        }
        QueryWrapper<RevokeEntity> wrapper = new QueryWrapper<>();
        wrapper.lambda().in(RevokeEntity::getTaskId, ids);
        List<RevokeEntity> list = this.list(wrapper);
        return list.stream().map(RevokeEntity::getRevokeTaskId).collect(Collectors.toList());
    }

}
