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.TemplateJsonEntity;
import com.yunzhupaas.flowable.entity.TemplateNodeEntity;
import com.yunzhupaas.exception.WorkFlowException;
import com.yunzhupaas.flowable.enums.NodeEnum;
import com.yunzhupaas.flowable.mapper.TemplateNodeMapper;
import com.yunzhupaas.flowable.service.TemplateJsonService;
import com.yunzhupaas.flowable.service.TemplateNodeService;
import com.yunzhupaas.util.RandomUtil;
import com.yunzhupaas.util.StringUtil;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

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

@Service
public class TemplateNodeServiceImpl extends SuperServiceImpl<TemplateNodeMapper, TemplateNodeEntity> implements TemplateNodeService {

    @Autowired
    private TemplateJsonService templateJsonService;

    @Override
    public List<TemplateNodeEntity> getList(String flowId) {
        QueryWrapper<TemplateNodeEntity> queryWrapper = new QueryWrapper<>();
        queryWrapper.lambda().eq(TemplateNodeEntity::getFlowId, flowId);
        return this.list(queryWrapper);
    }

    @Override
    public List<TemplateNodeEntity> getList(List<String> flowIds, String nodeType) {
        if (flowIds.isEmpty()) {
            return new ArrayList<>();
        }
        QueryWrapper<TemplateNodeEntity> queryWrapper = new QueryWrapper<>();
        queryWrapper.lambda().in(TemplateNodeEntity::getFlowId, flowIds);
        if (StringUtil.isNotBlank(nodeType)) {
            queryWrapper.lambda().eq(TemplateNodeEntity::getNodeType, nodeType);
        }
        return this.list(queryWrapper);
    }

    @Override
    public List<TemplateNodeEntity> getListLikeUserId(String userId) {
        QueryWrapper<TemplateNodeEntity> queryWrapper = new QueryWrapper<>();
        queryWrapper.lambda().like(TemplateNodeEntity::getNodeJson, userId);
        return this.list(queryWrapper);
    }

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

    @Override
    public void create(TemplateNodeEntity entity) {
        entity.setId(RandomUtil.uuId());
        this.save(entity);
    }

    @Override
    public boolean update(String id, TemplateNodeEntity entity) {
        entity.setId(id);
        return this.updateById(entity);
    }

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

    @Override
    public void deleteList(List<String> idList) {
        if (!idList.isEmpty()) {
            QueryWrapper<TemplateNodeEntity> queryWrapper = new QueryWrapper<>();
            queryWrapper.lambda().in(TemplateNodeEntity::getId, idList);
            this.remove(queryWrapper);
        }
    }

    @Override
    public void delete(List<String> idList) {
        if (!idList.isEmpty()) {
            QueryWrapper<TemplateNodeEntity> queryWrapper = new QueryWrapper<>();
            queryWrapper.lambda().in(TemplateNodeEntity::getId, idList);
            this.remove(queryWrapper);
        }
    }

    @Override
    public List<TemplateNodeEntity> getListStart() {
        List<TemplateJsonEntity> listOfEnable = templateJsonService.getListOfEnable();
        List<String> flowIds = listOfEnable.stream().map(TemplateJsonEntity::getId).collect(Collectors.toList());
        if (CollectionUtil.isNotEmpty(flowIds)) {
            QueryWrapper<TemplateNodeEntity> queryWrapper = new QueryWrapper<>();
            queryWrapper.lambda().select(TemplateNodeEntity::getFlowId, TemplateNodeEntity::getFormId);
            queryWrapper.lambda().eq(TemplateNodeEntity::getNodeType, NodeEnum.start.getType()).in(TemplateNodeEntity::getFlowId, flowIds);
            return this.list(queryWrapper);
        }
        return new ArrayList<>();
    }
}

