package org.springblade.device.feign;

import com.alibaba.fastjson.JSON;
import com.alibaba.fastjson.serializer.SerializerFeature;
import com.baomidou.mybatisplus.core.toolkit.IdWorker;
import com.rabbitmq.client.Channel;
import com.rabbitmq.client.Connection;
import lombok.AllArgsConstructor;
import lombok.SneakyThrows;
import org.springblade.device.dto.MsgPublishDto;
import org.springblade.device.util.RabbitMqConnectionUtil;
import org.springblade.third.doordevice.entity.InstructPublishLog;
import org.springblade.third.doordevice.entity.MqConf;
import org.springblade.third.doordevice.feign.IInstructPublishLogClient;
import org.springblade.third.doordevice.feign.IMqConfClient;
import org.springframework.web.bind.annotation.*;

import java.nio.charset.StandardCharsets;
import java.util.HashMap;
import java.util.Map;

/**
 *  client
 *
 * @author lidexi
 * @since 2021-04-19
 */

@AllArgsConstructor
@RestController
@RequestMapping("/feign/msgpublish")

public class MsgPublishClient implements IMsgPublishClient {

	private final IMqConfClient mqConfClient;
	private final IInstructPublishLogClient instructPublishLogClient;


	@Override
	@PostMapping("/publish")
	@SneakyThrows
	public void publish(@RequestBody MsgPublishDto msgPublishDto) {
		MqConf searchEntity = new MqConf();
		searchEntity.setId(null);
		MqConf conf = this.mqConfClient.detail(searchEntity);
		if (null == conf){
			throw new Exception("设备未绑定队列");
		}
		//  获取队列连接
		Connection connection = RabbitMqConnectionUtil.openConnection(conf);
		Channel channel = null;
		try {
			//  创建管道
			channel = connection.createChannel();
			long id = IdWorker.getId();
			//  构建数据
			Map<String,Object> map = new HashMap<>(3);
			map.put("id",id);
			map.put("code",msgPublishDto.getInstruct().getCode());
			map.put("data",msgPublishDto.getObject());
			map.put("serialNumber",msgPublishDto.getDoorDevice().getSerialNum());
			String msg = JSON.toJSONString(map, SerializerFeature.WriteNonStringValueAsString, SerializerFeature.WriteDateUseDateFormat);
//            String msg = JsonUtils.objectToJson(map);
			//  推送消息
			channel.basicPublish(msgPublishDto.getInstruct().getExchangeName(), msgPublishDto.getDoorDevice().getSerialNum(), null, msg.getBytes(StandardCharsets.UTF_8));


			//  保存指令推送日志
//            InstructPublishLog publishLog = new InstructPublishLog(id,device, instruct, JsonUtils.objectToJson(obj), m);
			InstructPublishLog publishLog = new InstructPublishLog(id, msgPublishDto.getDoorDevice(), msgPublishDto.getInstruct(), JSON.toJSONString(msgPublishDto.getObject(), SerializerFeature.WriteNonStringValueAsString, SerializerFeature.WriteDateUseDateFormat));
			this.instructPublishLogClient.save(publishLog);
		} finally {
			//  关闭连接,抛出异常
			RabbitMqConnectionUtil.closeConnection(connection,channel);
		}
	}

	@Override
	@PostMapping("/publishJVT")
	public void publishJVT(@RequestBody MsgPublishDto msgPublishDto) {
		MqConf searchEntity = new MqConf();
		searchEntity.setName("巨龙队列");
		MqConf mqConf = this.mqConfClient.detail(searchEntity);
		Connection connection = RabbitMqConnectionUtil.openConnection(mqConf);
		Channel channel = null;
		try {
			//  创建管道
			channel = connection.createChannel();
			//  构建数据
			Map<String,Object> map = new HashMap<>(2);
			map.put("id","10086");
			map.put("code",msgPublishDto.getInstruct().getCode());
			map.put("data",msgPublishDto.getObject());
			map.put("serialNumber", msgPublishDto.getMac());
			String msg = JSON.toJSONString(map, SerializerFeature.WriteNonStringValueAsString, SerializerFeature.WriteDateUseDateFormat);
			channel.basicPublish(msgPublishDto.getInstruct().getExchangeName(), msgPublishDto.getMac(), null, msg.getBytes(StandardCharsets.UTF_8));
		}catch (Exception e){

		}finally {
			//  关闭连接,抛出异常
			RabbitMqConnectionUtil.closeConnection(connection,channel);
		}
	}
}
