package com.xydl.cac.service.impl;

import com.xydl.cac.entity.Iec104Point;
import com.xydl.cac.exception.BusinessException;
import com.xydl.cac.repository.Iec104PointRepository;
import com.xydl.cac.service.Iec104PointService;
import lombok.extern.slf4j.Slf4j;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;
import org.springframework.util.CollectionUtils;

import javax.annotation.Resource;
import java.util.Date;
import java.util.List;

@Service
@Slf4j
@Transactional(rollbackFor = Exception.class)
public class Iec104PointServiceImpl implements Iec104PointService {

    @Resource
    Iec104PointRepository iec104PointRepository;

    @Override
    public List<Iec104Point> findAll() {
        return iec104PointRepository.findAll();
    }

    @Override
    public Iec104Point add(Iec104Point point) throws BusinessException {
        List<Iec104Point> list = iec104PointRepository.findBySadr(point.getSadr());
        if (CollectionUtils.isEmpty(list)) {
            point.setId(null);
            point.setCreateTime(new Date());
            return iec104PointRepository.save(point);
        } else {
            throw new BusinessException("该点位已存在");
        }
    }

    @Override
    public void delete(Integer id) {
        iec104PointRepository.deleteById(id);
    }
}
