package cn.superdesk.uniorder.dubbo.provider.order.receive;

import cn.superdesk.uniorder.common.exception.utils.ExceptionUtils;
import cn.superdesk.uniorder.common.json.AnJsonUtils;
import cn.superdesk.uniorder.common.numerical.AnStringUtils;
import cn.superdesk.uniorder.common.rest.response.R;
import cn.superdesk.uniorder.dto.param.operation.UnifiedOrderIssuesFlowingParam;
import cn.superdesk.uniorder.dto.receive.consumer.superdeskappservices.meetingroomreservation.MeetingRoomReservationOrderReceiveParam;
import cn.superdesk.uniorder.dubbo.facade.order.receive.DubboReceiveMeetingRoomReservationOrderService;
import cn.superdesk.uniorder.dubbo.facade.operation.DubboUnifiedOrderIssuesService;
import cn.superdesk.uniorder.event.CommonEventPublisher;
import cn.superdesk.uniorder.service.order.receive.ReceiveMeetingRoomReservationOrderService;
import com.alibaba.dubbo.config.annotation.Reference;
import com.alibaba.dubbo.config.annotation.Service;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;

/**
 * @Description: Java类作用描述
 * @Author: linjinyu
 * @CreateDate: 2021/11/30 下午4:38
 * @UpdateUser: linjinyu
 * @UpdateDate: 2021/11/30 下午4:38
 * @UpdateRemark: 修改内容
 * @Version: 1.0
 */
@Slf4j
@Service(version = "1.0.0", retries = -1, timeout = 180000)
public class DubboReceiveMeetingRoomReservationOrderServiceImpl implements DubboReceiveMeetingRoomReservationOrderService {

	@Autowired
	private ReceiveMeetingRoomReservationOrderService meetingRoomReservationOrderReceiveKafkaService;

	@Autowired
	private CommonEventPublisher commonEventPublisher;

	/****************************************** 引用自身 Dubbo provider start ******************************************/

	@Reference(version = "1.0.0", timeout = 10000, retries = -1)
	private DubboUnifiedOrderIssuesService dubboUnifiedOrderIssuesService;

	/****************************************** kafka 服务之窗 会议室预约 start ******************************************/

	@Override
	public R<String> httpMeetingRoomReservationOrderMergeReceive(MeetingRoomReservationOrderReceiveParam meetingRoomReservationOrderReceiveParam) {

		R<String> response = new R<>();

		String uid = AnStringUtils.getUUID();
		meetingRoomReservationOrderReceiveParam.setUid(uid);
		dubboUnifiedOrderIssuesService.developCall(AnJsonUtils.toJson(meetingRoomReservationOrderReceiveParam), AnJsonUtils.toJson(meetingRoomReservationOrderReceiveParam), meetingRoomReservationOrderReceiveParam.getOrderSn(), uid);

		UnifiedOrderIssuesFlowingParam callback = meetingRoomReservationOrderReceiveKafkaService.kafkaMeetingRoomReservationOrderMergeReceive(meetingRoomReservationOrderReceiveParam);

		commonEventPublisher.triggerUnifiedOrderDevelopCallBack(callback);

		return response;
	}

	@Override
	public void kafkaMeetingRoomReservationOrderMergeReceive(String message) {

		// TODO 20210927 ...

		try {

			MeetingRoomReservationOrderReceiveParam meetingRoomReservationOrderReceiveParam = AnJsonUtils.toObject(message, MeetingRoomReservationOrderReceiveParam.class);
			log.info("\n转换:\n{}", AnJsonUtils.toJson(meetingRoomReservationOrderReceiveParam));

			// TODO 在这里写吧
			String uid = AnStringUtils.getUUID();
			meetingRoomReservationOrderReceiveParam.setUid(uid);
			dubboUnifiedOrderIssuesService.developCall(message, AnJsonUtils.toJson(meetingRoomReservationOrderReceiveParam), meetingRoomReservationOrderReceiveParam.getOrderSn(), uid);

			UnifiedOrderIssuesFlowingParam callback = meetingRoomReservationOrderReceiveKafkaService.kafkaMeetingRoomReservationOrderMergeReceive(meetingRoomReservationOrderReceiveParam);

			commonEventPublisher.triggerUnifiedOrderDevelopCallBack(callback);

		} catch (Exception e) {
			log.error("kafka 会议室预约订单 kafkaMeetingRoomReservationOrderMergeReceive \n " + ExceptionUtils.getMessage(e));

			e.printStackTrace();
		}
	}

	@Override
	public void kafkaMeetingRoomReservationOrderDeleteReceive(String message) {

		// TODO 20210927 ...

		try {
			meetingRoomReservationOrderReceiveKafkaService.kafkaMeetingRoomReservationOrderDeleteReceive(message);
		} catch (Exception e) {
			log.error("kafka 会议室预约订单 kafkaMeetingRoomReservationOrderDeleteReceive \n " + ExceptionUtils.getMessage(e));

			e.printStackTrace();
		}
	}

	/****************************************** kafka 服务之窗 会议室预约 start ******************************************/
}
