package com.palmnest.service.outer.impl;

import com.fasterxml.jackson.core.JsonProcessingException;
import com.fasterxml.jackson.databind.ObjectMapper;
import com.palmnest.application.core.util.BeanUtils;
import com.palmnest.application.core.util.PropertiesKit;
import com.palmnest.application.dto.output.kafka.*;
import com.palmnest.model.TbCompany;
import com.palmnest.model.estateserve.AppUser;
import com.palmnest.model.kafka.KafkaProducerServer;
import com.palmnest.model.property.TbBuild;
import com.palmnest.model.property.TbOrganization;
import com.palmnest.model.property.VirtualArchitecture;
import com.palmnest.service.outer.KafkaOuterService;
import lombok.extern.slf4j.Slf4j;
import org.apache.commons.beanutils.ConvertUtils;
import org.springframework.scheduling.annotation.Async;
import org.springframework.stereotype.Service;

import javax.annotation.Resource;
import java.lang.reflect.InvocationTargetException;
import java.math.BigDecimal;
import java.util.Date;
import java.util.Map;

@Slf4j
@Service("kafkaOuterService")
public class KafkaOuterServiceImpl implements KafkaOuterService {


	@Resource(name = "kafkaProducerServer")
	KafkaProducerServer producer;

	private static String shoppingTopic = PropertiesKit.readPropertiesValue("shoppingTopic", "wechat.properties");
	private static String superTopic    = PropertiesKit.readPropertiesValue("superTopic", "wechat.properties");

	@Async
	@Override
	public void sendMessage(String topic, String key, String message) {
		producer.sendMessage(topic, key, message);
	}


	@Async
	@Override
	public void syncCompany(TbCompany company) {
		log.info("同步运营中心信息到子系统开始------topic：" + shoppingTopic);
		ObjectMapper      objectMapper = new ObjectMapper();
		KafkaOuterCompany outerCompany = new KafkaOuterCompany();
		try {
			ConvertUtils.register(new org.apache.commons.beanutils.converters.DateConverter(null),
					java.util.Date.class);//添加这一行代码，重新注册一个转换器，也可以自定义
			BeanUtils.copyProperties(outerCompany, company);
			outerCompany.setCreateTime(company.getCreateTime() == null ? new Date() : company.getCreateTime());
			outerCompany.setModifyTime(company.getModifyTime() == null ? new Date() : company.getModifyTime());
			outerCompany.setModifier("admin");
			sendMessage(shoppingTopic, "addOrUpdateCompany", objectMapper.writeValueAsString(outerCompany));
			log.info("同步运营中心信息到子系统结束------topic：" + shoppingTopic);
		} catch (JsonProcessingException e) {
			e.printStackTrace();
			log.error("同步运营中心信息到子系统失败1:" + e);
		} catch (IllegalAccessException e) {
			e.printStackTrace();
			log.error("同步运营中心信息到子系统失败2:" + e);
		} catch (InvocationTargetException e) {
			e.printStackTrace();
			log.error("同步运营中心信息到子系统失败3:" + e);
		}
	}


	@Async
	@Override
	public void syncOrg(Map<String, Object> map) {
		log.info("同步项目信息到子系统开始------topic：" + shoppingTopic);
		ObjectMapper           objectMapper = new ObjectMapper();
		KafkaOuterOrganization outerOrg     = new KafkaOuterOrganization();
		TbOrganization         org          = (TbOrganization) map.get("org");
		Integer                companyId    = Integer.parseInt(map.get("companyId").toString());
//		Integer companyId = (Integer) map.get("companyId");
		try {
			ConvertUtils.register(new org.apache.commons.beanutils.converters.DateConverter(null),
					java.util.Date.class);//添加这一行代码，重新注册一个转换器，也可以自定义
			BeanUtils.copyProperties(outerOrg, org);
			outerOrg.setCompanyId(companyId);
			outerOrg.setCreateTime(org.getCreateTime() == null ? new Date() : org.getCreateTime());
			outerOrg.setModifyTime(org.getModifyTime() == null ? new Date() : org.getModifyTime());
			outerOrg.setModifier("admin");
			sendMessage(shoppingTopic, "addOrUpdateOrg", objectMapper.writeValueAsString(outerOrg));
			if (org.getId() >= 615) {
				sendMessage(superTopic, "addOrUpdateOrg", objectMapper.writeValueAsString(outerOrg));
			}
			log.info("同步项目信息到子系统结束------topic：" + shoppingTopic);
		} catch (JsonProcessingException e) {
			e.printStackTrace();
			log.error("同步项目信息到子系统失败1:" + e);
		} catch (IllegalAccessException e) {
			e.printStackTrace();
			log.error("同步项目信息到子系统失败2:" + e);
		} catch (InvocationTargetException e) {
			e.printStackTrace();
			log.error("同步项目信息到子系统失败3:" + e);
		}

	}

	@Async
	@Override
	public void syncBuild(TbBuild build, Integer companyId) {
		log.info("同步楼宇信息到子系统开始------topic：" + shoppingTopic);
		ObjectMapper    objectMapper = new ObjectMapper();
		KafkaOuterBuild outerBuild   = new KafkaOuterBuild();
		try {
			ConvertUtils.register(new org.apache.commons.beanutils.converters.DateConverter(null),
					java.util.Date.class);//添加这一行代码，重新注册一个转换器，也可以自定义
			BeanUtils.copyProperties(outerBuild, build);
			outerBuild.setCreateTime(build.getCreateTime() == null ? new Date() : build.getCreateTime());
			outerBuild.setModifyTime(build.getModifyTime() == null ? new Date() : build.getModifyTime());
			outerBuild.setModifier("admin");
			outerBuild.setCompanyId(companyId);
			sendMessage(shoppingTopic, "addOrUpdateBuild", objectMapper.writeValueAsString(outerBuild));
			if (outerBuild.getId() >= 617) {
				sendMessage(superTopic, "addOrUpdateBuild", objectMapper.writeValueAsString(outerBuild));
			}
			log.info("同步楼宇信息到子系统结束------topic：" + shoppingTopic);
		} catch (JsonProcessingException e) {
			e.printStackTrace();
			log.error("同步楼宇信息到子系统失败1:" + e);
		} catch (IllegalAccessException e) {
			e.printStackTrace();
			log.error("同步楼宇信息到子系统失败2:" + e);
		} catch (InvocationTargetException e) {
			e.printStackTrace();
			log.error("同步楼宇信息到子系统失败3:" + e);
		}
	}

	@Async
	@Override
	public void syncVirtualarchitecture(VirtualArchitecture virtualArchitecture) {
		log.info("同步企业信息到子系统开始------topic：" + shoppingTopic);
		ObjectMapper                  objectMapper             = new ObjectMapper();
		KafkaOuterVirtualarchitecture outerVirtualarchitecture = new KafkaOuterVirtualarchitecture();
		try {
			virtualArchitecture.setReserveBalance(virtualArchitecture.getReserveBalance() == null ? new BigDecimal("0.00") : virtualArchitecture.getReserveBalance());
			ConvertUtils.register(new org.apache.commons.beanutils.converters.DateConverter(null),
					java.util.Date.class);//添加这一行代码，重新注册一个转换器，也可以自定义
			BeanUtils.copyProperties(outerVirtualarchitecture, virtualArchitecture);
			outerVirtualarchitecture.setCreateTime(virtualArchitecture.getCreateTime() == null ? new Date() : virtualArchitecture.getCreateTime());
			outerVirtualarchitecture.setModifyTime(virtualArchitecture.getModifyTime() == null ? new Date() : virtualArchitecture.getModifyTime());
			outerVirtualarchitecture.setModifier("admin");
			sendMessage(shoppingTopic, "addOrUpdateVirtualarchitecture", objectMapper.writeValueAsString(outerVirtualarchitecture));
			if (outerVirtualarchitecture.getId() >= 2500) {
				sendMessage(superTopic, "addOrUpdateVirtualarchitecture", objectMapper.writeValueAsString(outerVirtualarchitecture));
			}
			log.info("同步企业信息到子系统结束------topic：" + shoppingTopic);
		} catch (JsonProcessingException e) {
			e.printStackTrace();
			log.error("同步企业信息到子系统失败1:" + e);
		} catch (IllegalAccessException e) {
			e.printStackTrace();
			log.error("同步企业信息到子系统失败2:" + e);
		} catch (InvocationTargetException e) {
			e.printStackTrace();
			log.error("同步企业信息到子系统失败3:" + e);
		}
	}

	@Async
	@Override
	public void syncUser(AppUser user) {
		log.info("同步企业人员信息到子系统开始------topic：" + shoppingTopic);
		ObjectMapper   objectMapper = new ObjectMapper();
		KafkaOuterUser outerUser    = new KafkaOuterUser();
		try {
			ConvertUtils.register(new org.apache.commons.beanutils.converters.DateConverter(null),
					java.util.Date.class);//添加这一行代码，重新注册一个转换器，也可以自定义
			BeanUtils.copyProperties(outerUser, user);
			outerUser.setCreateTime(user.getCreateTime() == null ? new Date() : user.getCreateTime());
			outerUser.setModifyTime(user.getModifyTime() == null ? new Date() : user.getModifyTime());
			outerUser.setModifier("admin");
			outerUser.setBirthday(user.getBirthday() == null ? new Date() : user.getBirthday());
			outerUser.setEnteringTime(user.getEnteringTime() == null ? new Date() : user.getEnteringTime());
			sendMessage(shoppingTopic, "addOrUpdateUser", objectMapper.writeValueAsString(outerUser));
			log.info("同步企业人员信息到子系统结束------topic：" + shoppingTopic);
		} catch (JsonProcessingException e) {
			e.printStackTrace();
			log.error("同步企业人员信息到子系统失败1:" + e);
		} catch (IllegalAccessException e) {
			e.printStackTrace();
			log.error("同步企业人员信息到子系统失败2:" + e);
		} catch (InvocationTargetException e) {
			e.printStackTrace();
			log.error("同步企业人员信息到子系统失败3:" + e);
		}

	}

}
