package com.xydl.cac.service.impl;

import com.alibaba.excel.util.StringUtils;
import com.xydl.cac.entity.*;
import com.xydl.cac.exception.BusinessException;
import com.xydl.cac.repository.*;
import com.xydl.cac.service.I2syncService;
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.ArrayList;
import java.util.Date;
import java.util.List;

@Service
@Slf4j
@Transactional(rollbackFor = Exception.class)
public class I2syncServiceImpl implements I2syncService {

    @Resource
    ModevTypeRepository modevTypeRepository;
    @Resource
    ModevTypeService modevTypeService;
    @Resource
    I2syncFieldRepository syncFieldRepository;
    @Resource
    I2syncConfigRepository configRepository;
    @Resource
    I2syncRecordRepository recordRepository;
    @Resource
    NSensorRepository sensorRepository;

    @Override
    public List<ModevType> listFieldConfig() {
        List<ModevType> result = new ArrayList<>();
        List<ModevType> typeList = modevTypeRepository.findAll();
        for (ModevType item : typeList) {
            if (StringUtils.isNotBlank(item.getTablename())) {
                List<I2syncField> fieldList = syncFieldRepository.findByTableName(item.getTablename());
                if (!CollectionUtils.isEmpty(fieldList)) {
                    item.setI2syncFields(fieldList);
                    result.add(item);
                }
            }
        }
        return result;
    }

    @Override
    public List<I2syncField> listFieldConfig(String tablename) {
        List<I2syncField> fieldList = syncFieldRepository.findByTableName(tablename);
        return fieldList;
    }

    @Override
    public void updateFieldConfig(ModevType item) {
        syncFieldRepository.deleteByTableName(item.getTablename());
        for (I2syncField field : item.getI2syncFields()) {
            field.setTableName(item.getTablename());
            field.setId(null);
            syncFieldRepository.save(field);
        }
    }

    @Override
    public void delFieldConfig(String tablename) {
        syncFieldRepository.deleteByTableName(tablename);
    }

    @Override
    public List<I2syncConfig> listConfig() {
        List<I2syncConfig> list = configRepository.findAll();
        this.fillModeType(list);
        return list;
    }

    private void fillModeType(List<I2syncConfig> list) {
        List<ModevType> modevTypeList = modevTypeService.listAll();
        if (!CollectionUtils.isEmpty(list)) {
            for (I2syncConfig item : list) {
                for (ModevType type : modevTypeList) {
                    if (type.getId().equals(item.getModevtypeId())) {
                        item.setTypeName(type.getMc());
                        item.setTableName(type.getTablename());
                        item.setTypeCode(type.getTypeCode());
                    }
                }
            }
        }
    }

    @Override
    public void addConfig(I2syncConfig item) throws Exception {
        ModevType modevType = modevTypeService.detail(item.getModevtypeId());
        List<I2syncField> fieldList = syncFieldRepository.findByTableName(modevType.getTablename());
        if (CollectionUtils.isEmpty(fieldList)) {
            throw new BusinessException("还未配置该类型的字段映射");
        }
        item.setId(null);
        item.setCreateTime(new Date());
        configRepository.save(item);

        List<NSensor> list = sensorRepository.findByTypeId(item.getModevtypeId());
        for (NSensor sensor : list) {
            if (StringUtils.isNotBlank(sensor.getEquipmentId()) &&
                    StringUtils.isNotBlank(sensor.getSensorCode()) &&
                    StringUtils.isNotBlank(sensor.getPhase())) {
                List<I2syncRecord> recordList = recordRepository.findByEqmid(sensor.getDevId());
                if (CollectionUtils.isEmpty(recordList)) {
                    I2syncRecord record = new I2syncRecord();
                    record.setEqmid(sensor.getDevId());
                    record.setModevtypeId(sensor.getTypeId());
                    recordRepository.save(record);
                }
            }
        }
    }

    @Override
    public void delConfig(Integer id) {
        configRepository.deleteById(id);
    }

    @Override
    public List<I2syncRecord> listRecord(Integer modevtypeId) {
        List<I2syncRecord> list;
        if (modevtypeId != null) {
            list = recordRepository.findByModevtypeId(modevtypeId);
        } else {
            list = recordRepository.findAll();
        }

        for (I2syncRecord record : list) {
            List<NSensor> sensorList = sensorRepository.findByDevId(record.getEqmid());
            if (!CollectionUtils.isEmpty(sensorList)) {
                record.setSensor(sensorList.get(0));
            }
        }
        return list;
    }

}
