package com.zsc.edu.gateway.modules.iot.tsl.service.impl;

import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.zsc.edu.gateway.exception.ConstraintException;
import com.zsc.edu.gateway.modules.iot.tsl.dto.EventDto;
import com.zsc.edu.gateway.modules.iot.tsl.entity.CompareParam;
import com.zsc.edu.gateway.modules.iot.tsl.entity.Event;
import com.zsc.edu.gateway.modules.iot.tsl.entity.Param;
import com.zsc.edu.gateway.modules.iot.tsl.mapper.CompareParamMapper;
import com.zsc.edu.gateway.modules.iot.tsl.mapper.EventMapper;
import com.zsc.edu.gateway.modules.iot.tsl.repo.CompareParamRepository;
import com.zsc.edu.gateway.modules.iot.tsl.repo.EventRepository;
import com.zsc.edu.gateway.modules.iot.tsl.service.EventService;
import com.zsc.edu.gateway.modules.iot.tsl.service.ParamService;
import lombok.AllArgsConstructor;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

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

/**
 * @author Yao
 */
@Service
@AllArgsConstructor
public class EventServiceImpl extends ServiceImpl<EventRepository, Event> implements EventService {
    private final EventMapper mapper;
    private final ParamService paramService;
    private final CompareParamRepository compareParamRepository;
    private final CompareParamMapper compareParamMapper;

    /**
     * 新建物模型事件
     */
    @Override
    @Transactional
    public Event create(EventDto dto) {
        if (baseMapper.findByName(dto.getName()) != null) {
            throw new ConstraintException("该事件已存在！");
        }
        if (dto.getName() == null) {
            throw new ConstraintException("事件名称不能为空！");
        }
        Event event = mapper.toEntity(dto);
        save(event);
        List<CompareParam> paramsToInsert = dto.getOutputs().stream()
                .map(output -> {
                    CompareParam param = compareParamMapper.toEntity(output);
                    param.setForeignId(event.getId());
                    param.setForeignType(Param.ForeignType.EVENT);
                    return param;
                })
                .collect(Collectors.toList());
        compareParamRepository.insert(paramsToInsert);
        return event;
    }
    /**
     * 更新物模型事件
     */
    @Override
    @Transactional
    public Event update(EventDto dto, Long id) {
        if (dto.getName() == null) {
            throw new ConstraintException("事件名称不能为空！");
        }
        Event event = baseMapper.selectById(id);
        mapper.convert(dto, event);
        updateById(event);
        paramService.update(dto.getOutputs(), event.getId());
        return event;
    }

    /**
     * 查询详情
     * @param id Id
     * @return Event
     */
    @Override
    public Event detail(Long id) {
        Event event = baseMapper.selectById(id);
        if (event == null) {
            throw new ConstraintException("该事件不存在！请检查输入ID是否正确！");
        }
        return event;
    }

    /**
     *删除
     */
    @Override
    @Transactional
    public boolean delete(Long id) {
        removeById(id);
        return paramService.delete(id);
    }

    /**
     * 切换启用状态
     */
    @Override
    public boolean toggle(Long id) {
        Event event = detail(id);
        event.setEnabled(!event.getEnabled());
        return updateById(event);
    }
}
