package com.ho.datacollect.service.impl;

import com.ho.business.entity.Topology;
import com.ho.business.service.TopologyService;
import com.ho.business.vo.req.TopologyReqVo;
import com.ho.datacollect.api.constant.DataCollectConstant;
import com.ho.datacollect.api.vo.req.add.DataSetParam;
import com.ho.datacollect.feignclient.DataCollectTdFeignClient;
import com.ho.datacollect.service.DataCollectService;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.List;

/**
 * @author
 * @desc: 拓扑数据
 * 这个地方需要把拓扑数据和设备数据都入库
 * @date
 */

@Service(DataCollectConstant.KEY_MAP.TOPOLOGY)
@Slf4j
public class TopologyDataCollectServiceImpl implements DataCollectService {

    @Autowired
    TopologyService topologyService;

    @Autowired
    DataCollectTdFeignClient tdFeignClient;

    @Override
    public void doService(DataSetParam dataSetParam) {
        log.info("TopologyDataCollectServiceImpl=================");
        try {
            if (dataSetParam == null) {
                return;
            }
            //取topology
            List<Topology> topologyList = dataSetParam.getList();
            if (topologyList == null || topologyList.isEmpty()) {
                return;
            }
            Integer groupId = dataSetParam.getGroupId();
            Integer stationId = dataSetParam.getStationId();
            for (Topology topology : topologyList) {
                Topology topo = new Topology();
                //id设置为空,自增
                topo.setId(null);
                topo.setPid(topology.getPid());
                topo.setSrcId(topology.getId());
                topo.setGroupId(groupId);
                topo.setStationId(stationId);
                topo.setName(topology.getName());
                topo.setCategory(topology.getCategory());
                topo.setType(topology.getType());
                topo.setSubType(topology.getSubType());
                //遍历link连接点赋值
                 /*
                JSONArray link = topologyMqtt.getLink();
                List<JSONObject> linkList = link.toJavaList(JSONObject.class);
               for (JSONObject lidJsonObesct : linkList) {
                    if (topo.getLink1() == null) {
                        Integer lid = lidJsonObesct.getInteger("lid");
                        topo.setLink1(lid);
                        continue;
                    }
                    if (topo.getLink2() == null) {
                        Integer lid = lidJsonObesct.getInteger("lid");
                        topo.setLink1(lid);
                        continue;
                    }
                    if (topo.getLink3() == null) {
                        Integer lid = lidJsonObesct.getInteger("lid");
                        topo.setLink1(lid);
                        continue;
                    }
                    if (topo.getLink4() == null) {
                        Integer lid = lidJsonObesct.getInteger("lid");
                        topo.setLink1(lid);
                        continue;
                    }
                }*/
                //入库前判断是否已存在
                TopologyReqVo topologyReqVo = new TopologyReqVo();
                topologyReqVo.setGroupId(groupId);
                topologyReqVo.setStationId(stationId);
                topologyReqVo.setSrcId(topology.getId());
                Topology topogy = topologyService.selectBysev(topologyReqVo);
                if (topogy != null) {
                    topo.setId(topogy.getId());
                    topologyService.updateByPrimaryKey(topo);
                }
                // 先模拟报唯一主键错误
                else {
                    topologyService.insertSelective(topo);
                }


            }
            //} catch (BeansException e) {
        } catch (Exception e) {
            log.error("添加或更新topology数据失败: " + e.getMessage());
        }
    }
}
