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

import org.jeecg.modules.workFlow.flowlong.engine.dao.FlwHisInstanceDao;
import org.jeecg.modules.workFlow.flowlong.engine.entity.FlwHisInstance;
import org.jeecg.modules.workFlow.flowlong.mybatisplus.mapper.FlwHisInstanceMapper;
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 FlwHisInstanceDaoImpl implements FlwHisInstanceDao {
    private final FlwHisInstanceMapper hisInstanceMapper;

    public FlwHisInstanceDaoImpl(FlwHisInstanceMapper hisInstanceMapper) {
        this.hisInstanceMapper = hisInstanceMapper;
    }

    @Override
    public boolean insert(FlwHisInstance hisInstance) {
        return hisInstanceMapper.insert(hisInstance) > 0;
    }

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

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

    @Override
    public boolean updateById(FlwHisInstance hisInstance) {
        return hisInstanceMapper.updateById(hisInstance) > 0;
    }

    @Override
    public FlwHisInstance selectById(Long id) {
        return hisInstanceMapper.selectById(id);
    }

    private Optional<List<FlwHisInstance>> ofNullable(List<FlwHisInstance> hisInstances) {
        return Optional.ofNullable(null == hisInstances || hisInstances.isEmpty() ? null : hisInstances);
    }

    @Override
    public Optional<List<FlwHisInstance>> selectListByProcessId(Long processId) {
        return this.ofNullable(hisInstanceMapper.selectList(Wrappers.<FlwHisInstance>lambdaQuery()
                .eq(FlwHisInstance::getProcessId, processId)));
    }

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

}
