/*
 * Copyright (c) 京北方信息技术股份有限公司 Corporation 2021 . All rights reserved.
 *
 */
package net.northking.cctp.common.db.entity;

import net.northking.cctp.common.security.authentication.NKSecurityContext;

import java.util.Date;

/**
 * 业务实体类数据库接口
 *
 * @author : xiusong.xie
 * @version : 1.0
 */
public interface EntityDBService
{
  default void initBeforeInsert(Object record)
  {
    if (record instanceof Entity)
    {
      if(((Entity) record).getCreatedTime()==null)((Entity) record).setCreatedTime(new Date());
      if(((Entity) record).getCreatedBy()==null)((Entity) record).setCreatedBy(NKSecurityContext.getUserId());
      if(((Entity) record).getUpdatedTime()==null)((Entity) record).setUpdatedTime(new Date());
      if(((Entity) record).getUpdatedBy()==null)((Entity) record).setUpdatedBy(NKSecurityContext.getUserId());
    } else
    {
      String err = record.getClass() + " is not implements Entity";
      throw new ClassCastException(err);
    }
  }

  default void initBeforeUpdate(Object record)
  {
    if (record instanceof Entity)
    {
      if(((Entity) record).getUpdatedTime()==null)((Entity) record).setUpdatedTime(new Date());
      if(((Entity) record).getUpdatedBy()==null)((Entity) record).setUpdatedBy(NKSecurityContext.getUserId());
    } else
    {
      String err = record.getClass() + " is not implements Entity";
      throw new ClassCastException(err);
    }
  }
}
