/**
 * Copyright 2008-2009. Chongqing Communications Industry Services Co.,Ltd Information Technology Branch. All rights reserved. <a>http://www.crunii.com</a>
 */
package com.crunii.micro.platform.log.impl;

import com.crunii.micro.common.log.BusiLogComHttp;
import com.crunii.micro.common.utils.JsonUtil;
import com.crunii.micro.platform.log.BusiLogHandler;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.stereotype.Component;

import java.util.HashMap;
import java.util.Map;

/**
 * @author 田平 create 2019年12月31日下午5:43:41
 */
@Slf4j
@Component
public class BusiLogComHttpImpl implements BusiLogComHttp {

	@Autowired
	private BusiLogHandler busiLogHandler;

	@Value("${spring.application.name:NONE}")
	private String appName;

	/**
	 * <pre>
	 * map 里面的属性为com.crunii.micro.domain.entity.micro.TBusiLog的属性，
	 * ipAddr,appName,createTime不需要传，这3个字段不需要传递
	 * TBusiLog busiLog = new TBusiLog();
	 * CopyUtil.copyMapToBean(map, busiLog);
	 * </pre>
	 */
	@Override
	public void busiLogByMap(Map<String, Object> busiLogInfo) {
		// 异步调用新建对象，防止对象状态改变,影响结果
		Map<String, Object> busi = new HashMap<>(busiLogInfo);
		busi.put("appName", appName);
		// 修复没有body的情况数据不为json字符串
		Object sendData = busi.get("sendData");
		Object recvData = busi.get("recvData");
		if (sendData instanceof Map) {
			busi.put("sendData", JsonUtil.getJsonString(sendData));
		}
		if (recvData instanceof Map) {
			busi.put("recvData", JsonUtil.getJsonString(recvData));
		}
		busiLogHandler.doBusiLogByMap(busi);

	}

}
