Resource:
@RestController
@Api(description = "字典用户管理服务")
@DustMapping(value = "/dm", method = RequestMethod.POST)
public class AddUserResource {
@Autowired
DictManageService dms;
@ApiOperation(value = "更新应用",
consumes = "application/x-www-form-urlencoded", produces = "application/json")
@ApiImplicitParams({
@ApiImplicitParam(name = "data", value = "数据对象(JSON)", required = true, dataType = "json",
paramType = "form", example = "{}")
})
@ApiResponses({
@ApiResponse(code = 200,
message = "| 错误码 | 释义 |\n" +
"| - | - |\n" +
"| SUCCESS | 成功 |\n" +
"| DB_ERR | 数据库异常 |\n")
})
@DustMapping("/user/updateUser")
public Object updateUserInfo(String data) throws SQLException, DustMsException, BuzException {
checkNotNull(data);
JSONObject row = JSON.parseObject(data);
row.put("$rowState", RowState.MODIFIED);
return dms.save("user" , row);
}
@ApiOperation(value = "删除用户",
consumes = "application/x-www-form-urlencoded", produces = "application/json")
@ApiImplicitParams({
@ApiImplicitParam(name = "uid", value = "用户ID", required = true, dataType = "int",
paramType = "form", example = "{}")
})
@ApiResponses({
@ApiResponse(code = 200,
message = "| 错误码 | 释义 |\n" +
"| - | - |\n" +
"| SUCCESS | 成功 |\n" +
"| DB_ERR | 数据库异常 |\n")
})
@DustMapping("/user/deleteuser")
public Object deleteUserInfo(String uid) throws SQLException, DustMsException, BuzException {
// checkNotNull(id);
return dms.delete("user", uid);
}
@ApiOperation(value = "获取用户列表",
consumes = "application/x-www-form-urlencoded", produces = "application/json")
@ApiResponses({
@ApiResponse(code = 200,
message = "| 错误码 | 释义 |\n" +
"| - | - |\n" +
"| SUCCESS | 成功 |\n" +
"| DB_ERR | 数据库异常 |\n")
})
@DustMapping("/user/userlist")
public Object listUser() throws SQLException, DustMsException, BuzException {
return dms.list("user", null);
}
@ApiOperation(value = "新增用户",
consumes = "application/x-www-form-urlencoded", produces = "application/json")
@ApiImplicitParams({
@ApiImplicitParam(name = "data", value = "数据对象(JSON)", required = true, dataType = "json",
paramType = "form", example = "{}")
})
@ApiResponses({
@ApiResponse(code = 200,
message = "| 错误码 | 释义 |\n" +
"| - | - |\n" +
"| SUCCESS | 成功 |\n" +
"| DB_ERR | 数据库异常 |\n")
})
@DustMapping("/app/addUser")
public Object addUserInfo(String data) throws SQLException, DustMsException, BuzException {
checkNotNull(data);
JSONObject row = JSON.parseObject(data);
row.put("$rowState", RowState.ADDED);
return dms.save("user", row);
}
}
Serivice:
@SuppressWarnings("SpringJavaAutowiringInspection")
@Service
@EnableConfigurationProperties(DmProperties.class)
public class DictManageService extends BaseService {
@Autowired
DbAdapterManager dbAdapterManager;
@Autowired
DmProperties dmProperties;
private ISqlAdapter getDictSqlAdapter() throws SQLException {
ISqlAdapter sqlAdapter;
if (StringUtils.isNotEmpty(dmProperties.getDataSourceName())) {
sqlAdapter = dbAdapterManager.getAdapter(dmProperties.getDataSourceName());
} else {
sqlAdapter = DictGlobalConfig.getSqlAdapter();
}
if (sqlAdapter == null) {
throw new SQLException("DB not found");
}
return sqlAdapter;
}
public DataObj list(String alias, Condition condition) throws SQLException, BuzException, DustMsException {
ISqlAdapter sqlAdapter = getDictSqlAdapter();
DataObj obj = DataObjBuilder.create(com.hisense.dm.VERSION.APP_ID, "basic", alias);
if (condition != null) {
obj.addCondition(condition);
}
obj.search(sqlAdapter);
sqlAdapter.commitAndCloseQuiet();
return obj;
}
public DataObjRow save(String alias, JSONObject data) throws SQLException, BuzException, DustMsException {
ISqlAdapter sqlAdapter = getDictSqlAdapter();
DataObj obj = DataObjBuilder.create(com.hisense.dm.VERSION.APP_ID, "basic", alias);
json2Row(data, obj);
obj.validate();
RowState rowState = obj.moveTo(0).getRowState();
obj.save(sqlAdapter);
sqlAdapter.commit();
String id = Converter.toString(obj.moveTo(0).getValue("id"));
obj.addCondition(new Condition(obj.getTableName(), "id", id));
obj.search(sqlAdapter);
return obj.moveTo(0);
}
public boolean delete(String alias, String id) throws SQLException {
ISqlAdapter sqlAdapter = getDictSqlAdapter();
DataObj obj = DataObjBuilder.create(com.hisense.dm.VERSION.APP_ID, "basic", alias);
SqlCommand cmd = new SqlCommand("DELETE FROM " + obj.getTableName());
cmd.appendWhere("id=:id");
cmd.setParameter("id", id);
sqlAdapter.update(cmd);
sqlAdapter.commitAndCloseQuiet();
return true;
}
public boolean deleteObj(String id) throws SQLException {
ISqlAdapter sqlAdapter = getDictSqlAdapter();
//dataobj
SqlCommand deleteObj = new SqlCommand("DELETE FROM " + "dataobj");
deleteObj.appendWhere("id=:id");
deleteObj.setParameter("id", id);
sqlAdapter.update(deleteObj);
//dataobj_column
SqlCommand deleteColumn = new SqlCommand("DELETE FROM " + "dataobj_column");
deleteColumn.appendWhere("master_id=:id");
deleteColumn.setParameter("id", id);
sqlAdapter.update(deleteColumn);
//dataobj_table
SqlCommand deleteTable = new SqlCommand("DELETE FROM " + "dataobj_table");
deleteTable.appendWhere("master_id=:id");
deleteTable.setParameter("id", id);
sqlAdapter.update(deleteTable);
sqlAdapter.commitAndCloseQuiet();
return true;
}
public String getTableScript(String id, String dbType) {
DataObj obj = DataObjBuilder.create(Converter.toLong(id));
return obj.toTableScript(dbType);
}
public JSONObject getSchemaJSON(String id) {
DataObj obj = DataObjBuilder.create(Converter.toLong(id));
return obj.toSchemaJson();
}
}