|
|
|
package com.tptj.demo.hg.db.access.provider;
|
|
|
|
|
|
|
|
import com.fanruan.api.log.LogKit;
|
|
|
|
import com.fr.db.fun.impl.AbstractDBAccessProvider;
|
|
|
|
import com.fr.decision.base.util.UUIDUtil;
|
|
|
|
import com.fr.intelli.record.Focus;
|
|
|
|
import com.fr.record.analyzer.EnableMetrics;
|
|
|
|
import com.fr.stable.StringUtils;
|
|
|
|
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.ArrayList;
|
|
|
|
import java.util.List;
|
|
|
|
|
|
|
|
/**
|
|
|
|
* @author 秃破天际
|
|
|
|
* @version 10.0
|
|
|
|
* Created by 秃破天际 on 2021-04-25
|
|
|
|
**/
|
|
|
|
@EnableMetrics
|
|
|
|
public class DemoDbController extends AbstractDBAccessProvider {
|
|
|
|
|
|
|
|
private static DBAccessor accessor;
|
|
|
|
|
|
|
|
public static DBAccessor getAccessor() {
|
|
|
|
return accessor;
|
|
|
|
}
|
|
|
|
|
|
|
|
@Override
|
|
|
|
public DAOProvider[] registerDAO() {
|
|
|
|
return new DAOProvider[]{
|
|
|
|
DemoDao.DAO,
|
|
|
|
LinkDao.DAO
|
|
|
|
};
|
|
|
|
}
|
|
|
|
|
|
|
|
@Override
|
|
|
|
public void onDBAvailable(DBAccessor accessor) {
|
|
|
|
DemoDbController.accessor = accessor;
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* 业务对象——增
|
|
|
|
* @param bean
|
|
|
|
*/
|
|
|
|
public static void add( final DataBean bean ){
|
|
|
|
try{
|
|
|
|
accessor.runDMLAction(new DBAction<Boolean>() {
|
|
|
|
@Override
|
|
|
|
public Boolean run(DAOContext context) throws Exception {
|
|
|
|
context.getDAO(DemoDao.class).add( loadDemoEntity(bean,true) );
|
|
|
|
context.getDAO(LinkDao.class).add( loadLinkEntities(bean,true) );
|
|
|
|
return true;
|
|
|
|
}
|
|
|
|
});
|
|
|
|
}catch(Throwable e){
|
|
|
|
LogKit.error(e,"Add DataBean Error:{}",e.getMessage());
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* 业务对象——删
|
|
|
|
* @param id
|
|
|
|
*/
|
|
|
|
public static DataBean delete( final String id ){
|
|
|
|
try{
|
|
|
|
DemoEntity entity = accessor.runQueryAction((context)->
|
|
|
|
context.getDAO(DemoDao.class).getById(id)
|
|
|
|
);
|
|
|
|
if( null == entity ){
|
|
|
|
return null;
|
|
|
|
}
|
|
|
|
return accessor.runDMLAction(new DBAction<DataBean>() {
|
|
|
|
@Override
|
|
|
|
public DataBean run(DAOContext context) throws Exception {
|
|
|
|
context.getDAO(DemoDao.class).remove( id );
|
|
|
|
List<LinkEntity> list = context.getDAO(LinkDao.class).removeByKey(entity.getKey());
|
|
|
|
return toDataBean(entity,list);
|
|
|
|
}
|
|
|
|
});
|
|
|
|
}catch(Throwable e){
|
|
|
|
LogKit.error(e,"Delete DataBean[{}] Error:{}",id,e.getMessage());
|
|
|
|
}
|
|
|
|
return null;
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* 业务对象——改
|
|
|
|
* @param bean
|
|
|
|
*/
|
|
|
|
public static void update( DataBean bean ){
|
|
|
|
try{
|
|
|
|
accessor.runDMLAction(new DBAction<Boolean>() {
|
|
|
|
@Override
|
|
|
|
public Boolean run(DAOContext context) throws Exception {
|
|
|
|
context.getDAO(DemoDao.class).update( loadDemoEntity(bean,false) );
|
|
|
|
context.getDAO(LinkDao.class).addOrUpdate( loadLinkEntities(bean,false) );
|
|
|
|
return true;
|
|
|
|
}
|
|
|
|
});
|
|
|
|
}catch(Throwable e){
|
|
|
|
LogKit.error(e,"Update DataBean Error:{}",e.getMessage());
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
/**
|
|
|
|
* 业务对象——查
|
|
|
|
* @param key
|
|
|
|
* @return
|
|
|
|
*/
|
|
|
|
@Focus(id = "com.tptj.demo.hg.decision.db.access.provider.v10",text = "DBAccessProvider")
|
|
|
|
public static List<DataBean> like(String key ){
|
|
|
|
try{
|
|
|
|
return accessor.runQueryAction(new DBAction<List<DataBean>>() {
|
|
|
|
@Override
|
|
|
|
public List<DataBean> run(DAOContext context) throws Exception {
|
|
|
|
List<DataBean> result = new ArrayList<DataBean>();
|
|
|
|
List<DemoEntity> list = context.getDAO(DemoDao.class).likeByKey(key);
|
|
|
|
for( DemoEntity item : list ){
|
|
|
|
List<LinkEntity> links = context.getDAO(LinkDao.class).getByKey(item.getKey());
|
|
|
|
result.add( toDataBean(item,links) );
|
|
|
|
}
|
|
|
|
return result;
|
|
|
|
}
|
|
|
|
});
|
|
|
|
}catch(Throwable e){
|
|
|
|
LogKit.error(e,"Get DataBeans{} Error:{}",key,e.getMessage());
|
|
|
|
}
|
|
|
|
return new ArrayList<DataBean>();
|
|
|
|
}
|
|
|
|
|
|
|
|
private static DemoEntity loadDemoEntity( DataBean bean, boolean isAdd ){
|
|
|
|
DemoEntity entity = new DemoEntity();
|
|
|
|
if(isAdd || StringUtils.isEmpty( bean.getId() ) ){
|
|
|
|
bean.setId(UUIDUtil.generate());
|
|
|
|
}
|
|
|
|
entity.setId( bean.getId() );
|
|
|
|
entity.setKey( bean.getKey() );
|
|
|
|
entity.setBean( bean.getBean() );
|
|
|
|
return entity;
|
|
|
|
}
|
|
|
|
|
|
|
|
private static DataBean toDataBean( DemoEntity entity,List<LinkEntity> list ){
|
|
|
|
DataBean bean = new DataBean();
|
|
|
|
bean.setId( entity.getId() );
|
|
|
|
bean.setKey( entity.getKey() );
|
|
|
|
bean.setBean( entity.getBean() );
|
|
|
|
bean.setAttr3( toAttrBeans(list) );
|
|
|
|
return bean;
|
|
|
|
}
|
|
|
|
|
|
|
|
private static List<DemoBean> toAttrBeans( List<LinkEntity> links ){
|
|
|
|
List<DemoBean> list = new ArrayList<DemoBean>();
|
|
|
|
for( LinkEntity item : links ){
|
|
|
|
DemoBean bean = new DemoBean();
|
|
|
|
bean.setAttr1(item.getId());
|
|
|
|
bean.setAttr2(item.getAttr3());
|
|
|
|
list.add( bean );
|
|
|
|
}
|
|
|
|
return list;
|
|
|
|
}
|
|
|
|
|
|
|
|
private static List<LinkEntity> loadLinkEntities( DataBean bean, boolean isAdd ){
|
|
|
|
List<LinkEntity> list = new ArrayList<LinkEntity>();
|
|
|
|
List<DemoBean> attr3 = bean.getAttr3();
|
|
|
|
for( DemoBean item : attr3 ){
|
|
|
|
if(isAdd || StringUtils.isEmpty( item.getAttr1() ) ){
|
|
|
|
item.setAttr1(UUIDUtil.generate());
|
|
|
|
}
|
|
|
|
list.add( new LinkEntity()
|
|
|
|
.id( item.getAttr1() )
|
|
|
|
.key( bean.getKey() )
|
|
|
|
.attr3( item.getAttr2() )
|
|
|
|
);
|
|
|
|
}
|
|
|
|
return list;
|
|
|
|
}
|
|
|
|
|
|
|
|
}
|