提交 603928d6 编写于 作者: sq3536's avatar sq3536

entity

上级 14e35bc0
package cn.ibizlab.core.extensions.mapper; package cn.ibizlab.core.extensions.mapper;
import cn.ibizlab.core.lite.extensions.domain.EntityModel;
import cn.ibizlab.core.lite.extensions.domain.EntityObj; import cn.ibizlab.core.lite.extensions.domain.EntityObj;
import com.baomidou.mybatisplus.core.conditions.Wrapper; import com.baomidou.mybatisplus.core.conditions.Wrapper;
import com.baomidou.mybatisplus.core.mapper.BaseMapper; import com.baomidou.mybatisplus.core.mapper.BaseMapper;
import org.apache.ibatis.annotations.Insert;
import org.apache.ibatis.annotations.Param; import org.apache.ibatis.annotations.Param;
import java.util.List; import java.util.List;
...@@ -12,4 +14,19 @@ public interface DbEntityMapper extends BaseMapper<EntityObj>{ ...@@ -12,4 +14,19 @@ public interface DbEntityMapper extends BaseMapper<EntityObj>{
List<EntityObj> search(@Param("sql") String sql, @Param("ew") Wrapper<EntityObj> wrapper); List<EntityObj> search(@Param("sql") String sql, @Param("ew") Wrapper<EntityObj> wrapper);
Integer searchCount(@Param("sql") String sql, @Param("ew") Wrapper<EntityObj> wrapper); Integer searchCount(@Param("sql") String sql, @Param("ew") Wrapper<EntityObj> wrapper);
@Insert({"<script> "+" MERGE INTO ${model.tableName} T1\n" +
" USING (\n" +
"<foreach collection=\"list\" item=\"item\" index=\"index\" separator=\"UNION ALL\">\n" +
" SELECT ${model.mergeColumnSegment} FROM dual\n" +
"</foreach>\n" +
" ) T2 \n ${model.mergeSegment}"+
"</script>"})
boolean saveBatch(@Param("model") EntityModel model, @Param("list") List<EntityObj> list);
@Insert({"<script> "+" MERGE INTO ${model.tableName} T1\n" +
" USING (\n" +
" SELECT ${model.mergeColumnSegment} FROM dual\n" +
" ) T2 \n ${model.mergeSegment}"+
"</script>"})
boolean save(@Param("model") EntityModel model, @Param("entity") EntityObj entity);
} }
\ No newline at end of file
...@@ -200,6 +200,18 @@ public class EntityModel { ...@@ -200,6 +200,18 @@ public class EntityModel {
return unionKeyFields; return unionKeyFields;
} }
@JsonIgnore
@JSONField(serialize = false)
public List<FieldModel> getKeyFields() {
if(this.getKeyField()!=null&&this.getKeyField().isPhysicalField()) {
List<FieldModel> keyFields = new ArrayList<>();
keyFields.add(getKeyField());
return keyFields;
}
else
return getUnionKeyFields();
}
public FieldModel getField(String name) public FieldModel getField(String name)
{ {
if(StringUtils.isEmpty(name)) if(StringUtils.isEmpty(name))
...@@ -257,6 +269,91 @@ public class EntityModel { ...@@ -257,6 +269,91 @@ public class EntityModel {
return ""; return "";
} }
@JsonIgnore
@JSONField(serialize = false)
public String getMergeColumnSegment()
{
String sql="";
boolean first=true;
for(FieldModel fieldModel:this.getFields())
{
if(!fieldModel.isPhysicalField())
continue;
if(first)
first=false;
else
sql+=",";
sql+=("#{item."+fieldModel.getColumnName().toUpperCase()+"} "+fieldModel.getColumnName().toUpperCase());
}
return sql;
}
@JsonIgnore
@JSONField(serialize = false)
public String getMergeSegment()
{
String sql="";
sql+= " ON (";
boolean first=true;
for(FieldModel fieldModel:this.getKeyFields())
{
if(!fieldModel.isPhysicalField())
continue;
if(first)
first=false;
else
sql+=" and ";
sql+=("T1."+fieldModel.getColumnName().toUpperCase()+"=T2."+fieldModel.getColumnName().toUpperCase());
}
sql+=")\n" +
" WHEN NOT MATCHED THEN INSERT(";
first=true;
for(FieldModel fieldModel:this.getFields())
{
if(!fieldModel.isPhysicalField())
continue;
if(first)
first=false;
else
sql+=",";
sql+=(fieldModel.getColumnName().toUpperCase());
}
sql+=") VALUES (" ;
first=true;
for(FieldModel fieldModel:this.getFields())
{
if(!fieldModel.isPhysicalField())
continue;
if(first)
first=false;
else
sql+=",";
sql+=("T2."+fieldModel.getColumnName().toUpperCase());
}
sql+=")\n" +
" WHEN MATCHED THEN UPDATE SET " ;
first=true;
for(FieldModel fieldModel:this.getFields())
{
if(!fieldModel.isPhysicalField())
continue;
if(fieldModel.isKeyField())
continue;
if(fieldModel.isUnionKeyField())
continue;
if(first)
first=false;
else
sql+=",";
sql+=("T1."+fieldModel.getColumnName().toUpperCase()+"=T2."+fieldModel.getColumnName().toUpperCase());
}
return sql;
}
@JsonIgnore @JsonIgnore
@JSONField(serialize = false) @JSONField(serialize = false)
public String getDsName() public String getDsName()
......
...@@ -72,6 +72,13 @@ public class FieldModel { ...@@ -72,6 +72,13 @@ public class FieldModel {
return "UPDATEDATE".equals(this.getField().getPredefined()); return "UPDATEDATE".equals(this.getField().getPredefined());
} }
@JsonIgnore
@JSONField(serialize = false)
public boolean isCreateTimeField()
{
return "CREATEDATE".equals(this.getField().getPredefined());
}
@JsonIgnore @JsonIgnore
@JSONField(serialize = false) @JSONField(serialize = false)
public boolean isUnionKeyField() public boolean isUnionKeyField()
......
...@@ -18,6 +18,10 @@ public interface CommonEntityService { ...@@ -18,6 +18,10 @@ public interface CommonEntityService {
List<EntityObj> search(String dsName, String sql, QueryFilter filter); List<EntityObj> search(String dsName, String sql, QueryFilter filter);
boolean saveBatch(EntityModel entityModel, List<EntityObj> list);
boolean save(EntityModel entityModel, EntityObj entityObj);
EntityObj get(EntityModel entityModel, EntityObj entityObj);
void processList(DataModel dataModel, Timestamp lastModify, LiteDataCallback callback); void processList(DataModel dataModel, Timestamp lastModify, LiteDataCallback callback);
......
...@@ -146,9 +146,65 @@ public class DbEntityService extends ServiceImpl<DbEntityMapper, EntityObj> impl ...@@ -146,9 +146,65 @@ public class DbEntityService extends ServiceImpl<DbEntityMapper, EntityObj> impl
} }
} }
@Override
public boolean saveBatch(EntityModel entityModel, List<EntityObj> list) {
try {
dstDataSourceService.initDataSource(entityModel.getDsName());
DynamicDataSourceContextHolder.push(entityModel.getDsName());
list.forEach(entityObj -> {
entityObj.keySet().forEach(key -> {
if(!key.toUpperCase().equals(key))
entityObj.put(key.toUpperCase(),entityObj.get(key));
});
});
return baseMapper.saveBatch(entityModel, list);
} catch (Exception ex) {
log.error("详细错误信息:" + ex.getMessage() + ", 执行sql:" + entityModel.getTableName());
return false;
} finally {
DynamicDataSourceContextHolder.poll();
}
}
@Override
public boolean save(EntityModel entityModel, EntityObj entityObj) {
try {
dstDataSourceService.initDataSource(entityModel.getDsName());
DynamicDataSourceContextHolder.push(entityModel.getDsName());
entityObj.keySet().forEach(key -> {
if(!key.toUpperCase().equals(key))
entityObj.put(key.toUpperCase(),entityObj.get(key));
});
return baseMapper.save(entityModel, entityObj);
} catch (Exception ex) {
log.error("详细错误信息:" + ex.getMessage() + ", 执行sql:" + entityModel.getTableName());
return false;
} finally {
DynamicDataSourceContextHolder.poll();
}
}
@Override
public EntityObj get(EntityModel entityModel, EntityObj entityObj) {
QueryFilter filter=QueryFilter.createQuery();
entityModel.getKeyFields().forEach(item->{
Object val=entityObj.get(item.getColumnName());
if(val!=null)
{
filter.eq(item.getColumnName().toUpperCase(),val);
}
});
List<EntityObj> list=this.selectBase(entityModel,filter);
if(list.size()==1)
entityObj.putAll(list.get(0));
return entityObj;
}
@Autowired @Autowired
@Lazy @Lazy
......
...@@ -56,7 +56,32 @@ public class LiteDataService { ...@@ -56,7 +56,32 @@ public class LiteDataService {
} }
public boolean saveBatch(String systemId, String entityName, List<EntityObj> list) {
EntityModel entityModel = liteModelService.getEntityModel(systemId,entityName);
splitList(list, 500).forEach(objs->{
getEntityService(entityModel.getDsName()).saveBatch(entityModel,objs);
});
return true;
}
public boolean save(String systemId, String entityName, EntityObj entityObj) {
EntityModel entityModel = liteModelService.getEntityModel(systemId,entityName);
return getEntityService(entityModel.getDsName()).save(entityModel,entityObj);
}
public EntityObj get(String systemId, String entityName, EntityObj entityObj) {
EntityModel entityModel = liteModelService.getEntityModel(systemId,entityName);
return getEntityService(entityModel.getDsName()).get(entityModel,entityObj);
}
public List<EntityObj> search(String systemId, String entityName, QueryFilter filter) {
return search(systemId,entityName,"BASE",filter);
}
public List<EntityObj> search(String systemId, String entityName, String dataset,QueryFilter filter) {
EntityModel entityModel = liteModelService.getEntityModel(systemId,entityName);
return getEntityService(entityModel.getDsName()).search(dataset,entityModel,filter);
}
public void processDataModel(String metaModelId,Timestamp lastModify,LiteDataCallback callback) public void processDataModel(String metaModelId,Timestamp lastModify,LiteDataCallback callback)
......
...@@ -39,6 +39,20 @@ public class MongoEntityService implements CommonEntityService{ ...@@ -39,6 +39,20 @@ public class MongoEntityService implements CommonEntityService{
return null; return null;
} }
@Override
public boolean saveBatch(EntityModel entityModel, List<EntityObj> list) {
return false;
}
@Override
public boolean save(EntityModel entityModel, EntityObj entityObj) {
return false;
}
@Override
public EntityObj get(EntityModel entityModel, EntityObj entityObj) {
return null;
}
@Override @Override
......
...@@ -13,6 +13,7 @@ import cn.ibizlab.util.dict.CodeItem; ...@@ -13,6 +13,7 @@ import cn.ibizlab.util.dict.CodeItem;
import cn.ibizlab.util.dict.CodeList; import cn.ibizlab.util.dict.CodeList;
import cn.ibizlab.util.dict.Option; import cn.ibizlab.util.dict.Option;
import cn.ibizlab.util.errors.BadRequestAlertException; import cn.ibizlab.util.errors.BadRequestAlertException;
import cn.ibizlab.util.filter.QueryFilter;
import cn.ibizlab.util.helper.DataObject; import cn.ibizlab.util.helper.DataObject;
import com.alibaba.fastjson.JSON; import com.alibaba.fastjson.JSON;
import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper; import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
...@@ -27,8 +28,10 @@ import org.springframework.http.ResponseEntity; ...@@ -27,8 +28,10 @@ import org.springframework.http.ResponseEntity;
import org.springframework.util.StringUtils; import org.springframework.util.StringUtils;
import org.springframework.web.bind.annotation.*; import org.springframework.web.bind.annotation.*;
import javax.servlet.http.HttpServletRequest;
import java.util.ArrayList; import java.util.ArrayList;
import java.util.List; import java.util.List;
import java.util.Map;
@Slf4j @Slf4j
@Api(tags = {"接口" }) @Api(tags = {"接口" })
...@@ -42,11 +45,9 @@ public class DstCoreResource { ...@@ -42,11 +45,9 @@ public class DstCoreResource {
@Autowired @Autowired
private LiteDataService liteDataService; private LiteDataService liteDataService;
@Autowired
private DbEntityService dbEntityService;
@RequestMapping(method = RequestMethod.GET, value = "/dst/test/{modelid}/data/{key}") @RequestMapping(method = RequestMethod.GET, value = "/dst/datamodels/{modelid}/data/{key}")
public ResponseEntity<ModelObj> getstr(@PathVariable(name="modelid",required = false) String modelid,@PathVariable(name="key",required = false) String key) public ResponseEntity<ModelObj> getModelObj(@PathVariable(name="modelid",required = false) String modelid,@PathVariable(name="key",required = false) String key)
{ {
if(StringUtils.isEmpty(modelid)) if(StringUtils.isEmpty(modelid))
...@@ -178,7 +179,32 @@ public class DstCoreResource { ...@@ -178,7 +179,32 @@ public class DstCoreResource {
return ResponseEntity.status(HttpStatus.OK).body(liteModelService.getEntityModel(system,entity).getNesteds()); return ResponseEntity.status(HttpStatus.OK).body(liteModelService.getEntityModel(system,entity).getNesteds());
} }
@RequestMapping(method = RequestMethod.POST, value = "/dst/{system}/entitys/{entity}/get")
public ResponseEntity<EntityObj> getEntity(@PathVariable("system") String system, @PathVariable("entity") String entity, HttpServletRequest request) {
Map<String, String[]> params = request.getParameterMap();
EntityObj entityObj = new EntityObj();
for (String key : params.keySet()) {
entityObj.set(key.toUpperCase(),params.get(key)[0]);
}
return ResponseEntity.status(HttpStatus.OK).body(liteDataService.get(system,entity,entityObj));
}
@RequestMapping(method = RequestMethod.POST, value = "/dst/{system}/entitys/{entity}/search{dataset}")
public ResponseEntity<List<EntityObj>> searchEntity(@PathVariable("system") String system, @PathVariable("entity") String entity, @PathVariable(value = "dataset",required = false) String dataset,@RequestBody QueryFilter filter) {
if(StringUtils.isEmpty(dataset))
dataset="BASE";
return ResponseEntity.status(HttpStatus.OK).body(liteDataService.search(system,entity,filter));
}
@RequestMapping(method = RequestMethod.POST, value = "/dst/{system}/entitys/{entity}/save")
public ResponseEntity<Boolean> saveEntity(@PathVariable("system") String system, @PathVariable("entity") String entity,@RequestBody EntityObj entityObj) {
return ResponseEntity.status(HttpStatus.OK).body(liteDataService.save(system,entity,entityObj));
}
@RequestMapping(method = RequestMethod.POST, value = "/dst/{system}/entitys/{entity}/batch")
public ResponseEntity<Boolean> saveEntity(@PathVariable("system") String system, @PathVariable("entity") String entity,@RequestBody List<EntityObj> list) {
return ResponseEntity.status(HttpStatus.OK).body(liteDataService.saveBatch(system,entity,list));
}
@RequestMapping(method = RequestMethod.GET, value = "/dst/sysapps") @RequestMapping(method = RequestMethod.GET, value = "/dst/sysapps")
public ResponseEntity<List<DstSystem>> getSysApps() { public ResponseEntity<List<DstSystem>> getSysApps() {
......
Markdown 格式
0% or
您添加了 0 到此讨论。请谨慎行事。
先完成此消息的编辑!
想要评论请 注册