package com.keyware.shandan.frame.aspect;

import com.keyware.shandan.frame.annotation.UserSync;
import com.keyware.shandan.system.task.UserSyncHandler;
import lombok.extern.slf4j.Slf4j;
import org.aspectj.lang.JoinPoint;
import org.aspectj.lang.annotation.AfterReturning;
import org.aspectj.lang.annotation.Aspect;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Component;

/**
 * 用户数据同步到其他分系统切面类
 *
 * @author Administrator
 */
@Slf4j
@Aspect
@Component
public class UserInfoSyncAspect {

    @Autowired
    private UserSyncHandler syncHandler;

    @AfterReturning(value = "@annotation(com.keyware.shandan.frame.annotation.UserSync)")
    public void after(JoinPoint joinPoint) {
        UserSync sync = null;
        try {
            sync = AspectCommon.getAnnotationByMethod(joinPoint, UserSync.class);
        } catch (NoSuchMethodException e) {
            log.error("用户同步切面异常：读取方法注解失败", e);
            e.printStackTrace();
        }

        Object[] params = joinPoint.getArgs();
        if (sync != null && params != null && params.length > 0) {
            Object param = params[0];
            switch (sync.operateType()) {
                case INSERT:
                    syncHandler.insert(param);
                    break;
                case INSERT_OR_UPDATE:
                    syncHandler.insertOrUpdate(param);
                    break;
                case UPDATE:
                    syncHandler.update(param);
                    break;
                case DELETE:
                    syncHandler.delete(param);
                default:
            }
        }

    }
}
