|
|
|
@ -2,6 +2,8 @@ package com.supervision.manage.service.impl;
|
|
|
|
|
|
|
|
|
|
import cn.hutool.core.codec.Base64;
|
|
|
|
|
import cn.hutool.core.io.FileUtil;
|
|
|
|
|
import cn.hutool.core.lang.Assert;
|
|
|
|
|
import cn.hutool.core.thread.ThreadUtil;
|
|
|
|
|
import cn.hutool.core.util.StrUtil;
|
|
|
|
|
import cn.hutool.http.HttpUtil;
|
|
|
|
|
import cn.hutool.json.JSONUtil;
|
|
|
|
@ -11,7 +13,10 @@ import com.supervision.manage.service.FileManageService;
|
|
|
|
|
import com.supervision.manage.service.HumanManageService;
|
|
|
|
|
import com.supervision.model.FileResource;
|
|
|
|
|
import com.supervision.model.Human;
|
|
|
|
|
import com.supervision.model.MedicalRec;
|
|
|
|
|
import com.supervision.service.HumanService;
|
|
|
|
|
import com.supervision.service.MedicalRecService;
|
|
|
|
|
import com.supervision.vo.manage.HumanReqVo;
|
|
|
|
|
import lombok.RequiredArgsConstructor;
|
|
|
|
|
import lombok.extern.slf4j.Slf4j;
|
|
|
|
|
import org.springframework.beans.factory.annotation.Value;
|
|
|
|
@ -19,13 +24,26 @@ import org.springframework.stereotype.Service;
|
|
|
|
|
import org.springframework.transaction.annotation.Transactional;
|
|
|
|
|
|
|
|
|
|
import java.io.File;
|
|
|
|
|
import java.util.List;
|
|
|
|
|
import java.util.Map;
|
|
|
|
|
import java.util.concurrent.*;
|
|
|
|
|
|
|
|
|
|
@Slf4j
|
|
|
|
|
@Service
|
|
|
|
|
@RequiredArgsConstructor
|
|
|
|
|
public class HumanManageServiceImpl implements HumanManageService {
|
|
|
|
|
|
|
|
|
|
/**
|
|
|
|
|
* 创建一个线程池,本意是用来异步调用数字人使用的
|
|
|
|
|
* 核心线程数:2
|
|
|
|
|
* 最大线程数:5
|
|
|
|
|
* 队列长度:20
|
|
|
|
|
*/
|
|
|
|
|
ThreadPoolExecutor humanExecutor = new ThreadPoolExecutor(2, 5,
|
|
|
|
|
60L, TimeUnit.MILLISECONDS,
|
|
|
|
|
new LinkedBlockingQueue<>(20), ThreadUtil.newNamedThreadFactory("human", true),
|
|
|
|
|
new ThreadPoolExecutor.CallerRunsPolicy());
|
|
|
|
|
|
|
|
|
|
private final HumanService humanService;
|
|
|
|
|
private final FileManageService fileManageService;
|
|
|
|
|
@Value("${humanGenerate.baseUrl}")
|
|
|
|
@ -34,6 +52,7 @@ public class HumanManageServiceImpl implements HumanManageService {
|
|
|
|
|
private String silent;
|
|
|
|
|
@Value("${humanGenerate.dynamic}")
|
|
|
|
|
private String dynamic;
|
|
|
|
|
private final MedicalRecService medicalRecService;
|
|
|
|
|
|
|
|
|
|
@Override
|
|
|
|
|
@Transactional(rollbackFor = Exception.class)
|
|
|
|
@ -49,6 +68,78 @@ public class HumanManageServiceImpl implements HumanManageService {
|
|
|
|
|
humanService.updateById(human);
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
@Override
|
|
|
|
|
public List<Human> queryHumanList() {
|
|
|
|
|
return humanService.lambdaQuery().orderBy(true,false,Human::getCreateTime).list();
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
@Override
|
|
|
|
|
public boolean createHuman(HumanReqVo humanReqVo) {
|
|
|
|
|
|
|
|
|
|
Assert.notEmpty(humanReqVo.getImageFileId(), "图片不能为空");
|
|
|
|
|
Human human = new Human();
|
|
|
|
|
human.setImageFileId(humanReqVo.getImageFileId());
|
|
|
|
|
human.setDescription(humanReqVo.getDescription());
|
|
|
|
|
humanService.save(human);
|
|
|
|
|
|
|
|
|
|
// 异步调用生成数字人
|
|
|
|
|
humanExecutor.submit(() -> {
|
|
|
|
|
try {
|
|
|
|
|
generateHuman(human.getId());
|
|
|
|
|
} catch (Exception e) {
|
|
|
|
|
log.error("createHuman:生成数字人异常", e);
|
|
|
|
|
throw new BusinessException("生成数字人异常");
|
|
|
|
|
}
|
|
|
|
|
});
|
|
|
|
|
|
|
|
|
|
return true;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
@Override
|
|
|
|
|
public boolean updateHuman(HumanReqVo human) {
|
|
|
|
|
Assert.notEmpty(human.getId(), "数字人id不能为空");
|
|
|
|
|
Assert.notEmpty(human.getImageFileId(), "图片不能为空");
|
|
|
|
|
|
|
|
|
|
Human dbHuman = humanService.getById(human.getId());
|
|
|
|
|
Assert.notEmpty(dbHuman.getId(), "数字人不存在");
|
|
|
|
|
if (StrUtil.equals(dbHuman.getImageFileId(), human.getImageFileId())){
|
|
|
|
|
// 只更新描述信息
|
|
|
|
|
humanService.lambdaUpdate().eq(Human::getId, human.getId())
|
|
|
|
|
.set(Human::getDescription, human.getDescription()).update();
|
|
|
|
|
return true;
|
|
|
|
|
}
|
|
|
|
|
// 更新数字人状态
|
|
|
|
|
dbHuman.setImageFileId(human.getImageFileId());
|
|
|
|
|
dbHuman.setSilentTaskUid(null);
|
|
|
|
|
dbHuman.setDynamicTaskUid(null);
|
|
|
|
|
dbHuman.setDynamicVideoFileId(null);
|
|
|
|
|
dbHuman.setSilentVideoFileId(null);
|
|
|
|
|
dbHuman.setStatus(2);
|
|
|
|
|
dbHuman.setDescription(human.getDescription());
|
|
|
|
|
humanService.updateById(dbHuman);
|
|
|
|
|
// 异步调用生成数字人
|
|
|
|
|
humanExecutor.submit(() -> {
|
|
|
|
|
try {
|
|
|
|
|
generateHuman(human.getId());
|
|
|
|
|
} catch (Exception e) {
|
|
|
|
|
log.error("createHuman:生成数字人异常", e);
|
|
|
|
|
throw new BusinessException("生成数字人异常");
|
|
|
|
|
}
|
|
|
|
|
});
|
|
|
|
|
|
|
|
|
|
return true;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
@Override
|
|
|
|
|
public boolean deleteHuman(String id) {
|
|
|
|
|
Assert.notEmpty(id, "数字人id不能为空");
|
|
|
|
|
Long count = medicalRecService.lambdaQuery().eq(MedicalRec::getHumanId, id).count();
|
|
|
|
|
Assert.isTrue(count == 0, "该数字已被使用,不能删除");
|
|
|
|
|
return humanService.removeById(id);
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
private void generateSilent(Human human, File imageFile) throws Exception {
|
|
|
|
|
if (StrUtil.isAllBlank(human.getSilentVideoFileId(), human.getSilentTaskUid())) {
|
|
|
|
|
HumanGenerateDTO silentDTO;
|
|
|
|
@ -74,7 +165,7 @@ public class HumanManageServiceImpl implements HumanManageService {
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
private void generateDynamic(Human human, File imageFile) throws Exception {
|
|
|
|
|
if (StrUtil.isAllNotBlank(human.getDynamicVideoFileId(), human.getDynamicTaskUid())) {
|
|
|
|
|
if (StrUtil.isAllBlank(human.getDynamicVideoFileId(), human.getDynamicTaskUid())) {
|
|
|
|
|
HumanGenerateDTO dynamicDTO;
|
|
|
|
|
try {
|
|
|
|
|
String post = HttpUtil.post(humanBaseUrl + dynamic, Map.of("image", imageFile));
|
|
|
|
|