ESAspect.java.ftl 4.9 KB
Newer Older
zhouweidong's avatar
zhouweidong committed
1
<#ibiztemplate>
2
TARGET=PSSYSTEM
zhouweidong's avatar
zhouweidong committed
3 4 5 6 7 8 9 10 11
</#ibiztemplate>
<#assign hasESEntity=false>
<#list sys.getAllPSDataEntities() as dataEntity>
    <#if dataEntity.getUserTag()?? && dataEntity.getUserTag()=='elasticsearch'>
        <#assign hasESEntity=true>
        <#break>
    </#if>
</#list>
<#if hasESEntity==true>
12
package ${pub.getPKGCodeName()}.util.aspect;
zhouweidong's avatar
zhouweidong committed
13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46

import lombok.extern.slf4j.Slf4j;
import ${pub.getPKGCodeName()}.util.domain.EntityBase;
import org.aspectj.lang.JoinPoint;
import org.aspectj.lang.annotation.AfterReturning;
import org.aspectj.lang.annotation.Aspect;
import org.springframework.expression.EvaluationContext;
import org.springframework.expression.Expression;
import org.springframework.expression.ExpressionParser;
import org.springframework.expression.spel.standard.SpelExpressionParser;
import org.springframework.expression.spel.support.StandardEvaluationContext;
import org.springframework.scheduling.annotation.Async;
import org.springframework.stereotype.Component;
import org.springframework.util.ObjectUtils;
import org.springframework.util.StringUtils;
import java.util.List;

/**
 * es同步数据切面
 */
@Aspect
@Component
@Slf4j
public class ESAspect
{

    private final ExpressionParser parser = new SpelExpressionParser();

    <#list sys.getAllPSDataEntities() as dataEntity>
        <#if dataEntity.getUserTag()?? && dataEntity.getUserTag()=='elasticsearch'>
    /**
     * 实体[${dataEntity.codeName}]es切面
     * @param point
     */
47
    @AfterReturning(value = "(execution(* ${pub.getPKGCodeName()}.core.*.service.*${dataEntity.codeName}*.create*(..))||execution(* ${pub.getPKGCodeName()}.core.*.service.*${dataEntity.codeName}*.update*(..))||execution(* ${pub.getPKGCodeName()}.core.*.service.*${dataEntity.codeName}*.save*(..)) ||execution(* ${pub.getPKGCodeName()}.core.*.service.*${dataEntity.codeName}*.remove*(..))) && !execution(* ${pub.getPKGCodeName()}.core.es.service.*.create*(..)) && !execution(* ${pub.getPKGCodeName()}.core.es.service.*.update*(..)) && !execution(* ${pub.getPKGCodeName()}.core.es.service.*.save*(..)) && !execution(* ${pub.getPKGCodeName()}.core.es.service.*.remove*(..))")
zhouweidong's avatar
zhouweidong committed
48
    @Async
zhouweidong's avatar
zhouweidong committed
49
    public void Sync${dataEntity.codeName?lower_case?cap_first}(JoinPoint point) {
zhouweidong's avatar
zhouweidong committed
50 51 52 53 54 55 56 57 58
        syncSaveESData(point,"${dataEntity.codeName}");
    }
        </#if>
    </#list>

    /**
     * 异步往es中保存数据
     * @param point
     */
zhouweidong's avatar
zhouweidong committed
59
    public void syncSaveESData(JoinPoint point, String deName) {
zhouweidong's avatar
zhouweidong committed
60 61 62 63
        try {
            Object service=point.getTarget();
            String action=point.getSignature().getName();
            Object [] args = point.getArgs();
zhouweidong's avatar
zhouweidong committed
64
            if(ObjectUtils.isEmpty(args) || args.length==0 || StringUtils.isEmpty(action)) {
zhouweidong's avatar
zhouweidong committed
65
                return;
zhouweidong's avatar
zhouweidong committed
66
            }
zhouweidong's avatar
zhouweidong committed
67 68

            EvaluationContext exServiceCtx = new StandardEvaluationContext();
zhouweidong's avatar
zhouweidong committed
69
            exServiceCtx.setVariable("service", service);
zhouweidong's avatar
zhouweidong committed
70 71
            Expression esServiceExp = parser.parseExpression("#service.getESService()");
            Object exService=esServiceExp.getValue(exServiceCtx);
zhouweidong's avatar
zhouweidong committed
72
            if(ObjectUtils.isEmpty(exService)) {
zhouweidong's avatar
zhouweidong committed
73 74 75 76
                log.error("获取[{}]实体全文检索服务对象失败",deName);
                return;
            }
            Object arg=args[0];
zhouweidong's avatar
zhouweidong committed
77 78
            if ("remove".equals(action) || "removeBatch".equals(action)) {
                executeESMethod(exService, action, arg);
79
            }
zhouweidong's avatar
zhouweidong committed
80
            else if(arg instanceof EntityBase  || arg instanceof List) {
zhouweidong's avatar
zhouweidong committed
81
                EvaluationContext exMappingCtx = new StandardEvaluationContext();
zhouweidong's avatar
zhouweidong committed
82
                exMappingCtx.setVariable("service", service);
zhouweidong's avatar
zhouweidong committed
83 84
                Expression esMappingExp = parser.parseExpression("#service.getESMapping()");
                Object exMapping=esMappingExp.getValue(exMappingCtx);
zhouweidong's avatar
zhouweidong committed
85 86
                if(ObjectUtils.isEmpty(exMapping)) {
                    log.error("获取[{}]实体全文检索映射对象失败", deName);
zhouweidong's avatar
zhouweidong committed
87 88 89
                    return;
                }
                EvaluationContext exDomainCtx = new StandardEvaluationContext();
zhouweidong's avatar
zhouweidong committed
90 91
                exDomainCtx.setVariable("mapping", exMapping);
                exDomainCtx.setVariable("arg", arg);
zhouweidong's avatar
zhouweidong committed
92 93
                Expression esDomainExp = parser.parseExpression("#mapping.toESDomain(#arg)");
                arg=esDomainExp.getValue(exDomainCtx);
zhouweidong's avatar
zhouweidong committed
94
                executeESMethod(exService, action, arg);
95
            }
zhouweidong's avatar
zhouweidong committed
96
        } catch (Exception e) {
zhouweidong's avatar
zhouweidong committed
97
            log.error("同步[{}]实体全文检索数据失败,{}", deName, e);
zhouweidong's avatar
zhouweidong committed
98 99
        }
    }
100 101 102 103 104 105 106

    /**
     * 执行es方法
     * @param exService
     * @param action
     * @param arg
     */
zhouweidong's avatar
zhouweidong committed
107
    private void executeESMethod(Object exService, Object action, Object arg) {
108
        EvaluationContext esContext = new StandardEvaluationContext();
zhouweidong's avatar
zhouweidong committed
109 110 111
        esContext.setVariable("exService", exService);
        esContext.setVariable("arg", arg);
        Expression exExp = parser.parseExpression(String.format("#exService.%s(#arg)", action));
112 113
        exExp.getValue(esContext);
    }
zhouweidong's avatar
zhouweidong committed
114 115
}
</#if>