package com.xueyi.system.emcs.service;

import com.alibaba.fastjson2.JSONObject;
import com.baomidou.mybatisplus.core.toolkit.Wrappers;
import com.xueyi.common.core.constant.basic.SqlConstants;
import com.xueyi.common.mqtt.service.MqttMessageHandler;
import com.xueyi.common.web.utils.DateUtils;
import com.xueyi.system.api.digitalmans.domain.po.DmManDevicePo;
import com.xueyi.system.digitalmans.mapper.DmManDeviceMapper;
import com.xueyi.system.emcs.constant.EmcsUploadType;
import com.xueyi.system.emcs.domain.dto.DmDeviceLogFileDto;
import com.xueyi.system.emcs.domain.dto.DmExceptionLogDto;
import com.xueyi.system.emcs.mapper.DmDeviceLogFileMapper;
import com.xueyi.system.emcs.mapper.DmExceptionLogMapper;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.text.ParseException;

/**
 *
 * @author yk
 * @description 处理订阅到的mqtt设备异常记录
 * @date 2023-05-10 19:08
 */
@Service
public class LogUploadMessageHandler implements MqttMessageHandler {

    @Autowired
    private DmManDeviceMapper manDeviceMapper;

    @Autowired
    private DmExceptionLogMapper exceptionLogMapper;

    @Autowired
    private DmDeviceLogFileMapper deviceLogFileMapper;

    @Override
    public void handleMessage(String topic, String message) {
        System.out.println("发送日志到主题：" + topic + "，消息：" + message);
        JSONObject heartBeatObj = JSONObject.parseObject(message);
        String devId = heartBeatObj.getString("devId");
        String timestamp = heartBeatObj.getString("timestamp");
        String fileName = heartBeatObj.getString("file_name");
        String path = heartBeatObj.getString("path");
        String type = heartBeatObj.getString("type");
        Integer level = heartBeatObj.getInteger("level");
        Integer uploadType = heartBeatObj.getInteger("upload_type");


        DmExceptionLogDto exceptionLogDto = new DmExceptionLogDto();
        exceptionLogDto.setType(type);
        exceptionLogDto.setLevel(level);
        exceptionLogDto.setDevId(devId);

        DmManDevicePo devicePo = manDeviceMapper.selectOne(Wrappers.<DmManDevicePo>lambdaQuery().eq(DmManDevicePo::getDeviceId, devId).last(SqlConstants.LIMIT_ONE));
        try {
            if (null!= devicePo) {
                exceptionLogDto.setManCode(devicePo.getManCode());
                exceptionLogDto.setTId(devicePo.getTId());

                exceptionLogDto.setOccurTime(DateUtils.dateToLocalDateTime(DateUtils.parseLongToDate(timestamp)));
                // 上传类型为崩溃上传时，插入异常日志
                if (uploadType == EmcsUploadType.getTypeCrackUpload()) {
                    exceptionLogMapper.insert(exceptionLogDto);
                }

                //DmExceptionLogDto 持久化
                DmDeviceLogFileDto deviceLogFileDto = new DmDeviceLogFileDto();
                deviceLogFileDto.setManDeviceId(devicePo.getId());
                deviceLogFileDto.setUrl(path);
                deviceLogFileDto.setType(uploadType);
                deviceLogFileDto.setName(fileName);

                deviceLogFileMapper.insert(deviceLogFileDto);

            } else {
                return;
            }
        } catch (ParseException e) {
            throw new RuntimeException(e);
        }



    }
}
