package com.ho.datacollect.service.impl;

import com.ho.business.constant.DeviceTypeConstant;
import com.ho.business.entity.Device;
import com.ho.business.entity.DeviceCall;
import com.ho.business.entity.Topology;
import com.ho.business.entity.TranslateName;
import com.ho.business.service.DeviceCallService;
import com.ho.business.service.DeviceService;
import com.ho.business.service.DynamicConfigService;
import com.ho.common.tools.constant.CommonConstant;
import com.ho.common.tools.constant.RedisKeyConstant;
import com.ho.datacollect.api.constant.DataCollectConstant;
import com.ho.datacollect.api.vo.req.add.DataSetParam;
import com.ho.datacollect.service.DataCollectService;
import lombok.extern.slf4j.Slf4j;
import org.redisson.api.RLock;
import org.redisson.api.RedissonClient;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.Date;
import java.util.List;

/**
 * @author gyan
 * @desc: 设备入库
 * @DateTime: 2022/10/13 15:25
 */
@Service(DataCollectConstant.KEY_MAP.DEVICE)
@Slf4j
public class DeviceDataCollectServiceImpl implements DataCollectService {

    @Autowired
    DeviceService deviceService;

    @Autowired
    DeviceCallService deviceCallService;

    @Autowired
    RedissonClient redissonClient;

    @Autowired
    DynamicConfigService dynamicConfigService;

    @Override
    public void doService(DataSetParam dataSetParam) {
        log.info("DeviceDataCollectServiceImpl===============:{}", dataSetParam);
        RLock lock = null;
        try {
            if (dataSetParam == null) {
                return;
            }
            //设备数据来自于 topology,所以类型也是Topology
            List<Topology> topologyList = dataSetParam.getList();
            Integer groupId = dataSetParam.getGroupId();
            Integer stationId = dataSetParam.getStationId();
            if (topologyList.isEmpty()) {
                return;
            }
            lock = redissonClient.getLock(RedisKeyConstant.LOCK_KEY_DEVICE);
            //获取锁,没有获取到锁的阻塞在lock.lock() 这行
            lock.lock();
            String sn = dataSetParam.getSerialNo();
            Integer pid = 0;
            //拓扑转成device
            for (Topology topology : topologyList) {
                //根据stationId和设备id查，如果有数据就忽略，否则新增
                log.info("DeviceDataCollectServiceImpl===============:{},{}", stationId, topology.getId());
                DeviceCall deviceCall = deviceCallService.selectByStationIdAndSrcId(stationId, topology.getId());
                if (deviceCall != null) {
                    continue;
                }
                log.info("DeviceDataCollectServiceImpl===============deviceCall:{}", deviceCall);
                //此时device 还未初始化，所以先初始化
                deviceCall = new DeviceCall();
                //新增，先查设备类型
                //根据类型三要素查typeContract查 deviceTypeId ,然后再去device_type查 类型
                //修改为手工维护，并与devic_type_config表相关，此处拓扑表舍弃
//                Topology topologyQuery = new Topology();
//                topologyQuery.setCategory(topology.getCategory());
//                topologyQuery.setType(topology.getType());
//                topologyQuery.setSubType(topology.getSubType());
//                TypeContrast typeContract = typeContrastService.getTypeContract(topologyQuery);
//                //继续查deviceType
//                if (typeContract != null) {
//                    DeviceType one = deviceTypeService.getOne(typeContract.getDeviceTypeId());
//                    deviceCall.setDeviceType(one.getDeviceType());
//                }
                //父节点为0的时候代表电站
                if(CommonConstant.ZERO.equals(topology.getPid())){
                    pid = topology.getId();
                }
                deviceCall.setSerialNo(sn);
                deviceCall.setGroupId(groupId);
                deviceCall.setStationId(stationId);
                deviceCall.setSrcId(topology.getId());
                deviceCall.setDeviceName(topology.getName());
                TranslateName translate = dynamicConfigService.getNameLanguage(topology.getName());
                deviceCall.setDeviceNameEn(translate.getNameEn());
                deviceCall.setDeviceNameFra(translate.getNameFra());
                deviceCall.setDeviceNameSpa(translate.getNameSpa());
                deviceCall.setDeviceNameDe(translate.getNameDe());
                deviceCall.setDeviceNamePl(translate.getNamePl());
                deviceCall.setDeviceNameAra(translate.getNameAra());
                deviceCall.setPid(topology.getPid());
                deviceCall.setCategory(topology.getCategory());
                deviceCall.setCreateTime(new Date());
                //status 先传3
                deviceCall.setStatus(3);
                deviceCallService.insertSelective(deviceCall);
                Device device = new Device();
                BeanUtils.copyProperties(deviceCall,device);
                deviceService.insertSelective(device);
            }
            //将站点也作为一个设备存储到表里面
            DeviceCall deviceCall = deviceCallService.selectByStationIdAndSrcId(stationId, CommonConstant.ZERO);
            if (deviceCall == null) {
                deviceCall = new DeviceCall();
                deviceCall.setSerialNo(sn);
                deviceCall.setGroupId(groupId);
                deviceCall.setStationId(stationId);
                deviceCall.setSrcId(CommonConstant.ZERO);
                deviceCall.setDeviceName(CommonConstant.ACCESSPOINT);
                TranslateName translate = dynamicConfigService.getNameLanguage(CommonConstant.ACCESSPOINT);
                deviceCall.setDeviceNameEn(translate.getNameEn());
                deviceCall.setDeviceNameFra(translate.getNameFra());
                deviceCall.setDeviceNameSpa(translate.getNameSpa());
                deviceCall.setDeviceNameDe(translate.getNameDe());
                deviceCall.setDeviceNamePl(translate.getNamePl());
                deviceCall.setDeviceNameAra(translate.getNameAra());
                deviceCall.setDeviceType(DeviceTypeConstant.ACCESSPOINT);
                deviceCall.setPid(pid);
                deviceCall.setCreateTime(new Date());
                //status 先传3
                deviceCall.setStatus(3);
                log.info("准备添加接入站点进入设备表");
                deviceCallService.insertSelective(deviceCall);
                Device device = new Device();
                BeanUtils.copyProperties(deviceCall,device);
                deviceService.insertSelective(device);
            }

        } catch (Exception e) {
            log.error("添加设备数据失败:{}",e.getMessage());
        }finally {
            if (lock != null && lock.isHeldByCurrentThread()) {
                lock.unlock();
            }
        }


    }
}
