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.
262 lines
8.7 KiB
262 lines
8.7 KiB
package com.eco.plugin.xxx.bjgjdata.db.controller; |
|
|
|
import com.eco.plugin.xxx.bjgjdata.db.bean.DBEntity; |
|
import com.eco.plugin.xxx.bjgjdata.db.bean.OrgEntity; |
|
import com.eco.plugin.xxx.bjgjdata.db.bean.RecordEntity; |
|
import com.eco.plugin.xxx.bjgjdata.db.dao.DBDao; |
|
import com.eco.plugin.xxx.bjgjdata.db.dao.OrgDao; |
|
import com.eco.plugin.xxx.bjgjdata.db.dao.RecordDao; |
|
import com.eco.plugin.xxx.bjgjdata.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 xxx |
|
* @version 10.0 |
|
* Created by xxx 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, RecordDao.DAO, OrgDao.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).getByRybh(dbe.getRybh()); |
|
if(ae != null ){ |
|
ae.setUpdatetime(dbe.getUpdatetime()); |
|
ae.setJgmc(dbe.getJgmc()); |
|
ae.setFgsmc(dbe.getFgsmc()); |
|
ae.setWlkh(dbe.getWlkh()); |
|
ae.setRymc(dbe.getRymc()); |
|
context.getDAO(DBDao.class).update(ae); |
|
}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 batchOrg(List<OrgEntity> addOrUpdate, List<OrgEntity> delete){ |
|
//新增或者删除 |
|
if(addOrUpdate != null && addOrUpdate.size() > 0){ |
|
for(final OrgEntity dbe : addOrUpdate){ |
|
try{ |
|
accessor.runDMLAction(new DBAction<Boolean>() { |
|
@Override |
|
public Boolean run(DAOContext context) throws Exception { |
|
OrgEntity ae =context.getDAO(OrgDao.class).getByRybh(dbe.getJgid()); |
|
if(ae != null ){ |
|
dbe.setId(ae.getId()); |
|
context.getDAO(OrgDao.class).update(ae); |
|
}else{ |
|
context.getDAO(OrgDao.class).add(dbe); |
|
} |
|
return true; |
|
} |
|
}); |
|
}catch(Throwable e){ |
|
FRUtils.FRLogError("batch addOrUpdateOrg exception ->"+e.getMessage() + dbe.toString()); |
|
} |
|
} |
|
} |
|
|
|
if(delete != null && delete.size() > 0){ |
|
for(final OrgEntity dbe : delete){ |
|
|
|
try{ |
|
accessor.runDMLAction(new DBAction<Boolean>() { |
|
@Override |
|
public Boolean run(DAOContext context) throws Exception { |
|
context.getDAO(OrgDao.class).remove(dbe.getId()); |
|
return true; |
|
} |
|
}); |
|
}catch(Throwable e){ |
|
FRUtils.FRLogError("batch deleteOrg 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 { |
|
DBEntity ae =context.getDAO(DBDao.class).getById(addOrUpdate.getId()); |
|
if(ae != null ){ |
|
context.getDAO(DBDao.class).update(addOrUpdate); |
|
}else{ |
|
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; |
|
} |
|
|
|
/** |
|
* 添加记录 |
|
* @param recordEntity |
|
* @return |
|
*/ |
|
public static boolean addRecord(RecordEntity recordEntity){ |
|
|
|
try{ |
|
accessor.runDMLAction(new DBAction<Boolean>() { |
|
@Override |
|
public Boolean run(DAOContext context) throws Exception { |
|
context.getDAO(RecordDao.class).add(recordEntity); |
|
return true; |
|
} |
|
}); |
|
}catch(Throwable e){ |
|
FRUtils.FRLogError("addRecord exception ->"+e.getMessage() ); |
|
return false; |
|
} |
|
|
|
|
|
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(); |
|
} |
|
}
|
|
|