You've already forked FrameTour-BE
Compare commits
105 Commits
4360ef1313
...
master
| Author | SHA1 | Date | |
|---|---|---|---|
| 5cc32ddf61 | |||
| 07987835ec | |||
| 0a3f4119d7 | |||
| 51c7de2474 | |||
| 773d7f2254 | |||
| af131131ed | |||
| 3f6f1508c5 | |||
| dbee1d9709 | |||
| 83d1096fdb | |||
| 82925d203c | |||
| 3b11ddef6a | |||
| 6e7b4729a8 | |||
| 917cb37ccf | |||
| 7c0a3a63bb | |||
| 478467e124 | |||
| d5befd75e1 | |||
| b2c55c9feb | |||
| fef616c837 | |||
| a5fe00052d | |||
| 349b702fc3 | |||
| 9f5a61247b | |||
| 9321422e56 | |||
| 1834fe3ddd | |||
| fa8f92d38b | |||
| df33e7929f | |||
| 554f55a7c1 | |||
| f71149fd06 | |||
| e8eb8d816b | |||
| 576d87d113 | |||
| a2378053a8 | |||
| c92ea20575 | |||
| bb71cf9458 | |||
| 7749faf807 | |||
| c42b055d5f | |||
| fe3bda28b4 | |||
| 66775ea48b | |||
| 125fadd6c5 | |||
| 1f4a16f0e6 | |||
| e9916d6aca | |||
| b71452b3ed | |||
| 4a82ee6c4d | |||
| 24bbb63bf7 | |||
| ee13ef09f7 | |||
| 33c3a194ca | |||
| 71a8d3b539 | |||
| 82626f615b | |||
| de2eadf764 | |||
| fd143830d3 | |||
| 68916f3f53 | |||
| e27ed7d971 | |||
| 7a19f18962 | |||
| eade5f8092 | |||
| 42540e2dc4 | |||
| 15dda645b9 | |||
| 17419d83e7 | |||
| ae92ba10a7 | |||
| af60cc1540 | |||
| 60b4473279 | |||
| ecd5378b26 | |||
| 8c08c8947e | |||
| b165840176 | |||
| 71d6400a1e | |||
| b3fa10e8fd | |||
| 96e75a458f | |||
| d2ad14175d | |||
| 06c0ade9b4 | |||
| 36f85dbb63 | |||
| 9becd6bfa7 | |||
| 788184fc04 | |||
| 3cf7c81162 | |||
| 88d9463e25 | |||
| 590a7c6191 | |||
| d590286b13 | |||
| b92568b842 | |||
| 1de760fc87 | |||
| 4e9aac4cf3 | |||
| aa43d14316 | |||
| a2d87e7fdc | |||
| 57be6aa983 | |||
| cacb22a7bd | |||
| 300edbe582 | |||
| 9219ea4ab0 | |||
| e292a0798d | |||
| 4244b42d4b | |||
| 8058bc21f5 | |||
| 6dd08ac4e7 | |||
| 610a183be1 | |||
| e9a59cd466 | |||
| d60d7d9ad8 | |||
| d483c222d0 | |||
| a7ef2cb35a | |||
| cbc0584706 | |||
| 67932c374b | |||
| 8a88c74df2 | |||
| 3ce3972875 | |||
| 1945639f90 | |||
| 40d5874560 | |||
| 95419fee66 | |||
| 333c4d3ca7 | |||
| 90efc908c5 | |||
| d2846e6d8e | |||
| 7b18d7c2af | |||
| bcebe5defe | |||
| 4a86849372 | |||
| e418a5ccdb |
13
pom.xml
13
pom.xml
@@ -75,6 +75,12 @@
|
||||
<artifactId>spring-boot-starter-web</artifactId>
|
||||
</dependency>
|
||||
|
||||
<!-- WebSocket -->
|
||||
<dependency>
|
||||
<groupId>org.springframework.boot</groupId>
|
||||
<artifactId>spring-boot-starter-websocket</artifactId>
|
||||
</dependency>
|
||||
|
||||
<!-- Nacos服务发现 -->
|
||||
<dependency>
|
||||
<groupId>com.alibaba.cloud</groupId>
|
||||
@@ -267,6 +273,13 @@
|
||||
<version>5.0.0</version>
|
||||
</dependency>
|
||||
|
||||
<!-- 智谱AI SDK -->
|
||||
<dependency>
|
||||
<groupId>ai.z.openapi</groupId>
|
||||
<artifactId>zai-sdk</artifactId>
|
||||
<version>0.1.3</version>
|
||||
</dependency>
|
||||
|
||||
<!-- Spring Kafka -->
|
||||
<dependency>
|
||||
<groupId>org.springframework.kafka</groupId>
|
||||
|
||||
@@ -1,6 +1,5 @@
|
||||
package com.ycwl.basic;
|
||||
|
||||
import org.mybatis.spring.annotation.MapperScan;
|
||||
import org.springframework.boot.SpringApplication;
|
||||
import org.springframework.boot.autoconfigure.SpringBootApplication;
|
||||
import org.springframework.cloud.client.discovery.EnableDiscoveryClient;
|
||||
@@ -9,8 +8,6 @@ import org.springframework.cloud.openfeign.EnableFeignClients;
|
||||
@SpringBootApplication
|
||||
@EnableDiscoveryClient
|
||||
@EnableFeignClients
|
||||
@MapperScan(basePackages = "com.ycwl.basic.mapper")
|
||||
@MapperScan(basePackages = "com.ycwl.basic.*.mapper")
|
||||
public class Application {
|
||||
|
||||
public static void main(String[] args) {
|
||||
|
||||
@@ -147,6 +147,21 @@ public class OrderBiz {
|
||||
priceObj.setSlashPrice(priceCalculationResult.getOriginalAmount());
|
||||
priceObj.setFaceId(goodsId);
|
||||
break;
|
||||
case 13:
|
||||
PriceCalculationRequest aiCamCalculationRequest = new PriceCalculationRequest();
|
||||
ProductItem aiCamProductItem = new ProductItem();
|
||||
aiCamProductItem.setProductType(ProductType.AI_CAM_PHOTO_SET);
|
||||
aiCamProductItem.setProductId(scenicId.toString());
|
||||
aiCamProductItem.setPurchaseCount(1);
|
||||
aiCamProductItem.setScenicId(scenicId.toString());
|
||||
aiCamCalculationRequest.setProducts(Collections.singletonList(aiCamProductItem));
|
||||
aiCamCalculationRequest.setPreviewOnly(true); // 仅查询价格,不实际使用优惠
|
||||
PriceCalculationResult aiCamPriceCalculationResult = iPriceCalculationService.calculatePrice(aiCamCalculationRequest);
|
||||
priceObj.setPrice(aiCamPriceCalculationResult.getFinalAmount());
|
||||
priceObj.setSlashPrice(aiCamPriceCalculationResult.getOriginalAmount());
|
||||
priceObj.setFaceId(goodsId);
|
||||
priceObj.setScenicId(scenicId);
|
||||
break;
|
||||
}
|
||||
return priceObj;
|
||||
}
|
||||
@@ -215,11 +230,15 @@ public class OrderBiz {
|
||||
orderRepository.updateOrder(orderId, orderUpdate);
|
||||
orderItems.forEach(item -> {
|
||||
switch (item.getGoodsType()) {
|
||||
case -1: // vlog视频模板
|
||||
videoRepository.setUserIsBuyTemplate(order.getMemberId(), item.getGoodsId(), order.getId(), order.getFaceId());
|
||||
break;
|
||||
case 0: // vlog视频
|
||||
videoRepository.setUserIsBuyItem(order.getMemberId(), item.getGoodsId(), order.getId());
|
||||
break;
|
||||
case 1: // 视频原素材
|
||||
case 2: // 照片原素材
|
||||
case 13: // AI微单
|
||||
sourceRepository.setUserIsBuyItem(order.getMemberId(), item.getGoodsType(), item.getGoodsId(), order.getId());
|
||||
break;
|
||||
case 3:
|
||||
@@ -259,9 +278,11 @@ public class OrderBiz {
|
||||
switch (item.getGoodsType()) {
|
||||
case 0: // vlog视频
|
||||
videoRepository.setUserNotBuyItem(order.getMemberId(), item.getGoodsId());
|
||||
break;
|
||||
case 1: // 视频原素材
|
||||
case 2: // 照片原素材
|
||||
sourceRepository.setUserNotBuyItem(order.getMemberId(), item.getGoodsType(), item.getGoodsId());
|
||||
break;
|
||||
}
|
||||
});
|
||||
orderRepository.clearOrderCache(orderId); // 更新完了,清理下
|
||||
@@ -281,9 +302,11 @@ public class OrderBiz {
|
||||
switch (item.getGoodsType()) {
|
||||
case 0: // vlog视频
|
||||
videoRepository.setUserNotBuyItem(order.getMemberId(), item.getGoodsId());
|
||||
break;
|
||||
case 1: // 视频原素材
|
||||
case 2: // 照片原素材
|
||||
sourceRepository.setUserNotBuyItem(order.getMemberId(), item.getGoodsType(), item.getGoodsId());
|
||||
break;
|
||||
}
|
||||
});
|
||||
orderRepository.clearOrderCache(orderId); // 更新完了,清理下
|
||||
@@ -291,10 +314,14 @@ public class OrderBiz {
|
||||
}
|
||||
|
||||
/**
|
||||
* 检查用户是否购买了指定商品
|
||||
* 提供给PriceBiz使用,避免循环调用
|
||||
* 检查用户是否购买了指定商品,并额外校验订单的faceId是否匹配
|
||||
* @param userId 用户ID
|
||||
* @param faceId 人脸ID
|
||||
* @param goodsType 商品类型
|
||||
* @param goodsId 商品ID
|
||||
* @return 是否已购买且faceId匹配
|
||||
*/
|
||||
public boolean checkUserBuyItem(Long userId, int goodsType, Long goodsId) {
|
||||
return orderRepository.checkUserBuyItem(userId, goodsType, goodsId);
|
||||
public boolean checkUserBuyFaceItem(Long userId, Long faceId, int goodsType, Long goodsId) {
|
||||
return orderRepository.checkUserBuyFaceItem(userId, faceId, goodsType, goodsId);
|
||||
}
|
||||
}
|
||||
|
||||
@@ -7,9 +7,14 @@ import com.ycwl.basic.model.pc.face.entity.FaceEntity;
|
||||
import com.ycwl.basic.model.pc.order.entity.OrderEntity;
|
||||
import com.ycwl.basic.model.pc.price.entity.PriceConfigEntity;
|
||||
import com.ycwl.basic.model.pc.price.resp.GoodsListRespVO;
|
||||
import com.ycwl.basic.model.pc.price.resp.SimpleGoodsRespVO;
|
||||
import com.ycwl.basic.model.pc.scenic.entity.ScenicConfigEntity;
|
||||
import com.ycwl.basic.model.pc.template.resp.TemplateRespVO;
|
||||
import com.ycwl.basic.model.pc.video.entity.MemberVideoEntity;
|
||||
import com.ycwl.basic.product.capability.ProductTypeCapability;
|
||||
import com.ycwl.basic.product.service.IProductTypeCapabilityManagementService;
|
||||
import com.ycwl.basic.puzzle.entity.PuzzleTemplateEntity;
|
||||
import com.ycwl.basic.puzzle.mapper.PuzzleTemplateMapper;
|
||||
import com.ycwl.basic.repository.FaceRepository;
|
||||
import com.ycwl.basic.repository.MemberRelationRepository;
|
||||
import com.ycwl.basic.repository.PriceRepository;
|
||||
@@ -46,6 +51,10 @@ public class PriceBiz {
|
||||
private CouponBiz couponBiz;
|
||||
@Autowired
|
||||
private MemberRelationRepository memberRelationRepository;
|
||||
@Autowired
|
||||
private PuzzleTemplateMapper puzzleTemplateMapper;
|
||||
@Autowired
|
||||
private IProductTypeCapabilityManagementService productTypeCapabilityManagementService;
|
||||
|
||||
public List<GoodsListRespVO> listGoodsByScenic(Long scenicId) {
|
||||
List<GoodsListRespVO> goodsList = new ArrayList<>();
|
||||
@@ -67,6 +76,117 @@ public class PriceBiz {
|
||||
goodsList.add(new GoodsListRespVO(2L, "照片集", 2));
|
||||
}
|
||||
}
|
||||
// 拼图
|
||||
puzzleTemplateMapper.list(scenicId, null, 1).forEach(puzzleTemplate -> {
|
||||
GoodsListRespVO goods = new GoodsListRespVO();
|
||||
goods.setGoodsId(puzzleTemplate.getId());
|
||||
goods.setGoodsName(puzzleTemplate.getName());
|
||||
goods.setGoodsType(5);
|
||||
goodsList.add(goods);
|
||||
});
|
||||
return goodsList;
|
||||
}
|
||||
|
||||
/**
|
||||
* 根据景区ID和商品类型查询简化的商品列表
|
||||
*
|
||||
* @param scenicId 景区ID
|
||||
* @param productType 商品类型(可选,为空时返回所有商品)
|
||||
* @return 简化的商品列表
|
||||
*/
|
||||
public List<SimpleGoodsRespVO> listSimpleGoodsByScenic(Long scenicId, String productType) {
|
||||
List<SimpleGoodsRespVO> goodsList = new ArrayList<>();
|
||||
|
||||
// 如果 productType 为空,兼容旧逻辑
|
||||
if (productType == null || productType.isEmpty()) {
|
||||
return listAllSimpleGoods(scenicId);
|
||||
}
|
||||
|
||||
// 根据 productType 查询不同数据源
|
||||
switch (productType) {
|
||||
case "VLOG_VIDEO":
|
||||
// 从 template 表查询视频模板
|
||||
List<TemplateRespVO> templateList = templateRepository.getTemplateListByScenicId(scenicId);
|
||||
templateList.stream()
|
||||
.map(template -> new SimpleGoodsRespVO(template.getId(), template.getName(), productType))
|
||||
.forEach(goodsList::add);
|
||||
break;
|
||||
|
||||
case "PHOTO_VLOG":
|
||||
// TODO
|
||||
goodsList.add(new SimpleGoodsRespVO(scenicId, "【待实现】pLog视频", productType));
|
||||
break;
|
||||
|
||||
case "PHOTO":
|
||||
goodsList.add(new SimpleGoodsRespVO(scenicId, "单张照片", productType));
|
||||
break;
|
||||
|
||||
case "PHOTO_SET":
|
||||
// 返回固定的照片集条目
|
||||
goodsList.add(new SimpleGoodsRespVO(scenicId, "照片集", productType));
|
||||
break;
|
||||
|
||||
case "AI_CAM_PHOTO_SET":
|
||||
// 返回固定的照片集条目
|
||||
goodsList.add(new SimpleGoodsRespVO(scenicId, "AI微单照片集", productType));
|
||||
break;
|
||||
|
||||
case "PHOTO_LOG":
|
||||
// 从 template 表查询pLog模板
|
||||
goodsList.add(new SimpleGoodsRespVO(scenicId, "pLog图<景区打包>", productType));
|
||||
List<PuzzleTemplateEntity> puzzleList = puzzleTemplateMapper.list(scenicId, null, null);
|
||||
puzzleList.stream()
|
||||
.map(template -> new SimpleGoodsRespVO(template.getId(), template.getName(), productType))
|
||||
.forEach(goodsList::add);
|
||||
break;
|
||||
|
||||
case "RECORDING_SET":
|
||||
// 返回固定的录像集条目
|
||||
goodsList.add(new SimpleGoodsRespVO(scenicId, "录像集", productType));
|
||||
break;
|
||||
|
||||
case "PHOTO_PRINT":
|
||||
// 打印类返回单一通用条目
|
||||
goodsList.add(new SimpleGoodsRespVO(scenicId, "照片打印", productType));
|
||||
break;
|
||||
case "PHOTO_PRINT_MU":
|
||||
// 打印类返回单一通用条目
|
||||
goodsList.add(new SimpleGoodsRespVO(scenicId, "手机照片打印", productType));
|
||||
break;
|
||||
case "PHOTO_PRINT_FX":
|
||||
// 打印类返回单一通用条目
|
||||
goodsList.add(new SimpleGoodsRespVO(scenicId, "效果图片打印", productType));
|
||||
break;
|
||||
case "MACHINE_PRINT":
|
||||
// 打印类返回单一通用条目
|
||||
goodsList.add(new SimpleGoodsRespVO(scenicId, "一体机打印", productType));
|
||||
break;
|
||||
|
||||
default:
|
||||
// 不支持的 productType,返回空列表
|
||||
break;
|
||||
}
|
||||
|
||||
return goodsList;
|
||||
}
|
||||
|
||||
/**
|
||||
* 兼容旧逻辑:返回所有商品
|
||||
* 通过查询系统中所有已知的 productType,将结果综合到一起
|
||||
*/
|
||||
private List<SimpleGoodsRespVO> listAllSimpleGoods(Long scenicId) {
|
||||
List<SimpleGoodsRespVO> goodsList = new ArrayList<>();
|
||||
|
||||
// 从 ProductTypeCapability 服务查询所有已知的商品类型(仅包含启用的)
|
||||
List<ProductTypeCapability> capabilities = productTypeCapabilityManagementService.queryAll(false);
|
||||
|
||||
// 轮询每个商品类型,获取对应的商品列表
|
||||
for (ProductTypeCapability capability : capabilities) {
|
||||
String productType = capability.getProductType();
|
||||
List<SimpleGoodsRespVO> typeGoodsList = listSimpleGoodsByScenic(scenicId, productType);
|
||||
goodsList.addAll(typeGoodsList);
|
||||
}
|
||||
|
||||
return goodsList;
|
||||
}
|
||||
|
||||
@@ -161,7 +281,7 @@ public class PriceBiz {
|
||||
allContentsPurchased = false;
|
||||
break;
|
||||
}
|
||||
boolean hasPurchasedTemplate = orderBiz.checkUserBuyItem(userId, -1, videoEntities.getFirst().getVideoId());
|
||||
boolean hasPurchasedTemplate = orderBiz.checkUserBuyFaceItem(userId, faceId, -1, videoEntities.getFirst().getVideoId());
|
||||
if (!hasPurchasedTemplate) {
|
||||
allContentsPurchased = false;
|
||||
break;
|
||||
@@ -173,7 +293,7 @@ public class PriceBiz {
|
||||
if (scenicConfig != null) {
|
||||
// 检查录像集
|
||||
if (!Boolean.TRUE.equals(scenicConfig.getDisableSourceVideo())) {
|
||||
boolean hasPurchasedRecording = orderBiz.checkUserBuyItem(userId, 1, faceId);
|
||||
boolean hasPurchasedRecording = orderBiz.checkUserBuyFaceItem(userId, faceId, 1, faceId);
|
||||
if (!hasPurchasedRecording) {
|
||||
allContentsPurchased = false;
|
||||
}
|
||||
@@ -181,7 +301,7 @@ public class PriceBiz {
|
||||
|
||||
// 检查照片集
|
||||
if (allContentsPurchased && !Boolean.TRUE.equals(scenicConfig.getDisableSourceImage())) {
|
||||
boolean hasPurchasedPhoto = orderBiz.checkUserBuyItem(userId, 2, faceId);
|
||||
boolean hasPurchasedPhoto = orderBiz.checkUserBuyFaceItem(userId, faceId, 2, faceId);
|
||||
if (!hasPurchasedPhoto) {
|
||||
allContentsPurchased = false;
|
||||
}
|
||||
|
||||
@@ -1,34 +1,26 @@
|
||||
package com.ycwl.basic.config;
|
||||
|
||||
import org.springframework.cache.annotation.CachingConfigurerSupport;
|
||||
import org.springframework.cache.annotation.EnableCaching;
|
||||
import com.fasterxml.jackson.databind.ObjectMapper;
|
||||
import com.fasterxml.jackson.datatype.jsr310.JavaTimeModule;
|
||||
import com.fasterxml.jackson.databind.jsontype.BasicPolymorphicTypeValidator;
|
||||
import com.fasterxml.jackson.databind.jsontype.PolymorphicTypeValidator;
|
||||
import org.springframework.beans.factory.annotation.Autowired;
|
||||
import org.springframework.context.annotation.Bean;
|
||||
import org.springframework.context.annotation.Configuration;
|
||||
import org.springframework.data.redis.cache.RedisCacheConfiguration;
|
||||
import org.springframework.data.redis.connection.RedisConnectionFactory;
|
||||
import org.springframework.data.redis.core.RedisTemplate;
|
||||
import org.springframework.data.redis.serializer.Jackson2JsonRedisSerializer;
|
||||
import org.springframework.data.redis.serializer.RedisSerializationContext;
|
||||
import org.springframework.data.redis.serializer.StringRedisSerializer;
|
||||
|
||||
import java.time.Duration;
|
||||
|
||||
/**
|
||||
* @author wenshijia
|
||||
* @date 2021年07月05日 18:34
|
||||
* 修改redis缓存序列化器
|
||||
*/
|
||||
@Configuration
|
||||
@EnableCaching
|
||||
public class CustomRedisCacheManager extends CachingConfigurerSupport {
|
||||
|
||||
@Bean
|
||||
public RedisCacheConfiguration redisCacheConfiguration() {
|
||||
Jackson2JsonRedisSerializer<Object> jackson2JsonRedisSerializer = new Jackson2JsonRedisSerializer<>(Object.class);
|
||||
RedisCacheConfiguration configuration = RedisCacheConfiguration.defaultCacheConfig();
|
||||
configuration = configuration.serializeValuesWith(RedisSerializationContext.SerializationPair.fromSerializer(jackson2JsonRedisSerializer)).entryTtl(Duration.ofMinutes(1));
|
||||
return configuration;
|
||||
}
|
||||
public class CustomRedisCacheManager {
|
||||
@Autowired
|
||||
private ObjectMapper objectMapper;
|
||||
|
||||
/**
|
||||
* 处理redis连接工具显示redis key值显示乱码问题,value值没处理
|
||||
@@ -45,10 +37,23 @@ public class CustomRedisCacheManager extends CachingConfigurerSupport {
|
||||
|
||||
final StringRedisSerializer stringRedisSerializer = new StringRedisSerializer();
|
||||
redisTemplate.setKeySerializer(stringRedisSerializer);
|
||||
redisTemplate.setValueSerializer(stringRedisSerializer);
|
||||
|
||||
redisTemplate.setHashKeySerializer(stringRedisSerializer);
|
||||
|
||||
// Configure Jackson2JsonRedisSerializer with JavaTimeModule for value serialization
|
||||
ObjectMapper objectMapper = new ObjectMapper();
|
||||
objectMapper.registerModule(new JavaTimeModule());
|
||||
|
||||
// Configure type handling to prevent ClassCastException
|
||||
PolymorphicTypeValidator typeValidator = BasicPolymorphicTypeValidator.builder()
|
||||
.allowIfBaseType(Object.class)
|
||||
.build();
|
||||
objectMapper.activateDefaultTyping(typeValidator, ObjectMapper.DefaultTyping.NON_FINAL);
|
||||
|
||||
Jackson2JsonRedisSerializer<Object> jackson2JsonRedisSerializer = new Jackson2JsonRedisSerializer<>(objectMapper, Object.class);
|
||||
|
||||
redisTemplate.setValueSerializer(jackson2JsonRedisSerializer);
|
||||
redisTemplate.setHashValueSerializer(jackson2JsonRedisSerializer);
|
||||
|
||||
redisTemplate.afterPropertiesSet();
|
||||
return redisTemplate;
|
||||
}
|
||||
|
||||
@@ -3,6 +3,7 @@ package com.ycwl.basic.config;
|
||||
import com.baomidou.mybatisplus.annotation.DbType;
|
||||
import com.baomidou.mybatisplus.extension.plugins.MybatisPlusInterceptor;
|
||||
import com.baomidou.mybatisplus.extension.plugins.inner.PaginationInnerInterceptor;
|
||||
import org.mybatis.spring.annotation.MapperScan;
|
||||
import org.springframework.context.annotation.Bean;
|
||||
import org.springframework.context.annotation.Configuration;
|
||||
|
||||
@@ -11,6 +12,15 @@ import org.springframework.context.annotation.Configuration;
|
||||
* @date 2021年06月04日 9:42
|
||||
*/
|
||||
@Configuration
|
||||
@MapperScan(basePackages = {
|
||||
"com.ycwl.basic.mapper",
|
||||
"com.ycwl.basic.order.mapper",
|
||||
"com.ycwl.basic.pricing.mapper",
|
||||
"com.ycwl.basic.product.mapper",
|
||||
"com.ycwl.basic.profitsharing.mapper",
|
||||
"com.ycwl.basic.puzzle.mapper",
|
||||
"com.ycwl.basic.stats.mapper"
|
||||
})
|
||||
public class MybatisPlusPageConfig {
|
||||
|
||||
/* 旧版本配置
|
||||
|
||||
@@ -16,7 +16,12 @@ public enum SourceType {
|
||||
/**
|
||||
* 图片类型
|
||||
*/
|
||||
IMAGE(2, "图片");
|
||||
IMAGE(2, "图片"),
|
||||
|
||||
/**
|
||||
* AI微单类型
|
||||
*/
|
||||
AI_CAM(3, "AI微单");
|
||||
|
||||
private final int code;
|
||||
private final String description;
|
||||
@@ -68,4 +73,14 @@ public enum SourceType {
|
||||
public static boolean isImage(Integer code) {
|
||||
return code != null && code == IMAGE.code;
|
||||
}
|
||||
|
||||
/**
|
||||
* 判断给定的代码是否为AI微单类型
|
||||
*
|
||||
* @param code 类型代码
|
||||
* @return true-是AI微单,false-不是AI微单
|
||||
*/
|
||||
public static boolean isAiCam(Integer code) {
|
||||
return code != null && code == AI_CAM.code;
|
||||
}
|
||||
}
|
||||
|
||||
@@ -4,6 +4,14 @@ import cn.hutool.http.HttpUtil;
|
||||
import com.ycwl.basic.annotation.IgnoreToken;
|
||||
import com.ycwl.basic.image.enhancer.adapter.BceImageEnhancer;
|
||||
import com.ycwl.basic.image.enhancer.entity.BceEnhancerConfig;
|
||||
import com.ycwl.basic.image.pipeline.core.PhotoProcessContext;
|
||||
import com.ycwl.basic.image.pipeline.stages.DownloadStage;
|
||||
import com.ycwl.basic.image.pipeline.stages.ImageEnhanceStage;
|
||||
import com.ycwl.basic.image.pipeline.stages.ImageSRStage;
|
||||
import com.ycwl.basic.image.pipeline.stages.SourcePhotoUpdateStage;
|
||||
import com.ycwl.basic.image.pipeline.stages.CleanupStage;
|
||||
import com.ycwl.basic.pipeline.core.Pipeline;
|
||||
import com.ycwl.basic.pipeline.core.PipelineBuilder;
|
||||
import com.ycwl.basic.mapper.AioDeviceMapper;
|
||||
import com.ycwl.basic.mapper.MemberMapper;
|
||||
import com.ycwl.basic.model.aio.entity.AioDeviceBannerEntity;
|
||||
@@ -41,7 +49,9 @@ import org.springframework.web.multipart.MultipartFile;
|
||||
|
||||
import java.io.File;
|
||||
import java.util.Date;
|
||||
import java.util.HashMap;
|
||||
import java.util.List;
|
||||
import java.util.Map;
|
||||
import java.util.concurrent.TimeUnit;
|
||||
|
||||
@Slf4j
|
||||
@@ -129,27 +139,38 @@ public class AioDeviceController {
|
||||
redisTemplate.opsForValue().set("aio:faceId:"+resp.getFaceId().toString()+":pass", "1", 1, TimeUnit.DAYS);
|
||||
return;
|
||||
}
|
||||
log.info("超分开始!");
|
||||
log.info("超分开始!共{}张图片待处理", sourcePhotoList.size());
|
||||
|
||||
sourcePhotoList.forEach(photo -> {
|
||||
if (StringUtils.contains(photo.getUrl(), "_q_")) {
|
||||
log.debug("跳过已增强的图片: {}", photo.getUrl());
|
||||
return;
|
||||
}
|
||||
|
||||
try {
|
||||
File dstFile = new File(photo.getGoodsId()+".jpg");
|
||||
long fileSize = HttpUtil.downloadFile(photo.getUrl(), dstFile);
|
||||
log.info("超分开始:{}", fileSize);
|
||||
BceImageEnhancer enhancer = getEnhancer();
|
||||
MultipartFile enhancedFile = enhancer.enhance(dstFile.getName());
|
||||
log.info("超分结束:{}", photo.getUrl());
|
||||
String url = sourceService.uploadAndUpdateUrl(photo.getGoodsId(), enhancedFile);
|
||||
log.info("上传结束:->{}", url);
|
||||
// 创建超分Pipeline
|
||||
Pipeline<PhotoProcessContext> superResolutionPipeline = createSuperResolutionPipeline(photo.getGoodsId());
|
||||
|
||||
// 使用静态工厂方法创建Context
|
||||
PhotoProcessContext context = PhotoProcessContext.forSuperResolution(
|
||||
photo.getGoodsId(), photo.getUrl(), photo.getScenicId()
|
||||
);
|
||||
|
||||
// 启用图像增强和超分的Stage
|
||||
context.enableStage("image_enhance");
|
||||
context.enableStage("image_sr");
|
||||
|
||||
// 执行Pipeline
|
||||
boolean success = superResolutionPipeline.execute(context);
|
||||
|
||||
if (success) {
|
||||
log.info("超分成功: {} -> {}", photo.getUrl(), context.getResultUrl());
|
||||
} else {
|
||||
log.error("超分失败: {}", photo.getGoodsId());
|
||||
}
|
||||
|
||||
} catch (Exception e) {
|
||||
log.error("超分失败:{}", photo.getGoodsId(), e);
|
||||
} finally {
|
||||
File _file = new File(photo.getGoodsId()+".jpg");
|
||||
if (_file.exists()) {
|
||||
_file.delete();
|
||||
}
|
||||
}
|
||||
});
|
||||
redisTemplate.opsForValue().set("aio:faceId:"+sourcePhotoList.getFirst().getFaceId().toString()+":pass", "1", 1, TimeUnit.DAYS);
|
||||
@@ -206,6 +227,28 @@ public class AioDeviceController {
|
||||
return ApiResponse.success(orderService.queryOrder(orderId));
|
||||
}
|
||||
|
||||
/**
|
||||
* 创建源图片超分辨率增强Pipeline
|
||||
*
|
||||
* @param sourceId 源图片ID
|
||||
* @return 超分Pipeline
|
||||
*/
|
||||
private Pipeline<PhotoProcessContext> createSuperResolutionPipeline(Long sourceId) {
|
||||
// 创建带有百度云配置的ImageEnhanceStage
|
||||
BceEnhancerConfig config = new BceEnhancerConfig();
|
||||
config.setQps(1);
|
||||
config.setAppId("119554288");
|
||||
config.setApiKey("OX6QoijgKio3eVtA0PiUVf7f");
|
||||
config.setSecretKey("dYatXReVriPeiktTjUblhfubpcmYfuMk");
|
||||
|
||||
return new PipelineBuilder<PhotoProcessContext>("SourcePhotoSuperResolutionPipeline")
|
||||
.addStage(new DownloadStage()) // 1. 下载图片
|
||||
.addStage(new ImageEnhanceStage(config)).addStage(new ImageSRStage(config)) // 2. 图像增强(超分)
|
||||
.addStage(new SourcePhotoUpdateStage(sourceService, sourceId)) // 3. 上传并更新数据库
|
||||
.addStage(new CleanupStage()) // 4. 清理临时文件
|
||||
.build();
|
||||
}
|
||||
|
||||
private BceImageEnhancer getEnhancer() {
|
||||
BceImageEnhancer enhancer = new BceImageEnhancer();
|
||||
BceEnhancerConfig config = new BceEnhancerConfig();
|
||||
|
||||
@@ -0,0 +1,81 @@
|
||||
package com.ycwl.basic.controller.mobile;
|
||||
|
||||
import com.ycwl.basic.model.jwt.JwtInfo;
|
||||
import com.ycwl.basic.model.mobile.face.FaceRecognizeResp;
|
||||
import com.ycwl.basic.model.mobile.goods.GoodsDetailVO;
|
||||
import com.ycwl.basic.service.mobile.AppAiCamService;
|
||||
import com.ycwl.basic.utils.ApiResponse;
|
||||
import com.ycwl.basic.utils.JwtTokenUtil;
|
||||
import lombok.RequiredArgsConstructor;
|
||||
import lombok.extern.slf4j.Slf4j;
|
||||
import org.springframework.web.bind.annotation.*;
|
||||
|
||||
import java.util.List;
|
||||
|
||||
/**
|
||||
* AI相机相关接口
|
||||
*/
|
||||
@Slf4j
|
||||
@RestController
|
||||
@RequestMapping("/api/mobile/ai_cam/v1")
|
||||
@RequiredArgsConstructor
|
||||
public class AppAiCamController {
|
||||
|
||||
private final AppAiCamService appAiCamService;
|
||||
|
||||
/**
|
||||
* 根据faceId获取AI相机识别到的商品列表
|
||||
* @param faceId 人脸ID
|
||||
* @return 商品详情列表
|
||||
*/
|
||||
@GetMapping("/{faceId}/content")
|
||||
public ApiResponse<List<GoodsDetailVO>> getAiCamGoods(@PathVariable Long faceId) {
|
||||
try {
|
||||
List<GoodsDetailVO> goods = appAiCamService.getAiCamGoodsByFaceId(faceId);
|
||||
return ApiResponse.success(goods);
|
||||
} catch (Exception e) {
|
||||
log.error("获取AI相机商品失败: faceId={}", faceId, e);
|
||||
return ApiResponse.fail("获取商品列表失败");
|
||||
}
|
||||
}
|
||||
|
||||
/**
|
||||
* 批量添加会员与source的关联关系
|
||||
* @param faceId 人脸ID
|
||||
* @param sourceIds source ID列表
|
||||
* @return 添加结果
|
||||
*/
|
||||
@PostMapping("/{faceId}/relations")
|
||||
public ApiResponse<String> addMemberSourceRelations(
|
||||
@PathVariable Long faceId,
|
||||
@RequestBody List<Long> sourceIds
|
||||
) {
|
||||
try {
|
||||
int count = appAiCamService.addMemberSourceRelations(faceId, sourceIds);
|
||||
return ApiResponse.success("成功添加" + count + "条关联记录");
|
||||
} catch (IllegalArgumentException e) {
|
||||
log.warn("添加关联失败: faceId={}, error={}", faceId, e.getMessage());
|
||||
return ApiResponse.fail(e.getMessage());
|
||||
} catch (Exception e) {
|
||||
log.error("添加关联失败: faceId={}", faceId, e);
|
||||
return ApiResponse.fail("添加关联失败");
|
||||
}
|
||||
}
|
||||
|
||||
/**
|
||||
* 使用人脸样本创建或获取Face记录
|
||||
* @param faceSampleId 人脸样本ID
|
||||
* @return 人脸识别响应
|
||||
*/
|
||||
@GetMapping("/useSample/{faceSampleId}")
|
||||
public ApiResponse<FaceRecognizeResp> useSample(@PathVariable Long faceSampleId) {
|
||||
try {
|
||||
JwtInfo worker = JwtTokenUtil.getWorker();
|
||||
FaceRecognizeResp resp = appAiCamService.useSample(worker.getUserId(), faceSampleId);
|
||||
return ApiResponse.success(resp);
|
||||
} catch (Exception e) {
|
||||
log.error("使用人脸样本失败: faceSampleId={}", faceSampleId, e);
|
||||
return ApiResponse.fail("使用人脸样本失败: " + e.getMessage());
|
||||
}
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,98 @@
|
||||
package com.ycwl.basic.controller.mobile;
|
||||
|
||||
import com.ycwl.basic.model.jwt.JwtInfo;
|
||||
import com.ycwl.basic.model.mobile.chat.*;
|
||||
import com.ycwl.basic.service.mobile.FaceChatService;
|
||||
import com.ycwl.basic.utils.ApiResponse;
|
||||
import com.ycwl.basic.utils.JwtTokenUtil;
|
||||
import lombok.RequiredArgsConstructor;
|
||||
import lombok.extern.slf4j.Slf4j;
|
||||
import org.springframework.http.MediaType;
|
||||
import org.springframework.web.bind.annotation.*;
|
||||
import org.springframework.web.servlet.mvc.method.annotation.ResponseBodyEmitter;
|
||||
|
||||
import java.util.concurrent.CompletableFuture;
|
||||
|
||||
/**
|
||||
* 小程序人脸智能聊天接口。
|
||||
*/
|
||||
@Slf4j
|
||||
@RestController
|
||||
@RequestMapping("/api/mobile/chat/v1")
|
||||
@RequiredArgsConstructor
|
||||
public class AppChatController {
|
||||
|
||||
private final FaceChatService faceChatService;
|
||||
|
||||
/**
|
||||
* 获取或创建会话(同一人脸只保留一条)。
|
||||
*/
|
||||
@PostMapping("/faces/{faceId}/conversation")
|
||||
public ApiResponse<ChatConversationVO> createConversation(@PathVariable Long faceId) {
|
||||
JwtInfo worker = JwtTokenUtil.getWorker();
|
||||
ChatConversationVO vo = faceChatService.getOrCreateConversation(faceId, worker.getUserId());
|
||||
return ApiResponse.success(vo);
|
||||
}
|
||||
|
||||
/**
|
||||
* 同步发送消息,适用于短回复或前端自行轮询。
|
||||
*/
|
||||
@PostMapping("/conversations/{conversationId}/messages")
|
||||
public ApiResponse<ChatSendMessageResp> sendMessage(@PathVariable Long conversationId,
|
||||
@RequestBody ChatSendMessageReq req) {
|
||||
JwtInfo worker = JwtTokenUtil.getWorker();
|
||||
ChatSendMessageResp resp = faceChatService.sendMessage(conversationId, worker.getUserId(),
|
||||
req.getContent(), req.getTraceId());
|
||||
return ApiResponse.success(resp);
|
||||
}
|
||||
|
||||
/**
|
||||
* 流式返回,使用 HTTP chunked。小程序侧用 wx.request 的 onChunkReceived 消费。
|
||||
*/
|
||||
@PostMapping(value = "/conversations/{conversationId}/messages/stream", produces = "text/plain;charset=UTF-8")
|
||||
public ResponseBodyEmitter streamMessage(@PathVariable Long conversationId,
|
||||
@RequestBody ChatSendMessageReq req) {
|
||||
JwtInfo worker = JwtTokenUtil.getWorker();
|
||||
ResponseBodyEmitter emitter = new ResponseBodyEmitter(30_000L);
|
||||
CompletableFuture.runAsync(() -> {
|
||||
try {
|
||||
faceChatService.sendMessageStream(
|
||||
conversationId,
|
||||
worker.getUserId(),
|
||||
req.getContent(),
|
||||
req.getTraceId(),
|
||||
chunk -> {
|
||||
try {
|
||||
emitter.send(chunk, new MediaType("text", "plain", java.nio.charset.StandardCharsets.UTF_8));
|
||||
} catch (Exception e) {
|
||||
emitter.completeWithError(e);
|
||||
}
|
||||
});
|
||||
emitter.complete();
|
||||
} catch (Exception e) {
|
||||
log.error("streamMessage error", e);
|
||||
emitter.completeWithError(e);
|
||||
}
|
||||
});
|
||||
return emitter;
|
||||
}
|
||||
|
||||
/**
|
||||
* 查询历史消息,cursor 为最后一条 seq,limit 为条数。
|
||||
*/
|
||||
@GetMapping("/conversations/{conversationId}/messages")
|
||||
public ApiResponse<ChatMessagePageResp> listMessages(@PathVariable Long conversationId,
|
||||
@RequestParam(value = "cursor", required = false) Integer cursor,
|
||||
@RequestParam(value = "limit", required = false) Integer limit) {
|
||||
JwtInfo worker = JwtTokenUtil.getWorker();
|
||||
ChatMessagePageResp resp = faceChatService.listMessages(conversationId, cursor, limit, worker.getUserId());
|
||||
return ApiResponse.success(resp);
|
||||
}
|
||||
|
||||
@PostMapping("/conversations/{conversationId}/close")
|
||||
public ApiResponse<String> closeConversation(@PathVariable Long conversationId) {
|
||||
JwtInfo worker = JwtTokenUtil.getWorker();
|
||||
faceChatService.closeConversation(conversationId, worker.getUserId());
|
||||
return ApiResponse.success("OK");
|
||||
}
|
||||
}
|
||||
@@ -29,6 +29,7 @@ import com.ycwl.basic.order.dto.PaymentParamsResponse;
|
||||
import com.ycwl.basic.order.dto.PaymentCallbackResponse;
|
||||
import lombok.RequiredArgsConstructor;
|
||||
import lombok.extern.slf4j.Slf4j;
|
||||
import org.springframework.data.redis.core.RedisTemplate;
|
||||
import org.springframework.web.bind.annotation.*;
|
||||
|
||||
import jakarta.servlet.http.HttpServletRequest;
|
||||
@@ -55,6 +56,7 @@ public class AppOrderV2Controller {
|
||||
private final VideoTaskRepository videoTaskRepository;
|
||||
private final TemplateRepository templateRepository;
|
||||
private final VideoRepository videoRepository;
|
||||
private final RedisTemplate<String, Object> redisTemplate;
|
||||
|
||||
/**
|
||||
* 移动端价格计算
|
||||
@@ -86,7 +88,7 @@ public class AppOrderV2Controller {
|
||||
TaskEntity task = videoTaskRepository.getTaskById(video.getTaskId());
|
||||
request.setFaceId(task.getFaceId());
|
||||
}
|
||||
case RECORDING_SET, PHOTO_SET -> request.setFaceId(Long.valueOf(productItem.getProductId()));
|
||||
case RECORDING_SET, PHOTO_SET, AI_CAM_PHOTO_SET -> request.setFaceId(Long.valueOf(productItem.getProductId()));
|
||||
}
|
||||
}
|
||||
|
||||
@@ -119,6 +121,14 @@ public class AppOrderV2Controller {
|
||||
Integer count = sourceMapper.countUser(sourceReqQuery);
|
||||
product.setQuantity(count);
|
||||
break;
|
||||
case AI_CAM_PHOTO_SET:
|
||||
SourceReqQuery aiPhotoSetReqQuery = new SourceReqQuery();
|
||||
aiPhotoSetReqQuery.setMemberId(currentUserId);
|
||||
aiPhotoSetReqQuery.setType(13);
|
||||
aiPhotoSetReqQuery.setFaceId(face.getId());
|
||||
Integer _count = sourceMapper.countUser(aiPhotoSetReqQuery);
|
||||
product.setQuantity(_count);
|
||||
break;
|
||||
default:
|
||||
log.warn("未知的商品类型,跳过重复购买检查: productType={}", product.getProductType());
|
||||
break;
|
||||
@@ -341,4 +351,9 @@ public class AppOrderV2Controller {
|
||||
return "FAIL";
|
||||
}
|
||||
}
|
||||
|
||||
@GetMapping("/downloadable/{orderId}")
|
||||
public ApiResponse<Boolean> getDownloadableOrder(@PathVariable("orderId") Long orderId) {
|
||||
return ApiResponse.success(!redisTemplate.hasKey("order_content_not_downloadable_" + orderId));
|
||||
}
|
||||
}
|
||||
|
||||
@@ -205,26 +205,31 @@ public class AppPuzzleController {
|
||||
|
||||
// 设置模板ID
|
||||
vo.setTemplateId(record.getTemplateId());
|
||||
IsBuyRespVO isBuyRespVO = orderBiz.isBuy(face.getScenicId(), face.getMemberId(), face.getId(), 5, record.getTemplateId());
|
||||
if (isBuyRespVO.isBuy()) {
|
||||
IsBuyRespVO isBuyScenic = orderBiz.isBuy(face.getScenicId(), face.getMemberId(), face.getId(), 5, face.getScenicId());
|
||||
if (isBuyScenic.isBuy()) {
|
||||
vo.setIsBuy(1);
|
||||
} else {
|
||||
vo.setIsBuy(0);
|
||||
PriceCalculationRequest calculationRequest = new PriceCalculationRequest();
|
||||
ProductItem productItem = new ProductItem();
|
||||
productItem.setProductType(ProductType.PHOTO_LOG);
|
||||
productItem.setProductId(record.getTemplateId().toString());
|
||||
productItem.setPurchaseCount(1);
|
||||
productItem.setScenicId(face.getScenicId().toString());
|
||||
calculationRequest.setProducts(Collections.singletonList(productItem));
|
||||
calculationRequest.setUserId(face.getMemberId());
|
||||
calculationRequest.setFaceId(record.getFaceId());
|
||||
calculationRequest.setPreviewOnly(true); // 仅查询价格,不实际使用优惠
|
||||
PriceCalculationResult calculationResult = iPriceCalculationService.calculatePrice(calculationRequest);
|
||||
if (calculationResult.getFinalAmount().compareTo(BigDecimal.ZERO) > 0) {
|
||||
vo.setFreeCount(0);
|
||||
IsBuyRespVO isBuyRespVO = orderBiz.isBuy(face.getScenicId(), face.getMemberId(), face.getId(), 5, record.getTemplateId());
|
||||
if (isBuyRespVO.isBuy()) {
|
||||
vo.setIsBuy(1);
|
||||
} else {
|
||||
vo.setFreeCount(1);
|
||||
vo.setIsBuy(0);
|
||||
PriceCalculationRequest calculationRequest = new PriceCalculationRequest();
|
||||
ProductItem productItem = new ProductItem();
|
||||
productItem.setProductType(ProductType.PHOTO_LOG);
|
||||
productItem.setProductId(record.getTemplateId().toString());
|
||||
productItem.setPurchaseCount(1);
|
||||
productItem.setScenicId(face.getScenicId().toString());
|
||||
calculationRequest.setProducts(Collections.singletonList(productItem));
|
||||
calculationRequest.setUserId(face.getMemberId());
|
||||
calculationRequest.setFaceId(record.getFaceId());
|
||||
calculationRequest.setPreviewOnly(true); // 仅查询价格,不实际使用优惠
|
||||
PriceCalculationResult calculationResult = iPriceCalculationService.calculatePrice(calculationRequest);
|
||||
if (calculationResult.getFinalAmount().compareTo(BigDecimal.ZERO) > 0) {
|
||||
vo.setFreeCount(0);
|
||||
} else {
|
||||
vo.setFreeCount(1);
|
||||
}
|
||||
}
|
||||
}
|
||||
return vo;
|
||||
|
||||
@@ -88,6 +88,8 @@ public class AppScenicController {
|
||||
resp.setPrintEnableManual(scenicConfig.getBoolean("print_enable_manual", true));
|
||||
resp.setSceneMode(scenicConfig.getInteger("scene_mode", 0));
|
||||
resp.setPrintEnable(scenicConfig.getBoolean("print_enable", false));
|
||||
resp.setShowMyPagePaid(scenicConfig.getBoolean("show_my_page_paid", true));
|
||||
resp.setShowMyPageUnpaid(scenicConfig.getBoolean("show_my_page_unpaid", true));
|
||||
return ApiResponse.success(resp);
|
||||
}
|
||||
|
||||
|
||||
@@ -55,7 +55,7 @@ public class AppTaskController {
|
||||
|
||||
@PostMapping("/submit")
|
||||
public ApiResponse<String> submitVideoTask(@RequestBody VideoTaskReq videoTaskReq) {
|
||||
taskService.createTaskByFaceIdAndTemplateId(videoTaskReq.getFaceId(),videoTaskReq.getTemplateId(),0);
|
||||
taskService.createTaskByFaceIdAndTemplateId(videoTaskReq.getFaceId(),videoTaskReq.getTemplateId(),false);
|
||||
return ApiResponse.success("成功");
|
||||
}
|
||||
}
|
||||
|
||||
@@ -0,0 +1,50 @@
|
||||
package com.ycwl.basic.controller.monitor;
|
||||
|
||||
import com.ycwl.basic.integration.kafka.scheduler.AccountFaceSchedulerManager;
|
||||
import com.ycwl.basic.utils.ApiResponse;
|
||||
import lombok.RequiredArgsConstructor;
|
||||
import org.springframework.web.bind.annotation.GetMapping;
|
||||
import org.springframework.web.bind.annotation.RequestMapping;
|
||||
import org.springframework.web.bind.annotation.RestController;
|
||||
|
||||
import java.util.Map;
|
||||
|
||||
/**
|
||||
* 人脸识别监控接口
|
||||
* 提供调度器状态查询功能
|
||||
*/
|
||||
@RestController
|
||||
@RequestMapping("/api/monitor/face-recognition")
|
||||
@RequiredArgsConstructor
|
||||
public class FaceRecognitionMonitorController {
|
||||
|
||||
private final AccountFaceSchedulerManager schedulerManager;
|
||||
|
||||
/**
|
||||
* 获取所有账号的调度器统计信息
|
||||
* <p>
|
||||
* 示例返回:
|
||||
* {
|
||||
* "LTAI5xxx": {
|
||||
* "accountKey": "LTAI5xxx",
|
||||
* "cloudType": "ALI",
|
||||
* "activeThreads": 3,
|
||||
* "executorQueueSize": 12,
|
||||
* "schedulerQueueSize": 45
|
||||
* },
|
||||
* "245xxx": {
|
||||
* "accountKey": "245xxx",
|
||||
* "cloudType": "BAIDU",
|
||||
* "activeThreads": 8,
|
||||
* "executorQueueSize": 5,
|
||||
* "schedulerQueueSize": 20
|
||||
* }
|
||||
* }
|
||||
*
|
||||
* @return 所有账号的调度器状态
|
||||
*/
|
||||
@GetMapping("/schedulers")
|
||||
public ApiResponse<Map<String, AccountFaceSchedulerManager.AccountSchedulerStats>> getAllSchedulerStats() {
|
||||
return ApiResponse.success(schedulerManager.getAllStats());
|
||||
}
|
||||
}
|
||||
@@ -53,9 +53,9 @@ public class DeviceV2Controller {
|
||||
if (pageSize > 100) {
|
||||
pageSize = 100;
|
||||
}
|
||||
|
||||
|
||||
try {
|
||||
PageResponse<DeviceV2DTO> response = deviceIntegrationService.listDevices(page, pageSize, name, no, type, isActive, scenicId);
|
||||
PageResponse<DeviceV2DTO> response = deviceIntegrationService.listDevices(page, pageSize, name, no, type, isActive, scenicId, null);
|
||||
return ApiResponse.success(response);
|
||||
} catch (Exception e) {
|
||||
log.error("分页查询设备核心信息列表失败", e);
|
||||
@@ -380,7 +380,7 @@ public class DeviceV2Controller {
|
||||
@RequestParam(defaultValue = "10") Integer pageSize) {
|
||||
log.info("获取景区所有设备列表, scenicId: {}, page: {}, pageSize: {}", scenicId, page, pageSize);
|
||||
try {
|
||||
PageResponse<DeviceV2DTO> response = deviceIntegrationService.listDevices(page, pageSize, name, no, type, null, scenicId);
|
||||
PageResponse<DeviceV2DTO> response = deviceIntegrationService.listDevices(page, pageSize, name, no, type, null, scenicId, null);
|
||||
return ApiResponse.success(response);
|
||||
} catch (Exception e) {
|
||||
log.error("获取景区所有设备列表失败, scenicId: {}", scenicId, e);
|
||||
|
||||
@@ -2,6 +2,7 @@ package com.ycwl.basic.controller.pc;
|
||||
|
||||
import com.ycwl.basic.biz.PriceBiz;
|
||||
import com.ycwl.basic.model.pc.price.resp.GoodsListRespVO;
|
||||
import com.ycwl.basic.model.pc.price.resp.SimpleGoodsRespVO;
|
||||
import com.ycwl.basic.utils.ApiResponse;
|
||||
import org.springframework.beans.factory.annotation.Autowired;
|
||||
import org.springframework.web.bind.annotation.*;
|
||||
@@ -16,8 +17,10 @@ public class PriceConfigController {
|
||||
private PriceBiz priceBiz;
|
||||
|
||||
@GetMapping("/goodsList")
|
||||
public ApiResponse<List<GoodsListRespVO>> goodsList(@RequestParam Long scenicId) {
|
||||
return ApiResponse.success(priceBiz.listGoodsByScenic(scenicId));
|
||||
public ApiResponse<List<SimpleGoodsRespVO>> goodsList(
|
||||
@RequestParam Long scenicId,
|
||||
@RequestParam(required = false) String productType) {
|
||||
return ApiResponse.success(priceBiz.listSimpleGoodsByScenic(scenicId, productType));
|
||||
}
|
||||
|
||||
}
|
||||
@@ -71,7 +71,7 @@ public class PrinterController {
|
||||
// 重新打印(将状态设置为0-未开始,并更新打印机名称)
|
||||
@PostMapping("/task/reprint/{id}")
|
||||
public ApiResponse<Integer> reprint(@PathVariable("id") Integer id, @RequestBody ReprintRequest request) {
|
||||
int result = printTaskMapper.updateStatusAndPrinter(id, 0, request.getPrinterName());
|
||||
int result = printerService.handleReprint(id, request);
|
||||
return ApiResponse.success(result);
|
||||
}
|
||||
|
||||
|
||||
@@ -60,9 +60,9 @@ public class ScenicV2Controller {
|
||||
if (pageSize > 100) {
|
||||
pageSize = 100;
|
||||
}
|
||||
|
||||
|
||||
try {
|
||||
PageResponse<ScenicV2DTO> response = scenicIntegrationService.listScenics(page, pageSize, status, name);
|
||||
PageResponse<ScenicV2DTO> response = scenicIntegrationService.listScenics(page, pageSize, status, name, null);
|
||||
return ApiResponse.success(response);
|
||||
} catch (Exception e) {
|
||||
log.error("分页查询景区核心信息列表失败", e);
|
||||
@@ -156,7 +156,7 @@ public class ScenicV2Controller {
|
||||
log.info("查询景区列表, status: {}", status);
|
||||
try {
|
||||
// 默认查询1000条数据,第1页
|
||||
PageResponse<ScenicV2DTO> scenics = scenicIntegrationService.listScenics(1, 1000, status, null);
|
||||
PageResponse<ScenicV2DTO> scenics = scenicIntegrationService.listScenics(1, 1000, status, null, null);
|
||||
return ApiResponse.success(scenics);
|
||||
} catch (Exception e) {
|
||||
log.error("查询景区列表失败, status: {}", status, e);
|
||||
|
||||
@@ -1,34 +1,56 @@
|
||||
package com.ycwl.basic.controller.printer;
|
||||
|
||||
|
||||
import cn.hutool.core.date.DateUtil;
|
||||
import com.ycwl.basic.facebody.adapter.IFaceBodyAdapter;
|
||||
import com.ycwl.basic.mapper.FaceMapper;
|
||||
import com.ycwl.basic.model.pc.device.entity.DeviceEntity;
|
||||
import com.ycwl.basic.model.pc.face.entity.FaceEntity;
|
||||
import com.ycwl.basic.model.pc.source.entity.MemberSourceEntity;
|
||||
import com.ycwl.basic.model.task.resp.SearchFaceRespVo;
|
||||
import com.ycwl.basic.repository.MemberRelationRepository;
|
||||
import com.ycwl.basic.repository.SourceRepository;
|
||||
import com.ycwl.basic.service.printer.PrinterService;
|
||||
import com.ycwl.basic.service.task.TaskFaceService;
|
||||
import com.ycwl.basic.storage.adapters.IStorageAdapter;
|
||||
import com.ycwl.basic.annotation.IgnoreToken;
|
||||
import com.ycwl.basic.storage.StorageFactory;
|
||||
import com.ycwl.basic.integration.device.dto.device.DeviceV2DTO;
|
||||
import com.ycwl.basic.integration.device.service.DeviceIntegrationService;
|
||||
import com.ycwl.basic.integration.scenic.dto.scenic.ScenicV2DTO;
|
||||
import com.ycwl.basic.mapper.SourceMapper;
|
||||
import com.ycwl.basic.model.pc.faceSample.entity.FaceSampleEntity;
|
||||
import com.ycwl.basic.model.pc.mp.MpConfigEntity;
|
||||
import com.ycwl.basic.model.pc.scenic.req.ScenicReqQuery;
|
||||
import com.ycwl.basic.model.pc.source.entity.SourceEntity;
|
||||
import com.ycwl.basic.model.printer.FaceRecognizeWithSourcesResp;
|
||||
import com.ycwl.basic.repository.DeviceRepository;
|
||||
import com.ycwl.basic.repository.FaceRepository;
|
||||
import com.ycwl.basic.repository.ScenicRepository;
|
||||
import com.ycwl.basic.service.pc.FaceService;
|
||||
import com.ycwl.basic.service.pc.ScenicService;
|
||||
import com.ycwl.basic.storage.utils.StorageUtil;
|
||||
import com.ycwl.basic.utils.ApiResponse;
|
||||
import com.ycwl.basic.utils.SnowFlakeUtil;
|
||||
import com.ycwl.basic.utils.WxMpUtil;
|
||||
import jakarta.websocket.server.PathParam;
|
||||
import jakarta.servlet.http.HttpServletResponse;
|
||||
import lombok.RequiredArgsConstructor;
|
||||
import org.springframework.web.bind.annotation.GetMapping;
|
||||
import org.springframework.web.bind.annotation.PathVariable;
|
||||
import org.springframework.web.bind.annotation.PostMapping;
|
||||
import org.springframework.web.bind.annotation.RequestMapping;
|
||||
import org.springframework.web.bind.annotation.RequestParam;
|
||||
import org.springframework.web.bind.annotation.RestController;
|
||||
import org.springframework.web.multipart.MultipartFile;
|
||||
|
||||
import java.io.File;
|
||||
import java.io.FileInputStream;
|
||||
import java.io.IOException;
|
||||
import java.io.OutputStream;
|
||||
import jakarta.servlet.http.HttpServletResponse;
|
||||
|
||||
import java.util.ArrayList;
|
||||
import java.util.Date;
|
||||
import java.util.List;
|
||||
import java.util.UUID;
|
||||
|
||||
import static com.ycwl.basic.constant.StorageConstant.USER_FACE;
|
||||
|
||||
@IgnoreToken
|
||||
// 打印机大屏对接接口
|
||||
@@ -40,6 +62,14 @@ public class PrinterTvController {
|
||||
private final DeviceRepository deviceRepository;
|
||||
private final ScenicRepository scenicRepository;
|
||||
private final FaceRepository faceRepository;
|
||||
private final TaskFaceService faceService;
|
||||
private final FaceService pcFaceService;
|
||||
private final ScenicService scenicService;
|
||||
private final SourceMapper sourceMapper;
|
||||
private final FaceMapper faceMapper;
|
||||
private final MemberRelationRepository memberRelationRepository;
|
||||
private final SourceRepository sourceRepository;
|
||||
private final PrinterService printerService;
|
||||
|
||||
/**
|
||||
* 获取景区列表
|
||||
@@ -70,10 +100,20 @@ public class PrinterTvController {
|
||||
@GetMapping("/{sampleId}/qrcode")
|
||||
public void getQrcode(@PathVariable("sampleId") Long sampleId, HttpServletResponse response) throws Exception {
|
||||
File qrcode = new File("qrcode_"+sampleId+".jpg");
|
||||
FaceSampleEntity faceSample = faceRepository.getFaceSample(sampleId);
|
||||
if (faceSample == null) {
|
||||
response.setStatus(404);
|
||||
return;
|
||||
}
|
||||
String targetPath = "pages/printer/from_sample";
|
||||
DeviceV2DTO device = deviceRepository.getDeviceBasic(faceSample.getDeviceId());
|
||||
if (device.getType().equals("AI_CAM")) {
|
||||
// AI_CAM,需要修改path
|
||||
targetPath = "pages/ai-cam/from_sample";
|
||||
}
|
||||
try {
|
||||
FaceSampleEntity faceSample = faceRepository.getFaceSample(sampleId);
|
||||
MpConfigEntity scenicMpConfig = scenicRepository.getScenicMpConfig(faceSample.getScenicId());
|
||||
WxMpUtil.generateUnlimitedWXAQRCode(scenicMpConfig.getAppId(), scenicMpConfig.getAppSecret(), "pages/printer/from_sample", sampleId.toString(), qrcode);
|
||||
WxMpUtil.generateUnlimitedWXAQRCode(scenicMpConfig.getAppId(), scenicMpConfig.getAppSecret(), targetPath, sampleId.toString(), qrcode);
|
||||
|
||||
// 设置响应头
|
||||
response.setContentType("image/jpeg");
|
||||
@@ -96,4 +136,142 @@ public class PrinterTvController {
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
/**
|
||||
* 获取人脸绑定二维码
|
||||
* 生成小程序二维码,用于绑定人脸到用户账号
|
||||
*
|
||||
* @param faceId 人脸ID
|
||||
* @param response HTTP响应
|
||||
*/
|
||||
@GetMapping("/face/{faceId}/qrcode")
|
||||
public void getFaceQrcode(@PathVariable("faceId") Long faceId, HttpServletResponse response) throws Exception {
|
||||
File qrcode = new File("qrcode_face_" + faceId + ".jpg");
|
||||
try {
|
||||
FaceEntity face = faceRepository.getFace(faceId);
|
||||
if (face == null) {
|
||||
response.setStatus(404);
|
||||
return;
|
||||
}
|
||||
MpConfigEntity scenicMpConfig = scenicRepository.getScenicMpConfig(face.getScenicId());
|
||||
if (scenicMpConfig == null) {
|
||||
response.setStatus(500);
|
||||
return;
|
||||
}
|
||||
WxMpUtil.generateUnlimitedWXAQRCode(
|
||||
scenicMpConfig.getAppId(),
|
||||
scenicMpConfig.getAppSecret(),
|
||||
"pages/videoSynthesis/bind_face",
|
||||
faceId.toString(),
|
||||
qrcode
|
||||
);
|
||||
|
||||
// 设置响应头
|
||||
response.setContentType("image/jpeg");
|
||||
response.setHeader("Content-Disposition", "inline; filename=\"" + qrcode.getName() + "\"");
|
||||
|
||||
// 将二维码文件写入响应输出流
|
||||
try (FileInputStream fis = new FileInputStream(qrcode);
|
||||
OutputStream os = response.getOutputStream()) {
|
||||
byte[] buffer = new byte[1024];
|
||||
int bytesRead;
|
||||
while ((bytesRead = fis.read(buffer)) != -1) {
|
||||
os.write(buffer, 0, bytesRead);
|
||||
}
|
||||
os.flush();
|
||||
}
|
||||
} finally {
|
||||
// 删除临时文件
|
||||
if (qrcode.exists()) {
|
||||
qrcode.delete();
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
/**
|
||||
* 根据人脸样本ID查询图像素材
|
||||
*
|
||||
* @param faceSampleId 人脸样本ID
|
||||
* @return type=2且face_sample_id匹配的source记录
|
||||
*/
|
||||
@GetMapping("/{faceSampleId}/source")
|
||||
public ApiResponse<SourceEntity> getSourceByFaceSampleId(@PathVariable Long faceSampleId) {
|
||||
SourceEntity source = sourceMapper.getBySampleIdAndType(faceSampleId, 2);
|
||||
if (source == null) {
|
||||
return ApiResponse.fail("未找到对应的图像素材");
|
||||
}
|
||||
return ApiResponse.success(source);
|
||||
}
|
||||
|
||||
/**
|
||||
* 打印机大屏人脸识别
|
||||
* 上传照片,在景区人脸库中搜索匹配的人脸样本,返回识别结果和匹配到的图像素材
|
||||
*
|
||||
* @param file 人脸照片文件
|
||||
* @param scenicId 景区ID
|
||||
* @return 人脸识别结果和匹配的source列表
|
||||
*/
|
||||
@PostMapping("/{scenicId}/faceRecognize")
|
||||
public ApiResponse<FaceRecognizeWithSourcesResp> faceRecognize(
|
||||
@RequestParam("file") MultipartFile file,
|
||||
@PathVariable Long scenicId) throws Exception {
|
||||
|
||||
// 1. 上传人脸照片到存储
|
||||
IStorageAdapter adapter = StorageFactory.use("faces");
|
||||
String filePath = StorageUtil.joinPath(USER_FACE, DateUtil.format(new Date(), "yyyy-MM-dd"));
|
||||
String originalFilename = file.getOriginalFilename();
|
||||
String suffix = originalFilename.split("\\.", 2)[1];
|
||||
String fileName = UUID.randomUUID() + "." + suffix;
|
||||
String faceUrl = adapter.uploadFile(file, filePath, fileName);
|
||||
|
||||
// 2. 保存人脸数据到数据库
|
||||
Long faceId = SnowFlakeUtil.getLongId();
|
||||
FaceEntity faceEntity = new FaceEntity();
|
||||
faceEntity.setId(faceId);
|
||||
faceEntity.setScenicId(scenicId);
|
||||
faceEntity.setFaceUrl(faceUrl);
|
||||
faceEntity.setCreateAt(new Date());
|
||||
faceEntity.setMemberId(0L); // 打印机大屏端没有用户ID
|
||||
faceMapper.add(faceEntity);
|
||||
|
||||
// 3. 在景区人脸库中搜索(注意:这里使用scenicId作为数据库名,搜索的是景区内的人脸样本)
|
||||
pcFaceService.matchFaceId(faceId);
|
||||
|
||||
// 4. 自动添加照片到打印列表,并获取添加成功的照片列表
|
||||
List<SourceEntity> addedSources = printerService.autoAddPhotosToPreferPrint(faceId);
|
||||
|
||||
// 5. 根据自动添加结果决定返回的sources
|
||||
List<SourceEntity> sources;
|
||||
if (addedSources != null && !addedSources.isEmpty()) {
|
||||
// 如果自动添加成功,返回添加的照片列表
|
||||
sources = addedSources;
|
||||
} else {
|
||||
// 如果自动添加为空,按原逻辑查询匹配到的图像素材(type=2)
|
||||
sources = new ArrayList<>();
|
||||
List<MemberSourceEntity> memberSourceEntities = memberRelationRepository.listSourceByFaceRelation(faceId, 2);
|
||||
for (MemberSourceEntity memberSourceEntity : memberSourceEntities) {
|
||||
SourceEntity source = sourceRepository.getSource(memberSourceEntity.getSourceId());
|
||||
if (source != null) {
|
||||
sources.add(source);
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
// 6. 构造响应
|
||||
FaceRecognizeWithSourcesResp resp = new FaceRecognizeWithSourcesResp();
|
||||
resp.setUrl(faceUrl);
|
||||
resp.setFaceId(faceId);
|
||||
resp.setScenicId(scenicId);
|
||||
resp.setSources(sources);
|
||||
// 只有当添加了照片时才返回二维码URL
|
||||
if (addedSources != null && !addedSources.isEmpty()) {
|
||||
resp.setQrcodeUrl("https://zhentuai.com/printer/v1/tv/face/" + faceId + "/qrcode");
|
||||
} else {
|
||||
resp.setQrcodeUrl(null);
|
||||
}
|
||||
|
||||
return ApiResponse.success(resp);
|
||||
}
|
||||
|
||||
|
||||
}
|
||||
|
||||
@@ -59,7 +59,7 @@ public class ZTSourceMessage {
|
||||
* 判断是否为照片
|
||||
*/
|
||||
public boolean isPhoto() {
|
||||
return sourceType != null && sourceType == 2;
|
||||
return sourceType != null && (sourceType == 2 || sourceType == 3);
|
||||
}
|
||||
|
||||
}
|
||||
@@ -0,0 +1,290 @@
|
||||
package com.ycwl.basic.face.pipeline.core;
|
||||
|
||||
import com.ycwl.basic.facebody.adapter.IFaceBodyAdapter;
|
||||
import com.ycwl.basic.face.pipeline.enums.FaceMatchingScene;
|
||||
import com.ycwl.basic.integration.common.manager.ScenicConfigManager;
|
||||
import com.ycwl.basic.model.pc.face.entity.FaceEntity;
|
||||
import com.ycwl.basic.model.pc.faceSample.entity.FaceSampleEntity;
|
||||
import com.ycwl.basic.model.pc.source.entity.MemberSourceEntity;
|
||||
import com.ycwl.basic.model.task.resp.SearchFaceRespVo;
|
||||
import com.ycwl.basic.pipeline.core.PipelineContext;
|
||||
import lombok.Getter;
|
||||
import lombok.Setter;
|
||||
|
||||
import java.util.HashMap;
|
||||
import java.util.List;
|
||||
import java.util.Map;
|
||||
|
||||
/**
|
||||
* 人脸匹配管线上下文
|
||||
* 在各个Stage之间传递状态和数据
|
||||
*/
|
||||
@Getter
|
||||
@Setter
|
||||
public class FaceMatchingContext implements PipelineContext {
|
||||
|
||||
// ==================== 核心字段(构造时必填)====================
|
||||
|
||||
/**
|
||||
* 人脸ID(必填)
|
||||
*/
|
||||
private final Long faceId;
|
||||
|
||||
/**
|
||||
* 是否新用户
|
||||
*/
|
||||
private final boolean isNew;
|
||||
|
||||
// ==================== 场景标识 ====================
|
||||
|
||||
/**
|
||||
* 场景标识
|
||||
*/
|
||||
private FaceMatchingScene scene;
|
||||
|
||||
/**
|
||||
* 手动选择的样本ID(自定义匹配场景)
|
||||
*/
|
||||
private List<Long> faceSampleIds;
|
||||
|
||||
// ==================== 中间状态 ====================
|
||||
|
||||
/**
|
||||
* 人脸实体
|
||||
*/
|
||||
private FaceEntity face;
|
||||
|
||||
/**
|
||||
* 景区配置管理器
|
||||
*/
|
||||
private ScenicConfigManager scenicConfig;
|
||||
|
||||
/**
|
||||
* 人脸识别适配器
|
||||
*/
|
||||
private IFaceBodyAdapter faceBodyAdapter;
|
||||
|
||||
/**
|
||||
* 人脸搜索结果
|
||||
*/
|
||||
private SearchFaceRespVo searchResult;
|
||||
|
||||
/**
|
||||
* 人脸样本列表(自定义匹配场景)
|
||||
*/
|
||||
private List<FaceSampleEntity> faceSamples;
|
||||
|
||||
/**
|
||||
* 匹配到的样本ID列表
|
||||
*/
|
||||
private List<Long> sampleListIds;
|
||||
|
||||
/**
|
||||
* 源文件关联列表
|
||||
*/
|
||||
private List<MemberSourceEntity> memberSourceList;
|
||||
|
||||
/**
|
||||
* 免费源文件ID列表
|
||||
*/
|
||||
private List<Long> freeSourceIds;
|
||||
|
||||
/**
|
||||
* 人脸选择后置模式配置(自定义匹配场景)
|
||||
* 0: 并集, 1: 交集, 2: 直接使用
|
||||
*/
|
||||
private Integer faceSelectPostMode;
|
||||
|
||||
// ==================== 输出结果 ====================
|
||||
|
||||
/**
|
||||
* 最终结果
|
||||
*/
|
||||
private SearchFaceRespVo finalResult;
|
||||
|
||||
// ==================== Stage配置 ====================
|
||||
|
||||
/**
|
||||
* Stage开关配置表
|
||||
* Key: stageId, Value: 是否启用
|
||||
*/
|
||||
private Map<String, Boolean> stageEnabledMap = new HashMap<>();
|
||||
|
||||
// ==================== 构造函数(私有)====================
|
||||
|
||||
private FaceMatchingContext(Builder builder) {
|
||||
this.faceId = builder.faceId;
|
||||
this.isNew = builder.isNew;
|
||||
this.scene = builder.scene;
|
||||
this.faceSampleIds = builder.faceSampleIds;
|
||||
}
|
||||
|
||||
// ==================== 静态工厂方法 ====================
|
||||
|
||||
/**
|
||||
* 获取 Builder
|
||||
*/
|
||||
public static Builder builder() {
|
||||
return new Builder();
|
||||
}
|
||||
|
||||
/**
|
||||
* 快速创建自动匹配场景Context
|
||||
*/
|
||||
public static FaceMatchingContext forAutoMatching(Long faceId, boolean isNew) {
|
||||
return FaceMatchingContext.builder()
|
||||
.faceId(faceId)
|
||||
.isNew(isNew)
|
||||
.scene(FaceMatchingScene.AUTO_MATCHING)
|
||||
.build();
|
||||
}
|
||||
|
||||
/**
|
||||
* 快速创建自定义匹配场景Context
|
||||
*/
|
||||
public static FaceMatchingContext forCustomMatching(Long faceId, List<Long> faceSampleIds) {
|
||||
return FaceMatchingContext.builder()
|
||||
.faceId(faceId)
|
||||
.isNew(false)
|
||||
.faceSampleIds(faceSampleIds)
|
||||
.scene(FaceMatchingScene.CUSTOM_MATCHING)
|
||||
.build();
|
||||
}
|
||||
|
||||
/**
|
||||
* 快速创建仅识别场景Context
|
||||
*/
|
||||
public static FaceMatchingContext forRecognitionOnly(Long faceId) {
|
||||
return FaceMatchingContext.builder()
|
||||
.faceId(faceId)
|
||||
.isNew(false)
|
||||
.scene(FaceMatchingScene.RECOGNITION_ONLY)
|
||||
.build();
|
||||
}
|
||||
|
||||
// ==================== 业务方法 ====================
|
||||
|
||||
/**
|
||||
* 判断指定Stage是否启用
|
||||
*
|
||||
* @param stageId Stage唯一标识
|
||||
* @param defaultEnabled 默认值(当配置未指定时使用)
|
||||
* @return true-启用, false-禁用
|
||||
*/
|
||||
@Override
|
||||
public boolean isStageEnabled(String stageId, boolean defaultEnabled) {
|
||||
return stageEnabledMap.getOrDefault(stageId, defaultEnabled);
|
||||
}
|
||||
|
||||
/**
|
||||
* 判断指定Stage是否启用(默认为false)
|
||||
*
|
||||
* @param stageId Stage唯一标识
|
||||
* @return true-启用, false-禁用
|
||||
*/
|
||||
@Override
|
||||
public boolean isStageEnabled(String stageId) {
|
||||
return stageEnabledMap.getOrDefault(stageId, false);
|
||||
}
|
||||
|
||||
/**
|
||||
* 设置指定Stage的启用状态
|
||||
*
|
||||
* @param stageId Stage唯一标识
|
||||
* @param enabled 是否启用
|
||||
* @return this(支持链式调用)
|
||||
*/
|
||||
public FaceMatchingContext setStageState(String stageId, boolean enabled) {
|
||||
stageEnabledMap.put(stageId, enabled);
|
||||
return this;
|
||||
}
|
||||
|
||||
/**
|
||||
* 启用指定Stage
|
||||
*
|
||||
* @param stageId Stage唯一标识
|
||||
* @return this(支持链式调用)
|
||||
*/
|
||||
public FaceMatchingContext enableStage(String stageId) {
|
||||
stageEnabledMap.put(stageId, true);
|
||||
return this;
|
||||
}
|
||||
|
||||
/**
|
||||
* 禁用指定Stage
|
||||
*
|
||||
* @param stageId Stage唯一标识
|
||||
* @return this(支持链式调用)
|
||||
*/
|
||||
public FaceMatchingContext disableStage(String stageId) {
|
||||
stageEnabledMap.put(stageId, false);
|
||||
return this;
|
||||
}
|
||||
|
||||
/**
|
||||
* 批量设置Stage启用状态
|
||||
*
|
||||
* @param stages Stage配置Map(stageId -> enabled)
|
||||
* @return this(支持链式调用)
|
||||
*/
|
||||
public FaceMatchingContext setStages(Map<String, Boolean> stages) {
|
||||
if (stages != null) {
|
||||
stageEnabledMap.putAll(stages);
|
||||
}
|
||||
return this;
|
||||
}
|
||||
|
||||
/**
|
||||
* 清空所有Stage配置
|
||||
*
|
||||
* @return this(支持链式调用)
|
||||
*/
|
||||
public FaceMatchingContext clearStages() {
|
||||
stageEnabledMap.clear();
|
||||
return this;
|
||||
}
|
||||
|
||||
// ==================== Builder ====================
|
||||
|
||||
public static class Builder {
|
||||
private Long faceId;
|
||||
private boolean isNew = false;
|
||||
private FaceMatchingScene scene;
|
||||
private List<Long> faceSampleIds;
|
||||
|
||||
public Builder faceId(Long faceId) {
|
||||
this.faceId = faceId;
|
||||
return this;
|
||||
}
|
||||
|
||||
public Builder isNew(boolean isNew) {
|
||||
this.isNew = isNew;
|
||||
return this;
|
||||
}
|
||||
|
||||
public Builder scene(FaceMatchingScene scene) {
|
||||
this.scene = scene;
|
||||
return this;
|
||||
}
|
||||
|
||||
public Builder faceSampleIds(List<Long> faceSampleIds) {
|
||||
this.faceSampleIds = faceSampleIds;
|
||||
return this;
|
||||
}
|
||||
|
||||
public FaceMatchingContext build() {
|
||||
// 参数校验
|
||||
if (faceId == null) {
|
||||
throw new IllegalArgumentException("faceId is required");
|
||||
}
|
||||
if (scene == null) {
|
||||
throw new IllegalArgumentException("scene is required");
|
||||
}
|
||||
// 自定义匹配场景必须提供faceSampleIds
|
||||
if (scene == FaceMatchingScene.CUSTOM_MATCHING && (faceSampleIds == null || faceSampleIds.isEmpty())) {
|
||||
throw new IllegalArgumentException("faceSampleIds is required for CUSTOM_MATCHING scene");
|
||||
}
|
||||
return new FaceMatchingContext(this);
|
||||
}
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,25 @@
|
||||
package com.ycwl.basic.face.pipeline.enums;
|
||||
|
||||
/**
|
||||
* 人脸匹配场景枚举
|
||||
*/
|
||||
public enum FaceMatchingScene {
|
||||
|
||||
/**
|
||||
* 自动人脸匹配
|
||||
* 新用户上传人脸后自动执行匹配,或老用户重新匹配
|
||||
*/
|
||||
AUTO_MATCHING,
|
||||
|
||||
/**
|
||||
* 自定义人脸匹配
|
||||
* 用户手动选择人脸样本进行匹配
|
||||
*/
|
||||
CUSTOM_MATCHING,
|
||||
|
||||
/**
|
||||
* 仅识别
|
||||
* 只执行人脸识别,不处理后续业务逻辑(源文件关联、任务创建等)
|
||||
*/
|
||||
RECOGNITION_ONLY
|
||||
}
|
||||
@@ -0,0 +1,23 @@
|
||||
package com.ycwl.basic.face.pipeline.exception;
|
||||
|
||||
/**
|
||||
* Stage执行异常
|
||||
*/
|
||||
public class StageExecutionException extends RuntimeException {
|
||||
|
||||
private final String stageName;
|
||||
|
||||
public StageExecutionException(String stageName, String message) {
|
||||
super(String.format("[%s] %s", stageName, message));
|
||||
this.stageName = stageName;
|
||||
}
|
||||
|
||||
public StageExecutionException(String stageName, String message, Throwable cause) {
|
||||
super(String.format("[%s] %s", stageName, message), cause);
|
||||
this.stageName = stageName;
|
||||
}
|
||||
|
||||
public String getStageName() {
|
||||
return stageName;
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,234 @@
|
||||
package com.ycwl.basic.face.pipeline.factory;
|
||||
|
||||
import com.ycwl.basic.face.pipeline.core.FaceMatchingContext;
|
||||
import com.ycwl.basic.pipeline.core.Pipeline;
|
||||
import com.ycwl.basic.pipeline.core.PipelineBuilder;
|
||||
import com.ycwl.basic.face.pipeline.enums.FaceMatchingScene;
|
||||
import com.ycwl.basic.face.pipeline.stages.*;
|
||||
import com.ycwl.basic.service.pc.helper.ScenicConfigFacade;
|
||||
import lombok.extern.slf4j.Slf4j;
|
||||
import org.springframework.beans.factory.annotation.Autowired;
|
||||
import org.springframework.stereotype.Component;
|
||||
|
||||
/**
|
||||
* 人脸匹配Pipeline工厂
|
||||
* 负责为不同场景组装Pipeline
|
||||
*
|
||||
* 支持的场景:
|
||||
* 1. 自动人脸匹配(新用户/老用户)
|
||||
* 2. 自定义人脸匹配
|
||||
* 3. 仅识别
|
||||
*/
|
||||
@Slf4j
|
||||
@Component
|
||||
public class FaceMatchingPipelineFactory {
|
||||
|
||||
// ==================== 通用 Stage(13个)====================
|
||||
@Autowired
|
||||
private PrepareContextStage prepareContextStage;
|
||||
@Autowired
|
||||
private RecordMetricsStage recordMetricsStage;
|
||||
@Autowired
|
||||
private FaceRecognitionStage faceRecognitionStage;
|
||||
@Autowired
|
||||
private FaceRecoveryStage faceRecoveryStage;
|
||||
@Autowired
|
||||
private UpdateFaceResultStage updateFaceResultStage;
|
||||
@Autowired
|
||||
private BuildSourceRelationStage buildSourceRelationStage;
|
||||
@Autowired
|
||||
private ProcessFreeSourceStage processFreeSourceStage;
|
||||
@Autowired
|
||||
private ProcessBuyStatusStage processBuyStatusStage;
|
||||
@Autowired
|
||||
private HandleVideoRecreationStage handleVideoRecreationStage;
|
||||
@Autowired
|
||||
private PersistRelationsStage persistRelationsStage;
|
||||
@Autowired
|
||||
private CreateTaskStage createTaskStage;
|
||||
@Autowired
|
||||
private SetTaskStatusStage setTaskStatusStage;
|
||||
@Autowired
|
||||
private GeneratePuzzleStage generatePuzzleStage;
|
||||
|
||||
// ==================== 自定义匹配专属 Stage(6个)====================
|
||||
@Autowired
|
||||
private RecordCustomMatchMetricsStage recordCustomMatchMetricsStage;
|
||||
@Autowired
|
||||
private LoadFaceSamplesStage loadFaceSamplesStage;
|
||||
@Autowired
|
||||
private CustomFaceSearchStage customFaceSearchStage;
|
||||
@Autowired
|
||||
private LoadMatchedSamplesStage loadMatchedSamplesStage;
|
||||
@Autowired
|
||||
private FilterByTimeRangeStage filterByTimeRangeStage;
|
||||
@Autowired
|
||||
private FilterByDevicePhotoLimitStage filterByDevicePhotoLimitStage;
|
||||
@Autowired
|
||||
private DeleteOldRelationsStage deleteOldRelationsStage;
|
||||
|
||||
// ==================== 辅助服务 ====================
|
||||
@Autowired
|
||||
private ScenicConfigFacade scenicConfigFacade;
|
||||
|
||||
/**
|
||||
* 创建自动人脸匹配Pipeline
|
||||
*
|
||||
* @param isNew 是否新用户
|
||||
* @return Pipeline
|
||||
*/
|
||||
public Pipeline<FaceMatchingContext> createAutoMatchingPipeline(boolean isNew) {
|
||||
PipelineBuilder<FaceMatchingContext> builder = new PipelineBuilder<>("AutoMatching-" + (isNew ? "New" : "Old"));
|
||||
|
||||
// 1. 准备上下文
|
||||
builder.addStage(prepareContextStage);
|
||||
|
||||
// 2. 新用户设置任务状态
|
||||
if (isNew) {
|
||||
builder.addStage(setTaskStatusStage);
|
||||
}
|
||||
|
||||
// 3. 记录识别次数
|
||||
builder.addStage(recordMetricsStage);
|
||||
|
||||
// 4. 执行人脸识别
|
||||
builder.addStage(faceRecognitionStage);
|
||||
|
||||
// 5. 人脸识别补救
|
||||
builder.addStage(faceRecoveryStage);
|
||||
|
||||
// 6. 更新人脸结果
|
||||
builder.addStage(updateFaceResultStage);
|
||||
|
||||
// 7. 构建源文件关联
|
||||
builder.addStage(buildSourceRelationStage);
|
||||
|
||||
// 8. 处理免费源文件逻辑
|
||||
builder.addStage(processFreeSourceStage);
|
||||
|
||||
// 9. 处理购买状态
|
||||
builder.addStage(processBuyStatusStage);
|
||||
|
||||
// 10. 处理视频重切
|
||||
builder.addStage(handleVideoRecreationStage);
|
||||
|
||||
// 11. 持久化关联关系
|
||||
builder.addStage(persistRelationsStage);
|
||||
|
||||
// 12. 创建任务
|
||||
builder.addStage(createTaskStage);
|
||||
|
||||
// 13. 异步生成拼图模板
|
||||
builder.addStage(generatePuzzleStage);
|
||||
|
||||
log.debug("创建自动人脸匹配Pipeline: isNew={}, stageCount={}", isNew, builder.build().getStageCount());
|
||||
|
||||
return builder.build();
|
||||
}
|
||||
|
||||
/**
|
||||
* 创建自定义人脸匹配Pipeline
|
||||
*
|
||||
* @return Pipeline
|
||||
*/
|
||||
public Pipeline<FaceMatchingContext> createCustomMatchingPipeline() {
|
||||
PipelineBuilder<FaceMatchingContext> builder = new PipelineBuilder<>("CustomMatching");
|
||||
|
||||
// 1. 准备上下文
|
||||
builder.addStage(prepareContextStage);
|
||||
|
||||
// 2. 记录自定义匹配次数
|
||||
builder.addStage(recordCustomMatchMetricsStage);
|
||||
|
||||
// 3. 加载用户选择的人脸样本
|
||||
builder.addStage(loadFaceSamplesStage);
|
||||
|
||||
// 4. 根据配置执行自定义人脸搜索
|
||||
builder.addStage(customFaceSearchStage);
|
||||
|
||||
// 5. 加载匹配样本实体到缓存
|
||||
builder.addStage(loadMatchedSamplesStage);
|
||||
|
||||
// 6. 应用时间范围筛选
|
||||
builder.addStage(filterByTimeRangeStage);
|
||||
|
||||
// 7. 应用设备照片数量限制筛选
|
||||
builder.addStage(filterByDevicePhotoLimitStage);
|
||||
|
||||
// 8. 更新人脸结果
|
||||
builder.addStage(updateFaceResultStage);
|
||||
|
||||
// 9. 删除旧关系数据
|
||||
builder.addStage(deleteOldRelationsStage);
|
||||
|
||||
// 10. 构建源文件关联
|
||||
builder.addStage(buildSourceRelationStage);
|
||||
|
||||
// 11. 处理免费源文件逻辑
|
||||
builder.addStage(processFreeSourceStage);
|
||||
|
||||
// 12. 处理购买状态
|
||||
builder.addStage(processBuyStatusStage);
|
||||
|
||||
// 13. 处理视频重切
|
||||
builder.addStage(handleVideoRecreationStage);
|
||||
|
||||
// 14. 持久化关联关系
|
||||
builder.addStage(persistRelationsStage);
|
||||
|
||||
// 15. 创建任务
|
||||
builder.addStage(createTaskStage);
|
||||
|
||||
log.debug("创建自定义人脸匹配Pipeline: stageCount={}", builder.build().getStageCount());
|
||||
|
||||
return builder.build();
|
||||
}
|
||||
|
||||
/**
|
||||
* 创建仅识别Pipeline
|
||||
* 只执行人脸识别,不处理后续业务逻辑
|
||||
*
|
||||
* @return Pipeline
|
||||
*/
|
||||
public Pipeline<FaceMatchingContext> createRecognitionOnlyPipeline() {
|
||||
PipelineBuilder<FaceMatchingContext> builder = new PipelineBuilder<>("RecognitionOnly");
|
||||
|
||||
// 1. 准备上下文
|
||||
builder.addStage(prepareContextStage);
|
||||
|
||||
// 2. 执行人脸识别
|
||||
builder.addStage(faceRecognitionStage);
|
||||
|
||||
// 3. 人脸识别补救
|
||||
builder.addStage(faceRecoveryStage);
|
||||
|
||||
log.debug("创建仅识别Pipeline: stageCount={}", builder.build().getStageCount());
|
||||
|
||||
return builder.build();
|
||||
}
|
||||
|
||||
/**
|
||||
* 根据场景创建Pipeline
|
||||
*
|
||||
* @param scene 场景
|
||||
* @param isNew 是否新用户(仅AUTO_MATCHING场景需要)
|
||||
* @return Pipeline
|
||||
*/
|
||||
public Pipeline<FaceMatchingContext> createPipeline(FaceMatchingScene scene, boolean isNew) {
|
||||
return switch (scene) {
|
||||
case AUTO_MATCHING -> createAutoMatchingPipeline(isNew);
|
||||
case CUSTOM_MATCHING -> createCustomMatchingPipeline();
|
||||
case RECOGNITION_ONLY -> createRecognitionOnlyPipeline();
|
||||
};
|
||||
}
|
||||
|
||||
/**
|
||||
* 根据Context创建Pipeline
|
||||
*
|
||||
* @param context 上下文
|
||||
* @return Pipeline
|
||||
*/
|
||||
public Pipeline<FaceMatchingContext> createPipeline(FaceMatchingContext context) {
|
||||
return createPipeline(context.getScene(), context.isNew());
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,156 @@
|
||||
package com.ycwl.basic.face.pipeline.helper;
|
||||
|
||||
import cn.hutool.core.date.DateUtil;
|
||||
import com.ycwl.basic.integration.scenic.dto.scenic.ScenicV2DTO;
|
||||
import com.ycwl.basic.puzzle.dto.PuzzleGenerateRequest;
|
||||
import com.ycwl.basic.puzzle.dto.PuzzleGenerateResponse;
|
||||
import com.ycwl.basic.puzzle.dto.PuzzleTemplateDTO;
|
||||
import com.ycwl.basic.puzzle.service.IPuzzleGenerateService;
|
||||
import com.ycwl.basic.puzzle.service.IPuzzleTemplateService;
|
||||
import com.ycwl.basic.repository.ScenicRepository;
|
||||
import lombok.extern.slf4j.Slf4j;
|
||||
import org.springframework.beans.factory.annotation.Autowired;
|
||||
import org.springframework.stereotype.Service;
|
||||
|
||||
import java.util.Date;
|
||||
import java.util.HashMap;
|
||||
import java.util.List;
|
||||
import java.util.Map;
|
||||
|
||||
/**
|
||||
* 拼图生成编排器
|
||||
* 负责编排拼图模板的批量生成逻辑
|
||||
*
|
||||
* 职责:
|
||||
* 1. 查询景区的所有启用拼图模板
|
||||
* 2. 构建动态数据
|
||||
* 3. 逐个生成拼图图片
|
||||
* 4. 记录统计信息
|
||||
*
|
||||
* 设计说明:
|
||||
* - 从GeneratePuzzleStage中抽离出来,符合"薄Stage,厚Service"原则
|
||||
* - Stage只负责触发异步任务,业务逻辑由此Orchestrator承担
|
||||
*/
|
||||
@Slf4j
|
||||
@Service
|
||||
public class PuzzleGenerationOrchestrator {
|
||||
|
||||
@Autowired
|
||||
private IPuzzleTemplateService puzzleTemplateService;
|
||||
|
||||
@Autowired
|
||||
private IPuzzleGenerateService puzzleGenerateService;
|
||||
|
||||
@Autowired
|
||||
private ScenicRepository scenicRepository;
|
||||
|
||||
/**
|
||||
* 异步生成景区所有启用的拼图模板
|
||||
*
|
||||
* @param scenicId 景区ID
|
||||
* @param faceId 人脸ID
|
||||
* @param memberId 会员ID
|
||||
* @param faceUrl 人脸URL
|
||||
*/
|
||||
public void generateAllTemplatesAsync(Long scenicId, Long faceId, Long memberId, String faceUrl) {
|
||||
new Thread(() -> {
|
||||
try {
|
||||
log.info("开始异步生成景区拼图模板: scenicId={}, faceId={}", scenicId, faceId);
|
||||
|
||||
// 1. 查询该景区所有启用状态的拼图模板
|
||||
List<PuzzleTemplateDTO> templateList = puzzleTemplateService.listTemplates(
|
||||
scenicId, null, 1); // 查询启用状态的模板
|
||||
|
||||
if (templateList == null || templateList.isEmpty()) {
|
||||
log.info("景区不存在启用的拼图模板,跳过生成: scenicId={}", scenicId);
|
||||
return;
|
||||
}
|
||||
|
||||
log.info("景区存在 {} 个启用的拼图模板,开始逐个生成: scenicId={}", templateList.size(), scenicId);
|
||||
|
||||
// 2. 获取景区信息用于动态数据
|
||||
ScenicV2DTO scenicBasic = scenicRepository.getScenicBasic(scenicId);
|
||||
|
||||
// 3. 准备公共动态数据
|
||||
Map<String, String> baseDynamicData = buildBaseDynamicData(faceId, faceUrl, scenicBasic);
|
||||
|
||||
// 4. 使用虚拟线程池并行生成所有模板
|
||||
java.util.concurrent.atomic.AtomicInteger successCount = new java.util.concurrent.atomic.AtomicInteger(0);
|
||||
java.util.concurrent.atomic.AtomicInteger failCount = new java.util.concurrent.atomic.AtomicInteger(0);
|
||||
|
||||
try (java.util.concurrent.ExecutorService executor = java.util.concurrent.Executors.newVirtualThreadPerTaskExecutor()) {
|
||||
// 为每个模板创建一个异步任务
|
||||
List<java.util.concurrent.CompletableFuture<Void>> futures = templateList.stream()
|
||||
.map(template -> java.util.concurrent.CompletableFuture.runAsync(() -> {
|
||||
try {
|
||||
generateSingleTemplate(scenicId, faceId, memberId, template, baseDynamicData);
|
||||
successCount.incrementAndGet();
|
||||
} catch (Exception e) {
|
||||
log.error("拼图生成失败: scenicId={}, templateCode={}, templateName={}",
|
||||
scenicId, template.getCode(), template.getName(), e);
|
||||
failCount.incrementAndGet();
|
||||
}
|
||||
}, executor))
|
||||
.toList();
|
||||
|
||||
// 等待所有任务完成
|
||||
java.util.concurrent.CompletableFuture.allOf(futures.toArray(new java.util.concurrent.CompletableFuture[0])).join();
|
||||
}
|
||||
|
||||
log.info("景区拼图模板批量生成完成: scenicId={}, 总数={}, 成功={}, 失败={}",
|
||||
scenicId, templateList.size(), successCount.get(), failCount.get());
|
||||
|
||||
} catch (Exception e) {
|
||||
// 异步任务失败不影响主流程,仅记录日志
|
||||
log.error("异步生成拼图模板失败: scenicId={}, faceId={}", scenicId, faceId, e);
|
||||
}
|
||||
}, "PuzzleTemplateGenerator-" + scenicId + "-" + faceId).start();
|
||||
}
|
||||
|
||||
/**
|
||||
* 构建基础动态数据
|
||||
*/
|
||||
private Map<String, String> buildBaseDynamicData(Long faceId, String faceUrl, ScenicV2DTO scenicBasic) {
|
||||
Map<String, String> baseDynamicData = new HashMap<>();
|
||||
|
||||
if (faceUrl != null) {
|
||||
baseDynamicData.put("faceImage", faceUrl);
|
||||
baseDynamicData.put("userAvatar", faceUrl);
|
||||
}
|
||||
|
||||
baseDynamicData.put("faceId", String.valueOf(faceId));
|
||||
baseDynamicData.put("scenicName", scenicBasic.getName());
|
||||
baseDynamicData.put("scenicText", scenicBasic.getName());
|
||||
baseDynamicData.put("dateStr", DateUtil.format(new Date(), "yyyy.MM.dd"));
|
||||
|
||||
return baseDynamicData;
|
||||
}
|
||||
|
||||
/**
|
||||
* 生成单个拼图模板
|
||||
*/
|
||||
private void generateSingleTemplate(Long scenicId, Long faceId, Long memberId,
|
||||
PuzzleTemplateDTO template,
|
||||
Map<String, String> baseDynamicData) {
|
||||
log.info("开始生成拼图: scenicId={}, templateCode={}, templateName={}",
|
||||
scenicId, template.getCode(), template.getName());
|
||||
|
||||
// 构建生成请求
|
||||
PuzzleGenerateRequest generateRequest = new PuzzleGenerateRequest();
|
||||
generateRequest.setScenicId(scenicId);
|
||||
generateRequest.setUserId(memberId);
|
||||
generateRequest.setFaceId(faceId);
|
||||
generateRequest.setBusinessType("face_matching");
|
||||
generateRequest.setTemplateCode(template.getCode());
|
||||
generateRequest.setOutputFormat("PNG");
|
||||
generateRequest.setQuality(90);
|
||||
generateRequest.setDynamicData(new HashMap<>(baseDynamicData));
|
||||
generateRequest.setRequireRuleMatch(true);
|
||||
|
||||
// 调用拼图生成服务
|
||||
PuzzleGenerateResponse response = puzzleGenerateService.generate(generateRequest);
|
||||
|
||||
log.info("拼图生成成功: scenicId={}, templateCode={}, imageUrl={}",
|
||||
scenicId, template.getCode(), response.getImageUrl());
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,109 @@
|
||||
package com.ycwl.basic.face.pipeline.stages;
|
||||
|
||||
import com.ycwl.basic.face.pipeline.core.FaceMatchingContext;
|
||||
import com.ycwl.basic.pipeline.annotation.StageConfig;
|
||||
import com.ycwl.basic.pipeline.core.AbstractPipelineStage;
|
||||
import com.ycwl.basic.pipeline.core.StageResult;
|
||||
import com.ycwl.basic.pipeline.enums.StageOptionalMode;
|
||||
import com.ycwl.basic.model.pc.source.entity.MemberSourceEntity;
|
||||
import com.ycwl.basic.service.pc.processor.SourceRelationProcessor;
|
||||
import lombok.extern.slf4j.Slf4j;
|
||||
import org.springframework.beans.factory.annotation.Autowired;
|
||||
import org.springframework.stereotype.Component;
|
||||
|
||||
import java.util.List;
|
||||
|
||||
/**
|
||||
* 构建源文件关联Stage
|
||||
* 负责根据匹配到的样本ID构建member_source关联关系
|
||||
*
|
||||
* 职责:
|
||||
* 1. 从context.sampleListIds读取匹配的样本ID列表
|
||||
* 2. 调用sourceRelationProcessor.processMemberSources()生成MemberSourceEntity列表
|
||||
* 3. 更新context.memberSourceList
|
||||
*
|
||||
* 前置条件: context.sampleListIds不为空
|
||||
* 后置条件: context.memberSourceList已设置
|
||||
*/
|
||||
@Slf4j
|
||||
@Component
|
||||
@StageConfig(
|
||||
stageId = "build_source_relation",
|
||||
optionalMode = StageOptionalMode.UNSUPPORT,
|
||||
description = "构建源文件关联关系"
|
||||
)
|
||||
public class BuildSourceRelationStage extends AbstractPipelineStage<FaceMatchingContext> {
|
||||
|
||||
@Autowired
|
||||
private SourceRelationProcessor sourceRelationProcessor;
|
||||
|
||||
@Override
|
||||
public String getName() {
|
||||
return "BuildSourceRelation";
|
||||
}
|
||||
|
||||
@Override
|
||||
protected boolean shouldExecuteByBusinessLogic(FaceMatchingContext context) {
|
||||
// 只有当sampleListIds不为空时才执行
|
||||
List<Long> sampleListIds = context.getSampleListIds();
|
||||
if (sampleListIds == null || sampleListIds.isEmpty()) {
|
||||
// 从searchResult中获取
|
||||
if (context.getSearchResult() != null) {
|
||||
sampleListIds = context.getSearchResult().getSampleListIds();
|
||||
context.setSampleListIds(sampleListIds);
|
||||
}
|
||||
}
|
||||
|
||||
if (sampleListIds == null || sampleListIds.isEmpty()) {
|
||||
log.debug("sampleListIds为空,跳过源文件关联,faceId={}", context.getFaceId());
|
||||
return false;
|
||||
}
|
||||
|
||||
return true;
|
||||
}
|
||||
|
||||
@Override
|
||||
protected StageResult<FaceMatchingContext> doExecute(FaceMatchingContext context) {
|
||||
List<Long> sampleListIds = context.getSampleListIds();
|
||||
Long faceId = context.getFaceId();
|
||||
|
||||
// 防御性检查:sampleListIds为空
|
||||
if (sampleListIds == null || sampleListIds.isEmpty()) {
|
||||
// 尝试从searchResult中获取
|
||||
if (context.getSearchResult() != null) {
|
||||
sampleListIds = context.getSearchResult().getSampleListIds();
|
||||
if (sampleListIds != null && !sampleListIds.isEmpty()) {
|
||||
context.setSampleListIds(sampleListIds);
|
||||
} else {
|
||||
log.debug("sampleListIds为空,跳过源文件关联,faceId={}", faceId);
|
||||
return StageResult.skipped("sampleListIds为空");
|
||||
}
|
||||
} else {
|
||||
log.debug("sampleListIds为空,跳过源文件关联,faceId={}", faceId);
|
||||
return StageResult.skipped("sampleListIds为空");
|
||||
}
|
||||
}
|
||||
|
||||
try {
|
||||
// 处理源文件关联
|
||||
List<MemberSourceEntity> memberSourceEntityList =
|
||||
sourceRelationProcessor.processMemberSources(sampleListIds, context.getFace());
|
||||
|
||||
if (memberSourceEntityList == null || memberSourceEntityList.isEmpty()) {
|
||||
log.warn("未找到有效的源文件,faceId={}, sampleListIds={}", faceId, sampleListIds);
|
||||
return StageResult.skipped("未找到有效的源文件");
|
||||
}
|
||||
|
||||
context.setMemberSourceList(memberSourceEntityList);
|
||||
|
||||
log.info("构建源文件关联成功: faceId={}, 关联源文件数={}", faceId, memberSourceEntityList.size());
|
||||
|
||||
return StageResult.success(String.format("构建了%d个源文件关联", memberSourceEntityList.size()));
|
||||
|
||||
} catch (Exception e) {
|
||||
log.error("构建源文件关联失败,faceId={}, sampleListIds={}", faceId, sampleListIds, e);
|
||||
// 源文件关联失败不影响主流程,返回降级
|
||||
return StageResult.degraded("构建源文件关联失败: " + e.getMessage());
|
||||
}
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,73 @@
|
||||
package com.ycwl.basic.face.pipeline.stages;
|
||||
|
||||
import com.ycwl.basic.biz.TaskStatusBiz;
|
||||
import com.ycwl.basic.face.pipeline.core.FaceMatchingContext;
|
||||
import com.ycwl.basic.pipeline.annotation.StageConfig;
|
||||
import com.ycwl.basic.pipeline.core.AbstractPipelineStage;
|
||||
import com.ycwl.basic.pipeline.core.StageResult;
|
||||
import com.ycwl.basic.pipeline.enums.StageOptionalMode;
|
||||
import com.ycwl.basic.service.pc.helper.ScenicConfigFacade;
|
||||
import com.ycwl.basic.service.task.TaskService;
|
||||
import lombok.extern.slf4j.Slf4j;
|
||||
import org.springframework.beans.factory.annotation.Autowired;
|
||||
import org.springframework.stereotype.Component;
|
||||
|
||||
/**
|
||||
* 创建任务Stage
|
||||
* 负责根据配置决定是否自动创建任务
|
||||
*
|
||||
* 职责:
|
||||
* 1. 检查face_select_first配置
|
||||
* 2. 如果配置为false,则调用taskService.autoCreateTaskByFaceId()
|
||||
* 3. 如果配置为true,则设置任务状态为2(等待用户选择)
|
||||
*/
|
||||
@Slf4j
|
||||
@Component
|
||||
@StageConfig(
|
||||
stageId = "create_task",
|
||||
optionalMode = StageOptionalMode.UNSUPPORT,
|
||||
description = "根据配置创建视频任务"
|
||||
)
|
||||
public class CreateTaskStage extends AbstractPipelineStage<FaceMatchingContext> {
|
||||
|
||||
@Autowired
|
||||
private ScenicConfigFacade scenicConfigFacade;
|
||||
|
||||
@Autowired
|
||||
private TaskService taskService;
|
||||
|
||||
@Autowired
|
||||
private TaskStatusBiz taskStatusBiz;
|
||||
|
||||
@Override
|
||||
public String getName() {
|
||||
return "CreateTask";
|
||||
}
|
||||
|
||||
@Override
|
||||
protected StageResult<FaceMatchingContext> doExecute(FaceMatchingContext context) {
|
||||
Long scenicId = context.getFace().getScenicId();
|
||||
Long faceId = context.getFaceId();
|
||||
|
||||
try {
|
||||
boolean faceSelectFirst = scenicConfigFacade.isFaceSelectFirst(scenicId);
|
||||
|
||||
if (!faceSelectFirst) {
|
||||
// 配置为自动创建任务
|
||||
taskService.autoCreateTaskByFaceId(faceId);
|
||||
log.info("自动创建任务成功: faceId={}", faceId);
|
||||
return StageResult.success("自动创建任务成功");
|
||||
} else {
|
||||
// 配置为等待用户选择
|
||||
taskStatusBiz.setFaceCutStatus(faceId, 2);
|
||||
log.debug("景区配置 face_select_first=true,跳过自动创建任务: faceId={}", faceId);
|
||||
return StageResult.skipped("等待用户手动选择");
|
||||
}
|
||||
|
||||
} catch (Exception e) {
|
||||
log.error("创建任务失败,faceId={}", faceId, e);
|
||||
// 任务创建失败不影响主流程,返回降级而不是失败
|
||||
return StageResult.degraded("任务创建失败: " + e.getMessage());
|
||||
}
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,121 @@
|
||||
package com.ycwl.basic.face.pipeline.stages;
|
||||
|
||||
import com.ycwl.basic.exception.BaseException;
|
||||
import com.ycwl.basic.face.pipeline.core.FaceMatchingContext;
|
||||
import com.ycwl.basic.pipeline.annotation.StageConfig;
|
||||
import com.ycwl.basic.pipeline.core.AbstractPipelineStage;
|
||||
import com.ycwl.basic.pipeline.core.StageResult;
|
||||
import com.ycwl.basic.pipeline.enums.StageOptionalMode;
|
||||
import com.ycwl.basic.model.pc.faceSample.entity.FaceSampleEntity;
|
||||
import com.ycwl.basic.model.task.resp.SearchFaceRespVo;
|
||||
import com.ycwl.basic.service.pc.helper.SearchResultMerger;
|
||||
import com.ycwl.basic.service.task.TaskFaceService;
|
||||
import lombok.extern.slf4j.Slf4j;
|
||||
import org.springframework.beans.factory.annotation.Autowired;
|
||||
import org.springframework.stereotype.Component;
|
||||
|
||||
import java.util.ArrayList;
|
||||
import java.util.List;
|
||||
|
||||
/**
|
||||
* 自定义人脸搜索Stage
|
||||
* 负责根据faceSelectPostMode执行不同的搜索策略
|
||||
*
|
||||
* 职责:
|
||||
* 1. 从context.faceSelectPostMode读取配置
|
||||
* 2. 模式2: 直接使用用户选择的样本,不搜索
|
||||
* 3. 模式0/1: 对每个样本搜索,然后合并结果
|
||||
* 4. 更新context.searchResult
|
||||
*/
|
||||
@Slf4j
|
||||
@Component
|
||||
@StageConfig(
|
||||
stageId = "custom_face_search",
|
||||
optionalMode = StageOptionalMode.FORCE_ON,
|
||||
description = "根据配置执行自定义人脸搜索"
|
||||
)
|
||||
public class CustomFaceSearchStage extends AbstractPipelineStage<FaceMatchingContext> {
|
||||
|
||||
@Autowired
|
||||
private TaskFaceService taskFaceService;
|
||||
|
||||
@Autowired
|
||||
private SearchResultMerger resultMerger;
|
||||
|
||||
@Override
|
||||
public String getName() {
|
||||
return "CustomFaceSearch";
|
||||
}
|
||||
|
||||
@Override
|
||||
protected StageResult<FaceMatchingContext> doExecute(FaceMatchingContext context) {
|
||||
Integer faceSelectPostMode = context.getFaceSelectPostMode();
|
||||
List<FaceSampleEntity> faceSamples = context.getFaceSamples();
|
||||
List<Long> faceSampleIds = context.getFaceSampleIds();
|
||||
Long faceId = context.getFaceId();
|
||||
|
||||
if (faceSelectPostMode == null) {
|
||||
faceSelectPostMode = 0; // 默认为并集模式
|
||||
}
|
||||
|
||||
log.debug("face_select_post_mode配置值: {}, faceId={}", faceSelectPostMode, faceId);
|
||||
|
||||
try {
|
||||
SearchFaceRespVo mergedResult;
|
||||
|
||||
// 模式2:不搜索,直接使用用户选择的faceSampleIds
|
||||
if (Integer.valueOf(2).equals(faceSelectPostMode)) {
|
||||
log.debug("使用模式2:直接使用用户选择的人脸样本,不进行搜索,faceId={}", faceId);
|
||||
mergedResult = resultMerger.createDirectResult(faceSampleIds);
|
||||
// 保留原始matchResult
|
||||
if (context.getFace().getMatchResult() != null) {
|
||||
mergedResult.setSearchResultJson(context.getFace().getMatchResult());
|
||||
}
|
||||
} else {
|
||||
// 模式0(并集)和模式1(交集):需要进行搜索
|
||||
List<SearchFaceRespVo> searchResults = new ArrayList<>();
|
||||
|
||||
for (FaceSampleEntity faceSample : faceSamples) {
|
||||
try {
|
||||
SearchFaceRespVo result = taskFaceService.searchFace(
|
||||
context.getFaceBodyAdapter(),
|
||||
String.valueOf(context.getFace().getScenicId()),
|
||||
faceSample.getFaceUrl(),
|
||||
"自定义人脸匹配");
|
||||
|
||||
if (result != null) {
|
||||
searchResults.add(result);
|
||||
}
|
||||
} catch (Exception e) {
|
||||
log.warn("人脸样本搜索失败,faceSampleId={}, faceUrl={}, faceId={}",
|
||||
faceSample.getId(), faceSample.getFaceUrl(), faceId, e);
|
||||
// 继续处理其他样本,不中断整个流程
|
||||
}
|
||||
}
|
||||
|
||||
if (searchResults.isEmpty()) {
|
||||
log.warn("所有人脸样本搜索都失败,faceId={}, faceSampleIds={}", faceId, faceSampleIds);
|
||||
throw new BaseException("人脸识别失败,请重试");
|
||||
}
|
||||
|
||||
// 根据模式整合多个搜索结果
|
||||
mergedResult = resultMerger.merge(searchResults, faceSelectPostMode);
|
||||
}
|
||||
|
||||
context.setSearchResult(mergedResult);
|
||||
context.setSampleListIds(mergedResult.getSampleListIds());
|
||||
|
||||
log.info("自定义人脸搜索完成: faceId={}, mode={}, 匹配数={}",
|
||||
faceId, faceSelectPostMode,
|
||||
mergedResult.getSampleListIds() != null ? mergedResult.getSampleListIds().size() : 0);
|
||||
|
||||
return StageResult.success(String.format("自定义搜索完成,模式=%d", faceSelectPostMode));
|
||||
|
||||
} catch (BaseException e) {
|
||||
throw e;
|
||||
} catch (Exception e) {
|
||||
log.error("自定义人脸搜索失败,faceId={}, faceSampleIds={}", faceId, faceSampleIds, e);
|
||||
return StageResult.failed("自定义人脸搜索失败", e);
|
||||
}
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,74 @@
|
||||
package com.ycwl.basic.face.pipeline.stages;
|
||||
|
||||
import com.ycwl.basic.face.pipeline.core.FaceMatchingContext;
|
||||
import com.ycwl.basic.pipeline.annotation.StageConfig;
|
||||
import com.ycwl.basic.pipeline.core.AbstractPipelineStage;
|
||||
import com.ycwl.basic.pipeline.core.StageResult;
|
||||
import com.ycwl.basic.pipeline.enums.StageOptionalMode;
|
||||
import com.ycwl.basic.mapper.SourceMapper;
|
||||
import com.ycwl.basic.mapper.VideoMapper;
|
||||
import com.ycwl.basic.repository.MemberRelationRepository;
|
||||
import lombok.extern.slf4j.Slf4j;
|
||||
import org.springframework.beans.factory.annotation.Autowired;
|
||||
import org.springframework.stereotype.Component;
|
||||
|
||||
/**
|
||||
* 删除旧关系Stage
|
||||
* 负责在保存新关系前,删除该人脸的旧数据关系
|
||||
*
|
||||
* 职责:
|
||||
* 1. 删除member_source中该人脸的未购买关系
|
||||
* 2. 删除member_video中该人脸的未购买关系
|
||||
* 3. 清除缓存
|
||||
*/
|
||||
@Slf4j
|
||||
@Component
|
||||
@StageConfig(
|
||||
stageId = "delete_old_relations",
|
||||
optionalMode = StageOptionalMode.FORCE_ON,
|
||||
description = "删除人脸旧关系数据"
|
||||
)
|
||||
public class DeleteOldRelationsStage extends AbstractPipelineStage<FaceMatchingContext> {
|
||||
|
||||
@Autowired
|
||||
private SourceMapper sourceMapper;
|
||||
|
||||
@Autowired
|
||||
private VideoMapper videoMapper;
|
||||
|
||||
@Autowired
|
||||
private MemberRelationRepository memberRelationRepository;
|
||||
|
||||
@Override
|
||||
public String getName() {
|
||||
return "DeleteOldRelations";
|
||||
}
|
||||
|
||||
@Override
|
||||
protected StageResult<FaceMatchingContext> doExecute(FaceMatchingContext context) {
|
||||
Long faceId = context.getFaceId();
|
||||
Long memberId = context.getFace().getMemberId();
|
||||
|
||||
try {
|
||||
log.debug("删除人脸旧关系数据:faceId={}, memberId={}", faceId, memberId);
|
||||
|
||||
// 1. 删除member_source中的未购买关系
|
||||
sourceMapper.deleteNotBuyFaceRelation(memberId, faceId);
|
||||
|
||||
// 2. 删除member_video中的未购买关系
|
||||
videoMapper.deleteNotBuyFaceRelations(memberId, faceId);
|
||||
|
||||
// 3. 清除缓存
|
||||
memberRelationRepository.clearSCacheByFace(faceId);
|
||||
|
||||
log.debug("人脸旧关系数据删除完成:faceId={}", faceId);
|
||||
|
||||
return StageResult.success("旧关系数据已删除");
|
||||
|
||||
} catch (Exception e) {
|
||||
log.error("删除旧关系数据失败,faceId={}", faceId, e);
|
||||
// 删除失败不影响主流程,返回降级
|
||||
return StageResult.degraded("删除旧关系数据失败: " + e.getMessage());
|
||||
}
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,73 @@
|
||||
package com.ycwl.basic.face.pipeline.stages;
|
||||
|
||||
import com.ycwl.basic.exception.BaseException;
|
||||
import com.ycwl.basic.face.pipeline.core.FaceMatchingContext;
|
||||
import com.ycwl.basic.pipeline.annotation.StageConfig;
|
||||
import com.ycwl.basic.pipeline.core.AbstractPipelineStage;
|
||||
import com.ycwl.basic.pipeline.core.StageResult;
|
||||
import com.ycwl.basic.pipeline.enums.StageOptionalMode;
|
||||
import com.ycwl.basic.model.task.resp.SearchFaceRespVo;
|
||||
import com.ycwl.basic.service.task.TaskFaceService;
|
||||
import lombok.extern.slf4j.Slf4j;
|
||||
import org.springframework.beans.factory.annotation.Autowired;
|
||||
import org.springframework.stereotype.Component;
|
||||
|
||||
/**
|
||||
* 人脸识别Stage
|
||||
* 负责执行核心的人脸识别搜索
|
||||
*
|
||||
* 职责:
|
||||
* 1. 调用taskFaceService.searchFace()执行人脸搜索
|
||||
* 2. 将结果存入context.searchResult
|
||||
* 3. 识别失败则返回FAILED
|
||||
*/
|
||||
@Slf4j
|
||||
@Component
|
||||
@StageConfig(
|
||||
stageId = "face_recognition",
|
||||
optionalMode = StageOptionalMode.FORCE_ON,
|
||||
description = "执行人脸识别搜索"
|
||||
)
|
||||
public class FaceRecognitionStage extends AbstractPipelineStage<FaceMatchingContext> {
|
||||
|
||||
@Autowired
|
||||
private TaskFaceService taskFaceService;
|
||||
|
||||
@Override
|
||||
public String getName() {
|
||||
return "FaceRecognition";
|
||||
}
|
||||
|
||||
@Override
|
||||
protected StageResult<FaceMatchingContext> doExecute(FaceMatchingContext context) {
|
||||
try {
|
||||
SearchFaceRespVo searchResult = taskFaceService.searchFace(
|
||||
context.getFaceBodyAdapter(),
|
||||
String.valueOf(context.getFace().getScenicId()),
|
||||
context.getFace().getFaceUrl(),
|
||||
"人脸识别");
|
||||
|
||||
if (searchResult == null) {
|
||||
log.warn("人脸识别返回结果为空,faceId={}", context.getFaceId());
|
||||
return StageResult.failed("人脸识别失败,请换一张试试把~");
|
||||
}
|
||||
|
||||
context.setSearchResult(searchResult);
|
||||
|
||||
log.info("人脸识别完成: faceId={}, score={}, 匹配数={}",
|
||||
context.getFaceId(),
|
||||
searchResult.getScore(),
|
||||
searchResult.getSampleListIds() != null ? searchResult.getSampleListIds().size() : 0);
|
||||
|
||||
return StageResult.success(String.format("识别成功,匹配数=%d",
|
||||
searchResult.getSampleListIds() != null ? searchResult.getSampleListIds().size() : 0));
|
||||
|
||||
} catch (BaseException e) {
|
||||
throw e;
|
||||
} catch (Exception e) {
|
||||
log.error("人脸识别服务调用失败,faceId={}, scenicId={}",
|
||||
context.getFaceId(), context.getFace().getScenicId(), e);
|
||||
return StageResult.failed("人脸识别失败,请换一张试试把~", e);
|
||||
}
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,86 @@
|
||||
package com.ycwl.basic.face.pipeline.stages;
|
||||
|
||||
import com.ycwl.basic.face.pipeline.core.FaceMatchingContext;
|
||||
import com.ycwl.basic.pipeline.annotation.StageConfig;
|
||||
import com.ycwl.basic.pipeline.core.AbstractPipelineStage;
|
||||
import com.ycwl.basic.pipeline.core.StageResult;
|
||||
import com.ycwl.basic.pipeline.enums.StageOptionalMode;
|
||||
import com.ycwl.basic.model.task.resp.SearchFaceRespVo;
|
||||
import com.ycwl.basic.service.pc.processor.FaceRecoveryStrategy;
|
||||
import lombok.extern.slf4j.Slf4j;
|
||||
import org.springframework.beans.factory.annotation.Autowired;
|
||||
import org.springframework.stereotype.Component;
|
||||
|
||||
/**
|
||||
* 人脸识别补救Stage
|
||||
* 负责执行人脸识别的补救逻辑(降级)
|
||||
*
|
||||
* 职责:
|
||||
* 1. 从context.searchResult读取识别结果
|
||||
* 2. 调用faceRecoveryStrategy.executeFaceRecoveryLogic()执行补救
|
||||
* 3. 如果触发补救,更新searchResult并返回DEGRADED
|
||||
* 4. 否则返回SUCCESS
|
||||
*/
|
||||
@Slf4j
|
||||
@Component
|
||||
@StageConfig(
|
||||
stageId = "face_recovery",
|
||||
optionalMode = StageOptionalMode.SUPPORT,
|
||||
description = "执行人脸识别补救逻辑",
|
||||
defaultEnabled = true
|
||||
)
|
||||
public class FaceRecoveryStage extends AbstractPipelineStage<FaceMatchingContext> {
|
||||
|
||||
@Autowired
|
||||
private FaceRecoveryStrategy faceRecoveryStrategy;
|
||||
|
||||
@Override
|
||||
public String getName() {
|
||||
return "FaceRecovery";
|
||||
}
|
||||
|
||||
@Override
|
||||
protected boolean shouldExecuteByBusinessLogic(FaceMatchingContext context) {
|
||||
// 只有当searchResult不为空时才执行
|
||||
if (context.getSearchResult() == null) {
|
||||
log.debug("searchResult为空,跳过补救逻辑,faceId={}", context.getFaceId());
|
||||
return false;
|
||||
}
|
||||
return true;
|
||||
}
|
||||
|
||||
@Override
|
||||
protected StageResult<FaceMatchingContext> doExecute(FaceMatchingContext context) {
|
||||
SearchFaceRespVo searchResult = context.getSearchResult();
|
||||
Long faceId = context.getFaceId();
|
||||
|
||||
// 防御性检查:searchResult为空
|
||||
if (searchResult == null) {
|
||||
log.debug("searchResult为空,跳过补救逻辑,faceId={}", faceId);
|
||||
return StageResult.skipped("searchResult为空");
|
||||
}
|
||||
|
||||
try {
|
||||
// 执行补救逻辑(补救逻辑内部会判断是否需要触发)
|
||||
SearchFaceRespVo recoveredResult = faceRecoveryStrategy.executeFaceRecoveryLogic(
|
||||
searchResult,
|
||||
context.getScenicConfig(),
|
||||
context.getFaceBodyAdapter(),
|
||||
context.getFace().getScenicId());
|
||||
|
||||
// 如果结果发生变化,说明触发了补救
|
||||
if (recoveredResult != searchResult) {
|
||||
context.setSearchResult(recoveredResult);
|
||||
log.info("触发补救逻辑,重新搜索: faceId={}", faceId);
|
||||
return StageResult.degraded("触发补救逻辑,重新搜索");
|
||||
}
|
||||
|
||||
return StageResult.success("无需补救");
|
||||
|
||||
} catch (Exception e) {
|
||||
log.error("补救逻辑执行失败,faceId={}", faceId, e);
|
||||
// 补救失败不影响主流程,返回降级
|
||||
return StageResult.degraded("补救逻辑执行失败: " + e.getMessage());
|
||||
}
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,229 @@
|
||||
package com.ycwl.basic.face.pipeline.stages;
|
||||
|
||||
import com.ycwl.basic.face.pipeline.core.FaceMatchingContext;
|
||||
import com.ycwl.basic.pipeline.annotation.StageConfig;
|
||||
import com.ycwl.basic.pipeline.core.AbstractPipelineStage;
|
||||
import com.ycwl.basic.pipeline.core.StageResult;
|
||||
import com.ycwl.basic.pipeline.enums.StageOptionalMode;
|
||||
import com.ycwl.basic.integration.common.manager.DeviceConfigManager;
|
||||
import com.ycwl.basic.model.pc.faceSample.entity.FaceSampleEntity;
|
||||
import com.ycwl.basic.repository.DeviceRepository;
|
||||
import lombok.extern.slf4j.Slf4j;
|
||||
import org.springframework.beans.factory.annotation.Autowired;
|
||||
import org.springframework.stereotype.Component;
|
||||
|
||||
import java.util.*;
|
||||
import java.util.stream.Collectors;
|
||||
|
||||
/**
|
||||
* 按设备照片数量限制筛选样本Stage
|
||||
* 负责根据设备配置的照片数量限制(limit_photo)筛选匹配样本
|
||||
*
|
||||
* 职责:
|
||||
* 1. 从context.faceSamples读取样本实体缓存
|
||||
* 2. 按设备ID分组
|
||||
* 3. 对每个设备,根据其limit_photo配置筛选样本:
|
||||
* - 如果样本数 > limit_photo + 2: 按时间排序,去掉首尾,保留中间limit_photo张
|
||||
* - 如果样本数 > limit_photo + 1: 按时间排序,去掉尾部,保留前limit_photo张
|
||||
* - 如果样本数 > limit_photo: 保留前limit_photo张
|
||||
* - 否则: 保留全部
|
||||
* 4. 更新context.sampleListIds
|
||||
*
|
||||
* 前置条件: context.faceSamples不为空 (由LoadMatchedSamplesStage加载)
|
||||
* 配置说明: limit_photo=0或null表示不限制数量
|
||||
*/
|
||||
@Slf4j
|
||||
@Component
|
||||
@StageConfig(
|
||||
stageId = "filter_by_device_photo_limit",
|
||||
optionalMode = StageOptionalMode.SUPPORT,
|
||||
description = "按设备照片数量限制筛选样本",
|
||||
defaultEnabled = true
|
||||
)
|
||||
public class FilterByDevicePhotoLimitStage extends AbstractPipelineStage<FaceMatchingContext> {
|
||||
|
||||
@Autowired
|
||||
private DeviceRepository deviceRepository;
|
||||
|
||||
@Override
|
||||
public String getName() {
|
||||
return "FilterByDevicePhotoLimit";
|
||||
}
|
||||
|
||||
@Override
|
||||
protected boolean shouldExecuteByBusinessLogic(FaceMatchingContext context) {
|
||||
// 检查faceSamples是否为空
|
||||
List<FaceSampleEntity> faceSamples = context.getFaceSamples();
|
||||
if (faceSamples == null || faceSamples.isEmpty()) {
|
||||
log.debug("faceSamples为空,跳过设备照片限制筛选,faceId={}", context.getFaceId());
|
||||
return false;
|
||||
}
|
||||
return true;
|
||||
}
|
||||
|
||||
@Override
|
||||
protected StageResult<FaceMatchingContext> doExecute(FaceMatchingContext context) {
|
||||
List<FaceSampleEntity> faceSamples = context.getFaceSamples();
|
||||
List<Long> sampleListIds = context.getSampleListIds();
|
||||
Long faceId = context.getFaceId();
|
||||
|
||||
// 防御性检查:faceSamples为空
|
||||
if (faceSamples == null || faceSamples.isEmpty()) {
|
||||
log.debug("faceSamples为空,跳过设备照片限制筛选,faceId={}", faceId);
|
||||
return StageResult.skipped("faceSamples为空");
|
||||
}
|
||||
|
||||
try {
|
||||
// 1. 构建样本ID到实体的映射
|
||||
Map<Long, FaceSampleEntity> sampleMap = faceSamples.stream()
|
||||
.collect(Collectors.toMap(FaceSampleEntity::getId, sample -> sample, (a, b) -> a));
|
||||
|
||||
// 2. 按设备ID分组
|
||||
Map<Long, List<FaceSampleEntity>> deviceSamplesMap = new LinkedHashMap<>();
|
||||
Set<Long> passthroughSampleIds = new LinkedHashSet<>();
|
||||
|
||||
for (Long sampleId : sampleListIds) {
|
||||
FaceSampleEntity sample = sampleMap.get(sampleId);
|
||||
if (sample == null || sample.getDeviceId() == null) {
|
||||
passthroughSampleIds.add(sampleId); // 无设备ID的样本直接保留
|
||||
continue;
|
||||
}
|
||||
deviceSamplesMap
|
||||
.computeIfAbsent(sample.getDeviceId(), key -> new ArrayList<>())
|
||||
.add(sample);
|
||||
}
|
||||
|
||||
// 3. 对每个设备应用照片数量限制
|
||||
Map<Long, Integer> limitCache = new HashMap<>();
|
||||
Set<Long> retainedSampleIds = new LinkedHashSet<>(passthroughSampleIds);
|
||||
|
||||
for (Map.Entry<Long, List<FaceSampleEntity>> entry : deviceSamplesMap.entrySet()) {
|
||||
Long deviceId = entry.getKey();
|
||||
List<FaceSampleEntity> deviceSamples = entry.getValue();
|
||||
|
||||
// 读取设备配置
|
||||
Integer limitPhoto = limitCache.computeIfAbsent(deviceId, id -> {
|
||||
DeviceConfigManager deviceConfig = deviceRepository.getDeviceConfigManager(id);
|
||||
return deviceConfig != null ? deviceConfig.getInteger("limit_photo") : null;
|
||||
});
|
||||
|
||||
List<Long> retainedForDevice = applyLimitForDevice(deviceId, deviceSamples, limitPhoto);
|
||||
retainedSampleIds.addAll(retainedForDevice);
|
||||
}
|
||||
|
||||
// 4. 按原始顺序保留筛选后的样本ID
|
||||
List<Long> resultIds = sampleListIds.stream()
|
||||
.filter(retainedSampleIds::contains)
|
||||
.collect(Collectors.toList());
|
||||
|
||||
// 5. 更新context
|
||||
context.setSampleListIds(resultIds);
|
||||
|
||||
log.info("设备照片数量限制筛选完成: faceId={}, 原始样本数={}, 筛选后数={}",
|
||||
faceId, sampleListIds.size(), resultIds.size());
|
||||
|
||||
return StageResult.success(String.format("设备限制筛选: %d → %d",
|
||||
sampleListIds.size(), resultIds.size()));
|
||||
|
||||
} catch (Exception e) {
|
||||
log.error("设备照片数量限制筛选失败,faceId={}", faceId, e);
|
||||
// 筛选失败不影响主流程,返回降级
|
||||
return StageResult.degraded("设备照片数量限制筛选失败: " + e.getMessage());
|
||||
}
|
||||
}
|
||||
|
||||
/**
|
||||
* 对单个设备的样本应用照片数量限制
|
||||
*/
|
||||
private List<Long> applyLimitForDevice(Long deviceId, List<FaceSampleEntity> deviceSamples, Integer limitPhoto) {
|
||||
List<Long> deviceSampleIds = deviceSamples.stream()
|
||||
.map(FaceSampleEntity::getId)
|
||||
.collect(Collectors.toList());
|
||||
|
||||
// 无限制或限制数量<=0,保留全部
|
||||
if (limitPhoto == null || limitPhoto <= 0) {
|
||||
log.debug("设备照片限制: 设备ID={}, 无限制, 保留{}张照片", deviceId, deviceSampleIds.size());
|
||||
return deviceSampleIds;
|
||||
}
|
||||
|
||||
int sampleCount = deviceSamples.size();
|
||||
|
||||
// 样本数 > limit_photo + 2: 按时间排序,去掉首尾
|
||||
if (sampleCount > (limitPhoto + 2)) {
|
||||
List<Long> retained = processDeviceSamples(deviceSamples, limitPhoto, true);
|
||||
log.debug("设备照片限制: 设备ID={}, 限制={}张, 原始{}张, 去首尾后最终{}张",
|
||||
deviceId, limitPhoto, sampleCount, retained.size());
|
||||
return retained;
|
||||
}
|
||||
|
||||
// 样本数 > limit_photo + 1: 按时间排序,去掉尾部
|
||||
if (sampleCount > (limitPhoto + 1)) {
|
||||
List<Long> retained = processDeviceSamples(deviceSamples, limitPhoto, false);
|
||||
log.debug("设备照片限制: 设备ID={}, 限制={}张, 原始{}张, 去尾部后最终{}张",
|
||||
deviceId, limitPhoto, sampleCount, retained.size());
|
||||
return retained;
|
||||
}
|
||||
|
||||
// 样本数 > limit_photo: 保留前limit_photo张
|
||||
if (sampleCount > limitPhoto) {
|
||||
List<Long> retained = deviceSamples.stream()
|
||||
.limit(limitPhoto)
|
||||
.map(FaceSampleEntity::getId)
|
||||
.collect(Collectors.toList());
|
||||
log.debug("设备照片限制: 设备ID={}, 限制={}张, 原始{}张, 取前{}张",
|
||||
deviceId, limitPhoto, sampleCount, retained.size());
|
||||
return retained;
|
||||
}
|
||||
|
||||
// 样本数 <= limit_photo: 保留全部
|
||||
log.debug("设备照片限制: 设备ID={}, 限制={}张, 原始{}张, 无需筛选, 保留全部",
|
||||
deviceId, limitPhoto, sampleCount);
|
||||
return deviceSampleIds;
|
||||
}
|
||||
|
||||
/**
|
||||
* 处理设备样本,根据参数决定是否去掉首尾
|
||||
*
|
||||
* @param deviceSamples 设备样本列表
|
||||
* @param limitPhoto 限制数量
|
||||
* @param removeBoth 是否去掉首尾,true去掉首尾,false只去掉尾部
|
||||
* @return 处理后的样本ID列表
|
||||
*/
|
||||
private List<Long> processDeviceSamples(List<FaceSampleEntity> deviceSamples, int limitPhoto, boolean removeBoth) {
|
||||
// 创建原始排序的索引映射,用于后续恢复排序
|
||||
Map<Long, Integer> originalIndexMap = new HashMap<>();
|
||||
for (int i = 0; i < deviceSamples.size(); i++) {
|
||||
originalIndexMap.put(deviceSamples.get(i).getId(), i);
|
||||
}
|
||||
|
||||
// 按创建时间排序
|
||||
List<FaceSampleEntity> sortedByCreateTime = deviceSamples.stream()
|
||||
.sorted(Comparator.comparing(FaceSampleEntity::getCreateAt))
|
||||
.collect(Collectors.toList());
|
||||
|
||||
// 根据参数决定去掉首尾还是只去掉尾部
|
||||
List<FaceSampleEntity> filteredSamples;
|
||||
if (removeBoth && sortedByCreateTime.size() > 2) {
|
||||
// 去掉首尾
|
||||
filteredSamples = sortedByCreateTime.subList(1, sortedByCreateTime.size() - 1);
|
||||
} else if (!removeBoth && sortedByCreateTime.size() > 1) {
|
||||
// 只去掉尾部
|
||||
filteredSamples = sortedByCreateTime.subList(0, sortedByCreateTime.size() - 1);
|
||||
} else {
|
||||
filteredSamples = sortedByCreateTime;
|
||||
}
|
||||
|
||||
// 取前limitPhoto个
|
||||
List<FaceSampleEntity> limitedSamples = filteredSamples.stream()
|
||||
.limit(limitPhoto)
|
||||
.collect(Collectors.toList());
|
||||
|
||||
// 按原始顺序排序
|
||||
List<Long> resultIds = limitedSamples.stream()
|
||||
.sorted(Comparator.comparing(sample -> originalIndexMap.get(sample.getId())))
|
||||
.map(FaceSampleEntity::getId)
|
||||
.collect(Collectors.toList());
|
||||
|
||||
return resultIds;
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,136 @@
|
||||
package com.ycwl.basic.face.pipeline.stages;
|
||||
|
||||
import com.ycwl.basic.face.pipeline.core.FaceMatchingContext;
|
||||
import com.ycwl.basic.pipeline.annotation.StageConfig;
|
||||
import com.ycwl.basic.pipeline.core.AbstractPipelineStage;
|
||||
import com.ycwl.basic.pipeline.core.StageResult;
|
||||
import com.ycwl.basic.pipeline.enums.StageOptionalMode;
|
||||
import com.ycwl.basic.model.pc.faceSample.entity.FaceSampleEntity;
|
||||
import lombok.extern.slf4j.Slf4j;
|
||||
import org.springframework.stereotype.Component;
|
||||
|
||||
import java.util.*;
|
||||
import java.util.stream.Collectors;
|
||||
|
||||
/**
|
||||
* 按时间范围筛选样本Stage
|
||||
* 负责根据景区配置的游览时间(tour_time)筛选匹配样本
|
||||
*
|
||||
* 职责:
|
||||
* 1. 从context.scenicConfig读取tour_time配置(分钟)
|
||||
* 2. 从context.faceSamples读取样本实体缓存
|
||||
* 3. 找到最新的样本,以其拍摄时间为基准
|
||||
* 4. 筛选出时间范围内(最新样本时间 ± tour_time分钟)的样本
|
||||
* 5. 更新context.sampleListIds
|
||||
*
|
||||
* 前置条件:
|
||||
* - context.faceSamples不为空 (由LoadMatchedSamplesStage加载)
|
||||
* - context.scenicConfig配置了tour_time
|
||||
*
|
||||
* 配置说明: tour_time=0或null表示不限制时间范围
|
||||
*/
|
||||
@Slf4j
|
||||
@Component
|
||||
@StageConfig(
|
||||
stageId = "filter_by_time_range",
|
||||
optionalMode = StageOptionalMode.SUPPORT,
|
||||
description = "按游览时间范围筛选样本",
|
||||
defaultEnabled = true
|
||||
)
|
||||
public class FilterByTimeRangeStage extends AbstractPipelineStage<FaceMatchingContext> {
|
||||
|
||||
@Override
|
||||
public String getName() {
|
||||
return "FilterByTimeRange";
|
||||
}
|
||||
|
||||
@Override
|
||||
protected boolean shouldExecuteByBusinessLogic(FaceMatchingContext context) {
|
||||
// 检查faceSamples是否为空
|
||||
List<FaceSampleEntity> faceSamples = context.getFaceSamples();
|
||||
if (faceSamples == null || faceSamples.isEmpty()) {
|
||||
log.debug("faceSamples为空,跳过时间范围筛选,faceId={}", context.getFaceId());
|
||||
return false;
|
||||
}
|
||||
|
||||
// 检查是否配置了tour_time
|
||||
Integer tourMinutes = context.getScenicConfig() != null
|
||||
? context.getScenicConfig().getInteger("tour_time")
|
||||
: null;
|
||||
if (tourMinutes == null || tourMinutes <= 0) {
|
||||
log.debug("景区未配置tour_time或配置为0,跳过时间范围筛选,faceId={}", context.getFaceId());
|
||||
return false;
|
||||
}
|
||||
|
||||
return true;
|
||||
}
|
||||
|
||||
@Override
|
||||
protected StageResult<FaceMatchingContext> doExecute(FaceMatchingContext context) {
|
||||
List<FaceSampleEntity> faceSamples = context.getFaceSamples();
|
||||
List<Long> sampleListIds = context.getSampleListIds();
|
||||
Long faceId = context.getFaceId();
|
||||
|
||||
// 防御性检查:faceSamples为空
|
||||
if (faceSamples == null || faceSamples.isEmpty()) {
|
||||
log.debug("faceSamples为空,跳过时间范围筛选,faceId={}", faceId);
|
||||
return StageResult.skipped("faceSamples为空");
|
||||
}
|
||||
|
||||
// 防御性检查:tour_time配置
|
||||
Integer tourMinutes = context.getScenicConfig() != null
|
||||
? context.getScenicConfig().getInteger("tour_time")
|
||||
: null;
|
||||
if (tourMinutes == null || tourMinutes <= 0) {
|
||||
log.debug("景区未配置tour_time或配置为0,跳过时间范围筛选,faceId={}", faceId);
|
||||
return StageResult.skipped("未配置tour_time");
|
||||
}
|
||||
|
||||
try {
|
||||
// 1. 构建样本ID到实体的映射
|
||||
Map<Long, FaceSampleEntity> sampleMap = faceSamples.stream()
|
||||
.collect(Collectors.toMap(FaceSampleEntity::getId, sample -> sample, (a, b) -> a));
|
||||
|
||||
// 2. 找到最新的样本(拍摄时间最晚)
|
||||
FaceSampleEntity topMatchSample = faceSamples.stream()
|
||||
.filter(sample -> sample.getCreateAt() != null)
|
||||
.max(Comparator.comparing(FaceSampleEntity::getCreateAt))
|
||||
.orElse(null);
|
||||
|
||||
if (topMatchSample == null || topMatchSample.getCreateAt() == null) {
|
||||
log.warn("未找到有效的样本拍摄时间,保留所有样本,faceId={}", faceId);
|
||||
return StageResult.success("样本无拍摄时间,保留所有");
|
||||
}
|
||||
|
||||
Date referenceTime = topMatchSample.getCreateAt();
|
||||
long referenceMillis = referenceTime.getTime();
|
||||
long tourMillis = tourMinutes * 60 * 1000L;
|
||||
|
||||
// 3. 筛选时间范围内的样本
|
||||
List<Long> filteredIds = sampleListIds.stream()
|
||||
.filter(sampleId -> {
|
||||
FaceSampleEntity sample = sampleMap.get(sampleId);
|
||||
if (sample == null || sample.getCreateAt() == null) {
|
||||
return false; // 无时间信息的样本被过滤
|
||||
}
|
||||
long timeDiff = Math.abs(sample.getCreateAt().getTime() - referenceMillis);
|
||||
return timeDiff <= tourMillis;
|
||||
})
|
||||
.collect(Collectors.toList());
|
||||
|
||||
// 4. 更新context
|
||||
context.setSampleListIds(filteredIds);
|
||||
|
||||
log.info("时间范围筛选完成: faceId={}, tour_time={}分钟, 原始样本数={}, 筛选后数={}",
|
||||
faceId, tourMinutes, sampleListIds.size(), filteredIds.size());
|
||||
|
||||
return StageResult.success(String.format("时间筛选: %d → %d",
|
||||
sampleListIds.size(), filteredIds.size()));
|
||||
|
||||
} catch (Exception e) {
|
||||
log.error("时间范围筛选失败,faceId={}", faceId, e);
|
||||
// 筛选失败不影响主流程,返回降级
|
||||
return StageResult.degraded("时间范围筛选失败: " + e.getMessage());
|
||||
}
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,66 @@
|
||||
package com.ycwl.basic.face.pipeline.stages;
|
||||
|
||||
import com.ycwl.basic.face.pipeline.core.FaceMatchingContext;
|
||||
import com.ycwl.basic.pipeline.annotation.StageConfig;
|
||||
import com.ycwl.basic.pipeline.core.AbstractPipelineStage;
|
||||
import com.ycwl.basic.pipeline.core.StageResult;
|
||||
import com.ycwl.basic.pipeline.enums.StageOptionalMode;
|
||||
import com.ycwl.basic.face.pipeline.helper.PuzzleGenerationOrchestrator;
|
||||
import lombok.extern.slf4j.Slf4j;
|
||||
import org.springframework.beans.factory.annotation.Autowired;
|
||||
import org.springframework.stereotype.Component;
|
||||
|
||||
/**
|
||||
* 生成拼图模板Stage
|
||||
* 负责触发景区拼图模板的异步生成任务
|
||||
*
|
||||
* 职责:
|
||||
* 1. 从context读取必要参数(scenicId, faceId, memberId, faceUrl)
|
||||
* 2. 调用puzzleOrchestrator.generateAllTemplatesAsync()触发异步生成
|
||||
* 3. 立即返回,不等待生成完成
|
||||
*
|
||||
* 业务说明:
|
||||
* - 拼图生成是异步的,不影响主流程
|
||||
* - 具体的拼图生成逻辑由PuzzleGenerationOrchestrator负责
|
||||
* - Stage只负责触发任务,符合"薄Stage,厚Service"原则
|
||||
*/
|
||||
@Slf4j
|
||||
@Component
|
||||
@StageConfig(
|
||||
stageId = "generate_puzzle",
|
||||
optionalMode = StageOptionalMode.SUPPORT,
|
||||
description = "异步生成拼图模板",
|
||||
defaultEnabled = true
|
||||
)
|
||||
public class GeneratePuzzleStage extends AbstractPipelineStage<FaceMatchingContext> {
|
||||
|
||||
@Autowired
|
||||
private PuzzleGenerationOrchestrator puzzleOrchestrator;
|
||||
|
||||
@Override
|
||||
public String getName() {
|
||||
return "GeneratePuzzle";
|
||||
}
|
||||
|
||||
@Override
|
||||
protected StageResult<FaceMatchingContext> doExecute(FaceMatchingContext context) {
|
||||
Long scenicId = context.getFace().getScenicId();
|
||||
Long faceId = context.getFaceId();
|
||||
Long memberId = context.getFace().getMemberId();
|
||||
String faceUrl = context.getFace().getFaceUrl();
|
||||
|
||||
try {
|
||||
// 触发异步生成拼图模板
|
||||
puzzleOrchestrator.generateAllTemplatesAsync(scenicId, faceId, memberId, faceUrl);
|
||||
|
||||
log.debug("拼图模板异步生成任务已提交: scenicId={}, faceId={}", scenicId, faceId);
|
||||
|
||||
return StageResult.success("拼图模板已提交异步生成");
|
||||
|
||||
} catch (Exception e) {
|
||||
log.error("提交拼图生成任务失败: scenicId={}, faceId={}", scenicId, faceId, e);
|
||||
// 拼图生成失败不影响主流程,返回降级
|
||||
return StageResult.degraded("提交拼图生成任务失败: " + e.getMessage());
|
||||
}
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,92 @@
|
||||
package com.ycwl.basic.face.pipeline.stages;
|
||||
|
||||
import com.ycwl.basic.face.pipeline.core.FaceMatchingContext;
|
||||
import com.ycwl.basic.pipeline.annotation.StageConfig;
|
||||
import com.ycwl.basic.pipeline.core.AbstractPipelineStage;
|
||||
import com.ycwl.basic.pipeline.core.StageResult;
|
||||
import com.ycwl.basic.pipeline.enums.StageOptionalMode;
|
||||
import com.ycwl.basic.model.pc.source.entity.MemberSourceEntity;
|
||||
import com.ycwl.basic.service.pc.processor.VideoRecreationHandler;
|
||||
import lombok.extern.slf4j.Slf4j;
|
||||
import org.springframework.beans.factory.annotation.Autowired;
|
||||
import org.springframework.stereotype.Component;
|
||||
|
||||
import java.util.List;
|
||||
|
||||
/**
|
||||
* 处理视频重切Stage
|
||||
* 负责触发视频重新切片处理
|
||||
*
|
||||
* 职责:
|
||||
* 1. 从context读取必要参数(scenicId, memberSourceList, faceId, memberId, sampleListIds, isNew)
|
||||
* 2. 调用videoRecreationHandler.handleVideoRecreation()触发视频重切
|
||||
*
|
||||
* 前置条件: context.memberSourceList不为空
|
||||
* 业务说明: 视频重切用于根据人脸识别结果重新生成个性化视频片段
|
||||
*/
|
||||
@Slf4j
|
||||
@Component
|
||||
@StageConfig(
|
||||
stageId = "handle_video_recreation",
|
||||
optionalMode = StageOptionalMode.SUPPORT,
|
||||
description = "处理视频重切逻辑",
|
||||
defaultEnabled = true
|
||||
)
|
||||
public class HandleVideoRecreationStage extends AbstractPipelineStage<FaceMatchingContext> {
|
||||
|
||||
@Autowired
|
||||
private VideoRecreationHandler videoRecreationHandler;
|
||||
|
||||
@Override
|
||||
public String getName() {
|
||||
return "HandleVideoRecreation";
|
||||
}
|
||||
|
||||
@Override
|
||||
protected boolean shouldExecuteByBusinessLogic(FaceMatchingContext context) {
|
||||
// 只有当memberSourceList不为空时才执行
|
||||
List<MemberSourceEntity> memberSourceList = context.getMemberSourceList();
|
||||
if (memberSourceList == null || memberSourceList.isEmpty()) {
|
||||
log.debug("memberSourceList为空,跳过视频重切,faceId={}", context.getFaceId());
|
||||
return false;
|
||||
}
|
||||
return true;
|
||||
}
|
||||
|
||||
@Override
|
||||
protected StageResult<FaceMatchingContext> doExecute(FaceMatchingContext context) {
|
||||
Long scenicId = context.getFace().getScenicId();
|
||||
List<MemberSourceEntity> memberSourceEntityList = context.getMemberSourceList();
|
||||
Long faceId = context.getFaceId();
|
||||
Long memberId = context.getFace().getMemberId();
|
||||
List<Long> sampleListIds = context.getSampleListIds();
|
||||
boolean isNew = context.isNew();
|
||||
|
||||
// 防御性检查:memberSourceList为空
|
||||
if (memberSourceEntityList == null || memberSourceEntityList.isEmpty()) {
|
||||
log.debug("memberSourceList为空,跳过视频重切,faceId={}", faceId);
|
||||
return StageResult.skipped("memberSourceList为空");
|
||||
}
|
||||
|
||||
try {
|
||||
// 处理视频重切
|
||||
videoRecreationHandler.handleVideoRecreation(
|
||||
scenicId,
|
||||
memberSourceEntityList,
|
||||
faceId,
|
||||
memberId,
|
||||
sampleListIds,
|
||||
isNew);
|
||||
|
||||
log.info("视频重切处理完成: faceId={}, scenicId={}, 源文件数={}",
|
||||
faceId, scenicId, memberSourceEntityList.size());
|
||||
|
||||
return StageResult.success("视频重切处理完成");
|
||||
|
||||
} catch (Exception e) {
|
||||
log.error("处理视频重切失败,faceId={}", faceId, e);
|
||||
// 视频重切失败不影响主流程,返回降级
|
||||
return StageResult.degraded("视频重切处理失败: " + e.getMessage());
|
||||
}
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,73 @@
|
||||
package com.ycwl.basic.face.pipeline.stages;
|
||||
|
||||
import com.ycwl.basic.exception.BaseException;
|
||||
import com.ycwl.basic.face.pipeline.core.FaceMatchingContext;
|
||||
import com.ycwl.basic.pipeline.annotation.StageConfig;
|
||||
import com.ycwl.basic.pipeline.core.AbstractPipelineStage;
|
||||
import com.ycwl.basic.pipeline.core.StageResult;
|
||||
import com.ycwl.basic.pipeline.enums.StageOptionalMode;
|
||||
import com.ycwl.basic.mapper.FaceSampleMapper;
|
||||
import com.ycwl.basic.model.pc.faceSample.entity.FaceSampleEntity;
|
||||
import lombok.extern.slf4j.Slf4j;
|
||||
import org.springframework.beans.factory.annotation.Autowired;
|
||||
import org.springframework.stereotype.Component;
|
||||
|
||||
import java.util.List;
|
||||
|
||||
/**
|
||||
* 加载人脸样本Stage
|
||||
* 负责加载用户选择的人脸样本数据
|
||||
*
|
||||
* 职责:
|
||||
* 1. 从context.faceSampleIds读取用户选择的样本ID列表
|
||||
* 2. 调用faceSampleMapper.listByIds()加载样本实体
|
||||
* 3. 更新context.faceSamples
|
||||
*/
|
||||
@Slf4j
|
||||
@Component
|
||||
@StageConfig(
|
||||
stageId = "load_face_samples",
|
||||
optionalMode = StageOptionalMode.FORCE_ON,
|
||||
description = "加载用户选择的人脸样本"
|
||||
)
|
||||
public class LoadFaceSamplesStage extends AbstractPipelineStage<FaceMatchingContext> {
|
||||
|
||||
@Autowired
|
||||
private FaceSampleMapper faceSampleMapper;
|
||||
|
||||
@Override
|
||||
public String getName() {
|
||||
return "LoadFaceSamples";
|
||||
}
|
||||
|
||||
@Override
|
||||
protected StageResult<FaceMatchingContext> doExecute(FaceMatchingContext context) {
|
||||
List<Long> faceSampleIds = context.getFaceSampleIds();
|
||||
Long faceId = context.getFaceId();
|
||||
|
||||
if (faceSampleIds == null || faceSampleIds.isEmpty()) {
|
||||
log.warn("faceSampleIds为空,faceId={}", faceId);
|
||||
return StageResult.failed("faceSampleIds不能为空");
|
||||
}
|
||||
|
||||
try {
|
||||
List<FaceSampleEntity> faceSamples = faceSampleMapper.listByIds(faceSampleIds);
|
||||
|
||||
if (faceSamples.isEmpty()) {
|
||||
log.warn("未找到指定的人脸样本,faceSampleIds: {}, faceId={}", faceSampleIds, faceId);
|
||||
throw new BaseException("未找到指定的人脸样本");
|
||||
}
|
||||
|
||||
context.setFaceSamples(faceSamples);
|
||||
|
||||
log.info("加载人脸样本成功: faceId={}, sampleCount={}", faceId, faceSamples.size());
|
||||
return StageResult.success(String.format("加载了%d个人脸样本", faceSamples.size()));
|
||||
|
||||
} catch (BaseException e) {
|
||||
throw e;
|
||||
} catch (Exception e) {
|
||||
log.error("加载人脸样本失败,faceId={}, faceSampleIds={}", faceId, faceSampleIds, e);
|
||||
return StageResult.failed("加载人脸样本失败", e);
|
||||
}
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,95 @@
|
||||
package com.ycwl.basic.face.pipeline.stages;
|
||||
|
||||
import com.ycwl.basic.face.pipeline.core.FaceMatchingContext;
|
||||
import com.ycwl.basic.pipeline.annotation.StageConfig;
|
||||
import com.ycwl.basic.pipeline.core.AbstractPipelineStage;
|
||||
import com.ycwl.basic.pipeline.core.StageResult;
|
||||
import com.ycwl.basic.pipeline.enums.StageOptionalMode;
|
||||
import com.ycwl.basic.mapper.FaceSampleMapper;
|
||||
import com.ycwl.basic.model.pc.faceSample.entity.FaceSampleEntity;
|
||||
import lombok.extern.slf4j.Slf4j;
|
||||
import org.springframework.beans.factory.annotation.Autowired;
|
||||
import org.springframework.stereotype.Component;
|
||||
|
||||
import java.util.List;
|
||||
|
||||
/**
|
||||
* 加载匹配样本实体Stage
|
||||
* 负责将sampleListIds对应的样本实体加载到context.faceSamples,供后续Stage使用
|
||||
*
|
||||
* 职责:
|
||||
* 1. 从context.sampleListIds读取匹配到的样本ID列表
|
||||
* 2. 调用faceSampleMapper.listByIds()批量加载样本实体
|
||||
* 3. 更新context.faceSamples作为样本实体缓存
|
||||
*
|
||||
* 设计目的:
|
||||
* - 避免后续多个Stage重复调用faceSampleMapper.listByIds()
|
||||
* - 统一加载时机,提高性能
|
||||
* - 为后续筛选Stage提供样本实体数据源
|
||||
*
|
||||
* 前置条件: context.sampleListIds不为空
|
||||
*
|
||||
* 应用场景: 自定义匹配场景,在CustomFaceSearchStage之后
|
||||
*/
|
||||
@Slf4j
|
||||
@Component
|
||||
@StageConfig(
|
||||
stageId = "load_matched_samples",
|
||||
optionalMode = StageOptionalMode.UNSUPPORT,
|
||||
description = "加载匹配样本实体到缓存"
|
||||
)
|
||||
public class LoadMatchedSamplesStage extends AbstractPipelineStage<FaceMatchingContext> {
|
||||
|
||||
@Autowired
|
||||
private FaceSampleMapper faceSampleMapper;
|
||||
|
||||
@Override
|
||||
public String getName() {
|
||||
return "LoadMatchedSamples";
|
||||
}
|
||||
|
||||
@Override
|
||||
protected boolean shouldExecuteByBusinessLogic(FaceMatchingContext context) {
|
||||
// 检查sampleListIds是否为空
|
||||
List<Long> sampleListIds = context.getSampleListIds();
|
||||
if (sampleListIds == null || sampleListIds.isEmpty()) {
|
||||
log.debug("sampleListIds为空,跳过加载匹配样本,faceId={}", context.getFaceId());
|
||||
return false;
|
||||
}
|
||||
return true;
|
||||
}
|
||||
|
||||
@Override
|
||||
protected StageResult<FaceMatchingContext> doExecute(FaceMatchingContext context) {
|
||||
List<Long> sampleListIds = context.getSampleListIds();
|
||||
Long faceId = context.getFaceId();
|
||||
|
||||
// 防御性检查:如果sampleListIds为空,直接跳过
|
||||
if (sampleListIds == null || sampleListIds.isEmpty()) {
|
||||
log.debug("sampleListIds为空,跳过加载匹配样本,faceId={}", faceId);
|
||||
return StageResult.skipped("sampleListIds为空");
|
||||
}
|
||||
|
||||
try {
|
||||
// 批量加载样本实体
|
||||
List<FaceSampleEntity> faceSamples = faceSampleMapper.listByIds(sampleListIds);
|
||||
|
||||
if (faceSamples == null || faceSamples.isEmpty()) {
|
||||
log.warn("未找到任何匹配样本实体,faceId={}, sampleListIds={}", faceId, sampleListIds);
|
||||
return StageResult.skipped("未找到匹配样本实体");
|
||||
}
|
||||
|
||||
// 存入context缓存,供后续Stage使用
|
||||
context.setFaceSamples(faceSamples);
|
||||
|
||||
log.info("加载匹配样本实体完成: faceId={}, 样本数={}", faceId, faceSamples.size());
|
||||
|
||||
return StageResult.success(String.format("已加载%d个样本实体", faceSamples.size()));
|
||||
|
||||
} catch (Exception e) {
|
||||
log.error("加载匹配样本实体失败,faceId={}", faceId, e);
|
||||
// 加载失败影响后续流程,返回失败
|
||||
return StageResult.failed("加载匹配样本实体失败: " + e.getMessage());
|
||||
}
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,98 @@
|
||||
package com.ycwl.basic.face.pipeline.stages;
|
||||
|
||||
import com.ycwl.basic.face.pipeline.core.FaceMatchingContext;
|
||||
import com.ycwl.basic.pipeline.annotation.StageConfig;
|
||||
import com.ycwl.basic.pipeline.core.AbstractPipelineStage;
|
||||
import com.ycwl.basic.pipeline.core.StageResult;
|
||||
import com.ycwl.basic.pipeline.enums.StageOptionalMode;
|
||||
import com.ycwl.basic.mapper.SourceMapper;
|
||||
import com.ycwl.basic.model.pc.source.entity.MemberSourceEntity;
|
||||
import com.ycwl.basic.repository.MemberRelationRepository;
|
||||
import lombok.extern.slf4j.Slf4j;
|
||||
import org.springframework.beans.factory.annotation.Autowired;
|
||||
import org.springframework.stereotype.Component;
|
||||
|
||||
import java.util.List;
|
||||
|
||||
/**
|
||||
* 持久化关联关系Stage
|
||||
* 负责过滤并保存源文件关联关系到数据库
|
||||
*
|
||||
* 职责:
|
||||
* 1. 从context.memberSourceList读取关联关系
|
||||
* 2. 过滤已存在的关联关系和无效的source引用
|
||||
* 3. 保存到数据库
|
||||
* 4. 清除缓存
|
||||
*/
|
||||
@Slf4j
|
||||
@Component
|
||||
@StageConfig(
|
||||
stageId = "persist_relations",
|
||||
optionalMode = StageOptionalMode.FORCE_ON,
|
||||
description = "持久化源文件关联关系"
|
||||
)
|
||||
public class PersistRelationsStage extends AbstractPipelineStage<FaceMatchingContext> {
|
||||
|
||||
@Autowired
|
||||
private SourceMapper sourceMapper;
|
||||
|
||||
@Autowired
|
||||
private MemberRelationRepository memberRelationRepository;
|
||||
|
||||
@Override
|
||||
public String getName() {
|
||||
return "PersistRelations";
|
||||
}
|
||||
|
||||
@Override
|
||||
protected boolean shouldExecuteByBusinessLogic(FaceMatchingContext context) {
|
||||
// 只有当memberSourceList不为空时才执行
|
||||
List<MemberSourceEntity> memberSourceList = context.getMemberSourceList();
|
||||
if (memberSourceList == null || memberSourceList.isEmpty()) {
|
||||
log.debug("memberSourceList为空,跳过持久化,faceId={}", context.getFaceId());
|
||||
return false;
|
||||
}
|
||||
return true;
|
||||
}
|
||||
|
||||
@Override
|
||||
protected StageResult<FaceMatchingContext> doExecute(FaceMatchingContext context) {
|
||||
List<MemberSourceEntity> memberSourceEntityList = context.getMemberSourceList();
|
||||
Long faceId = context.getFaceId();
|
||||
|
||||
// 防御性检查:memberSourceList为空
|
||||
if (memberSourceEntityList == null || memberSourceEntityList.isEmpty()) {
|
||||
log.debug("memberSourceList为空,跳过持久化,faceId={}", faceId);
|
||||
return StageResult.skipped("memberSourceList为空");
|
||||
}
|
||||
|
||||
try {
|
||||
// 1. 过滤已存在的关联关系
|
||||
List<MemberSourceEntity> existingFiltered = sourceMapper.filterExistingRelations(memberSourceEntityList);
|
||||
|
||||
// 2. 过滤无效的source引用
|
||||
List<MemberSourceEntity> validFiltered = sourceMapper.filterValidSourceRelations(existingFiltered);
|
||||
|
||||
if (!validFiltered.isEmpty()) {
|
||||
// 3. 保存到数据库
|
||||
sourceMapper.addRelations(validFiltered);
|
||||
|
||||
log.debug("创建关联关系: faceId={}, 原始数量={}, 过滤后数量={}",
|
||||
faceId, memberSourceEntityList.size(), validFiltered.size());
|
||||
} else {
|
||||
log.warn("没有有效的关联关系可创建: faceId={}, 原始数量={}",
|
||||
faceId, memberSourceEntityList.size());
|
||||
return StageResult.skipped("没有有效的关联关系可创建");
|
||||
}
|
||||
|
||||
// 4. 清除缓存
|
||||
memberRelationRepository.clearSCacheByFace(faceId);
|
||||
|
||||
return StageResult.success(String.format("持久化了%d条关联关系", validFiltered.size()));
|
||||
|
||||
} catch (Exception e) {
|
||||
log.error("持久化关联关系失败,faceId={}", faceId, e);
|
||||
return StageResult.failed("保存关联关系失败", e);
|
||||
}
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,91 @@
|
||||
package com.ycwl.basic.face.pipeline.stages;
|
||||
|
||||
import com.ycwl.basic.face.pipeline.core.FaceMatchingContext;
|
||||
import com.ycwl.basic.pipeline.annotation.StageConfig;
|
||||
import com.ycwl.basic.pipeline.core.AbstractPipelineStage;
|
||||
import com.ycwl.basic.pipeline.core.StageResult;
|
||||
import com.ycwl.basic.pipeline.enums.StageOptionalMode;
|
||||
import com.ycwl.basic.facebody.adapter.IFaceBodyAdapter;
|
||||
import com.ycwl.basic.integration.common.manager.ScenicConfigManager;
|
||||
import com.ycwl.basic.model.pc.face.entity.FaceEntity;
|
||||
import com.ycwl.basic.repository.FaceRepository;
|
||||
import com.ycwl.basic.repository.ScenicRepository;
|
||||
import com.ycwl.basic.service.pc.ScenicService;
|
||||
import lombok.extern.slf4j.Slf4j;
|
||||
import org.springframework.beans.factory.annotation.Autowired;
|
||||
import org.springframework.stereotype.Component;
|
||||
|
||||
/**
|
||||
* 准备上下文Stage
|
||||
* 负责加载人脸实体、景区配置、识别适配器等必要数据
|
||||
*
|
||||
* 职责:
|
||||
* 1. 加载FaceEntity(如不存在则失败)
|
||||
* 2. 检查是否人工选择(是则跳过,除非isNew=true)
|
||||
* 3. 加载ScenicConfigManager和IFaceBodyAdapter
|
||||
* 4. 更新Context
|
||||
*/
|
||||
@Slf4j
|
||||
@Component
|
||||
@StageConfig(
|
||||
stageId = "prepare_context",
|
||||
optionalMode = StageOptionalMode.FORCE_ON,
|
||||
description = "准备人脸匹配上下文数据"
|
||||
)
|
||||
public class PrepareContextStage extends AbstractPipelineStage<FaceMatchingContext> {
|
||||
|
||||
@Autowired
|
||||
private FaceRepository faceRepository;
|
||||
|
||||
@Autowired
|
||||
private ScenicRepository scenicRepository;
|
||||
|
||||
@Autowired
|
||||
private ScenicService scenicService;
|
||||
|
||||
@Override
|
||||
public String getName() {
|
||||
return "PrepareContext";
|
||||
}
|
||||
|
||||
@Override
|
||||
protected StageResult<FaceMatchingContext> doExecute(FaceMatchingContext context) {
|
||||
Long faceId = context.getFaceId();
|
||||
boolean isNew = context.isNew();
|
||||
|
||||
// 1. 加载人脸实体
|
||||
FaceEntity face = faceRepository.getFace(faceId);
|
||||
if (face == null) {
|
||||
log.warn("人脸不存在,faceId: {}", faceId);
|
||||
return StageResult.failed("人脸不存在,faceId: " + faceId);
|
||||
}
|
||||
|
||||
context.setFace(face);
|
||||
log.debug("加载人脸实体成功: faceId={}, memberId={}, scenicId={}",
|
||||
faceId, face.getMemberId(), face.getScenicId());
|
||||
|
||||
// 2. 检查是否人工选择
|
||||
// 人工选择的无需重新匹配(新用户除外)
|
||||
if (!isNew && Integer.valueOf(1).equals(face.getIsManual())) {
|
||||
log.info("人工选择的人脸,无需匹配,faceId: {}", faceId);
|
||||
return StageResult.skipped("人工选择的人脸,无需重新匹配");
|
||||
}
|
||||
|
||||
// 3. 加载景区配置
|
||||
ScenicConfigManager scenicConfig = scenicRepository.getScenicConfigManager(face.getScenicId());
|
||||
context.setScenicConfig(scenicConfig);
|
||||
log.debug("加载景区配置成功: scenicId={}", face.getScenicId());
|
||||
|
||||
// 4. 加载人脸识别适配器
|
||||
IFaceBodyAdapter faceBodyAdapter = scenicService.getScenicFaceBodyAdapter(face.getScenicId());
|
||||
if (faceBodyAdapter == null) {
|
||||
log.error("无法获取人脸识别适配器,scenicId: {}", face.getScenicId());
|
||||
return StageResult.failed("人脸识别服务不可用,请稍后再试");
|
||||
}
|
||||
|
||||
context.setFaceBodyAdapter(faceBodyAdapter);
|
||||
log.debug("加载人脸识别适配器成功: scenicId={}", face.getScenicId());
|
||||
|
||||
return StageResult.success("上下文准备完成");
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,92 @@
|
||||
package com.ycwl.basic.face.pipeline.stages;
|
||||
|
||||
import com.ycwl.basic.face.pipeline.core.FaceMatchingContext;
|
||||
import com.ycwl.basic.pipeline.annotation.StageConfig;
|
||||
import com.ycwl.basic.pipeline.core.AbstractPipelineStage;
|
||||
import com.ycwl.basic.pipeline.core.StageResult;
|
||||
import com.ycwl.basic.pipeline.enums.StageOptionalMode;
|
||||
import com.ycwl.basic.model.pc.source.entity.MemberSourceEntity;
|
||||
import com.ycwl.basic.service.pc.processor.BuyStatusProcessor;
|
||||
import lombok.extern.slf4j.Slf4j;
|
||||
import org.springframework.beans.factory.annotation.Autowired;
|
||||
import org.springframework.stereotype.Component;
|
||||
|
||||
import java.util.List;
|
||||
|
||||
/**
|
||||
* 处理购买状态Stage
|
||||
* 负责更新源文件的购买状态标记
|
||||
*
|
||||
* 职责:
|
||||
* 1. 从context.memberSourceList读取源文件关联列表
|
||||
* 2. 从context.freeSourceIds读取免费源文件ID列表
|
||||
* 3. 调用buyStatusProcessor.processBuyStatus()更新购买状态
|
||||
*
|
||||
* 前置条件: context.memberSourceList不为空
|
||||
* 业务说明: 购买状态影响前端显示和用户下载权限
|
||||
*/
|
||||
@Slf4j
|
||||
@Component
|
||||
@StageConfig(
|
||||
stageId = "process_buy_status",
|
||||
optionalMode = StageOptionalMode.SUPPORT,
|
||||
description = "处理源文件购买状态",
|
||||
defaultEnabled = true
|
||||
)
|
||||
public class ProcessBuyStatusStage extends AbstractPipelineStage<FaceMatchingContext> {
|
||||
|
||||
@Autowired
|
||||
private BuyStatusProcessor buyStatusProcessor;
|
||||
|
||||
@Override
|
||||
public String getName() {
|
||||
return "ProcessBuyStatus";
|
||||
}
|
||||
|
||||
@Override
|
||||
protected boolean shouldExecuteByBusinessLogic(FaceMatchingContext context) {
|
||||
// 只有当memberSourceList不为空时才执行
|
||||
List<MemberSourceEntity> memberSourceList = context.getMemberSourceList();
|
||||
if (memberSourceList == null || memberSourceList.isEmpty()) {
|
||||
log.debug("memberSourceList为空,跳过购买状态处理,faceId={}", context.getFaceId());
|
||||
return false;
|
||||
}
|
||||
return true;
|
||||
}
|
||||
|
||||
@Override
|
||||
protected StageResult<FaceMatchingContext> doExecute(FaceMatchingContext context) {
|
||||
List<MemberSourceEntity> memberSourceEntityList = context.getMemberSourceList();
|
||||
List<Long> freeSourceIds = context.getFreeSourceIds();
|
||||
Long memberId = context.getFace().getMemberId();
|
||||
Long scenicId = context.getFace().getScenicId();
|
||||
Long faceId = context.getFaceId();
|
||||
|
||||
// 防御性检查:memberSourceList为空
|
||||
if (memberSourceEntityList == null || memberSourceEntityList.isEmpty()) {
|
||||
log.debug("memberSourceList为空,跳过购买状态处理,faceId={}", faceId);
|
||||
return StageResult.skipped("memberSourceList为空");
|
||||
}
|
||||
|
||||
try {
|
||||
// 处理购买状态
|
||||
buyStatusProcessor.processBuyStatus(
|
||||
memberSourceEntityList,
|
||||
freeSourceIds,
|
||||
memberId,
|
||||
scenicId,
|
||||
faceId);
|
||||
|
||||
log.info("购买状态处理完成: faceId={}, 源文件数={}, 免费数={}",
|
||||
faceId, memberSourceEntityList.size(),
|
||||
freeSourceIds != null ? freeSourceIds.size() : 0);
|
||||
|
||||
return StageResult.success("购买状态处理完成");
|
||||
|
||||
} catch (Exception e) {
|
||||
log.error("处理购买状态失败,faceId={}", faceId, e);
|
||||
// 购买状态处理失败不影响主流程,返回降级
|
||||
return StageResult.degraded("购买状态处理失败: " + e.getMessage());
|
||||
}
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,89 @@
|
||||
package com.ycwl.basic.face.pipeline.stages;
|
||||
|
||||
import com.ycwl.basic.face.pipeline.core.FaceMatchingContext;
|
||||
import com.ycwl.basic.pipeline.annotation.StageConfig;
|
||||
import com.ycwl.basic.pipeline.core.AbstractPipelineStage;
|
||||
import com.ycwl.basic.pipeline.core.StageResult;
|
||||
import com.ycwl.basic.pipeline.enums.StageOptionalMode;
|
||||
import com.ycwl.basic.model.pc.source.entity.MemberSourceEntity;
|
||||
import com.ycwl.basic.service.pc.processor.SourceRelationProcessor;
|
||||
import lombok.extern.slf4j.Slf4j;
|
||||
import org.springframework.beans.factory.annotation.Autowired;
|
||||
import org.springframework.stereotype.Component;
|
||||
|
||||
import java.util.List;
|
||||
|
||||
/**
|
||||
* 处理免费源文件Stage
|
||||
* 负责根据业务规则确定哪些源文件可以免费访问
|
||||
*
|
||||
* 职责:
|
||||
* 1. 从context.memberSourceList读取源文件关联列表
|
||||
* 2. 调用sourceRelationProcessor.processFreeSourceLogic()确定免费源文件
|
||||
* 3. 更新context.freeSourceIds
|
||||
*
|
||||
* 前置条件: context.memberSourceList不为空
|
||||
* 后置条件: context.freeSourceIds已设置
|
||||
*/
|
||||
@Slf4j
|
||||
@Component
|
||||
@StageConfig(
|
||||
stageId = "process_free_source",
|
||||
optionalMode = StageOptionalMode.SUPPORT,
|
||||
description = "处理免费源文件逻辑",
|
||||
defaultEnabled = true
|
||||
)
|
||||
public class ProcessFreeSourceStage extends AbstractPipelineStage<FaceMatchingContext> {
|
||||
|
||||
@Autowired
|
||||
private SourceRelationProcessor sourceRelationProcessor;
|
||||
|
||||
@Override
|
||||
public String getName() {
|
||||
return "ProcessFreeSource";
|
||||
}
|
||||
|
||||
@Override
|
||||
protected boolean shouldExecuteByBusinessLogic(FaceMatchingContext context) {
|
||||
// 只有当memberSourceList不为空时才执行
|
||||
List<MemberSourceEntity> memberSourceList = context.getMemberSourceList();
|
||||
if (memberSourceList == null || memberSourceList.isEmpty()) {
|
||||
log.debug("memberSourceList为空,跳过免费逻辑,faceId={}", context.getFaceId());
|
||||
return false;
|
||||
}
|
||||
return true;
|
||||
}
|
||||
|
||||
@Override
|
||||
protected StageResult<FaceMatchingContext> doExecute(FaceMatchingContext context) {
|
||||
List<MemberSourceEntity> memberSourceEntityList = context.getMemberSourceList();
|
||||
Long scenicId = context.getFace().getScenicId();
|
||||
boolean isNew = context.isNew();
|
||||
Long faceId = context.getFaceId();
|
||||
|
||||
// 防御性检查:memberSourceList为空
|
||||
if (memberSourceEntityList == null || memberSourceEntityList.isEmpty()) {
|
||||
log.debug("memberSourceList为空,跳过免费逻辑,faceId={}", faceId);
|
||||
return StageResult.skipped("memberSourceList为空");
|
||||
}
|
||||
|
||||
try {
|
||||
// 处理免费逻辑
|
||||
List<Long> freeSourceIds = sourceRelationProcessor.processFreeSourceLogic(
|
||||
memberSourceEntityList, scenicId, isNew);
|
||||
|
||||
context.setFreeSourceIds(freeSourceIds);
|
||||
|
||||
log.info("免费源文件处理完成: faceId={}, 总源文件数={}, 免费数={}",
|
||||
faceId, memberSourceEntityList.size(), freeSourceIds != null ? freeSourceIds.size() : 0);
|
||||
|
||||
return StageResult.success(String.format("确定了%d个免费源文件",
|
||||
freeSourceIds != null ? freeSourceIds.size() : 0));
|
||||
|
||||
} catch (Exception e) {
|
||||
log.error("处理免费源文件失败,faceId={}", faceId, e);
|
||||
// 免费逻辑失败不影响主流程,返回降级
|
||||
return StageResult.degraded("免费源文件处理失败: " + e.getMessage());
|
||||
}
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,71 @@
|
||||
package com.ycwl.basic.face.pipeline.stages;
|
||||
|
||||
import com.ycwl.basic.face.pipeline.core.FaceMatchingContext;
|
||||
import com.ycwl.basic.face.pipeline.enums.FaceMatchingScene;
|
||||
import com.ycwl.basic.pipeline.annotation.StageConfig;
|
||||
import com.ycwl.basic.pipeline.core.AbstractPipelineStage;
|
||||
import com.ycwl.basic.pipeline.core.StageResult;
|
||||
import com.ycwl.basic.pipeline.enums.StageOptionalMode;
|
||||
import com.ycwl.basic.service.pc.helper.FaceMetricsRecorder;
|
||||
import lombok.extern.slf4j.Slf4j;
|
||||
import org.springframework.beans.factory.annotation.Autowired;
|
||||
import org.springframework.stereotype.Component;
|
||||
|
||||
/**
|
||||
* 记录自定义匹配次数Stage
|
||||
* 负责记录自定义人脸匹配调用次数,用于监控
|
||||
*
|
||||
* 职责:
|
||||
* 1. 仅在CUSTOM_MATCHING场景执行
|
||||
* 2. 调用metricsRecorder.recordCustomMatchCount()记录次数
|
||||
*/
|
||||
@Slf4j
|
||||
@Component
|
||||
@StageConfig(
|
||||
stageId = "record_custom_match_metrics",
|
||||
optionalMode = StageOptionalMode.SUPPORT,
|
||||
description = "记录自定义匹配指标",
|
||||
defaultEnabled = true
|
||||
)
|
||||
public class RecordCustomMatchMetricsStage extends AbstractPipelineStage<FaceMatchingContext> {
|
||||
|
||||
@Autowired
|
||||
private FaceMetricsRecorder metricsRecorder;
|
||||
|
||||
@Override
|
||||
public String getName() {
|
||||
return "RecordCustomMatchMetrics";
|
||||
}
|
||||
|
||||
@Override
|
||||
protected boolean shouldExecuteByBusinessLogic(FaceMatchingContext context) {
|
||||
// 只有自定义匹配场景才执行
|
||||
if (context.getScene() != FaceMatchingScene.CUSTOM_MATCHING) {
|
||||
log.debug("非自定义匹配场景,跳过记录,faceId={}", context.getFaceId());
|
||||
return false;
|
||||
}
|
||||
return true;
|
||||
}
|
||||
|
||||
@Override
|
||||
protected StageResult<FaceMatchingContext> doExecute(FaceMatchingContext context) {
|
||||
Long faceId = context.getFaceId();
|
||||
|
||||
// 防御性检查:只有自定义匹配场景才执行
|
||||
if (context.getScene() != FaceMatchingScene.CUSTOM_MATCHING) {
|
||||
log.debug("非自定义匹配场景,跳过记录,faceId={}", faceId);
|
||||
return StageResult.skipped("非自定义匹配场景");
|
||||
}
|
||||
|
||||
try {
|
||||
metricsRecorder.recordCustomMatchCount(faceId);
|
||||
log.debug("记录自定义匹配次数: faceId={}", faceId);
|
||||
return StageResult.success("自定义匹配指标记录完成");
|
||||
|
||||
} catch (Exception e) {
|
||||
log.error("记录自定义匹配指标失败,faceId={}", faceId, e);
|
||||
// 指标记录失败不影响主流程,返回降级
|
||||
return StageResult.degraded("指标记录失败: " + e.getMessage());
|
||||
}
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,63 @@
|
||||
package com.ycwl.basic.face.pipeline.stages;
|
||||
|
||||
import com.ycwl.basic.face.pipeline.core.FaceMatchingContext;
|
||||
import com.ycwl.basic.pipeline.annotation.StageConfig;
|
||||
import com.ycwl.basic.pipeline.core.AbstractPipelineStage;
|
||||
import com.ycwl.basic.pipeline.core.StageResult;
|
||||
import com.ycwl.basic.pipeline.enums.StageOptionalMode;
|
||||
import com.ycwl.basic.service.pc.helper.FaceMetricsRecorder;
|
||||
import lombok.extern.slf4j.Slf4j;
|
||||
import org.springframework.beans.factory.annotation.Autowired;
|
||||
import org.springframework.stereotype.Component;
|
||||
|
||||
/**
|
||||
* 记录识别次数Stage
|
||||
* 负责记录人脸识别调用次数,用于监控和防重复检查
|
||||
*
|
||||
* 职责:
|
||||
* 1. 调用metricsRecorder.recordRecognitionCount()记录识别次数
|
||||
* 2. 检查searchResult是否触发低阈值检测
|
||||
* 3. 如果是,调用metricsRecorder.recordLowThreshold()记录
|
||||
*/
|
||||
@Slf4j
|
||||
@Component
|
||||
@StageConfig(
|
||||
stageId = "record_metrics",
|
||||
optionalMode = StageOptionalMode.SUPPORT,
|
||||
description = "记录人脸识别指标",
|
||||
defaultEnabled = true
|
||||
)
|
||||
public class RecordMetricsStage extends AbstractPipelineStage<FaceMatchingContext> {
|
||||
|
||||
@Autowired
|
||||
private FaceMetricsRecorder metricsRecorder;
|
||||
|
||||
@Override
|
||||
public String getName() {
|
||||
return "RecordMetrics";
|
||||
}
|
||||
|
||||
@Override
|
||||
protected StageResult<FaceMatchingContext> doExecute(FaceMatchingContext context) {
|
||||
Long faceId = context.getFaceId();
|
||||
|
||||
try {
|
||||
// 1. 记录识别次数
|
||||
metricsRecorder.recordRecognitionCount(faceId);
|
||||
log.debug("记录识别次数: faceId={}", faceId);
|
||||
|
||||
// 2. 检查是否触发低阈值检测
|
||||
if (context.getSearchResult() != null && context.getSearchResult().isLowThreshold()) {
|
||||
metricsRecorder.recordLowThreshold(faceId);
|
||||
log.debug("触发低阈值检测,记录faceId: {}", faceId);
|
||||
}
|
||||
|
||||
return StageResult.success("识别指标记录完成");
|
||||
|
||||
} catch (Exception e) {
|
||||
log.error("记录识别指标失败,faceId={}", faceId, e);
|
||||
// 指标记录失败不影响主流程,返回降级
|
||||
return StageResult.degraded("指标记录失败: " + e.getMessage());
|
||||
}
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,69 @@
|
||||
package com.ycwl.basic.face.pipeline.stages;
|
||||
|
||||
import com.ycwl.basic.biz.TaskStatusBiz;
|
||||
import com.ycwl.basic.face.pipeline.core.FaceMatchingContext;
|
||||
import com.ycwl.basic.pipeline.annotation.StageConfig;
|
||||
import com.ycwl.basic.pipeline.core.AbstractPipelineStage;
|
||||
import com.ycwl.basic.pipeline.core.StageResult;
|
||||
import com.ycwl.basic.pipeline.enums.StageOptionalMode;
|
||||
import lombok.extern.slf4j.Slf4j;
|
||||
import org.springframework.beans.factory.annotation.Autowired;
|
||||
import org.springframework.stereotype.Component;
|
||||
|
||||
/**
|
||||
* 设置任务状态Stage
|
||||
* 负责为新用户设置任务状态为待处理
|
||||
*
|
||||
* 职责:
|
||||
* 1. 仅在isNew=true时执行
|
||||
* 2. 调用taskStatusBiz.setFaceCutStatus(faceId, 0)
|
||||
*/
|
||||
@Slf4j
|
||||
@Component
|
||||
@StageConfig(
|
||||
stageId = "set_task_status",
|
||||
optionalMode = StageOptionalMode.FORCE_ON,
|
||||
description = "设置新用户任务状态"
|
||||
)
|
||||
public class SetTaskStatusStage extends AbstractPipelineStage<FaceMatchingContext> {
|
||||
|
||||
@Autowired
|
||||
private TaskStatusBiz taskStatusBiz;
|
||||
|
||||
@Override
|
||||
public String getName() {
|
||||
return "SetTaskStatus";
|
||||
}
|
||||
|
||||
@Override
|
||||
protected boolean shouldExecuteByBusinessLogic(FaceMatchingContext context) {
|
||||
// 只有新用户才执行
|
||||
if (!context.isNew()) {
|
||||
log.debug("非新用户,跳过设置任务状态,faceId={}", context.getFaceId());
|
||||
return false;
|
||||
}
|
||||
return true;
|
||||
}
|
||||
|
||||
@Override
|
||||
protected StageResult<FaceMatchingContext> doExecute(FaceMatchingContext context) {
|
||||
Long faceId = context.getFaceId();
|
||||
|
||||
// 防御性检查:只有新用户才执行
|
||||
if (!context.isNew()) {
|
||||
log.debug("非新用户,跳过设置任务状态,faceId={}", faceId);
|
||||
return StageResult.skipped("非新用户");
|
||||
}
|
||||
|
||||
try {
|
||||
taskStatusBiz.setFaceCutStatus(faceId, 0);
|
||||
log.debug("设置新用户任务状态: faceId={}, status=0", faceId);
|
||||
return StageResult.success("任务状态已设置");
|
||||
|
||||
} catch (Exception e) {
|
||||
log.error("设置任务状态失败,faceId={}", faceId, e);
|
||||
// 任务状态设置失败不影响主流程,返回降级
|
||||
return StageResult.degraded("任务状态设置失败: " + e.getMessage());
|
||||
}
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,95 @@
|
||||
package com.ycwl.basic.face.pipeline.stages;
|
||||
|
||||
import com.ycwl.basic.face.pipeline.core.FaceMatchingContext;
|
||||
import com.ycwl.basic.pipeline.annotation.StageConfig;
|
||||
import com.ycwl.basic.pipeline.core.AbstractPipelineStage;
|
||||
import com.ycwl.basic.pipeline.core.StageResult;
|
||||
import com.ycwl.basic.pipeline.enums.StageOptionalMode;
|
||||
import com.ycwl.basic.mapper.FaceMapper;
|
||||
import com.ycwl.basic.model.pc.face.entity.FaceEntity;
|
||||
import com.ycwl.basic.model.task.resp.SearchFaceRespVo;
|
||||
import com.ycwl.basic.repository.FaceRepository;
|
||||
import lombok.extern.slf4j.Slf4j;
|
||||
import org.springframework.beans.factory.annotation.Autowired;
|
||||
import org.springframework.stereotype.Component;
|
||||
|
||||
import java.math.BigDecimal;
|
||||
import java.util.Date;
|
||||
import java.util.stream.Collectors;
|
||||
|
||||
/**
|
||||
* 更新人脸结果Stage
|
||||
* 负责将人脸识别结果保存到数据库
|
||||
*
|
||||
* 职责:
|
||||
* 1. 从context.searchResult读取识别结果
|
||||
* 2. 更新FaceEntity(score、matchResult、firstMatchRate、matchSampleIds)
|
||||
* 3. 清除缓存
|
||||
*/
|
||||
@Slf4j
|
||||
@Component
|
||||
@StageConfig(
|
||||
stageId = "update_face_result",
|
||||
optionalMode = StageOptionalMode.FORCE_ON,
|
||||
description = "更新人脸识别结果到数据库"
|
||||
)
|
||||
public class UpdateFaceResultStage extends AbstractPipelineStage<FaceMatchingContext> {
|
||||
|
||||
@Autowired
|
||||
private FaceMapper faceMapper;
|
||||
|
||||
@Autowired
|
||||
private FaceRepository faceRepository;
|
||||
|
||||
@Override
|
||||
public String getName() {
|
||||
return "UpdateFaceResult";
|
||||
}
|
||||
|
||||
@Override
|
||||
protected StageResult<FaceMatchingContext> doExecute(FaceMatchingContext context) {
|
||||
SearchFaceRespVo searchResult = context.getSearchResult();
|
||||
if (searchResult == null) {
|
||||
log.warn("searchResult为空,跳过更新人脸结果,faceId={}", context.getFaceId());
|
||||
return StageResult.skipped("searchResult为空");
|
||||
}
|
||||
|
||||
try {
|
||||
FaceEntity originalFace = context.getFace();
|
||||
Long faceId = context.getFaceId();
|
||||
|
||||
FaceEntity faceEntity = new FaceEntity();
|
||||
faceEntity.setId(faceId);
|
||||
faceEntity.setScore(searchResult.getScore());
|
||||
faceEntity.setMatchResult(searchResult.getSearchResultJson());
|
||||
|
||||
if (searchResult.getFirstMatchRate() != null) {
|
||||
faceEntity.setFirstMatchRate(BigDecimal.valueOf(searchResult.getFirstMatchRate()));
|
||||
}
|
||||
|
||||
if (searchResult.getSampleListIds() != null) {
|
||||
faceEntity.setMatchSampleIds(searchResult.getSampleListIds().stream()
|
||||
.map(String::valueOf)
|
||||
.collect(Collectors.joining(",")));
|
||||
}
|
||||
|
||||
faceEntity.setCreateAt(new Date());
|
||||
faceEntity.setScenicId(originalFace.getScenicId());
|
||||
faceEntity.setMemberId(originalFace.getMemberId());
|
||||
faceEntity.setFaceUrl(originalFace.getFaceUrl());
|
||||
|
||||
faceMapper.update(faceEntity);
|
||||
faceRepository.clearFaceCache(faceId);
|
||||
|
||||
log.debug("人脸结果更新成功:faceId={}, score={}, sampleCount={}",
|
||||
faceId, searchResult.getScore(),
|
||||
searchResult.getSampleListIds() != null ? searchResult.getSampleListIds().size() : 0);
|
||||
|
||||
return StageResult.success("人脸结果更新成功");
|
||||
|
||||
} catch (Exception e) {
|
||||
log.error("更新人脸结果失败,faceId={}", context.getFaceId(), e);
|
||||
return StageResult.failed("保存人脸识别结果失败", e);
|
||||
}
|
||||
}
|
||||
}
|
||||
@@ -45,6 +45,7 @@ public class AliFaceBodyAdapter implements IFaceBodyAdapter {
|
||||
private static final Map<String, IRateLimiter> deleteDbLimiters = new ConcurrentHashMap<>();
|
||||
private static final Map<String, IRateLimiter> deleteEntityLimiters = new ConcurrentHashMap<>();
|
||||
|
||||
@Getter // 添加getter,支持获取accessKeyId
|
||||
private AliFaceBodyConfig config;
|
||||
|
||||
public boolean setConfig(AliFaceBodyConfig config) {
|
||||
@@ -184,10 +185,8 @@ public class AliFaceBodyAdapter implements IFaceBodyAdapter {
|
||||
addFaceRequest.setImageUrl(faceUrl);
|
||||
addFaceRequest.setExtraData(extData);
|
||||
AddFaceResp respVo = new AddFaceResp();
|
||||
try {
|
||||
addFaceLimiter.acquire();
|
||||
} catch (InterruptedException ignored) {
|
||||
}
|
||||
// QPS控制已由外层调度器管理,这里不再需要限流
|
||||
// 移除阻塞等待: addFaceLimiter.acquire()
|
||||
try {
|
||||
AddFaceResponse acsResponse = client.getAcsResponse(addFaceRequest);
|
||||
respVo.setScore(acsResponse.getData().getQualitieScore());
|
||||
|
||||
@@ -8,6 +8,7 @@ import com.ycwl.basic.facebody.entity.SearchFaceResp;
|
||||
import com.ycwl.basic.facebody.entity.SearchFaceResultItem;
|
||||
import com.ycwl.basic.utils.ratelimiter.FixedRateLimiter;
|
||||
import com.ycwl.basic.utils.ratelimiter.IRateLimiter;
|
||||
import lombok.Getter;
|
||||
import lombok.extern.slf4j.Slf4j;
|
||||
import org.json.JSONArray;
|
||||
import org.json.JSONObject;
|
||||
@@ -40,6 +41,8 @@ public class BceFaceBodyAdapter implements IFaceBodyAdapter {
|
||||
private static final Map<String, IRateLimiter> deleteDbLimiters = new ConcurrentHashMap<>();
|
||||
private static final Map<String, IRateLimiter> deleteEntityLimiters = new ConcurrentHashMap<>();
|
||||
private static final Map<String, IRateLimiter> deleteFaceLimiters = new ConcurrentHashMap<>();
|
||||
|
||||
@Getter // 添加getter,支持获取appId和addQps
|
||||
private BceFaceBodyConfig config;
|
||||
|
||||
public boolean setConfig(BceFaceBodyConfig config) {
|
||||
@@ -149,10 +152,8 @@ public class BceFaceBodyAdapter implements IFaceBodyAdapter {
|
||||
options.put("user_info", extData);
|
||||
// options.put("quality_control", "LOW");
|
||||
options.put("action_type", "REPLACE");
|
||||
try {
|
||||
addEntityLimiter.acquire();
|
||||
} catch (InterruptedException ignored) {
|
||||
}
|
||||
// QPS控制已由外层调度器管理,这里不再需要限流
|
||||
// 移除阻塞等待: addEntityLimiter.acquire()
|
||||
JSONObject response = client.addUser(faceUrl, "URL", dbName, entityId, options);
|
||||
int errorCode = response.getInt("error_code");
|
||||
if (errorCode == 0) {
|
||||
@@ -164,10 +165,7 @@ public class BceFaceBodyAdapter implements IFaceBodyAdapter {
|
||||
log.warn("无法正常访问URL图片,错误码: 222204,尝试下载图片转base64后重试,URL: {}", faceUrl);
|
||||
String base64Image = downloadImageAsBase64(faceUrl);
|
||||
if (base64Image != null) {
|
||||
try {
|
||||
addEntityLimiter.acquire();
|
||||
} catch (InterruptedException ignored) {
|
||||
}
|
||||
// 重试时也不需要限流,由外层调度器控制
|
||||
JSONObject retryResponse = client.addUser(base64Image, "BASE64", dbName, entityId, options);
|
||||
if (retryResponse.getInt("error_code") == 0) {
|
||||
log.info("使用base64重试添加人脸成功,entityId: {}", entityId);
|
||||
@@ -420,7 +418,7 @@ public class BceFaceBodyAdapter implements IFaceBodyAdapter {
|
||||
ByteArrayOutputStream baos = null;
|
||||
try {
|
||||
// 下载图片
|
||||
URL url = new URL(imageUrl.replace("oss-cn-shanghai.aliyuncs.com", "oss-cn-shanghai-internal.aliyuncs.com"));
|
||||
URL url = new URL(imageUrl);
|
||||
image = ImageIO.read(url);
|
||||
if (image == null) {
|
||||
log.error("无法读取图片,URL: {}", imageUrl);
|
||||
|
||||
@@ -1,4 +1,62 @@
|
||||
package com.ycwl.basic.image.enhancer;
|
||||
|
||||
import com.ycwl.basic.image.enhancer.adapter.BceImageEnhancer;
|
||||
import com.ycwl.basic.image.enhancer.adapter.BceImageSR;
|
||||
import com.ycwl.basic.image.enhancer.adapter.IEnhancer;
|
||||
import com.ycwl.basic.image.enhancer.entity.BceEnhancerConfig;
|
||||
|
||||
/**
|
||||
* 图像增强器工厂
|
||||
* 用于创建不同类型的图像增强器实例
|
||||
*/
|
||||
public class ImageEnhancerFactory {
|
||||
|
||||
/**
|
||||
* 增强器类型枚举
|
||||
*/
|
||||
public enum EnhancerType {
|
||||
/**
|
||||
* 图像清晰度增强
|
||||
* 使用imageDefinitionEnhance接口,适合提升整体清晰度
|
||||
*/
|
||||
DEFINITION_ENHANCE,
|
||||
|
||||
/**
|
||||
* 图像超分辨率
|
||||
* 使用imageQualityEnhance接口,适合放大图片同时保持质量
|
||||
*/
|
||||
SUPER_RESOLUTION
|
||||
}
|
||||
|
||||
/**
|
||||
* 创建图像增强器
|
||||
*
|
||||
* @param type 增强器类型
|
||||
* @param config 百度云配置
|
||||
* @return 图像增强器实例
|
||||
*/
|
||||
public static IEnhancer createEnhancer(EnhancerType type, BceEnhancerConfig config) {
|
||||
IEnhancer enhancer = switch (type) {
|
||||
case DEFINITION_ENHANCE -> new BceImageEnhancer();
|
||||
case SUPER_RESOLUTION -> new BceImageSR();
|
||||
};
|
||||
|
||||
if (enhancer instanceof BceImageEnhancer) {
|
||||
((BceImageEnhancer) enhancer).setConfig(config);
|
||||
} else if (enhancer instanceof BceImageSR) {
|
||||
((BceImageSR) enhancer).setConfig(config);
|
||||
}
|
||||
|
||||
return enhancer;
|
||||
}
|
||||
|
||||
/**
|
||||
* 创建默认的图像增强器(清晰度增强)
|
||||
*
|
||||
* @param config 百度云配置
|
||||
* @return 图像增强器实例
|
||||
*/
|
||||
public static IEnhancer createDefaultEnhancer(BceEnhancerConfig config) {
|
||||
return createEnhancer(EnhancerType.DEFINITION_ENHANCE, config);
|
||||
}
|
||||
}
|
||||
|
||||
604
src/main/java/com/ycwl/basic/image/pipeline/CLAUDE.md
Normal file
604
src/main/java/com/ycwl/basic/image/pipeline/CLAUDE.md
Normal file
@@ -0,0 +1,604 @@
|
||||
# Image Pipeline 图片处理管线
|
||||
|
||||
## 概述
|
||||
|
||||
Image Pipeline 是一个通用的、可扩展的图片处理管线框架,用于组织和执行一系列图片处理操作(Stage)。
|
||||
|
||||
### 核心特性
|
||||
|
||||
- **责任链模式**: 将图片处理流程拆分为独立的 Stage,按顺序执行
|
||||
- **Builder 模式**: 灵活组装管线,支持条件性添加 Stage
|
||||
- **动态 Stage 添加**: 支持在运行时根据条件动态添加后续 Stage
|
||||
- **降级策略**: 支持多级降级执行,确保管线在异常情况下的稳定性
|
||||
- **配置驱动**: 支持通过外部配置控制 Stage 的启用/禁用
|
||||
- **类型安全**: 使用泛型和枚举确保类型安全
|
||||
- **解耦设计**: Context 独立于业务模型,支持多种使用场景
|
||||
- **自动清理**: 无论成功或失败都会在 finally 中兜底调用 `context.cleanup()`
|
||||
|
||||
## 包结构
|
||||
|
||||
```
|
||||
com.ycwl.basic.image.pipeline
|
||||
├── annotation/ # 注解定义
|
||||
│ └── StageConfig # Stage 配置注解
|
||||
├── core/ # 核心类
|
||||
│ ├── AbstractPipelineStage # Stage 抽象基类
|
||||
│ ├── PhotoProcessContext # 管线上下文
|
||||
│ ├── Pipeline # 管线执行器
|
||||
│ ├── PipelineBuilder # 管线构建器
|
||||
│ ├── PipelineStage # Stage 接口
|
||||
│ └── StageResult # Stage 执行结果
|
||||
├── enums/ # 枚举定义
|
||||
│ ├── ImageSource # 图片来源枚举
|
||||
│ ├── ImageType # 图片类型枚举
|
||||
│ ├── PipelineScene # 管线场景枚举
|
||||
│ └── StageOptionalMode # Stage 可选模式枚举
|
||||
├── exception/ # 异常类
|
||||
│ ├── PipelineException # 管线异常
|
||||
│ └── StageExecutionException # Stage 执行异常
|
||||
├── stages/ # 具体 Stage 实现
|
||||
│ ├── CleanupStage # 清理临时文件
|
||||
│ ├── ConditionalRotateStage # 条件性旋转
|
||||
│ ├── DownloadStage # 下载图片
|
||||
│ ├── ImageEnhanceStage # 图像增强(超分)
|
||||
│ ├── ImageOrientationStage # 图像方向检测
|
||||
│ ├── ImageQualityCheckStage # 图像质量检测
|
||||
│ ├── PuzzleBorderStage # 拼图边框处理
|
||||
│ ├── RestoreOrientationStage # 恢复图片方向
|
||||
│ ├── SourcePhotoUpdateStage # 源图片更新
|
||||
│ ├── UploadStage # 上传图片
|
||||
│ └── WatermarkStage # 水印处理
|
||||
└── util/ # 工具类
|
||||
└── TempFileManager # 临时文件管理器
|
||||
```
|
||||
|
||||
## 核心组件
|
||||
|
||||
### 1. Pipeline - 管线执行器
|
||||
|
||||
**职责**: 按顺序执行一系列 Stage,管理执行流程和异常处理。
|
||||
|
||||
**关键特性**:
|
||||
- 顺序执行所有 Stage
|
||||
- 支持动态添加后续 Stage
|
||||
- 循环检测(最大执行 100 个 Stage)
|
||||
- 详细的日志输出(带状态图标)
|
||||
|
||||
**使用示例**:
|
||||
```java
|
||||
Pipeline<PhotoProcessContext> pipeline = new PipelineBuilder<>("MyPipeline")
|
||||
.addStage(new DownloadStage())
|
||||
.addStage(new WatermarkStage())
|
||||
.addStage(new UploadStage())
|
||||
.addStage(new CleanupStage())
|
||||
.build();
|
||||
|
||||
boolean success = pipeline.execute(context);
|
||||
```
|
||||
|
||||
### 2. PhotoProcessContext - 管线上下文
|
||||
|
||||
**职责**: 在各个 Stage 之间传递状态和数据,提供临时文件管理。
|
||||
|
||||
**核心字段**:
|
||||
- `processId`: 处理过程唯一标识,用于隔离临时文件
|
||||
- `originalUrl`: 原图 URL
|
||||
- `scenicId`: 景区 ID
|
||||
- `imageType`: 图片类型(普通照片/拼图/手机上传)
|
||||
- `tempFileManager`: 临时文件管理器
|
||||
|
||||
**静态工厂方法**:
|
||||
```java
|
||||
// 从打印订单创建(打印场景)
|
||||
PhotoProcessContext context = PhotoProcessContext.fromPrinterOrderItem(orderItem, scenicId);
|
||||
|
||||
// 为超分辨率场景创建
|
||||
PhotoProcessContext context = PhotoProcessContext.forSuperResolution(itemId, url, scenicId);
|
||||
|
||||
// 使用 Builder 自定义创建
|
||||
PhotoProcessContext context = PhotoProcessContext.builder()
|
||||
.processId("custom-id")
|
||||
.originalUrl("https://example.com/image.jpg")
|
||||
.scenicId(12345L)
|
||||
.imageType(ImageType.NORMAL_PHOTO)
|
||||
.source(ImageSource.IPC)
|
||||
.scene(PipelineScene.IMAGE_PRINT)
|
||||
.build();
|
||||
```
|
||||
|
||||
**重要方法**:
|
||||
- `getCurrentFile()`: 获取当前处理中的文件
|
||||
- `updateProcessedFile(File)`: 更新处理后的文件
|
||||
- `setResultUrl(String)`: 设置最终结果 URL(会触发回调)
|
||||
- `cleanup()`: 清理所有临时文件
|
||||
- `isStageEnabled(stageId, default)`: 判断 Stage 是否启用
|
||||
|
||||
### 3. AbstractPipelineStage - Stage 抽象基类
|
||||
|
||||
**职责**: 提供 Stage 的通用实现和模板方法。
|
||||
|
||||
**执行流程**:
|
||||
```
|
||||
shouldExecute() → beforeExecute() → doExecute() → afterExecute()
|
||||
```
|
||||
|
||||
**子类需要实现**:
|
||||
- `getName()`: 返回 Stage 名称
|
||||
- `doExecute(context)`: 实现具体处理逻辑
|
||||
- `shouldExecuteByBusinessLogic(context)`: (可选)实现条件判断
|
||||
|
||||
**Stage 执行判断逻辑**:
|
||||
1. 检查 `@StageConfig` 注解
|
||||
2. 根据 `optionalMode` 决定是否检查外部配置
|
||||
- `FORCE_ON`: 强制执行,不检查外部配置
|
||||
- `SUPPORT`: 检查外部配置(`context.isStageEnabled()`)
|
||||
- `UNSUPPORT`: 不检查外部配置
|
||||
3. 执行业务逻辑判断(`shouldExecuteByBusinessLogic()`)
|
||||
|
||||
### 4. StageResult - Stage 执行结果
|
||||
|
||||
**状态类型**:
|
||||
- `SUCCESS`: 执行成功
|
||||
- `SKIPPED`: 跳过执行
|
||||
- `FAILED`: 执行失败(会终止管线)
|
||||
- `DEGRADED`: 降级执行(继续管线但记录警告)
|
||||
|
||||
**静态工厂方法**:
|
||||
```java
|
||||
// 成功
|
||||
StageResult.success();
|
||||
StageResult.success("处理完成");
|
||||
|
||||
// 成功并动态添加后续 Stage
|
||||
StageResult.successWithNext("质量不佳,添加增强", new ImageEnhanceStage());
|
||||
|
||||
// 跳过
|
||||
StageResult.skipped("条件不满足");
|
||||
|
||||
// 失败
|
||||
StageResult.failed("下载失败");
|
||||
StageResult.failed("处理失败", exception);
|
||||
|
||||
// 降级
|
||||
StageResult.degraded("使用备用方案");
|
||||
```
|
||||
|
||||
### 5. @StageConfig - Stage 配置注解
|
||||
|
||||
**字段**:
|
||||
- `stageId`: Stage 唯一标识(用于外部配置控制)
|
||||
- `optionalMode`: 可选模式
|
||||
- `FORCE_ON`: 强制执行(如 DownloadStage、CleanupStage)
|
||||
- `SUPPORT`: 支持外部控制(如 WatermarkStage、ImageEnhanceStage)
|
||||
- `UNSUPPORT`: 不支持外部控制(如 RestoreOrientationStage)
|
||||
- `defaultEnabled`: 默认是否启用
|
||||
- `description`: 描述信息
|
||||
|
||||
**示例**:
|
||||
```java
|
||||
@StageConfig(
|
||||
stageId = "watermark",
|
||||
optionalMode = StageOptionalMode.SUPPORT,
|
||||
description = "水印处理",
|
||||
defaultEnabled = true
|
||||
)
|
||||
public class WatermarkStage extends AbstractPipelineStage<PhotoProcessContext> {
|
||||
// ...
|
||||
}
|
||||
```
|
||||
|
||||
## Stage 列表
|
||||
|
||||
### 核心 Stage
|
||||
|
||||
| Stage | 职责 | Optional Mode | 执行条件 |
|
||||
|-------|------|---------------|---------|
|
||||
| DownloadStage | 从 URL 下载图片到本地 | FORCE_ON | 总是执行 |
|
||||
| CleanupStage | 清理所有临时文件 | FORCE_ON | 总是执行(优先级 999) |
|
||||
|
||||
### 图片处理 Stage
|
||||
|
||||
| Stage | 职责 | Optional Mode | 执行条件 |
|
||||
|-------|------|---------------|---------|
|
||||
| ImageOrientationStage | 检测图片方向(横竖) | UNSUPPORT | 仅普通照片 |
|
||||
| ConditionalRotateStage | 条件性旋转(竖图变横图) | UNSUPPORT | 仅竖图 |
|
||||
| RestoreOrientationStage | 恢复图片方向(横图变回竖图) | UNSUPPORT | 需要旋转的照片 |
|
||||
| WatermarkStage | 添加水印 | SUPPORT | 仅普通照片 |
|
||||
| PuzzleBorderStage | 处理拼图边框 | UNSUPPORT | 仅拼图 |
|
||||
| ImageEnhanceStage | 图像增强(超分) | SUPPORT | 可配置 |
|
||||
| ImageQualityCheckStage | 图像质量检测 | SUPPORT | 仅普通照片 |
|
||||
|
||||
> **提示**:`ImageEnhanceStage` 的默认构造函数会尝试从 `BCE_IMAGE_APP_ID/BCE_IMAGE_API_KEY/BCE_IMAGE_SECRET_KEY` 环境变量读取百度云凭据;若未配置则自动跳过执行。
|
||||
|
||||
### 存储 Stage
|
||||
|
||||
| Stage | 职责 | Optional Mode | 执行条件 |
|
||||
|-------|------|---------------|---------|
|
||||
| UploadStage | 上传图片到存储服务 | FORCE_ON | 总是执行 |
|
||||
| SourcePhotoUpdateStage | 更新源图片记录 | UNSUPPORT | 总是执行 |
|
||||
|
||||
### 辅助 Stage
|
||||
|
||||
| Stage | 职责 | Optional Mode | 执行条件 |
|
||||
|-------|------|---------------|---------|
|
||||
| NoOpStage | 调试/占位 Stage,不做任何处理 | UNSUPPORT | 仅用于保持流程完整或调试 |
|
||||
|
||||
## 典型管线示例
|
||||
|
||||
### 1. 打印照片处理管线
|
||||
|
||||
```java
|
||||
Pipeline<PhotoProcessContext> pipeline = new PipelineBuilder<>("PrintPipeline")
|
||||
.addStage(new DownloadStage()) // 1. 下载
|
||||
.addStage(new ImageOrientationStage()) // 2. 检测方向
|
||||
.addStage(new ConditionalRotateStage()) // 3. 旋转竖图
|
||||
.addStage(new WatermarkStage()) // 4. 添加水印
|
||||
.addStage(new RestoreOrientationStage()) // 5. 恢复方向
|
||||
.addStage(new UploadStage()) // 6. 上传
|
||||
.addStage(new CleanupStage()) // 7. 清理
|
||||
.build();
|
||||
```
|
||||
|
||||
### 2. 拼图处理管线
|
||||
|
||||
```java
|
||||
Pipeline<PhotoProcessContext> pipeline = new PipelineBuilder<>("PuzzlePipeline")
|
||||
.addStage(new DownloadStage()) // 1. 下载
|
||||
.addStage(new PuzzleBorderStage()) // 2. 添加拼图边框
|
||||
.addStage(new UploadStage()) // 3. 上传
|
||||
.addStage(new CleanupStage()) // 4. 清理
|
||||
.build();
|
||||
```
|
||||
|
||||
### 3. 超分辨率增强管线
|
||||
|
||||
```java
|
||||
Pipeline<PhotoProcessContext> pipeline = new PipelineBuilder<>("SuperResolutionPipeline")
|
||||
.addStage(new DownloadStage()) // 1. 下载
|
||||
.addStage(new ImageEnhanceStage(config)) // 2. 超分增强
|
||||
.addStage(new SourcePhotoUpdateStage(sourceService, sourceId)) // 3. 更新记录
|
||||
.addStage(new CleanupStage()) // 4. 清理
|
||||
.build();
|
||||
```
|
||||
|
||||
### 4. 带质量检测的管线(动态 Stage)
|
||||
|
||||
```java
|
||||
Pipeline<PhotoProcessContext> pipeline = new PipelineBuilder<>("QualityCheckPipeline")
|
||||
.addStage(new DownloadStage()) // 1. 下载
|
||||
.addStage(new ImageQualityCheckStage()) // 2. 质量检测(可能动态添加 ImageEnhanceStage)
|
||||
.addStage(new WatermarkStage()) // 3. 水印
|
||||
.addStage(new UploadStage()) // 4. 上传
|
||||
.addStage(new CleanupStage()) // 5. 清理
|
||||
.build();
|
||||
```
|
||||
|
||||
## 扩展指南
|
||||
|
||||
### 如何创建新的 Stage
|
||||
|
||||
1. **继承 AbstractPipelineStage**:
|
||||
```java
|
||||
@Slf4j
|
||||
@StageConfig(
|
||||
stageId = "my_stage",
|
||||
optionalMode = StageOptionalMode.SUPPORT,
|
||||
description = "我的自定义 Stage",
|
||||
defaultEnabled = true
|
||||
)
|
||||
public class MyStage extends AbstractPipelineStage<PhotoProcessContext> {
|
||||
|
||||
@Override
|
||||
public String getName() {
|
||||
return "MyStage";
|
||||
}
|
||||
|
||||
@Override
|
||||
protected boolean shouldExecuteByBusinessLogic(PhotoProcessContext context) {
|
||||
// 实现条件判断
|
||||
return context.getImageType() == ImageType.NORMAL_PHOTO;
|
||||
}
|
||||
|
||||
@Override
|
||||
protected StageResult doExecute(PhotoProcessContext context) {
|
||||
File currentFile = context.getCurrentFile();
|
||||
|
||||
try {
|
||||
// 1. 获取输入文件
|
||||
if (currentFile == null || !currentFile.exists()) {
|
||||
return StageResult.failed("当前文件不存在");
|
||||
}
|
||||
|
||||
// 2. 处理图片
|
||||
File outputFile = context.getTempFileManager()
|
||||
.createTempFile("my_output", ".jpg");
|
||||
|
||||
// 执行具体处理逻辑
|
||||
doSomethingWithImage(currentFile, outputFile);
|
||||
|
||||
// 3. 更新 Context
|
||||
context.updateProcessedFile(outputFile);
|
||||
|
||||
// 4. 返回成功结果
|
||||
return StageResult.success("处理完成");
|
||||
|
||||
} catch (Exception e) {
|
||||
log.error("处理失败", e);
|
||||
return StageResult.failed("处理失败: " + e.getMessage(), e);
|
||||
}
|
||||
}
|
||||
|
||||
private void doSomethingWithImage(File input, File output) {
|
||||
// 具体实现
|
||||
}
|
||||
}
|
||||
```
|
||||
|
||||
2. **添加到管线**:
|
||||
```java
|
||||
pipeline.addStage(new MyStage());
|
||||
```
|
||||
|
||||
### 如何实现降级策略
|
||||
|
||||
参考 `WatermarkStage` 的实现,使用循环尝试多种方案:
|
||||
|
||||
```java
|
||||
@Override
|
||||
protected StageResult doExecute(PhotoProcessContext context) {
|
||||
List<Strategy> strategies = Arrays.asList(
|
||||
Strategy.ADVANCED,
|
||||
Strategy.STANDARD,
|
||||
Strategy.BASIC
|
||||
);
|
||||
|
||||
for (int i = 0; i < strategies.size(); i++) {
|
||||
Strategy strategy = strategies.get(i);
|
||||
try {
|
||||
StageResult result = tryStrategy(context, strategy);
|
||||
|
||||
if (i > 0) {
|
||||
// 使用了降级策略
|
||||
return StageResult.degraded("降级到: " + strategy);
|
||||
}
|
||||
|
||||
return result;
|
||||
|
||||
} catch (Exception e) {
|
||||
log.warn("策略 {} 失败: {}", strategy, e.getMessage());
|
||||
}
|
||||
}
|
||||
|
||||
// 所有策略都失败
|
||||
return StageResult.degraded("所有策略失败,跳过处理");
|
||||
}
|
||||
```
|
||||
|
||||
### 如何动态添加后续 Stage
|
||||
|
||||
参考 `ImageQualityCheckStage` 的实现:
|
||||
|
||||
```java
|
||||
@Override
|
||||
protected StageResult doExecute(PhotoProcessContext context) {
|
||||
boolean needsEnhancement = checkQuality(context.getCurrentFile());
|
||||
|
||||
if (needsEnhancement) {
|
||||
ImageEnhanceStage enhanceStage = new ImageEnhanceStage();
|
||||
return StageResult.successWithNext("质量不佳,添加增强", enhanceStage);
|
||||
}
|
||||
|
||||
return StageResult.success("质量良好");
|
||||
}
|
||||
```
|
||||
|
||||
## 最佳实践
|
||||
|
||||
### 1. Context 管理
|
||||
|
||||
- **总是使用静态工厂方法或 Builder**: 避免直接调用构造函数
|
||||
- **及时清理临时文件**: 在 finally 块或使用 CleanupStage
|
||||
- **使用回调更新外部状态**: 通过 `resultUrlCallback` 而非直接操作业务对象
|
||||
|
||||
### 2. Stage 设计
|
||||
|
||||
- **单一职责**: 每个 Stage 只做一件事
|
||||
- **可组合**: Stage 应该可以灵活组合
|
||||
- **幂等性**: 相同输入应产生相同输出
|
||||
- **异常处理**: 捕获异常并返回 `StageResult.failed()` 或 `StageResult.degraded()`
|
||||
- **日志记录**: 在关键操作处记录 debug/info 日志
|
||||
|
||||
### 3. 管线构建
|
||||
|
||||
- **CleanupStage 总是最后**: 确保临时文件总是被清理
|
||||
- **DownloadStage 总是最前**: 确保有本地文件可用
|
||||
- **合理使用 optionalMode**:
|
||||
- 必需的 Stage 使用 `FORCE_ON`
|
||||
- 可选的 Stage 使用 `SUPPORT`
|
||||
- 内部逻辑控制的 Stage 使用 `UNSUPPORT`
|
||||
|
||||
### 4. 性能优化
|
||||
|
||||
- **复用 TempFileManager**: 自动管理临时文件生命周期
|
||||
- **避免重复下载**: 使用 `context.getCurrentFile()` 获取最新文件
|
||||
- **及时更新 processedFile**: 使用 `context.updateProcessedFile()` 通知下一个 Stage
|
||||
|
||||
### 5. 错误处理
|
||||
|
||||
- **失败即停止**: 使用 `StageResult.failed()` 终止管线
|
||||
- **降级继续执行**: 使用 `StageResult.degraded()` 记录问题但继续执行
|
||||
- **跳过非关键 Stage**: 使用 `StageResult.skipped()` 表示条件不满足
|
||||
- **携带异常信息**: `StageResult.failed(message, exception)` 便于排查问题
|
||||
|
||||
## 配置控制
|
||||
|
||||
### 1. 景区级配置
|
||||
|
||||
通过 `ScenicConfigManager` 加载景区配置:
|
||||
|
||||
```java
|
||||
context.setScenicConfigManager(scenicConfigManager);
|
||||
```
|
||||
|
||||
Stage 内部可以获取配置:
|
||||
|
||||
```java
|
||||
ScenicConfigManager config = context.getScenicConfigManager();
|
||||
Boolean enabled = config.getBoolean("my_feature_enabled");
|
||||
String value = config.getString("my_setting");
|
||||
```
|
||||
|
||||
### 2. 请求级配置
|
||||
|
||||
通过 `loadStageConfig()` 加载请求参数:
|
||||
|
||||
```java
|
||||
Map<String, Boolean> stageConfig = new HashMap<>();
|
||||
stageConfig.put("watermark", false); // 禁用水印
|
||||
stageConfig.put("image_enhance", true); // 启用增强
|
||||
|
||||
context.loadStageConfig(scenicConfigManager, stageConfig);
|
||||
```
|
||||
|
||||
### 3. Stage 启用判断
|
||||
|
||||
在 `AbstractPipelineStage.shouldExecute()` 中自动处理:
|
||||
|
||||
```java
|
||||
// 对于 optionalMode = SUPPORT 的 Stage
|
||||
@StageConfig(
|
||||
stageId = "watermark",
|
||||
optionalMode = StageOptionalMode.SUPPORT,
|
||||
defaultEnabled = true
|
||||
)
|
||||
public class WatermarkStage extends AbstractPipelineStage<PhotoProcessContext> {
|
||||
// 如果外部配置禁用了 watermark,则不执行
|
||||
}
|
||||
```
|
||||
|
||||
## 测试指南
|
||||
|
||||
### 单元测试结构
|
||||
|
||||
```java
|
||||
@Test
|
||||
public void testStageSuccess() {
|
||||
// 1. 准备 Context
|
||||
PhotoProcessContext context = PhotoProcessContext.builder()
|
||||
.processId("test-1")
|
||||
.originalUrl("https://example.com/test.jpg")
|
||||
.scenicId(123L)
|
||||
.build();
|
||||
|
||||
// 2. 创建 Stage
|
||||
MyStage stage = new MyStage();
|
||||
|
||||
// 3. 执行
|
||||
StageResult result = stage.execute(context);
|
||||
|
||||
// 4. 断言
|
||||
assertTrue(result.isSuccess());
|
||||
assertNotNull(context.getCurrentFile());
|
||||
}
|
||||
|
||||
@Test
|
||||
public void testStageSkipped() {
|
||||
// 测试条件不满足时跳过
|
||||
}
|
||||
|
||||
@Test
|
||||
public void testStageFailed() {
|
||||
// 测试异常情况
|
||||
}
|
||||
|
||||
@Test
|
||||
public void testStageDegraded() {
|
||||
// 测试降级情况
|
||||
}
|
||||
```
|
||||
|
||||
### 管线集成测试
|
||||
|
||||
```java
|
||||
@Test
|
||||
public void testPipelineExecution() {
|
||||
Pipeline<PhotoProcessContext> pipeline = new PipelineBuilder<>("TestPipeline")
|
||||
.addStage(new DownloadStage())
|
||||
.addStage(new WatermarkStage())
|
||||
.addStage(new UploadStage())
|
||||
.addStage(new CleanupStage())
|
||||
.build();
|
||||
|
||||
PhotoProcessContext context = createTestContext();
|
||||
|
||||
boolean success = pipeline.execute(context);
|
||||
|
||||
assertTrue(success);
|
||||
assertNotNull(context.getResultUrl());
|
||||
}
|
||||
```
|
||||
|
||||
## 常见问题
|
||||
|
||||
### Q: 如何跳过某个 Stage?
|
||||
|
||||
A: 有三种方式:
|
||||
1. 使用外部配置(适用于 `optionalMode = SUPPORT` 的 Stage)
|
||||
2. 在 `shouldExecuteByBusinessLogic()` 中返回 false
|
||||
3. 构建管线时不添加该 Stage
|
||||
|
||||
### Q: 如何在运行时决定是否添加某个 Stage?
|
||||
|
||||
A: 使用 `PipelineBuilder.addStageIf()`:
|
||||
```java
|
||||
builder.addStageIf(needWatermark, new WatermarkStage());
|
||||
```
|
||||
|
||||
或者使用动态 Stage 添加(`StageResult.successWithNext()`)。
|
||||
|
||||
### Q: 如何处理 Stage 执行失败?
|
||||
|
||||
A: 返回 `StageResult.failed()`,管线会立即终止。如果希望继续执行,使用 `StageResult.degraded()`。
|
||||
|
||||
### Q: 临时文件什么时候被清理?
|
||||
|
||||
A: 由 `CleanupStage` 负责,通常放在管线最后。也可以手动调用 `context.cleanup()`。此外,`Pipeline` 在 finally 中还会再调用一次 `context.cleanup()`,保证失败或异常时也能释放所有临时文件。
|
||||
|
||||
### Q: 如何获取最终处理结果?
|
||||
|
||||
A: 使用 `context.getResultUrl()`,或者在构建 Context 时提供 `resultUrlCallback`。
|
||||
|
||||
### Q: 如何支持新的图片来源或场景?
|
||||
|
||||
A: 扩展 `ImageSource` 或 `PipelineScene` 枚举,然后在 Stage 中添加相应的判断逻辑。
|
||||
|
||||
## 架构演进
|
||||
|
||||
### 已实现的特性
|
||||
|
||||
- ✅ 责任链模式的基础管线框架
|
||||
- ✅ Builder 模式的管线构建
|
||||
- ✅ 动态 Stage 添加
|
||||
- ✅ 多级降级策略
|
||||
- ✅ 配置驱动的 Stage 控制
|
||||
- ✅ Context 与业务模型解耦
|
||||
- ✅ 类型安全的图片分类
|
||||
|
||||
### 未来可能的改进
|
||||
|
||||
- 🔄 支持并行执行某些 Stage
|
||||
- 🔄 支持 Stage 执行超时控制
|
||||
- 🔄 支持管线执行的暂停/恢复
|
||||
- 🔄 支持更细粒度的性能监控
|
||||
- 🔄 支持 Stage 执行的重试机制
|
||||
- 🔄 支持管线执行的可视化追踪
|
||||
|
||||
## 相关文档
|
||||
|
||||
- [ImageUtils 工具类](../utils/ImageUtils.java)
|
||||
- [StorageFactory 存储工厂](../storage/StorageFactory.java)
|
||||
- [WatermarkFactory 水印工厂](../image/watermark/ImageWatermarkFactory.java)
|
||||
|
||||
## 维护者
|
||||
|
||||
- 图片处理管线 - 基础架构团队
|
||||
@@ -0,0 +1,374 @@
|
||||
package com.ycwl.basic.image.pipeline.core;
|
||||
|
||||
import com.ycwl.basic.image.pipeline.enums.ImageSource;
|
||||
import com.ycwl.basic.image.pipeline.enums.ImageType;
|
||||
import com.ycwl.basic.image.pipeline.enums.PipelineScene;
|
||||
import com.ycwl.basic.integration.common.manager.ScenicConfigManager;
|
||||
import com.ycwl.basic.model.Crop;
|
||||
import com.ycwl.basic.model.PrinterOrderItem;
|
||||
import com.ycwl.basic.image.pipeline.util.TempFileManager;
|
||||
import com.ycwl.basic.storage.adapters.IStorageAdapter;
|
||||
import com.ycwl.basic.pipeline.core.PipelineContext;
|
||||
import lombok.Getter;
|
||||
import lombok.Setter;
|
||||
|
||||
import java.io.File;
|
||||
import java.util.HashMap;
|
||||
import java.util.Map;
|
||||
import java.util.UUID;
|
||||
import java.util.function.Consumer;
|
||||
|
||||
/**
|
||||
* 图片处理管线上下文
|
||||
* 在各个Stage之间传递状态和数据
|
||||
*/
|
||||
@Getter
|
||||
@Setter
|
||||
public class PhotoProcessContext implements PipelineContext {
|
||||
|
||||
// ==================== 核心字段(构造时必填)====================
|
||||
|
||||
/**
|
||||
* 处理过程唯一标识
|
||||
* 用于 TempFileManager 创建隔离的临时文件目录
|
||||
*/
|
||||
private final String processId;
|
||||
|
||||
/**
|
||||
* 原图 URL
|
||||
*/
|
||||
private final String originalUrl;
|
||||
|
||||
/**
|
||||
* 景区 ID
|
||||
*/
|
||||
private final Long scenicId;
|
||||
|
||||
/**
|
||||
* 临时文件管理器
|
||||
*/
|
||||
private final TempFileManager tempFileManager;
|
||||
|
||||
// ==================== 图片元信息 ====================
|
||||
|
||||
/**
|
||||
* 图片类型
|
||||
*/
|
||||
private ImageType imageType = ImageType.NORMAL_PHOTO;
|
||||
|
||||
/**
|
||||
* 裁剪/旋转信息
|
||||
*/
|
||||
private Crop crop;
|
||||
|
||||
// ==================== 管线配置 ====================
|
||||
|
||||
/**
|
||||
* 景区配置管理器,用于获取景区相关配置
|
||||
*/
|
||||
private ScenicConfigManager scenicConfigManager;
|
||||
|
||||
/**
|
||||
* 管线处理场景(打印、增强等)
|
||||
*/
|
||||
private PipelineScene scene;
|
||||
|
||||
/**
|
||||
* 图片来源(IPC、相机、手机等)
|
||||
*/
|
||||
private ImageSource source;
|
||||
|
||||
/**
|
||||
* Stage开关配置表
|
||||
* Key: stageId, Value: 是否启用
|
||||
* 整合了景区配置和请求参数
|
||||
*/
|
||||
private Map<String, Boolean> stageEnabledMap = new HashMap<>();
|
||||
|
||||
// ==================== 处理过程状态 ====================
|
||||
|
||||
private File originalFile;
|
||||
private File processedFile;
|
||||
private boolean isLandscape = true;
|
||||
private boolean rotationApplied = false;
|
||||
private boolean cleaned = false;
|
||||
|
||||
/**
|
||||
* 图像需要旋转的角度(用于后续Stage使用)
|
||||
* 由 ImageOrientationStage 从 Crop.rotation 中提取并设置
|
||||
*/
|
||||
private Integer imageRotation;
|
||||
|
||||
private String resultUrl;
|
||||
private IStorageAdapter storageAdapter;
|
||||
|
||||
// ==================== 回调 ====================
|
||||
|
||||
/**
|
||||
* 结果 URL 回调
|
||||
* 用于在 setResultUrl 时通知外部更新相关数据
|
||||
*/
|
||||
private Consumer<String> resultUrlCallback;
|
||||
|
||||
// ==================== 构造函数(私有)====================
|
||||
|
||||
private PhotoProcessContext(Builder builder) {
|
||||
this.processId = builder.processId;
|
||||
this.originalUrl = builder.originalUrl;
|
||||
this.scenicId = builder.scenicId;
|
||||
this.tempFileManager = new TempFileManager(processId);
|
||||
|
||||
this.imageType = builder.imageType;
|
||||
this.crop = builder.crop;
|
||||
this.scene = builder.scene;
|
||||
this.source = builder.source;
|
||||
this.resultUrlCallback = builder.resultUrlCallback;
|
||||
}
|
||||
|
||||
// ==================== 静态工厂方法 ====================
|
||||
|
||||
/**
|
||||
* 从 PrinterOrderItem 创建 Context(打印场景兼容方法)
|
||||
*
|
||||
* @param orderItem 打印订单项
|
||||
* @param scenicId 景区ID
|
||||
* @return PhotoProcessContext
|
||||
*/
|
||||
public static PhotoProcessContext fromPrinterOrderItem(PrinterOrderItem orderItem, Long scenicId) {
|
||||
return PhotoProcessContext.builder()
|
||||
.processId(orderItem.getId().toString())
|
||||
.originalUrl(orderItem.getCropUrl())
|
||||
.scenicId(scenicId)
|
||||
.imageType(ImageType.fromSourceId(orderItem.getSourceId()))
|
||||
.crop(orderItem.getCrop())
|
||||
.resultUrlCallback(url -> orderItem.setCropUrl(url))
|
||||
.build();
|
||||
}
|
||||
|
||||
/**
|
||||
* 为超分辨率场景创建 Context
|
||||
*
|
||||
* @param itemId 项目ID(用于临时文件隔离)
|
||||
* @param url 原图URL
|
||||
* @param scenicId 景区ID
|
||||
* @return PhotoProcessContext
|
||||
*/
|
||||
public static PhotoProcessContext forSuperResolution(Long itemId, String url, Long scenicId) {
|
||||
return PhotoProcessContext.builder()
|
||||
.processId(itemId.toString())
|
||||
.originalUrl(url)
|
||||
.scenicId(scenicId)
|
||||
.imageType(ImageType.NORMAL_PHOTO)
|
||||
.source(ImageSource.IPC)
|
||||
.scene(PipelineScene.SOURCE_PHOTO_SUPER_RESOLUTION)
|
||||
.build();
|
||||
}
|
||||
|
||||
/**
|
||||
* 获取 Builder
|
||||
*/
|
||||
public static Builder builder() {
|
||||
return new Builder();
|
||||
}
|
||||
|
||||
// ==================== 业务方法 ====================
|
||||
|
||||
/**
|
||||
* 从景区配置和请求参数中加载Stage开关配置
|
||||
*
|
||||
* @param scenicConfigManager 景区配置管理器
|
||||
* @param requestParams 请求参数中的Stage配置
|
||||
*/
|
||||
public void loadStageConfig(ScenicConfigManager scenicConfigManager, Map<String, Boolean> requestParams) {
|
||||
// 请求参数覆盖
|
||||
if (requestParams != null) {
|
||||
stageEnabledMap.putAll(requestParams);
|
||||
}
|
||||
}
|
||||
|
||||
/**
|
||||
* 判断指定Stage是否启用
|
||||
*
|
||||
* @param stageId Stage唯一标识
|
||||
* @param defaultEnabled 默认值(当配置未指定时使用)
|
||||
* @return true-启用, false-禁用
|
||||
*/
|
||||
@Override
|
||||
public boolean isStageEnabled(String stageId, boolean defaultEnabled) {
|
||||
return stageEnabledMap.getOrDefault(stageId, defaultEnabled);
|
||||
}
|
||||
|
||||
/**
|
||||
* 判断指定Stage是否启用(默认为false)
|
||||
*
|
||||
* @param stageId Stage唯一标识
|
||||
* @return true-启用, false-禁用
|
||||
*/
|
||||
@Override
|
||||
public boolean isStageEnabled(String stageId) {
|
||||
return stageEnabledMap.getOrDefault(stageId, false);
|
||||
}
|
||||
|
||||
/**
|
||||
* 设置指定Stage的启用状态
|
||||
*
|
||||
* @param stageId Stage唯一标识
|
||||
* @param enabled 是否启用
|
||||
* @return this(支持链式调用)
|
||||
*/
|
||||
public PhotoProcessContext setStageState(String stageId, boolean enabled) {
|
||||
stageEnabledMap.put(stageId, enabled);
|
||||
return this;
|
||||
}
|
||||
|
||||
/**
|
||||
* 启用指定Stage
|
||||
*
|
||||
* @param stageId Stage唯一标识
|
||||
* @return this(支持链式调用)
|
||||
*/
|
||||
public PhotoProcessContext enableStage(String stageId) {
|
||||
stageEnabledMap.put(stageId, true);
|
||||
return this;
|
||||
}
|
||||
|
||||
/**
|
||||
* 禁用指定Stage
|
||||
*
|
||||
* @param stageId Stage唯一标识
|
||||
* @return this(支持链式调用)
|
||||
*/
|
||||
public PhotoProcessContext disableStage(String stageId) {
|
||||
stageEnabledMap.put(stageId, false);
|
||||
return this;
|
||||
}
|
||||
|
||||
/**
|
||||
* 批量设置Stage启用状态
|
||||
*
|
||||
* @param stages Stage配置Map(stageId -> enabled)
|
||||
* @return this(支持链式调用)
|
||||
*/
|
||||
public PhotoProcessContext setStages(Map<String, Boolean> stages) {
|
||||
if (stages != null) {
|
||||
stageEnabledMap.putAll(stages);
|
||||
}
|
||||
return this;
|
||||
}
|
||||
|
||||
/**
|
||||
* 清空所有Stage配置
|
||||
*
|
||||
* @return this(支持链式调用)
|
||||
*/
|
||||
public PhotoProcessContext clearStages() {
|
||||
stageEnabledMap.clear();
|
||||
return this;
|
||||
}
|
||||
|
||||
/**
|
||||
* 设置最终处理结果URL
|
||||
*/
|
||||
public void setResultUrl(String url) {
|
||||
this.resultUrl = url;
|
||||
if (resultUrlCallback != null) {
|
||||
resultUrlCallback.accept(url);
|
||||
}
|
||||
}
|
||||
|
||||
/**
|
||||
* 获取当前处理中的文件
|
||||
* 如果有processedFile则返回,否则返回originalFile
|
||||
*/
|
||||
public File getCurrentFile() {
|
||||
return processedFile != null ? processedFile : originalFile;
|
||||
}
|
||||
|
||||
/**
|
||||
* 更新处理后的文件
|
||||
*/
|
||||
public void updateProcessedFile(File newFile) {
|
||||
this.processedFile = newFile;
|
||||
tempFileManager.registerTempFile(newFile);
|
||||
}
|
||||
|
||||
/**
|
||||
* 清理所有临时文件
|
||||
*/
|
||||
@Override
|
||||
public void cleanup() {
|
||||
if (cleaned) {
|
||||
return;
|
||||
}
|
||||
tempFileManager.cleanup();
|
||||
cleaned = true;
|
||||
}
|
||||
|
||||
// ==================== Builder ====================
|
||||
|
||||
public static class Builder {
|
||||
private String processId;
|
||||
private String originalUrl;
|
||||
private Long scenicId;
|
||||
private ImageType imageType = ImageType.NORMAL_PHOTO;
|
||||
private Crop crop;
|
||||
private PipelineScene scene;
|
||||
private ImageSource source;
|
||||
private Consumer<String> resultUrlCallback;
|
||||
|
||||
public Builder processId(String processId) {
|
||||
this.processId = processId;
|
||||
return this;
|
||||
}
|
||||
|
||||
public Builder originalUrl(String originalUrl) {
|
||||
this.originalUrl = originalUrl;
|
||||
return this;
|
||||
}
|
||||
|
||||
public Builder scenicId(Long scenicId) {
|
||||
this.scenicId = scenicId;
|
||||
return this;
|
||||
}
|
||||
|
||||
public Builder imageType(ImageType imageType) {
|
||||
this.imageType = imageType;
|
||||
return this;
|
||||
}
|
||||
|
||||
public Builder crop(Crop crop) {
|
||||
this.crop = crop;
|
||||
return this;
|
||||
}
|
||||
|
||||
public Builder scene(PipelineScene scene) {
|
||||
this.scene = scene;
|
||||
return this;
|
||||
}
|
||||
|
||||
public Builder source(ImageSource source) {
|
||||
this.source = source;
|
||||
return this;
|
||||
}
|
||||
|
||||
public Builder resultUrlCallback(Consumer<String> callback) {
|
||||
this.resultUrlCallback = callback;
|
||||
return this;
|
||||
}
|
||||
|
||||
public PhotoProcessContext build() {
|
||||
// 参数校验
|
||||
if (originalUrl == null || originalUrl.isBlank()) {
|
||||
throw new IllegalArgumentException("originalUrl is required");
|
||||
}
|
||||
if (scenicId == null) {
|
||||
throw new IllegalArgumentException("scenicId is required");
|
||||
}
|
||||
// processId 可以自动生成
|
||||
if (processId == null || processId.isBlank()) {
|
||||
processId = UUID.randomUUID().toString();
|
||||
}
|
||||
return new PhotoProcessContext(this);
|
||||
}
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,63 @@
|
||||
package com.ycwl.basic.image.pipeline.enums;
|
||||
|
||||
/**
|
||||
* 图片来源枚举
|
||||
* 用于标识图片的拍摄或上传来源
|
||||
*/
|
||||
public enum ImageSource {
|
||||
|
||||
/**
|
||||
* IPC设备(IP Camera)
|
||||
* 景区固定安装的网络摄像头拍摄的照片
|
||||
*/
|
||||
IPC("ipc", "IPC设备"),
|
||||
|
||||
/**
|
||||
* 相机设备
|
||||
* 工作人员使用专业相机拍摄的照片
|
||||
*/
|
||||
CAMERA("camera", "相机设备"),
|
||||
|
||||
/**
|
||||
* 手机上传
|
||||
* 用户通过手机客户端上传的照片
|
||||
*/
|
||||
PHONE("phone", "手机上传"),
|
||||
|
||||
/**
|
||||
* 未知来源
|
||||
* 无法确定来源或其他特殊情况
|
||||
*/
|
||||
UNKNOWN("unknown", "未知来源");
|
||||
|
||||
private final String code;
|
||||
private final String description;
|
||||
|
||||
ImageSource(String code, String description) {
|
||||
this.code = code;
|
||||
this.description = description;
|
||||
}
|
||||
|
||||
public String getCode() {
|
||||
return code;
|
||||
}
|
||||
|
||||
public String getDescription() {
|
||||
return description;
|
||||
}
|
||||
|
||||
/**
|
||||
* 根据code获取枚举
|
||||
*/
|
||||
public static ImageSource fromCode(String code) {
|
||||
if (code == null) {
|
||||
return UNKNOWN;
|
||||
}
|
||||
for (ImageSource source : values()) {
|
||||
if (source.code.equalsIgnoreCase(code)) {
|
||||
return source;
|
||||
}
|
||||
}
|
||||
return UNKNOWN;
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,59 @@
|
||||
package com.ycwl.basic.image.pipeline.enums;
|
||||
|
||||
/**
|
||||
* 图片类型枚举
|
||||
* 用于区分管线处理的图片类型,替代通过 sourceId 判断的逻辑
|
||||
*/
|
||||
public enum ImageType {
|
||||
|
||||
/**
|
||||
* 普通照片
|
||||
* 对应原 sourceId > 0 的情况(IPC设备拍摄)
|
||||
*/
|
||||
NORMAL_PHOTO("normal", "普通照片"),
|
||||
|
||||
/**
|
||||
* 拼图
|
||||
* 对应原 sourceId == 0 的情况
|
||||
*/
|
||||
PUZZLE("puzzle", "拼图"),
|
||||
|
||||
/**
|
||||
* 手机上传
|
||||
* 对应原 sourceId == null 的情况
|
||||
*/
|
||||
MOBILE_UPLOAD("mobile", "手机上传");
|
||||
|
||||
private final String code;
|
||||
private final String description;
|
||||
|
||||
ImageType(String code, String description) {
|
||||
this.code = code;
|
||||
this.description = description;
|
||||
}
|
||||
|
||||
public String getCode() {
|
||||
return code;
|
||||
}
|
||||
|
||||
public String getDescription() {
|
||||
return description;
|
||||
}
|
||||
|
||||
/**
|
||||
* 从 sourceId 推断图片类型
|
||||
* 用于兼容现有数据结构
|
||||
*
|
||||
* @param sourceId 源ID(null=手机上传, 0=拼图, >0=普通照片)
|
||||
* @return 对应的图片类型
|
||||
*/
|
||||
public static ImageType fromSourceId(Long sourceId) {
|
||||
if (sourceId == null) {
|
||||
return MOBILE_UPLOAD;
|
||||
} else if (sourceId == 0) {
|
||||
return PUZZLE;
|
||||
} else {
|
||||
return NORMAL_PHOTO;
|
||||
}
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,63 @@
|
||||
package com.ycwl.basic.image.pipeline.enums;
|
||||
|
||||
/**
|
||||
* 管线处理场景枚举
|
||||
* 用于区分不同的图片处理业务场景
|
||||
*/
|
||||
public enum PipelineScene {
|
||||
|
||||
/**
|
||||
* 图片打印场景
|
||||
* 包括照片打印、拼图打印等
|
||||
*/
|
||||
IMAGE_PRINT("image_print", "图片打印"),
|
||||
|
||||
/**
|
||||
* 图片增强场景
|
||||
* 包括图片美化、滤镜处理等
|
||||
*/
|
||||
IMAGE_ENHANCE("image_enhance", "图片增强"),
|
||||
|
||||
/**
|
||||
* 源图片超分辨率增强场景
|
||||
* IPC设备拍摄的源图片进行质量提升
|
||||
*/
|
||||
SOURCE_PHOTO_SUPER_RESOLUTION("source_photo_sr", "源图片超分辨率增强"),
|
||||
|
||||
/**
|
||||
* AI相机照片增强场景
|
||||
* AI相机拍摄的照片进行超分辨率和质量增强
|
||||
*/
|
||||
AI_CAM_ENHANCE("ai_cam_enhance", "AI相机照片增强");
|
||||
|
||||
private final String code;
|
||||
private final String description;
|
||||
|
||||
PipelineScene(String code, String description) {
|
||||
this.code = code;
|
||||
this.description = description;
|
||||
}
|
||||
|
||||
public String getCode() {
|
||||
return code;
|
||||
}
|
||||
|
||||
public String getDescription() {
|
||||
return description;
|
||||
}
|
||||
|
||||
/**
|
||||
* 根据code获取枚举
|
||||
*/
|
||||
public static PipelineScene fromCode(String code) {
|
||||
if (code == null) {
|
||||
return null;
|
||||
}
|
||||
for (PipelineScene scene : values()) {
|
||||
if (scene.code.equals(code)) {
|
||||
return scene;
|
||||
}
|
||||
}
|
||||
return null;
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,25 @@
|
||||
package com.ycwl.basic.image.pipeline.exception;
|
||||
|
||||
import com.ycwl.basic.pipeline.exception.PipelineException;
|
||||
|
||||
/**
|
||||
* Stage执行异常
|
||||
*/
|
||||
public class StageExecutionException extends PipelineException {
|
||||
|
||||
private final String stageName;
|
||||
|
||||
public StageExecutionException(String stageName, String message) {
|
||||
super(String.format("Stage '%s' 执行失败: %s", stageName, message));
|
||||
this.stageName = stageName;
|
||||
}
|
||||
|
||||
public StageExecutionException(String stageName, String message, Throwable cause) {
|
||||
super(String.format("Stage '%s' 执行失败: %s", stageName, message), cause);
|
||||
this.stageName = stageName;
|
||||
}
|
||||
|
||||
public String getStageName() {
|
||||
return stageName;
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,47 @@
|
||||
package com.ycwl.basic.image.pipeline.stages;
|
||||
|
||||
import com.ycwl.basic.image.pipeline.core.PhotoProcessContext;
|
||||
import com.ycwl.basic.pipeline.annotation.StageConfig;
|
||||
import com.ycwl.basic.pipeline.core.AbstractPipelineStage;
|
||||
import com.ycwl.basic.pipeline.core.StageResult;
|
||||
import com.ycwl.basic.pipeline.enums.StageOptionalMode;
|
||||
import lombok.extern.slf4j.Slf4j;
|
||||
|
||||
/**
|
||||
* 清理临时文件Stage
|
||||
* 总是在管线最后执行,清理所有临时文件
|
||||
*/
|
||||
@Slf4j
|
||||
@StageConfig(
|
||||
stageId = "cleanup",
|
||||
optionalMode = StageOptionalMode.FORCE_ON,
|
||||
description = "清理临时文件",
|
||||
defaultEnabled = true
|
||||
)
|
||||
public class CleanupStage extends AbstractPipelineStage<PhotoProcessContext> {
|
||||
|
||||
@Override
|
||||
public String getName() {
|
||||
return "CleanupStage";
|
||||
}
|
||||
|
||||
@Override
|
||||
public int getPriority() {
|
||||
return 999;
|
||||
}
|
||||
|
||||
@Override
|
||||
protected StageResult<PhotoProcessContext> doExecute(PhotoProcessContext context) {
|
||||
try {
|
||||
int fileCount = context.getTempFileManager().getTempFileCount();
|
||||
context.cleanup();
|
||||
|
||||
log.info("临时文件清理完成: 共{}个文件", fileCount);
|
||||
return StageResult.success(String.format("已清理 %d 个临时文件", fileCount));
|
||||
|
||||
} catch (Exception e) {
|
||||
log.warn("临时文件清理失败,但不影响主流程", e);
|
||||
return StageResult.degraded("清理失败: " + e.getMessage());
|
||||
}
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,92 @@
|
||||
package com.ycwl.basic.image.pipeline.stages;
|
||||
|
||||
import com.ycwl.basic.image.pipeline.core.PhotoProcessContext;
|
||||
import com.ycwl.basic.image.pipeline.enums.ImageType;
|
||||
import com.ycwl.basic.pipeline.annotation.StageConfig;
|
||||
import com.ycwl.basic.pipeline.core.AbstractPipelineStage;
|
||||
import com.ycwl.basic.pipeline.core.StageResult;
|
||||
import com.ycwl.basic.pipeline.enums.StageOptionalMode;
|
||||
import com.ycwl.basic.utils.ImageUtils;
|
||||
import lombok.extern.slf4j.Slf4j;
|
||||
|
||||
import java.io.File;
|
||||
|
||||
/**
|
||||
* 条件旋转Stage
|
||||
* 根据图片需要旋转的角度进行旋转(便于后续水印处理)
|
||||
*/
|
||||
@Slf4j
|
||||
@StageConfig(
|
||||
stageId = "rotate",
|
||||
optionalMode = StageOptionalMode.UNSUPPORT,
|
||||
description = "根据需要旋转图片"
|
||||
)
|
||||
public class ConditionalRotateStage extends AbstractPipelineStage<PhotoProcessContext> {
|
||||
|
||||
@Override
|
||||
public String getName() {
|
||||
return "ConditionalRotateStage";
|
||||
}
|
||||
|
||||
@Override
|
||||
protected boolean shouldExecuteByBusinessLogic(PhotoProcessContext context) {
|
||||
Integer rotation = context.getImageRotation();
|
||||
return context.getImageType() == ImageType.NORMAL_PHOTO && (rotation != null && rotation != 0);
|
||||
}
|
||||
|
||||
@Override
|
||||
protected StageResult<PhotoProcessContext> doExecute(PhotoProcessContext context) {
|
||||
try {
|
||||
File currentFile = context.getCurrentFile();
|
||||
if (currentFile == null || !currentFile.exists()) {
|
||||
return StageResult.failed("当前文件不存在");
|
||||
}
|
||||
|
||||
// 获取需要旋转的角度
|
||||
Integer rotation = context.getImageRotation();
|
||||
if (rotation == null || rotation == 0) {
|
||||
return StageResult.skipped("无需旋转");
|
||||
}
|
||||
|
||||
File rotatedFile = context.getTempFileManager()
|
||||
.createTempFile("rotated", ".jpg");
|
||||
|
||||
// 根据实际角度进行旋转
|
||||
log.debug("旋转图片{}度: {} -> {}", rotation, currentFile.getName(), rotatedFile.getName());
|
||||
rotateByDegree(currentFile, rotatedFile, rotation);
|
||||
|
||||
if (!rotatedFile.exists()) {
|
||||
return StageResult.failed("旋转后的文件未生成");
|
||||
}
|
||||
|
||||
context.updateProcessedFile(rotatedFile);
|
||||
context.setRotationApplied(true);
|
||||
|
||||
log.info("图片已旋转{}度", rotation);
|
||||
return StageResult.success("已旋转" + rotation + "度");
|
||||
|
||||
} catch (Exception e) {
|
||||
log.error("图片旋转失败", e);
|
||||
return StageResult.failed("旋转失败: " + e.getMessage(), e);
|
||||
}
|
||||
}
|
||||
|
||||
/**
|
||||
* 根据角度旋转图片
|
||||
*/
|
||||
private void rotateByDegree(File input, File output, int degree) throws Exception {
|
||||
switch (degree) {
|
||||
case 90:
|
||||
ImageUtils.rotateImage90(input, output);
|
||||
break;
|
||||
case 180:
|
||||
ImageUtils.rotateImage180(input, output);
|
||||
break;
|
||||
case 270:
|
||||
ImageUtils.rotateImage270(input, output);
|
||||
break;
|
||||
default:
|
||||
throw new IllegalArgumentException("不支持的旋转角度: " + degree);
|
||||
}
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,76 @@
|
||||
package com.ycwl.basic.image.pipeline.stages;
|
||||
|
||||
import cn.hutool.http.HttpUtil;
|
||||
import com.ycwl.basic.image.pipeline.core.PhotoProcessContext;
|
||||
import com.ycwl.basic.image.pipeline.enums.ImageType;
|
||||
import com.ycwl.basic.pipeline.annotation.StageConfig;
|
||||
import com.ycwl.basic.pipeline.core.AbstractPipelineStage;
|
||||
import com.ycwl.basic.pipeline.core.StageResult;
|
||||
import com.ycwl.basic.pipeline.enums.StageOptionalMode;
|
||||
import lombok.extern.slf4j.Slf4j;
|
||||
import org.apache.commons.lang3.StringUtils;
|
||||
|
||||
import java.io.File;
|
||||
|
||||
/**
|
||||
* 下载图片Stage
|
||||
* 从URL下载原图到本地临时文件
|
||||
*/
|
||||
@Slf4j
|
||||
@StageConfig(
|
||||
stageId = "download",
|
||||
optionalMode = StageOptionalMode.FORCE_ON,
|
||||
description = "下载图片",
|
||||
defaultEnabled = true
|
||||
)
|
||||
public class DownloadStage extends AbstractPipelineStage<PhotoProcessContext> {
|
||||
|
||||
@Override
|
||||
public String getName() {
|
||||
return "DownloadStage";
|
||||
}
|
||||
|
||||
@Override
|
||||
protected StageResult<PhotoProcessContext> doExecute(PhotoProcessContext context) {
|
||||
String url = context.getOriginalUrl();
|
||||
if (StringUtils.isBlank(url)) {
|
||||
return StageResult.failed("原图URL为空");
|
||||
}
|
||||
|
||||
try {
|
||||
String fileExtension = determineFileExtension(context);
|
||||
String filePrefix = context.getImageType() == ImageType.PUZZLE ? "puzzle" : "print";
|
||||
|
||||
File downloadFile = context.getTempFileManager()
|
||||
.createTempFile(filePrefix, fileExtension);
|
||||
|
||||
log.debug("开始下载图片: {} -> {}", url, downloadFile.getName());
|
||||
HttpUtil.downloadFile(url, downloadFile);
|
||||
|
||||
if (!downloadFile.exists() || downloadFile.length() == 0) {
|
||||
return StageResult.failed("下载的文件不存在或为空");
|
||||
}
|
||||
|
||||
context.setOriginalFile(downloadFile);
|
||||
log.info("图片下载成功: {} ({}KB)", downloadFile.getName(),
|
||||
downloadFile.length() / 1024);
|
||||
|
||||
return StageResult.success(String.format("已下载 %dKB", downloadFile.length() / 1024));
|
||||
|
||||
} catch (Exception e) {
|
||||
log.error("图片下载失败: {}", url, e);
|
||||
return StageResult.failed("下载失败: " + e.getMessage(), e);
|
||||
}
|
||||
}
|
||||
|
||||
private String determineFileExtension(PhotoProcessContext context) {
|
||||
if (context.getImageType() == ImageType.PUZZLE) {
|
||||
return ".png";
|
||||
}
|
||||
String url = context.getOriginalUrl();
|
||||
if (url.toLowerCase().endsWith(".png")) {
|
||||
return ".png";
|
||||
}
|
||||
return ".jpg";
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,187 @@
|
||||
package com.ycwl.basic.image.pipeline.stages;
|
||||
|
||||
import com.ycwl.basic.image.enhancer.adapter.BceImageEnhancer;
|
||||
import com.ycwl.basic.image.enhancer.entity.BceEnhancerConfig;
|
||||
import com.ycwl.basic.image.pipeline.core.PhotoProcessContext;
|
||||
import com.ycwl.basic.image.pipeline.enums.ImageSource;
|
||||
import com.ycwl.basic.pipeline.annotation.StageConfig;
|
||||
import com.ycwl.basic.pipeline.core.AbstractPipelineStage;
|
||||
import com.ycwl.basic.pipeline.core.StageResult;
|
||||
import com.ycwl.basic.pipeline.enums.StageOptionalMode;
|
||||
import lombok.extern.slf4j.Slf4j;
|
||||
import org.springframework.web.multipart.MultipartFile;
|
||||
|
||||
import java.io.File;
|
||||
import java.io.FileOutputStream;
|
||||
import java.io.IOException;
|
||||
|
||||
/**
|
||||
* 图像增强Stage
|
||||
* 使用百度云图像增强API提升图片清晰度和质量
|
||||
*
|
||||
* 支持两种增强模式:
|
||||
* 1. 图像清晰度增强 (BceImageEnhancer)
|
||||
* 2. 图像超分辨率 (BceImageSR)
|
||||
*/
|
||||
@Slf4j
|
||||
@StageConfig(
|
||||
stageId = "image_enhance",
|
||||
optionalMode = StageOptionalMode.SUPPORT,
|
||||
description = "图像增强处理",
|
||||
defaultEnabled = false // 默认不启用,需要外部配置开启
|
||||
)
|
||||
public class ImageEnhanceStage extends AbstractPipelineStage<PhotoProcessContext> {
|
||||
|
||||
private BceEnhancerConfig enhancerConfig;
|
||||
|
||||
/**
|
||||
* 构造函数 - 使用默认配置
|
||||
*/
|
||||
public ImageEnhanceStage() {
|
||||
this(buildConfigFromEnvironment());
|
||||
}
|
||||
|
||||
/**
|
||||
* 构造函数 - 使用自定义配置
|
||||
*
|
||||
* @param enhancerConfig 图像增强配置
|
||||
*/
|
||||
public ImageEnhanceStage(BceEnhancerConfig enhancerConfig) {
|
||||
this.enhancerConfig = enhancerConfig;
|
||||
}
|
||||
|
||||
@Override
|
||||
public String getName() {
|
||||
return "ImageEnhanceStage";
|
||||
}
|
||||
|
||||
@Override
|
||||
protected boolean shouldExecuteByBusinessLogic(PhotoProcessContext context) {
|
||||
// 仅对照片源为IPC的图片进行增强
|
||||
return context.getSource() == ImageSource.IPC;
|
||||
}
|
||||
|
||||
@Override
|
||||
protected StageResult<PhotoProcessContext> doExecute(PhotoProcessContext context) {
|
||||
// 检查配置是否完整
|
||||
if (!isConfigValid()) {
|
||||
log.warn("图像增强配置不完整,跳过增强处理。请在ImageEnhanceStage中配置百度云API凭证");
|
||||
return StageResult.skipped("配置不完整,跳过增强");
|
||||
}
|
||||
|
||||
File currentFile = context.getCurrentFile();
|
||||
if (currentFile == null || !currentFile.exists()) {
|
||||
return StageResult.skipped("当前文件不存在");
|
||||
}
|
||||
|
||||
try {
|
||||
log.debug("开始图像增强: {}", currentFile.getName());
|
||||
|
||||
// 创建百度云图像增强客户端
|
||||
BceImageEnhancer enhancer = new BceImageEnhancer();
|
||||
enhancer.setConfig(enhancerConfig);
|
||||
|
||||
// 调用图像增强API
|
||||
// 注意:百度云API需要传入图片URL,这里使用本地文件的绝对路径
|
||||
String imageUrl = currentFile.getAbsolutePath();
|
||||
MultipartFile enhancedImage = enhancer.enhance(imageUrl);
|
||||
|
||||
if (enhancedImage == null || enhancedImage.isEmpty()) {
|
||||
log.warn("图像增强返回空结果,可能是API调用失败");
|
||||
return StageResult.degraded("增强失败,使用原图");
|
||||
}
|
||||
|
||||
// 保存增强后的图片到临时文件
|
||||
File enhancedFile = context.getTempFileManager()
|
||||
.createTempFile("enhanced", ".jpg");
|
||||
|
||||
saveMultipartFileToFile(enhancedImage, enhancedFile);
|
||||
|
||||
if (!enhancedFile.exists() || enhancedFile.length() == 0) {
|
||||
return StageResult.degraded("增强结果保存失败,使用原图");
|
||||
}
|
||||
|
||||
// 更新处理后的文件
|
||||
context.updateProcessedFile(enhancedFile);
|
||||
|
||||
long originalSize = currentFile.length();
|
||||
long enhancedSize = enhancedFile.length();
|
||||
double sizeRatio = (double) enhancedSize / originalSize;
|
||||
|
||||
log.info("图像增强完成: 原始{}KB -> 增强后{}KB (比例: {})",
|
||||
originalSize / 1024,
|
||||
enhancedSize / 1024,
|
||||
String.format("%.2f", sizeRatio));
|
||||
|
||||
return StageResult.success(String.format("已增强 (%dKB -> %dKB)",
|
||||
originalSize / 1024,
|
||||
enhancedSize / 1024));
|
||||
|
||||
} catch (Exception e) {
|
||||
log.error("图像增强失败: {}", e.getMessage(), e);
|
||||
|
||||
// 增强失败时返回降级状态,继续使用原图
|
||||
return StageResult.degraded("增强失败: " + e.getMessage());
|
||||
}
|
||||
}
|
||||
|
||||
/**
|
||||
* 检查配置是否有效
|
||||
*/
|
||||
private boolean isConfigValid() {
|
||||
if (enhancerConfig == null) {
|
||||
return false;
|
||||
}
|
||||
|
||||
String appId = enhancerConfig.getAppId();
|
||||
String apiKey = enhancerConfig.getApiKey();
|
||||
String secretKey = enhancerConfig.getSecretKey();
|
||||
|
||||
// 检查字段是否为 null 或空
|
||||
if (appId == null || appId.isBlank() ||
|
||||
apiKey == null || apiKey.isBlank() ||
|
||||
secretKey == null || secretKey.isBlank()) {
|
||||
return false;
|
||||
}
|
||||
|
||||
// 检查是否包含 TODO 占位符
|
||||
if (appId.contains("TODO") || apiKey.contains("TODO") || secretKey.contains("TODO")) {
|
||||
return false;
|
||||
}
|
||||
|
||||
return true;
|
||||
}
|
||||
|
||||
private static BceEnhancerConfig buildConfigFromEnvironment() {
|
||||
BceEnhancerConfig config = new BceEnhancerConfig();
|
||||
config.setAppId(System.getenv("BCE_IMAGE_APP_ID"));
|
||||
config.setApiKey(System.getenv("BCE_IMAGE_API_KEY"));
|
||||
config.setSecretKey(System.getenv("BCE_IMAGE_SECRET_KEY"));
|
||||
config.setQps(1.0f);
|
||||
return config;
|
||||
}
|
||||
|
||||
/**
|
||||
* 保存MultipartFile到本地文件
|
||||
*/
|
||||
private void saveMultipartFileToFile(MultipartFile multipartFile, File targetFile) throws IOException {
|
||||
try (FileOutputStream fos = new FileOutputStream(targetFile)) {
|
||||
fos.write(multipartFile.getBytes());
|
||||
fos.flush();
|
||||
}
|
||||
}
|
||||
|
||||
/**
|
||||
* 获取当前配置(用于调试)
|
||||
*/
|
||||
public BceEnhancerConfig getEnhancerConfig() {
|
||||
return enhancerConfig;
|
||||
}
|
||||
|
||||
/**
|
||||
* 设置配置(用于动态配置)
|
||||
*/
|
||||
public void setEnhancerConfig(BceEnhancerConfig enhancerConfig) {
|
||||
this.enhancerConfig = enhancerConfig;
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,94 @@
|
||||
package com.ycwl.basic.image.pipeline.stages;
|
||||
|
||||
import com.ycwl.basic.image.pipeline.core.PhotoProcessContext;
|
||||
import com.ycwl.basic.image.pipeline.enums.ImageSource;
|
||||
import com.ycwl.basic.image.pipeline.enums.ImageType;
|
||||
import com.ycwl.basic.model.Crop;
|
||||
import com.ycwl.basic.pipeline.annotation.StageConfig;
|
||||
import com.ycwl.basic.pipeline.core.AbstractPipelineStage;
|
||||
import com.ycwl.basic.pipeline.core.StageResult;
|
||||
import com.ycwl.basic.pipeline.enums.StageOptionalMode;
|
||||
import com.ycwl.basic.utils.ImageUtils;
|
||||
import lombok.extern.slf4j.Slf4j;
|
||||
|
||||
import java.io.File;
|
||||
|
||||
/**
|
||||
* 图片方向检测Stage
|
||||
* 检测图片是横图还是竖图,并记录到Context
|
||||
*/
|
||||
@Slf4j
|
||||
@StageConfig(
|
||||
stageId = "orientation",
|
||||
optionalMode = StageOptionalMode.UNSUPPORT,
|
||||
description = "图片方向检测"
|
||||
)
|
||||
public class ImageOrientationStage extends AbstractPipelineStage<PhotoProcessContext> {
|
||||
|
||||
@Override
|
||||
public String getName() {
|
||||
return "ImageOrientationStage";
|
||||
}
|
||||
|
||||
@Override
|
||||
protected boolean shouldExecuteByBusinessLogic(PhotoProcessContext context) {
|
||||
return context.getImageType() == ImageType.NORMAL_PHOTO;
|
||||
}
|
||||
|
||||
@Override
|
||||
protected StageResult<PhotoProcessContext> doExecute(PhotoProcessContext context) {
|
||||
try {
|
||||
File currentFile = context.getCurrentFile();
|
||||
if (currentFile == null || !currentFile.exists()) {
|
||||
return StageResult.failed("当前文件不存在");
|
||||
}
|
||||
|
||||
boolean isLandscape = detectOrientation(currentFile, context.getCrop());
|
||||
context.setLandscape(isLandscape);
|
||||
|
||||
// 保存 rotation 信息到 context,方便后续 Stage 使用
|
||||
if (context.getCrop() != null && context.getCrop().getRotation() != null) {
|
||||
context.setImageRotation(context.getCrop().getRotation());
|
||||
}
|
||||
|
||||
String orientation = isLandscape ? "横图" : "竖图";
|
||||
log.info("图片方向检测: {}", orientation);
|
||||
|
||||
return StageResult.success(orientation);
|
||||
|
||||
} catch (Exception e) {
|
||||
log.error("图片方向检测失败", e);
|
||||
return StageResult.failed("方向检测失败: " + e.getMessage(), e);
|
||||
}
|
||||
}
|
||||
|
||||
/**
|
||||
* 检测图片方向
|
||||
* 综合考虑物理分辨率和rotation字段
|
||||
* rotation表示"需要旋转多少度照片才能正确显示"
|
||||
*/
|
||||
private boolean detectOrientation(File imageFile, Crop crop) {
|
||||
try {
|
||||
// 先获取物理分辨率方向
|
||||
boolean physicalLandscape = ImageUtils.isLandscape(imageFile);
|
||||
|
||||
// 如果有rotation信息,需要综合判断
|
||||
if (crop != null && crop.getRotation() != null) {
|
||||
int rotation = crop.getRotation();
|
||||
// rotation=90/270 会翻转方向
|
||||
boolean isLandscape = (rotation == 90 || rotation == 270) ? !physicalLandscape : physicalLandscape;
|
||||
log.debug("综合判断方向: physicalLandscape={}, rotation={}, finalLandscape={}",
|
||||
physicalLandscape, rotation, isLandscape);
|
||||
return isLandscape;
|
||||
}
|
||||
|
||||
// 没有rotation信息,直接使用物理分辨率
|
||||
log.debug("从图片尺寸判断方向: isLandscape={}", physicalLandscape);
|
||||
return physicalLandscape;
|
||||
|
||||
} catch (Exception e) {
|
||||
log.warn("从图片尺寸判断方向失败,默认为横图: {}", e.getMessage());
|
||||
return true;
|
||||
}
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,124 @@
|
||||
package com.ycwl.basic.image.pipeline.stages;
|
||||
|
||||
import com.ycwl.basic.image.pipeline.core.PhotoProcessContext;
|
||||
import com.ycwl.basic.image.pipeline.enums.ImageType;
|
||||
import com.ycwl.basic.pipeline.annotation.StageConfig;
|
||||
import com.ycwl.basic.pipeline.core.AbstractPipelineStage;
|
||||
import com.ycwl.basic.pipeline.core.StageResult;
|
||||
import com.ycwl.basic.pipeline.enums.StageOptionalMode;
|
||||
import com.ycwl.basic.utils.ImageUtils;
|
||||
import lombok.extern.slf4j.Slf4j;
|
||||
|
||||
import java.io.File;
|
||||
|
||||
/**
|
||||
* 图像质量检测Stage
|
||||
* 检测图片质量,如果检测到质量不佳则动态添加ImageEnhanceStage
|
||||
*
|
||||
* 此Stage展示了如何在运行时动态添加后续Stage的能力
|
||||
*/
|
||||
@Slf4j
|
||||
@StageConfig(
|
||||
stageId = "quality_check",
|
||||
optionalMode = StageOptionalMode.SUPPORT,
|
||||
description = "图像质量检测",
|
||||
defaultEnabled = false // 默认不启用
|
||||
)
|
||||
public class ImageQualityCheckStage extends AbstractPipelineStage<PhotoProcessContext> {
|
||||
|
||||
/**
|
||||
* 质量阈值:图片尺寸小于此阈值认为质量不佳
|
||||
* 例如:小于100KB的图片可能需要增强
|
||||
*/
|
||||
private static final long QUALITY_THRESHOLD_BYTES = 100 * 1024; // 100KB
|
||||
|
||||
/**
|
||||
* 分辨率阈值:图片分辨率低于此值认为质量不佳
|
||||
*/
|
||||
private static final int MIN_WIDTH = 800;
|
||||
private static final int MIN_HEIGHT = 600;
|
||||
|
||||
@Override
|
||||
public String getName() {
|
||||
return "ImageQualityCheckStage";
|
||||
}
|
||||
|
||||
@Override
|
||||
protected boolean shouldExecuteByBusinessLogic(PhotoProcessContext context) {
|
||||
// 仅对普通照片执行质量检测
|
||||
return context.getImageType() == ImageType.NORMAL_PHOTO;
|
||||
}
|
||||
|
||||
@Override
|
||||
protected StageResult<PhotoProcessContext> doExecute(PhotoProcessContext context) {
|
||||
File currentFile = context.getCurrentFile();
|
||||
if (currentFile == null || !currentFile.exists()) {
|
||||
return StageResult.failed("当前文件不存在");
|
||||
}
|
||||
|
||||
try {
|
||||
// 检查文件大小
|
||||
long fileSize = currentFile.length();
|
||||
log.debug("图像质量检测: 文件大小={}KB, 阈值={}KB",
|
||||
fileSize / 1024,
|
||||
QUALITY_THRESHOLD_BYTES / 1024);
|
||||
|
||||
boolean needsEnhancement = false;
|
||||
String reason = "";
|
||||
|
||||
// 检查:文件大小
|
||||
if (fileSize < QUALITY_THRESHOLD_BYTES) {
|
||||
needsEnhancement = true;
|
||||
reason = String.format("文件过小(%dKB < %dKB)",
|
||||
fileSize / 1024,
|
||||
QUALITY_THRESHOLD_BYTES / 1024);
|
||||
}
|
||||
|
||||
// TODO: 可以添加更多质量检测维度
|
||||
// 例如:使用BufferedImage读取图片获取分辨率
|
||||
// 例如:使用OpenCV进行图片质量评估
|
||||
// 例如:检查图片的EXIF信息
|
||||
|
||||
// 如果需要增强,动态添加ImageEnhanceStage
|
||||
if (needsEnhancement) {
|
||||
log.info("检测到图片质量不佳({}), 动态添加ImageEnhanceStage", reason);
|
||||
|
||||
ImageEnhanceStage enhanceStage = new ImageEnhanceStage();
|
||||
|
||||
// 使用successWithNext返回,动态添加后续Stage
|
||||
return StageResult.successWithNext(
|
||||
"质量不佳,添加增强Stage: " + reason,
|
||||
enhanceStage
|
||||
);
|
||||
}
|
||||
|
||||
// 质量良好,无需增强
|
||||
log.info("图像质量良好,无需增强: {}KB", fileSize / 1024);
|
||||
return StageResult.success("质量良好,无需增强");
|
||||
|
||||
} catch (Exception e) {
|
||||
log.error("图像质量检测失败", e);
|
||||
// 检测失败时不影响管线,继续执行
|
||||
return StageResult.degraded("质量检测失败: " + e.getMessage());
|
||||
}
|
||||
}
|
||||
|
||||
/**
|
||||
* 示例:也可以根据其他条件动态添加不同的Stage
|
||||
*/
|
||||
@SuppressWarnings("unused")
|
||||
private StageResult<PhotoProcessContext> checkAndAddMultipleStages(PhotoProcessContext context) {
|
||||
// 示例:根据不同条件添加多个Stage
|
||||
|
||||
// 假设检测到需要多个增强操作
|
||||
ImageEnhanceStage enhanceStage = new ImageEnhanceStage();
|
||||
// 其他Stage...
|
||||
|
||||
// 可以一次性添加多个Stage
|
||||
return StageResult.successWithNext(
|
||||
"检测到需要多重处理",
|
||||
enhanceStage
|
||||
// 可以添加更多Stage
|
||||
);
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,126 @@
|
||||
package com.ycwl.basic.image.pipeline.stages;
|
||||
|
||||
import com.ycwl.basic.image.pipeline.core.PhotoProcessContext;
|
||||
import com.ycwl.basic.pipeline.annotation.StageConfig;
|
||||
import com.ycwl.basic.pipeline.core.AbstractPipelineStage;
|
||||
import com.ycwl.basic.pipeline.core.StageResult;
|
||||
import com.ycwl.basic.pipeline.enums.StageOptionalMode;
|
||||
import lombok.extern.slf4j.Slf4j;
|
||||
|
||||
import javax.imageio.ImageIO;
|
||||
import java.awt.Graphics2D;
|
||||
import java.awt.RenderingHints;
|
||||
import java.awt.image.BufferedImage;
|
||||
import java.io.File;
|
||||
|
||||
/**
|
||||
* 图像缩放Stage
|
||||
* 支持按比例放大或缩小图片
|
||||
*/
|
||||
@Slf4j
|
||||
@StageConfig(
|
||||
stageId = "image_resize",
|
||||
optionalMode = StageOptionalMode.SUPPORT,
|
||||
description = "图像缩放处理",
|
||||
defaultEnabled = true
|
||||
)
|
||||
public class ImageResizeStage extends AbstractPipelineStage<PhotoProcessContext> {
|
||||
|
||||
private final double scaleFactor;
|
||||
|
||||
/**
|
||||
* 构造函数
|
||||
* @param scaleFactor 缩放比例(例如: 1.5表示放大1.5倍, 0.333表示缩小到1/3)
|
||||
*/
|
||||
public ImageResizeStage(double scaleFactor) {
|
||||
if (scaleFactor <= 0) {
|
||||
throw new IllegalArgumentException("scaleFactor must be positive");
|
||||
}
|
||||
this.scaleFactor = scaleFactor;
|
||||
}
|
||||
|
||||
@Override
|
||||
public String getName() {
|
||||
return "ImageResizeStage";
|
||||
}
|
||||
|
||||
@Override
|
||||
protected StageResult<PhotoProcessContext> doExecute(PhotoProcessContext context) {
|
||||
File currentFile = context.getCurrentFile();
|
||||
if (currentFile == null || !currentFile.exists()) {
|
||||
return StageResult.skipped("当前文件不存在");
|
||||
}
|
||||
|
||||
BufferedImage originalImage = null;
|
||||
BufferedImage resizedImage = null;
|
||||
|
||||
try {
|
||||
log.debug("开始图像缩放处理: file={}, scaleFactor={}", currentFile.getName(), scaleFactor);
|
||||
|
||||
// 读取原图
|
||||
originalImage = ImageIO.read(currentFile);
|
||||
if (originalImage == null) {
|
||||
return StageResult.failed("无法读取图片文件");
|
||||
}
|
||||
|
||||
int originalWidth = originalImage.getWidth();
|
||||
int originalHeight = originalImage.getHeight();
|
||||
|
||||
// 计算新尺寸
|
||||
int newWidth = (int) Math.round(originalWidth * scaleFactor);
|
||||
int newHeight = (int) Math.round(originalHeight * scaleFactor);
|
||||
|
||||
// 检查尺寸是否合理
|
||||
if (newWidth <= 0 || newHeight <= 0) {
|
||||
return StageResult.failed("缩放后尺寸无效: " + newWidth + "x" + newHeight);
|
||||
}
|
||||
|
||||
// 创建缩放后的图像
|
||||
resizedImage = new BufferedImage(newWidth, newHeight, BufferedImage.TYPE_INT_RGB);
|
||||
Graphics2D g2d = resizedImage.createGraphics();
|
||||
|
||||
try {
|
||||
// 设置高质量渲染选项
|
||||
g2d.setRenderingHint(RenderingHints.KEY_INTERPOLATION, RenderingHints.VALUE_INTERPOLATION_BICUBIC);
|
||||
g2d.setRenderingHint(RenderingHints.KEY_RENDERING, RenderingHints.VALUE_RENDER_QUALITY);
|
||||
g2d.setRenderingHint(RenderingHints.KEY_ANTIALIASING, RenderingHints.VALUE_ANTIALIAS_ON);
|
||||
|
||||
// 执行缩放
|
||||
g2d.drawImage(originalImage, 0, 0, newWidth, newHeight, null);
|
||||
} finally {
|
||||
g2d.dispose();
|
||||
}
|
||||
|
||||
// 保存缩放后的图片
|
||||
File resizedFile = context.getTempFileManager().createTempFile("resized", ".jpg");
|
||||
ImageIO.write(resizedImage, "jpg", resizedFile);
|
||||
|
||||
if (!resizedFile.exists() || resizedFile.length() == 0) {
|
||||
return StageResult.failed("缩放后图片保存失败");
|
||||
}
|
||||
|
||||
// 更新处理后的文件
|
||||
context.updateProcessedFile(resizedFile);
|
||||
|
||||
log.info("图像缩放完成: {}x{} -> {}x{} (比例: {})",
|
||||
originalWidth, originalHeight,
|
||||
newWidth, newHeight,
|
||||
scaleFactor);
|
||||
|
||||
return StageResult.success(String.format("缩放完成 (%dx%d -> %dx%d)",
|
||||
originalWidth, originalHeight, newWidth, newHeight));
|
||||
|
||||
} catch (Exception e) {
|
||||
log.error("图像缩放失败: {}", e.getMessage(), e);
|
||||
return StageResult.failed("缩放失败: " + e.getMessage(), e);
|
||||
} finally {
|
||||
// 释放图像资源
|
||||
if (originalImage != null) {
|
||||
originalImage.flush();
|
||||
}
|
||||
if (resizedImage != null) {
|
||||
resizedImage.flush();
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,190 @@
|
||||
package com.ycwl.basic.image.pipeline.stages;
|
||||
|
||||
import com.ycwl.basic.image.enhancer.adapter.BceImageSR;
|
||||
import com.ycwl.basic.image.enhancer.entity.BceEnhancerConfig;
|
||||
import com.ycwl.basic.image.pipeline.core.PhotoProcessContext;
|
||||
import com.ycwl.basic.image.pipeline.enums.ImageSource;
|
||||
import com.ycwl.basic.pipeline.annotation.StageConfig;
|
||||
import com.ycwl.basic.pipeline.core.AbstractPipelineStage;
|
||||
import com.ycwl.basic.pipeline.core.StageResult;
|
||||
import com.ycwl.basic.pipeline.enums.StageOptionalMode;
|
||||
import lombok.extern.slf4j.Slf4j;
|
||||
import org.springframework.web.multipart.MultipartFile;
|
||||
|
||||
import java.io.File;
|
||||
import java.io.FileOutputStream;
|
||||
import java.io.IOException;
|
||||
|
||||
/**
|
||||
* 图像超分辨率Stage
|
||||
* 使用百度云图像超分辨率API提升图片分辨率和清晰度
|
||||
*
|
||||
* 与ImageEnhanceStage的区别:
|
||||
* - ImageEnhanceStage: 使用BceImageEnhancer进行清晰度增强
|
||||
* - ImageSRStage: 使用BceImageSR进行超分辨率处理
|
||||
*/
|
||||
@Slf4j
|
||||
@StageConfig(
|
||||
stageId = "image_sr",
|
||||
optionalMode = StageOptionalMode.SUPPORT,
|
||||
description = "图像超分辨率处理",
|
||||
defaultEnabled = false // 默认不启用,需要外部配置开启
|
||||
)
|
||||
public class ImageSRStage extends AbstractPipelineStage<PhotoProcessContext> {
|
||||
|
||||
private BceEnhancerConfig enhancerConfig;
|
||||
|
||||
/**
|
||||
* 构造函数 - 使用默认配置
|
||||
*/
|
||||
public ImageSRStage() {
|
||||
this(buildConfigFromEnvironment());
|
||||
}
|
||||
|
||||
/**
|
||||
* 构造函数 - 使用自定义配置
|
||||
*
|
||||
* @param enhancerConfig 图像增强配置
|
||||
*/
|
||||
public ImageSRStage(BceEnhancerConfig enhancerConfig) {
|
||||
this.enhancerConfig = enhancerConfig;
|
||||
}
|
||||
|
||||
@Override
|
||||
public String getName() {
|
||||
return "ImageSRStage";
|
||||
}
|
||||
|
||||
@Override
|
||||
protected boolean shouldExecuteByBusinessLogic(PhotoProcessContext context) {
|
||||
// 仅对照片源为IPC的图片进行超分辨率处理
|
||||
return context.getSource() == ImageSource.IPC;
|
||||
}
|
||||
|
||||
@Override
|
||||
protected StageResult<PhotoProcessContext> doExecute(PhotoProcessContext context) {
|
||||
// 检查配置是否完整
|
||||
if (!isConfigValid()) {
|
||||
log.warn("图像超分辨率配置不完整,跳过处理。请配置百度云API凭证");
|
||||
return StageResult.skipped("配置不完整,跳过超分辨率");
|
||||
}
|
||||
|
||||
File currentFile = context.getCurrentFile();
|
||||
if (currentFile == null || !currentFile.exists()) {
|
||||
return StageResult.skipped("当前文件不存在");
|
||||
}
|
||||
|
||||
try {
|
||||
log.debug("开始图像超分辨率处理: {}", currentFile.getName());
|
||||
|
||||
// 创建百度云图像超分辨率客户端
|
||||
BceImageSR srEnhancer = new BceImageSR();
|
||||
srEnhancer.setConfig(enhancerConfig);
|
||||
|
||||
// 调用图像超分辨率API
|
||||
// 注意:百度云API需要传入图片URL,这里使用本地文件的绝对路径
|
||||
String imageUrl = currentFile.getAbsolutePath();
|
||||
MultipartFile enhancedImage = srEnhancer.enhance(imageUrl);
|
||||
|
||||
if (enhancedImage == null || enhancedImage.isEmpty()) {
|
||||
log.warn("图像超分辨率返回空结果,可能是API调用失败");
|
||||
return StageResult.degraded("超分辨率失败,使用原图");
|
||||
}
|
||||
|
||||
// 保存超分辨率后的图片到临时文件
|
||||
File enhancedFile = context.getTempFileManager()
|
||||
.createTempFile("sr_enhanced", ".jpg");
|
||||
|
||||
saveMultipartFileToFile(enhancedImage, enhancedFile);
|
||||
|
||||
if (!enhancedFile.exists() || enhancedFile.length() == 0) {
|
||||
return StageResult.degraded("超分辨率结果保存失败,使用原图");
|
||||
}
|
||||
|
||||
// 更新处理后的文件
|
||||
context.updateProcessedFile(enhancedFile);
|
||||
|
||||
long originalSize = currentFile.length();
|
||||
long enhancedSize = enhancedFile.length();
|
||||
double sizeRatio = (double) enhancedSize / originalSize;
|
||||
|
||||
log.info("图像超分辨率完成: 原始{}KB -> 超分后{}KB (比例: {})",
|
||||
originalSize / 1024,
|
||||
enhancedSize / 1024,
|
||||
String.format("%.2f", sizeRatio));
|
||||
|
||||
return StageResult.success(String.format("超分辨率完成 (%dKB -> %dKB)",
|
||||
originalSize / 1024,
|
||||
enhancedSize / 1024));
|
||||
|
||||
} catch (Exception e) {
|
||||
log.error("图像超分辨率失败: {}", e.getMessage(), e);
|
||||
|
||||
// 超分辨率失败时返回降级状态,继续使用原图
|
||||
return StageResult.degraded("超分辨率失败: " + e.getMessage());
|
||||
}
|
||||
}
|
||||
|
||||
/**
|
||||
* 检查配置是否有效
|
||||
*/
|
||||
private boolean isConfigValid() {
|
||||
if (enhancerConfig == null) {
|
||||
return false;
|
||||
}
|
||||
|
||||
String appId = enhancerConfig.getAppId();
|
||||
String apiKey = enhancerConfig.getApiKey();
|
||||
String secretKey = enhancerConfig.getSecretKey();
|
||||
|
||||
// 检查字段是否为 null 或空
|
||||
if (appId == null || appId.isBlank() ||
|
||||
apiKey == null || apiKey.isBlank() ||
|
||||
secretKey == null || secretKey.isBlank()) {
|
||||
return false;
|
||||
}
|
||||
|
||||
// 检查是否包含 TODO 占位符
|
||||
if (appId.contains("TODO") || apiKey.contains("TODO") || secretKey.contains("TODO")) {
|
||||
return false;
|
||||
}
|
||||
|
||||
return true;
|
||||
}
|
||||
|
||||
/**
|
||||
* 从环境变量构建配置
|
||||
*/
|
||||
private static BceEnhancerConfig buildConfigFromEnvironment() {
|
||||
BceEnhancerConfig config = new BceEnhancerConfig();
|
||||
config.setAppId(System.getenv("BCE_IMAGE_APP_ID"));
|
||||
config.setApiKey(System.getenv("BCE_IMAGE_API_KEY"));
|
||||
config.setSecretKey(System.getenv("BCE_IMAGE_SECRET_KEY"));
|
||||
config.setQps(1.0f);
|
||||
return config;
|
||||
}
|
||||
|
||||
/**
|
||||
* 保存MultipartFile到本地文件
|
||||
*/
|
||||
private void saveMultipartFileToFile(MultipartFile multipartFile, File targetFile) throws IOException {
|
||||
try (FileOutputStream fos = new FileOutputStream(targetFile)) {
|
||||
fos.write(multipartFile.getBytes());
|
||||
fos.flush();
|
||||
}
|
||||
}
|
||||
|
||||
/**
|
||||
* 获取当前配置(用于调试)
|
||||
*/
|
||||
public BceEnhancerConfig getEnhancerConfig() {
|
||||
return enhancerConfig;
|
||||
}
|
||||
|
||||
/**
|
||||
* 设置配置(用于动态配置)
|
||||
*/
|
||||
public void setEnhancerConfig(BceEnhancerConfig enhancerConfig) {
|
||||
this.enhancerConfig = enhancerConfig;
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,17 @@
|
||||
package com.ycwl.basic.image.pipeline.stages;
|
||||
|
||||
import com.ycwl.basic.image.pipeline.core.PhotoProcessContext;
|
||||
import com.ycwl.basic.pipeline.core.AbstractPipelineStage;
|
||||
import com.ycwl.basic.pipeline.core.StageResult;
|
||||
|
||||
public class NoOpStage extends AbstractPipelineStage<PhotoProcessContext> {
|
||||
@Override
|
||||
protected StageResult<PhotoProcessContext> doExecute(PhotoProcessContext context) {
|
||||
return StageResult.skipped("无操作");
|
||||
}
|
||||
|
||||
@Override
|
||||
public String getName() {
|
||||
return "NoOpStage";
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,69 @@
|
||||
package com.ycwl.basic.image.pipeline.stages;
|
||||
|
||||
import com.ycwl.basic.image.pipeline.core.PhotoProcessContext;
|
||||
import com.ycwl.basic.image.pipeline.enums.ImageType;
|
||||
import com.ycwl.basic.pipeline.annotation.StageConfig;
|
||||
import com.ycwl.basic.pipeline.core.AbstractPipelineStage;
|
||||
import com.ycwl.basic.pipeline.core.StageResult;
|
||||
import com.ycwl.basic.pipeline.enums.StageOptionalMode;
|
||||
import com.ycwl.basic.utils.ImageUtils;
|
||||
import lombok.extern.slf4j.Slf4j;
|
||||
|
||||
import java.io.File;
|
||||
|
||||
/**
|
||||
* 拼图边框处理Stage
|
||||
* 为拼图添加白边框并向上偏移
|
||||
*/
|
||||
@Slf4j
|
||||
@StageConfig(
|
||||
stageId = "puzzle_border",
|
||||
optionalMode = StageOptionalMode.SUPPORT,
|
||||
description = "拼图边框处理",
|
||||
defaultEnabled = true
|
||||
)
|
||||
public class PuzzleBorderStage extends AbstractPipelineStage<PhotoProcessContext> {
|
||||
|
||||
private static final int BORDER_LR = 20;
|
||||
private static final int BORDER_TB = 30;
|
||||
private static final int SHIFT_UP = 15;
|
||||
|
||||
@Override
|
||||
public String getName() {
|
||||
return "PuzzleBorderStage";
|
||||
}
|
||||
|
||||
@Override
|
||||
protected boolean shouldExecuteByBusinessLogic(PhotoProcessContext context) {
|
||||
return context.getImageType() == ImageType.PUZZLE;
|
||||
}
|
||||
|
||||
@Override
|
||||
protected StageResult<PhotoProcessContext> doExecute(PhotoProcessContext context) {
|
||||
try {
|
||||
File currentFile = context.getCurrentFile();
|
||||
if (currentFile == null || !currentFile.exists()) {
|
||||
return StageResult.failed("当前文件不存在");
|
||||
}
|
||||
|
||||
File processedFile = context.getTempFileManager()
|
||||
.createTempFile("puzzle_processed", ".png");
|
||||
|
||||
log.debug("拼图添加边框: lr={}, tb={}, shiftUp={}", BORDER_LR, BORDER_TB, SHIFT_UP);
|
||||
ImageUtils.addBorderAndShiftUp(currentFile, processedFile, BORDER_LR, BORDER_TB, SHIFT_UP);
|
||||
|
||||
if (!processedFile.exists()) {
|
||||
return StageResult.failed("处理后的文件未生成");
|
||||
}
|
||||
|
||||
context.updateProcessedFile(processedFile);
|
||||
log.info("拼图边框处理完成: {}KB", processedFile.length() / 1024);
|
||||
|
||||
return StageResult.success(String.format("边框处理完成 (%dKB)", processedFile.length() / 1024));
|
||||
|
||||
} catch (Exception e) {
|
||||
log.error("拼图边框处理失败", e);
|
||||
return StageResult.failed("边框处理失败: " + e.getMessage(), e);
|
||||
}
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,115 @@
|
||||
package com.ycwl.basic.image.pipeline.stages;
|
||||
|
||||
import com.ycwl.basic.image.pipeline.core.PhotoProcessContext;
|
||||
import com.ycwl.basic.image.pipeline.enums.ImageType;
|
||||
import com.ycwl.basic.pipeline.annotation.StageConfig;
|
||||
import com.ycwl.basic.pipeline.core.AbstractPipelineStage;
|
||||
import com.ycwl.basic.pipeline.core.StageResult;
|
||||
import com.ycwl.basic.pipeline.enums.StageOptionalMode;
|
||||
import com.ycwl.basic.utils.ImageUtils;
|
||||
import lombok.extern.slf4j.Slf4j;
|
||||
|
||||
import java.io.File;
|
||||
|
||||
/**
|
||||
* 恢复图片方向Stage
|
||||
* 如果之前旋转过图片,现在反向旋转恢复原方向
|
||||
* - rotation=90 时恢复需要旋转270度
|
||||
* - rotation=270 时恢复需要旋转90度
|
||||
*/
|
||||
@Slf4j
|
||||
@StageConfig(
|
||||
stageId = "restore",
|
||||
optionalMode = StageOptionalMode.UNSUPPORT,
|
||||
description = "恢复竖图方向"
|
||||
)
|
||||
public class RestoreOrientationStage extends AbstractPipelineStage<PhotoProcessContext> {
|
||||
|
||||
@Override
|
||||
public String getName() {
|
||||
return "RestoreOrientationStage";
|
||||
}
|
||||
|
||||
@Override
|
||||
protected boolean shouldExecuteByBusinessLogic(PhotoProcessContext context) {
|
||||
return context.getImageType() == ImageType.NORMAL_PHOTO && context.isRotationApplied();
|
||||
}
|
||||
|
||||
@Override
|
||||
protected StageResult<PhotoProcessContext> doExecute(PhotoProcessContext context) {
|
||||
try {
|
||||
File currentFile = context.getCurrentFile();
|
||||
if (currentFile == null || !currentFile.exists()) {
|
||||
return StageResult.failed("当前文件不存在");
|
||||
}
|
||||
|
||||
Integer rotation = context.getImageRotation();
|
||||
if (rotation == null) {
|
||||
return StageResult.skipped("无旋转信息");
|
||||
}
|
||||
|
||||
// 计算恢复旋转需要的角度(反向旋转)
|
||||
int restoreAngle = getRestoreAngle(rotation);
|
||||
|
||||
String extension = getFileExtension(currentFile);
|
||||
File finalFile = context.getTempFileManager()
|
||||
.createTempFile("final", extension);
|
||||
|
||||
log.debug("恢复图片方向(旋转{}度): {} -> {}", restoreAngle, currentFile.getName(), finalFile.getName());
|
||||
rotateByAngle(currentFile, finalFile, restoreAngle);
|
||||
|
||||
if (!finalFile.exists()) {
|
||||
return StageResult.failed("旋转后的文件未生成");
|
||||
}
|
||||
|
||||
context.updateProcessedFile(finalFile);
|
||||
log.info("图片方向已恢复(旋转{}度)", restoreAngle);
|
||||
|
||||
return StageResult.success("已恢复原方向");
|
||||
|
||||
} catch (Exception e) {
|
||||
log.error("图片旋转失败", e);
|
||||
return StageResult.failed("旋转失败: " + e.getMessage(), e);
|
||||
}
|
||||
}
|
||||
|
||||
/**
|
||||
* 计算恢复方向需要的旋转角度
|
||||
* @param originalRotation 原始旋转角度
|
||||
* @return 恢复需要的旋转角度
|
||||
*/
|
||||
private int getRestoreAngle(int originalRotation) {
|
||||
// rotation=90 时,恢复需要旋转270度 (360-90=270)
|
||||
// rotation=270 时,恢复需要旋转90度 (360-270=90)
|
||||
// rotation=180 时,恢复需要旋转180度 (360-180=180)
|
||||
return (360 - originalRotation) % 360;
|
||||
}
|
||||
|
||||
/**
|
||||
* 根据角度旋转图片
|
||||
*/
|
||||
private void rotateByAngle(File input, File output, int angle) throws Exception {
|
||||
switch (angle) {
|
||||
case 90:
|
||||
ImageUtils.rotateImage90(input, output);
|
||||
break;
|
||||
case 180:
|
||||
ImageUtils.rotateImage180(input, output);
|
||||
break;
|
||||
case 270:
|
||||
ImageUtils.rotateImage270(input, output);
|
||||
break;
|
||||
default:
|
||||
break;
|
||||
}
|
||||
}
|
||||
|
||||
private String getFileExtension(File file) {
|
||||
String name = file.getName();
|
||||
int lastDot = name.lastIndexOf('.');
|
||||
if (lastDot > 0) {
|
||||
return name.substring(lastDot);
|
||||
}
|
||||
return ".jpg";
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,82 @@
|
||||
package com.ycwl.basic.image.pipeline.stages;
|
||||
|
||||
import com.ycwl.basic.image.pipeline.core.PhotoProcessContext;
|
||||
import com.ycwl.basic.pipeline.annotation.StageConfig;
|
||||
import com.ycwl.basic.pipeline.core.AbstractPipelineStage;
|
||||
import com.ycwl.basic.pipeline.core.StageResult;
|
||||
import com.ycwl.basic.pipeline.enums.StageOptionalMode;
|
||||
import com.ycwl.basic.service.pc.SourceService;
|
||||
import lombok.extern.slf4j.Slf4j;
|
||||
|
||||
import java.io.File;
|
||||
|
||||
/**
|
||||
* 源图片上传和更新Stage
|
||||
* 专门用于将增强后的图片上传并更新数据库中的URL
|
||||
*
|
||||
* 与UploadStage的区别:
|
||||
* - UploadStage: 通用上传,仅上传到存储并设置Context.resultUrl
|
||||
* - SourcePhotoUpdateStage: 专门用于源图片,上传+更新数据库source表的URL字段
|
||||
*/
|
||||
@Slf4j
|
||||
@StageConfig(
|
||||
stageId = "source_photo_update",
|
||||
optionalMode = StageOptionalMode.UNSUPPORT,
|
||||
description = "源图片上传和数据库更新",
|
||||
defaultEnabled = true
|
||||
)
|
||||
public class SourcePhotoUpdateStage extends AbstractPipelineStage<PhotoProcessContext> {
|
||||
|
||||
private final SourceService sourceService;
|
||||
private final Long sourceId;
|
||||
|
||||
/**
|
||||
* 构造函数
|
||||
*
|
||||
* @param sourceService 源图片服务
|
||||
* @param sourceId 源图片ID
|
||||
*/
|
||||
public SourcePhotoUpdateStage(SourceService sourceService, Long sourceId) {
|
||||
this.sourceService = sourceService;
|
||||
this.sourceId = sourceId;
|
||||
}
|
||||
|
||||
@Override
|
||||
public String getName() {
|
||||
return "SourcePhotoUpdateStage";
|
||||
}
|
||||
|
||||
@Override
|
||||
protected StageResult<PhotoProcessContext> doExecute(PhotoProcessContext context) {
|
||||
File fileToUpload = context.getCurrentFile();
|
||||
if (fileToUpload == null || !fileToUpload.exists()) {
|
||||
return StageResult.failed("没有可上传的文件");
|
||||
}
|
||||
|
||||
if (sourceService == null) {
|
||||
return StageResult.failed("SourceService未注入");
|
||||
}
|
||||
|
||||
if (sourceId == null) {
|
||||
return StageResult.failed("SourceId为空");
|
||||
}
|
||||
|
||||
try {
|
||||
|
||||
// 调用SourceService上传并更新URL
|
||||
String uploadedUrl = sourceService.uploadAndUpdateUrl(sourceId, fileToUpload);
|
||||
|
||||
// 设置结果URL到Context
|
||||
context.setResultUrl(uploadedUrl);
|
||||
|
||||
log.info("源图片上传并更新成功: sourceId={}, url={}", sourceId, uploadedUrl);
|
||||
|
||||
return StageResult.success("已上传并更新: " + uploadedUrl);
|
||||
|
||||
} catch (Exception e) {
|
||||
log.error("源图片上传并更新失败: sourceId={}", sourceId, e);
|
||||
return StageResult.failed("上传并更新失败: " + e.getMessage(), e);
|
||||
}
|
||||
}
|
||||
|
||||
}
|
||||
@@ -0,0 +1,82 @@
|
||||
package com.ycwl.basic.image.pipeline.stages;
|
||||
|
||||
import com.ycwl.basic.image.pipeline.core.PhotoProcessContext;
|
||||
import com.ycwl.basic.mapper.PrinterMapper;
|
||||
import com.ycwl.basic.pipeline.annotation.StageConfig;
|
||||
import com.ycwl.basic.pipeline.core.AbstractPipelineStage;
|
||||
import com.ycwl.basic.pipeline.core.StageResult;
|
||||
import com.ycwl.basic.pipeline.enums.StageOptionalMode;
|
||||
import lombok.extern.slf4j.Slf4j;
|
||||
|
||||
/**
|
||||
* 更新MemberPrint记录Stage
|
||||
* 用于更新member_print表中的cropUrl字段
|
||||
*/
|
||||
@Slf4j
|
||||
@StageConfig(
|
||||
stageId = "update_member_print",
|
||||
optionalMode = StageOptionalMode.UNSUPPORT,
|
||||
description = "更新MemberPrint记录",
|
||||
defaultEnabled = true
|
||||
)
|
||||
public class UpdateMemberPrintStage extends AbstractPipelineStage<PhotoProcessContext> {
|
||||
|
||||
private final PrinterMapper printerMapper;
|
||||
private final Integer memberPrintId;
|
||||
private final Long memberId;
|
||||
private final Long scenicId;
|
||||
|
||||
/**
|
||||
* 构造函数
|
||||
* @param printerMapper PrinterMapper实例
|
||||
* @param memberPrintId MemberPrint记录ID
|
||||
* @param memberId 用户ID
|
||||
* @param scenicId 景区ID
|
||||
*/
|
||||
public UpdateMemberPrintStage(PrinterMapper printerMapper, Integer memberPrintId, Long memberId, Long scenicId) {
|
||||
this.printerMapper = printerMapper;
|
||||
this.memberPrintId = memberPrintId;
|
||||
this.memberId = memberId;
|
||||
this.scenicId = scenicId;
|
||||
}
|
||||
|
||||
@Override
|
||||
public String getName() {
|
||||
return "UpdateMemberPrintStage";
|
||||
}
|
||||
|
||||
@Override
|
||||
protected StageResult<PhotoProcessContext> doExecute(PhotoProcessContext context) {
|
||||
String resultUrl = context.getResultUrl();
|
||||
|
||||
if (resultUrl == null || resultUrl.trim().isEmpty()) {
|
||||
return StageResult.skipped("结果URL为空,跳过更新");
|
||||
}
|
||||
|
||||
if (memberPrintId == null || memberId == null || scenicId == null) {
|
||||
log.warn("MemberPrint更新参数不完整: memberPrintId={}, memberId={}, scenicId={}",
|
||||
memberPrintId, memberId, scenicId);
|
||||
return StageResult.skipped("更新参数不完整");
|
||||
}
|
||||
|
||||
try {
|
||||
log.debug("开始更新MemberPrint记录: id={}, newCropUrl={}", memberPrintId, resultUrl);
|
||||
|
||||
// 更新cropUrl字段
|
||||
int rows = printerMapper.setPhotoCropped(memberId, scenicId, memberPrintId.longValue(), resultUrl, null);
|
||||
|
||||
if (rows > 0) {
|
||||
log.info("MemberPrint记录更新成功: id={}, cropUrl已更新", memberPrintId);
|
||||
return StageResult.success("更新成功");
|
||||
} else {
|
||||
log.warn("MemberPrint记录更新失败: 可能记录不存在, id={}", memberPrintId);
|
||||
return StageResult.degraded("更新失败,记录可能不存在");
|
||||
}
|
||||
|
||||
} catch (Exception e) {
|
||||
log.error("更新MemberPrint记录异常: id={}", memberPrintId, e);
|
||||
// 更新失败不影响整个流程,使用降级状态
|
||||
return StageResult.degraded("更新异常: " + e.getMessage());
|
||||
}
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,103 @@
|
||||
package com.ycwl.basic.image.pipeline.stages;
|
||||
|
||||
import com.ycwl.basic.image.pipeline.core.PhotoProcessContext;
|
||||
import com.ycwl.basic.pipeline.annotation.StageConfig;
|
||||
import com.ycwl.basic.pipeline.core.AbstractPipelineStage;
|
||||
import com.ycwl.basic.pipeline.core.StageResult;
|
||||
import com.ycwl.basic.pipeline.enums.StageOptionalMode;
|
||||
import com.ycwl.basic.storage.StorageFactory;
|
||||
import com.ycwl.basic.storage.adapters.IStorageAdapter;
|
||||
import com.ycwl.basic.storage.enums.StorageAcl;
|
||||
import lombok.extern.slf4j.Slf4j;
|
||||
|
||||
import java.io.File;
|
||||
|
||||
/**
|
||||
* 上传图片Stage
|
||||
* 支持降级: 配置的存储 -> 默认assets-ext存储
|
||||
*/
|
||||
@Slf4j
|
||||
@StageConfig(
|
||||
stageId = "upload",
|
||||
optionalMode = StageOptionalMode.FORCE_ON,
|
||||
description = "上传图片到存储",
|
||||
defaultEnabled = true
|
||||
)
|
||||
public class UploadStage extends AbstractPipelineStage<PhotoProcessContext> {
|
||||
|
||||
private static final String DEFAULT_STORAGE = "assets-ext";
|
||||
|
||||
@Override
|
||||
public String getName() {
|
||||
return "UploadStage";
|
||||
}
|
||||
|
||||
@Override
|
||||
protected StageResult<PhotoProcessContext> doExecute(PhotoProcessContext context) {
|
||||
File fileToUpload = context.getCurrentFile();
|
||||
if (fileToUpload == null || !fileToUpload.exists()) {
|
||||
return StageResult.failed("没有可上传的文件");
|
||||
}
|
||||
|
||||
IStorageAdapter adapter = context.getStorageAdapter();
|
||||
boolean usingDefaultStorage = false;
|
||||
|
||||
if (adapter == null) {
|
||||
log.debug("未配置存储适配器,使用默认存储: {}", DEFAULT_STORAGE);
|
||||
try {
|
||||
adapter = StorageFactory.use(DEFAULT_STORAGE);
|
||||
usingDefaultStorage = true;
|
||||
} catch (Exception e) {
|
||||
return StageResult.failed("无法获取默认存储: " + e.getMessage(), e);
|
||||
}
|
||||
}
|
||||
|
||||
try {
|
||||
String uploadedUrl = uploadFile(adapter, fileToUpload);
|
||||
context.setResultUrl(uploadedUrl);
|
||||
|
||||
log.info("文件上传成功: {}", uploadedUrl);
|
||||
|
||||
if (usingDefaultStorage) {
|
||||
return StageResult.degraded("降级: 使用默认存储 " + DEFAULT_STORAGE);
|
||||
}
|
||||
|
||||
return StageResult.success("已上传");
|
||||
|
||||
} catch (Exception e) {
|
||||
log.error("文件上传失败", e);
|
||||
|
||||
if (!usingDefaultStorage) {
|
||||
log.warn("尝试降级到默认存储");
|
||||
try {
|
||||
IStorageAdapter defaultAdapter = StorageFactory.use(DEFAULT_STORAGE);
|
||||
String uploadedUrl = uploadFile(defaultAdapter, fileToUpload);
|
||||
context.setResultUrl(uploadedUrl);
|
||||
|
||||
log.info("降级上传成功: {}", uploadedUrl);
|
||||
return StageResult.degraded("降级: 使用默认存储 " + DEFAULT_STORAGE);
|
||||
|
||||
} catch (Exception fallbackEx) {
|
||||
log.error("降级上传也失败", fallbackEx);
|
||||
return StageResult.failed("上传失败(包括降级): " + fallbackEx.getMessage(), fallbackEx);
|
||||
}
|
||||
}
|
||||
|
||||
return StageResult.failed("上传失败: " + e.getMessage(), e);
|
||||
}
|
||||
}
|
||||
|
||||
private String uploadFile(IStorageAdapter adapter, File file) throws Exception {
|
||||
String filename = file.getName();
|
||||
String extension = filename.substring(filename.lastIndexOf('.') + 1);
|
||||
if (extension.equals("jpg")) {
|
||||
extension = "jpeg";
|
||||
}
|
||||
String uploadPath = "print/" + filename;
|
||||
|
||||
String url = adapter.uploadFile("image/" + extension, file, uploadPath);
|
||||
adapter.setAcl(StorageAcl.PUBLIC_READ, uploadPath);
|
||||
|
||||
return url;
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,44 @@
|
||||
package com.ycwl.basic.image.pipeline.stages;
|
||||
|
||||
import com.ycwl.basic.image.watermark.enums.ImageWatermarkOperatorEnum;
|
||||
import lombok.Builder;
|
||||
import lombok.Getter;
|
||||
|
||||
import java.io.File;
|
||||
|
||||
/**
|
||||
* 水印Stage配置
|
||||
* 封装水印处理所需的所有配置参数
|
||||
*/
|
||||
@Getter
|
||||
@Builder
|
||||
public class WatermarkConfig {
|
||||
|
||||
/**
|
||||
* 水印类型
|
||||
*/
|
||||
private final ImageWatermarkOperatorEnum watermarkType;
|
||||
|
||||
/**
|
||||
* 景区文字
|
||||
*/
|
||||
private final String scenicText;
|
||||
|
||||
/**
|
||||
* 日期格式
|
||||
*/
|
||||
@Builder.Default
|
||||
private final String dateFormat = "yyyy.MM.dd";
|
||||
|
||||
/**
|
||||
* 二维码文件
|
||||
*/
|
||||
private final File qrcodeFile;
|
||||
|
||||
/**
|
||||
* 缩放倍数,用于将所有定位和大小乘以该倍数
|
||||
* 默认值为 1.0(不缩放)
|
||||
*/
|
||||
@Builder.Default
|
||||
private final Double scale = 1.0;
|
||||
}
|
||||
@@ -0,0 +1,190 @@
|
||||
package com.ycwl.basic.image.pipeline.stages;
|
||||
|
||||
import com.ycwl.basic.image.pipeline.core.PhotoProcessContext;
|
||||
import com.ycwl.basic.image.pipeline.enums.ImageType;
|
||||
import com.ycwl.basic.image.watermark.ImageWatermarkFactory;
|
||||
import com.ycwl.basic.image.watermark.entity.WatermarkInfo;
|
||||
import com.ycwl.basic.image.watermark.enums.ImageWatermarkOperatorEnum;
|
||||
import com.ycwl.basic.image.watermark.operator.IOperator;
|
||||
import com.ycwl.basic.pipeline.annotation.StageConfig;
|
||||
import com.ycwl.basic.pipeline.core.AbstractPipelineStage;
|
||||
import com.ycwl.basic.pipeline.core.StageResult;
|
||||
import com.ycwl.basic.pipeline.enums.StageOptionalMode;
|
||||
import lombok.extern.slf4j.Slf4j;
|
||||
import org.apache.commons.lang3.StringUtils;
|
||||
|
||||
import java.io.File;
|
||||
import java.util.Arrays;
|
||||
import java.util.Date;
|
||||
import java.util.List;
|
||||
|
||||
/**
|
||||
* 水印处理Stage
|
||||
* 支持三级降级: 配置的水印类型 -> PRINTER_DEFAULT -> 无水印
|
||||
*/
|
||||
@Slf4j
|
||||
@StageConfig(
|
||||
stageId = "watermark",
|
||||
optionalMode = StageOptionalMode.SUPPORT,
|
||||
description = "水印处理",
|
||||
defaultEnabled = true
|
||||
)
|
||||
public class WatermarkStage extends AbstractPipelineStage<PhotoProcessContext> {
|
||||
|
||||
private static final int OFFSET_FOR_PRINTER = 40;
|
||||
|
||||
private final WatermarkConfig config;
|
||||
|
||||
/**
|
||||
* 构造函数
|
||||
*
|
||||
* @param config 水印配置
|
||||
*/
|
||||
public WatermarkStage(WatermarkConfig config) {
|
||||
this.config = config;
|
||||
}
|
||||
|
||||
@Override
|
||||
public String getName() {
|
||||
return "WatermarkStage";
|
||||
}
|
||||
|
||||
@Override
|
||||
protected boolean shouldExecuteByBusinessLogic(PhotoProcessContext context) {
|
||||
return context.getImageType() == ImageType.NORMAL_PHOTO;
|
||||
}
|
||||
|
||||
@Override
|
||||
protected StageResult<PhotoProcessContext> doExecute(PhotoProcessContext context) {
|
||||
if (config == null || config.getWatermarkType() == null) {
|
||||
log.info("未配置水印类型,跳过水印处理");
|
||||
return StageResult.skipped("未配置水印");
|
||||
}
|
||||
|
||||
ImageWatermarkOperatorEnum watermarkType = config.getWatermarkType();
|
||||
|
||||
List<ImageWatermarkOperatorEnum> fallbackChain = buildFallbackChain(watermarkType);
|
||||
log.debug("水印降级链: {}", fallbackChain);
|
||||
|
||||
for (int i = 0; i < fallbackChain.size(); i++) {
|
||||
ImageWatermarkOperatorEnum type = fallbackChain.get(i);
|
||||
|
||||
if (type == null) {
|
||||
log.warn("所有水印处理均失败,跳过水印");
|
||||
return StageResult.degraded("降级: 跳过水印处理");
|
||||
}
|
||||
|
||||
try {
|
||||
StageResult<PhotoProcessContext> result = applyWatermark(context, type);
|
||||
|
||||
if (i > 0) {
|
||||
String degradeMsg = String.format("降级: %s -> %s",
|
||||
watermarkType.getType(), type.getType());
|
||||
log.warn(degradeMsg);
|
||||
return StageResult.degraded(degradeMsg);
|
||||
}
|
||||
|
||||
return result;
|
||||
|
||||
} catch (Exception e) {
|
||||
log.warn("水印类型 {} 处理失败: {}", type.getType(), e.getMessage());
|
||||
|
||||
if (i == fallbackChain.size() - 2) {
|
||||
log.warn("所有水印类型均失败,准备跳过水印", e);
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
return StageResult.degraded("降级: 跳过水印处理");
|
||||
}
|
||||
|
||||
/**
|
||||
* 构建降级链
|
||||
*/
|
||||
private List<ImageWatermarkOperatorEnum> buildFallbackChain(ImageWatermarkOperatorEnum primary) {
|
||||
if (primary == ImageWatermarkOperatorEnum.PRINTER_DEFAULT) {
|
||||
return Arrays.asList(primary, null);
|
||||
}
|
||||
return Arrays.asList(
|
||||
primary,
|
||||
ImageWatermarkOperatorEnum.PRINTER_DEFAULT,
|
||||
null
|
||||
);
|
||||
}
|
||||
|
||||
/**
|
||||
* 应用水印
|
||||
*/
|
||||
private StageResult<PhotoProcessContext> applyWatermark(PhotoProcessContext context, ImageWatermarkOperatorEnum type)
|
||||
throws Exception {
|
||||
|
||||
File currentFile = context.getCurrentFile();
|
||||
if (currentFile == null || !currentFile.exists()) {
|
||||
throw new IllegalStateException("当前文件不存在");
|
||||
}
|
||||
|
||||
String fileExt = type.getPreferFileType();
|
||||
File watermarkedFile = context.getTempFileManager()
|
||||
.createTempFile("watermark_" + type.getType(), "." + fileExt);
|
||||
|
||||
WatermarkInfo watermarkInfo = buildWatermarkInfo(context, currentFile, watermarkedFile, type);
|
||||
|
||||
IOperator operator = ImageWatermarkFactory.get(type);
|
||||
File result = operator.process(watermarkInfo);
|
||||
|
||||
if (result == null || !result.exists()) {
|
||||
throw new RuntimeException("水印处理后文件不存在");
|
||||
}
|
||||
|
||||
context.updateProcessedFile(result);
|
||||
log.info("水印应用成功: type={}, size={}KB", type.getType(), result.length() / 1024);
|
||||
|
||||
return StageResult.success(String.format("水印: %s (%dKB)",
|
||||
type.getType(), result.length() / 1024));
|
||||
}
|
||||
|
||||
/**
|
||||
* 构建水印参数
|
||||
*/
|
||||
private WatermarkInfo buildWatermarkInfo(PhotoProcessContext context, File originalFile,
|
||||
File watermarkedFile, ImageWatermarkOperatorEnum type) {
|
||||
WatermarkInfo info = new WatermarkInfo();
|
||||
info.setOriginalFile(originalFile);
|
||||
info.setWatermarkedFile(watermarkedFile);
|
||||
|
||||
// 从 config 读取景区文字
|
||||
String scenicText = config.getScenicText();
|
||||
if (StringUtils.isNotBlank(scenicText)) {
|
||||
info.setScenicLine(scenicText);
|
||||
}
|
||||
|
||||
// 从 config 读取日期格式
|
||||
Date now = new Date();
|
||||
String dateFormat = config.getDateFormat();
|
||||
info.setDatetime(now);
|
||||
info.setDtFormat(dateFormat);
|
||||
|
||||
// 从 config 读取二维码文件
|
||||
File qrcodeFile = config.getQrcodeFile();
|
||||
if (qrcodeFile != null && qrcodeFile.exists()) {
|
||||
info.setQrcodeFile(qrcodeFile);
|
||||
}
|
||||
|
||||
// 从 config 读取缩放倍数
|
||||
Double scale = config.getScale();
|
||||
if (scale != null) {
|
||||
info.setScale(scale);
|
||||
}
|
||||
|
||||
// 根据旋转状态自己处理 offsetLeft
|
||||
if (context.isRotationApplied()) {
|
||||
if (context.getImageRotation() == 90) {
|
||||
info.setOffsetLeft(OFFSET_FOR_PRINTER);
|
||||
} else if (context.getImageRotation() == 270) {
|
||||
info.setOffsetRight(OFFSET_FOR_PRINTER);
|
||||
}
|
||||
}
|
||||
|
||||
return info;
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,127 @@
|
||||
package com.ycwl.basic.image.pipeline.util;
|
||||
|
||||
import lombok.extern.slf4j.Slf4j;
|
||||
|
||||
import java.io.File;
|
||||
import java.io.IOException;
|
||||
import java.nio.file.Files;
|
||||
import java.nio.file.Path;
|
||||
import java.util.ArrayList;
|
||||
import java.util.List;
|
||||
import java.util.UUID;
|
||||
|
||||
/**
|
||||
* 临时文件管理器
|
||||
* 统一管理所有临时文件的创建和清理
|
||||
*/
|
||||
@Slf4j
|
||||
public class TempFileManager {
|
||||
|
||||
private final String processId;
|
||||
private final Path tempDir;
|
||||
private final List<File> tempFiles;
|
||||
|
||||
public TempFileManager() {
|
||||
this.processId = UUID.randomUUID().toString();
|
||||
this.tempDir = initTempDirectory();
|
||||
this.tempFiles = new ArrayList<>();
|
||||
}
|
||||
|
||||
public TempFileManager(String processId) {
|
||||
this.processId = processId;
|
||||
this.tempDir = initTempDirectory();
|
||||
this.tempFiles = new ArrayList<>();
|
||||
}
|
||||
|
||||
private Path initTempDirectory() {
|
||||
Path baseDir = Path.of(System.getProperty("java.io.tmpdir"), "photo_process", processId);
|
||||
try {
|
||||
Files.createDirectories(baseDir);
|
||||
log.debug("创建隔离临时目录: {}", baseDir);
|
||||
return baseDir;
|
||||
} catch (IOException e) {
|
||||
log.warn("无法创建隔离临时目录,尝试使用系统临时目录", e);
|
||||
try {
|
||||
Path fallback = Files.createTempDirectory("photo_process_" + processId + "_");
|
||||
log.debug("创建备用临时目录: {}", fallback);
|
||||
return fallback;
|
||||
} catch (IOException ex) {
|
||||
log.warn("无法创建系统临时目录,使用当前目录", ex);
|
||||
return Path.of(".");
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
/**
|
||||
* 创建临时文件
|
||||
*
|
||||
* @param prefix 文件名前缀
|
||||
* @param suffix 文件名后缀(如 .jpg, .png)
|
||||
* @return 临时文件
|
||||
*/
|
||||
public File createTempFile(String prefix, String suffix) {
|
||||
String filename = String.format("%s_%s%s", prefix, processId, suffix);
|
||||
File tempFile = tempDir.resolve(filename).toFile();
|
||||
tempFiles.add(tempFile);
|
||||
log.debug("创建临时文件: {}", tempFile.getAbsolutePath());
|
||||
return tempFile;
|
||||
}
|
||||
|
||||
/**
|
||||
* 注册外部创建的临时文件
|
||||
*/
|
||||
public void registerTempFile(File file) {
|
||||
if (file != null && !tempFiles.contains(file)) {
|
||||
tempFiles.add(file);
|
||||
log.debug("注册临时文件: {}", file.getAbsolutePath());
|
||||
}
|
||||
}
|
||||
|
||||
/**
|
||||
* 清理所有临时文件
|
||||
*/
|
||||
public void cleanup() {
|
||||
int deletedCount = 0;
|
||||
int failedCount = 0;
|
||||
|
||||
for (File file : tempFiles) {
|
||||
if (file != null && file.exists()) {
|
||||
boolean deleted = file.delete();
|
||||
if (deleted) {
|
||||
deletedCount++;
|
||||
log.debug("删除临时文件: {}", file.getAbsolutePath());
|
||||
} else {
|
||||
failedCount++;
|
||||
log.warn("无法删除临时文件: {}", file.getAbsolutePath());
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
tempFiles.clear();
|
||||
|
||||
if (deletedCount > 0) {
|
||||
log.info("清理临时文件: 成功{}个, 失败{}个", deletedCount, failedCount);
|
||||
}
|
||||
|
||||
cleanupTempDirectory();
|
||||
}
|
||||
|
||||
private void cleanupTempDirectory() {
|
||||
if (tempDir != null && !tempDir.equals(Path.of("."))) {
|
||||
try {
|
||||
Files.deleteIfExists(tempDir);
|
||||
log.debug("删除临时目录: {}", tempDir);
|
||||
} catch (IOException e) {
|
||||
log.warn("无法删除临时目录: {}", tempDir, e);
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
public String getProcessId() {
|
||||
return processId;
|
||||
}
|
||||
|
||||
public int getTempFileCount() {
|
||||
return tempFiles.size();
|
||||
}
|
||||
}
|
||||
@@ -33,6 +33,13 @@ public class WatermarkInfo {
|
||||
private Integer offsetLeft;
|
||||
private Integer offsetRight;
|
||||
|
||||
/**
|
||||
* 缩放倍数,用于将所有定位和大小乘以该倍数
|
||||
* 例如: scale=2.0 表示所有尺寸和位置都放大2倍
|
||||
* null 表示使用默认值1.0(不缩放)
|
||||
*/
|
||||
private Double scale;
|
||||
|
||||
public String getDatetimeLine() {
|
||||
if (datetimeLine == null) {
|
||||
datetimeLine = DateUtil.format(datetime, dtFormat);
|
||||
|
||||
@@ -64,11 +64,14 @@ public class PrinterDefaultWatermarkOperator implements IOperator {
|
||||
|
||||
@Override
|
||||
public File process(WatermarkInfo info) throws ImageWatermarkException {
|
||||
// 获取四边偏移值,优先使用传入的值,否则使用默认值
|
||||
int offsetTop = info.getOffsetTop() != null ? info.getOffsetTop() : DEFAULT_OFFSET_TOP;
|
||||
int offsetBottom = info.getOffsetBottom() != null ? info.getOffsetBottom() : DEFAULT_OFFSET_BOTTOM;
|
||||
int offsetLeft = info.getOffsetLeft() != null ? info.getOffsetLeft() : DEFAULT_OFFSET_LEFT;
|
||||
int offsetRight = info.getOffsetRight() != null ? info.getOffsetRight() : DEFAULT_OFFSET_RIGHT;
|
||||
// 获取缩放倍数,默认为1.0(不缩放)
|
||||
double scale = info.getScale() != null ? info.getScale() : 1.0;
|
||||
|
||||
// 获取四边偏移值,优先使用传入的值,否则使用默认值,并应用缩放
|
||||
int offsetTop = (int) ((info.getOffsetTop() != null ? info.getOffsetTop() : DEFAULT_OFFSET_TOP) * scale);
|
||||
int offsetBottom = (int) ((info.getOffsetBottom() != null ? info.getOffsetBottom() : DEFAULT_OFFSET_BOTTOM) * scale);
|
||||
int offsetLeft = (int) ((info.getOffsetLeft() != null ? info.getOffsetLeft() : DEFAULT_OFFSET_LEFT) * scale);
|
||||
int offsetRight = (int) ((info.getOffsetRight() != null ? info.getOffsetRight() : DEFAULT_OFFSET_RIGHT) * scale);
|
||||
|
||||
BufferedImage baseImage;
|
||||
BufferedImage qrcodeImage;
|
||||
@@ -86,17 +89,26 @@ public class PrinterDefaultWatermarkOperator implements IOperator {
|
||||
} catch (IOException e) {
|
||||
throw new ImageWatermarkException("图片打开失败");
|
||||
}
|
||||
|
||||
// 应用缩放到所有常量
|
||||
int scaledExtraBorder = (int) (EXTRA_BORDER_PX * scale);
|
||||
int scaledOffsetY = (int) (OFFSET_Y * scale);
|
||||
int scaledQrcodeSize = (int) (QRCODE_SIZE * scale);
|
||||
int scaledQrcodeOffsetY = (int) (QRCODE_OFFSET_Y * scale);
|
||||
int scaledScenicFontSize = (int) (SCENIC_FONT_SIZE * scale);
|
||||
int scaledDatetimeFontSize = (int) (DATETIME_FONT_SIZE * scale);
|
||||
|
||||
// 新图像画布
|
||||
BufferedImage newImage = new BufferedImage(baseImage.getWidth() + 2 * EXTRA_BORDER_PX, baseImage.getHeight() + 2 * EXTRA_BORDER_PX, BufferedImage.TYPE_INT_RGB);
|
||||
BufferedImage newImage = new BufferedImage(baseImage.getWidth() + 2 * scaledExtraBorder, baseImage.getHeight() + 2 * scaledExtraBorder, BufferedImage.TYPE_INT_RGB);
|
||||
Graphics2D g2d = newImage.createGraphics();
|
||||
g2d.setColor(BG_COLOR);
|
||||
g2d.fillRect(0, 0, newImage.getWidth(), newImage.getHeight());
|
||||
g2d.setRenderingHint(RenderingHints.KEY_ANTIALIASING, RenderingHints.VALUE_ANTIALIAS_ON);
|
||||
g2d.drawImage(baseImage, EXTRA_BORDER_PX, EXTRA_BORDER_PX, null);
|
||||
int newQrcodeHeight = QRCODE_SIZE;
|
||||
g2d.drawImage(baseImage, scaledExtraBorder, scaledExtraBorder, null);
|
||||
int newQrcodeHeight = scaledQrcodeSize;
|
||||
int newQrcodeWidth = (int) (newQrcodeHeight * 1.0 / qrcodeImage.getHeight() * qrcodeImage.getWidth());
|
||||
Font scenicFont = new Font(defaultFontName, Font.BOLD, SCENIC_FONT_SIZE);
|
||||
Font datetimeFont = new Font(defaultFontName, Font.BOLD, DATETIME_FONT_SIZE);
|
||||
Font scenicFont = new Font(defaultFontName, Font.BOLD, scaledScenicFontSize);
|
||||
Font datetimeFont = new Font(defaultFontName, Font.BOLD, scaledDatetimeFontSize);
|
||||
FontMetrics scenicFontMetrics = g2d.getFontMetrics(scenicFont);
|
||||
FontMetrics datetimeFontMetrics = g2d.getFontMetrics(datetimeFont);
|
||||
int scenicLineHeight = scenicFontMetrics.getHeight();
|
||||
@@ -106,13 +118,14 @@ public class PrinterDefaultWatermarkOperator implements IOperator {
|
||||
|
||||
// 二维码放置在左下角,距离左边缘图片宽度的5%,再加上左侧偏移
|
||||
int qrcodeOffsetX = (int) (newImage.getWidth() * QRCODE_LEFT_MARGIN_RATIO) + offsetLeft;
|
||||
int qrcodeOffsetY = EXTRA_BORDER_PX + baseImage.getHeight() - OFFSET_Y - newQrcodeHeight - offsetBottom;
|
||||
int qrcodeOffsetY = scaledExtraBorder + baseImage.getHeight() - scaledOffsetY - newQrcodeHeight - offsetBottom;
|
||||
Shape originalClip = g2d.getClip();
|
||||
|
||||
// 创建比二维码大10像素的白色圆形背景
|
||||
int whiteCircleSize = Math.max(newQrcodeWidth, newQrcodeHeight) + 10;
|
||||
// 创建比二维码大10像素的白色圆形背景(10像素也要缩放)
|
||||
int whiteCirclePadding = (int) (10 * scale);
|
||||
int whiteCircleSize = Math.max(newQrcodeWidth, newQrcodeHeight) + whiteCirclePadding;
|
||||
int whiteCircleX = qrcodeOffsetX - (whiteCircleSize - newQrcodeWidth) / 2;
|
||||
int whiteCircleY = qrcodeOffsetY + QRCODE_OFFSET_Y - (whiteCircleSize - newQrcodeHeight) / 2;
|
||||
int whiteCircleY = qrcodeOffsetY + scaledQrcodeOffsetY - (whiteCircleSize - newQrcodeHeight) / 2;
|
||||
|
||||
// 绘制白色圆形背景
|
||||
g2d.setColor(Color.WHITE);
|
||||
@@ -122,7 +135,7 @@ public class PrinterDefaultWatermarkOperator implements IOperator {
|
||||
// 用白色圆形尺寸裁切二维码(保持二维码原始尺寸,但用大圆裁切)
|
||||
Ellipse2D qrcodeCircle = new Ellipse2D.Double(whiteCircleX, whiteCircleY, whiteCircleSize, whiteCircleSize);
|
||||
g2d.setClip(qrcodeCircle);
|
||||
g2d.drawImage(qrcodeImage, qrcodeOffsetX, qrcodeOffsetY + QRCODE_OFFSET_Y, newQrcodeWidth, newQrcodeHeight, null);
|
||||
g2d.drawImage(qrcodeImage, qrcodeOffsetX, qrcodeOffsetY + scaledQrcodeOffsetY, newQrcodeWidth, newQrcodeHeight, null);
|
||||
g2d.setClip(originalClip);
|
||||
|
||||
// 在圆形二维码中央绘制圆形头像
|
||||
@@ -130,7 +143,7 @@ public class PrinterDefaultWatermarkOperator implements IOperator {
|
||||
// 计算圆形头像的尺寸和位置
|
||||
int avatarDiameter = (int) (newQrcodeHeight * 0.45);
|
||||
int avatarX = qrcodeOffsetX + (newQrcodeWidth - avatarDiameter) / 2;
|
||||
int avatarY = qrcodeOffsetY + QRCODE_OFFSET_Y + (newQrcodeHeight - avatarDiameter) / 2;
|
||||
int avatarY = qrcodeOffsetY + scaledQrcodeOffsetY + (newQrcodeHeight - avatarDiameter) / 2;
|
||||
|
||||
// 保存当前的渲染设置
|
||||
RenderingHints originalHints = g2d.getRenderingHints();
|
||||
@@ -149,10 +162,10 @@ public class PrinterDefaultWatermarkOperator implements IOperator {
|
||||
double faceHeight = faceImage.getHeight();
|
||||
double scaleX = avatarDiameter / faceWidth;
|
||||
double scaleY = avatarDiameter / faceHeight;
|
||||
double scale = Math.max(scaleX, scaleY); // 使用较大的缩放比例以填满圆形
|
||||
double faceScale = Math.max(scaleX, scaleY); // 使用较大的缩放比例以填满圆形
|
||||
|
||||
int scaledWidth = (int) (faceWidth * scale);
|
||||
int scaledHeight = (int) (faceHeight * scale);
|
||||
int scaledWidth = (int) (faceWidth * faceScale);
|
||||
int scaledHeight = (int) (faceHeight * faceScale);
|
||||
|
||||
// 计算居中位置
|
||||
int faceDrawX = avatarX + (avatarDiameter - scaledWidth) / 2;
|
||||
@@ -167,7 +180,7 @@ public class PrinterDefaultWatermarkOperator implements IOperator {
|
||||
}
|
||||
|
||||
// 计算文字与二维码垂直居中对齐的Y坐标
|
||||
int qrcodeTop = qrcodeOffsetY + QRCODE_OFFSET_Y;
|
||||
int qrcodeTop = qrcodeOffsetY + scaledQrcodeOffsetY;
|
||||
int qrcodeBottom = qrcodeTop + newQrcodeHeight;
|
||||
int qrcodeCenter = (qrcodeTop + qrcodeBottom) / 2;
|
||||
|
||||
|
||||
@@ -1,54 +0,0 @@
|
||||
package com.ycwl.basic.integration.device.client;
|
||||
|
||||
import com.ycwl.basic.integration.common.response.CommonResponse;
|
||||
import com.ycwl.basic.integration.common.response.PageResponse;
|
||||
import com.ycwl.basic.integration.device.dto.defaults.*;
|
||||
import org.springframework.cloud.openfeign.FeignClient;
|
||||
import org.springframework.web.bind.annotation.*;
|
||||
|
||||
import java.util.Map;
|
||||
|
||||
/**
|
||||
* 默认配置管理 Feign 客户端
|
||||
*/
|
||||
@FeignClient(name = "zt-device", contextId = "device-default-config", path = "/api/device/config/v2/defaults")
|
||||
public interface DefaultConfigClient {
|
||||
|
||||
/**
|
||||
* 获取默认配置列表
|
||||
*/
|
||||
@GetMapping
|
||||
CommonResponse<PageResponse<DefaultConfigResponse>> listDefaultConfigs(@RequestParam(value = "page", defaultValue = "1") int page,
|
||||
@RequestParam(value = "pageSize", defaultValue = "10") int pageSize);
|
||||
|
||||
/**
|
||||
* 根据配置键获取默认配置
|
||||
*/
|
||||
@GetMapping("/{configKey}")
|
||||
CommonResponse<DefaultConfigResponse> getDefaultConfig(@PathVariable("configKey") String configKey);
|
||||
|
||||
/**
|
||||
* 创建默认配置
|
||||
*/
|
||||
@PostMapping
|
||||
CommonResponse<String> createDefaultConfig(@RequestBody DefaultConfigRequest request);
|
||||
|
||||
/**
|
||||
* 更新默认配置
|
||||
*/
|
||||
@PutMapping("/{configKey}")
|
||||
CommonResponse<Map<String, Object>> updateDefaultConfig(@PathVariable("configKey") String configKey,
|
||||
@RequestBody Map<String, Object> updates);
|
||||
|
||||
/**
|
||||
* 删除默认配置
|
||||
*/
|
||||
@DeleteMapping("/{configKey}")
|
||||
CommonResponse<String> deleteDefaultConfig(@PathVariable("configKey") String configKey);
|
||||
|
||||
/**
|
||||
* 批量更新默认配置
|
||||
*/
|
||||
@PostMapping("/batch")
|
||||
CommonResponse<BatchDefaultConfigResponse> batchUpdateDefaultConfigs(@RequestBody BatchDefaultConfigRequest request);
|
||||
}
|
||||
@@ -1,62 +0,0 @@
|
||||
package com.ycwl.basic.integration.device.client;
|
||||
|
||||
import com.ycwl.basic.integration.common.response.CommonResponse;
|
||||
import com.ycwl.basic.integration.device.dto.config.*;
|
||||
import org.springframework.cloud.openfeign.FeignClient;
|
||||
import org.springframework.web.bind.annotation.*;
|
||||
|
||||
import java.util.List;
|
||||
import java.util.Map;
|
||||
|
||||
@FeignClient(name = "zt-device", contextId = "device-config-v2", path = "/api/device/config/v2")
|
||||
public interface DeviceConfigV2Client {
|
||||
|
||||
/**
|
||||
* 获取设备所有配置
|
||||
*/
|
||||
@GetMapping("/{deviceId}")
|
||||
CommonResponse<List<DeviceConfigV2DTO>> getDeviceConfigs(@PathVariable("deviceId") Long deviceId);
|
||||
|
||||
/**
|
||||
* 根据设备编号获取设备所有配置
|
||||
*/
|
||||
@GetMapping("/no/{no}")
|
||||
CommonResponse<List<DeviceConfigV2DTO>> getDeviceConfigsByNo(@PathVariable("no") String no);
|
||||
|
||||
/**
|
||||
* 获取设备特定配置
|
||||
*/
|
||||
@GetMapping("/{deviceId}/key/{configKey}")
|
||||
CommonResponse<DeviceConfigV2DTO> getDeviceConfigByKey(@PathVariable("deviceId") Long deviceId,
|
||||
@PathVariable("configKey") String configKey);
|
||||
|
||||
/**
|
||||
* 创建设备配置
|
||||
*/
|
||||
@PostMapping("/{deviceId}")
|
||||
CommonResponse<DeviceConfigV2DTO> createDeviceConfig(@PathVariable("deviceId") Long deviceId,
|
||||
@RequestBody CreateDeviceConfigRequest request);
|
||||
|
||||
/**
|
||||
* 更新设备配置
|
||||
*/
|
||||
@PutMapping("/{deviceId}/{id}")
|
||||
CommonResponse<String> updateDeviceConfig(@PathVariable("deviceId") Long deviceId,
|
||||
@PathVariable("id") Long id,
|
||||
@RequestBody UpdateDeviceConfigRequest request);
|
||||
|
||||
/**
|
||||
* 删除设备配置
|
||||
*/
|
||||
@DeleteMapping("/{deviceId}/{id}")
|
||||
CommonResponse<String> deleteDeviceConfig(@PathVariable("deviceId") Long deviceId,
|
||||
@PathVariable("id") Long id);
|
||||
|
||||
/**
|
||||
* 批量更新设备配置
|
||||
*/
|
||||
@PostMapping("/{deviceId}/batch")
|
||||
CommonResponse<BatchUpdateResponse> batchUpdateDeviceConfig(@PathVariable("deviceId") Long deviceId,
|
||||
@RequestBody BatchDeviceConfigRequest request);
|
||||
|
||||
}
|
||||
@@ -1,51 +0,0 @@
|
||||
package com.ycwl.basic.integration.device.client;
|
||||
|
||||
import com.ycwl.basic.integration.common.response.CommonResponse;
|
||||
import com.ycwl.basic.integration.device.dto.status.DeviceStatusDTO;
|
||||
import com.ycwl.basic.integration.device.dto.status.OnlineStatusResponseDTO;
|
||||
import org.springframework.cloud.openfeign.FeignClient;
|
||||
import org.springframework.web.bind.annotation.GetMapping;
|
||||
import org.springframework.web.bind.annotation.PathVariable;
|
||||
import org.springframework.web.bind.annotation.PostMapping;
|
||||
|
||||
import java.util.List;
|
||||
|
||||
@FeignClient(name = "zt-device", contextId = "deviceStatusClient", path = "/api/device/status")
|
||||
public interface DeviceStatusClient {
|
||||
|
||||
/**
|
||||
* 获取设备状态
|
||||
*/
|
||||
@GetMapping("/{deviceNo}")
|
||||
CommonResponse<DeviceStatusDTO> getDeviceStatus(@PathVariable("deviceNo") String deviceNo);
|
||||
|
||||
/**
|
||||
* 检查设备是否在线
|
||||
*/
|
||||
@GetMapping("/{deviceNo}/online")
|
||||
CommonResponse<OnlineStatusResponseDTO> isDeviceOnline(@PathVariable("deviceNo") String deviceNo);
|
||||
|
||||
/**
|
||||
* 获取所有在线设备
|
||||
*/
|
||||
@GetMapping("/online")
|
||||
CommonResponse<List<DeviceStatusDTO>> getAllOnlineDevices();
|
||||
|
||||
/**
|
||||
* 设置设备离线
|
||||
*/
|
||||
@PostMapping("/{deviceNo}/offline")
|
||||
CommonResponse<String> setDeviceOffline(@PathVariable("deviceNo") String deviceNo);
|
||||
|
||||
/**
|
||||
* 设置设备在线
|
||||
*/
|
||||
@PostMapping("/{deviceNo}/online")
|
||||
CommonResponse<String> setDeviceOnline(@PathVariable("deviceNo") String deviceNo);
|
||||
|
||||
/**
|
||||
* 清理过期设备状态
|
||||
*/
|
||||
@PostMapping("/clean")
|
||||
CommonResponse<String> cleanExpiredDevices();
|
||||
}
|
||||
@@ -2,48 +2,57 @@ package com.ycwl.basic.integration.device.client;
|
||||
|
||||
import com.ycwl.basic.integration.common.response.CommonResponse;
|
||||
import com.ycwl.basic.integration.common.response.PageResponse;
|
||||
import com.ycwl.basic.integration.device.dto.config.*;
|
||||
import com.ycwl.basic.integration.device.dto.defaults.*;
|
||||
import com.ycwl.basic.integration.device.dto.device.*;
|
||||
import com.ycwl.basic.integration.device.dto.status.DeviceStatusDTO;
|
||||
import com.ycwl.basic.integration.device.dto.status.OnlineStatusResponseDTO;
|
||||
import org.springframework.cloud.openfeign.FeignClient;
|
||||
import org.springframework.web.bind.annotation.*;
|
||||
|
||||
@FeignClient(name = "zt-device", contextId = "device-v2", path = "/api/device/v2")
|
||||
import java.util.List;
|
||||
import java.util.Map;
|
||||
|
||||
@FeignClient(name = "zt-device", contextId = "device-v2", path = "/api/device")
|
||||
public interface DeviceV2Client {
|
||||
|
||||
// ==================== Device V2 Operations ====================
|
||||
|
||||
/**
|
||||
* 获取设备核心信息
|
||||
*/
|
||||
@GetMapping("/{id}")
|
||||
@GetMapping("v2/{id}")
|
||||
CommonResponse<DeviceV2DTO> getDevice(@PathVariable("id") Long id);
|
||||
|
||||
/**
|
||||
* 根据设备编号获取设备核心信息
|
||||
*/
|
||||
@GetMapping("/no/{no}")
|
||||
@GetMapping("v2/no/{no}")
|
||||
CommonResponse<DeviceV2DTO> getDeviceByNo(@PathVariable("no") String no);
|
||||
|
||||
/**
|
||||
* 创建设备
|
||||
*/
|
||||
@PostMapping("/")
|
||||
@PostMapping("v2/")
|
||||
CommonResponse<DeviceV2DTO> createDevice(@RequestBody CreateDeviceRequest request);
|
||||
|
||||
/**
|
||||
* 更新设备
|
||||
*/
|
||||
@PutMapping("/{id}")
|
||||
@PutMapping("v2/{id}")
|
||||
CommonResponse<String> updateDevice(@PathVariable("id") Long id,
|
||||
@RequestBody UpdateDeviceRequest request);
|
||||
|
||||
/**
|
||||
* 删除设备
|
||||
*/
|
||||
@DeleteMapping("/{id}")
|
||||
@DeleteMapping("v2/{id}")
|
||||
CommonResponse<String> deleteDevice(@PathVariable("id") Long id);
|
||||
|
||||
/**
|
||||
* 分页获取设备列表(核心信息)
|
||||
*/
|
||||
@GetMapping("/")
|
||||
@GetMapping("v2/")
|
||||
CommonResponse<PageResponse<DeviceV2DTO>> listDevices(
|
||||
@RequestParam(value = "page", defaultValue = "1") Integer page,
|
||||
@RequestParam(value = "pageSize", defaultValue = "10") Integer pageSize,
|
||||
@@ -51,12 +60,141 @@ public interface DeviceV2Client {
|
||||
@RequestParam(value = "no", required = false) String no,
|
||||
@RequestParam(value = "type", required = false) String type,
|
||||
@RequestParam(value = "isActive", required = false) Integer isActive,
|
||||
@RequestParam(value = "scenicId", required = false) Long scenicId);
|
||||
@RequestParam(value = "scenicId", required = false) Long scenicId,
|
||||
@RequestParam(value = "scenicIds", required = false) String scenicIds);
|
||||
|
||||
/**
|
||||
* 根据配置条件筛选设备
|
||||
*/
|
||||
@PostMapping("/filter-by-configs")
|
||||
@PostMapping("v2/filter-by-configs")
|
||||
CommonResponse<FilterDevicesByConfigsResponse> filterDevicesByConfigs(
|
||||
@RequestBody FilterDevicesByConfigsRequest request);
|
||||
|
||||
// ==================== Device Config V2 Operations ====================
|
||||
|
||||
/**
|
||||
* 获取设备所有配置
|
||||
*/
|
||||
@GetMapping("config/v2/{deviceId}")
|
||||
CommonResponse<List<DeviceConfigV2DTO>> getDeviceConfigs(@PathVariable("deviceId") Long deviceId);
|
||||
|
||||
/**
|
||||
* 根据设备编号获取设备所有配置
|
||||
*/
|
||||
@GetMapping("config/v2/no/{no}")
|
||||
CommonResponse<List<DeviceConfigV2DTO>> getDeviceConfigsByNo(@PathVariable("no") String no);
|
||||
|
||||
/**
|
||||
* 获取设备特定配置
|
||||
*/
|
||||
@GetMapping("config/v2/{deviceId}/key/{configKey}")
|
||||
CommonResponse<DeviceConfigV2DTO> getDeviceConfigByKey(@PathVariable("deviceId") Long deviceId,
|
||||
@PathVariable("configKey") String configKey);
|
||||
|
||||
/**
|
||||
* 创建设备配置
|
||||
*/
|
||||
@PostMapping("config/v2/{deviceId}")
|
||||
CommonResponse<DeviceConfigV2DTO> createDeviceConfig(@PathVariable("deviceId") Long deviceId,
|
||||
@RequestBody CreateDeviceConfigRequest request);
|
||||
|
||||
/**
|
||||
* 更新设备配置
|
||||
*/
|
||||
@PutMapping("config/v2/{deviceId}/{id}")
|
||||
CommonResponse<String> updateDeviceConfig(@PathVariable("deviceId") Long deviceId,
|
||||
@PathVariable("id") Long id,
|
||||
@RequestBody UpdateDeviceConfigRequest request);
|
||||
|
||||
/**
|
||||
* 删除设备配置
|
||||
*/
|
||||
@DeleteMapping("config/v2/{deviceId}/{id}")
|
||||
CommonResponse<String> deleteDeviceConfig(@PathVariable("deviceId") Long deviceId,
|
||||
@PathVariable("id") Long id);
|
||||
|
||||
/**
|
||||
* 批量更新设备配置
|
||||
*/
|
||||
@PostMapping("config/v2/{deviceId}/batch")
|
||||
CommonResponse<BatchUpdateResponse> batchUpdateDeviceConfig(@PathVariable("deviceId") Long deviceId,
|
||||
@RequestBody BatchDeviceConfigRequest request);
|
||||
|
||||
// ==================== Default Config Operations ====================
|
||||
|
||||
/**
|
||||
* 获取默认配置列表
|
||||
*/
|
||||
@GetMapping("config/v2/defaults")
|
||||
CommonResponse<PageResponse<DefaultConfigResponse>> listDefaultConfigs(@RequestParam(value = "page", defaultValue = "1") int page,
|
||||
@RequestParam(value = "pageSize", defaultValue = "10") int pageSize);
|
||||
|
||||
/**
|
||||
* 根据配置键获取默认配置
|
||||
*/
|
||||
@GetMapping("config/v2/defaults/{configKey}")
|
||||
CommonResponse<DefaultConfigResponse> getDefaultConfig(@PathVariable("configKey") String configKey);
|
||||
|
||||
/**
|
||||
* 创建默认配置
|
||||
*/
|
||||
@PostMapping("config/v2/defaults")
|
||||
CommonResponse<String> createDefaultConfig(@RequestBody DefaultConfigRequest request);
|
||||
|
||||
/**
|
||||
* 更新默认配置
|
||||
*/
|
||||
@PutMapping("config/v2/defaults/{configKey}")
|
||||
CommonResponse<Map<String, Object>> updateDefaultConfig(@PathVariable("configKey") String configKey,
|
||||
@RequestBody Map<String, Object> updates);
|
||||
|
||||
/**
|
||||
* 删除默认配置
|
||||
*/
|
||||
@DeleteMapping("config/v2/defaults/{configKey}")
|
||||
CommonResponse<String> deleteDefaultConfig(@PathVariable("configKey") String configKey);
|
||||
|
||||
/**
|
||||
* 批量更新默认配置
|
||||
*/
|
||||
@PostMapping("config/v2/defaults/batch")
|
||||
CommonResponse<BatchDefaultConfigResponse> batchUpdateDefaultConfigs(@RequestBody BatchDefaultConfigRequest request);
|
||||
|
||||
// ==================== Device Status Operations ====================
|
||||
|
||||
/**
|
||||
* 获取设备状态
|
||||
*/
|
||||
@GetMapping("status/{deviceNo}")
|
||||
CommonResponse<DeviceStatusDTO> getDeviceStatus(@PathVariable("deviceNo") String deviceNo);
|
||||
|
||||
/**
|
||||
* 检查设备是否在线
|
||||
*/
|
||||
@GetMapping("status/{deviceNo}/online")
|
||||
CommonResponse<OnlineStatusResponseDTO> isDeviceOnline(@PathVariable("deviceNo") String deviceNo);
|
||||
|
||||
/**
|
||||
* 获取所有在线设备
|
||||
*/
|
||||
@GetMapping("status/online")
|
||||
CommonResponse<List<DeviceStatusDTO>> getAllOnlineDevices();
|
||||
|
||||
/**
|
||||
* 设置设备离线
|
||||
*/
|
||||
@PostMapping("status/{deviceNo}/offline")
|
||||
CommonResponse<String> setDeviceOffline(@PathVariable("deviceNo") String deviceNo);
|
||||
|
||||
/**
|
||||
* 设置设备在线
|
||||
*/
|
||||
@PostMapping("status/{deviceNo}/online")
|
||||
CommonResponse<String> setDeviceOnline(@PathVariable("deviceNo") String deviceNo);
|
||||
|
||||
/**
|
||||
* 清理过期设备状态
|
||||
*/
|
||||
@PostMapping("status/clean")
|
||||
CommonResponse<String> cleanExpiredDevices();
|
||||
}
|
||||
@@ -3,7 +3,7 @@ package com.ycwl.basic.integration.device.service;
|
||||
import com.ycwl.basic.integration.common.exception.IntegrationException;
|
||||
import com.ycwl.basic.integration.common.response.CommonResponse;
|
||||
import com.ycwl.basic.integration.common.service.IntegrationFallbackService;
|
||||
import com.ycwl.basic.integration.device.client.DeviceConfigV2Client;
|
||||
import com.ycwl.basic.integration.device.client.DeviceV2Client;
|
||||
import com.ycwl.basic.integration.device.dto.config.*;
|
||||
import lombok.RequiredArgsConstructor;
|
||||
import lombok.extern.slf4j.Slf4j;
|
||||
@@ -18,7 +18,7 @@ import java.util.Map;
|
||||
@RequiredArgsConstructor
|
||||
public class DeviceConfigIntegrationService {
|
||||
|
||||
private final DeviceConfigV2Client deviceConfigV2Client;
|
||||
private final DeviceV2Client deviceConfigV2Client;
|
||||
private final IntegrationFallbackService fallbackService;
|
||||
|
||||
private static final String SERVICE_NAME = "zt-device";
|
||||
|
||||
@@ -4,7 +4,7 @@ import com.ycwl.basic.integration.common.exception.IntegrationException;
|
||||
import com.ycwl.basic.integration.common.response.CommonResponse;
|
||||
import com.ycwl.basic.integration.common.response.PageResponse;
|
||||
import com.ycwl.basic.integration.common.service.IntegrationFallbackService;
|
||||
import com.ycwl.basic.integration.device.client.DefaultConfigClient;
|
||||
import com.ycwl.basic.integration.device.client.DeviceV2Client;
|
||||
import com.ycwl.basic.integration.device.dto.defaults.*;
|
||||
import lombok.RequiredArgsConstructor;
|
||||
import lombok.extern.slf4j.Slf4j;
|
||||
@@ -21,7 +21,7 @@ import java.util.Map;
|
||||
@RequiredArgsConstructor
|
||||
public class DeviceDefaultConfigIntegrationService {
|
||||
|
||||
private final DefaultConfigClient defaultConfigClient;
|
||||
private final DeviceV2Client defaultConfigClient;
|
||||
private final IntegrationFallbackService fallbackService;
|
||||
|
||||
private static final String SERVICE_NAME = "zt-device";
|
||||
|
||||
@@ -67,11 +67,28 @@ public class DeviceIntegrationService {
|
||||
}
|
||||
|
||||
public PageResponse<DeviceV2DTO> listDevices(Integer page, Integer pageSize, String name, String no,
|
||||
String type, Integer isActive, Long scenicId) {
|
||||
log.debug("分页查询设备列表, page: {}, pageSize: {}, name: {}, no: {}, type: {}, isActive: {}, scenicId: {}",
|
||||
page, pageSize, name, no, type, isActive, scenicId);
|
||||
String type, Integer isActive, Long scenicId, String scenicIds) {
|
||||
log.debug("分页查询设备列表, page: {}, pageSize: {}, name: {}, no: {}, type: {}, isActive: {}, scenicId: {}, scenicIds: {}",
|
||||
page, pageSize, name, no, type, isActive, scenicId, scenicIds);
|
||||
|
||||
// 参数优先级处理:scenicId 优先于 scenicIds
|
||||
Long finalScenicId = null;
|
||||
String finalScenicIds = null;
|
||||
|
||||
if (scenicId != null) {
|
||||
// 优先使用单个 scenicId(向后兼容)
|
||||
finalScenicId = scenicId;
|
||||
finalScenicIds = null;
|
||||
log.debug("使用单个 scenicId 参数: {}", finalScenicId);
|
||||
} else if (scenicIds != null && !scenicIds.trim().isEmpty()) {
|
||||
// 使用 scenicIds
|
||||
finalScenicId = null;
|
||||
finalScenicIds = scenicIds;
|
||||
log.debug("使用 scenicIds 参数: {}", finalScenicIds);
|
||||
}
|
||||
|
||||
CommonResponse<PageResponse<DeviceV2DTO>> response = deviceV2Client.listDevices(
|
||||
page, pageSize, name, no, type, isActive, scenicId);
|
||||
page, pageSize, name, no, type, isActive, finalScenicId, finalScenicIds);
|
||||
return handleResponse(response, "分页查询设备列表失败");
|
||||
}
|
||||
|
||||
@@ -163,14 +180,14 @@ public class DeviceIntegrationService {
|
||||
* 获取景区的IPC设备列表
|
||||
*/
|
||||
public PageResponse<DeviceV2DTO> getScenicIpcDevices(Long scenicId, Integer page, Integer pageSize) {
|
||||
return listDevices(page, pageSize, null, null, "IPC", 1, scenicId);
|
||||
return listDevices(page, pageSize, null, null, "IPC", 1, scenicId, null);
|
||||
}
|
||||
|
||||
/**
|
||||
* 获取景区的所有激活设备
|
||||
*/
|
||||
public PageResponse<DeviceV2DTO> getScenicActiveDevices(Long scenicId, Integer page, Integer pageSize) {
|
||||
return listDevices(page, pageSize, null, null, null, 1, scenicId);
|
||||
return listDevices(page, pageSize, null, null, null, 1, scenicId, null);
|
||||
}
|
||||
|
||||
/**
|
||||
|
||||
@@ -3,7 +3,7 @@ package com.ycwl.basic.integration.device.service;
|
||||
import com.ycwl.basic.integration.common.exception.IntegrationException;
|
||||
import com.ycwl.basic.integration.common.response.CommonResponse;
|
||||
import com.ycwl.basic.integration.common.service.IntegrationFallbackService;
|
||||
import com.ycwl.basic.integration.device.client.DeviceStatusClient;
|
||||
import com.ycwl.basic.integration.device.client.DeviceV2Client;
|
||||
import com.ycwl.basic.integration.device.dto.status.DeviceStatusDTO;
|
||||
import com.ycwl.basic.integration.device.dto.status.OnlineStatusResponseDTO;
|
||||
import lombok.RequiredArgsConstructor;
|
||||
@@ -18,7 +18,7 @@ import java.util.Optional;
|
||||
@RequiredArgsConstructor
|
||||
public class DeviceStatusIntegrationService {
|
||||
|
||||
private final DeviceStatusClient deviceStatusClient;
|
||||
private final DeviceV2Client deviceStatusClient;
|
||||
private final IntegrationFallbackService fallbackService;
|
||||
|
||||
private static final String SERVICE_NAME = "zt-device";
|
||||
|
||||
17
src/main/java/com/ycwl/basic/integration/glm/GlmClient.java
Normal file
17
src/main/java/com/ycwl/basic/integration/glm/GlmClient.java
Normal file
@@ -0,0 +1,17 @@
|
||||
package com.ycwl.basic.integration.glm;
|
||||
|
||||
import java.util.List;
|
||||
|
||||
/**
|
||||
* 智谱 GLM 模型调用抽象。
|
||||
*/
|
||||
public interface GlmClient {
|
||||
/**
|
||||
* 流式回复,实时回调分片,同时返回完整文本。
|
||||
*/
|
||||
String streamReply(Long faceId,
|
||||
Long memberId,
|
||||
String traceId,
|
||||
List<ai.z.openapi.service.model.ChatMessage> messages,
|
||||
java.util.function.Consumer<String> chunkConsumer);
|
||||
}
|
||||
118
src/main/java/com/ycwl/basic/integration/glm/GlmClientImpl.java
Normal file
118
src/main/java/com/ycwl/basic/integration/glm/GlmClientImpl.java
Normal file
@@ -0,0 +1,118 @@
|
||||
package com.ycwl.basic.integration.glm;
|
||||
|
||||
import ai.z.openapi.ZhipuAiClient;
|
||||
import ai.z.openapi.service.model.ChatCompletionCreateParams;
|
||||
import ai.z.openapi.service.model.ChatCompletionResponse;
|
||||
import ai.z.openapi.service.model.ChatMessage;
|
||||
import ai.z.openapi.service.model.ChatMessageRole;
|
||||
import ai.z.openapi.service.model.ChatThinking;
|
||||
import ai.z.openapi.service.model.Delta;
|
||||
import ai.z.openapi.service.model.ModelData;
|
||||
import io.reactivex.rxjava3.core.Flowable;
|
||||
import lombok.extern.slf4j.Slf4j;
|
||||
import org.apache.commons.lang3.StringUtils;
|
||||
import org.springframework.beans.factory.annotation.Value;
|
||||
import org.springframework.stereotype.Component;
|
||||
|
||||
import java.util.ArrayList;
|
||||
import java.util.List;
|
||||
import java.util.function.Consumer;
|
||||
|
||||
/**
|
||||
* 智谱 GLM 官方 SDK 调用实现,流式拆分文本。
|
||||
*/
|
||||
@Slf4j
|
||||
@Component
|
||||
public class GlmClientImpl implements GlmClient {
|
||||
|
||||
private static final String DEFAULT_MODEL = "glm-4.5-airx";
|
||||
|
||||
private final ZhipuAiClient client;
|
||||
|
||||
public GlmClientImpl(@Value("${zhipu.api-key:}") String apiKeyFromCfg) {
|
||||
String apiKey = StringUtils.isNotBlank(apiKeyFromCfg) ? apiKeyFromCfg : System.getenv("ZHIPU_API_KEY");
|
||||
if (StringUtils.isBlank(apiKey)) {
|
||||
log.warn("未配置 ZHIPU_API_KEY,GLM 调用将返回占位结果");
|
||||
client = null;
|
||||
} else {
|
||||
client = ZhipuAiClient.builder().apiKey(apiKey).build();
|
||||
}
|
||||
}
|
||||
|
||||
@Override
|
||||
public String streamReply(Long faceId,
|
||||
Long memberId,
|
||||
String traceId,
|
||||
List<ChatMessage> messages,
|
||||
Consumer<String> chunkConsumer) {
|
||||
if (client == null) {
|
||||
String fallback = "GLM 未配置 API KEY,无法调用。";
|
||||
if (chunkConsumer != null) {
|
||||
chunkConsumer.accept(fallback);
|
||||
}
|
||||
return fallback;
|
||||
}
|
||||
List<ChatMessage> finalMessages = messages == null ? new ArrayList<>() : new ArrayList<>(messages);
|
||||
if (finalMessages.isEmpty()) {
|
||||
finalMessages.add(ChatMessage.builder()
|
||||
.role(ChatMessageRole.USER.value())
|
||||
.content("你好")
|
||||
.build());
|
||||
}
|
||||
finalMessages.addFirst(ChatMessage.builder()
|
||||
.role(ChatMessageRole.SYSTEM.value())
|
||||
.content("你是一个智能销售,然后用户是实际想购买的人。你需要尽可能推广景区生成的Vlog、视频、照片等内容给用户,鼓励购买。注意,返回内容需要为普通文本格式,禁止出现Markdown标记。")
|
||||
.build());
|
||||
finalMessages.addFirst(ChatMessage.builder()
|
||||
.role(ChatMessageRole.SYSTEM.value())
|
||||
.content("注意,返回内容需要为普通文本格式,禁止使用Markdown格式进行返回。")
|
||||
.build());
|
||||
ChatCompletionCreateParams request = ChatCompletionCreateParams.builder()
|
||||
.model(DEFAULT_MODEL)
|
||||
.messages(finalMessages)
|
||||
.thinking(ChatThinking.builder().type("enabled").build())
|
||||
.stream(true)
|
||||
.maxTokens(4096)
|
||||
.temperature(0.8f)
|
||||
.build();
|
||||
|
||||
ChatCompletionResponse response = client.chat().createChatCompletion(request);
|
||||
if (!response.isSuccess()) {
|
||||
String msg = "GLM 调用失败: " + response.getMsg();
|
||||
log.warn(msg);
|
||||
if (chunkConsumer != null) {
|
||||
chunkConsumer.accept(msg);
|
||||
}
|
||||
return msg;
|
||||
}
|
||||
StringBuilder sb = new StringBuilder();
|
||||
Flowable<ModelData> flowable = response.getFlowable();
|
||||
flowable.blockingSubscribe(
|
||||
data -> {
|
||||
if (data.getChoices() == null || data.getChoices().isEmpty()) {
|
||||
return;
|
||||
}
|
||||
Delta delta = data.getChoices().getFirst().getDelta();
|
||||
if (delta == null) {
|
||||
return;
|
||||
}
|
||||
String piece = delta.getContent();
|
||||
if (StringUtils.isNotBlank(piece)) {
|
||||
sb.append(piece);
|
||||
if (chunkConsumer != null) {
|
||||
chunkConsumer.accept(piece);
|
||||
}
|
||||
}
|
||||
},
|
||||
error -> {
|
||||
log.error("GLM 流式调用异常", error);
|
||||
String err = "GLM 调用异常:" + error.getMessage();
|
||||
sb.append(err);
|
||||
if (chunkConsumer != null) {
|
||||
chunkConsumer.accept(err);
|
||||
}
|
||||
}
|
||||
);
|
||||
return sb.toString();
|
||||
}
|
||||
}
|
||||
@@ -1,49 +0,0 @@
|
||||
package com.ycwl.basic.integration.kafka.config;
|
||||
|
||||
import lombok.extern.slf4j.Slf4j;
|
||||
import org.springframework.boot.autoconfigure.condition.ConditionalOnProperty;
|
||||
import org.springframework.context.annotation.Bean;
|
||||
import org.springframework.context.annotation.Configuration;
|
||||
|
||||
import java.util.concurrent.LinkedBlockingQueue;
|
||||
import java.util.concurrent.ThreadPoolExecutor;
|
||||
import java.util.concurrent.TimeUnit;
|
||||
|
||||
/**
|
||||
* 人脸识别异步处理线程池配置
|
||||
*/
|
||||
@Slf4j
|
||||
@Configuration
|
||||
@ConditionalOnProperty(name = "kafka.enabled", havingValue = "true")
|
||||
public class FaceRecognitionThreadPoolConfig {
|
||||
|
||||
/**
|
||||
* 创建人脸识别专用线程池
|
||||
* - 核心线程数:32
|
||||
* - 最大线程数:128
|
||||
* - 队列容量:1000(避免无限制增长)
|
||||
* - 拒绝策略:CallerRunsPolicy(调用者线程执行)
|
||||
*/
|
||||
@Bean(name = "faceRecognitionExecutor", destroyMethod = "shutdown")
|
||||
public ThreadPoolExecutor faceRecognitionExecutor() {
|
||||
ThreadPoolExecutor executor = new ThreadPoolExecutor(
|
||||
32, // 核心线程数
|
||||
128, // 最大线程数
|
||||
60L, // 空闲线程存活时间
|
||||
TimeUnit.SECONDS, // 时间单位
|
||||
new LinkedBlockingQueue<>(1000), // 任务队列
|
||||
r -> {
|
||||
Thread thread = new Thread(r);
|
||||
thread.setName("face-recognition-" + thread.getId());
|
||||
thread.setDaemon(false);
|
||||
return thread;
|
||||
},
|
||||
new ThreadPoolExecutor.CallerRunsPolicy() // 超过容量时由调用者线程执行
|
||||
);
|
||||
|
||||
log.info("人脸识别线程池初始化完成 - 核心线程数: {}, 最大线程数: {}, 队列容量: 1000",
|
||||
executor.getCorePoolSize(), executor.getMaximumPoolSize());
|
||||
|
||||
return executor;
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,180 @@
|
||||
package com.ycwl.basic.integration.kafka.scheduler;
|
||||
|
||||
import com.google.common.util.concurrent.ThreadFactoryBuilder;
|
||||
import lombok.AllArgsConstructor;
|
||||
import lombok.Data;
|
||||
import lombok.extern.slf4j.Slf4j;
|
||||
import org.springframework.stereotype.Component;
|
||||
|
||||
import java.util.HashMap;
|
||||
import java.util.Map;
|
||||
import java.util.concurrent.ConcurrentHashMap;
|
||||
import java.util.concurrent.LinkedBlockingQueue;
|
||||
import java.util.concurrent.ThreadPoolExecutor;
|
||||
import java.util.concurrent.TimeUnit;
|
||||
|
||||
/**
|
||||
* 账号级别的人脸识别调度器管理
|
||||
* 每个账号(accessKeyId/appId)拥有独立的:
|
||||
* 1. 线程池 - 资源隔离
|
||||
* 2. QPS调度器 - 精确控制每个账号的QPS
|
||||
* 3. 任务队列 - 独立排队
|
||||
* <p>
|
||||
* 核心优势:
|
||||
* - 多个阿里云账号互不影响,充分利用多账号QPS优势
|
||||
* - 百度云和阿里云任务完全隔离
|
||||
* - 每个账号严格按自己的QPS限制调度
|
||||
*/
|
||||
@Slf4j
|
||||
@Component
|
||||
public class AccountFaceSchedulerManager {
|
||||
|
||||
// 账号 -> 调度器上下文的映射
|
||||
private final ConcurrentHashMap<String, AccountSchedulerContext> schedulers = new ConcurrentHashMap<>();
|
||||
|
||||
/**
|
||||
* 获取或创建账号的调度器上下文
|
||||
*
|
||||
* @param accountKey 账号唯一标识 (accessKeyId 或 appId)
|
||||
* @param cloudType 云类型 ("ALI" 或 "BAIDU")
|
||||
* @param qps 该账号的QPS限制
|
||||
* @return 调度器上下文
|
||||
*/
|
||||
public AccountSchedulerContext getOrCreateScheduler(
|
||||
String accountKey,
|
||||
String cloudType,
|
||||
float qps
|
||||
) {
|
||||
return schedulers.computeIfAbsent(accountKey, key -> {
|
||||
log.info("创建账号调度器: accountKey={}, cloudType={}, qps={}",
|
||||
accountKey, cloudType, qps);
|
||||
return createSchedulerContext(accountKey, cloudType, qps);
|
||||
});
|
||||
}
|
||||
|
||||
/**
|
||||
* 创建调度器上下文
|
||||
*/
|
||||
private AccountSchedulerContext createSchedulerContext(
|
||||
String accountKey,
|
||||
String cloudType,
|
||||
float qps
|
||||
) {
|
||||
// 根据云类型和QPS计算线程池参数
|
||||
ThreadPoolConfig poolConfig = calculateThreadPoolConfig(cloudType, qps);
|
||||
|
||||
// 创建独立线程池
|
||||
ThreadPoolExecutor executor = new ThreadPoolExecutor(
|
||||
poolConfig.coreSize,
|
||||
poolConfig.maxSize,
|
||||
60L, TimeUnit.SECONDS,
|
||||
new LinkedBlockingQueue<>(poolConfig.queueCapacity),
|
||||
new ThreadFactoryBuilder()
|
||||
.setNameFormat(cloudType.toLowerCase() + "-" + accountKey.substring(0, Math.min(8, accountKey.length())) + "-%d")
|
||||
.build(),
|
||||
new ThreadPoolExecutor.AbortPolicy() // 快速失败,避免阻塞
|
||||
);
|
||||
|
||||
// 创建QPS调度器
|
||||
QpsScheduler scheduler = new QpsScheduler(
|
||||
Math.round(qps), // 每秒调度的任务数
|
||||
poolConfig.maxConcurrent, // 最大并发数
|
||||
executor
|
||||
);
|
||||
|
||||
log.info("账号调度器创建成功: accountKey={}, threadPool=[core={}, max={}, queue={}], qps={}, maxConcurrent={}",
|
||||
accountKey,
|
||||
poolConfig.coreSize,
|
||||
poolConfig.maxSize,
|
||||
poolConfig.queueCapacity,
|
||||
Math.round(qps),
|
||||
poolConfig.maxConcurrent);
|
||||
|
||||
return new AccountSchedulerContext(accountKey, cloudType, executor, scheduler);
|
||||
}
|
||||
|
||||
/**
|
||||
* 根据云类型和QPS计算线程池参数
|
||||
*/
|
||||
private ThreadPoolConfig calculateThreadPoolConfig(String cloudType, float qps) {
|
||||
// 假设每个任务平均执行时间 500ms
|
||||
int avgExecutionTimeMs = 500;
|
||||
|
||||
// 所需线程数 = QPS × 平均执行时间(秒)
|
||||
int requiredThreads = Math.max(1, (int) Math.ceil(qps * avgExecutionTimeMs / 1000.0));
|
||||
|
||||
// 核心线程数 = 所需线程数 × 2 (留有余量)
|
||||
int coreSize = requiredThreads * 2;
|
||||
|
||||
// 最大线程数 = 核心线程数 × 2
|
||||
int maxSize = coreSize * 2;
|
||||
|
||||
// 队列容量 = QPS × 60 (可容纳1分钟的任务)
|
||||
int queueCapacity = Math.max(100, (int) (qps * 60));
|
||||
|
||||
// 最大并发数 = 所需线程数 × 1.5 (防止瞬时抖动)
|
||||
int maxConcurrent = Math.max(2, (int) (requiredThreads * 1.5));
|
||||
|
||||
log.debug("计算线程池参数 - cloudType={}, qps={}, requiredThreads={}, coreSize={}, maxSize={}, queue={}, maxConcurrent={}",
|
||||
cloudType, qps, requiredThreads, coreSize, maxSize, queueCapacity, maxConcurrent);
|
||||
|
||||
return new ThreadPoolConfig(coreSize, maxSize, queueCapacity, maxConcurrent);
|
||||
}
|
||||
|
||||
/**
|
||||
* 获取所有调度器的监控信息
|
||||
*/
|
||||
public Map<String, AccountSchedulerStats> getAllStats() {
|
||||
Map<String, AccountSchedulerStats> stats = new HashMap<>();
|
||||
schedulers.forEach((key, ctx) -> {
|
||||
stats.put(key, new AccountSchedulerStats(
|
||||
ctx.getAccountKey(),
|
||||
ctx.getCloudType(),
|
||||
ctx.getExecutor().getActiveCount(),
|
||||
ctx.getExecutor().getQueue().size(),
|
||||
ctx.getScheduler().getQueueSize()
|
||||
));
|
||||
});
|
||||
return stats;
|
||||
}
|
||||
|
||||
/**
|
||||
* 关闭所有调度器 (应用关闭时调用)
|
||||
*/
|
||||
public void shutdownAll() {
|
||||
log.info("关闭所有账号调度器, total={}", schedulers.size());
|
||||
schedulers.forEach((key, ctx) -> {
|
||||
try {
|
||||
ctx.getScheduler().shutdown();
|
||||
ctx.getExecutor().shutdown();
|
||||
} catch (Exception e) {
|
||||
log.error("关闭调度器失败, accountKey={}", key, e);
|
||||
}
|
||||
});
|
||||
}
|
||||
|
||||
/**
|
||||
* 线程池配置
|
||||
*/
|
||||
@Data
|
||||
@AllArgsConstructor
|
||||
static class ThreadPoolConfig {
|
||||
int coreSize;
|
||||
int maxSize;
|
||||
int queueCapacity;
|
||||
int maxConcurrent;
|
||||
}
|
||||
|
||||
/**
|
||||
* 账号调度器统计信息
|
||||
*/
|
||||
@Data
|
||||
@AllArgsConstructor
|
||||
public static class AccountSchedulerStats {
|
||||
String accountKey;
|
||||
String cloudType;
|
||||
int activeThreads;
|
||||
int executorQueueSize;
|
||||
int schedulerQueueSize;
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,34 @@
|
||||
package com.ycwl.basic.integration.kafka.scheduler;
|
||||
|
||||
import lombok.AllArgsConstructor;
|
||||
import lombok.Data;
|
||||
|
||||
import java.util.concurrent.ThreadPoolExecutor;
|
||||
|
||||
/**
|
||||
* 账号调度器上下文
|
||||
* 封装每个账号的线程池和QPS调度器
|
||||
*/
|
||||
@Data
|
||||
@AllArgsConstructor
|
||||
public class AccountSchedulerContext {
|
||||
/**
|
||||
* 账号唯一标识 (accessKeyId 或 appId)
|
||||
*/
|
||||
private String accountKey;
|
||||
|
||||
/**
|
||||
* 云类型 ("ALI" 或 "BAIDU")
|
||||
*/
|
||||
private String cloudType;
|
||||
|
||||
/**
|
||||
* 该账号专属的线程池
|
||||
*/
|
||||
private ThreadPoolExecutor executor;
|
||||
|
||||
/**
|
||||
* 该账号专属的QPS调度器
|
||||
*/
|
||||
private QpsScheduler scheduler;
|
||||
}
|
||||
@@ -0,0 +1,114 @@
|
||||
package com.ycwl.basic.integration.kafka.scheduler;
|
||||
|
||||
import lombok.extern.slf4j.Slf4j;
|
||||
|
||||
import java.util.concurrent.*;
|
||||
|
||||
/**
|
||||
* QPS 调度器
|
||||
* 定期从队列取任务,严格控制 QPS
|
||||
* 每秒调度固定数量的任务,确保不超过云端 API 的 QPS 限制
|
||||
*/
|
||||
@Slf4j
|
||||
public class QpsScheduler {
|
||||
private final BlockingQueue<Runnable> taskQueue;
|
||||
private final ThreadPoolExecutor workerPool;
|
||||
private final ScheduledExecutorService scheduler;
|
||||
private final int qps;
|
||||
private final Semaphore concurrentLimiter; // 并发数限制,防止瞬时抖动
|
||||
|
||||
/**
|
||||
* 创建 QPS 调度器
|
||||
*
|
||||
* @param qps 每秒允许的最大请求数
|
||||
* @param maxConcurrent 最大并发数
|
||||
* @param workerPool 工作线程池
|
||||
*/
|
||||
public QpsScheduler(int qps, int maxConcurrent, ThreadPoolExecutor workerPool) {
|
||||
this.qps = qps;
|
||||
this.taskQueue = new LinkedBlockingQueue<>();
|
||||
this.workerPool = workerPool;
|
||||
this.scheduler = new ScheduledThreadPoolExecutor(1, r -> {
|
||||
Thread thread = new Thread(r);
|
||||
thread.setName("qps-scheduler-" + workerPool.getThreadFactory().newThread(() -> {}).getName());
|
||||
thread.setDaemon(true);
|
||||
return thread;
|
||||
});
|
||||
this.concurrentLimiter = new Semaphore(maxConcurrent);
|
||||
|
||||
// 每秒调度一次,取 qps 个任务
|
||||
scheduler.scheduleAtFixedRate(this::dispatch, 0, 1, TimeUnit.SECONDS);
|
||||
|
||||
log.info("QPS调度器已启动: qps={}, maxConcurrent={}", qps, maxConcurrent);
|
||||
}
|
||||
|
||||
/**
|
||||
* 调度任务
|
||||
* 每秒执行一次,从队列中取出 qps 个任务提交到工作线程池
|
||||
*/
|
||||
private void dispatch() {
|
||||
int dispatched = 0;
|
||||
for (int i = 0; i < qps; i++) {
|
||||
Runnable task = taskQueue.poll();
|
||||
if (task == null) {
|
||||
break; // 队列为空,结束本次调度
|
||||
}
|
||||
|
||||
// 检查并发数限制
|
||||
if (concurrentLimiter.tryAcquire()) {
|
||||
try {
|
||||
workerPool.execute(() -> {
|
||||
try {
|
||||
task.run();
|
||||
} catch (Exception e) {
|
||||
log.error("任务执行失败", e);
|
||||
} finally {
|
||||
concurrentLimiter.release();
|
||||
}
|
||||
});
|
||||
dispatched++;
|
||||
} catch (RejectedExecutionException e) {
|
||||
// 线程池拒绝,释放并发许可,任务丢弃
|
||||
concurrentLimiter.release();
|
||||
log.warn("任务被线程池拒绝", e);
|
||||
}
|
||||
} else {
|
||||
// 并发数已满,任务放回队列,等待下次调度
|
||||
taskQueue.offer(task);
|
||||
break;
|
||||
}
|
||||
}
|
||||
|
||||
if (dispatched > 0 || taskQueue.size() > 0) {
|
||||
log.debug("QPS调度完成: dispatched={}, remainQueue={}, availableConcurrent={}",
|
||||
dispatched, taskQueue.size(), concurrentLimiter.availablePermits());
|
||||
}
|
||||
}
|
||||
|
||||
/**
|
||||
* 提交任务到调度队列
|
||||
*
|
||||
* @param task 待执行的任务
|
||||
* @return 是否成功提交
|
||||
*/
|
||||
public boolean submit(Runnable task) {
|
||||
return taskQueue.offer(task);
|
||||
}
|
||||
|
||||
/**
|
||||
* 获取队列中等待调度的任务数量
|
||||
*
|
||||
* @return 队列大小
|
||||
*/
|
||||
public int getQueueSize() {
|
||||
return taskQueue.size();
|
||||
}
|
||||
|
||||
/**
|
||||
* 关闭调度器
|
||||
*/
|
||||
public void shutdown() {
|
||||
scheduler.shutdown();
|
||||
log.info("QPS调度器已关闭, qps={}", qps);
|
||||
}
|
||||
}
|
||||
@@ -1,17 +1,22 @@
|
||||
package com.ycwl.basic.integration.kafka.service;
|
||||
|
||||
import com.fasterxml.jackson.databind.ObjectMapper;
|
||||
import com.ycwl.basic.facebody.adapter.AliFaceBodyAdapter;
|
||||
import com.ycwl.basic.facebody.adapter.BceFaceBodyAdapter;
|
||||
import com.ycwl.basic.facebody.adapter.IFaceBodyAdapter;
|
||||
import com.ycwl.basic.facebody.entity.AddFaceResp;
|
||||
import com.ycwl.basic.facebody.entity.AliFaceBodyConfig;
|
||||
import com.ycwl.basic.facebody.entity.BceFaceBodyConfig;
|
||||
import com.ycwl.basic.integration.common.manager.DeviceConfigManager;
|
||||
import com.ycwl.basic.integration.kafka.dto.FaceProcessingMessage;
|
||||
import com.ycwl.basic.integration.kafka.scheduler.AccountFaceSchedulerManager;
|
||||
import com.ycwl.basic.integration.kafka.scheduler.AccountSchedulerContext;
|
||||
import com.ycwl.basic.mapper.FaceSampleAiCamMapper;
|
||||
import com.ycwl.basic.mapper.FaceSampleMapper;
|
||||
import com.ycwl.basic.model.pc.faceSample.entity.FaceSampleEntity;
|
||||
import com.ycwl.basic.repository.DeviceRepository;
|
||||
import com.ycwl.basic.service.pc.ScenicService;
|
||||
import com.ycwl.basic.service.task.TaskFaceService;
|
||||
import com.ycwl.basic.task.DynamicTaskGenerator;
|
||||
import com.ycwl.basic.integration.common.manager.DeviceConfigManager;
|
||||
// 不再需要SnowFlakeUtil,使用外部传入的ID
|
||||
import com.ycwl.basic.utils.JacksonUtil;
|
||||
import lombok.RequiredArgsConstructor;
|
||||
import lombok.extern.slf4j.Slf4j;
|
||||
@@ -20,13 +25,16 @@ import org.springframework.kafka.annotation.KafkaListener;
|
||||
import org.springframework.kafka.support.Acknowledgment;
|
||||
import org.springframework.stereotype.Service;
|
||||
|
||||
import java.time.ZoneId;
|
||||
import java.util.Date;
|
||||
import java.util.concurrent.ThreadPoolExecutor;
|
||||
|
||||
/**
|
||||
* 人脸处理Kafka消费服务
|
||||
* 消费外部系统发送到zt-face topic的消息
|
||||
* <p>
|
||||
* 核心改进:
|
||||
* 1. 按账号(accessKeyId/appId)隔离线程池和QPS调度器
|
||||
* 2. 确保数据库优先写入,状态流转清晰
|
||||
* 3. 严格QPS控制,线程资源高效利用
|
||||
*/
|
||||
@Slf4j
|
||||
@Service
|
||||
@@ -35,56 +43,212 @@ import java.util.concurrent.ThreadPoolExecutor;
|
||||
public class FaceProcessingKafkaService {
|
||||
|
||||
private static final String ZT_FACE_TOPIC = "zt-face";
|
||||
private static final String ZT_AI_CAM_FACE_TOPIC = "zt-ai-cam-face";
|
||||
|
||||
private final FaceSampleMapper faceSampleMapper;
|
||||
private final FaceSampleAiCamMapper faceSampleAiCamMapper;
|
||||
private final TaskFaceService taskFaceService;
|
||||
private final ScenicService scenicService;
|
||||
private final DeviceRepository deviceRepository;
|
||||
private final ThreadPoolExecutor faceRecognitionExecutor;
|
||||
private final AccountFaceSchedulerManager schedulerManager;
|
||||
|
||||
/**
|
||||
* 消费外部系统发送的人脸处理消息
|
||||
* 先保存人脸样本数据,再进行异步人脸识别处理
|
||||
* 核心流程:
|
||||
* 1. 同步写入数据库 (最高优先级)
|
||||
* 2. 获取账号调度器上下文
|
||||
* 3. 提交到账号专属调度器队列
|
||||
*/
|
||||
@KafkaListener(topics = ZT_FACE_TOPIC, containerFactory = "manualCommitKafkaListenerContainerFactory")
|
||||
public void processFaceMessage(String message, Acknowledgment ack) {
|
||||
Long faceSampleId = null;
|
||||
try {
|
||||
FaceProcessingMessage faceMessage = JacksonUtil.parseObject(message, FaceProcessingMessage.class);
|
||||
log.debug("接收到外部人脸处理消息, scenicId: {}, deviceId: {}, faceUrl: {}",
|
||||
faceMessage.getScenicId(), faceMessage.getDeviceId(), faceMessage.getFaceUrl());
|
||||
faceSampleId = faceMessage.getFaceSampleId();
|
||||
|
||||
// 使用外部传入的faceSampleId
|
||||
Long externalFaceId = faceMessage.getFaceSampleId();
|
||||
if (externalFaceId == null) {
|
||||
log.error("外部消息中未包含faceSampleId");
|
||||
// 即使消息格式错误,也消费消息避免重复处理
|
||||
log.debug("接收人脸消息: scenicId={}, deviceId={}, faceSampleId={}",
|
||||
faceMessage.getScenicId(), faceMessage.getDeviceId(), faceSampleId);
|
||||
|
||||
// ========== 第一步: 同步写入数据库 (最高优先级) ==========
|
||||
boolean saved = saveFaceSample(faceMessage, faceSampleId);
|
||||
if (!saved) {
|
||||
log.error("数据库写入失败, 不提交识别任务, faceSampleId={}", faceSampleId);
|
||||
// 数据库写入失败,消费消息避免重复
|
||||
ack.acknowledge();
|
||||
return;
|
||||
}
|
||||
|
||||
// 先保存人脸样本数据
|
||||
boolean saved = saveFaceSample(faceMessage, externalFaceId);
|
||||
|
||||
// 然后异步进行人脸识别处理(使用专用线程池)
|
||||
if (saved) {
|
||||
faceRecognitionExecutor.execute(() -> processFaceRecognitionAsync(faceMessage));
|
||||
log.debug("人脸识别任务已提交至线程池, faceSampleId: {}, 活跃线程: {}, 队列大小: {}",
|
||||
externalFaceId, faceRecognitionExecutor.getActiveCount(),
|
||||
faceRecognitionExecutor.getQueue().size());
|
||||
} else {
|
||||
log.warn("人脸样本保存失败,但消息仍将被消费, faceSampleId: {}", externalFaceId);
|
||||
log.debug("数据库写入成功, faceSampleId={}, status=0", faceSampleId);
|
||||
|
||||
// ========== 第二步: 获取账号调度器上下文 ==========
|
||||
AccountSchedulerContext schedulerCtx = getSchedulerContextForScenic(faceMessage.getScenicId());
|
||||
if (schedulerCtx == null) {
|
||||
log.error("无法获取调度器上下文, faceSampleId={}", faceSampleId);
|
||||
updateFaceSampleStatusSafely(faceSampleId, -1);
|
||||
ack.acknowledge();
|
||||
return;
|
||||
}
|
||||
|
||||
// 无论处理是否成功,都消费消息
|
||||
// ========== 第三步: 提交到账号专属调度器 ==========
|
||||
boolean submitted = schedulerCtx.getScheduler().submit(() -> {
|
||||
processFaceRecognitionAsync(faceMessage);
|
||||
});
|
||||
|
||||
if (submitted) {
|
||||
log.debug("任务已提交到调度器, account={}, cloudType={}, faceSampleId={}, schedulerQueue={}",
|
||||
schedulerCtx.getAccountKey(),
|
||||
schedulerCtx.getCloudType(),
|
||||
faceSampleId,
|
||||
schedulerCtx.getScheduler().getQueueSize());
|
||||
} else {
|
||||
log.error("调度器队列已满, account={}, faceSampleId={}",
|
||||
schedulerCtx.getAccountKey(), faceSampleId);
|
||||
updateFaceSampleStatusSafely(faceSampleId, -1);
|
||||
}
|
||||
|
||||
// 无论成功失败,都消费消息
|
||||
ack.acknowledge();
|
||||
|
||||
} catch (Exception e) {
|
||||
log.error("处理外部人脸消息失败: {}", e.getMessage(), e);
|
||||
// 即使发生异常也消费消息,避免消息堆积
|
||||
log.error("处理人脸消息异常, faceSampleId={}", faceSampleId, e);
|
||||
if (faceSampleId != null) {
|
||||
updateFaceSampleStatusSafely(faceSampleId, -1);
|
||||
}
|
||||
ack.acknowledge();
|
||||
}
|
||||
}
|
||||
|
||||
/**
|
||||
* 消费AI相机发送的人脸处理消息 (zt-ai-cam-face)
|
||||
* 逻辑与 zt-face 类似,但写入不同的表,且人脸库分组依据不同
|
||||
*/
|
||||
@KafkaListener(topics = ZT_AI_CAM_FACE_TOPIC, containerFactory = "manualCommitKafkaListenerContainerFactory")
|
||||
public void processAiCamFaceMessage(String message, Acknowledgment ack) {
|
||||
Long faceSampleId = null;
|
||||
try {
|
||||
FaceProcessingMessage faceMessage = JacksonUtil.parseObject(message, FaceProcessingMessage.class);
|
||||
faceSampleId = faceMessage.getFaceSampleId();
|
||||
|
||||
log.debug("接收AI相机人脸消息: scenicId={}, deviceId={}, faceSampleId={}",
|
||||
faceMessage.getScenicId(), faceMessage.getDeviceId(), faceSampleId);
|
||||
|
||||
// ========== 第一步: 同步写入数据库 (FaceSampleAiCam) ==========
|
||||
boolean saved = saveAiCamFaceSample(faceMessage, faceSampleId);
|
||||
if (!saved) {
|
||||
log.error("AI相机数据库写入失败, 不提交识别任务, faceSampleId={}", faceSampleId);
|
||||
ack.acknowledge();
|
||||
return;
|
||||
}
|
||||
|
||||
log.debug("AI相机数据库写入成功, faceSampleId={}, status=0", faceSampleId);
|
||||
|
||||
// ========== 第二步: 获取账号调度器上下文 ==========
|
||||
AccountSchedulerContext schedulerCtx = getSchedulerContextForScenic(faceMessage.getScenicId());
|
||||
if (schedulerCtx == null) {
|
||||
log.error("无法获取调度器上下文, faceSampleId={}", faceSampleId);
|
||||
updateAiCamFaceSampleStatusSafely(faceSampleId, -1);
|
||||
ack.acknowledge();
|
||||
return;
|
||||
}
|
||||
|
||||
// ========== 第三步: 提交到账号专属调度器 ==========
|
||||
boolean submitted = schedulerCtx.getScheduler().submit(() -> {
|
||||
processAiCamFaceRecognitionAsync(faceMessage);
|
||||
});
|
||||
|
||||
if (submitted) {
|
||||
log.debug("AI相机任务已提交到调度器, account={}, cloudType={}, faceSampleId={}, schedulerQueue={}",
|
||||
schedulerCtx.getAccountKey(),
|
||||
schedulerCtx.getCloudType(),
|
||||
faceSampleId,
|
||||
schedulerCtx.getScheduler().getQueueSize());
|
||||
} else {
|
||||
log.error("调度器队列已满, account={}, faceSampleId={}",
|
||||
schedulerCtx.getAccountKey(), faceSampleId);
|
||||
updateAiCamFaceSampleStatusSafely(faceSampleId, -1);
|
||||
}
|
||||
|
||||
ack.acknowledge();
|
||||
|
||||
} catch (Exception e) {
|
||||
log.error("处理AI相机人脸消息异常, faceSampleId={}", faceSampleId, e);
|
||||
if (faceSampleId != null) {
|
||||
updateAiCamFaceSampleStatusSafely(faceSampleId, -1);
|
||||
}
|
||||
ack.acknowledge();
|
||||
}
|
||||
}
|
||||
|
||||
/**
|
||||
* 根据景区获取对应的账号调度器上下文
|
||||
* 关键: 按 accessKeyId/appId 隔离,而非按云类型
|
||||
*/
|
||||
private AccountSchedulerContext getSchedulerContextForScenic(Long scenicId) {
|
||||
try {
|
||||
// 获取景区的 adapter
|
||||
IFaceBodyAdapter adapter = scenicService.getScenicFaceBodyAdapter(scenicId);
|
||||
if (adapter == null) {
|
||||
log.error("景区 adapter 不存在, scenicId={}", scenicId);
|
||||
return null;
|
||||
}
|
||||
|
||||
// 提取账号信息和QPS配置
|
||||
if (adapter instanceof AliFaceBodyAdapter aliAdapter) {
|
||||
AliFaceBodyConfig config = aliAdapter.getConfig();
|
||||
|
||||
if (config == null || config.getAccessKeyId() == null) {
|
||||
log.error("阿里云配置为空, scenicId={}", scenicId);
|
||||
return null;
|
||||
}
|
||||
|
||||
// 使用 accessKeyId 作为唯一标识
|
||||
String accountKey = config.getAccessKeyId();
|
||||
float qps = 2.0f; // 阿里云固定 2 QPS (AddFace操作)
|
||||
|
||||
return schedulerManager.getOrCreateScheduler(accountKey, "ALI", qps);
|
||||
|
||||
} else if (adapter instanceof BceFaceBodyAdapter baiduAdapter) {
|
||||
BceFaceBodyConfig config = baiduAdapter.getConfig();
|
||||
|
||||
if (config == null || config.getAppId() == null) {
|
||||
log.error("百度云配置为空, scenicId={}", scenicId);
|
||||
return null;
|
||||
}
|
||||
|
||||
// 使用 appId 作为唯一标识
|
||||
String accountKey = config.getAppId();
|
||||
float qps = config.getAddQps(); // 百度云可配置 QPS
|
||||
|
||||
return schedulerManager.getOrCreateScheduler(accountKey, "BAIDU", qps);
|
||||
|
||||
} else {
|
||||
log.error("未知的 adapter 类型: {}", adapter.getClass().getName());
|
||||
return null;
|
||||
}
|
||||
|
||||
} catch (Exception e) {
|
||||
log.error("获取调度器上下文失败, scenicId={}", scenicId, e);
|
||||
return null;
|
||||
}
|
||||
}
|
||||
|
||||
/**
|
||||
* 安全地更新人脸样本状态
|
||||
* 捕获所有异常,确保状态更新失败不影响主流程
|
||||
*/
|
||||
private void updateFaceSampleStatusSafely(Long faceSampleId, Integer status) {
|
||||
if (faceSampleId == null) {
|
||||
return;
|
||||
}
|
||||
try {
|
||||
faceSampleMapper.updateStatus(faceSampleId, status);
|
||||
log.debug("状态更新成功: faceSampleId={}, status={}", faceSampleId, status);
|
||||
} catch (Exception e) {
|
||||
log.error("状态更新失败(非致命): faceSampleId={}, status={}", faceSampleId, status, e);
|
||||
// 不抛出异常,避免影响消息消费
|
||||
}
|
||||
}
|
||||
|
||||
/**
|
||||
* 保存人脸样本数据到数据库
|
||||
* @param faceMessage 人脸处理消息
|
||||
@@ -126,69 +290,59 @@ public class FaceProcessingKafkaService {
|
||||
private void processFaceRecognitionAsync(FaceProcessingMessage message) {
|
||||
Long faceSampleId = message.getFaceSampleId();
|
||||
Long scenicId = message.getScenicId();
|
||||
String faceUrl = message.getFaceUrl();
|
||||
|
||||
// 直接使用faceSampleId作为唯一标识
|
||||
String faceUniqueId = faceSampleId.toString();
|
||||
|
||||
// 获取人脸识别适配器
|
||||
IFaceBodyAdapter faceBodyAdapter = scenicService.getScenicFaceBodyAdapter(scenicId);
|
||||
if (faceBodyAdapter == null) {
|
||||
log.error("人脸识别适配器不存在, scenicId: {}", scenicId);
|
||||
updateFaceSampleStatus(faceSampleId, -1);
|
||||
return;
|
||||
}
|
||||
|
||||
try {
|
||||
// 更新状态为处理中
|
||||
updateFaceSampleStatus(faceSampleId, 1);
|
||||
// ========== 第一步: 更新状态为"处理中" ==========
|
||||
updateFaceSampleStatusSafely(faceSampleId, 1);
|
||||
log.debug("开始人脸识别, faceSampleId={}, status=1", faceSampleId);
|
||||
|
||||
// 确保人脸数据库存在
|
||||
taskFaceService.assureFaceDb(faceBodyAdapter, scenicId.toString());
|
||||
// ========== 第二步: 获取 adapter ==========
|
||||
IFaceBodyAdapter adapter = scenicService.getScenicFaceBodyAdapter(scenicId);
|
||||
if (adapter == null) {
|
||||
log.error("adapter 不存在, scenicId={}, faceSampleId={}", scenicId, faceSampleId);
|
||||
updateFaceSampleStatusSafely(faceSampleId, -1);
|
||||
return;
|
||||
}
|
||||
|
||||
// 添加人脸到识别服务(使用faceSampleId作为唯一标识)
|
||||
AddFaceResp addFaceResp = faceBodyAdapter.addFace(
|
||||
scenicId.toString(),
|
||||
faceSampleId.toString(),
|
||||
faceUrl,
|
||||
faceUniqueId // 即faceSampleId.toString()
|
||||
// ========== 第三步: 确保人脸数据库存在 ==========
|
||||
taskFaceService.assureFaceDb(adapter, scenicId.toString());
|
||||
|
||||
// ========== 第四步: 调用 addFace (QPS已由调度器控制) ==========
|
||||
String faceUniqueId = faceSampleId.toString();
|
||||
AddFaceResp addFaceResp = adapter.addFace(
|
||||
scenicId.toString(),
|
||||
faceSampleId.toString(),
|
||||
message.getFaceUrl(),
|
||||
faceUniqueId
|
||||
);
|
||||
|
||||
// ========== 第五步: 更新识别结果 ==========
|
||||
if (addFaceResp != null) {
|
||||
// 更新人脸样本得分和状态
|
||||
// 成功: 更新 score 和状态
|
||||
faceSampleMapper.updateScore(faceSampleId, addFaceResp.getScore());
|
||||
updateFaceSampleStatus(faceSampleId, 2);
|
||||
log.debug("人脸识别处理成功, faceSampleId: {}", faceSampleId);
|
||||
updateFaceSampleStatusSafely(faceSampleId, 2);
|
||||
|
||||
// 查询设备配置,判断是否启用预订功能
|
||||
log.info("人脸识别成功, faceSampleId={}, score={}, status=2",
|
||||
faceSampleId, addFaceResp.getScore());
|
||||
|
||||
// 可选: 触发预订任务
|
||||
Long deviceId = message.getDeviceId();
|
||||
DeviceConfigManager deviceConfig = deviceRepository.getDeviceConfigManager(deviceId);
|
||||
if (deviceConfig != null &&
|
||||
Integer.valueOf(1).equals(deviceConfig.getInteger("enable_pre_book"))) {
|
||||
Integer.valueOf(1).equals(deviceConfig.getInteger("enable_pre_book"))) {
|
||||
DynamicTaskGenerator.addTask(faceSampleId);
|
||||
}
|
||||
|
||||
} else {
|
||||
log.warn("人脸添加返回空结果, faceSampleId: {}", faceSampleId);
|
||||
updateFaceSampleStatus(faceSampleId, -1);
|
||||
// addFace 返回 null,识别失败
|
||||
log.warn("addFace 返回 null, faceSampleId={}", faceSampleId);
|
||||
updateFaceSampleStatusSafely(faceSampleId, -1);
|
||||
}
|
||||
|
||||
} catch (Exception e) {
|
||||
log.error("人脸识别处理失败, faceSampleId: {}, error: {}",
|
||||
faceSampleId, e.getMessage(), e);
|
||||
|
||||
// 标记人脸样本为处理失败状态
|
||||
updateFaceSampleStatus(faceSampleId, -1);
|
||||
}
|
||||
}
|
||||
|
||||
/**
|
||||
* 更新人脸样本状态
|
||||
*/
|
||||
private void updateFaceSampleStatus(Long faceSampleId, Integer status) {
|
||||
try {
|
||||
faceSampleMapper.updateStatus(faceSampleId, status);
|
||||
} catch (Exception e) {
|
||||
log.error("更新人脸样本状态失败, faceSampleId: {}", faceSampleId, e);
|
||||
// ========== 异常处理: 更新状态为失败 ==========
|
||||
log.error("人脸识别异常, faceSampleId={}", faceSampleId, e);
|
||||
updateFaceSampleStatusSafely(faceSampleId, -1);
|
||||
}
|
||||
}
|
||||
|
||||
@@ -226,18 +380,132 @@ public class FaceProcessingKafkaService {
|
||||
.source("retry-manual")
|
||||
.build();
|
||||
|
||||
// 提交到线程池进行异步处理
|
||||
faceRecognitionExecutor.execute(() -> processFaceRecognitionAsync(message));
|
||||
// 获取账号调度器上下文
|
||||
AccountSchedulerContext schedulerCtx = getSchedulerContextForScenic(faceSample.getScenicId());
|
||||
if (schedulerCtx == null) {
|
||||
log.error("无法获取调度器上下文, faceSampleId={}", faceSampleId);
|
||||
return false;
|
||||
}
|
||||
|
||||
log.info("人脸识别重试任务已提交, faceSampleId: {}, 活跃线程: {}, 队列大小: {}",
|
||||
faceSampleId, faceRecognitionExecutor.getActiveCount(),
|
||||
faceRecognitionExecutor.getQueue().size());
|
||||
// 提交到调度器进行异步处理
|
||||
boolean submitted = schedulerCtx.getScheduler().submit(() -> processFaceRecognitionAsync(message));
|
||||
|
||||
return true;
|
||||
if (submitted) {
|
||||
log.info("人脸识别重试任务已提交, faceSampleId={}, account={}, cloudType={}, schedulerQueue={}",
|
||||
faceSampleId,
|
||||
schedulerCtx.getAccountKey(),
|
||||
schedulerCtx.getCloudType(),
|
||||
schedulerCtx.getScheduler().getQueueSize());
|
||||
return true;
|
||||
} else {
|
||||
log.error("调度器队列已满,重试任务被拒绝, faceSampleId={}", faceSampleId);
|
||||
return false;
|
||||
}
|
||||
|
||||
} catch (Exception e) {
|
||||
log.error("提交人脸识别重试任务失败, faceSampleId: {}", faceSampleId, e);
|
||||
return false;
|
||||
}
|
||||
}
|
||||
|
||||
/**
|
||||
* 安全地更新AI相机人脸样本状态
|
||||
*/
|
||||
private void updateAiCamFaceSampleStatusSafely(Long faceSampleId, Integer status) {
|
||||
if (faceSampleId == null) {
|
||||
return;
|
||||
}
|
||||
try {
|
||||
faceSampleAiCamMapper.updateStatus(faceSampleId, status);
|
||||
log.debug("AI相机样本状态更新成功: faceSampleId={}, status={}", faceSampleId, status);
|
||||
} catch (Exception e) {
|
||||
log.error("AI相机样本状态更新失败(非致命): faceSampleId={}, status={}", faceSampleId, status, e);
|
||||
}
|
||||
}
|
||||
|
||||
/**
|
||||
* 保存AI相机人脸样本数据到数据库
|
||||
*/
|
||||
private boolean saveAiCamFaceSample(FaceProcessingMessage faceMessage, Long externalFaceId) {
|
||||
try {
|
||||
FaceSampleEntity faceSample = new FaceSampleEntity();
|
||||
faceSample.setId(externalFaceId); // 使用外部传入的ID
|
||||
faceSample.setScenicId(faceMessage.getScenicId());
|
||||
faceSample.setDeviceId(faceMessage.getDeviceId());
|
||||
faceSample.setStatus(0); // 初始状态
|
||||
faceSample.setFaceUrl(faceMessage.getFaceUrl());
|
||||
|
||||
if (faceMessage.getShotTime() != null) {
|
||||
faceSample.setCreateAt(faceMessage.getShotTime());
|
||||
} else {
|
||||
faceSample.setCreateAt(new Date());
|
||||
}
|
||||
|
||||
faceSampleAiCamMapper.add(faceSample);
|
||||
|
||||
return true;
|
||||
} catch (Exception e) {
|
||||
log.error("保存AI相机人脸样本数据失败, 外部faceId: {}, scenicId: {}, deviceId: {}",
|
||||
externalFaceId, faceMessage.getScenicId(), faceMessage.getDeviceId(), e);
|
||||
return false;
|
||||
}
|
||||
}
|
||||
|
||||
/**
|
||||
* 异步执行AI相机人脸识别处理逻辑
|
||||
* 区别: 使用 deviceId 作为人脸库分组
|
||||
*/
|
||||
private void processAiCamFaceRecognitionAsync(FaceProcessingMessage message) {
|
||||
Long faceSampleId = message.getFaceSampleId();
|
||||
Long scenicId = message.getScenicId();
|
||||
Long deviceId = message.getDeviceId();
|
||||
|
||||
try {
|
||||
updateAiCamFaceSampleStatusSafely(faceSampleId, 1);
|
||||
log.debug("开始AI相机人脸识别, faceSampleId={}, status=1", faceSampleId);
|
||||
|
||||
IFaceBodyAdapter adapter = scenicService.getScenicFaceBodyAdapter(scenicId);
|
||||
if (adapter == null) {
|
||||
log.error("adapter 不存在, scenicId={}, faceSampleId={}", scenicId, faceSampleId);
|
||||
updateAiCamFaceSampleStatusSafely(faceSampleId, -1);
|
||||
return;
|
||||
}
|
||||
|
||||
// 使用 "AiCam" + deviceId 作为人脸库分组
|
||||
String dbName = "AiCam" + deviceId;
|
||||
taskFaceService.assureFaceDb(adapter, dbName);
|
||||
|
||||
String faceUniqueId = faceSampleId.toString();
|
||||
// groupName 使用 deviceId
|
||||
AddFaceResp addFaceResp = adapter.addFace(
|
||||
dbName,
|
||||
faceSampleId.toString(),
|
||||
message.getFaceUrl(),
|
||||
faceUniqueId
|
||||
);
|
||||
|
||||
if (addFaceResp != null) {
|
||||
faceSampleAiCamMapper.updateScore(faceSampleId, addFaceResp.getScore());
|
||||
updateAiCamFaceSampleStatusSafely(faceSampleId, 2);
|
||||
|
||||
log.info("AI相机人脸识别成功, faceSampleId={}, score={}, status=2",
|
||||
faceSampleId, addFaceResp.getScore());
|
||||
|
||||
// 预订任务逻辑与原逻辑保持一致 (如果需要)
|
||||
DeviceConfigManager deviceConfig = deviceRepository.getDeviceConfigManager(deviceId);
|
||||
if (deviceConfig != null &&
|
||||
Integer.valueOf(1).equals(deviceConfig.getInteger("enable_pre_book"))) {
|
||||
DynamicTaskGenerator.addTask(faceSampleId);
|
||||
}
|
||||
|
||||
} else {
|
||||
log.warn("addFace 返回 null, faceSampleId={}", faceSampleId);
|
||||
updateAiCamFaceSampleStatusSafely(faceSampleId, -1);
|
||||
}
|
||||
|
||||
} catch (Exception e) {
|
||||
log.error("AI相机人脸识别异常, faceSampleId={}", faceSampleId, e);
|
||||
updateAiCamFaceSampleStatusSafely(faceSampleId, -1);
|
||||
}
|
||||
}
|
||||
}
|
||||
@@ -1,59 +0,0 @@
|
||||
package com.ycwl.basic.integration.render.client;
|
||||
|
||||
import com.ycwl.basic.integration.common.response.CommonResponse;
|
||||
import com.ycwl.basic.integration.render.dto.config.BatchRenderWorkerConfigRequest;
|
||||
import com.ycwl.basic.integration.render.dto.config.RenderWorkerConfigV2DTO;
|
||||
import org.springframework.cloud.openfeign.FeignClient;
|
||||
import org.springframework.web.bind.annotation.*;
|
||||
|
||||
import java.util.List;
|
||||
import java.util.Map;
|
||||
|
||||
/**
|
||||
* 渲染工作器配置V2客户端
|
||||
*/
|
||||
@FeignClient(name = "zt-render-worker", contextId = "render-worker-config-v2", path = "/api/render/worker/config/v2")
|
||||
public interface RenderWorkerConfigV2Client {
|
||||
|
||||
/**
|
||||
* 获取工作器所有配置
|
||||
*/
|
||||
@GetMapping("/{workerId}")
|
||||
CommonResponse<List<RenderWorkerConfigV2DTO>> getWorkerConfigs(@PathVariable("workerId") Long workerId);
|
||||
|
||||
/**
|
||||
* 根据配置键获取特定配置
|
||||
*/
|
||||
@GetMapping("/{workerId}/key/{configKey}")
|
||||
CommonResponse<RenderWorkerConfigV2DTO> getWorkerConfigByKey(@PathVariable("workerId") Long workerId,
|
||||
@PathVariable("configKey") String configKey);
|
||||
|
||||
/**
|
||||
* 创建配置
|
||||
*/
|
||||
@PostMapping("/{workerId}")
|
||||
CommonResponse<RenderWorkerConfigV2DTO> createWorkerConfig(@PathVariable("workerId") Long workerId,
|
||||
@RequestBody RenderWorkerConfigV2DTO config);
|
||||
|
||||
/**
|
||||
* 更新配置
|
||||
*/
|
||||
@PutMapping("/{workerId}/{id}")
|
||||
CommonResponse<Void> updateWorkerConfig(@PathVariable("workerId") Long workerId,
|
||||
@PathVariable("id") Long id,
|
||||
@RequestBody Map<String, Object> updates);
|
||||
|
||||
/**
|
||||
* 删除配置
|
||||
*/
|
||||
@DeleteMapping("/{workerId}/{id}")
|
||||
CommonResponse<Void> deleteWorkerConfig(@PathVariable("workerId") Long workerId,
|
||||
@PathVariable("id") Long id);
|
||||
|
||||
/**
|
||||
* 批量更新配置
|
||||
*/
|
||||
@PostMapping("/{workerId}/batch")
|
||||
CommonResponse<Void> batchUpdateWorkerConfigs(@PathVariable("workerId") Long workerId,
|
||||
@RequestBody BatchRenderWorkerConfigRequest request);
|
||||
}
|
||||
@@ -2,45 +2,52 @@ package com.ycwl.basic.integration.render.client;
|
||||
|
||||
import com.ycwl.basic.integration.common.response.CommonResponse;
|
||||
import com.ycwl.basic.integration.common.response.PageResponse;
|
||||
import com.ycwl.basic.integration.render.dto.config.BatchRenderWorkerConfigRequest;
|
||||
import com.ycwl.basic.integration.render.dto.config.RenderWorkerConfigV2DTO;
|
||||
import com.ycwl.basic.integration.render.dto.worker.*;
|
||||
import org.springframework.cloud.openfeign.FeignClient;
|
||||
import org.springframework.web.bind.annotation.*;
|
||||
|
||||
import java.util.List;
|
||||
import java.util.Map;
|
||||
|
||||
/**
|
||||
* 渲染工作器V2客户端
|
||||
*/
|
||||
@FeignClient(name = "zt-render-worker", contextId = "render-worker-v2", path = "/api/render/worker/v2")
|
||||
@FeignClient(name = "zt-render-worker", contextId = "render-worker-v2", path = "/api/render/worker")
|
||||
public interface RenderWorkerV2Client {
|
||||
|
||||
// ==================== Worker V2 Operations ====================
|
||||
|
||||
/**
|
||||
* 获取工作器核心信息
|
||||
*/
|
||||
@GetMapping("/{id}")
|
||||
@GetMapping("v2/{id}")
|
||||
CommonResponse<RenderWorkerV2DTO> getWorker(@PathVariable("id") Long id);
|
||||
|
||||
/**
|
||||
* 创建工作器
|
||||
*/
|
||||
@PostMapping
|
||||
@PostMapping("v2")
|
||||
CommonResponse<RenderWorkerV2DTO> createWorker(@RequestBody CreateRenderWorkerRequest request);
|
||||
|
||||
/**
|
||||
* 更新工作器
|
||||
*/
|
||||
@PutMapping("/{id}")
|
||||
@PutMapping("v2/{id}")
|
||||
CommonResponse<Void> updateWorker(@PathVariable("id") Long id,
|
||||
@RequestBody UpdateRenderWorkerRequest request);
|
||||
|
||||
/**
|
||||
* 删除工作器
|
||||
*/
|
||||
@DeleteMapping("/{id}")
|
||||
@DeleteMapping("v2/{id}")
|
||||
CommonResponse<Void> deleteWorker(@PathVariable("id") Long id);
|
||||
|
||||
/**
|
||||
* 分页查询工作器列表(核心信息)
|
||||
*/
|
||||
@GetMapping
|
||||
@GetMapping("v2")
|
||||
CommonResponse<PageResponse<RenderWorkerV2DTO>> listWorkers(@RequestParam(defaultValue = "1") Integer page,
|
||||
@RequestParam(defaultValue = "10") Integer pageSize,
|
||||
@RequestParam(required = false) Integer isEnabled,
|
||||
@@ -49,6 +56,50 @@ public interface RenderWorkerV2Client {
|
||||
/**
|
||||
* 根据key获取工作器核心信息
|
||||
*/
|
||||
@GetMapping("/key/{key}")
|
||||
@GetMapping("v2/key/{key}")
|
||||
CommonResponse<RenderWorkerV2DTO> getWorkerByKey(@PathVariable("key") String key);
|
||||
|
||||
// ==================== Worker Config V2 Operations ====================
|
||||
|
||||
/**
|
||||
* 获取工作器所有配置
|
||||
*/
|
||||
@GetMapping("config/v2/{workerId}")
|
||||
CommonResponse<List<RenderWorkerConfigV2DTO>> getWorkerConfigs(@PathVariable("workerId") Long workerId);
|
||||
|
||||
/**
|
||||
* 根据配置键获取特定配置
|
||||
*/
|
||||
@GetMapping("config/v2/{workerId}/key/{configKey}")
|
||||
CommonResponse<RenderWorkerConfigV2DTO> getWorkerConfigByKey(@PathVariable("workerId") Long workerId,
|
||||
@PathVariable("configKey") String configKey);
|
||||
|
||||
/**
|
||||
* 创建配置
|
||||
*/
|
||||
@PostMapping("config/v2/{workerId}")
|
||||
CommonResponse<RenderWorkerConfigV2DTO> createWorkerConfig(@PathVariable("workerId") Long workerId,
|
||||
@RequestBody RenderWorkerConfigV2DTO config);
|
||||
|
||||
/**
|
||||
* 更新配置
|
||||
*/
|
||||
@PutMapping("config/v2/{workerId}/{id}")
|
||||
CommonResponse<Void> updateWorkerConfig(@PathVariable("workerId") Long workerId,
|
||||
@PathVariable("id") Long id,
|
||||
@RequestBody Map<String, Object> updates);
|
||||
|
||||
/**
|
||||
* 删除配置
|
||||
*/
|
||||
@DeleteMapping("config/v2/{workerId}/{id}")
|
||||
CommonResponse<Void> deleteWorkerConfig(@PathVariable("workerId") Long workerId,
|
||||
@PathVariable("id") Long id);
|
||||
|
||||
/**
|
||||
* 批量更新配置
|
||||
*/
|
||||
@PostMapping("config/v2/{workerId}/batch")
|
||||
CommonResponse<Void> batchUpdateWorkerConfigs(@PathVariable("workerId") Long workerId,
|
||||
@RequestBody BatchRenderWorkerConfigRequest request);
|
||||
}
|
||||
@@ -3,7 +3,7 @@ package com.ycwl.basic.integration.render.service;
|
||||
import com.ycwl.basic.integration.common.exception.IntegrationException;
|
||||
import com.ycwl.basic.integration.common.response.CommonResponse;
|
||||
import com.ycwl.basic.integration.common.service.IntegrationFallbackService;
|
||||
import com.ycwl.basic.integration.render.client.RenderWorkerConfigV2Client;
|
||||
import com.ycwl.basic.integration.render.client.RenderWorkerV2Client;
|
||||
import com.ycwl.basic.integration.render.dto.config.BatchRenderWorkerConfigRequest;
|
||||
import com.ycwl.basic.integration.render.dto.config.RenderWorkerConfigV2DTO;
|
||||
import lombok.RequiredArgsConstructor;
|
||||
@@ -23,7 +23,7 @@ import java.util.Map;
|
||||
@RequiredArgsConstructor
|
||||
public class RenderWorkerConfigIntegrationService {
|
||||
|
||||
private final RenderWorkerConfigV2Client renderWorkerConfigV2Client;
|
||||
private final RenderWorkerV2Client renderWorkerConfigV2Client;
|
||||
private final IntegrationFallbackService fallbackService;
|
||||
|
||||
private static final String SERVICE_NAME = "zt-render-worker";
|
||||
|
||||
@@ -1,28 +0,0 @@
|
||||
package com.ycwl.basic.integration.scenic.client;
|
||||
|
||||
import com.ycwl.basic.integration.common.response.CommonResponse;
|
||||
import com.ycwl.basic.integration.scenic.dto.config.DefaultConfigDTO;
|
||||
import org.springframework.cloud.openfeign.FeignClient;
|
||||
import org.springframework.web.bind.annotation.*;
|
||||
|
||||
import java.util.List;
|
||||
|
||||
@FeignClient(name = "zt-scenic", contextId = "scenic-default-config", path = "/api/scenic/default-config")
|
||||
public interface DefaultConfigClient {
|
||||
|
||||
@GetMapping("/")
|
||||
CommonResponse<List<DefaultConfigDTO>> listDefaultConfigs();
|
||||
|
||||
@GetMapping("/{configKey}")
|
||||
CommonResponse<DefaultConfigDTO> getDefaultConfig(@PathVariable("configKey") String configKey);
|
||||
|
||||
@PostMapping("/")
|
||||
CommonResponse<DefaultConfigDTO> createDefaultConfig(@RequestBody DefaultConfigDTO request);
|
||||
|
||||
@PutMapping("/{configKey}")
|
||||
CommonResponse<DefaultConfigDTO> updateDefaultConfig(@PathVariable("configKey") String configKey,
|
||||
@RequestBody DefaultConfigDTO request);
|
||||
|
||||
@DeleteMapping("/{configKey}")
|
||||
CommonResponse<Void> deleteDefaultConfig(@PathVariable("configKey") String configKey);
|
||||
}
|
||||
@@ -1,37 +0,0 @@
|
||||
package com.ycwl.basic.integration.scenic.client;
|
||||
|
||||
import com.ycwl.basic.integration.common.response.CommonResponse;
|
||||
import com.ycwl.basic.integration.scenic.dto.config.*;
|
||||
import org.springframework.cloud.openfeign.FeignClient;
|
||||
import org.springframework.web.bind.annotation.*;
|
||||
|
||||
import java.util.List;
|
||||
import java.util.Map;
|
||||
|
||||
@FeignClient(name = "zt-scenic", contextId = "scenic-config-v2", path = "/api/scenic/config/v2")
|
||||
public interface ScenicConfigV2Client {
|
||||
|
||||
@GetMapping("/{scenicId}")
|
||||
CommonResponse<List<ScenicConfigV2DTO>> listConfigs(@PathVariable("scenicId") Long scenicId);
|
||||
|
||||
@GetMapping("/{scenicId}/key/{configKey}")
|
||||
CommonResponse<ScenicConfigV2DTO> getConfigByKey(@PathVariable("scenicId") Long scenicId,
|
||||
@PathVariable("configKey") String configKey);
|
||||
|
||||
@PostMapping("/{scenicId}")
|
||||
CommonResponse<ScenicConfigV2DTO> createConfig(@PathVariable("scenicId") Long scenicId,
|
||||
@RequestBody CreateConfigRequest request);
|
||||
|
||||
@PutMapping("/{scenicId}/{id}")
|
||||
CommonResponse<ScenicConfigV2DTO> updateConfig(@PathVariable("scenicId") Long scenicId,
|
||||
@PathVariable("id") String id,
|
||||
@RequestBody UpdateConfigRequest request);
|
||||
|
||||
@DeleteMapping("/{scenicId}/{id}")
|
||||
CommonResponse<Void> deleteConfig(@PathVariable("scenicId") Long scenicId,
|
||||
@PathVariable("id") String id);
|
||||
|
||||
@PostMapping("/{scenicId}/batch")
|
||||
CommonResponse<BatchUpdateResponse> batchUpdateConfigs(@PathVariable("scenicId") Long scenicId,
|
||||
@RequestBody BatchConfigRequest request);
|
||||
}
|
||||
@@ -1,39 +1,88 @@
|
||||
package com.ycwl.basic.integration.scenic.client;
|
||||
|
||||
import com.ycwl.basic.integration.common.response.CommonResponse;
|
||||
import com.ycwl.basic.integration.common.response.PageResponse;
|
||||
import com.ycwl.basic.integration.scenic.dto.config.*;
|
||||
import com.ycwl.basic.integration.scenic.dto.filter.ScenicFilterPageResponse;
|
||||
import com.ycwl.basic.integration.scenic.dto.filter.ScenicFilterRequest;
|
||||
import com.ycwl.basic.integration.scenic.dto.scenic.CreateScenicRequest;
|
||||
import com.ycwl.basic.integration.scenic.dto.scenic.ScenicV2DTO;
|
||||
import com.ycwl.basic.integration.common.response.PageResponse;
|
||||
import com.ycwl.basic.integration.scenic.dto.scenic.UpdateScenicRequest;
|
||||
import org.springframework.cloud.openfeign.FeignClient;
|
||||
import org.springframework.web.bind.annotation.*;
|
||||
|
||||
import java.util.List;
|
||||
import java.util.Map;
|
||||
|
||||
@FeignClient(name = "zt-scenic", contextId = "scenic-v2", path = "/api/scenic/v2")
|
||||
@FeignClient(name = "zt-scenic", contextId = "scenic-v2", path = "/api/scenic")
|
||||
public interface ScenicV2Client {
|
||||
|
||||
@GetMapping("/{scenicId}")
|
||||
// ==================== Scenic V2 Operations ====================
|
||||
|
||||
@GetMapping("v2/{scenicId}")
|
||||
CommonResponse<ScenicV2DTO> getScenic(@PathVariable("scenicId") Long scenicId);
|
||||
|
||||
@PostMapping("/")
|
||||
@PostMapping("v2/")
|
||||
CommonResponse<ScenicV2DTO> createScenic(@RequestBody CreateScenicRequest request);
|
||||
|
||||
@PutMapping("/{scenicId}")
|
||||
@PutMapping("v2/{scenicId}")
|
||||
CommonResponse<ScenicV2DTO> updateScenic(@PathVariable("scenicId") Long scenicId,
|
||||
@RequestBody UpdateScenicRequest request);
|
||||
|
||||
@DeleteMapping("/{scenicId}")
|
||||
@DeleteMapping("v2/{scenicId}")
|
||||
CommonResponse<Void> deleteScenic(@PathVariable("scenicId") Long scenicId);
|
||||
|
||||
@PostMapping("/filter")
|
||||
@PostMapping("v2/filter")
|
||||
CommonResponse<ScenicFilterPageResponse> filterScenics(@RequestBody ScenicFilterRequest request);
|
||||
|
||||
@GetMapping("/")
|
||||
@GetMapping("v2/")
|
||||
CommonResponse<PageResponse<ScenicV2DTO>> listScenics(@RequestParam(defaultValue = "1") Integer page,
|
||||
@RequestParam(defaultValue = "10") Integer pageSize,
|
||||
@RequestParam(required = false) Integer status,
|
||||
@RequestParam(required = false) String name);
|
||||
@RequestParam(required = false) String name,
|
||||
@RequestParam(required = false) String scenicIds);
|
||||
|
||||
// ==================== Scenic Config V2 Operations ====================
|
||||
|
||||
@GetMapping("config/v2/{scenicId}")
|
||||
CommonResponse<List<ScenicConfigV2DTO>> listConfigs(@PathVariable("scenicId") Long scenicId);
|
||||
|
||||
@GetMapping("config/v2/{scenicId}/key/{configKey}")
|
||||
CommonResponse<ScenicConfigV2DTO> getConfigByKey(@PathVariable("scenicId") Long scenicId,
|
||||
@PathVariable("configKey") String configKey);
|
||||
|
||||
@PostMapping("config/v2/{scenicId}")
|
||||
CommonResponse<ScenicConfigV2DTO> createConfig(@PathVariable("scenicId") Long scenicId,
|
||||
@RequestBody CreateConfigRequest request);
|
||||
|
||||
@PutMapping("config/v2/{scenicId}/{id}")
|
||||
CommonResponse<ScenicConfigV2DTO> updateConfig(@PathVariable("scenicId") Long scenicId,
|
||||
@PathVariable("id") String id,
|
||||
@RequestBody UpdateConfigRequest request);
|
||||
|
||||
@DeleteMapping("config/v2/{scenicId}/{id}")
|
||||
CommonResponse<Void> deleteConfig(@PathVariable("scenicId") Long scenicId,
|
||||
@PathVariable("id") String id);
|
||||
|
||||
@PostMapping("config/v2/{scenicId}/batch")
|
||||
CommonResponse<BatchUpdateResponse> batchUpdateConfigs(@PathVariable("scenicId") Long scenicId,
|
||||
@RequestBody BatchConfigRequest request);
|
||||
|
||||
// ==================== Default Config Operations ====================
|
||||
|
||||
@GetMapping("default-config/")
|
||||
CommonResponse<List<DefaultConfigDTO>> listDefaultConfigs();
|
||||
|
||||
@GetMapping("default-config/{configKey}")
|
||||
CommonResponse<DefaultConfigDTO> getDefaultConfig(@PathVariable("configKey") String configKey);
|
||||
|
||||
@PostMapping("default-config/")
|
||||
CommonResponse<DefaultConfigDTO> createDefaultConfig(@RequestBody DefaultConfigDTO request);
|
||||
|
||||
@PutMapping("default-config/{configKey}")
|
||||
CommonResponse<DefaultConfigDTO> updateDefaultConfig(@PathVariable("configKey") String configKey,
|
||||
@RequestBody DefaultConfigDTO request);
|
||||
|
||||
@DeleteMapping("default-config/{configKey}")
|
||||
CommonResponse<Void> deleteDefaultConfig(@PathVariable("configKey") String configKey);
|
||||
}
|
||||
@@ -3,7 +3,7 @@ package com.ycwl.basic.integration.scenic.service;
|
||||
import com.ycwl.basic.integration.common.exception.IntegrationException;
|
||||
import com.ycwl.basic.integration.common.response.CommonResponse;
|
||||
import com.ycwl.basic.integration.common.service.IntegrationFallbackService;
|
||||
import com.ycwl.basic.integration.scenic.client.ScenicConfigV2Client;
|
||||
import com.ycwl.basic.integration.scenic.client.ScenicV2Client;
|
||||
import com.ycwl.basic.integration.scenic.dto.config.*;
|
||||
import lombok.RequiredArgsConstructor;
|
||||
import lombok.extern.slf4j.Slf4j;
|
||||
@@ -17,7 +17,7 @@ import java.util.Map;
|
||||
@RequiredArgsConstructor
|
||||
public class ScenicConfigIntegrationService {
|
||||
|
||||
private final ScenicConfigV2Client scenicConfigV2Client;
|
||||
private final ScenicV2Client scenicConfigV2Client;
|
||||
private final IntegrationFallbackService fallbackService;
|
||||
|
||||
private static final String SERVICE_NAME = "zt-scenic";
|
||||
|
||||
@@ -2,7 +2,7 @@ package com.ycwl.basic.integration.scenic.service;
|
||||
|
||||
import com.ycwl.basic.integration.common.exception.IntegrationException;
|
||||
import com.ycwl.basic.integration.common.response.CommonResponse;
|
||||
import com.ycwl.basic.integration.scenic.client.DefaultConfigClient;
|
||||
import com.ycwl.basic.integration.scenic.client.ScenicV2Client;
|
||||
import com.ycwl.basic.integration.scenic.dto.config.DefaultConfigDTO;
|
||||
import lombok.RequiredArgsConstructor;
|
||||
import lombok.extern.slf4j.Slf4j;
|
||||
@@ -15,7 +15,7 @@ import java.util.List;
|
||||
@RequiredArgsConstructor
|
||||
public class ScenicDefaultConfigIntegrationService {
|
||||
|
||||
private final DefaultConfigClient defaultConfigClient;
|
||||
private final ScenicV2Client defaultConfigClient;
|
||||
|
||||
public List<DefaultConfigDTO> listDefaultConfigs() {
|
||||
log.debug("获取默认配置列表");
|
||||
|
||||
@@ -3,7 +3,6 @@ package com.ycwl.basic.integration.scenic.service;
|
||||
import com.ycwl.basic.integration.common.exception.IntegrationException;
|
||||
import com.ycwl.basic.integration.common.response.CommonResponse;
|
||||
import com.ycwl.basic.integration.common.service.IntegrationFallbackService;
|
||||
import com.ycwl.basic.integration.scenic.client.ScenicConfigV2Client;
|
||||
import com.ycwl.basic.integration.scenic.client.ScenicV2Client;
|
||||
import com.ycwl.basic.integration.scenic.dto.filter.ScenicFilterPageResponse;
|
||||
import com.ycwl.basic.integration.scenic.dto.filter.ScenicFilterRequest;
|
||||
@@ -23,7 +22,6 @@ import java.util.Map;
|
||||
public class ScenicIntegrationService {
|
||||
|
||||
private final ScenicV2Client scenicV2Client;
|
||||
private final ScenicConfigV2Client scenicConfigV2Client;
|
||||
private final IntegrationFallbackService fallbackService;
|
||||
|
||||
private static final String SERVICE_NAME = "zt-scenic";
|
||||
@@ -65,9 +63,10 @@ public class ScenicIntegrationService {
|
||||
return handleResponse(response, "筛选景区失败");
|
||||
}
|
||||
|
||||
public PageResponse<ScenicV2DTO> listScenics(Integer page, Integer pageSize, Integer status, String name) {
|
||||
log.debug("分页查询景区列表, page: {}, pageSize: {}, status: {}, name: {}", page, pageSize, status, name);
|
||||
CommonResponse<PageResponse<ScenicV2DTO>> response = scenicV2Client.listScenics(page, pageSize, status, name);
|
||||
public PageResponse<ScenicV2DTO> listScenics(Integer page, Integer pageSize, Integer status, String name, String scenicIds) {
|
||||
log.debug("分页查询景区列表, page: {}, pageSize: {}, status: {}, name: {}, scenicIds: {}",
|
||||
page, pageSize, status, name, scenicIds);
|
||||
CommonResponse<PageResponse<ScenicV2DTO>> response = scenicV2Client.listScenics(page, pageSize, status, name, scenicIds);
|
||||
return handleResponse(response, "分页查询景区列表失败");
|
||||
}
|
||||
|
||||
|
||||
@@ -19,6 +19,7 @@ import lombok.extern.slf4j.Slf4j;
|
||||
import org.apache.commons.lang3.StringUtils;
|
||||
import org.apache.commons.lang3.Strings;
|
||||
import org.springframework.beans.factory.annotation.Autowired;
|
||||
import org.springframework.context.annotation.Lazy;
|
||||
import org.springframework.data.redis.core.RedisTemplate;
|
||||
import org.springframework.stereotype.Component;
|
||||
import org.springframework.web.method.HandlerMethod;
|
||||
@@ -41,10 +42,13 @@ import static com.ycwl.basic.constant.JwtRoleConstant.MERCHANT;
|
||||
@Component
|
||||
public class AuthInterceptor implements HandlerInterceptor {
|
||||
|
||||
@Lazy
|
||||
@Autowired
|
||||
RedisTemplate redisTemplate;
|
||||
@Lazy
|
||||
@Autowired
|
||||
private ScenicAccountMapper scenicAccountMapper;
|
||||
@Lazy
|
||||
@Autowired
|
||||
private AdminUserMapper adminUserMapper;
|
||||
|
||||
|
||||
@@ -0,0 +1,16 @@
|
||||
package com.ycwl.basic.mapper;
|
||||
|
||||
import com.ycwl.basic.model.mobile.chat.entity.FaceChatConversationEntity;
|
||||
import org.apache.ibatis.annotations.Mapper;
|
||||
import org.apache.ibatis.annotations.Param;
|
||||
|
||||
@Mapper
|
||||
public interface FaceChatConversationMapper {
|
||||
FaceChatConversationEntity findByFaceId(@Param("faceId") Long faceId);
|
||||
|
||||
FaceChatConversationEntity getById(@Param("id") Long id);
|
||||
|
||||
int insert(FaceChatConversationEntity entity);
|
||||
|
||||
int updateStatus(@Param("id") Long id, @Param("status") String status);
|
||||
}
|
||||
Some files were not shown because too many files have changed in this diff Show More
Reference in New Issue
Block a user