/**
 * Copyright (c) 2015-2021, Michael Yang 杨福海 (fuhai999@gmail.com).
 * <p>
 * Licensed under the Apache License, Version 2.0 (the "License");
 * you may not use this file except in compliance with the License.
 * You may obtain a copy of the License at
 * <p>
 * http://www.apache.org/licenses/LICENSE-2.0
 * <p>
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS,
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 * See the License for the specific language governing permissions and
 * limitations under the License.
 */
package io.jboot.db.tx;


import com.jfinal.aop.Interceptor;
import com.jfinal.aop.Invocation;
import com.jfinal.kit.Ret;
import com.jfinal.plugin.activerecord.*;
import io.jboot.aop.InterceptorBuilder;
import io.jboot.aop.Interceptors;
import io.jboot.aop.annotation.AutoLoad;
import io.jboot.utils.AnnotationUtil;
import io.jboot.utils.StrUtil;

import java.lang.reflect.Method;

/**
 * 缓存操作的拦截器
 *
 * @author michael yang
 */
@AutoLoad
public class TxEnableInterceptor implements Interceptor, InterceptorBuilder {


    @Override
    public void intercept(Invocation inv) {

        TxEnable txEnable = inv.getMethod().getAnnotation(TxEnable.class);
        String configName = AnnotationUtil.get(txEnable.config());

        DbPro dbPro = StrUtil.isBlank(configName) ? Db.use() : Db.use(configName);
        Config config = StrUtil.isBlank(configName) ? DbKit.getConfig() : DbKit.getConfig(configName);

        int transactionLevel = txEnable.transactionLevel();
        if (transactionLevel == -1) {
            transactionLevel = config.getTransactionLevel();
        }

        IAtom runnable = () -> {

            inv.invoke();

            Object result = inv.getReturnValue();

            if (result == null) {
                return false;
            }

            if (result instanceof Boolean) {
                return (boolean) result;
            }

            if (result instanceof Ret) {
                return ((Ret) result).isOk();
            }

            return true;
        };

        if (txEnable.inNewThread()) {
            try {
                dbPro.txInNewThread(transactionLevel, runnable).get();
            } catch (Exception e) {
                e.printStackTrace();
            }
        } else {
            dbPro.tx(transactionLevel, runnable);
        }

    }


    @Override
    public void build(Class<?> targetClass, Method method, Interceptors interceptors) {
        if (Util.hasAnnotation(method, TxEnable.class)) {
            interceptors.add(this);
        }
    }
}

@@ -729,16 +729,46 @@
 cation;%0A
+import com.jfinal.kit.LogKit;%0A
 import c
@@ -1799,24 +1799,169 @@
 .invoke();%0A%0A
+            //%E6%B2%A1%E6%9C%89%E8%BF%94%E5%9B%9E%E5%80%BC%E7%9A%84%E6%96%B9%E6%B3%95%EF%BC%8C%E5%8F%AA%E8%A6%81%E6%B2%A1%E6%9C%89%E5%BC%82%E5%B8%B8%E5%B0%B1%E6%98%AF%E6%8F%90%E4%BA%A4%E4%BA%8B%E5%8A%A1%0A            if (inv.getMethod().getReturnType() == void.class) %7B%0A                return true;%0A            %7D%0A%0A
             
@@ -2501,26 +2501,36 @@
     
-e.printStackTrace(
+LogKit.error(e.toString(), e
 );%0A 

add @TxEnable() annotation