package com.xhpc.user.aspect;


import com.xhpc.common.core.utils.SecurityUtils;
import com.xhpc.common.core.utils.StringUtils;
import com.xhpc.common.util.LogUserUtils;
import com.xhpc.system.api.model.LoginUser;
import org.apache.commons.beanutils.BeanUtils;
import org.aspectj.lang.ProceedingJoinPoint;
import org.aspectj.lang.annotation.Around;
import org.aspectj.lang.annotation.Aspect;
import org.aspectj.lang.annotation.Pointcut;
import org.springframework.context.annotation.Configuration;
import org.springframework.stereotype.Component;
import org.springframework.web.context.request.RequestContextHolder;
import org.springframework.web.context.request.ServletRequestAttributes;

import javax.servlet.http.HttpServletRequest;
import java.util.Date;

/*
 * TODO AO切面，插入创建人，创建时间，修改人，修改时间
 * @author fjd
 * @date 2020-07-09 11:59
 */
@Aspect
@Component
@Configuration
public class UserDaoAspect {
    private static final String CREATE_BY = "createBy";
    private static final String CREATE_TIME = "createTime";
    private static final String UPDATE_BY = "updateBy";
    private static final String UPDATE_TIME = "updateTime";
    private static final String TENANT_ID = "tenantId";

    @Pointcut("execution(* com.xhpc..*.update*(..))")
    public void daoUpdate() {
    }

    @Pointcut("execution(* com.xhpc..*.insert*(..))")
    public void daoCreate() {
    }

    @Around("daoUpdate()")
    public Object doAroundUpdate(ProceedingJoinPoint point) throws Throwable {
        ServletRequestAttributes attributes = (ServletRequestAttributes) RequestContextHolder.getRequestAttributes();
        if (attributes == null) {
            return point.proceed();
        }
        HttpServletRequest request = attributes.getRequest();
        try {
            LogUserUtils logUserUtils = new LogUserUtils();
            LoginUser loginUser = logUserUtils.getLogUser(request);
            String userName = "";
            try {
                userName = SecurityUtils.getUsername();
            } catch (Exception e) {
            }
            if (StringUtils.isEmpty(userName)) {
                userName = loginUser.getUsername();
            }
            if (StringUtils.isEmpty(userName)) {
                userName = "admin";
            }
            if (userName != null) {
                Object[] objects = point.getArgs();
                if (objects != null && objects.length > 0) {
                    for (Object arg : objects) {
                        if (isProperty(arg, UPDATE_BY) && StringUtils.isEmpty(BeanUtils.getProperty(arg, UPDATE_BY))) {
                            BeanUtils.setProperty(arg, UPDATE_BY, userName);
                        }
                        if (isProperty(arg, UPDATE_TIME) && StringUtils.isEmpty(BeanUtils.getProperty(arg, UPDATE_TIME))) {
                            BeanUtils.setProperty(arg, UPDATE_TIME, new Date());
                        }
                    }
                }
            }
        } catch (Exception e) {
        }
        Object object = point.proceed();
        return object;

    }

    @Around("daoCreate()")
    public Object doAroundCreate(ProceedingJoinPoint point) throws Throwable {
        ServletRequestAttributes attributes = (ServletRequestAttributes) RequestContextHolder.getRequestAttributes();
        if (attributes == null) {
            return point.proceed();
        }
        HttpServletRequest request = attributes.getRequest();
        try {
            Object[] objects = point.getArgs();
            if (objects != null && objects.length > 0) {
                for (Object arg : objects) {
                    LogUserUtils logUserUtils = new LogUserUtils();
                    LoginUser loginUser = logUserUtils.getLogUser(request);
                    String userName = "";
                    try {
                        userName = SecurityUtils.getUsername();
                    } catch (Exception e) {
                    }
                    if (StringUtils.isEmpty(userName)) {
                        userName = loginUser.getUsername();
                    }
                    if (StringUtils.isEmpty(userName)) {
                        userName = "admin";
                    }
                    Date date = new Date();
                    if (isProperty(arg, CREATE_BY) && StringUtils.isEmpty(BeanUtils.getProperty(arg, CREATE_BY))) {
                        BeanUtils.setProperty(arg, CREATE_BY, userName);
                    }
                    if (isProperty(arg, UPDATE_BY) && StringUtils.isEmpty(BeanUtils.getProperty(arg, UPDATE_BY))) {
                        BeanUtils.setProperty(arg, UPDATE_BY, userName);
                    }

                    if (isProperty(arg, CREATE_TIME) && StringUtils.isEmpty(BeanUtils.getProperty(arg, CREATE_TIME))) {
                        BeanUtils.setProperty(arg, CREATE_TIME, date);
                    }
                    if (isProperty(arg, UPDATE_TIME) && StringUtils.isEmpty(BeanUtils.getProperty(arg, UPDATE_TIME))) {
                        BeanUtils.setProperty(arg, UPDATE_TIME, date);
                    }

                    if (isProperty(arg, TENANT_ID) && StringUtils.isEmpty(BeanUtils.getProperty(arg, TENANT_ID))) {
                        BeanUtils.setProperty(arg, TENANT_ID, loginUser.getTenantId());
                    }
                }
            }
        } catch (Exception e) {
        }
        Object object = point.proceed();
        return object;
    }

    public static boolean isProperty(Object bean, String field) {
        return StringUtils.isProperty(bean, field);
    }

}
