package com.usky.transfer.service.mqtt.add;

import com.alibaba.fastjson.JSONObject;
import com.alibaba.nacos.shaded.com.google.gson.JsonObject;
import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.toolkit.Wrappers;
import com.usky.common.core.util.UUIDUtils;
import com.usky.transfer.domain.DmpDevice;
import com.usky.transfer.domain.DmpDeviceStatus;
import com.usky.transfer.domain.DmpProduct;
import com.usky.transfer.service.DmpDeviceService;
import com.usky.transfer.service.DmpDeviceStatusService;
import com.usky.transfer.service.DmpProductService;
import com.usky.transfer.service.mqtt.MqttStrategy;
import com.usky.transfer.service.vo.DeviceMapVO;
import com.usky.transfer.service.vo.MqttBaseVO;
import com.usky.transfer.service.vo.ProductMapVO;
import lombok.extern.slf4j.Slf4j;
import org.checkerframework.checker.units.qual.A;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.time.LocalDateTime;
import java.util.HashMap;
import java.util.Map;
import java.util.Objects;

@Slf4j
@Service("add")
public class Add implements MqttStrategy {
    @Autowired
    private DmpProductService dmpProductService;

    @Autowired
    private DmpDeviceService dmpDeviceService;

    @Autowired
    private DmpDeviceStatusService dmpDeviceStatusService;

    public String disposeMessage(MqttBaseVO mqttBaseVO){
        String[] topics = mqttBaseVO.getTopic().toString().split("/");
        String topic_deviceUUID = topics[3];
        String deviceInfoStr = mqttBaseVO.getData().toString();
        JSONObject deviceInfoJson = JSONObject.parseObject(deviceInfoStr);
        String productCode = deviceInfoJson.get("productCode").toString();
        String deviceId = deviceInfoJson.get("deviceId").toString();
        Integer categoryType = Integer.valueOf(deviceInfoJson.get("categoryType").toString());

        //判断上报数据对应产品是否注册，如未注册则为非法
        Map<String, ProductMapVO> productMapList = dmpProductService.getProductMap();
        if(!productMapList.containsKey(productCode)){
            //通过查询数据库再确认下产品是否注册
            LambdaQueryWrapper<DmpProduct> queryWrapper = Wrappers.lambdaQuery();
            queryWrapper.eq(DmpProduct::getDeleteFlag,0)
                    .eq(DmpProduct::getProductCode,productCode);
            DmpProduct one = dmpProductService.getOne(queryWrapper);
            if(one == null){
                log.error(productCode+" 产品未注册");
                return null;
            }else{
                dmpProductService.deleteProductCache();
            }
        }

        //判断上报数据设备是否已注册(要判断注册过的设备是不是属于本产品的)，未注册自动注册
        Map<String, DeviceMapVO> deviceMapList = dmpProductService.getDeviceMap(productCode);
        if(!deviceMapList.containsKey(deviceId)){
            DmpDevice dmpDeviceInfo = new DmpDevice();
            ProductMapVO productMapVO = productMapList.get(productCode);
            dmpDeviceInfo.setDeviceId(deviceId);
            if(Objects.nonNull(deviceInfoJson.get("deviceName"))){
                dmpDeviceInfo.setDeviceName(deviceInfoJson.get("deviceName").toString());
            }
            if(Objects.nonNull(deviceInfoJson.get("simCode"))){
                dmpDeviceInfo.setSimCode(deviceInfoJson.get("simCode").toString());
            }
            if(Objects.nonNull(deviceInfoJson.get("installAddress"))){
                dmpDeviceInfo.setInstallAddress(deviceInfoJson.get("installAddress").toString());
            }
            dmpDeviceInfo.setDeviceType(productMapVO.getDeviceType());
            dmpDeviceInfo.setProductId(productMapVO.getProductId());
            dmpDeviceInfo.setProductCode(productCode);
            dmpDeviceInfo.setCreatedBy(productMapVO.getCreatedBy());
            dmpDeviceInfo.setCreatedTime(LocalDateTime.now());
            dmpDeviceInfo.setTenantId(productMapVO.getTenantId());
            dmpDeviceInfo.setServiceStatus(1);
            dmpDeviceInfo.setDeviceUuid(deviceInfoJson.get("deviceUuid").toString());
            dmpDeviceInfo.setCategoryType(categoryType);
            if(categoryType == 3){
                dmpDeviceInfo.setGatewayUuid(topic_deviceUUID);
            }
            dmpDeviceService.save(dmpDeviceInfo);

            DmpDeviceStatus dmpDeviceStatus = new DmpDeviceStatus();
            dmpDeviceStatus.setDeviceId(dmpDeviceInfo.getDeviceId());
            dmpDeviceStatus.setProductId(dmpDeviceInfo.getProductId());
            dmpDeviceStatus.setDeviceStatus(2);
            dmpDeviceStatus.setLastOfflineTime(LocalDateTime.now());
            dmpDeviceStatus.setProductCode(dmpDeviceInfo.getProductCode());
            dmpDeviceStatus.setDeviceUuid(dmpDeviceInfo.getDeviceUuid());
            dmpDeviceStatusService.save(dmpDeviceStatus);

            dmpProductService.deleteDeviceCache(productCode);
        }else if(deviceMapList.containsKey(deviceId)){
            LambdaQueryWrapper<DmpDevice> queryWrapper = Wrappers.lambdaQuery();
            queryWrapper.eq(DmpDevice::getDeleteFlag,0)
                    .eq(DmpDevice::getProductCode,productCode)
                    .eq(DmpDevice::getDeviceId,deviceId);
            DmpDevice one = dmpDeviceService.getOne(queryWrapper);
            if(one == null){
                DmpDevice dmpDeviceInfo = new DmpDevice();
                ProductMapVO productMapVO = productMapList.get(productCode);
                dmpDeviceInfo.setDeviceId(deviceId);
                if(Objects.nonNull(deviceInfoJson.get("deviceName"))){
                    dmpDeviceInfo.setDeviceName(deviceInfoJson.get("deviceName").toString());
                }
                if(Objects.nonNull(deviceInfoJson.get("simCode"))){
                    dmpDeviceInfo.setSimCode(deviceInfoJson.get("simCode").toString());
                }
                if(Objects.nonNull(deviceInfoJson.get("installAddress"))){
                    dmpDeviceInfo.setInstallAddress(deviceInfoJson.get("installAddress").toString());
                }
                dmpDeviceInfo.setDeviceType(productMapVO.getDeviceType());
                dmpDeviceInfo.setProductId(productMapVO.getProductId());
                dmpDeviceInfo.setProductCode(productCode);
                dmpDeviceInfo.setCreatedBy(productMapVO.getCreatedBy());
                dmpDeviceInfo.setCreatedTime(LocalDateTime.now());
                dmpDeviceInfo.setTenantId(productMapVO.getTenantId());
                dmpDeviceInfo.setServiceStatus(1);
                dmpDeviceInfo.setDeviceUuid(deviceInfoJson.get("deviceUuid").toString());
                dmpDeviceInfo.setCategoryType(categoryType);
                if(categoryType == 3){
                    dmpDeviceInfo.setGatewayUuid(topic_deviceUUID);
                }
                dmpDeviceService.save(dmpDeviceInfo);

                DmpDeviceStatus dmpDeviceStatus = new DmpDeviceStatus();
                dmpDeviceStatus.setDeviceId(dmpDeviceInfo.getDeviceId());
                dmpDeviceStatus.setProductId(dmpDeviceInfo.getProductId());
                dmpDeviceStatus.setDeviceStatus(2);
                dmpDeviceStatus.setLastOfflineTime(LocalDateTime.now());
                dmpDeviceStatus.setProductCode(dmpDeviceInfo.getProductCode());
                dmpDeviceStatus.setDeviceUuid(dmpDeviceInfo.getDeviceUuid());
                dmpDeviceStatusService.save(dmpDeviceStatus);

                dmpProductService.deleteDeviceCache(productCode);
            }
        }

        return null;
    }
}
