You can not select more than 25 topics
Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.
177 lines
5.4 KiB
177 lines
5.4 KiB
package com.eco.plugin.xx.kdxfsso.db.controller; |
|
|
|
import com.eco.plugin.xx.kdxfsso.db.bean.DBEntity; |
|
import com.eco.plugin.xx.kdxfsso.db.dao.DBDao; |
|
import com.eco.plugin.xx.kdxfsso.utils.FRUtils; |
|
import com.fr.db.fun.impl.AbstractDBAccessProvider; |
|
import com.fr.plugin.transform.FunctionRecorder; |
|
import com.fr.record.analyzer.EnableMetrics; |
|
import com.fr.stable.db.accessor.DBAccessor; |
|
import com.fr.stable.db.action.DBAction; |
|
import com.fr.stable.db.dao.DAOContext; |
|
import com.fr.stable.db.dao.DAOProvider; |
|
|
|
import java.util.List; |
|
|
|
/** |
|
* @author xx |
|
* @version 10.0 |
|
* Created by xx on 2021-07-29 |
|
**/ |
|
@EnableMetrics |
|
@FunctionRecorder |
|
public class DBController extends AbstractDBAccessProvider { |
|
|
|
private static DBAccessor accessor; |
|
|
|
public static DBAccessor getAccessor() { |
|
return accessor; |
|
} |
|
|
|
@Override |
|
public DAOProvider[] registerDAO() { |
|
return new DAOProvider[]{ |
|
DBDao.DAO |
|
}; |
|
} |
|
|
|
@Override |
|
public void onDBAvailable(DBAccessor accessor) { |
|
DBController.accessor = accessor; |
|
} |
|
|
|
|
|
/** |
|
* 批量操作 |
|
* @param addOrUpdate |
|
* @param delete |
|
* @return |
|
*/ |
|
public static boolean batch(List<DBEntity> addOrUpdate, List<DBEntity> delete){ |
|
//新增或者删除 |
|
if(addOrUpdate != null && addOrUpdate.size() > 0){ |
|
for(final DBEntity dbe : addOrUpdate){ |
|
|
|
try{ |
|
accessor.runDMLAction(new DBAction<Boolean>() { |
|
@Override |
|
public Boolean run(DAOContext context) throws Exception { |
|
DBEntity ae =context.getDAO(DBDao.class).getById(dbe.getId()); |
|
if(ae != null ){ |
|
context.getDAO(DBDao.class).update(dbe); |
|
}else{ |
|
context.getDAO(DBDao.class).add(dbe); |
|
} |
|
return true; |
|
} |
|
}); |
|
}catch(Throwable e){ |
|
FRUtils.FRLogError("batch addOrUpdate exception ->"+e.getMessage() + dbe.toString()); |
|
} |
|
} |
|
} |
|
|
|
if(delete != null && delete.size() > 0){ |
|
for(final DBEntity dbe : delete){ |
|
|
|
try{ |
|
accessor.runDMLAction(new DBAction<Boolean>() { |
|
@Override |
|
public Boolean run(DAOContext context) throws Exception { |
|
context.getDAO(DBDao.class).remove(dbe.getId()); |
|
return true; |
|
} |
|
}); |
|
}catch(Throwable e){ |
|
FRUtils.FRLogError("batch delete exception ->"+e.getMessage() + dbe.toString()); |
|
} |
|
} |
|
} |
|
|
|
return true; |
|
} |
|
|
|
|
|
/** |
|
* 单个操作 |
|
* @param addOrUpdate |
|
* @param delete |
|
* @return |
|
*/ |
|
public static boolean single(DBEntity addOrUpdate,DBEntity delete){ |
|
//新增或者删除 |
|
if(addOrUpdate != null ){ |
|
try{ |
|
accessor.runDMLAction(new DBAction<Boolean>() { |
|
@Override |
|
public Boolean run(DAOContext context) throws Exception { |
|
context.getDAO(DBDao.class).add(addOrUpdate); |
|
return true; |
|
} |
|
}); |
|
}catch(Throwable e){ |
|
FRUtils.FRLogError("single addOrUpdate exception ->"+e.getMessage() + addOrUpdate.toString()); |
|
} |
|
} |
|
|
|
if(delete != null ){ |
|
try{ |
|
accessor.runDMLAction(new DBAction<Boolean>() { |
|
@Override |
|
public Boolean run(DAOContext context) throws Exception { |
|
context.getDAO(DBDao.class).remove(delete.getId()); |
|
return true; |
|
} |
|
}); |
|
}catch(Throwable e){ |
|
FRUtils.FRLogError("single delete exception ->"+e.getMessage() + delete.toString()); |
|
} |
|
} |
|
|
|
return true; |
|
} |
|
|
|
/** |
|
* 根据username获取信息 |
|
* @param username |
|
* @return |
|
*/ |
|
public static DBEntity getByUsername(final String username){ |
|
try{ |
|
return accessor.runQueryAction(new DBAction<DBEntity>() { |
|
@Override |
|
public DBEntity run(DAOContext context) throws Exception { |
|
DBEntity result = context.getDAO(DBDao.class).getByUsername(username); |
|
|
|
return result == null ? new DBEntity() : result; |
|
} |
|
}); |
|
}catch(Throwable e){ |
|
FRUtils.FRLogError("exception getByUsername:"+e.getMessage()); |
|
} |
|
|
|
return new DBEntity(); |
|
} |
|
|
|
/** |
|
* 根据id获取信息 |
|
* @param id |
|
* @return |
|
*/ |
|
public static DBEntity getById(final String id){ |
|
try{ |
|
return accessor.runQueryAction(new DBAction<DBEntity>() { |
|
@Override |
|
public DBEntity run(DAOContext context) throws Exception { |
|
DBEntity result = context.getDAO(DBDao.class).getById(id); |
|
|
|
return result == null ? new DBEntity() : result; |
|
} |
|
}); |
|
}catch(Throwable e){ |
|
FRUtils.FRLogError("exception getById:"+e.getMessage()); |
|
} |
|
|
|
return new DBEntity(); |
|
} |
|
}
|
|
|