package com.xydl.cac.service.impl;

import com.xydl.cac.entity.ModevType;
import com.xydl.cac.entity.NSensor;
import com.xydl.cac.exception.BusinessException;
import com.xydl.cac.model.StaticVariable;
import com.xydl.cac.repository.ModevTypePointRepository;
import com.xydl.cac.repository.ModevTypeRepository;
import com.xydl.cac.repository.NSensorRepository;
import com.xydl.cac.service.ModevTypeService;
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.List;
import java.util.Optional;

@Service
@Slf4j
@Transactional(rollbackFor = Exception.class)
public class ModevTypeServiceImpl implements ModevTypeService {

    @Resource
    ModevTypeRepository repository;
    @Resource
    ModevTypePointRepository pointRepository;
    @Resource
    NSensorRepository sensorRepository;

    @Override
    public List<ModevType> listAll() {
        if (StaticVariable.modevType_Cache == null) {
            StaticVariable.modevType_Cache = repository.findAll();
        }
        return StaticVariable.modevType_Cache;
    }

    @Override
    public List<ModevType> sensorCount() {
        List<ModevType> list = this.listAll();
        for (ModevType modevType : list) {
            long count = sensorRepository.countByTypeId(modevType.getId());
            modevType.setSensorCount(count);
        }
        return list;
    }

    @Override
    public ModevType add(ModevType item) throws Exception {
        item.setId(null);
        List<ModevType> byMc = repository.findByMc(item.getMc());
        if (!CollectionUtils.isEmpty(byMc)) {
            throw new BusinessException("该监测装置类型已存在");
        }
        StaticVariable.modevType_Cache = null;
        return repository.save(item);
    }

    @Override
    public void update(ModevType item) throws Exception {
        List<ModevType> byMc = repository.findByMc(item.getMc());
        if (!CollectionUtils.isEmpty(byMc)) {
            for (ModevType it : byMc) {
                if (it.getId().intValue() != item.getId().intValue()) {
                    throw new BusinessException("该监测装置类型已存在");
                }
            }
        }
        repository.save(item);
        StaticVariable.modevType_Cache = null;
    }

    @Override
    public void delete(Integer id) throws Exception {
        List<NSensor> list = sensorRepository.findByTypeId(id);
        if (!CollectionUtils.isEmpty(list)) {
            throw new BusinessException("存在该类型的监测装置，不能删除");
        }
        repository.deleteById(id);
        pointRepository.deleteByModevtypeId(id);
        StaticVariable.modevType_Cache = null;
    }

    @Override
    public ModevType detail(Integer id) throws Exception {
        Optional<ModevType> optional = repository.findById(id);
        if (!optional.isPresent()) {
            throw new BusinessException("未找到该类型");
        }
        return optional.get();
    }
}
