package com.kxs.common.log.event;

import cn.hutool.core.util.StrUtil;
import com.fasterxml.jackson.annotation.JsonFilter;
import com.fasterxml.jackson.databind.ObjectMapper;
import com.fasterxml.jackson.databind.ser.FilterProvider;
import com.fasterxml.jackson.databind.ser.impl.SimpleBeanPropertyFilter;
import com.fasterxml.jackson.databind.ser.impl.SimpleFilterProvider;
import com.kxs.common.core.constant.SecurityConstants;
import com.kxs.common.core.jackson.SkyJavaTimeModule;
import com.kxs.common.log.config.SkyLogProperties;
import com.kxs.system.api.feign.RemoteLogService;
import com.kxs.system.api.model.SysLog;
import lombok.RequiredArgsConstructor;
import lombok.SneakyThrows;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.InitializingBean;
import org.springframework.context.event.EventListener;
import org.springframework.core.annotation.Order;
import org.springframework.scheduling.annotation.Async;

import java.util.Objects;

/**
 * 异步监听日志事件
 *
 * @author 没秃顶的码农
 * @date 2023/10/28
 */
@Slf4j
@RequiredArgsConstructor
public class SysLogListener implements InitializingBean {

	// new 一个 避免日志脱敏策略影响全局ObjectMapper
	private final static ObjectMapper OBJECT_MAPPER = new ObjectMapper();

	private final RemoteLogService remoteLogService;

	private final SkyLogProperties logProperties;

	@SneakyThrows
	@Async
	@Order
	@EventListener(SysLogEvent.class)
	public void saveSysLog(SysLogEvent event) {
		SysLogEventSource source = (SysLogEventSource) event.getSource();
		SysLog sysLog = new SysLog();
		BeanUtils.copyProperties(source, sysLog);

		// json 格式刷参数放在异步中处理，提升性能
		if (Objects.nonNull(source.getBody())) {
			String params = OBJECT_MAPPER.writeValueAsString(source.getBody());
			sysLog.setParams(StrUtil.subPre(params, logProperties.getMaxLength()));
		}
		log.info("保存日志:{}", sysLog);
		remoteLogService.saveLog(sysLog, SecurityConstants.FROM_IN);
	}

	@Override
	public void afterPropertiesSet() {
		OBJECT_MAPPER.addMixIn(Object.class, PropertyFilterMixIn.class);
		String[] ignorableFieldNames = logProperties.getExcludeFields().toArray(new String[0]);

		FilterProvider filters = new SimpleFilterProvider().addFilter("filter properties by name",
				SimpleBeanPropertyFilter.serializeAllExcept(ignorableFieldNames));
		OBJECT_MAPPER.setFilterProvider(filters);
		OBJECT_MAPPER.registerModule(new SkyJavaTimeModule());
	}

	@JsonFilter("filter properties by name")
	static class PropertyFilterMixIn {

	}

}
