package com.xydl.cac.service.impl;

import com.beanit.iec61850bean.FileInformation;
import com.xydl.cac.entity.IcdFile;
import com.xydl.cac.entity.IcdIed;
import com.xydl.cac.entity.IedDlConfig;
import com.xydl.cac.entity.NSensor;
import com.xydl.cac.exception.BusinessException;
import com.xydl.cac.iec.IecClient;
import com.xydl.cac.repository.IcdFileRepository;
import com.xydl.cac.repository.IcdIedRepository;
import com.xydl.cac.repository.IedDlConfigRepository;
import com.xydl.cac.repository.NSensorRepository;
import com.xydl.cac.service.IcdFileConfigService;
import com.xydl.cac.service.IedDlConfigService;
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 IedDlConfigServiceImpl implements IedDlConfigService {

    @Resource
    IedDlConfigRepository repository;
    @Resource
    NSensorRepository sensorRepository;
    @Resource
    IcdFileConfigService configService;
    @Resource
    IcdFileRepository fileRepository;
    @Resource
    IcdIedRepository iedRepository;

    @Override
    public List<IedDlConfig> listAll() {
        List<IedDlConfig> list = repository.findAll();
        List<IcdIed> iedList = configService.listIed();
        for (IedDlConfig item : list) {
            List<NSensor> sensorList = sensorRepository.findByDevId(item.getDevId());
            if (!CollectionUtils.isEmpty(sensorList)) {
                item.setSensor(sensorList.get(0));
            }
            for (IcdIed ied : iedList) {
                if (ied.getId().intValue() == item.getIedId().intValue()) {
                    item.setIed(ied);
                    break;
                }
            }
        }
        return list;
    }

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

    @Override
    public IedDlConfig add(IedDlConfig item) throws Exception {
        List<IedDlConfig> list = repository.findByDevId(item.getDevId());
        if (!CollectionUtils.isEmpty(list)) {
            throw new BusinessException("已存在该devId的配置");
        }
        item.setId(null);
        return repository.save(item);
    }

    @Override
    public void update(IedDlConfig item) throws Exception {
        repository.save(item);
    }

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

    @Override
    public List<FileInformation> listFiles(Integer iedId, String path) throws Exception {
        Optional<IcdIed> optional = iedRepository.findById(iedId);
        if (!optional.isPresent()) {
            throw new BusinessException("未找到该IED");
        }
        IcdIed ied = optional.get();
        Optional<IcdFile> optionalFile = fileRepository.findById(ied.getIcdFileId());
        if (!optionalFile.isPresent()) {
            throw new BusinessException("未找到该icd文件");
        }
        IcdFile icdFile = optionalFile.get();
        IecClient iecClient = new IecClient();
        try {
            iecClient.init(ied, icdFile.getXml());
            iecClient.connect();
            return iecClient.listFile(path);
        } catch (Exception ex) {
            throw new BusinessException(ex.getMessage());
        } finally {
            iecClient.disconnect();
        }
    }
}
