package com.mortals.xhx.busiz.web;

import cn.hutool.core.util.IdUtil;
import com.alibaba.fastjson.JSON;
import com.alibaba.fastjson.JSONObject;
import com.mortals.framework.util.DateUtils;
import com.mortals.xhx.base.system.message.impl.MessageProducer;
import com.mortals.xhx.busiz.req.UploadDeviceReq;
import com.mortals.xhx.busiz.rsp.ApiResp;
import com.mortals.xhx.common.code.ApiRespCodeEnum;
import com.mortals.xhx.common.key.Constant;
import com.mortals.xhx.common.key.QueueKey;
import com.mortals.xhx.common.model.DefaultTbQueueMsgHeaders;
import com.mortals.xhx.common.model.MessageHeader;
import com.mortals.xhx.module.device.model.DeviceEntity;
import com.mortals.xhx.module.device.model.DeviceQuery;
import com.mortals.xhx.module.device.service.DeviceService;
import com.mortals.xhx.queue.DefaultTbQueueMsg;
import com.mortals.xhx.queue.TbQueueMsg;
import com.mortals.xhx.queue.TbQueueMsgHeaders;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import java.util.List;

import static com.mortals.xhx.common.key.Constant.MESSAGETYPE_NOTIFY_REFRESH;

@RestController
@Slf4j
@RequestMapping("/notify")
public class DeviceSendMsgController {
    @Autowired
    private DeviceService deviceService;
    @Autowired
    private MessageProducer messageProducer;

    @PostMapping("refreshMessage")
    public String refreshMessage(@RequestBody UploadDeviceReq req) {
        log.info("【设备通知刷新数据】【请求体】-->  " + JSONObject.toJSONString(req));
        ApiResp<String> rsp = new ApiResp<>();
        rsp.setMsg(ApiRespCodeEnum.SUCCESS.getLabel());
        rsp.setCode(ApiRespCodeEnum.SUCCESS.getValue());
        try {
            //查询站点设备并通知每个设备
            List<DeviceEntity> deviceList = deviceService.find(new DeviceQuery().siteId(req.getSiteId()));
            for (DeviceEntity deviceEntity : deviceList) {
                TbQueueMsgHeaders header = new DefaultTbQueueMsgHeaders();
                header.put(MessageHeader.MESSAGETYPE, MESSAGETYPE_NOTIFY_REFRESH);
                header.put(MessageHeader.DEVICECODE, deviceEntity.getDeviceCode());
                header.put(MessageHeader.TIMESTAMP,  DateUtils.getCurrStrDateTime());

                TbQueueMsg queueMsg = new DefaultTbQueueMsg(IdUtil.fastUUID(), "==", header);
                messageProducer.sendMsg(QueueKey.DEFAULT_EXCHANGE,Constant.DOWN_TOPIC + deviceEntity.getDeviceCode(),JSON.toJSONString(queueMsg));
            }
        } catch (Exception e) {
            log.error("接收数据失败", e);
            rsp.setCode(ApiRespCodeEnum.FAILED.getValue());
            rsp.setMsg(e.getMessage());
            return JSON.toJSONString(rsp);
        }

        log.debug("响应【设备通知刷新数据】【响应体】-->  " + JSONObject.toJSONString(rsp));
        return JSON.toJSONString(rsp);
    }



}
