
package org.jeecg.modules.workFlow.flowlong.mybatisplus.impl;

import org.jeecg.modules.workFlow.flowlong.engine.dao.FlwInstanceDao;
import org.jeecg.modules.workFlow.flowlong.engine.entity.FlwInstance;
import org.jeecg.modules.workFlow.flowlong.mybatisplus.mapper.FlwInstanceMapper;
import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.toolkit.Wrappers;

import java.util.List;
import java.util.Optional;

/**
 * 流程实例数据访问层接口实现类
 *
 * <p>
 * <a href="https://aizuda.com">官网</a>尊重知识产权，不允许非法使用，后果自负，不允许非法使用，后果自负
 * </p>
 *
 * @author hubin
 * @since 1.0
 */
public class FlwInstanceDaoImpl implements FlwInstanceDao {
    private final FlwInstanceMapper instanceMapper;

    public FlwInstanceDaoImpl(FlwInstanceMapper instanceMapper) {
        this.instanceMapper = instanceMapper;
    }

    @Override
    public boolean insert(FlwInstance instance) {
        return instanceMapper.insert(instance) > 0;
    }

    @Override
    public boolean deleteById(Long id) {
        return instanceMapper.deleteById(id) > 0;
    }

    @Override
    public boolean deleteByProcessId(Long processId) {
        return instanceMapper.delete(Wrappers.<FlwInstance>lambdaQuery()
                .eq(FlwInstance::getProcessId, processId)) > 0;
    }

    @Override
    public boolean updateById(FlwInstance instance) {
        return instanceMapper.updateById(instance) > 0;
    }

    @Override
    public Long selectCountByParentInstanceId(Long parentInstanceId) {
        return instanceMapper.selectCount(Wrappers.<FlwInstance>lambdaQuery().eq(FlwInstance::getParentInstanceId, parentInstanceId));
    }

    @Override
    public FlwInstance selectById(Long id) {
        return instanceMapper.selectById(id);
    }

    @Override
    public Optional<List<FlwInstance>> selectListByParentInstanceId(Long parentInstanceId) {
        return Optional.ofNullable(instanceMapper.selectList(Wrappers.<FlwInstance>lambdaQuery()
                .eq(FlwInstance::getParentInstanceId, parentInstanceId)));
    }

    @Override
    public Optional<List<FlwInstance>> selectListByBusinessKey(String businessKey) {
        return Optional.ofNullable(instanceMapper.selectList(Wrappers.<FlwInstance>lambdaQuery()
                .eq(FlwInstance::getBusinessKey, businessKey)));
    }

    @Override
    public List<FlwInstance> selectInstancesListByCreator(Long creatorId) {
        LambdaQueryWrapper<FlwInstance> wrapper = Wrappers.lambdaQuery(FlwInstance.class);
        wrapper.eq(FlwInstance::getCreateId, creatorId);
        return instanceMapper.selectList(wrapper);
    }
}
