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.proxyclient.ProxyClientOrderOptBecomeInvalidQueryCriteria;
import cn.superdesk.uniorder.dto.receive.consumer.superdeskmall.proxyclient.ProxyClientOrderReceiveParam;
import cn.superdesk.uniorder.dto.test.ExportToJsonPluginDto;
import cn.superdesk.uniorder.dubbo.consumer.order.receive.ReceiveProxyClientOrderDubboConsumerService;
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.*;


@Api(value = "开放API-订单数据接收-代客下单", tags = {"开放API-订单数据接收-代客下单"})
@Slf4j
@RestController
@RequestMapping("/openapi/order/proxy/receive")
public class ProxyClientOrderReceiveController {

	@Autowired
	private ReceiveProxyClientOrderDubboConsumerService receiveProxyClientOrderDubboConsumerService;

	@Autowired
	private FakeSuperdeskMallProducerHandler fakeSuperdeskMallProducerHandler;

	/**
	 * http://192.168.6.223:8089/web/openapi/order/proxy/receive/optBecomeInvalid
	 *
	 * @param queryCriteria
	 * @return
	 */
	@ApiOperation(value = "订单作废")
	@RequestMapping(value = "/optBecomeInvalid", method = RequestMethod.POST)
	@ResponseBody
	public R<String> optBecomeInvalid(@RequestBody ProxyClientOrderOptBecomeInvalidQueryCriteria queryCriteria) {


		receiveProxyClientOrderDubboConsumerService.proxyClientOrderOptBecomeInvalid(queryCriteria);

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


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

				ProxyClientOrderReceiveParam proxyClientOrderReceiveParam = AnJsonUtils.toObject(dataBean.getMessageBody(), ProxyClientOrderReceiveParam.class);
				fakeSuperdeskMallProducerHandler.fakeProducerProxyClientOrder(proxyClientOrderReceiveParam);

			}
//			else if ("proxyclient_order_refund".equals(dataBean.getTopic()) && "refund".equals(dataBean.getMessageKey())) {
//
//				ScoreOrderRefundReceiveParam scoreOrderRefundReceiveParam = JsonUtils.toObject(dataBean.getMessageBody(), ScoreOrderRefundReceiveParam.class);
//				fakeSuperdeskMallProducerHandler.fakeProducerScoreOrderRefund(scoreOrderRefundReceiveParam);
//			}
		});

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