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

import cn.superdesk.app.supercloud.dto.receive.organisation.VirtualarchitectureReceiveParam;
import cn.superdesk.app.transfer.constants.receive.TopicReceiveConstants;
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.SlaveVirtualarchitectureReceiveParam;
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.beans.factory.annotation.Value;
import org.springframework.web.bind.annotation.*;

@Api(value = "[开放API]-服务之窗-企业" ,tags = "[开放API]-服务之窗-企业")
@Slf4j
@RestController
@RequestMapping("/openapi/superdeskapp/organisation/virtualarchitecture/receive")
public class VirtualarchitectureReceiveController {

	@Autowired
	private FakeSuperdeskAppProducerHandler fakeSuperdeskAppProducerHandler;


	/**
	 * 侨城汇 福利商城 在用 已查证 mark by linjinyu at 20220421
	 * topic superdesk-mall-receive_virtualarchitecture - superdesk.mall.kafka.topic.organisation.virtualarchitecture
	 * message_key mergeReceiveVirtualarchitecture
	 */
	@Value("${superdesk.mall.kafka.topic.organisation.virtualarchitecture}")
	private String TOPIC_RECEIVE_VIRTUALARCHITECTURE_FROM_SUPERMALL;

	/**
	 * http://192.168.6.223:8089/web/openapi/superdeskapp/organisation/virtualarchitecture/receive/test/timeline
	 * http://192.168.6.225:8089/web/openapi/superdeskapp/organisation/virtualarchitecture/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());

			/****************************************** 来源 服务之窗 start ******************************************/
			if (TopicReceiveConstants.TOPIC_RECEIVE_VIRTUALARCHITECTURE.equals(dataBean.getTopic()) && "mergeReceiveVirtualarchitecture".equals(dataBean.getMessageKey())) {

				VirtualarchitectureReceiveParam virtualarchitectureReceiveParam = AnJsonUtils.toObject(dataBean.getMessageBody(), VirtualarchitectureReceiveParam.class);

				fakeSuperdeskAppProducerHandler.kafkaMergeReceiveVirtualarchitectureFromSuperdesk(virtualarchitectureReceiveParam);

			} else if (TopicReceiveConstants.TOPIC_RECEIVE_VIRTUALARCHITECTURE.equals(dataBean.getTopic()) && "deleteReceiveVirtualarchitecture".equals(dataBean.getMessageKey())) {

				VirtualarchitectureReceiveParam virtualarchitectureReceiveParam = AnJsonUtils.toObject(dataBean.getMessageBody(), VirtualarchitectureReceiveParam.class);

				fakeSuperdeskAppProducerHandler.kafkaDeleteReceiveVirtualarchitectureFromSuperdesk(virtualarchitectureReceiveParam);
			}
			/****************************************** 来源 服务之窗   end ******************************************/

			/****************************************** 来源 福利商城 start ******************************************/
			else if (TOPIC_RECEIVE_VIRTUALARCHITECTURE_FROM_SUPERMALL.equals(dataBean.getTopic()) && "mergeReceiveVirtualarchitecture".equals(dataBean.getMessageKey())) {

				SlaveVirtualarchitectureReceiveParam slaveVirtualarchitectureReceiveParam = AnJsonUtils.toObject(dataBean.getMessageBody(), SlaveVirtualarchitectureReceiveParam.class);

				fakeSuperdeskAppProducerHandler.kafkaMergeReceiveVirtualarchitectureFromSupermall(slaveVirtualarchitectureReceiveParam);

			} else if (TOPIC_RECEIVE_VIRTUALARCHITECTURE_FROM_SUPERMALL.equals(dataBean.getTopic()) && "deleteReceiveVirtualarchitecture".equals(dataBean.getMessageKey())) {

				SlaveVirtualarchitectureReceiveParam slaveVirtualarchitectureReceiveParam = AnJsonUtils.toObject(dataBean.getMessageBody(), SlaveVirtualarchitectureReceiveParam.class);

				fakeSuperdeskAppProducerHandler.kafkaDeleteReceiveVirtualarchitectureFromSupermall(slaveVirtualarchitectureReceiveParam);
			}
			/****************************************** 来源 福利商城   end ******************************************/

		});

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