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.superdeskapp.UnifiedChannelProfitSyncReceiveParam;
import cn.superdesk.uniorder.dto.receive.consumer.superdeskapp.UnifiedChannelSyncReceiveParam;
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/6/30 下午2:51
 * @UpdateUser: linjinyu
 * @UpdateDate: 2021/6/30 下午2:51
 * @UpdateRemark: 修改内容
 * @Version: 1.0
 */
@Api(value = "[开放API]-服务之窗-运营商|渠道商" ,tags = "[开放API]-服务之窗-运营商|渠道商")
@Slf4j
@RestController
@RequestMapping("/openapi/superdeskapp/organisation/operater/receive")
public class OperaterReceiveController {

	@Autowired
	private FakeSuperdeskAppProducerHandler fakeSuperdeskAppProducerHandler;

	/**
	 * http://192.168.6.223:8089/web/openapi/superdeskapp/organisation/operater/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());

			// superdesk.app.services.organization.kafka.topic.channel.main=sw-un-operater
			// 运营商_渠道商-新增
			// sw-un-operater-insert
			// 运营商_渠道商-修改
			// sw-un-operater-update
			// 运营商_渠道商-删除
			// sw-un-operater-delete

			// superdesk.app.services.organization.kafka.topic.channel.opt=sw-un-businesspor
			// 渠道商-业务分润-新增
			// sw-un-businesspor-insert
			// 渠道商-业务分润-修改
			// sw-un-businesspor-update
			// 渠道商-业务分润-删除
			// sw-un-businesspor-delete

			/****************************************** 运营商_渠道商 start ******************************************/
			if ("sw-un-operater".equals(dataBean.getTopic()) && "sw-un-operater-insert".equals(dataBean.getMessageKey())) {

				UnifiedChannelSyncReceiveParam receiveParam = AnJsonUtils.toObject(dataBean.getMessageBody(), UnifiedChannelSyncReceiveParam.class);

				fakeSuperdeskAppProducerHandler.kafkaOperaterInsert(receiveParam);

			} else if ("sw-un-operater".equals(dataBean.getTopic()) && "sw-un-operater-update".equals(dataBean.getMessageKey())) {

				UnifiedChannelSyncReceiveParam receiveParam = AnJsonUtils.toObject(dataBean.getMessageBody(), UnifiedChannelSyncReceiveParam.class);

				fakeSuperdeskAppProducerHandler.kafkaOperaterUpdate(receiveParam);

			} else if ("sw-un-operater".equals(dataBean.getTopic()) && "sw-un-operater-delete".equals(dataBean.getMessageKey())) {

				UnifiedChannelSyncReceiveParam receiveParam = AnJsonUtils.toObject(dataBean.getMessageBody(), UnifiedChannelSyncReceiveParam.class);

				fakeSuperdeskAppProducerHandler.kafkaOperaterDelete(receiveParam);
			}
			/****************************************** 运营商_渠道商   end ******************************************/

			/****************************************** 渠道商-业务分润 start ******************************************/
			else if ("sw-un-businesspor".equals(dataBean.getTopic()) && "sw-un-businesspor-insert".equals(dataBean.getMessageKey())) {

				UnifiedChannelProfitSyncReceiveParam receiveParam = AnJsonUtils.toObject(dataBean.getMessageBody(), UnifiedChannelProfitSyncReceiveParam.class);

				fakeSuperdeskAppProducerHandler.kafkaOperaterBusinessProfitInsert(receiveParam);

			} else if ("sw-un-businesspor".equals(dataBean.getTopic()) && "sw-un-businesspor-update".equals(dataBean.getMessageKey())) {

				UnifiedChannelProfitSyncReceiveParam receiveParam = AnJsonUtils.toObject(dataBean.getMessageBody(), UnifiedChannelProfitSyncReceiveParam.class);

				fakeSuperdeskAppProducerHandler.kafkaOperaterBusinessProfitUpdate(receiveParam);

			} else if ("sw-un-businesspor".equals(dataBean.getTopic()) && "sw-un-businesspor-delete".equals(dataBean.getMessageKey())) {

				UnifiedChannelProfitSyncReceiveParam receiveParam = AnJsonUtils.toObject(dataBean.getMessageBody(), UnifiedChannelProfitSyncReceiveParam.class);

				fakeSuperdeskAppProducerHandler.kafkaOperaterBusinessProfitDelete(receiveParam);
			}
			/****************************************** 渠道商-业务分润   end ******************************************/
		});

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