package com.xydl.cac.service.impl;

import com.xydl.cac.entity.Bdz;
import com.xydl.cac.entity.Jg;
import com.xydl.cac.entity.Zsb;
import com.xydl.cac.exception.BusinessException;
import com.xydl.cac.model.StaticVariable;
import com.xydl.cac.repository.BdzRepository;
import com.xydl.cac.repository.JgRepository;
import com.xydl.cac.repository.NSensorRepository;
import com.xydl.cac.repository.ZsbRepository;
import com.xydl.cac.service.JgService;
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.ArrayList;
import java.util.List;
import java.util.Optional;

@Service
@Slf4j
@Transactional(rollbackFor = Exception.class)
public class JgServiceImpl implements JgService {

    @Resource
    JgRepository repository;
    @Resource
    BdzRepository bdzRepository;
    @Resource
    ZsbRepository zsbRepository;
    @Resource
    NSensorRepository sensorRepository;

    @Override
    public List<Jg> listAll(Integer bdzid) {
        if (bdzid == null) {
            if (StaticVariable.jg_Cache == null) {
                StaticVariable.jg_Cache = repository.findAll();
                this.fillBdzName(StaticVariable.jg_Cache);
            }
            return StaticVariable.jg_Cache;
        } else {
            List<Jg> result = repository.findByBdzid(bdzid);
            Optional<Bdz> optional = bdzRepository.findById(bdzid);
            if (optional.isPresent() && !CollectionUtils.isEmpty(result)) {
                for (Jg jg : result) {
                    jg.setBdzName(optional.get().getMc());
                }
            }
            return result;
        }
    }

    @Override
    public List<Jg> sensorCount(Integer bdzid) {
        List<Jg> result;
        if (bdzid == null) {
            if (StaticVariable.jg_Cache == null) {
                StaticVariable.jg_Cache = repository.findAll();
                this.fillBdzName(StaticVariable.jg_Cache);
            }
            result = StaticVariable.jg_Cache;
        } else {
            result = repository.findByBdzid(bdzid);
            Optional<Bdz> optional = bdzRepository.findById(bdzid);
            if (optional.isPresent() && !CollectionUtils.isEmpty(result)) {
                for (Jg jg : result) {
                    jg.setBdzName(optional.get().getMc());
                }
            }
        }
        for (Jg jg : result) {
            List<Zsb> zsbList = zsbRepository.findByJgid(jg.getId());
            List<Integer> idList = new ArrayList<>();
            for (Zsb zsb : zsbList) {
                idList.add(zsb.getId());
            }
            if (idList.size() > 0) {
                long count = sensorRepository.countByZsbIdIn(idList);
                jg.setSensorCount(count);
            }
        }
        return result;
    }

    private void fillBdzName(List<Jg> list) {
        List<Bdz> bdzList = bdzRepository.findAll();
        if (!CollectionUtils.isEmpty(list) && !CollectionUtils.isEmpty(bdzList)) {
            for (Jg jg : list) {
                for (Bdz bdz : bdzList) {
                    if (bdz.getId().equals(jg.getBdzid())) {
                        jg.setBdzName(bdz.getMc());
                        break;
                    }
                }
            }
        }
    }

    @Override
    public Jg add(Jg item) throws Exception {
        item.setId(null);
        Optional<Bdz> optionalBdz = bdzRepository.findById(item.getBdzid());
        if (!optionalBdz.isPresent()) {
            throw new BusinessException("未找到该变电站id=" + item.getBdzid());
        }
        List<Jg> list = repository.findByBdzidAndMc(item.getBdzid(), item.getMc());
        if (!CollectionUtils.isEmpty(list)) {
            throw new BusinessException("该名称已被使用");
        }
        StaticVariable.jg_Cache = null;
        return repository.save(item);
    }

    @Override
    public void update(Jg item) throws Exception {
        Optional<Bdz> optionalBdz = bdzRepository.findById(item.getBdzid());
        if (!optionalBdz.isPresent()) {
            throw new BusinessException("未找到该变电站id=" + item.getBdzid());
        }
        List<Jg> list = repository.findByBdzidAndMcAndIdIsNot(item.getBdzid(), item.getMc(), item.getId());
        if (!CollectionUtils.isEmpty(list)) {
            throw new BusinessException("该名称已被使用");
        }
        repository.save(item);
        StaticVariable.jg_Cache = null;
        StaticVariable.zsb_Cache = null;
    }

    @Override
    public void delete(Integer id) throws Exception {
        List<Zsb> zsbList = zsbRepository.findByJgid(id);
        if (!CollectionUtils.isEmpty(zsbList)) {
            throw new BusinessException("已被主设备使用不能删除");
        }
        repository.deleteById(id);
        StaticVariable.jg_Cache = null;
    }

    @Override
    public Jg detail(Integer id) throws Exception {
        Optional<Jg> optional = repository.findById(id);
        if (!optional.isPresent()) {
            throw new BusinessException("未找到该区域id=" + id);
        }
        Jg jg = optional.get();
        Optional<Bdz> optionalBdz = bdzRepository.findById(jg.getBdzid());
        if (optionalBdz.isPresent()) {
            jg.setBdzName(optionalBdz.get().getMc());
        }
        return jg;
    }
}
