package cn.superdesk.uniorder.controller._openapi.superdeskmall.order;

import cn.superdesk.uniorder.common.json.AnJsonUtils;
import cn.superdesk.uniorder.common.mq.kafka.producer.FakeSuperdeskMallProducerHandler;
import cn.superdesk.uniorder.common.rest.response.R;
import cn.superdesk.uniorder.common.rest.response.ResponseResultInnerUtils;
import cn.superdesk.uniorder.dto.receive.consumer.superdeskmall.unified.mix.UnifiedMixOrderReceiveParam;
import cn.superdesk.uniorder.dto.receive.consumer.superdeskmall.unified.mix.refund.UnifiedMixRefundOrderReceiveParam;
import cn.superdesk.uniorder.dto.test.ExportToJsonPluginDto;
import cn.superdesk.uniorder.dubbo.consumer.order.receive.ReceiveMallMixOrderDubboConsumerService;
import cn.superdesk.uniorder.interceptor.logger.annotation.ControllerLoggerIgnore;
import io.swagger.annotations.ApiOperation;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.web.bind.annotation.*;

/**
 * @Description: Java类作用描述
 * @Author: linjinyu
 * @CreateDate: 2021/4/7 下午8:09
 * @UpdateUser: linjinyu
 * @UpdateDate: 2021/4/7 下午8:09
 * @UpdateRemark: 修改内容
 * @Version: 1.0
 */
@Slf4j
@RestController
@RequestMapping("/openapi/order/mix/receive")
public class MallMixOrderReceiveController {

	@Autowired
	private ReceiveMallMixOrderDubboConsumerService receiveMallMixOrderDubboConsumerService;

	@Autowired
	private FakeSuperdeskMallProducerHandler fakeSuperdeskMallProducerHandler;

	@Value(value = "${superdesk.mall.kafka.topic.mix.order}")
	private String SUPERDESK_MALL_KAFKA_TOPIC_MIX_ORDER;
	@Value(value = "${superdesk.mall.kafka.topic.mix.refund}")
	private String SUPERDESK_MALL_KAFKA_TOPIC_MIX_REFUND;

	/**
	 * @param scoreOrderReceiveParam
	 * @return
	 */
	@ApiOperation(value = "HTTP方式接收-订单")
	@RequestMapping(value = "/order", method = RequestMethod.POST)
	@ResponseBody
	public R<String> httpMixOrderReceive(@RequestBody UnifiedMixOrderReceiveParam scoreOrderReceiveParam) {

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

		return receiveMallMixOrderDubboConsumerService.httpUnifiedMixOrderReceive(scoreOrderReceiveParam);

	}

	/**
	 * @param scoreOrderRefundReceiveParam
	 * @return
	 */
	@ApiOperation(value = "HTTP方式接收-退单")
	@RequestMapping(value = "/order/refund", method = RequestMethod.POST)
	@ResponseBody
	public R<String> httpMixRefundOrderReceive(@RequestBody UnifiedMixRefundOrderReceiveParam scoreOrderRefundReceiveParam) {

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

		return receiveMallMixOrderDubboConsumerService.httpUnifiedMixRefundOrderReceive(scoreOrderRefundReceiveParam);

	}

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

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

		fakeSuperdeskMallProducerHandler.fakeProducerMixOrder(mixOrderReceiveParam);

		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> fakeUnifiedOrderRefundReceive(@RequestBody UnifiedMixRefundOrderReceiveParam mixRefundOrderReceiveParam) {

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

		fakeSuperdeskMallProducerHandler.fakeProducerMixRefundOrder(mixRefundOrderReceiveParam);

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

	}

	/**
	 * https://order.api.avic-s.com/web/openapi/order/unified/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 ((SUPERDESK_MALL_KAFKA_TOPIC_MIX_ORDER.equals(dataBean.getTopic())) && "order".equals(dataBean.getMessageKey())) {

				UnifiedMixOrderReceiveParam scoreOrderReceiveParam = AnJsonUtils.toObject(dataBean.getMessageBody(), UnifiedMixOrderReceiveParam.class);
				fakeSuperdeskMallProducerHandler.fakeProducerMixOrder(scoreOrderReceiveParam);

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

				UnifiedMixRefundOrderReceiveParam unifiedOrderRefundReceiveParam = AnJsonUtils.toObject(dataBean.getMessageBody(), UnifiedMixRefundOrderReceiveParam.class);
				fakeSuperdeskMallProducerHandler.fakeProducerMixRefundOrder(unifiedOrderRefundReceiveParam);

			}
		});

		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");
	}
}
