package h2o.dao.advanced;

import h2o.common.util.lang.StringUtil;
import h2o.dao.Dao;
import h2o.dao.DbUtil;

import java.util.List;

/**
 * Created by zhangjianwei on 2017/7/1.
 */
public final class DaoBasicUtil {

    private final Dao dao;

    public DaoBasicUtil() {
        this.dao = DbUtil.getDao();
    }

    public DaoBasicUtil( Dao dao ) {
        this.dao = dao;
    }


    public void add( Object entity ) {
        dao.update( DbUtil.sqlBuilder.buildInsertSql(entity) , entity );
    }

    public void batAdd( List<Object> entities ) {
        dao.update( DbUtil.sqlBuilder.buildInsertSqlIncludeNull(entities.get(0)) , entities );
    }

    public int edit( AbstractEntity entity ) {
        return dao.update( DbUtil.sqlBuilder.buildUpdateSql3( entity , entity.get_w() , entity.get_pks() ) , entity );
    }

    public int edit(Object entity , String w , String... skip ) {
        return dao.update( DbUtil.sqlBuilder.buildUpdateSql3( entity , w , skip ) , entity );
    }

    public int del( AbstractEntity entity ) {
        return dao.update( "delete from " + entity.get_tableName() +  " where " + entity.get_w() , entity );
    }

    public <T extends AbstractEntity> T get( T entity ) {
        return this.get( entity , false );
    }

    public <T extends AbstractEntity> T getAndLock( T entity ) {
        return this.get( entity , true );
    }

    public <T extends AbstractEntity> T get( T entity , boolean lock ) {

        StringBuilder sql = new StringBuilder();

        StringUtil.append( sql , "select * from " , entity.get_tableName() ,  " where " , entity.get_w() );
        if( lock ) {
            sql.append(" for update ");
        }

        return (T)dao.get( entity.getClass() , sql.toString() , entity );
    }

}

@@ -20,16 +20,67 @@
 anced;%0A%0A
+import h2o.common.util.collections.CollectionUtil;%0A
 import h
@@ -894,22 +894,31 @@
 nt edit(
-Object
+ AbstractEntity
  entity 
@@ -934,26 +934,322 @@
 w , 
-String... skip ) %7B
+Object... args ) %7B%0A%0A        Object%5B%5D para;%0A        if( CollectionUtil.argsIsBlank( args ) ) %7B%0A            para = new Object%5B%5D %7B entity %7D;%0A        %7D else %7B%0A            para = new Object%5B args.length + 1 %5D;%0A            para%5B0%5D = entity;%0A%0A            System.arraycopy( args , 0 , para , 1 , args.length  );%0A        %7D%0A
 %0A   
@@ -1324,23 +1324,33 @@
 w , 
-skip ) , entity
+entity.get_pks() ) , para
  );%0A

修改getter名,以免冲突