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.dao.entity.organisation.SysClique;
import cn.superdesk.uniorder.dao.entity.organisation.SysCliqueOrg;
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.*;


/**
 * @Description: Java类作用描述
 * @Author: linjinyu
 * @CreateDate: 2021/5/7 下午4:02
 * @UpdateUser: linjinyu
 * @UpdateDate: 2021/5/7 下午4:02
 * @UpdateRemark: 修改内容
 * @Version: 1.0
 */
@Api(value = "[开放API]-服务之窗-集团" ,tags = "[开放API]-服务之窗-集团")
@Slf4j
@RestController
@RequestMapping("/openapi/superdeskapp/organisation/clique/receive")
public class CliqueReceiveController {

	@Autowired
	private FakeSuperdeskAppProducerHandler fakeSuperdeskAppProducerHandler;


	@ApiOperation(value = "HTTP方式接收-转kafka-kafkaMergeReceiveClique")
	@RequestMapping(value = "/fake/kafkaMergeReceiveClique", method = RequestMethod.POST)
	@ResponseBody
	public R<String> kafkaMergeReceiveClique(@RequestBody SysClique sysCliqueEntity) {

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

		fakeSuperdeskAppProducerHandler.kafkaMergeReceiveClique(sysCliqueEntity);

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

	}

	@ApiOperation(value = "HTTP方式接收-转kafka-kafkaDeleteReceiveClique")
	@RequestMapping(value = "/fake/kafkaDeleteReceiveClique", method = RequestMethod.POST)
	@ResponseBody
	public R<String> kafkaDeleteReceiveClique(@RequestBody SysClique sysCliqueEntity) {

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

		fakeSuperdeskAppProducerHandler.kafkaDeleteReceiveClique(sysCliqueEntity);

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

	}

	/**
	 * http://192.168.6.223:8089/web/openapi/superdeskapp/organisation/clique/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_clique".equals(dataBean.getTopic()) && "mergeReceiveClique".equals(dataBean.getMessageKey())) {

				SysClique sysCliqueEntity = AnJsonUtils.toObject(dataBean.getMessageBody(), SysClique.class);

				fakeSuperdeskAppProducerHandler.kafkaMergeReceiveClique(sysCliqueEntity);

			} else if ("superdesk-app-transfer-receive_clique".equals(dataBean.getTopic()) && "deleteReceiveClique".equals(dataBean.getMessageKey())) {

				SysClique sysCliqueEntity = AnJsonUtils.toObject(dataBean.getMessageBody(), SysClique.class);

				fakeSuperdeskAppProducerHandler.kafkaDeleteReceiveClique(sysCliqueEntity);
			} else if ("superdesk-app-transfer-receive_clique_org".equals(dataBean.getTopic()) && "mergeReceiveCliqueOrg".equals(dataBean.getMessageKey())) {

				SysCliqueOrg sysCliqueOrgEntity = AnJsonUtils.toObject(dataBean.getMessageBody(), SysCliqueOrg.class);

				fakeSuperdeskAppProducerHandler.kafkaMergeReceiveCliqueOrg(sysCliqueOrgEntity);
			} else if ("superdesk-app-transfer-receive_clique_org".equals(dataBean.getTopic()) && "deleteReceiveCliqueOrg".equals(dataBean.getMessageKey())) {

				SysCliqueOrg sysCliqueOrgEntity = AnJsonUtils.toObject(dataBean.getMessageBody(), SysCliqueOrg.class);

				fakeSuperdeskAppProducerHandler.kafkaDeleteReceiveCliqueOrg(sysCliqueOrgEntity);
			}
		});

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