增加 save or update

This commit is contained in:
zhou-hao
2018-07-08 20:54:48 +08:00
parent fac2554099
commit f38b3669ec
3 changed files with 55 additions and 18 deletions

View File

@@ -14,6 +14,8 @@ import java.util.List;
* @since 3.0
*/
public interface DynamicFormOperationService {
String idProperty = "id";
<T> PagerResult<T> selectPager(String formId, QueryParamEntity paramEntity);
<T> T selectSingle(String formId, QueryParamEntity paramEntity);
@@ -24,13 +26,15 @@ public interface DynamicFormOperationService {
<T> int update(String formId, UpdateParamEntity<T> paramEntity);
<T> T updateById(String formId, String id, T data);
<T> T updateById(String formId, Object id, T data);
<T> void insert(String formId, T entity);
<T> T insert(String formId, T entity);
<T> T saveOrUpdate(String formId, T entity);
int delete(String formId, DeleteParamEntity paramEntity);
int deleteById(String formId, String id);
int deleteById(String formId, Object id);
}

View File

@@ -8,6 +8,7 @@ import org.hswebframework.ezorm.rdb.RDBDatabase;
import org.hswebframework.ezorm.rdb.RDBQuery;
import org.hswebframework.ezorm.rdb.RDBTable;
import org.hswebframework.web.NotFoundException;
import org.hswebframework.web.bean.FastBeanCopier;
import org.hswebframework.web.commons.entity.PagerResult;
import org.hswebframework.web.commons.entity.param.DeleteParamEntity;
import org.hswebframework.web.commons.entity.param.QueryParamEntity;
@@ -25,7 +26,9 @@ import org.springframework.transaction.annotation.Transactional;
import org.springframework.util.StringUtils;
import java.sql.SQLException;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
import java.util.Objects;
@Service("dynamicFormOperationService")
@@ -114,11 +117,30 @@ public class SimpleDynamicFormOperationService implements DynamicFormOperationSe
@Override
@SneakyThrows
public <T> void insert(String formId, T entity) {
public <T> T insert(String formId, T entity) {
RDBTable<T> table = getTable(formId);
Insert<T> insert = table.createInsert();
eventPublisher.publishEvent(new FormDataInsertBeforeEvent<>(formId, table, entity));
insert.value(entity).exec();
return entity;
}
@Override
@SneakyThrows
public <T> T saveOrUpdate(String formId, T entity) {
Map<String, Object> map = FastBeanCopier.copy(entity, new HashMap<>(), FastBeanCopier.include(idProperty));
Object id = map.get(idProperty);
if (id == null) {
return insert(formId, entity);
}
int total = getTable(formId).createQuery().where(idProperty, id).total();
if (total > 0) {
return updateById(formId, String.valueOf(id), entity);
}
return insert(formId, entity);
}
@Override
@@ -134,21 +156,21 @@ public class SimpleDynamicFormOperationService implements DynamicFormOperationSe
@Override
@SneakyThrows
public int deleteById(String formId, String id) {
public int deleteById(String formId, Object id) {
Objects.requireNonNull(id, "主键不能为空");
RDBTable table = getTable(formId);
return table.createDelete().where("id", id).exec();
return table.createDelete().where(idProperty, id).exec();
}
@Override
@SneakyThrows
public <T> T updateById(String formId, String id, T data) {
public <T> T updateById(String formId, Object id, T data) {
Objects.requireNonNull(id, "主键不能为空");
RDBTable<T> table = getTable(formId);
eventPublisher.publishEvent(new FormDataUpdateBeforeEvent<>(formId, table, data, id));
table.createUpdate()
.set(data)
.where("id", id)
.where(idProperty, id)
.exec();
return data;
}

View File

@@ -2,8 +2,10 @@ package org.hswebframework.web.controller.form;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import io.swagger.annotations.ResponseHeader;
import org.hswebframework.web.authorization.Permission;
import org.hswebframework.web.authorization.annotation.Authorize;
import org.hswebframework.web.authorization.annotation.Logical;
import org.hswebframework.web.commons.entity.PagerResult;
import org.hswebframework.web.commons.entity.param.DeleteParamEntity;
import org.hswebframework.web.commons.entity.param.QueryParamEntity;
@@ -11,6 +13,7 @@ import org.hswebframework.web.commons.entity.param.UpdateParamEntity;
import org.hswebframework.web.controller.message.ResponseMessage;
import org.hswebframework.web.service.form.DynamicFormOperationService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.http.HttpStatus;
import org.springframework.web.bind.annotation.*;
import java.util.List;
@@ -45,7 +48,7 @@ public class DynamicFormOperationController {
@GetMapping("/{formId}/no-paging")
@ApiOperation("不分页动态查询")
@Authorize(action = Permission.ACTION_QUERY)
public ResponseMessage<List<Object>> select(@PathVariable String formId, QueryParamEntity paramEntity) {
public ResponseMessage<List<Object>> selectNoPaging(@PathVariable String formId, QueryParamEntity paramEntity) {
paramEntity.setPaging(false);
return ResponseMessage.ok(dynamicFormOperationService.select(formId, paramEntity));
}
@@ -67,34 +70,42 @@ public class DynamicFormOperationController {
@PostMapping("/{formId}")
@ApiOperation("新增")
@Authorize(action = Permission.ACTION_ADD)
@ResponseStatus(HttpStatus.CREATED)
public ResponseMessage<Map<String, Object>> add(@PathVariable String formId,
@RequestBody Map<String, Object> data) {
dynamicFormOperationService.insert(formId, data);
return ResponseMessage.ok(data);
return ResponseMessage.ok(dynamicFormOperationService.insert(formId, data));
}
@PatchMapping("/{formId}")
@ApiOperation("新增或者修改")
@Authorize(action = {Permission.ACTION_ADD, Permission.ACTION_UPDATE}, logical = Logical.OR)
public ResponseMessage<Object> saveOrUpdate(@PathVariable String formId,
@RequestBody Map<String, Object> data) {
return ResponseMessage.ok(dynamicFormOperationService.saveOrUpdate(formId, data));
}
@PutMapping("/{formId}")
@ApiOperation("动态修改")
@Authorize(action = Permission.ACTION_UPDATE)
public ResponseMessage<Integer> update(@PathVariable String formId,
@RequestBody UpdateParamEntity<Map<String, Object>> paramEntity) {
public ResponseMessage<Integer> dynamicUpdate(@PathVariable String formId,
@RequestBody UpdateParamEntity<Map<String, Object>> paramEntity) {
return ResponseMessage.ok(dynamicFormOperationService.update(formId, paramEntity));
}
@PutMapping("/{formId}/{id}")
@ApiOperation("根据主键修改")
@Authorize(action = Permission.ACTION_UPDATE)
public ResponseMessage<Map<String, Object>> update(@PathVariable String formId,
@PathVariable String id,
@RequestBody Map<String, Object> param) {
public ResponseMessage<Map<String, Object>> updateById(@PathVariable String formId,
@PathVariable String id,
@RequestBody Map<String, Object> param) {
return ResponseMessage.ok(dynamicFormOperationService.updateById(formId, id, param));
}
@DeleteMapping("/{formId}/{id}")
@ApiOperation("根据主键删除")
@Authorize(action = Permission.ACTION_DELETE)
public ResponseMessage<Integer> delete(@PathVariable String formId,
@PathVariable String id) {
public ResponseMessage<Integer> deleteById(@PathVariable String formId,
@PathVariable String id) {
return ResponseMessage.ok(dynamicFormOperationService.deleteById(formId, id));
}
}