

package com.lideeyunji.module.biz.enhance;

import com.lideeyunji.core.framework.config.aspect.enhance.model.EnhanceContext;
import com.lideeyunji.core.framework.config.aspect.enhance.model.EnhanceParam;
import com.lideeyunji.core.framework.config.aspect.enhance.model.EnhanceResult;
import com.lideeyunji.core.framework.config.aspect.enhance.plugin.AroundAdvicePlugin;
import com.lideeyunji.core.framework.utils.Func;
import com.lideeyunji.tool.framework.exception.lideeYunJiException;
import lombok.extern.slf4j.Slf4j;
import org.springframework.stereotype.Component;

import java.util.List;
import java.util.Map;

/**
 * 环绕增强
 */
@Component
@Slf4j
public class MyAroudEnhance implements AroundAdvicePlugin {
    @Override
    public void beforeExecute(EnhanceContext enhanceContext) {
        EnhanceParam param = enhanceContext.getParam();
        Map<String, Object> params = param.getParams();
        String name = Func.getMap2Str(params, "name");
        if(Func.isNotEmpty(name) && Func.equals(name,"李四")){
            throw new lideeYunJiException("名称不能为李四");
        }
    }

    @Override
    public void afterExecute(EnhanceContext enhanceContext) {
        EnhanceResult result = enhanceContext.getResult();
        List<Map<String, Object>> dataMapList = result.getRecords();
        if(Func.isEmpty(dataMapList)){
            return;
        }
        for(Map<String,Object> dataMap:dataMapList){
            String name = Func.getMap2Str(dataMap,"name");
            dataMap.put("name",name+"-aroud");
        }
    }

}
