package com.mortals.xhx.common.utils;

import com.alibaba.fastjson.JSON;
import com.alibaba.fastjson.JSONObject;
import com.alibaba.fastjson.TypeReference;
import com.mortals.framework.util.HttpUtil;
import com.mortals.xhx.base.framework.ws.message.SendToAllRequest;
import com.mortals.xhx.base.framework.ws.util.WebSocketUtil;
import com.mortals.xhx.busiz.req.DeviceReq;
import com.mortals.xhx.busiz.rsp.ApiResp;
import com.mortals.xhx.busiz.rsp.DeviceResp;
import com.mortals.xhx.common.code.DeviceOnlineStatusEnum;
import com.mortals.xhx.module.device.model.DeviceEntity;
import com.mortals.xhx.module.device.model.DeviceLogEntity;
import com.mortals.xhx.module.device.service.DeviceLogService;
import com.mortals.xhx.module.device.service.DeviceService;
import lombok.AllArgsConstructor;
import lombok.extern.slf4j.Slf4j;
import org.springframework.util.ObjectUtils;

import java.util.Date;
import java.util.HashMap;
import java.util.Map;

import static com.mortals.framework.util.HttpUtil.HEADER_CONTENT_TYPE;

/**
 * 上报任务
 *
 * @author: zxfei
 * @date: 2022/4/28 10:56
 * @description:
 **/
@Slf4j
@AllArgsConstructor
public class UploadTask implements Runnable {
    private DeviceReq req;
    private DeviceLogService deviceLogService;
    private DeviceService deviceService;

    @Override
    public void run() {
        try {
            DeviceEntity deviceEntity = deviceService.getExtCache(req.getDevicenum());
            boolean bool = false;
            if (!ObjectUtils.isEmpty(deviceEntity)) {
                if (deviceEntity.getDeviceOnlineStatus() == DeviceOnlineStatusEnum.离线.getValue()) {
                    bool = true;
                }
                deviceEntity.setOnlineTime(new Date());
                deviceEntity.setDeviceOnlineStatus(DeviceOnlineStatusEnum.在线.getValue());
                deviceEntity.setIp(req.getIp());
                deviceEntity.setPort(req.getPort());
                deviceEntity.setSiteNum(req.getSitenum());
                deviceEntity.setSiteName(req.getSitename());
                deviceEntity.setCenternum(req.getCenternum());
                deviceService.update(deviceEntity);

                DeviceLogEntity deviceLogEntity = new DeviceLogEntity();
                deviceLogEntity.initAttrValue();
                deviceLogEntity.setDeviceId(deviceEntity.getId());
                deviceLogEntity.setDeviceName(deviceEntity.getDeviceName());
                deviceLogEntity.setDeviceNum(deviceEntity.getDeviceCode());
                deviceLogEntity.setContent(JSONObject.toJSONString(req));
                deviceLogEntity.setCreateTime(new Date());
                deviceLogService.save(deviceLogEntity);

                if (bool) {
                    WebSocketUtil.broadcast(SendToAllRequest.TYPE, new SendToAllRequest().setContent(JSON.toJSONString(deviceEntity)));
                }

            } else {
                //新增设备
                deviceEntity = new DeviceEntity();
                deviceEntity.initAttrValue();
                deviceEntity.setDeviceCode(req.getDevicenum());
                deviceEntity.setOnlineTime(new Date());
                deviceEntity.setDeviceOnlineStatus(DeviceOnlineStatusEnum.在线.getValue());
                deviceEntity.setIp(req.getIp());
                deviceEntity.setDeviceType(req.getType());
                deviceEntity.setPort(req.getPort());
                deviceEntity.setSiteNum(req.getSitenum());
                deviceEntity.setSiteName(req.getSitename());
                deviceEntity.setCenternum(req.getCenternum());
                deviceEntity.setCreateUserId(1L);
                deviceEntity.setCreateTime(new Date());
                deviceService.save(deviceEntity);
                DeviceLogEntity deviceLogEntity = new DeviceLogEntity();
                deviceLogEntity.initAttrValue();
                deviceLogEntity.setDeviceId(deviceEntity.getId());
                deviceLogEntity.setDeviceName(deviceEntity.getDeviceName());
                deviceLogEntity.setDeviceNum(deviceEntity.getDeviceCode());
                deviceLogEntity.setContent(JSONObject.toJSONString(req));
                deviceLogEntity.setCreateTime(new Date());
                deviceLogService.save(deviceLogEntity);
                WebSocketUtil.broadcast(SendToAllRequest.TYPE, new SendToAllRequest().setContent(JSON.toJSONString(deviceEntity)));
            }
        } catch (Exception e) {
            log.error("异常：" ,e);
        }
    }
}
