package com.chinaweal.aiccs.aiccs.business.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.chinaweal.aiccs.aiccs.business.entity.TSTwfProcessNode;
import com.chinaweal.aiccs.aiccs.business.mapper.TSTwfProcessNodeMapper;
import com.chinaweal.aiccs.aiccs.business.service.TSTwfProcessNodeService;
import com.chinaweal.aiccs.common.constant.Constant;
import com.chinaweal.youfool.framework.springboot.mybatis.plus.BaseServiceImpl;
import org.springframework.stereotype.Service;

import java.util.List;

/**
 * <p>
 * 流程实例节点 服务实现类
 * </p>
 *
 * @author <a href="https://blog.lroyia.top">lroyia</a>
 * @since 2021-01-29
 */
@Service
public class TSTwfProcessNodeServiceImpl extends BaseServiceImpl<TSTwfProcessNodeMapper, TSTwfProcessNode> implements TSTwfProcessNodeService {

    @Override
    public TSTwfProcessNode findTwfProcessNodeByProcessidAndStatus(String processId, int status) {
        return getOne(new LambdaQueryWrapper<TSTwfProcessNode>().eq(TSTwfProcessNode::getProcessid, processId)
                .eq(TSTwfProcessNode::getStatus, status));
    }

    @Override
    public List<TSTwfProcessNode> findTwfProcessNodeByProcessid(String processId) {
        return list(new LambdaQueryWrapper<TSTwfProcessNode>().eq(TSTwfProcessNode::getProcessid, processId));
    }

    @Override
    public List<TSTwfProcessNode> findTwfprocessnodeByProcessidAndNodename(String processId, String nodeName) {
        return list(new LambdaQueryWrapper<TSTwfProcessNode>().eq(TSTwfProcessNode::getProcessid, processId)
                .eq(TSTwfProcessNode::getCurrentnode, nodeName)
                .eq(TSTwfProcessNode::getStatus, 1));
    }

    @Override
    public void deleteTwfprocessnodeByProcessid(String processId) {
        remove(new LambdaQueryWrapper<TSTwfProcessNode>().eq(TSTwfProcessNode::getProcessid, processId));
    }

    @Override
    public String findLastTwfprocessnodeByProcessidAndNodename(String processId, String currentNode, String nextNode) {
        List<TSTwfProcessNode> list = list(new LambdaQueryWrapper<TSTwfProcessNode>().eq(TSTwfProcessNode::getProcessid, processId)
                .eq(TSTwfProcessNode::getCurrentnode,nextNode)
                .orderByDesc(TSTwfProcessNode::getCreatedon));
        TSTwfProcessNode tsTwfProcessNode = list.get(0);
        return tsTwfProcessNode.getCurruserid();
    }

    @Override
    public List<TSTwfProcessNode> queryByPROCESSIDAndCURRENTNODE(String PROCESSID, String CURRENTNODE) {
        return baseMapper.queryByPROCESSIDAndCURRENTNODE(PROCESSID,CURRENTNODE);
    }

    @Override
    public String queryRealName(String userID) {
        return baseMapper.queryRealName(userID);
    }

    @Override
    public TSTwfProcessNode queryByPROCESSID(String PROCESSID) {
        return baseMapper.queryByPROCESSID(PROCESSID);
    }
}
