|
@@ -0,0 +1,105 @@
|
|
|
+package com.abi.task.common.tablestore;
|
|
|
+
|
|
|
+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 lombok.extern.slf4j.Slf4j;
|
|
|
+import org.apache.commons.lang3.StringUtils;
|
|
|
+import org.springframework.beans.factory.annotation.Autowired;
|
|
|
+import org.springframework.stereotype.Component;
|
|
|
+
|
|
|
+import java.lang.reflect.Field;
|
|
|
+import java.lang.reflect.Method;
|
|
|
+import java.time.LocalDateTime;
|
|
|
+import java.util.ArrayList;
|
|
|
+import java.util.Date;
|
|
|
+import java.util.List;
|
|
|
+
|
|
|
+/**
|
|
|
+ * 进一步封装的tablestore工具类
|
|
|
+ */
|
|
|
+@Component
|
|
|
+@Slf4j
|
|
|
+public class TableStorePlusUtils {
|
|
|
+
|
|
|
+ @Autowired
|
|
|
+ private TableStoreUtils tableStoreUtils;
|
|
|
+
|
|
|
+ /**
|
|
|
+ * 存储一行数据(如果主键key相同,则覆盖)
|
|
|
+ * @param entity
|
|
|
+ * @throws IllegalAccessException
|
|
|
+ */
|
|
|
+ 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;
|
|
|
+
|
|
|
+
|
|
|
+ //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);
|
|
|
+ }
|
|
|
+
|
|
|
+ tableStoreUtils.putRow(primaryKeyName, primaryKeyVal, tableName, columns);
|
|
|
+ }catch (Exception e){
|
|
|
+ log.info("setRow error",e);
|
|
|
+ throw new BusinessException("存储table异常");
|
|
|
+ }
|
|
|
+ }
|
|
|
+
|
|
|
+}
|