package com.xydl.cac.service.impl;

import com.xydl.cac.entity.*;
import com.xydl.cac.exception.BusinessException;
import com.xydl.cac.model.BindDetail;
import com.xydl.cac.model.BindingModel;
import com.xydl.cac.model.ColumnModel;
import com.xydl.cac.repository.*;
import com.xydl.cac.service.*;
import lombok.extern.slf4j.Slf4j;
import org.apache.commons.lang3.StringUtils;
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 ParamIndexServiceImpl implements ParamIndexService {

    @Resource
    NSensorRepository sensorRepository;
    @Resource
    ModevTypeService modevTypeService;
    @Resource
    IcdFileConfigService configService;
    @Resource
    RptparamindexRepository rptparamindexRepository;
    @Resource
    ParamBindService bindService;
    @Resource
    DataService dataService;

    @Override
    public List<Rptparamindex> listRpt(String param) {
        List<Rptparamindex> list = rptparamindexRepository.findAllByParamindexStartingWith(param + "$");
        return list;
    }

    @Override
    public BindDetail getRpt(Integer sensorId) throws Exception {
        Optional<NSensor> optionalNSensor = sensorRepository.findById(sensorId);
        if (!optionalNSensor.isPresent()) {
            throw new BusinessException("未找到该监测装置");
        }
        NSensor sensor = optionalNSensor.get();
        ModevType modevType = modevTypeService.detail(sensor.getTypeId());
        sensor.setTableName(modevType.getTablename());

        BindDetail result = new BindDetail();
        result.setSensorId(sensorId);
        if (StringUtils.isNotBlank(sensor.getTableName())) {
            List<ColumnModel> columnList = dataService.getDataTableColumns(sensor.getTableName());
            result.setColumnList(columnList);
        }

        List<Rptparamindex> rptList = rptparamindexRepository.findAllByEqmid(sensor.getDevId());
        result.setRptList(rptList);
        if (!CollectionUtils.isEmpty(rptList)) {
            this.findMatchIed(result, rptList.get(0).getParamindex());
        }
        return result;
    }

    private void findMatchIed(BindDetail result, String paramindex) {
        List<String> iedList = configService.iedList();
        for (String ied : iedList) {
            if (paramindex.startsWith(ied)) {
                List<IcdConfigTypeInst> instList = bindService.instList(ied);
                for (IcdConfigTypeInst inst : instList) {
                    if (paramindex.startsWith(inst.getParamIndex())) {
                        result.setIcdid(inst.getId());
                        result.setIedName(ied);
                        return;
                    }
                }
            }
        }
    }

    @Override
    public void updateRpt(BindingModel item) throws Exception {
        Optional<NSensor> optionalNSensor = sensorRepository.findById(item.getSensorId());
        if (!optionalNSensor.isPresent()) {
            throw new BusinessException("未找到该监测装置");
        }
        NSensor sensor = optionalNSensor.get();
        ModevType modevType = modevTypeService.detail(sensor.getTypeId());
        sensor.setTableName(modevType.getTablename());

        // 清除旧的
        List<Rptparamindex> rptList = rptparamindexRepository.findAllByEqmid(sensor.getDevId());
        for (Rptparamindex rpt : rptList) {
            rpt.setEqmid(null);
            rpt.setColname(null);
            rpt.setTablename(null);
            rptparamindexRepository.save(rpt);
        }

        // 设置新的
        for (Rptparamindex rpt : item.getRptList()) {
            Optional<Rptparamindex> optional = rptparamindexRepository.findById(rpt.getParamindex());
            if (optional.isPresent()) {
                Rptparamindex record = optional.get();
                record.setTablename(sensor.getTableName());
                record.setEqmid(sensor.getDevId());
                record.setColname(rpt.getColname());
                rptparamindexRepository.save(record);
            }
        }
    }

    @Override
    public void clearAll() {
        rptparamindexRepository.deleteAll();
    }
}
