package com.yh.saas.platform.config;

import org.aspectj.lang.annotation.Aspect;
import org.aspectj.lang.annotation.Before;
import org.aspectj.lang.annotation.Pointcut;
import org.springframework.stereotype.Component;

import com.alipay.sofa.runtime.api.annotation.SofaReference;
import com.winsea.svc.base.base.service.IAccountOperationHistoryService;
import com.winsea.svc.base.security.service.IAccountService;
import com.winsea.svc.base.security.util.AuthSecurityUtils;
import com.yh.saas.common.support.plugin.PluginFeature;

import lombok.extern.slf4j.Slf4j;

/**
 * <p>
 * 操作历史截面
 * </p>
 *
 * @author ZhangMingYu
 * @since 2020年3月6日13:06:39
 */
@Aspect
@Component
@Slf4j
public class PluginFeatureAspect {

	@SofaReference
	private IAccountOperationHistoryService accountOperationHistoryService;

	@SofaReference
	private IAccountService accountService;

	@Pointcut("@annotation(com.yh.saas.common.support.plugin.PluginFeature)")
	public void aspect() {
	}

	@Before("aspect()&&@annotation(pluginFeature)")
	public void deBefore(PluginFeature pluginFeature) {
		if (null == AuthSecurityUtils.getCurrentUserInfo()) {
			accountService.logout();
		}
		String accountId = AuthSecurityUtils.getCurrentUserInfo().getUserId();
		accountOperationHistoryService.create(accountId, pluginFeature.id(), pluginFeature.name(),
				pluginFeature.desc());
	}
}
