package com.ematou.wxservice.interceptor;

import com.ematou.wxservice.common.utils.DateUtil;
import org.apache.ibatis.executor.Executor;
import org.apache.ibatis.mapping.MappedStatement;
import org.apache.ibatis.mapping.SqlCommandType;
import org.apache.ibatis.plugin.*;
import org.springframework.stereotype.Component;

import java.lang.reflect.Field;
import java.sql.Timestamp;
import java.util.Date;
import java.util.Map;
import java.util.Properties;

/**
 * @author lhm
 * @version 1.0
 * 2021-04-30 15:30
 */
@Intercepts({@Signature(type = Executor.class, method = "update", args = {MappedStatement.class, Object.class})})
@Component
public class MybatisInsertAndUpdateInterceptor implements Interceptor {


    @Override
    public Object intercept(Invocation invocation) throws Throwable {

        Object[] args = invocation.getArgs();
        MappedStatement mappedStatement = (MappedStatement) args[0];

        if (mappedStatement.getSqlCommandType() == SqlCommandType.UPDATE) {
            Object arg = args[1];
            if (arg instanceof Map) {
                Map<?, ?> map = (Map<?, ?>) arg;
                for (Object key : map.keySet()) {
                    if (key.toString().contains("param")) {
                        handleUpdateData(map.get(key));
                    }
                }
            } else {
                handleInsertData(arg);
            }
        } else if (mappedStatement.getSqlCommandType() == SqlCommandType.INSERT) {
            Object arg = args[1];
            if (arg instanceof Map) {
                Map<?, ?> map = (Map<?, ?>) arg;
                for (Object key : map.keySet()) {
                    if (key.toString().contains("param")) {
                        handleInsertData(map.get(key));
                    }
                }
            } else {
                handleInsertData(arg);
            }
        }

        return invocation.proceed();
    }

    private void handleInsertData(Object o) throws IllegalAccessException {
        Field[] fields = o.getClass().getDeclaredFields();

        for (Field field : fields) {
            switch (field.getName()) {
                case "createTime":
                    field.setAccessible(true);
                    field.set(o, DateUtil.formatDate(new Date()));
                    break;
                case "modTime":
                    field.setAccessible(true);
                    field.set(o, DateUtil.formatDate(new Date()));
                    break;
                case "tstm":
                    field.setAccessible(true);
                    field.set(o, new Timestamp(System.currentTimeMillis()));
                    break;
                case "createrSn":
                    // TODO
                    field.setAccessible(true);
                    field.set(o, "admin");
                    break;
            }
        }

    }

    private void handleUpdateData(Object o) throws IllegalAccessException {
        Field[] fields = o.getClass().getDeclaredFields();

        for (Field field : fields) {
            switch (field.getName()) {
                case "modTime":
                    field.setAccessible(true);
                    field.set(o, DateUtil.formatDate(new Date()));
                    break;
                case "tstm":
                    field.setAccessible(true);
                    field.set(o, new Timestamp(System.currentTimeMillis()));
                    break;
                case "moderSn":
                    // TODO
                    field.setAccessible(true);
                    field.set(o, "admin");
                    break;
            }
        }

    }

    @Override
    public Object plugin(Object target) {
        return Plugin.wrap(target, this);
    }

    @Override
    public void setProperties(Properties properties) {

    }
}
