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

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.toolkit.CollectionUtils;
import com.baomidou.mybatisplus.core.toolkit.Wrappers;
import com.usky.common.core.util.BeanMapperUtils;
import com.usky.common.core.util.JsonUtils;
import com.usky.fire.domain.SpOwnerCompany;
import com.usky.fire.domain.TbAlarm;
import com.usky.fire.service.SpOwnerCompanyService;
import com.usky.fire.service.TbAlarmService;
import com.usky.fire.service.TbInfoService;
import com.usky.fire.service.mqtt.MqttStrategy;
import com.usky.fire.service.vo.FireAlarmProperty;
import com.usky.fire.service.vo.FireAlarmVO;
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.util.List;

/**
 * @author yq
 * @date 2021/11/9 17:03
 */
@Service("fireInfoAndAlarm")
public class FireStrategy implements MqttStrategy {
    @Autowired
    private TbAlarmService tbAlarmService;
    @Autowired
    private TbInfoService tbInfoService;
    @Autowired
    private SpOwnerCompanyService spOwnerCompanyService;

    @Override
    public String disposeMessage(MqttBaseVO mqttBaseVO) {
//        LambdaQueryWrapper<SpOwnerCompany> queryWrapper = Wrappers.lambdaQuery();
//        queryWrapper.select(SpOwnerCompany::getOwnerId);
//        List<SpOwnerCompany> list = spOwnerCompanyService.list(queryWrapper);
//        String topic = mqttBaseVO.getTopic();
//        topic = topic.split("/")[3];
//        String finalTopic = topic;
//        list.stream().filter(s -> s.getOwnerId().equals(finalTopic))
//                .findAny().ifPresent(s -> {
//            FireAlarmVO fireAlarmVo = JsonUtils.fromJson(mqttBaseVO.getData().toString(), FireAlarmVO.class);
//            TbAlarm tbAlarm = this.enhanceData(fireAlarmVo);
//            if ("ALARM".equals(fireAlarmVo.getType())) {
//                tbAlarmService.save(tbAlarm);
//
//            } else if ("STATE".equals(fireAlarmVo.getType())) {
////            LambdaUpdateWrapper<DeviceStatus> updateWrapper = Wrappers.lambdaUpdate();
////            updateWrapper.set(DeviceStatus::getDevicestatus,fireAlarmVo.getDevState())
////                    .eq(DeviceStatus::getDeviceid,fireAlarmVo.getDevId());
////            deviceStatusService.update(updateWrapper);
//            }
//        });

        return null;
    }

    /**
     * 增强数据
     *
     * @return
     */
    public TbAlarm enhanceData(FireAlarmVO fireAlarmVO) {
        TbAlarm tbAlarm = new TbAlarm();
        tbAlarm.setCreateTime(LocalDateTime.now());
        tbAlarm.setDeviceId(fireAlarmVO.getDevId());
        tbAlarm.setDeviceName(fireAlarmVO.getDeviceName());
        tbAlarm.setAlarmTime(LocalDateTime.now());
        if ("ALARM".equals(fireAlarmVO.getType())) {
            List<FireAlarmProperty> dp = fireAlarmVO.getAlarams().get(0).getDp();
            if (CollectionUtils.isNotEmpty(dp)) {
                BeanMapperUtils.copy(dp.get(0), tbAlarm);
            }
        } else {
            tbAlarm.setAlarmContent(JsonUtils.toJson(fireAlarmVO));
        }
        return tbAlarm;
    }

    public static void main(String[] args) {
        String topic = "/usky/ytDP0001/+/+/info";

        String[] split = topic.split("/");
        System.out.println(split[3]);
    }
}
