|
@@ -1,25 +1,24 @@
|
|
|
package com.boman.system.common;
|
|
|
|
|
|
-import com.alibaba.fastjson.JSONArray;
|
|
|
import com.alibaba.fastjson.JSONObject;
|
|
|
-import com.boman.gen.domain.GenTable;
|
|
|
+import com.boman.common.core.utils.SpringUtils;
|
|
|
+import com.boman.common.core.utils.StringUtils;
|
|
|
+import com.boman.gen.domain.GenTableColumn;
|
|
|
+import com.boman.system.service.impl.BaseSaveService;
|
|
|
+import com.boman.system.utils.IdUtils;
|
|
|
import org.slf4j.Logger;
|
|
|
import org.slf4j.LoggerFactory;
|
|
|
import org.springframework.beans.factory.annotation.Autowired;
|
|
|
import org.springframework.stereotype.Component;
|
|
|
-import org.springframework.transaction.annotation.Propagation;
|
|
|
-import org.springframework.transaction.annotation.Transactional;
|
|
|
|
|
|
-import javax.annotation.Resource;
|
|
|
-import java.lang.reflect.Method;
|
|
|
-import java.util.Collection;
|
|
|
+import java.util.List;
|
|
|
|
|
|
|
|
|
* @author shiqian
|
|
|
* @description
|
|
|
* @date 2021年03月22日 09:51
|
|
|
**/
|
|
|
-@Component("tableServiceCmdService-one")
|
|
|
+@Component
|
|
|
public class TableServiceCmdService {
|
|
|
|
|
|
@Autowired
|
|
@@ -35,184 +34,21 @@ public class TableServiceCmdService {
|
|
|
return execute(baseTableDTO, actionName);
|
|
|
}
|
|
|
|
|
|
- public final ValueHolder execute(BaseTableDTO baseTableDTO, String actionName) throws Exception {
|
|
|
+ public final ValueHolder execute(BaseTableDTO baseTableDTO, String actionName) {
|
|
|
TableServiceContext context = TableServiceContext.createFrom(baseTableDTO, actionName);
|
|
|
+ BaseSaveService baseSaveService = SpringUtils.getBean(BaseSaveService.class);
|
|
|
+
|
|
|
+ List<GenTableColumn> columns = context.getTable().getColumns();
|
|
|
+ String pkName = IdUtils.getPkName(columns);
|
|
|
+ StringUtils.requireNonNull(pkName);
|
|
|
+ Long maxId = IdUtils.getMaxId(baseTableDTO.getTable(), pkName);
|
|
|
+ RowResult rowResult = baseSaveService.insertRow(context.getRealTableName(), pkName, maxId, context.getRows().get(0));
|
|
|
|
|
|
- TableService service = getServiceByBusinessName(context.getTable());
|
|
|
-
|
|
|
- service.configure(context);
|
|
|
-
|
|
|
- executeAction(context, service);
|
|
|
-
|
|
|
-
|
|
|
- return buildResult(context);
|
|
|
-
|
|
|
-
|
|
|
-
|
|
|
-
|
|
|
-
|
|
|
-
|
|
|
-
|
|
|
- }
|
|
|
-
|
|
|
- public TableService getServiceByBusinessName(GenTable table){
|
|
|
- return ApplicationContextHandle.getBean(table.getBusinessName() + "Service", TableService.class);
|
|
|
- }
|
|
|
-
|
|
|
-
|
|
|
-
|
|
|
-
|
|
|
-
|
|
|
-
|
|
|
-
|
|
|
-
|
|
|
-
|
|
|
-
|
|
|
-
|
|
|
-
|
|
|
-
|
|
|
-
|
|
|
-
|
|
|
-
|
|
|
-
|
|
|
-
|
|
|
-
|
|
|
-
|
|
|
-
|
|
|
-
|
|
|
-
|
|
|
- private void executeAction(TableServiceContext context, TableService service) throws Exception {
|
|
|
- Collection<Filter> filters = context.getFilters();
|
|
|
- for (MainTableRecord row : context.getRows()) {
|
|
|
-
|
|
|
-
|
|
|
-
|
|
|
-
|
|
|
-
|
|
|
-
|
|
|
- executeActionOnRow(context, service, row);
|
|
|
- }
|
|
|
- }
|
|
|
-
|
|
|
- private ValidationResults validate(TableServiceContext context, MainTableRecord row) {
|
|
|
- Collection<Validator> validators = context.getValidators();
|
|
|
- ValidationResults result = new ValidationResults();
|
|
|
- for (Validator validator : validators) {
|
|
|
- try {
|
|
|
- validator.validate(context, row);
|
|
|
- } catch (Exception e) {
|
|
|
-
|
|
|
- }
|
|
|
- }
|
|
|
- return result;
|
|
|
- }
|
|
|
-
|
|
|
-
|
|
|
-
|
|
|
-
|
|
|
-
|
|
|
-
|
|
|
-
|
|
|
-
|
|
|
-
|
|
|
-
|
|
|
-
|
|
|
-
|
|
|
-
|
|
|
-
|
|
|
-
|
|
|
-
|
|
|
-
|
|
|
-
|
|
|
-
|
|
|
-
|
|
|
-
|
|
|
-
|
|
|
-
|
|
|
-
|
|
|
-
|
|
|
-
|
|
|
-
|
|
|
-
|
|
|
-
|
|
|
-
|
|
|
-
|
|
|
-
|
|
|
-
|
|
|
-
|
|
|
-
|
|
|
-
|
|
|
-
|
|
|
-
|
|
|
-
|
|
|
-
|
|
|
-
|
|
|
- @Transactional(rollbackFor = Exception.class, propagation = Propagation.REQUIRED)
|
|
|
- public void executeActionOnRow(TableServiceContext context, TableService service, MainTableRecord row) throws Exception {
|
|
|
- try {
|
|
|
- Method customAction = null;
|
|
|
- Class serviceClazz = service.getClass();
|
|
|
- String actionName = context.getActionName();
|
|
|
- try {
|
|
|
-
|
|
|
- customAction = serviceClazz.getMethod(actionName, TableServiceContext.class, MainTableRecord.class);
|
|
|
-
|
|
|
- } catch (Exception e) {
|
|
|
- LOGGER.warn(String.format("试图在类%s中查找%s方法失败!", serviceClazz, actionName));
|
|
|
- }
|
|
|
- if (customAction != null) {
|
|
|
- LOGGER.debug(String.format("调用类%s的%s方法.", serviceClazz, actionName));
|
|
|
- row.setResult((RowResult) customAction.invoke(service, context, row));
|
|
|
- } else {
|
|
|
- LOGGER.debug(String.format("调用类%s的executeAction方法.", serviceClazz));
|
|
|
- row.setResult(service.executeAction(context, row));
|
|
|
- }
|
|
|
- } catch (Exception e) {
|
|
|
-
|
|
|
-
|
|
|
-
|
|
|
-
|
|
|
-
|
|
|
-
|
|
|
-
|
|
|
-
|
|
|
-
|
|
|
-
|
|
|
-
|
|
|
- throw e;
|
|
|
- }
|
|
|
- }
|
|
|
-
|
|
|
- private ValueHolder buildResult(TableServiceContext context) {
|
|
|
ValueHolder valueHolder = new ValueHolder();
|
|
|
- boolean code = context.getRows().stream().noneMatch(v -> -1 == v.getResult().getCode());
|
|
|
- long errorSize = context.getRows().stream().filter(v -> -1 == v.getResult().getCode()).count();
|
|
|
-
|
|
|
- if (context.getRows().size() == 1) {
|
|
|
- return context.getRows().get(0).getResult().toValueHolder();
|
|
|
- } else if (code) {
|
|
|
- valueHolder.setCode(ResultCode.SUCCESS);
|
|
|
- valueHolder.setMessage("操作成功" + context.getRows().size() + "条!");
|
|
|
- } else {
|
|
|
- valueHolder.setCode(ResultCode.FAIL);
|
|
|
- valueHolder.setMessage("操作成功" + (context.getRows().size() - errorSize) + "条!,操作失败" + errorSize + "条!");
|
|
|
- JSONArray datas = new JSONArray();
|
|
|
- context.getRows().stream().filter(v -> -1 == v.getResult().getCode()).forEach(
|
|
|
- rowPostData -> {
|
|
|
- JSONObject row = new JSONObject();
|
|
|
- row.put("objid", rowPostData.getId());
|
|
|
- row.put("message", rowPostData.getResult().getMessage());
|
|
|
- datas.add(row);
|
|
|
- }
|
|
|
- );
|
|
|
-
|
|
|
- valueHolder.setData(datas);
|
|
|
- }
|
|
|
-
|
|
|
+ valueHolder.setCode(ResultCode.SUCCESS);
|
|
|
+ valueHolder.setMessage("操作成功" + ((JSONObject) rowResult.getData()).getInteger("successCnt") + "条!");
|
|
|
+ valueHolder.setData(rowResult);
|
|
|
return valueHolder;
|
|
|
}
|
|
|
|
|
|
- private ValueHolder buildErrorResult(TableServiceContext context, Exception e) {
|
|
|
- return new ValueHolder(ResultCode.FAIL,e.getMessage());
|
|
|
- }
|
|
|
}
|