package com.xydl.cac.service.impl;

import com.xydl.cac.entity.ModevTypePoint;
import com.xydl.cac.entity.NSensor;
import com.xydl.cac.entity.WarnRule;
import com.xydl.cac.exception.BusinessException;
import com.xydl.cac.model.StaticVariable;
import com.xydl.cac.repository.ModevTypePointRepository;
import com.xydl.cac.repository.WarnRuleRepository;
import com.xydl.cac.service.DataService;
import com.xydl.cac.service.NSensorService;
import com.xydl.cac.service.WarnRuleService;
import com.xydl.cac.util.DateUtil;
import lombok.extern.slf4j.Slf4j;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import javax.annotation.Resource;
import java.util.List;
import java.util.Map;
import java.util.Optional;

@Service
@Slf4j
@Transactional(rollbackFor = Exception.class)
public class WarnRuleServiceImpl implements WarnRuleService {

    @Resource
    WarnRuleRepository repository;
    @Resource
    NSensorService sensorService;
    @Resource
    ModevTypePointRepository typePointRepository;
    @Resource
    DataService dataService;

    @Override
    public List<WarnRule> listAll(Integer sensorId) throws Exception {
        List<WarnRule> list;
        if (sensorId != null) {
            list = repository.findBySensorId(sensorId);
        } else {
            list = repository.findAll();
        }
        for (WarnRule item : list) {
            NSensor sensor = sensorService.detail(item.getSensorId());
            item.setNSensor(sensor);
            Optional<ModevTypePoint> optionalModevTypePoint = typePointRepository.findById(item.getModevtypePointId());
            if (optionalModevTypePoint.isPresent()) {
                item.setTypePoint(optionalModevTypePoint.get());
            }
        }
        return list;
    }

    @Override
    public WarnRule add(WarnRule item) throws Exception {
        item.setId(null);
        item.setLastDTime(null);
        NSensor sensor = sensorService.detail(item.getSensorId());
        if (sensor == null) {
            throw new BusinessException("未找到该装置id=" + item.getSensorId());
        }
        Optional<ModevTypePoint> optionalModevTypePoint = typePointRepository.findById(item.getModevtypePointId());
        if (!optionalModevTypePoint.isPresent()) {
            throw new BusinessException("未找到该监测装置类型(" + sensor.getTypeName() + ")的属性点" + item.getModevtypePointId());
        }
        ModevTypePoint typePoint = optionalModevTypePoint.get();
        Map<String, Object> map = dataService.getLastOneData(sensor.getTableName(), sensor.getDevId(), typePoint);
        if (map != null) {
            String str = (String) map.get("acquisitionTime");
            item.setLastDTime(DateUtil.parse(str));
        }
        return repository.save(item);
    }

    @Override
    public void update(WarnRule item) throws Exception {
        Optional<WarnRule> optional = repository.findById(item.getId());
        if (!optional.isPresent()) {
            throw new BusinessException("未找到该告警规则id=" + item.getId());
        }
        WarnRule rule = optional.get();
        rule.setComparator(item.getComparator());
        rule.setOperator(item.getOperator());
        rule.setThreshold(item.getThreshold());
        rule.setLevel(item.getLevel());
        rule.setActive(item.getActive());
        rule.setNotifyCom(item.getNotifyCom());
        repository.save(rule);
        StaticVariable.rule_Cache.remove(rule.getId());
    }

    @Override
    public void delete(Integer id) throws Exception {
        repository.deleteById(id);
        StaticVariable.rule_Cache.remove(id);
    }
}
