package net.northking.cctp.common.jpa.interceptor;

import net.northking.cctp.common.persistence.IDEntity;
import net.northking.cctp.common.util.UUIDUtil;
import org.springframework.util.StringUtils;

/**
 * 处理主键ID，统一使用 32位 UUID
 */
public class IDEntityJpaInterceptor extends BasicJpaInterceptor
{
  public static final IDEntityJpaInterceptor Instance = new IDEntityJpaInterceptor();

  private IDEntityJpaInterceptor()
  {
  }

  @Override
  public void beforeInsert(Object entity)
  {
    if (entity instanceof IDEntity)
    {
      IDEntity idEntity = (IDEntity) entity;
      if (!StringUtils.hasText(idEntity.getId()))
      {
        idEntity.setId(UUIDUtil.create32UUID());
      }
    }
  }
}
