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.flsc.UnifiedFlscOrderReceiveParam;
import cn.superdesk.uniorder.dto.receive.consumer.superdeskmall.unified.flsc.refund.UnifiedFlscRefundOrderReceiveParam;
import cn.superdesk.uniorder.dto.test.ExportToJsonPluginDto;
import cn.superdesk.uniorder.dubbo.consumer.order.receive.ReceiveMallFlscOrderDubboConsumerService;
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/flsc/receive")
public class MallFlscOrderReceiveController {

    @Autowired
    private ReceiveMallFlscOrderDubboConsumerService receiveMallFlscOrderDubboConsumerService;

    @Autowired
    private FakeSuperdeskMallProducerHandler fakeSuperdeskMallProducerHandler;

    @Value(value = "${superdesk.mall.kafka.topic.flsc.order}")
    private String SUPERDESK_MALL_KAFKA_TOPIC_FLSC_ORDER;
    @Value(value = "${superdesk.mall.kafka.topic.flsc.refund}")
    private String SUPERDESK_MALL_KAFKA_TOPIC_FLSC_REFUND;

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

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

        return receiveMallFlscOrderDubboConsumerService.httpUnifiedFlscOrderReceive(scoreOrderReceiveParam);

    }

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

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

        return receiveMallFlscOrderDubboConsumerService.httpUnifiedFlscRefundOrderReceive(scoreOrderRefundReceiveParam);

    }

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

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

        fakeSuperdeskMallProducerHandler.fakeProducerFlscOrder(flscOrderReceiveParam);

        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 UnifiedFlscRefundOrderReceiveParam flscRefundOrderReceiveParam) {

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

        fakeSuperdeskMallProducerHandler.fakeProducerFlscRefundOrder(flscRefundOrderReceiveParam);

        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 = "批量-timeline-HTTP方式接收-转kafka-订单-开发环境")
    @RequestMapping(value = "/test/timeline", method = RequestMethod.POST)
    @ResponseBody
    public R<String> test(@RequestBody ExportToJsonPluginDto dto) {

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

            log.info("\n批量-timeline-HTTP方式接收-转kafka-订单-文本\n{}\n{}", dataBean.getMessageKey(), dataBean.getMessageBody());


            /**
             * # KAFKA TOPIC 福利商城 订单
             * superdesk.mall.kafka.topic.flsc.order=flsc_order
             * # KAFKA TOPIC 福利商城 退单
             * superdesk.mall.kafka.topic.flsc.refund=flsc_refund
             */

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

                UnifiedFlscOrderReceiveParam unifiedFlscOrderReceiveParam = AnJsonUtils.toObject(dataBean.getMessageBody(), UnifiedFlscOrderReceiveParam.class);

                log.info("\n批量-timeline-HTTP方式接收-转kafka-订单-转换\n{}", AnJsonUtils.toJson(unifiedFlscOrderReceiveParam));

                fakeSuperdeskMallProducerHandler.fakeProducerFlscOrder(unifiedFlscOrderReceiveParam);

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

                UnifiedFlscRefundOrderReceiveParam unifiedOrderRefundReceiveParam = AnJsonUtils.toObject(dataBean.getMessageBody(), UnifiedFlscRefundOrderReceiveParam.class);
                fakeSuperdeskMallProducerHandler.fakeProducerFlscRefundOrder(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");
    }
}
