package cn.fateverse.workflow.service.impl;

import cn.fateverse.common.core.entity.Option;
import cn.fateverse.workflow.entity.ProcessListener;
import cn.fateverse.workflow.entity.dto.ProcessListenerDto;
import cn.fateverse.workflow.entity.vo.ProcessListenerVo;
import cn.fateverse.workflow.entity.query.ProcessListenerQuery;
import cn.fateverse.workflow.mapper.ProcessListenerMapper;
import cn.fateverse.workflow.service.ProcessListenerService;
import cn.fateverse.common.core.result.page.TableDataInfo;
import cn.fateverse.common.mybatis.utils.PageUtils;
import lombok.extern.slf4j.Slf4j;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Propagation;
import org.springframework.transaction.annotation.Transactional;

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

/**
 * 系统内置监听器 Controller
 *
 * @author clay
 * @date 2023-03-27
 */
@Slf4j
@Service
public class ProcessListenerServiceImpl implements ProcessListenerService {

    private final ProcessListenerMapper processListenerMapper;

    public ProcessListenerServiceImpl(ProcessListenerMapper processListenerMapper) {
        this.processListenerMapper = processListenerMapper;
    }

    @Override
    public ProcessListenerVo searchById(String id){
        ProcessListener processListener = processListenerMapper.selectById(id);
        return ProcessListenerVo.toProcessListenerVo(processListener);
    }

    @Override
    public TableDataInfo<ProcessListenerVo> searchList(ProcessListenerQuery query){
        PageUtils.startPage();
        List<ProcessListener> list = processListenerMapper.selectList(query);
        return PageUtils.convertDataTable(list, ProcessListenerVo::toProcessListenerVo);
    }

    @Override
    public List<Option> searchOptionList(String type) {
        ProcessListenerQuery query = new ProcessListenerQuery();
        query.setListenerType(type);
        List<ProcessListener> list = processListenerMapper.selectList(query);
        return list.stream().map(ProcessListener::toOption).collect(Collectors.toList());
    }

    @Override
    public List<ProcessListenerVo> exportList(ProcessListenerQuery query){
        List<ProcessListener> list = processListenerMapper.selectList(query);
        return list.stream().map(ProcessListenerVo::toProcessListenerVo)
                .collect(Collectors.toList());
    }

    @Override
    @Transactional(rollbackFor = Exception.class,propagation = Propagation.REQUIRED)
    public int save(ProcessListenerDto processListener){
        String id = UUID.randomUUID().toString();
        ProcessListener info = ProcessListenerDto.toProcessListener(processListener);
        info.setId(id);
        return processListenerMapper.insert(info);
    }

    @Override
    @Transactional(rollbackFor = Exception.class,propagation = Propagation.REQUIRED)
    public int edit(ProcessListenerDto processListener){
        ProcessListener info = ProcessListenerDto.toProcessListener(processListener);
        return processListenerMapper.update(info);
    }

    @Override
    @Transactional(rollbackFor = Exception.class,propagation = Propagation.REQUIRED)
    public int removeById(String id){
        return processListenerMapper.deleteById(id);
    }

    @Override
    @Transactional(rollbackFor = Exception.class,propagation = Propagation.REQUIRED)
    public int removeBatch(List<String> idList){
        return processListenerMapper.deleteBatchByIdList(idList);
    }

}