package cn.superdesk.uniorder.controller._openapi.superdeskapp.services.meetingroomreservation;

import cn.superdesk.uniorder.common.json.AnJsonUtils;
import cn.superdesk.uniorder.common.mq.kafka.producer.FakeSuperdeskAppProducerHandler;
import cn.superdesk.uniorder.common.rest.response.R;
import cn.superdesk.uniorder.common.rest.response.ResponseResultInnerUtils;
import cn.superdesk.uniorder.dto.receive.consumer.superdeskappservices.meetingroomreservation.MeetingRoomReservationOrderReceiveParam;
import cn.superdesk.uniorder.dto.test.ExportToJsonPluginDto;
import cn.superdesk.uniorder.dubbo.consumer.order.receive.ReceiveMeetingRoomReservationOrderDubboConsumerService;
import cn.superdesk.uniorder.interceptor.logger.annotation.ControllerLoggerIgnore;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

/**
 * @Description: Java类作用描述
 * @Author: linjinyu
 * @CreateDate: 2021/9/28 上午11:25
 * @UpdateUser: linjinyu
 * @UpdateDate: 2021/9/28 上午11:25
 * @UpdateRemark: 修改内容
 * @Version: 1.0
 */
@Api(value = "开放API-订单数据接收-会议室预订", tags = {"开放API-订单数据接收-会议室预订"})
@Slf4j
@RestController
@RequestMapping("/openapi/order/meetingRoomReservation/receive")
public class MeetingRoomReservationOrderReceiveController {


	@Autowired
	private ReceiveMeetingRoomReservationOrderDubboConsumerService receiveMeetingRoomReservationOrderDubboConsumerService;

	@Autowired
	private FakeSuperdeskAppProducerHandler fakeSuperdeskAppProducerHandler;

	@ApiOperation(value = "HTTP方式接收-订单")
	@RequestMapping(value = "/order", method = RequestMethod.POST)
	@ResponseBody
	public R<String> httpMeetingRoomReservationOrderReceive(@RequestBody MeetingRoomReservationOrderReceiveParam orderReceiveParam) {

		log.info("httpMeetingRoomReservationOrderReceive -> params -> \n{}", AnJsonUtils.toPrettyJson(orderReceiveParam));

		return receiveMeetingRoomReservationOrderDubboConsumerService.httpMeetingRoomReservationOrderMergeReceive(orderReceiveParam);
	}


//	@ApiOperation(value = "HTTP方式接收-退单")
//	@RequestMapping(value = "/order/refund", method = RequestMethod.POST)
//	@ResponseBody
//	public R<String> httpMeetingRoomReservationOrderRefundReceive(@RequestBody MeetingRoomReservationOrderRefundReceiveParam scoreOrderRefundReceiveParam) {
//
//		log.info("httpMeetingRoomReservationOrderRefundReceive -> params -> \n{}", JsonUtils.toPrettyJson(scoreOrderRefundReceiveParam));
//
//		return unifiedOrderDubboConsumerService.httpMeetingRoomReservationOrderRefundReceive(scoreOrderRefundReceiveParam);
//
//	}

	@ApiOperation(value = "HTTP方式接收-转kafka-订单")
	@RequestMapping(value = "/fake/order", method = RequestMethod.POST)
	@ResponseBody
	public R<String> fakeMeetingRoomReservationOrderReceive(@RequestBody MeetingRoomReservationOrderReceiveParam orderReceiveParam) {

		log.info("fakeMeetingRoomReservationOrderReceive -> params -> \n{}", AnJsonUtils.toPrettyJson(orderReceiveParam));

		fakeSuperdeskAppProducerHandler.kafkaMergeReceiveMeetingRoomReservationOrder(orderReceiveParam);

		return new ResponseResultInnerUtils<String>().setData("成功接收-http -> kafka -> producer -> send-不代表 成功处理");

	}

//	@ApiOperation(value = "HTTP方式接收-转kafka-退单")
//	@RequestMapping(value = "/fake/order/refund", method = RequestMethod.POST)
//	@ResponseBody
//	public R<String> fakeMeetingRoomReservationOrderRefundReceive(@RequestBody MeetingRoomReservationOrderRefundReceiveParam scoreOrderRefundReceiveParam) {
//
//		log.info("fakeMeetingRoomReservationOrderRefundReceive -> params -> \n{}", JsonUtils.toPrettyJson(scoreOrderRefundReceiveParam));
//
//		fakeSuperdeskAppProducerHandler.fakeProducerMeetingRoomReservationOrderRefund(scoreOrderRefundReceiveParam);
//
//		return new ResultUtil<String>().setData("成功接收-http -> kafka -> producer -> send-不代表 成功处理");
//
//	}

	/**
	 * http://192.168.6.223:8089/web/openapi/order/score/receive/test/timeline
	 *
	 * @param dto
	 * @return
	 */
	@ApiOperation(value = "测试")
	@RequestMapping(value = "/test/timeline", method = RequestMethod.POST)
	@ResponseBody
	public R<String> test(@RequestBody ExportToJsonPluginDto dto) {

		dto.getData().forEach(dataBean -> {

			log.info("{} {}", dataBean.getMessageKey(), dataBean.getMessageBody());

			if ("order_score_test".equals(dataBean.getTopic()) && "order".equals(dataBean.getMessageKey())) {

				MeetingRoomReservationOrderReceiveParam scoreOrderReceiveParam = AnJsonUtils.toObject(dataBean.getMessageBody(), MeetingRoomReservationOrderReceiveParam.class);

				fakeSuperdeskAppProducerHandler.kafkaMergeReceiveMeetingRoomReservationOrder(scoreOrderReceiveParam);

			} else if ("order_score_refund_test".equals(dataBean.getTopic()) && "refund".equals(dataBean.getMessageKey())) {

//				MeetingRoomReservationOrderRefundReceiveParam scoreOrderRefundReceiveParam = JsonUtils.toObject(dataBean.getMessageBody(), MeetingRoomReservationOrderRefundReceiveParam.class);
//				fakeSuperdeskAppProducerHandler.fakeProducerMeetingRoomReservationOrderRefund(scoreOrderRefundReceiveParam);
			}
		});

		return new ResponseResultInnerUtils<String>().setData("test/timeline");
	}


	@ControllerLoggerIgnore
	@ApiOperation(value = "读取")
	@RequestMapping(value = "/test/timeline/read", method = RequestMethod.POST)
	@ResponseBody
	public R<String> timelineRead(@RequestBody ExportToJsonPluginDto dto) {

		dto.getData().forEach(dataBean -> {

			log.info("{} {}", dataBean.getMessageKey(), dataBean.getMessageBody());

		});

		return new ResponseResultInnerUtils<String>().setData("test/timeline/read");
	}

}
