|
|
@@ -12,6 +12,7 @@ import com.yunfeiyun.agmp.common.core.page.PageDomain;
|
|
|
import com.yunfeiyun.agmp.common.utils.DateUtils;
|
|
|
import com.yunfeiyun.agmp.common.utils.JSONUtils;
|
|
|
import com.yunfeiyun.agmp.common.utils.StringUtils;
|
|
|
+import com.yunfeiyun.agmp.iot.common.domain.dto.MgUpdateBatchDto;
|
|
|
import lombok.Data;
|
|
|
import lombok.extern.slf4j.Slf4j;
|
|
|
import org.apache.http.util.TextUtils;
|
|
|
@@ -22,6 +23,7 @@ import org.bson.conversions.Bson;
|
|
|
import org.bson.types.ObjectId;
|
|
|
import org.springframework.beans.factory.annotation.Autowired;
|
|
|
import org.springframework.data.domain.Sort;
|
|
|
+import org.springframework.data.mongodb.core.BulkOperations;
|
|
|
import org.springframework.data.mongodb.core.MongoTemplate;
|
|
|
import org.springframework.data.mongodb.core.aggregation.*;
|
|
|
import org.springframework.data.mongodb.core.query.Criteria;
|
|
|
@@ -671,5 +673,32 @@ public class MongoService<T>{
|
|
|
collection.createIndexes(indexModelListCreate);
|
|
|
}
|
|
|
}
|
|
|
+
|
|
|
+ public void updateBatch(Class<T> entity, List<MgUpdateBatchDto> updateBatchDtoList) {
|
|
|
+ TableName annotation = entity.getAnnotation(TableName.class);
|
|
|
+ if (annotation == null || StringUtils.isEmpty(annotation.value())) {
|
|
|
+ throw new RuntimeException(entity.getName() + "未被@TableName修饰或value为空");
|
|
|
+ }
|
|
|
+ /**
|
|
|
+ * 不得用entity实体,会自动将类名转换为小写开头的表名
|
|
|
+ */
|
|
|
+ BulkOperations operations = mongoTemplate.bulkOps(BulkOperations.BulkMode.UNORDERED, annotation.value());
|
|
|
+ for(MgUpdateBatchDto updateBatchDto : updateBatchDtoList) {
|
|
|
+ Query query = updateBatchDto.getQuery();
|
|
|
+ Map<String, Object> updateField = updateBatchDto.getUpdateField();
|
|
|
+ if (query == null || updateField == null || updateField.isEmpty()) {
|
|
|
+ throw new RuntimeException("query 或 更新内容 不可为空");
|
|
|
+ }
|
|
|
+ Update update = new Update();
|
|
|
+ updateField.forEach((key, value) -> {
|
|
|
+ //忽略空值
|
|
|
+ if (value != null) {
|
|
|
+ update.set(key, value);
|
|
|
+ }
|
|
|
+ });
|
|
|
+ operations.updateOne(query, update);
|
|
|
+ }
|
|
|
+ operations.execute();
|
|
|
+ }
|
|
|
}
|
|
|
|