package com.tykj.base.aop;


import com.tykj.base.entity.BaseEntity;
import org.aspectj.lang.JoinPoint;
import org.aspectj.lang.annotation.Aspect;
import org.aspectj.lang.annotation.Before;
import org.dom4j.Document;
import org.dom4j.DocumentException;
import org.dom4j.DocumentHelper;
import org.dom4j.Element;
import org.hibernate.mapping.KeyValue;
import org.hibernate.mapping.Map;
import org.springframework.stereotype.Component;

import java.util.Date;
import java.util.List;

import static java.util.Objects.isNull;

/**
 * @author C
 */
@Aspect
@Component
public class EntityHandle {

    @Before("execution(* org.springframework.data.repository.CrudRepository.save(..)) && args(com.tykj.base.entity.BaseEntity))")
    public void checkTimes(JoinPoint point) {
        Object[] args = point.getArgs();
        for (Object arg : args) {
            if (arg instanceof BaseEntity) {
                BaseEntity entity = (BaseEntity) arg;
                if (isNull(entity.getCreatedTime())) {
                    entity.setCreatedTime(new Date());
                }
                entity.setUpdatedTime(new Date());
            }
        }
    }

//    @Before("execution(* org.hibernate.internal.SessionImpl.saveOrUpdate(..))")
    public void addTime(JoinPoint point) throws DocumentException {
        Object[] args = point.getArgs();
        for (Object arg : args) {
            if (arg instanceof Map) {


            }
        }

    }


}
