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

import cn.superdesk.uniorder.common.json.AnJsonUtils;
import cn.superdesk.uniorder.common.rest.response.R;
import cn.superdesk.uniorder.common.rest.response.ResponseResultInnerUtils;
import cn.superdesk.uniorder.dto.receive.consumer.superdeskmall.score.ScoreOrderReceiveParam;
import cn.superdesk.uniorder.dto.receive.consumer.superdeskmall.score.refund.ScoreOrderRefundReceiveParam;
import cn.superdesk.uniorder.dto.test.ExportToJsonPluginDto;
import cn.superdesk.uniorder.dubbo.consumer.order.receive.ReceiveMallFlscQchOrderDubboConsumerService;
import cn.superdesk.uniorder.interceptor.logger.annotation.ControllerLoggerIgnore;
import cn.superdesk.uniorder.common.mq.kafka.producer.FakeSuperdeskMallProducerHandler;
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.beans.factory.annotation.Value;
import org.springframework.web.bind.annotation.*;

/**
 * @Description: Java类作用描述
 * @Author: linjinyu
 * @CreateDate: 2021/3/8 上午11:17
 * @UpdateUser: linjinyu
 * @UpdateDate: 2021/3/8 上午11:17
 * @UpdateRemark: 修改内容
 * @Version: 1.0
 */
@Api(value = "开放API-订单数据接收-侨城汇", tags = {"开放API-订单数据接收-侨城汇"})
@Slf4j
@RestController
@RequestMapping("/openapi/order/score/receive")
public class MallFlscQchOrderReceiveController {


	@Autowired
	private ReceiveMallFlscQchOrderDubboConsumerService receiveMallFlscQchOrderDubboConsumerService;

	@Autowired
	private FakeSuperdeskMallProducerHandler fakeSuperdeskMallProducerHandler;

	@Value(value = "${superdesk.mall.kafka.topic.flsc_qch.order}")
	private String SUPERDESK_MALL_KAFKA_TOPIC_FLSC_QCH_ORDER;
	@Value(value = "${superdesk.mall.kafka.topic.flsc_qch.refund}")
	private String SUPERDESK_MALL_KAFKA_TOPIC_FLSC_QCH_REFUND;

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

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

		return receiveMallFlscQchOrderDubboConsumerService.httpScoreOrderReceive(scoreOrderReceiveParam);

	}

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

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

		return receiveMallFlscQchOrderDubboConsumerService.httpScoreOrderRefundReceive(scoreOrderRefundReceiveParam);

	}

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

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

		fakeSuperdeskMallProducerHandler.fakeProducerMallFlscQchOrder(scoreOrderReceiveParam);

		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> fakeScoreOrderRefundReceive(@RequestBody ScoreOrderRefundReceiveParam scoreOrderRefundReceiveParam) {

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

		fakeSuperdeskMallProducerHandler.fakeProducerMallFlscQchRefundOrder(scoreOrderRefundReceiveParam);

		return new ResponseResultInnerUtils<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 (SUPERDESK_MALL_KAFKA_TOPIC_FLSC_QCH_ORDER.equals(dataBean.getTopic()) && "order".equals(dataBean.getMessageKey())) {

				ScoreOrderReceiveParam scoreOrderReceiveParam = AnJsonUtils.toObject(dataBean.getMessageBody(), ScoreOrderReceiveParam.class);
				fakeSuperdeskMallProducerHandler.fakeProducerMallFlscQchOrder(scoreOrderReceiveParam);

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

				ScoreOrderRefundReceiveParam scoreOrderRefundReceiveParam = AnJsonUtils.toObject(dataBean.getMessageBody(), ScoreOrderRefundReceiveParam.class);
				fakeSuperdeskMallProducerHandler.fakeProducerMallFlscQchRefundOrder(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");
	}

}
