package com.usky.fire.service.mqtt.info;

import com.alibaba.fastjson.JSONObject;
import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.toolkit.CollectionUtils;
import com.baomidou.mybatisplus.core.toolkit.StringUtils;
import com.baomidou.mybatisplus.core.toolkit.Wrappers;
import com.usky.common.core.exception.BusinessException;
import com.usky.common.core.util.JsonUtils;
import com.usky.fire.domain.*;
import com.usky.fire.service.*;
import com.usky.fire.service.mqtt.MqttStrategy;
import com.usky.fire.service.util.OnlineMethod;
import com.usky.fire.service.vo.MqttBaseVO;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.time.LocalDateTime;
import java.time.ZoneOffset;
import java.util.ArrayList;
import java.util.List;
import java.util.Map;

/**
 * @author zyj
 * @date 2022/12/6 15:07
 */
@Service("info")
public class Info implements MqttStrategy {

    @Autowired
    private BaseDeviceService baseDeviceService;

    @Autowired
    private BaseDeviceAttributeService baseDeviceAttributeService;

    @Autowired
    private BaseCompanyService baseCompanyService;

    @Autowired
    private DataRtService dataRtService;

    @Override
    public String disposeMessage(MqttBaseVO mqttBaseVO) {
        Map map = JsonUtils.fromJson(mqttBaseVO.getData().toString(), Map.class);
        List<Map<String, Object>> devs = (List) JSONObject.parseArray(JSONObject.toJSONString(map.get("devs")));
        List<Map<String, Object>> dp = (List) JSONObject.parseArray(JSONObject.toJSONString(devs.get(0).get("dp")));
        LambdaQueryWrapper<BaseDevice> queryWrapper1 = Wrappers.lambdaQuery();
        queryWrapper1.select(BaseDevice::getCompanyId, BaseDevice::getDeviceType)
                .eq(BaseDevice::getDeviceCode, map.get("devId").toString())
                .eq(BaseDevice::getEnable, 1);
        List<BaseDevice> list = baseDeviceService.list(queryWrapper1);
        List<BaseDeviceAttribute> list1 = new ArrayList<>();
        List<BaseCompany> list2 = new ArrayList<>();
        if (CollectionUtils.isNotEmpty(list)) {
            LambdaQueryWrapper<BaseDeviceAttribute> queryWrapper = Wrappers.lambdaQuery();
            queryWrapper.select(BaseDeviceAttribute::getSystemType)
                    .eq(BaseDeviceAttribute::getDeviceCode, map.get("devId").toString());
            list1 = baseDeviceAttributeService.list(queryWrapper);

            LambdaQueryWrapper<BaseCompany> queryWrapper2 = Wrappers.lambdaQuery();
            queryWrapper2.select(BaseCompany::getCompanyName).
                    eq(BaseCompany::getCompanyId, list.get(0).getCompanyId());
            list2 = baseCompanyService.list(queryWrapper2);
        }

        if (CollectionUtils.isNotEmpty(list) && CollectionUtils.isNotEmpty(list1) && CollectionUtils.isNotEmpty(devs) && CollectionUtils.isNotEmpty(dp)) {
            DataRt dataRt = new DataRt();
            String timestamp1;

            switch (list1.get(0).getSystemType()) {
                case 1:
                    for(int i=0;i<dp.size();i++){
                        dataRt.setDeviceCode(map.get("devId").toString());
                        dataRt.setDeviceName(map.get("deviceName").toString());
                        timestamp1 = map.get("timeStamp").toString();
                        if (StringUtils.isNotBlank(timestamp1)) {
                            long timestamp = Long.valueOf(timestamp1).longValue();
                            LocalDateTime time2 = LocalDateTime.ofEpochSecond(timestamp, 0, ZoneOffset.ofHours(8));
                            dataRt.setDataTime(time2);
                        }
                        dataRt.setSystemType(1);
                        dataRt.setCompanyId(list.get(0).getCompanyId());
                        if (CollectionUtils.isNotEmpty(list2)) {
                            dataRt.setCompanyName(list2.get(0).getCompanyName());
                        }
                        dataRt.setPorts(null);
                        dataRt.setPortCode("");
                        dataRt.setPortName("");
                        dataRt.setPortData(dp.get(i).get("value").toString());
                        dataRt.setDeviceType(list.get(0).getDeviceType());
                        dataRt.setContent("");


                        List<DataRt> list3 = new ArrayList<>();
                        LambdaQueryWrapper<DataRt> queryWrapper3 = Wrappers.lambdaQuery();
                        queryWrapper3.select(DataRt::getId).
                                eq(DataRt::getDeviceCode, map.get("devId").toString());
                        list3 = dataRtService.list(queryWrapper3);
                        if (CollectionUtils.isNotEmpty(list3)){
                            dataRt.setId(list3.get(0).getId());
                            dataRtService.updateById(dataRt);
                        }else{
                            dataRtService.save(dataRt);
                        }
                    }
                    break;
                case 2:
                    for(int i=0;i<dp.size();i++){
                        dataRt.setDeviceCode(map.get("devId").toString());
                        dataRt.setDeviceName(map.get("deviceName").toString());
                        timestamp1 = map.get("timeStamp").toString();
                        if (StringUtils.isNotBlank(timestamp1)) {
                            long timestamp = Long.valueOf(timestamp1).longValue();
                            LocalDateTime time2 = LocalDateTime.ofEpochSecond(timestamp, 0, ZoneOffset.ofHours(8));
                            dataRt.setDataTime(time2);
                        }
                        dataRt.setSystemType(2);
                        dataRt.setCompanyId(list.get(0).getCompanyId());
                        if (CollectionUtils.isNotEmpty(list2)) {
                            dataRt.setCompanyName(list2.get(0).getCompanyName());
                        }
                        dataRt.setPorts(null);
                        dataRt.setPortCode(dp.get(i).get("serial").toString());
                        dataRt.setPortName(dp.get(i).get("property").toString());
                        dataRt.setPortData(dp.get(i).get("value").toString());
                        dataRt.setDeviceType(list.get(0).getDeviceType());
                        dataRt.setContent("");

                        List<DataRt> list3 = new ArrayList<>();
                        LambdaQueryWrapper<DataRt> queryWrapper3 = Wrappers.lambdaQuery();
                        queryWrapper3.select(DataRt::getId).
                                eq(DataRt::getDeviceCode, map.get("devId").toString())
                                .eq(DataRt::getPortCode, dp.get(i).get("serial").toString());
                        list3 = dataRtService.list(queryWrapper3);
                        if (CollectionUtils.isNotEmpty(list3)){
                            dataRt.setId(list3.get(0).getId());
                            dataRtService.updateById(dataRt);
                        }else{
                            dataRtService.save(dataRt);
                        }
                    }
                    break;
                case 3:
                    for(int i=0;i<dp.size();i++){
                        dataRt.setDeviceCode(map.get("devId").toString());
                        dataRt.setDeviceName(map.get("deviceName").toString());
                        timestamp1 = map.get("timeStamp").toString();
                        if (StringUtils.isNotBlank(timestamp1)) {
                            long timestamp = Long.valueOf(timestamp1).longValue();
                            LocalDateTime time2 = LocalDateTime.ofEpochSecond(timestamp, 0, ZoneOffset.ofHours(8));
                            dataRt.setDataTime(time2);
                        }
                        dataRt.setSystemType(3);
                        dataRt.setCompanyId(list.get(0).getCompanyId());
                        if (CollectionUtils.isNotEmpty(list2)) {
                            dataRt.setCompanyName(list2.get(0).getCompanyName());
                        }
                        dataRt.setPorts(null);
                        dataRt.setPortCode(dp.get(i).get("serial").toString());
                        dataRt.setPortName(dp.get(i).get("property").toString());
                        dataRt.setPortData(dp.get(i).get("value").toString());
                        dataRt.setDeviceType(list.get(0).getDeviceType());
                        dataRt.setContent("");
                        List<DataRt> list3 = new ArrayList<>();
                        LambdaQueryWrapper<DataRt> queryWrapper3 = Wrappers.lambdaQuery();
                        queryWrapper3.select(DataRt::getId).
                                eq(DataRt::getDeviceCode, map.get("devId").toString())
                                .eq(DataRt::getPortCode, dp.get(i).get("serial").toString());
                        list3 = dataRtService.list(queryWrapper3);
                        if (CollectionUtils.isNotEmpty(list3)){
                            dataRt.setId(list3.get(0).getId());
                            dataRtService.updateById(dataRt);
                        }else{
                            dataRtService.save(dataRt);
                        }
                    }
                    break;
                case 4:
                    for(int i=0;i<dp.size();i++){
                        dataRt.setDeviceCode(map.get("devId").toString());
                        dataRt.setDeviceName(map.get("deviceName").toString());
                        timestamp1 = map.get("timeStamp").toString();
                        if (StringUtils.isNotBlank(timestamp1)) {
                            long timestamp = Long.valueOf(timestamp1).longValue();
                            LocalDateTime time2 = LocalDateTime.ofEpochSecond(timestamp, 0, ZoneOffset.ofHours(8));
                            dataRt.setDataTime(time2);
                        }
                        dataRt.setSystemType(4);
                        dataRt.setCompanyId(list.get(0).getCompanyId());
                        if (CollectionUtils.isNotEmpty(list2)) {
                            dataRt.setCompanyName(list2.get(0).getCompanyName());
                        }
                        dataRt.setPorts(null);
                        dataRt.setPortCode(dp.get(i).get("serial").toString());
                        dataRt.setPortName(dp.get(i).get("property").toString());
                        dataRt.setPortData(dp.get(i).get("value").toString());
                        dataRt.setDeviceType(list.get(0).getDeviceType());
                        dataRt.setContent("");
                        List<DataRt> list3 = new ArrayList<>();
                        LambdaQueryWrapper<DataRt> queryWrapper3 = Wrappers.lambdaQuery();
                        queryWrapper3.select(DataRt::getId).
                                eq(DataRt::getDeviceCode, map.get("devId").toString())
                                .eq(DataRt::getPortCode, dp.get(i).get("serial").toString());
                        list3 = dataRtService.list(queryWrapper3);
                        if (CollectionUtils.isNotEmpty(list3)){
                            dataRt.setId(list3.get(0).getId());
                            dataRtService.updateById(dataRt);
                        }else{
                            dataRtService.save(dataRt);
                        }
                    }

                    break;
                case 5:
                    for(int i=0;i<dp.size()+7;i++){
                        dataRt.setDeviceCode(map.get("devId").toString());
                        dataRt.setDeviceName(map.get("deviceName").toString());
                        timestamp1 = map.get("timeStamp").toString();
                        if (StringUtils.isNotBlank(timestamp1)) {
                            long timestamp = Long.valueOf(timestamp1).longValue();
                            LocalDateTime time2 = LocalDateTime.ofEpochSecond(timestamp, 0, ZoneOffset.ofHours(8));
                            dataRt.setDataTime(time2);
                        }
                        dataRt.setSystemType(5);
                        dataRt.setCompanyId(list.get(0).getCompanyId());
                        if (CollectionUtils.isNotEmpty(list2)) {
                            dataRt.setCompanyName(list2.get(0).getCompanyName());
                        }
                        List<DataRt> list3 = new ArrayList<>();

                        if(i>=0 && i<7){
                            dataRt.setPorts(i+1);
                            dataRt.setPortCode((i+1)+"");
                            if(i==0){
                                dataRt.setPortName("供电过压");
                            }else if(i==1){
                                dataRt.setPortName("供电低压");
                            }else if(i==2){
                                dataRt.setPortName("供电过流");
                            }else if(i==3){
                                dataRt.setPortName("漏电报警");
                            }else if(i==4){
                                dataRt.setPortName("A相线缆温度超高");
                            }else if(i==5){
                                dataRt.setPortName("B相线缆温度超高");
                            }else if(i==6){
                                dataRt.setPortName("C相线缆温度超高");
                            }
                            dataRt.setPortData("0");
                           LambdaQueryWrapper<DataRt> queryWrapper3 = Wrappers.lambdaQuery();
                            queryWrapper3.select(DataRt::getId).
                                    eq(DataRt::getDeviceCode, map.get("devId").toString())
                                    .eq(DataRt::getPortCode, String.valueOf(i+1));
                            list3 = dataRtService.list(queryWrapper3);

                        }else{
                            dataRt.setPorts(Integer.parseInt(dp.get(i-7).get("serial").toString()));
                            dataRt.setPortCode(dp.get(i-7).get("serial").toString());
                            dataRt.setPortName(dp.get(i-7).get("property").toString());
                            dataRt.setPortData(dp.get(i-7).get("value").toString());

                            LambdaQueryWrapper<DataRt> queryWrapper3 = Wrappers.lambdaQuery();
                            queryWrapper3.select(DataRt::getId).
                                    eq(DataRt::getDeviceCode, map.get("devId").toString())
                                    .eq(DataRt::getPortCode, dp.get(i-7).get("serial").toString());
                            list3 = dataRtService.list(queryWrapper3);
                        }
                        dataRt.setDeviceType(list.get(0).getDeviceType());
                        dataRt.setContent("");
                        if (CollectionUtils.isNotEmpty(list3)){
                            dataRt.setId(list3.get(0).getId());
                            dataRtService.updateById(dataRt);
                        }else{
                            dataRtService.save(dataRt);
                        }
                    }

                    break;
                case 6:
                    for(int i=0;i<dp.size();i++){
                        dataRt.setDeviceCode(map.get("devId").toString());
                        dataRt.setDeviceName(map.get("deviceName").toString());
                        timestamp1 = map.get("timeStamp").toString();
                        if (StringUtils.isNotBlank(timestamp1)) {
                            long timestamp = Long.valueOf(timestamp1).longValue();
                            LocalDateTime time2 = LocalDateTime.ofEpochSecond(timestamp, 0, ZoneOffset.ofHours(8));
                            dataRt.setDataTime(time2);
                        }
                        dataRt.setSystemType(6);
                        dataRt.setCompanyId(list.get(0).getCompanyId());
                        if (CollectionUtils.isNotEmpty(list2)) {
                            dataRt.setCompanyName(list2.get(0).getCompanyName());
                        }
                        dataRt.setPorts(null);
                        dataRt.setPortCode(dp.get(i).get("serial").toString());
                        dataRt.setPortName(dp.get(i).get("property").toString());
                        dataRt.setPortData(dp.get(i).get("value").toString());
                        dataRt.setDeviceType(list.get(0).getDeviceType());
                        dataRt.setContent("");
                        List<DataRt> list3 = new ArrayList<>();
                        LambdaQueryWrapper<DataRt> queryWrapper3 = Wrappers.lambdaQuery();
                        queryWrapper3.select(DataRt::getId).
                                eq(DataRt::getDeviceCode, map.get("devId").toString())
                                .eq(DataRt::getPortCode, dp.get(i).get("serial").toString());
                        list3 = dataRtService.list(queryWrapper3);
                        if (CollectionUtils.isNotEmpty(list3)){
                            dataRt.setId(list3.get(0).getId());
                            dataRtService.updateById(dataRt);
                        }else{
                            dataRtService.save(dataRt);
                        }
                    }

                    break;
                default:
                    throw new BusinessException("未定义类型");
            }
        }
        return null;
    }


}
