Showing
12 changed files
with
367 additions
and
32 deletions
| ... | @@ -88,4 +88,9 @@ public class MybatisPlusConfig { | ... | @@ -88,4 +88,9 @@ public class MybatisPlusConfig { |
| 88 | return new MybatisPlusSqlInjector(); | 88 | return new MybatisPlusSqlInjector(); |
| 89 | } | 89 | } |
| 90 | 90 | ||
| 91 | + @Bean | ||
| 92 | + public MysqlTypeTransferUtil mysqlTypeTransferUtil() { | ||
| 93 | + return new MysqlTypeTransferUtil(); | ||
| 94 | + } | ||
| 95 | + | ||
| 91 | } | 96 | } | ... | ... |
| ... | @@ -15,6 +15,7 @@ import lombok.experimental.Accessors; | ... | @@ -15,6 +15,7 @@ import lombok.experimental.Accessors; |
| 15 | 15 | ||
| 16 | import java.io.Serializable; | 16 | import java.io.Serializable; |
| 17 | import java.time.LocalDateTime; | 17 | import java.time.LocalDateTime; |
| 18 | +import org.apache.ibatis.type.JdbcType; | ||
| 18 | 19 | ||
| 19 | /** | 20 | /** |
| 20 | * order DB | 21 | * order DB |
| ... | @@ -153,6 +154,7 @@ public class OrderDb extends Model<OrderDb> implements Serializable { | ... | @@ -153,6 +154,7 @@ public class OrderDb extends Model<OrderDb> implements Serializable { |
| 153 | * "doctors":["String"] | 154 | * "doctors":["String"] |
| 154 | * } | 155 | * } |
| 155 | */ | 156 | */ |
| 157 | + @TableField(jdbcType = JdbcType.OTHER) | ||
| 156 | private JsonNode customerNoticeJson; | 158 | private JsonNode customerNoticeJson; |
| 157 | /** | 159 | /** |
| 158 | * 是否删除;0:未删除,1:已删除 | 160 | * 是否删除;0:未删除,1:已删除 | ... | ... |
| ... | @@ -14,6 +14,7 @@ import java.time.LocalDateTime; | ... | @@ -14,6 +14,7 @@ import java.time.LocalDateTime; |
| 14 | import lombok.Data; | 14 | import lombok.Data; |
| 15 | import lombok.EqualsAndHashCode; | 15 | import lombok.EqualsAndHashCode; |
| 16 | import lombok.experimental.Accessors; | 16 | import lombok.experimental.Accessors; |
| 17 | +import org.apache.ibatis.type.JdbcType; | ||
| 17 | 18 | ||
| 18 | /** | 19 | /** |
| 19 | * order detail DB | 20 | * order detail DB |
| ... | @@ -76,6 +77,7 @@ public class OrderDetailDb extends Model<OrderDetailDb> implements Serializable | ... | @@ -76,6 +77,7 @@ public class OrderDetailDb extends Model<OrderDetailDb> implements Serializable |
| 76 | * "allergies": [] //过敏源 | 77 | * "allergies": [] //过敏源 |
| 77 | * } | 78 | * } |
| 78 | */ | 79 | */ |
| 80 | + @TableField(jdbcType = JdbcType.OTHER) | ||
| 79 | private JsonNode mealDetailJson; | 81 | private JsonNode mealDetailJson; |
| 80 | /** | 82 | /** |
| 81 | * 是否删除;0:未删除,1:已删除 | 83 | * 是否删除;0:未删除,1:已删除 | ... | ... |
| 1 | +package com.tianting.infoloop.model.dto; | ||
| 2 | + | ||
| 3 | +import java.util.List; | ||
| 4 | +import lombok.AllArgsConstructor; | ||
| 5 | +import lombok.Builder; | ||
| 6 | +import lombok.Data; | ||
| 7 | +import lombok.NoArgsConstructor; | ||
| 8 | + | ||
| 9 | +@Data | ||
| 10 | +@Builder | ||
| 11 | +@AllArgsConstructor | ||
| 12 | +@NoArgsConstructor | ||
| 13 | +public class CustomerNoticeDto { | ||
| 14 | + private List<String> avoids; | ||
| 15 | + private List<String> doctors; | ||
| 16 | +} | ||
| ... | \ No newline at end of file | ... | \ No newline at end of file |
| 1 | +package com.tianting.infoloop.model.dto; | ||
| 2 | + | ||
| 3 | +import java.util.List; | ||
| 4 | +import lombok.AllArgsConstructor; | ||
| 5 | +import lombok.Builder; | ||
| 6 | +import lombok.Data; | ||
| 7 | +import lombok.NoArgsConstructor; | ||
| 8 | + | ||
| 9 | +@Data | ||
| 10 | +@Builder | ||
| 11 | +@AllArgsConstructor | ||
| 12 | +@NoArgsConstructor | ||
| 13 | +public class MealDetailJson { | ||
| 14 | + private String basicMaterials; | ||
| 15 | + private List<String> tastes; | ||
| 16 | + private List<String> efficacies; | ||
| 17 | + private List<String> dockers; | ||
| 18 | + private String energy; | ||
| 19 | + private String fat; | ||
| 20 | + private String protein; | ||
| 21 | + private String carbohydrate; | ||
| 22 | + private List<String> allergies; | ||
| 23 | + private List<String> avoids; | ||
| 24 | + private List<String> skus; | ||
| 25 | +} |
| ... | @@ -57,6 +57,8 @@ import com.tianting.infoloop.model.db.OrderOperationRecordDb; | ... | @@ -57,6 +57,8 @@ import com.tianting.infoloop.model.db.OrderOperationRecordDb; |
| 57 | import com.tianting.infoloop.service.OrderDbService; | 57 | import com.tianting.infoloop.service.OrderDbService; |
| 58 | import com.tianting.infoloop.service.OrderDetailDbService; | 58 | import com.tianting.infoloop.service.OrderDetailDbService; |
| 59 | import com.tianting.infoloop.service.OrderOperationRecordDbService; | 59 | import com.tianting.infoloop.service.OrderOperationRecordDbService; |
| 60 | +import com.tianting.infoloop.utils.DbToProtoUtil; | ||
| 61 | +import com.tianting.infoloop.utils.GenericBeanToProtoListConverter; | ||
| 60 | import com.tianting.infoloop.utils.ProtoBeanUtil; | 62 | import com.tianting.infoloop.utils.ProtoBeanUtil; |
| 61 | import io.grpc.Status; | 63 | import io.grpc.Status; |
| 62 | import io.grpc.stub.StreamObserver; | 64 | import io.grpc.stub.StreamObserver; |
| ... | @@ -120,8 +122,10 @@ public class OrderGrpcService extends ClientCustomerOrderServiceRpcGrpc.ClientCu | ... | @@ -120,8 +122,10 @@ public class OrderGrpcService extends ClientCustomerOrderServiceRpcGrpc.ClientCu |
| 120 | try { | 122 | try { |
| 121 | final var list = orderDbService.getOrdersByCustomerId(request.getCustomerId()); | 123 | final var list = orderDbService.getOrdersByCustomerId(request.getCustomerId()); |
| 122 | if (!list.isEmpty()) { | 124 | if (!list.isEmpty()) { |
| 123 | - builder.addAllResponses(ProtoBeanUtil.beanListToProtoList(list, | 125 | +// final var allResponses = ProtoBeanUtil.beanListToProtoList(list, |
| 124 | - SingleClientCustomerOrderRpcResponse.class)); | 126 | +// SingleClientCustomerOrderRpcResponse.class); |
| 127 | + final var allResponses = DbToProtoUtil.orderDbListToProtoList(list); | ||
| 128 | + builder.addAllResponses(allResponses); | ||
| 125 | } | 129 | } |
| 126 | } catch (final Exception e) { | 130 | } catch (final Exception e) { |
| 127 | log.error("getClientCustomerOrdersByCustomerId error;", e); | 131 | log.error("getClientCustomerOrdersByCustomerId error;", e); |
| ... | @@ -139,7 +143,8 @@ public class OrderGrpcService extends ClientCustomerOrderServiceRpcGrpc.ClientCu | ... | @@ -139,7 +143,8 @@ public class OrderGrpcService extends ClientCustomerOrderServiceRpcGrpc.ClientCu |
| 139 | try { | 143 | try { |
| 140 | final var data = orderDbService.getById(request.getId()); | 144 | final var data = orderDbService.getById(request.getId()); |
| 141 | if (data != null) { | 145 | if (data != null) { |
| 142 | - builder.setResponse(ProtoBeanUtil.beanToProto(data, SingleClientCustomerOrderRpcResponse.class)); | 146 | +// builder.setResponse(ProtoBeanUtil.beanToProto(data, SingleClientCustomerOrderRpcResponse.class)); |
| 147 | + builder.setResponse(DbToProtoUtil.orderDbToProto(data)); | ||
| 143 | } | 148 | } |
| 144 | } catch (final Exception e) { | 149 | } catch (final Exception e) { |
| 145 | log.error("getClientCustomerOrderById error;", e); | 150 | log.error("getClientCustomerOrderById error;", e); |
| ... | @@ -162,7 +167,8 @@ public class OrderGrpcService extends ClientCustomerOrderServiceRpcGrpc.ClientCu | ... | @@ -162,7 +167,8 @@ public class OrderGrpcService extends ClientCustomerOrderServiceRpcGrpc.ClientCu |
| 162 | } | 167 | } |
| 163 | final var list = orderDbService.listByIds(request.getIdsList()); | 168 | final var list = orderDbService.listByIds(request.getIdsList()); |
| 164 | if (!list.isEmpty()) { | 169 | if (!list.isEmpty()) { |
| 165 | - builder.addAllResponses(ProtoBeanUtil.beanListToProtoList(list, SingleClientCustomerOrderRpcResponse.class)); | 170 | +// builder.addAllResponses(ProtoBeanUtil.beanListToProtoList(list, SingleClientCustomerOrderRpcResponse.class)); |
| 171 | + builder.addAllResponses(DbToProtoUtil.orderDbListToProtoList(list)); | ||
| 166 | } | 172 | } |
| 167 | } catch (final Exception e) { | 173 | } catch (final Exception e) { |
| 168 | log.error("getClientCustomerOrdersByIds error;", e); | 174 | log.error("getClientCustomerOrdersByIds error;", e); |
| ... | @@ -186,11 +192,13 @@ public class OrderGrpcService extends ClientCustomerOrderServiceRpcGrpc.ClientCu | ... | @@ -186,11 +192,13 @@ public class OrderGrpcService extends ClientCustomerOrderServiceRpcGrpc.ClientCu |
| 186 | queryWrapper.eq(OrderDb::getEnterpriseId, request.getEnterpriseId()); | 192 | queryWrapper.eq(OrderDb::getEnterpriseId, request.getEnterpriseId()); |
| 187 | queryWrapper.eq(OrderDb::getClientId, request.getClientId()); | 193 | queryWrapper.eq(OrderDb::getClientId, request.getClientId()); |
| 188 | queryWrapper.eq(OrderDb::getStatus, request.getStatusValue()); | 194 | queryWrapper.eq(OrderDb::getStatus, request.getStatusValue()); |
| 195 | + queryWrapper.orderByDesc(OrderDb::getCreatedAt); | ||
| 189 | final var result = orderDbService.page(page, queryWrapper); | 196 | final var result = orderDbService.page(page, queryWrapper); |
| 190 | builder.setTotal(result.getTotal()); | 197 | builder.setTotal(result.getTotal()); |
| 191 | builder.setPages(result.getPages()); | 198 | builder.setPages(result.getPages()); |
| 192 | if (!result.getRecords().isEmpty()) { | 199 | if (!result.getRecords().isEmpty()) { |
| 193 | - builder.addAllResponses(ProtoBeanUtil.beanListToProtoList(result.getRecords(), SingleClientCustomerOrderRpcResponse.class)); | 200 | +// builder.addAllResponses(ProtoBeanUtil.beanListToProtoList(result.getRecords(), SingleClientCustomerOrderRpcResponse.class)); |
| 201 | + builder.addAllResponses(DbToProtoUtil.orderDbListToProtoList(result.getRecords())); | ||
| 194 | } | 202 | } |
| 195 | } catch (final Exception e) { | 203 | } catch (final Exception e) { |
| 196 | log.error("queryClientCustomerOrdersByPagination error;", e); | 204 | log.error("queryClientCustomerOrdersByPagination error;", e); |
| ... | @@ -298,9 +306,11 @@ public class OrderGrpcService extends ClientCustomerOrderServiceRpcGrpc.ClientCu | ... | @@ -298,9 +306,11 @@ public class OrderGrpcService extends ClientCustomerOrderServiceRpcGrpc.ClientCu |
| 298 | if (request.getShouldFilterIsRead()) { | 306 | if (request.getShouldFilterIsRead()) { |
| 299 | queryWrapper.eq(OrderDb::getIsRead, request.getIsRead()); | 307 | queryWrapper.eq(OrderDb::getIsRead, request.getIsRead()); |
| 300 | } | 308 | } |
| 309 | + queryWrapper.orderByDesc(OrderDb::getCreatedAt); | ||
| 301 | final var list = orderDbService.list(queryWrapper); | 310 | final var list = orderDbService.list(queryWrapper); |
| 302 | if (!list.isEmpty()) { | 311 | if (!list.isEmpty()) { |
| 303 | - builder.addAllResponses(ProtoBeanUtil.beanListToProtoList(list, SingleClientCustomerOrderRpcResponse.class)); | 312 | +// builder.addAllResponses(ProtoBeanUtil.beanListToProtoList(list, SingleClientCustomerOrderRpcResponse.class)); |
| 313 | + builder.addAllResponses(DbToProtoUtil.orderDbListToProtoList(list)); | ||
| 304 | } | 314 | } |
| 305 | } catch (final Exception e) { | 315 | } catch (final Exception e) { |
| 306 | log.error("queryTestExampleDbByCondition error;", e); | 316 | log.error("queryTestExampleDbByCondition error;", e); |
| ... | @@ -391,7 +401,8 @@ public class OrderGrpcService extends ClientCustomerOrderServiceRpcGrpc.ClientCu | ... | @@ -391,7 +401,8 @@ public class OrderGrpcService extends ClientCustomerOrderServiceRpcGrpc.ClientCu |
| 391 | try { | 401 | try { |
| 392 | final var data = orderDetailDbService.getById(request.getId()); | 402 | final var data = orderDetailDbService.getById(request.getId()); |
| 393 | if (data != null) { | 403 | if (data != null) { |
| 394 | - builder.setResponse(ProtoBeanUtil.beanToProto(data, SingleClientCustomerOrderDetailRpcResponse.class)); | 404 | +// builder.setResponse(ProtoBeanUtil.beanToProto(data, SingleClientCustomerOrderDetailRpcResponse.class)); |
| 405 | + builder.setResponse(DbToProtoUtil.orderDetailDbToProto(data)); | ||
| 395 | } | 406 | } |
| 396 | } catch (final Exception e) { | 407 | } catch (final Exception e) { |
| 397 | log.error("getClientCustomerOrderDetailById error;", e); | 408 | log.error("getClientCustomerOrderDetailById error;", e); |
| ... | @@ -416,7 +427,8 @@ public class OrderGrpcService extends ClientCustomerOrderServiceRpcGrpc.ClientCu | ... | @@ -416,7 +427,8 @@ public class OrderGrpcService extends ClientCustomerOrderServiceRpcGrpc.ClientCu |
| 416 | } | 427 | } |
| 417 | final var list = orderDetailDbService.listByIds(request.getIdsList()); | 428 | final var list = orderDetailDbService.listByIds(request.getIdsList()); |
| 418 | if (!list.isEmpty()) { | 429 | if (!list.isEmpty()) { |
| 419 | - builder.addAllResponses(ProtoBeanUtil.beanListToProtoList(list, SingleClientCustomerOrderDetailRpcResponse.class)); | 430 | +// builder.addAllResponses(ProtoBeanUtil.beanListToProtoList(list, SingleClientCustomerOrderDetailRpcResponse.class)); |
| 431 | + builder.addAllResponses(DbToProtoUtil.orderDetailDbListToProtoList(list)); | ||
| 420 | } | 432 | } |
| 421 | } catch (final Exception e) { | 433 | } catch (final Exception e) { |
| 422 | log.error("getClientCustomerOrderDetailsByIds error;", e); | 434 | log.error("getClientCustomerOrderDetailsByIds error;", e); |
| ... | @@ -442,7 +454,8 @@ public class OrderGrpcService extends ClientCustomerOrderServiceRpcGrpc.ClientCu | ... | @@ -442,7 +454,8 @@ public class OrderGrpcService extends ClientCustomerOrderServiceRpcGrpc.ClientCu |
| 442 | } | 454 | } |
| 443 | final var list = orderDetailDbService.getOrderDetailsByOrderIds(request); | 455 | final var list = orderDetailDbService.getOrderDetailsByOrderIds(request); |
| 444 | if (!list.isEmpty()) { | 456 | if (!list.isEmpty()) { |
| 445 | - builder.addAllResponses(ProtoBeanUtil.beanListToProtoList(list, SingleClientCustomerOrderDetailRpcResponse.class)); | 457 | +// builder.addAllResponses(ProtoBeanUtil.beanListToProtoList(list, SingleClientCustomerOrderDetailRpcResponse.class)); |
| 458 | + builder.addAllResponses(DbToProtoUtil.orderDetailDbListToProtoList(list)); | ||
| 446 | } | 459 | } |
| 447 | } catch (final Exception e) { | 460 | } catch (final Exception e) { |
| 448 | log.error("getClientCustomerOrderDetailsByIds error;", e); | 461 | log.error("getClientCustomerOrderDetailsByIds error;", e); | ... | ... |
| ... | @@ -16,6 +16,8 @@ import com.infoloop.tianting.clientcustomerorderservice.UpdateClientCustomerOrde | ... | @@ -16,6 +16,8 @@ import com.infoloop.tianting.clientcustomerorderservice.UpdateClientCustomerOrde |
| 16 | import com.tianting.infoloop.mapper.OrderDbMapper; | 16 | import com.tianting.infoloop.mapper.OrderDbMapper; |
| 17 | import com.tianting.infoloop.model.db.OrderDb; | 17 | import com.tianting.infoloop.model.db.OrderDb; |
| 18 | import com.tianting.infoloop.model.db.OrderDetailDb; | 18 | import com.tianting.infoloop.model.db.OrderDetailDb; |
| 19 | +import com.tianting.infoloop.model.dto.CustomerNoticeDto; | ||
| 20 | +import com.tianting.infoloop.model.dto.MealDetailJson; | ||
| 19 | import com.tianting.infoloop.service.OrderDbService; | 21 | import com.tianting.infoloop.service.OrderDbService; |
| 20 | import com.tianting.infoloop.service.OrderDetailDbService; | 22 | import com.tianting.infoloop.service.OrderDetailDbService; |
| 21 | import com.tianting.infoloop.utils.exception.ErrorCodeConstants; | 23 | import com.tianting.infoloop.utils.exception.ErrorCodeConstants; |
| ... | @@ -98,9 +100,12 @@ public class OrderDbServiceImpl extends ServiceImpl<OrderDbMapper, OrderDb> impl | ... | @@ -98,9 +100,12 @@ public class OrderDbServiceImpl extends ServiceImpl<OrderDbMapper, OrderDb> impl |
| 98 | if (createOrderRpcRequest.getCreation().getShouldCreateRemark()) { | 100 | if (createOrderRpcRequest.getCreation().getShouldCreateRemark()) { |
| 99 | orderDb.setRemark(createOrderRpcRequest.getCreation().getRemark()); | 101 | orderDb.setRemark(createOrderRpcRequest.getCreation().getRemark()); |
| 100 | } | 102 | } |
| 103 | + ObjectMapper objectMapper = new ObjectMapper(); | ||
| 101 | if (createOrderRpcRequest.getCreation().getShouldCreateCustomerNoticeJson()) { | 104 | if (createOrderRpcRequest.getCreation().getShouldCreateCustomerNoticeJson()) { |
| 102 | - ObjectMapper objectMapper = new ObjectMapper(); | 105 | + JsonNode notice = objectMapper.valueToTree(CustomerNoticeDto.builder() |
| 103 | - JsonNode notice = objectMapper.valueToTree(createOrderRpcRequest.getCreation().getCustomerNoticeJson()); | 106 | + .avoids(createOrderRpcRequest.getCreation().getCustomerNoticeJson().getAvoidsList()) |
| 107 | + .doctors(createOrderRpcRequest.getCreation().getCustomerNoticeJson().getDoctorsList()) | ||
| 108 | + .build()); | ||
| 104 | orderDb.setCustomerNoticeJson(notice); | 109 | orderDb.setCustomerNoticeJson(notice); |
| 105 | } | 110 | } |
| 106 | LocalDateTime mealTime = Instant.ofEpochMilli(createOrderRpcRequest.getCreation().getMealTime()) | 111 | LocalDateTime mealTime = Instant.ofEpochMilli(createOrderRpcRequest.getCreation().getMealTime()) |
| ... | @@ -149,7 +154,19 @@ public class OrderDbServiceImpl extends ServiceImpl<OrderDbMapper, OrderDb> impl | ... | @@ -149,7 +154,19 @@ public class OrderDbServiceImpl extends ServiceImpl<OrderDbMapper, OrderDb> impl |
| 149 | } | 154 | } |
| 150 | orderDetailDb.setNotServe(false); | 155 | orderDetailDb.setNotServe(false); |
| 151 | if (detailCreation.getShouldCreateMealDetailJson()) { | 156 | if (detailCreation.getShouldCreateMealDetailJson()) { |
| 152 | -// orderDetailDb.setMealDetailJson(objectMapper.valueToTree(detailCreation.getMealDetailJson())); | 157 | + orderDetailDb.setMealDetailJson(objectMapper.valueToTree(MealDetailJson.builder() |
| 158 | + .allergies(detailCreation.getMealDetailJson().getAllergiesList()) | ||
| 159 | + .avoids(detailCreation.getMealDetailJson().getAvoidsList()) | ||
| 160 | + .basicMaterials(detailCreation.getMealDetailJson().getBasicMaterials()) | ||
| 161 | + .carbohydrate(detailCreation.getMealDetailJson().getCarbohydrate()) | ||
| 162 | + .dockers(detailCreation.getMealDetailJson().getDockersList()) | ||
| 163 | + .efficacies(detailCreation.getMealDetailJson().getEfficaciesList()) | ||
| 164 | + .energy(detailCreation.getMealDetailJson().getEnergy()) | ||
| 165 | + .fat(detailCreation.getMealDetailJson().getFat()) | ||
| 166 | + .protein(detailCreation.getMealDetailJson().getProtein()) | ||
| 167 | + .skus(detailCreation.getMealDetailJson().getSkusList()) | ||
| 168 | + .tastes(detailCreation.getMealDetailJson().getTastesList()) | ||
| 169 | + .build()));//detailCreation.getMealDetailJson() | ||
| 153 | } | 170 | } |
| 154 | return orderDetailDb; | 171 | return orderDetailDb; |
| 155 | }) | 172 | }) |
| ... | @@ -308,7 +325,10 @@ public class OrderDbServiceImpl extends ServiceImpl<OrderDbMapper, OrderDb> impl | ... | @@ -308,7 +325,10 @@ public class OrderDbServiceImpl extends ServiceImpl<OrderDbMapper, OrderDb> impl |
| 308 | orderDb.setRemark(creation.getRemark()); | 325 | orderDb.setRemark(creation.getRemark()); |
| 309 | } | 326 | } |
| 310 | if (creation.getShouldCreateCustomerNoticeJson()) { | 327 | if (creation.getShouldCreateCustomerNoticeJson()) { |
| 311 | - JsonNode notice = objectMapper.valueToTree(creation.getCustomerNoticeJson()); | 328 | + JsonNode notice = objectMapper.valueToTree(CustomerNoticeDto.builder() |
| 329 | + .avoids(creation.getCustomerNoticeJson().getAvoidsList()) | ||
| 330 | + .doctors(creation.getCustomerNoticeJson().getDoctorsList()) | ||
| 331 | + .build()); | ||
| 312 | orderDb.setCustomerNoticeJson(notice); | 332 | orderDb.setCustomerNoticeJson(notice); |
| 313 | } | 333 | } |
| 314 | LocalDateTime mealTime = Instant.ofEpochMilli(creation.getMealTime()) | 334 | LocalDateTime mealTime = Instant.ofEpochMilli(creation.getMealTime()) |
| ... | @@ -356,7 +376,19 @@ public class OrderDbServiceImpl extends ServiceImpl<OrderDbMapper, OrderDb> impl | ... | @@ -356,7 +376,19 @@ public class OrderDbServiceImpl extends ServiceImpl<OrderDbMapper, OrderDb> impl |
| 356 | } | 376 | } |
| 357 | orderDetailDb.setNotServe(false); | 377 | orderDetailDb.setNotServe(false); |
| 358 | if (detailCreation.getShouldCreateMealDetailJson()) { | 378 | if (detailCreation.getShouldCreateMealDetailJson()) { |
| 359 | - orderDetailDb.setMealDetailJson(objectMapper.valueToTree(detailCreation.getMealDetailJson())); | 379 | + orderDetailDb.setMealDetailJson(objectMapper.valueToTree(MealDetailJson.builder() |
| 380 | + .allergies(detailCreation.getMealDetailJson().getAllergiesList()) | ||
| 381 | + .avoids(detailCreation.getMealDetailJson().getAvoidsList()) | ||
| 382 | + .basicMaterials(detailCreation.getMealDetailJson().getBasicMaterials()) | ||
| 383 | + .carbohydrate(detailCreation.getMealDetailJson().getCarbohydrate()) | ||
| 384 | + .dockers(detailCreation.getMealDetailJson().getDockersList()) | ||
| 385 | + .efficacies(detailCreation.getMealDetailJson().getEfficaciesList()) | ||
| 386 | + .energy(detailCreation.getMealDetailJson().getEnergy()) | ||
| 387 | + .fat(detailCreation.getMealDetailJson().getFat()) | ||
| 388 | + .protein(detailCreation.getMealDetailJson().getProtein()) | ||
| 389 | + .skus(detailCreation.getMealDetailJson().getSkusList()) | ||
| 390 | + .tastes(detailCreation.getMealDetailJson().getTastesList()) | ||
| 391 | + .build())); | ||
| 360 | } | 392 | } |
| 361 | return orderDetailDb; | 393 | return orderDetailDb; |
| 362 | }) | 394 | }) |
| ... | @@ -377,7 +409,8 @@ public class OrderDbServiceImpl extends ServiceImpl<OrderDbMapper, OrderDb> impl | ... | @@ -377,7 +409,8 @@ public class OrderDbServiceImpl extends ServiceImpl<OrderDbMapper, OrderDb> impl |
| 377 | QueryWrapper<OrderDb> wrapper = new QueryWrapper<>(); | 409 | QueryWrapper<OrderDb> wrapper = new QueryWrapper<>(); |
| 378 | wrapper.eq("customerId", customerId) | 410 | wrapper.eq("customerId", customerId) |
| 379 | .orderByDesc("createdAt"); | 411 | .orderByDesc("createdAt"); |
| 380 | - return orderDbMapper.selectList(wrapper); | 412 | + final var res = orderDbMapper.selectList(wrapper); |
| 413 | + return res; | ||
| 381 | } | 414 | } |
| 382 | 415 | ||
| 383 | @Override | 416 | @Override |
| ... | @@ -462,7 +495,10 @@ public class OrderDbServiceImpl extends ServiceImpl<OrderDbMapper, OrderDb> impl | ... | @@ -462,7 +495,10 @@ public class OrderDbServiceImpl extends ServiceImpl<OrderDbMapper, OrderDb> impl |
| 462 | } | 495 | } |
| 463 | ObjectMapper objectMapper = new ObjectMapper(); | 496 | ObjectMapper objectMapper = new ObjectMapper(); |
| 464 | if (updateOrderRequest.getModification().getShouldUpdateCustomerNoticeJson()) { | 497 | if (updateOrderRequest.getModification().getShouldUpdateCustomerNoticeJson()) { |
| 465 | - orderDb.setCustomerNoticeJson(objectMapper.valueToTree(updateOrderRequest.getModification().getCustomerNoticeJson())); | 498 | + orderDb.setCustomerNoticeJson(objectMapper.valueToTree(CustomerNoticeDto.builder() |
| 499 | + .avoids(updateOrderRequest.getModification().getCustomerNoticeJson().getAvoidsList()) | ||
| 500 | + .doctors(updateOrderRequest.getModification().getCustomerNoticeJson().getDoctorsList()) | ||
| 501 | + .build())); | ||
| 466 | } | 502 | } |
| 467 | if (updateOrderRequest.getModification().getShouldUpdateIsRead()) { | 503 | if (updateOrderRequest.getModification().getShouldUpdateIsRead()) { |
| 468 | orderDb.setIsRead(updateOrderRequest.getModification().getIsRead()); | 504 | orderDb.setIsRead(updateOrderRequest.getModification().getIsRead()); |
| ... | @@ -557,7 +593,10 @@ public class OrderDbServiceImpl extends ServiceImpl<OrderDbMapper, OrderDb> impl | ... | @@ -557,7 +593,10 @@ public class OrderDbServiceImpl extends ServiceImpl<OrderDbMapper, OrderDb> impl |
| 557 | } | 593 | } |
| 558 | ObjectMapper objectMapper = new ObjectMapper(); | 594 | ObjectMapper objectMapper = new ObjectMapper(); |
| 559 | if (modification.getShouldUpdateCustomerNoticeJson()) { | 595 | if (modification.getShouldUpdateCustomerNoticeJson()) { |
| 560 | - orderDb.setCustomerNoticeJson(objectMapper.valueToTree(modification.getCustomerNoticeJson())); | 596 | + orderDb.setCustomerNoticeJson(objectMapper.valueToTree(CustomerNoticeDto.builder() |
| 597 | + .avoids(modification.getCustomerNoticeJson().getAvoidsList()) | ||
| 598 | + .doctors(modification.getCustomerNoticeJson().getDoctorsList()) | ||
| 599 | + .build())); | ||
| 561 | } | 600 | } |
| 562 | if (modification.getShouldUpdateIsRead()) { | 601 | if (modification.getShouldUpdateIsRead()) { |
| 563 | orderDb.setIsRead(modification.getIsRead()); | 602 | orderDb.setIsRead(modification.getIsRead()); | ... | ... |
| ... | @@ -17,6 +17,7 @@ import com.tianting.infoloop.mapper.OrderDbMapper; | ... | @@ -17,6 +17,7 @@ import com.tianting.infoloop.mapper.OrderDbMapper; |
| 17 | import com.tianting.infoloop.mapper.OrderDetailMapper; | 17 | import com.tianting.infoloop.mapper.OrderDetailMapper; |
| 18 | import com.tianting.infoloop.model.db.OrderDb; | 18 | import com.tianting.infoloop.model.db.OrderDb; |
| 19 | import com.tianting.infoloop.model.db.OrderDetailDb; | 19 | import com.tianting.infoloop.model.db.OrderDetailDb; |
| 20 | +import com.tianting.infoloop.model.dto.MealDetailJson; | ||
| 20 | import com.tianting.infoloop.service.OrderDetailDbService; | 21 | import com.tianting.infoloop.service.OrderDetailDbService; |
| 21 | import com.tianting.infoloop.utils.ProtoBeanUtil; | 22 | import com.tianting.infoloop.utils.ProtoBeanUtil; |
| 22 | import com.tianting.infoloop.utils.exception.ErrorCodeConstants; | 23 | import com.tianting.infoloop.utils.exception.ErrorCodeConstants; |
| ... | @@ -67,7 +68,19 @@ public class OrderDetailDbServiceImpl extends ServiceImpl<OrderDetailMapper, Ord | ... | @@ -67,7 +68,19 @@ public class OrderDetailDbServiceImpl extends ServiceImpl<OrderDetailMapper, Ord |
| 67 | } | 68 | } |
| 68 | orderDetailDb.setNotServe(false); | 69 | orderDetailDb.setNotServe(false); |
| 69 | if (createOrderDetailRequest.getCreation().getShouldCreateMealDetailJson()) { | 70 | if (createOrderDetailRequest.getCreation().getShouldCreateMealDetailJson()) { |
| 70 | - orderDetailDb.setMealDetailJson(objectMapper.valueToTree(createOrderDetailRequest.getCreation().getMealDetailJson())); | 71 | + orderDetailDb.setMealDetailJson(objectMapper.valueToTree(MealDetailJson.builder() |
| 72 | + .allergies(createOrderDetailRequest.getCreation().getMealDetailJson().getAllergiesList()) | ||
| 73 | + .avoids(createOrderDetailRequest.getCreation().getMealDetailJson().getAvoidsList()) | ||
| 74 | + .basicMaterials(createOrderDetailRequest.getCreation().getMealDetailJson().getBasicMaterials()) | ||
| 75 | + .carbohydrate(createOrderDetailRequest.getCreation().getMealDetailJson().getCarbohydrate()) | ||
| 76 | + .dockers(createOrderDetailRequest.getCreation().getMealDetailJson().getDockersList()) | ||
| 77 | + .efficacies(createOrderDetailRequest.getCreation().getMealDetailJson().getEfficaciesList()) | ||
| 78 | + .energy(createOrderDetailRequest.getCreation().getMealDetailJson().getEnergy()) | ||
| 79 | + .fat(createOrderDetailRequest.getCreation().getMealDetailJson().getFat()) | ||
| 80 | + .protein(createOrderDetailRequest.getCreation().getMealDetailJson().getProtein()) | ||
| 81 | + .skus(createOrderDetailRequest.getCreation().getMealDetailJson().getSkusList()) | ||
| 82 | + .tastes(createOrderDetailRequest.getCreation().getMealDetailJson().getTastesList()) | ||
| 83 | + .build())); | ||
| 71 | } | 84 | } |
| 72 | boolean res = this.save(orderDetailDb); | 85 | boolean res = this.save(orderDetailDb); |
| 73 | if (!res) { | 86 | if (!res) { |
| ... | @@ -80,8 +93,6 @@ public class OrderDetailDbServiceImpl extends ServiceImpl<OrderDetailMapper, Ord | ... | @@ -80,8 +93,6 @@ public class OrderDetailDbServiceImpl extends ServiceImpl<OrderDetailMapper, Ord |
| 80 | public List<OrderDetailDb> batchCreateOrderDetailDb(BatchCreateClientCustomerOrderDetailsRpcRequest | 93 | public List<OrderDetailDb> batchCreateOrderDetailDb(BatchCreateClientCustomerOrderDetailsRpcRequest |
| 81 | batchCreateOrderDetailsRequest) { | 94 | batchCreateOrderDetailsRequest) { |
| 82 | ObjectMapper objectMapper = new ObjectMapper(); | 95 | ObjectMapper objectMapper = new ObjectMapper(); |
| 83 | -// objectMapper.setPropertyNamingStrategy(PropertyNamingStrategy.LOWER_CAMEL_CASE); | ||
| 84 | -// objectMapper.configure(SerializationFeature.FAIL_ON_EMPTY_BEANS, false); | ||
| 85 | var list = batchCreateOrderDetailsRequest.getCreationsList().stream() | 96 | var list = batchCreateOrderDetailsRequest.getCreationsList().stream() |
| 86 | .map(creation -> { | 97 | .map(creation -> { |
| 87 | OrderDetailDb orderDetailDb = new OrderDetailDb(); | 98 | OrderDetailDb orderDetailDb = new OrderDetailDb(); |
| ... | @@ -110,14 +121,19 @@ public class OrderDetailDbServiceImpl extends ServiceImpl<OrderDetailMapper, Ord | ... | @@ -110,14 +121,19 @@ public class OrderDetailDbServiceImpl extends ServiceImpl<OrderDetailMapper, Ord |
| 110 | orderDetailDb.setAdjustSkuRemark(creation.getAdjustSkuRemark()); | 121 | orderDetailDb.setAdjustSkuRemark(creation.getAdjustSkuRemark()); |
| 111 | } | 122 | } |
| 112 | if (creation.getShouldCreateMealDetailJson()) { | 123 | if (creation.getShouldCreateMealDetailJson()) { |
| 113 | -// final JsonNode a; | 124 | + orderDetailDb.setMealDetailJson(objectMapper.valueToTree(MealDetailJson.builder() |
| 114 | -// try { | 125 | + .allergies(creation.getMealDetailJson().getAllergiesList()) |
| 115 | -// a = objectMapper.readTree(ProtoBeanUtil.protoToJson(creation.getMealDetailJson())); | 126 | + .avoids(creation.getMealDetailJson().getAvoidsList()) |
| 116 | -// orderDetailDb.setMealDetailJson(a); | 127 | + .basicMaterials(creation.getMealDetailJson().getBasicMaterials()) |
| 117 | -// System.out.println(a); | 128 | + .carbohydrate(creation.getMealDetailJson().getCarbohydrate()) |
| 118 | -// } catch (JsonProcessingException e) { | 129 | + .dockers(creation.getMealDetailJson().getDockersList()) |
| 119 | -// e.printStackTrace(); | 130 | + .efficacies(creation.getMealDetailJson().getEfficaciesList()) |
| 120 | -// } | 131 | + .energy(creation.getMealDetailJson().getEnergy()) |
| 132 | + .fat(creation.getMealDetailJson().getFat()) | ||
| 133 | + .protein(creation.getMealDetailJson().getProtein()) | ||
| 134 | + .skus(creation.getMealDetailJson().getSkusList()) | ||
| 135 | + .tastes(creation.getMealDetailJson().getTastesList()) | ||
| 136 | + .build())); | ||
| 121 | } | 137 | } |
| 122 | return orderDetailDb; | 138 | return orderDetailDb; |
| 123 | }) | 139 | }) |
| ... | @@ -162,8 +178,19 @@ public class OrderDetailDbServiceImpl extends ServiceImpl<OrderDetailMapper, Ord | ... | @@ -162,8 +178,19 @@ public class OrderDetailDbServiceImpl extends ServiceImpl<OrderDetailMapper, Ord |
| 162 | } | 178 | } |
| 163 | if (updateOrderDetailRequest.getModification().getShouldUpdateMealDetailJson()) { | 179 | if (updateOrderDetailRequest.getModification().getShouldUpdateMealDetailJson()) { |
| 164 | ObjectMapper objectMapper = new ObjectMapper(); | 180 | ObjectMapper objectMapper = new ObjectMapper(); |
| 165 | - orderDetailDb.setMealDetailJson(objectMapper.valueToTree(updateOrderDetailRequest. | 181 | + orderDetailDb.setMealDetailJson(objectMapper.valueToTree(MealDetailJson.builder() |
| 166 | - getModification().getMealDetailJson())); | 182 | + .allergies(updateOrderDetailRequest.getModification().getMealDetailJson().getAllergiesList()) |
| 183 | + .avoids(updateOrderDetailRequest.getModification().getMealDetailJson().getAvoidsList()) | ||
| 184 | + .basicMaterials(updateOrderDetailRequest.getModification().getMealDetailJson().getBasicMaterials()) | ||
| 185 | + .carbohydrate(updateOrderDetailRequest.getModification().getMealDetailJson().getCarbohydrate()) | ||
| 186 | + .dockers(updateOrderDetailRequest.getModification().getMealDetailJson().getDockersList()) | ||
| 187 | + .efficacies(updateOrderDetailRequest.getModification().getMealDetailJson().getEfficaciesList()) | ||
| 188 | + .energy(updateOrderDetailRequest.getModification().getMealDetailJson().getEnergy()) | ||
| 189 | + .fat(updateOrderDetailRequest.getModification().getMealDetailJson().getFat()) | ||
| 190 | + .protein(updateOrderDetailRequest.getModification().getMealDetailJson().getProtein()) | ||
| 191 | + .skus(updateOrderDetailRequest.getModification().getMealDetailJson().getSkusList()) | ||
| 192 | + .tastes(updateOrderDetailRequest.getModification().getMealDetailJson().getTastesList()) | ||
| 193 | + .build())); | ||
| 167 | } | 194 | } |
| 168 | return orderDetailMapper.updateById(orderDetailDb) > 0; | 195 | return orderDetailMapper.updateById(orderDetailDb) > 0; |
| 169 | } | 196 | } |
| ... | @@ -203,7 +230,19 @@ public class OrderDetailDbServiceImpl extends ServiceImpl<OrderDetailMapper, Ord | ... | @@ -203,7 +230,19 @@ public class OrderDetailDbServiceImpl extends ServiceImpl<OrderDetailMapper, Ord |
| 203 | } | 230 | } |
| 204 | if (modification.getShouldUpdateMealDetailJson()) { | 231 | if (modification.getShouldUpdateMealDetailJson()) { |
| 205 | ObjectMapper objectMapper = new ObjectMapper(); | 232 | ObjectMapper objectMapper = new ObjectMapper(); |
| 206 | - orderDetailDb.setMealDetailJson(objectMapper.valueToTree(modification.getMealDetailJson())); | 233 | + orderDetailDb.setMealDetailJson(objectMapper.valueToTree(MealDetailJson.builder() |
| 234 | + .allergies(modification.getMealDetailJson().getAllergiesList()) | ||
| 235 | + .avoids(modification.getMealDetailJson().getAvoidsList()) | ||
| 236 | + .basicMaterials(modification.getMealDetailJson().getBasicMaterials()) | ||
| 237 | + .carbohydrate(modification.getMealDetailJson().getCarbohydrate()) | ||
| 238 | + .dockers(modification.getMealDetailJson().getDockersList()) | ||
| 239 | + .efficacies(modification.getMealDetailJson().getEfficaciesList()) | ||
| 240 | + .energy(modification.getMealDetailJson().getEnergy()) | ||
| 241 | + .fat(modification.getMealDetailJson().getFat()) | ||
| 242 | + .protein(modification.getMealDetailJson().getProtein()) | ||
| 243 | + .skus(modification.getMealDetailJson().getSkusList()) | ||
| 244 | + .tastes(modification.getMealDetailJson().getTastesList()) | ||
| 245 | + .build())); | ||
| 207 | } | 246 | } |
| 208 | return orderDetailDb; | 247 | return orderDetailDb; |
| 209 | }).collect(Collectors.toList()); | 248 | }).collect(Collectors.toList()); | ... | ... |
This diff is collapsed. Click to expand it.
| 1 | +package com.tianting.infoloop.utils; | ||
| 2 | + | ||
| 3 | +import com.fasterxml.jackson.databind.JsonNode; | ||
| 4 | +import com.fasterxml.jackson.databind.ObjectMapper; | ||
| 5 | +import com.google.protobuf.Message; | ||
| 6 | +import com.google.protobuf.util.JsonFormat; | ||
| 7 | +import java.lang.reflect.Constructor; | ||
| 8 | +import java.lang.reflect.Field; | ||
| 9 | +import java.time.Instant; | ||
| 10 | +import java.time.LocalDateTime; | ||
| 11 | +import java.time.ZoneOffset; | ||
| 12 | +import java.util.ArrayList; | ||
| 13 | +import java.util.Iterator; | ||
| 14 | +import java.util.List; | ||
| 15 | +import java.util.Map; | ||
| 16 | + | ||
| 17 | +public class GenericBeanToProtoListConverter { | ||
| 18 | + | ||
| 19 | + // 将Java Bean列表转换为对应的ProtoBuf消息列表 | ||
| 20 | + public static <M, T extends Message> List<T> beanListToProtoList(List<M> beanSourceList, Class<T> protoBean) { | ||
| 21 | + List<T> resultList = new ArrayList<>(); | ||
| 22 | + | ||
| 23 | + try { | ||
| 24 | + for (M bean : beanSourceList) { | ||
| 25 | + // 创建ProtoBuf消息实例 | ||
| 26 | + Constructor<T> declaredConstructor; | ||
| 27 | + try { | ||
| 28 | + declaredConstructor = protoBean.getDeclaredConstructor(); | ||
| 29 | + declaredConstructor.setAccessible(true); | ||
| 30 | + T instance = declaredConstructor.newInstance(); | ||
| 31 | + | ||
| 32 | + // 将Java Bean的各个字段值设置到ProtoBuf消息实例中 | ||
| 33 | + setProtoFieldsFromBean(bean, instance, true); | ||
| 34 | + | ||
| 35 | + resultList.add(instance); | ||
| 36 | + } catch (NoSuchMethodException e) { | ||
| 37 | + // 如果没有无参构造函数,尝试查找其他构造函数并传入合适参数(这里假设可以传入空参数列表创建实例,实际可能需根据具体情况调整) | ||
| 38 | + Constructor<?>[] constructors = protoBean.getConstructors(); | ||
| 39 | + if (constructors.length > 0) { | ||
| 40 | + declaredConstructor = (Constructor<T>) constructors[0]; | ||
| 41 | + declaredConstructor.setAccessible(true); | ||
| 42 | + T instance = declaredConstructor.newInstance(); | ||
| 43 | + | ||
| 44 | + // 将Java Bean的各个字段值设置到ProtoBuf消息实例中 | ||
| 45 | + setProtoFieldsFromBean(bean, instance, true); | ||
| 46 | + | ||
| 47 | + resultList.add(instance); | ||
| 48 | + } else { | ||
| 49 | + throw new IllegalArgumentException("ProtoBuf消息类型 " + protoBean.getName() + " 没有可用的构造函数来创建实例", e); | ||
| 50 | + } | ||
| 51 | + } | ||
| 52 | + } | ||
| 53 | + } catch (Exception e) { | ||
| 54 | + e.printStackTrace(); | ||
| 55 | + // 根据具体需求,可在这里进行更详细的错误处理,比如抛出特定的转换失败异常给调用者等 | ||
| 56 | + throw new RuntimeException("Java Bean列表转换为ProtoBuf消息列表失败", e); | ||
| 57 | + } | ||
| 58 | + | ||
| 59 | + return resultList; | ||
| 60 | + } | ||
| 61 | + | ||
| 62 | + // 将Java Bean的各个字段值设置到ProtoBuf消息实例中 | ||
| 63 | + private static <M, T extends Message> void setProtoFieldsFromBean(M bean, T protoInstance, boolean ignoreUnknownFields) throws Exception { | ||
| 64 | + // 获取Java Bean的所有字段 | ||
| 65 | + Field[] beanFields = bean.getClass().getDeclaredFields(); | ||
| 66 | + for (Field beanField : beanFields) { | ||
| 67 | + beanField.setAccessible(true); | ||
| 68 | + | ||
| 69 | + // 获取ProtoBuf消息实例中对应的字段 | ||
| 70 | + Field protoField; | ||
| 71 | + try { | ||
| 72 | + protoField = protoInstance.getClass().getDeclaredField(beanField.getName()); | ||
| 73 | + protoField.setAccessible(true); | ||
| 74 | + } catch (NoSuchFieldException e) { | ||
| 75 | + if (ignoreUnknownFields) { | ||
| 76 | + continue; | ||
| 77 | + } else { | ||
| 78 | + throw e; | ||
| 79 | + } | ||
| 80 | + } | ||
| 81 | + | ||
| 82 | + // 获取Java Bean字段的值 | ||
| 83 | + Object beanFieldValue = beanField.get(bean); | ||
| 84 | + | ||
| 85 | + if (beanFieldValue!= null) { | ||
| 86 | + if (beanFieldValue instanceof JsonNode) { | ||
| 87 | + // 处理JsonNode类型的字段 | ||
| 88 | + JsonNode jsonNodeValue = (JsonNode) beanFieldValue; | ||
| 89 | + | ||
| 90 | + // 创建对应的嵌套ProtoBuf消息实例(假设是嵌套消息类型,这里以通用的处理方式为例) | ||
| 91 | + Constructor<?> nestedProtoConstructor = protoField.getType().getDeclaredConstructor(); | ||
| 92 | + nestedProtoConstructor.setAccessible(true); | ||
| 93 | + Object nestedProtoInstance = nestedProtoConstructor.newInstance(); | ||
| 94 | + | ||
| 95 | + // 设置嵌套ProtoBuf消息实例的字段值(这里假设嵌套消息类型有类似的处理逻辑,具体根据实际情况而定) | ||
| 96 | + setProtoFieldsFromBean(jsonNodeValue, nestedProtoInstance, ignoreUnknownFields); | ||
| 97 | + | ||
| 98 | + // 将设置好的嵌套ProtoBuf消息实例设置到ProtoBuf消息实例的对应字段中 | ||
| 99 | + protoField.set(protoInstance, nestedProtoInstance); | ||
| 100 | + } else if (beanFieldValue instanceof LocalDateTime) { | ||
| 101 | + // 处理LocalDateTime类型的字段,将其转换为时间戳(毫秒)后设置到ProtoBuf消息实例中 | ||
| 102 | + LocalDateTime localDateTimeValue = (LocalDateTime) beanFieldValue; | ||
| 103 | + long timestamp = localDateTimeValue.toInstant(ZoneOffset.UTC).toEpochMilli(); | ||
| 104 | + protoField.set(protoInstance, timestamp); | ||
| 105 | + } else if (beanFieldValue instanceof Integer) { | ||
| 106 | + // 处理Integer类型字段,直接设置到ProtoBuf消息实例对应字段 | ||
| 107 | + protoField.set(protoInstance, (Integer) beanFieldValue); | ||
| 108 | + } else if (beanFieldValue instanceof String) { | ||
| 109 | + // 处理String类型字段,直接设置到ProtoBuf消息实例对应字段 | ||
| 110 | + protoField.set(protoInstance, (String) beanFieldValue); | ||
| 111 | + } else if (beanFieldValue instanceof Boolean) { | ||
| 112 | + // 处理Boolean类型字段,直接设置到ProtoBuf消息实例对应字段 | ||
| 113 | + protoField.set(protoInstance, (Boolean) beanFieldValue); | ||
| 114 | + } else if (beanFieldValue instanceof Long) { | ||
| 115 | + // 处理Long类型字段,直接设置到ProtoBuf消息实例对应字段 | ||
| 116 | + protoField.set(protoInstance, (Long) beanFieldValue); | ||
| 117 | + } else { | ||
| 118 | + // 对于其他未明确处理的类型,尝试通过其toString方法转换为字符串后设置(可能需要根据具体类型进一步优化) | ||
| 119 | + protoField.set(protoInstance, beanFieldValue.toString()); | ||
| 120 | + } | ||
| 121 | + } else { | ||
| 122 | + // 如果Java Bean字段的值为null,将ProtoBuf消息实例的对应字段也设置为null | ||
| 123 | + protoField.set(protoInstance, null); | ||
| 124 | + } | ||
| 125 | + } | ||
| 126 | + } | ||
| 127 | + | ||
| 128 | + // 将JsonNode的值设置到对应的ProtoBuf消息实例(这里假设是嵌套消息类型,以通用处理方式为例) | ||
| 129 | + private static void setProtoFieldsFromBean(JsonNode jsonNode, Object protoInstance, boolean ignoreUnknownFields) throws Exception { | ||
| 130 | + // 获取JsonNode的所有字段 | ||
| 131 | + Iterator<Map.Entry<String, JsonNode>> jsonNodeFields = jsonNode.fields(); | ||
| 132 | + while (jsonNodeFields.hasNext()) { | ||
| 133 | + Map.Entry<String, JsonNode> entry = jsonNodeFields.next(); | ||
| 134 | + String fieldName = entry.getKey(); | ||
| 135 | + JsonNode fieldValue = entry.getValue(); | ||
| 136 | + | ||
| 137 | + // 获取ProtoBuf消息实例中对应的字段 | ||
| 138 | + Field protoField; | ||
| 139 | + try { | ||
| 140 | + protoField = protoInstance.getClass().getDeclaredField(fieldName); | ||
| 141 | + protoField.setAccessible(true); | ||
| 142 | + } catch (NoSuchFieldException e) { | ||
| 143 | + if (ignoreUnknownFields) { | ||
| 144 | + continue; | ||
| 145 | + } else { | ||
| 146 | + throw e; | ||
| 147 | + } | ||
| 148 | + } | ||
| 149 | + | ||
| 150 | + if (fieldValue!= null) { | ||
| 151 | + if (fieldValue instanceof JsonNode) { | ||
| 152 | + // 递归处理嵌套的JsonNode类型的字段 | ||
| 153 | + Constructor<?> nestedProtoConstructor = protoField.getType().getDeclaredConstructor(); | ||
| 154 | + nestedProtoConstructor.setAccessible(true); | ||
| 155 | + Object nestedProtoInstance = nestedProtoInstance = nestedProtoConstructor.newInstance(); | ||
| 156 | + | ||
| 157 | + setProtoFieldsFromBean((JsonNode) fieldValue, nestedProtoInstance, ignoreUnknownFields); | ||
| 158 | + | ||
| 159 | + protoField.set(protoInstance, nestedProtoInstance); | ||
| 160 | + } else { | ||
| 161 | + // 将非JsonNode类型的值设置到ProtoBuf消息实例的对应字段中 | ||
| 162 | + protoField.set(protoInstance, fieldValue.asText()); | ||
| 163 | + } | ||
| 164 | + } else { | ||
| 165 | + // 如果JsonNode字段的值为null,将ProtoBuf消息实例的对应字段也设置为null | ||
| 166 | + protoField.set(protoInstance, null); | ||
| 167 | + } | ||
| 168 | + } | ||
| 169 | + } | ||
| 170 | +} | ||
| ... | \ No newline at end of file | ... | \ No newline at end of file |
| ... | @@ -8,7 +8,12 @@ import java.sql.CallableStatement; | ... | @@ -8,7 +8,12 @@ import java.sql.CallableStatement; |
| 8 | import java.sql.PreparedStatement; | 8 | import java.sql.PreparedStatement; |
| 9 | import java.sql.ResultSet; | 9 | import java.sql.ResultSet; |
| 10 | import java.sql.SQLException; | 10 | import java.sql.SQLException; |
| 11 | +import org.apache.ibatis.type.MappedJdbcTypes; | ||
| 12 | +import org.apache.ibatis.type.MappedTypes; | ||
| 11 | 13 | ||
| 14 | +@MappedTypes(JsonNode.class) | ||
| 15 | +// 定义这个类型处理器对应的JDBC类型为OTHER(因为JSON数据在这种情况下没有直接对应的标准JDBC类型) | ||
| 16 | +@MappedJdbcTypes(JdbcType.OTHER) | ||
| 12 | public class MysqlTypeTransferUtil extends BaseTypeHandler<JsonNode> { | 17 | public class MysqlTypeTransferUtil extends BaseTypeHandler<JsonNode> { |
| 13 | 18 | ||
| 14 | private static final ObjectMapper objectMapper = new ObjectMapper(); | 19 | private static final ObjectMapper objectMapper = new ObjectMapper(); | ... | ... |
| 1 | package com.tianting.infoloop.utils; | 1 | package com.tianting.infoloop.utils; |
| 2 | 2 | ||
| 3 | import cn.hutool.core.date.LocalDateTimeUtil; | 3 | import cn.hutool.core.date.LocalDateTimeUtil; |
| 4 | +import com.fasterxml.jackson.databind.JsonNode; | ||
| 5 | +import com.fasterxml.jackson.databind.ObjectMapper; | ||
| 4 | import com.google.gson.Gson; | 6 | import com.google.gson.Gson; |
| 5 | import com.google.gson.GsonBuilder; | 7 | import com.google.gson.GsonBuilder; |
| 6 | import com.google.gson.TypeAdapter; | 8 | import com.google.gson.TypeAdapter; |
| ... | @@ -159,7 +161,24 @@ public class ProtoBeanUtil { | ... | @@ -159,7 +161,24 @@ public class ProtoBeanUtil { |
| 159 | return LocalDate.ofInstant(Instant.ofEpochMilli(Long.parseLong(in.nextString())), DateFormatterConstants.CHINA_ZONE); | 161 | return LocalDate.ofInstant(Instant.ofEpochMilli(Long.parseLong(in.nextString())), DateFormatterConstants.CHINA_ZONE); |
| 160 | } | 162 | } |
| 161 | } | 163 | } |
| 162 | - ).create(); | 164 | + ) |
| 165 | +// .registerTypeAdapter(JsonNode.class, new TypeAdapter<JsonNode>() { | ||
| 166 | +// private final ObjectMapper objectMapper = new ObjectMapper(); | ||
| 167 | +// @Override | ||
| 168 | +// public void write(JsonWriter out, JsonNode value) throws IOException { | ||
| 169 | +// if (value!= null) { | ||
| 170 | +// out.value(value.toString()); | ||
| 171 | +// } else { | ||
| 172 | +// out.nullValue(); | ||
| 173 | +// } | ||
| 174 | +// } | ||
| 175 | +// | ||
| 176 | +// @Override | ||
| 177 | +// public JsonNode read(JsonReader in) throws IOException { | ||
| 178 | +// return objectMapper.readValue(in.nextString(), JsonNode.class); | ||
| 179 | +// } | ||
| 180 | +// }) | ||
| 181 | + .create(); | ||
| 163 | return gson; | 182 | return gson; |
| 164 | } | 183 | } |
| 165 | 184 | ... | ... |
-
Please register or login to post a comment