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

import java.util.List;

/**
 * 数据库访问服务
 *
 * @author : xiusong.xie
 * @version : 1.0
 */
@SuppressWarnings("ALL")
public abstract class AbstractService<T> implements BasicService<T>
{

  @Override
  public int deleteByExample(T record)
  {
    return getDao().deleteByExample(record);
  }

  @Override
  public int deleteByPrimaryKey(Object id)
  {
    return getDao().deleteByPrimaryKey(id);
  }

  @Override
  public int deleteByPrimaryKeys(List<Object> ids)
  {
    return getDao().deleteByPrimaryKeys(ids);
  }

  @Override
  public int insert(T record)
  {
    beforeInsert(record);
    return getDao().insert(record);
  }

  @Override
  public int insertByBatch(List<T> list)
  {
    beforeBatchInsert(list);
    return getDao().insertByBatch(list);
  }

  @Override
  public T findByPrimaryKey(Object id)
  {
    return getDao().findByPrimaryKey(id);
  }

  @Override
  public List<T> findByPrimaryKeys(List<Object> ids)
  {
    return getDao().findByPrimaryKeys(ids);
  }

  @Override
  public int updateByPrimaryKey(T record)
  {
    beforeUpdate(record);
    return getDao().updateByPrimaryKey(record);
  }

  @Override
  public List<T> findAll()
  {
    return query(createEntity());
  }

  @Override
  public List<T> query(T record)
  {
    return getDao().query(record);
  }


  @Override
  public long count(T record)
  {
    return getDao().queryCount(record);
  }

  @Override
  public long count()
  {
    return getDao().queryCount(createEntity());
  }

  protected abstract void beforeInsert(T record);

  protected abstract void beforeUpdate(T record);

  protected void beforeBatchInsert(List<T> list)
  {
    for (T record : list)
    {
      beforeInsert(record);
    }
  }
}
