package com.njcn.system.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.njcn.system.mapper.TemplateRelMapper;
import com.njcn.system.pojo.param.EventTemplateParam;
import com.njcn.system.pojo.po.TemplateRel;
import com.njcn.system.service.IEventTreeService;
import org.springframework.stereotype.Service;

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

@Service
public class EventTreeServiceImpl extends ServiceImpl<TemplateRelMapper, TemplateRel> implements IEventTreeService {

    /**
     * 根据模板id获取绑定树节点数据
     * @author hany
     * @date 2022/09/09
     */
    @Override
    public List<String> selectEventTree(List<String> idList) {
        return this.lambdaQuery().in(TemplateRel::getRtId,idList).list().stream().map(TemplateRel::getRdId).collect(Collectors.toList());
    }

    @Override
    public boolean updateTemplate(EventTemplateParam.TemplateRelevancy templateRelevancy) {
        deleteTemplate(templateRelevancy.getId());
        if (!templateRelevancy.getIdList().isEmpty()){
            TemplateRel templateRel = new TemplateRel();
            templateRel.setRtId(templateRelevancy.getId());
            templateRelevancy.getIdList().forEach(
                    pojo->{
                        templateRel.setRdId(pojo);
                        this.baseMapper.insert(templateRel);
                    });
        }
        return true;
    }


    public boolean deleteTemplate(String temIndex) {
        QueryWrapper<TemplateRel> templateRelQueryWrapper = new QueryWrapper<>();
        templateRelQueryWrapper.eq("report_relevancy.Rt_Id",temIndex);
        int row = this.baseMapper.delete(templateRelQueryWrapper);
        return row > 0;
    }
}
