package cn.superdesk.uniorder.controller._openapi.superdeskapp.organisation;

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.SuperMallSupplierReceiveParam;
import cn.superdesk.uniorder.dto.test.ExportToJsonPluginDto;
import cn.superdesk.uniorder.common.mq.kafka.producer.FakeSuperdeskAppProducerHandler;
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/superdeskapp/organisation/merchant/receive")
public class MerchantReceiveController {

	@Autowired
	private FakeSuperdeskAppProducerHandler fakeSuperdeskAppProducerHandler;

	/**
	 * http://192.168.6.223:8089/web/openapi/superdeskapp/organisation/merchant/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-app-transfer-receive_merchant".equals(dataBean.getTopic()) && "mergeReceiveMerchant".equals(dataBean.getMessageKey())) {
				SuperMallSupplierReceiveParam superMallSupplierReceiveParam = AnJsonUtils.toObject(dataBean.getMessageBody(), SuperMallSupplierReceiveParam.class);
				fakeSuperdeskAppProducerHandler.kafkaMergeReceiveSupperdeskUniServiceSupplier(superMallSupplierReceiveParam);
			}
		});

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