|
@@ -1,18 +1,19 @@
|
|
|
package com.abi.task.common.tablestore;
|
|
|
|
|
|
+import cn.hutool.core.collection.CollectionUtil;
|
|
|
import cn.hutool.core.date.DateUtil;
|
|
|
import com.abi.task.common.api.exception.BusinessException;
|
|
|
import com.abi.task.common.tablestore.common.TableStore;
|
|
|
import com.abi.task.common.tablestore.common.TableStoreEntity;
|
|
|
import com.abi.task.common.utils.IStringUtil;
|
|
|
import com.abi.task.common.utils.TableStoreUtils;
|
|
|
-import com.alicloud.openservices.tablestore.model.Column;
|
|
|
-import com.alicloud.openservices.tablestore.model.ColumnValue;
|
|
|
-import com.alicloud.openservices.tablestore.model.Row;
|
|
|
+import com.alicloud.openservices.tablestore.model.*;
|
|
|
import lombok.extern.slf4j.Slf4j;
|
|
|
import org.apache.commons.lang3.StringUtils;
|
|
|
+import org.apache.poi.ss.formula.functions.T;
|
|
|
import org.springframework.beans.factory.annotation.Autowired;
|
|
|
import org.springframework.stereotype.Component;
|
|
|
+import sun.security.provider.certpath.SunCertPathBuilderException;
|
|
|
|
|
|
import java.lang.reflect.Field;
|
|
|
import java.lang.reflect.Method;
|
|
@@ -30,70 +31,97 @@ public class TableStorePlusUtils {
|
|
|
private TableStoreUtils tableStoreUtils;
|
|
|
|
|
|
/**
|
|
|
- * 存储一行数据(如果主键key相同,则覆盖)
|
|
|
+ * 存储单条 重构方法
|
|
|
* @param entity
|
|
|
*/
|
|
|
public void putRow(TableStoreEntity entity){
|
|
|
- try {
|
|
|
- List<Column> columns = new ArrayList<>();
|
|
|
- //表名
|
|
|
- TableStore tableStore = entity.getClass().getAnnotation(TableStore.class);
|
|
|
- String tableName = tableStore.tableName();
|
|
|
- if (StringUtils.isBlank(tableName)) {
|
|
|
- throw new BusinessException("存储表名为空");
|
|
|
- }
|
|
|
- //主键
|
|
|
- String primaryKeyName = tableStore.primaryKeyName();
|
|
|
- if (StringUtils.isBlank(primaryKeyName)) {
|
|
|
- throw new BusinessException("存储主键为空");
|
|
|
- }
|
|
|
- //主键的驼峰转下划线
|
|
|
- primaryKeyName = IStringUtil.camelToUnderline(primaryKeyName);
|
|
|
- //主键的值
|
|
|
- String primaryKeyVal = null;
|
|
|
-
|
|
|
+ putRow(Arrays.asList(entity));
|
|
|
+ }
|
|
|
|
|
|
- //column列表 循环放入字段
|
|
|
- Field[] fields = entity.getClass().getDeclaredFields();
|
|
|
- for (Field field : fields) {
|
|
|
- String fieldName = field.getName();
|
|
|
- String underlineFieldName = IStringUtil.camelToUnderline(fieldName);
|
|
|
- //获取method
|
|
|
- Method method = entity.getClass().getMethod("get" + fieldName.substring(0, 1).toUpperCase() + fieldName.substring(1));
|
|
|
- if (method == null){
|
|
|
- throw new BusinessException("get方法不存在");
|
|
|
+ /**
|
|
|
+ * 存储多行数据(如果主键key相同,则覆盖)
|
|
|
+ * @param entityList
|
|
|
+ */
|
|
|
+ public void putRow(List<TableStoreEntity> entityList){
|
|
|
+ if(CollectionUtil.isEmpty(entityList)){
|
|
|
+ throw new BusinessException("入参列表为空");
|
|
|
+ }
|
|
|
+ try {
|
|
|
+ String primaryKeyName = null;
|
|
|
+ String tableName = null;
|
|
|
+ List<String> pkValueList = new ArrayList<>();
|
|
|
+ List<List<Column>> columnsList = new ArrayList<>();
|
|
|
+
|
|
|
+ for(TableStoreEntity entity:entityList){
|
|
|
+ List<Column> columns = new ArrayList<>();
|
|
|
+ //表名
|
|
|
+ TableStore tableStore = entity.getClass().getAnnotation(TableStore.class);
|
|
|
+ tableName = tableStore.tableName();
|
|
|
+ if (StringUtils.isBlank(tableName)) {
|
|
|
+ throw new BusinessException("存储表名为空");
|
|
|
}
|
|
|
- //反射调用拿到结果
|
|
|
- Object fieldValue = method.invoke(entity);
|
|
|
- //如果是主键则赋值,主键值不放入column,不然阿里会报错
|
|
|
- if (primaryKeyVal == null && underlineFieldName.equals(primaryKeyName)) {
|
|
|
- primaryKeyVal = (String) fieldValue;
|
|
|
- continue;
|
|
|
+ //主键
|
|
|
+ primaryKeyName = tableStore.primaryKeyName();
|
|
|
+ if (StringUtils.isBlank(primaryKeyName)) {
|
|
|
+ throw new BusinessException("存储主键为空");
|
|
|
}
|
|
|
- //放入table的column
|
|
|
- Column column = null;
|
|
|
- if (fieldValue instanceof String) {
|
|
|
- column = new Column(underlineFieldName, ColumnValue.fromString(String.valueOf(fieldValue)));
|
|
|
- } else if (fieldValue instanceof Integer) {
|
|
|
- column = new Column(underlineFieldName, ColumnValue.fromLong(Long.valueOf((Integer) fieldValue)));
|
|
|
- } else if (fieldValue instanceof Long) {
|
|
|
- column = new Column(underlineFieldName, ColumnValue.fromLong((Long) fieldValue));
|
|
|
- } else if (fieldValue instanceof Double) {
|
|
|
- column = new Column(underlineFieldName, ColumnValue.fromDouble((Double) fieldValue));
|
|
|
- } else if (fieldValue instanceof Boolean) {
|
|
|
- column = new Column(underlineFieldName, ColumnValue.fromBoolean((Boolean) fieldValue));
|
|
|
- } else if (fieldValue instanceof Date) {
|
|
|
- column = new Column(underlineFieldName, ColumnValue.fromString(DateUtil.format((Date) fieldValue, "yyyy-MM-dd HH:mm:ss")));
|
|
|
- } else if (fieldValue instanceof LocalDateTime) {
|
|
|
- column = new Column(underlineFieldName, ColumnValue.fromString(DateUtil.format((LocalDateTime) fieldValue, "yyyy-MM-dd HH:mm:ss")));
|
|
|
- } else {
|
|
|
- log.info("不识别的字段类型 fieldName=" + fieldName);
|
|
|
- continue;
|
|
|
+ //主键的驼峰转下划线
|
|
|
+ primaryKeyName = IStringUtil.camelToUnderline(primaryKeyName);
|
|
|
+ //主键的值
|
|
|
+ String primaryKeyVal = null;
|
|
|
+
|
|
|
+
|
|
|
+ //column列表 循环放入字段
|
|
|
+ Field[] fields = entity.getClass().getDeclaredFields();
|
|
|
+ for (Field field : fields) {
|
|
|
+ String fieldName = field.getName();
|
|
|
+ String underlineFieldName = IStringUtil.camelToUnderline(fieldName);
|
|
|
+ //获取method
|
|
|
+ Method method = entity.getClass().getMethod("get" + fieldName.substring(0, 1).toUpperCase() + fieldName.substring(1));
|
|
|
+ if (method == null){
|
|
|
+ throw new BusinessException("get方法不存在");
|
|
|
+ }
|
|
|
+ //反射调用拿到结果
|
|
|
+ Object fieldValue = method.invoke(entity);
|
|
|
+ //如果是主键则赋值,主键值不放入column,不然阿里会报错
|
|
|
+ if (primaryKeyVal == null && underlineFieldName.equals(primaryKeyName)) {
|
|
|
+ primaryKeyVal = (String) fieldValue;
|
|
|
+ continue;
|
|
|
+ }
|
|
|
+ //放入table的column
|
|
|
+ Column column = null;
|
|
|
+ if (fieldValue instanceof String) {
|
|
|
+ column = new Column(underlineFieldName, ColumnValue.fromString(String.valueOf(fieldValue)));
|
|
|
+ } else if (fieldValue instanceof Integer) {
|
|
|
+ column = new Column(underlineFieldName, ColumnValue.fromLong(Long.valueOf((Integer) fieldValue)));
|
|
|
+ } else if (fieldValue instanceof Long) {
|
|
|
+ column = new Column(underlineFieldName, ColumnValue.fromLong((Long) fieldValue));
|
|
|
+ } else if (fieldValue instanceof Double) {
|
|
|
+ column = new Column(underlineFieldName, ColumnValue.fromDouble((Double) fieldValue));
|
|
|
+ } else if (fieldValue instanceof Boolean) {
|
|
|
+ column = new Column(underlineFieldName, ColumnValue.fromBoolean((Boolean) fieldValue));
|
|
|
+ } else if (fieldValue instanceof Date) {
|
|
|
+ column = new Column(underlineFieldName, ColumnValue.fromString(DateUtil.format((Date) fieldValue, "yyyy-MM-dd HH:mm:ss")));
|
|
|
+ } else if (fieldValue instanceof LocalDateTime) {
|
|
|
+ column = new Column(underlineFieldName, ColumnValue.fromString(DateUtil.format((LocalDateTime) fieldValue, "yyyy-MM-dd HH:mm:ss")));
|
|
|
+ } else {
|
|
|
+ log.info("不识别的字段类型 fieldName=" + fieldName);
|
|
|
+ continue;
|
|
|
+ }
|
|
|
+ columns.add(column);
|
|
|
}
|
|
|
- columns.add(column);
|
|
|
+ //放入调用入参
|
|
|
+ pkValueList.add(primaryKeyVal);
|
|
|
+ columnsList.add(columns);
|
|
|
+ }
|
|
|
+
|
|
|
+ //真实去调用
|
|
|
+ if(pkValueList.size()>1){
|
|
|
+ tableStoreUtils.batchPutRow(primaryKeyName, pkValueList, tableName, columnsList);
|
|
|
+ }else{
|
|
|
+ tableStoreUtils.putRow(primaryKeyName, pkValueList.get(0), tableName, columnsList.get(0));
|
|
|
}
|
|
|
|
|
|
- tableStoreUtils.putRow(primaryKeyName, primaryKeyVal, tableName, columns);
|
|
|
}catch (Exception e){
|
|
|
log.info("setRow error",e);
|
|
|
throw new BusinessException("存储table异常");
|