package com.yunzhupaas.engine.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.yunzhupaas.base.service.SuperServiceImpl;
import com.yunzhupaas.engine.entity.FlowOperatorUserEntity;
import com.yunzhupaas.engine.mapper.FlowOperatorUserMapper;
import com.yunzhupaas.engine.service.FlowOperatorUserService;
import com.yunzhupaas.util.RandomUtil;
import com.yunzhupaas.util.StringUtil;
import org.springframework.stereotype.Service;

import java.util.List;
import java.util.Set;

/**
 * 流程依次审批
 *
 * @author 云筑产品开发平台组
 * @version V3.1.0
 * @copyright 深圳市乐程软件有限公司
 */
@Service
public class FlowOperatorUserServiceImpl extends SuperServiceImpl<FlowOperatorUserMapper, FlowOperatorUserEntity> implements FlowOperatorUserService {

    @Override
    public List<FlowOperatorUserEntity> getList(String taskId) {
        return getTaskList(taskId, null);
    }

    @Override
    public FlowOperatorUserEntity getInfo(String id) {
        QueryWrapper<FlowOperatorUserEntity> queryWrapper = new QueryWrapper<>();
        queryWrapper.lambda().eq(FlowOperatorUserEntity::getId, id);
        return this.getOne(queryWrapper);
    }

    @Override
    public List<FlowOperatorUserEntity> getTaskList(String taskId, String taskNodeId) {
        QueryWrapper<FlowOperatorUserEntity> queryWrapper = new QueryWrapper<>();
        if (StringUtil.isNotEmpty(taskId)) {
            queryWrapper.lambda().eq(FlowOperatorUserEntity::getTaskId, taskId);
        }
        if (StringUtil.isNotEmpty(taskNodeId)) {
            queryWrapper.lambda().eq(FlowOperatorUserEntity::getTaskNodeId, taskNodeId);
        }
        queryWrapper.lambda().orderByAsc(FlowOperatorUserEntity::getSortCode);
        return this.list(queryWrapper);
    }

    @Override
    public void create(List<FlowOperatorUserEntity> list) {
        for (FlowOperatorUserEntity entity : list) {
            entity.setId(RandomUtil.uuId());
            this.save(entity);
        }
    }

    @Override
    public void update(String id, FlowOperatorUserEntity entity) {
        entity.setId(id);
        this.updateById(entity);
    }

    @Override
    public void delete(FlowOperatorUserEntity entity) {
        if (entity != null) {
            this.removeById(entity.getId());
        }
    }

    @Override
    public void deleteByTaskId(String taskId) {
        QueryWrapper<FlowOperatorUserEntity> queryWrapper = new QueryWrapper<>();
        queryWrapper.lambda().eq(FlowOperatorUserEntity::getTaskId, taskId);
        this.remove(queryWrapper);
    }

    @Override
    public void updateReject(String taskId, Set<String> taskNodeId) {
        if (taskNodeId.size() > 0) {
            QueryWrapper<FlowOperatorUserEntity> queryWrapper = new QueryWrapper<>();
            queryWrapper.lambda().eq(FlowOperatorUserEntity::getTaskId, taskId);
            queryWrapper.lambda().in(FlowOperatorUserEntity::getTaskNodeId, taskNodeId);
            this.remove(queryWrapper);
        }
    }
}

