package com.dayou.service.workflow;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.dayou.common.BaseEntity;
import com.dayou.entity.WorkNodeTask;
import com.dayou.enums.workflow.NodeTaskVersion;
import com.dayou.mapper.WorkNodeTaskMapper;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.dayou.vo.NameCodeVO;
import com.dayou.enums.workflow.HandlerPermissionEnum;
import com.dayou.enums.workflow.NodeTaskEnum;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;

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

/**
 * <p>
 * 工作流节点任务 服务实现类
 * </p>
 *
 * @author wucl
 * @since 2023-10-17
 */
@Service
public class WorkNodeTaskServiceImpl extends ServiceImpl<WorkNodeTaskMapper, WorkNodeTask> implements IWorkNodeTaskService {

    @Autowired
    private IWorkNodeService workNodeService;

    @Override
    @SuppressWarnings("unchecked")
    public Page<WorkNodeTask> selectPage(Page page,WorkNodeTask workNodeTask){
        return this.page(page, new QueryWrapper<WorkNodeTask>(workNodeTask));
    }


    @Override
    public WorkNodeTask detail(Long id){
        return this.getById(id);
    }

    @Override
    public Boolean add(WorkNodeTask workNodeTask){
        return  this.save(workNodeTask);
    }

    @Override
    public Boolean update(WorkNodeTask workNodeTask){
        return  this.updateById(workNodeTask);
    }

    @Override
    public Boolean delete(Long id){
        //逻辑删除
        return this.removeById(id);
    }

    @Override
    public List<WorkNodeTask> allNodeTask() {
        List<NodeTaskEnum> enums = Arrays.asList(NodeTaskEnum.values());
        return enums.stream().map(x -> {
            WorkNodeTask workNodeTask = new WorkNodeTask();
            workNodeTask.setTaskType(x.getCode());
            workNodeTask.setDescription(x.getMsg());
            workNodeTask.setBusinessHandle(x.getBusinessHandle());
            return workNodeTask;
        }).collect(Collectors.toList());
    }

    @Override
    public List<NameCodeVO> allHandlerPermission() {
        List<HandlerPermissionEnum> enums = Arrays.asList(HandlerPermissionEnum.values());
        return enums.stream().map(x -> {
            NameCodeVO nameCodeVO = new NameCodeVO();
            nameCodeVO.setName(x.getMsg());
            nameCodeVO.setCode(x.getCode());
            return nameCodeVO;
        }).collect(Collectors.toList());
    }

    @Override
    public WorkNodeTask getLatestNodeTask(Long nodeId) {
        WorkNodeTask task = this.getOne(new LambdaQueryWrapper<WorkNodeTask>().eq(WorkNodeTask::getNodeId, nodeId)
                .eq(WorkNodeTask::getVersion, NodeTaskVersion.LATEST.name()).eq(BaseEntity::getDeleted, Boolean.FALSE));
        return task;
    }

}
