package com.mortals.xhx.busiz.web;

import com.alibaba.fastjson.JSON;
import com.alibaba.fastjson.JSONObject;
import com.mortals.framework.exception.AppException;
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.common.code.ApiRespCodeEnum;
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.extern.apachecommons.CommonsLog;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.util.ObjectUtils;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import java.util.Date;

/**
 * 设备
 *
 * @author: zxfei
 * @date: 2021/8/24 20:28
 */
@RestController
@CommonsLog
@RequestMapping("/api/device")
public class DeviceApiController {

    @Autowired
    private DeviceLogService deviceLogService;
    @Autowired
    private DeviceService deviceService;


    /**
     * 设备数据上报
     *
     * @param req
     * @return
     */
    @PostMapping("upload")
    public String upload(DeviceReq req) {
        log.info("【设备数据上报】【请求体】-->  " + JSONObject.toJSONString(req));
        ApiResp rsp = new ApiResp<>();
        rsp.setMsg(ApiRespCodeEnum.SUCCESS.getLabel());
        rsp.setCode(ApiRespCodeEnum.SUCCESS.getValue());
        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.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.setPort(req.getPort());
                deviceEntity.setSiteNum(req.getSitenum());
                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);
            rsp.setCode(ApiRespCodeEnum.FAILED.getValue());
            rsp.setMsg(e.getMessage());
            return JSON.toJSONString(rsp);
        }

        log.info("响应【设备数据上报】【响应体】-->  " + JSONObject.toJSONString(rsp));
        return JSON.toJSONString(rsp);
    }


}
