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

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.flashdeliver.FlashDeliverOrderReceiveParam;
import cn.superdesk.uniorder.dto.receive.consumer.superdeskappservices.flashdeliver.FlashDeliverOrderStatusUpdateReceiveParam;
import cn.superdesk.uniorder.dto.receive.consumer.superdeskappservices.flashdeliver.FlashDeliverRefundOrderReceiveParam;
import cn.superdesk.uniorder.dto.test.ExportToJsonPluginDto;
import cn.superdesk.uniorder.dubbo.consumer.order.receive.ReceiveFlashDeliverOrderDubboConsumerService;
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: kafka 服务之窗 楼闪送
 * @Author: linjinyu
 * @CreateDate: 2021/5/12 下午5:23
 * @UpdateUser: linjinyu
 * @UpdateDate: 2021/5/12 下午5:23
 * @UpdateRemark: 修改内容
 * @Version: 1.0
 */
@Api(value = "开放API-订单数据接收-楼闪送", tags = {"开放API-订单数据接收-楼闪送"})
@Slf4j
@RestController
@RequestMapping("/openapi/order/flashdeliver/receive")
public class FlashDeliverOrderReceiveController {

	@Autowired
	private ReceiveFlashDeliverOrderDubboConsumerService receiveFlashDeliverOrderDubboConsumerService;

	@Autowired
	private FakeSuperdeskAppProducerHandler fakeSuperdeskAppProducerHandler;

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

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

		return receiveFlashDeliverOrderDubboConsumerService.httpFlashDeliverOrderReceive(orderReceiveParam);
	}


	@ApiOperation(value = "HTTP方式接收-退单")
	@RequestMapping(value = "/order/refund", method = RequestMethod.POST)
	@ResponseBody
	public R<String> httpFlashDeliverOrderRefundReceive(@RequestBody FlashDeliverRefundOrderReceiveParam refundOrderReceiveParam) {

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

		return receiveFlashDeliverOrderDubboConsumerService.httpFlashDeliverOrderRefundReceive(refundOrderReceiveParam);

	}

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

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

		fakeSuperdeskAppProducerHandler.kafkaMergeReceiveFlashDeliverOrder(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> fakeFlashDeliverOrderRefundReceive(@RequestBody FlashDeliverRefundOrderReceiveParam refundOrderReceiveParam) {

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

		fakeSuperdeskAppProducerHandler.kafkaMergeReceiveFlashDeliverOrderRefund(refundOrderReceiveParam);

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

	}

	/**
	 * http://192.168.6.223:8089/web/openapi/order/flashdeliver/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 ("flashdeliver_order".equals(dataBean.getTopic())
				// && "order".equals(dataBean.getMessageKey())
			) {

				FlashDeliverOrderReceiveParam flashDeliverOrderReceiveParam = AnJsonUtils.toObject(dataBean.getMessageBody(), FlashDeliverOrderReceiveParam.class);
				fakeSuperdeskAppProducerHandler.kafkaMergeReceiveFlashDeliverOrder(flashDeliverOrderReceiveParam);

			}
			// 退单
			else if ("flashdeliver_order_refund".equals(dataBean.getTopic())
				// && "order".equals(dataBean.getMessageKey())
			) {

				FlashDeliverRefundOrderReceiveParam flashdeliverOrderRefundReceiveParam = AnJsonUtils.toObject(dataBean.getMessageBody(), FlashDeliverRefundOrderReceiveParam.class);
				fakeSuperdeskAppProducerHandler.kafkaMergeReceiveFlashDeliverOrderRefund(flashdeliverOrderRefundReceiveParam);

			}
			// 订单 状态
			else if ("unifiedorderupdatedev".equals(dataBean.getTopic())
					&& "refund".equals(dataBean.getMessageKey())
			) {

				FlashDeliverOrderStatusUpdateReceiveParam flashdeliverOrderStatusUpdateReceiveParam = AnJsonUtils.toObject(dataBean.getMessageBody(), FlashDeliverOrderStatusUpdateReceiveParam.class);
				fakeSuperdeskAppProducerHandler.kafkaFlashDeliverOrderReceiveStateUpdate(flashdeliverOrderStatusUpdateReceiveParam);
			}
		});

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