diff --git a/src/main/java/com/ai/da/common/utils/MinioUtil.java b/src/main/java/com/ai/da/common/utils/MinioUtil.java index bf5bea69..f8b86a51 100644 --- a/src/main/java/com/ai/da/common/utils/MinioUtil.java +++ b/src/main/java/com/ai/da/common/utils/MinioUtil.java @@ -484,6 +484,57 @@ public class MinioUtil { return null; // or throw an exception } } + + /** + * 从 MinIO 下载对象到本地路径 + * + * @param bucketName 存储桶名称 + * @param objectName MinIO 上对象的名称 + * @param localFilePath 本地文件路径 + */ + public void downloadMinioObjectToLocal(String bucketName, String objectName, String localFilePath) { + File localFile = new File(localFilePath); + File parentDir = localFile.getParentFile(); + if (parentDir != null) { + parentDir.mkdirs(); // 创建文件夹,确保路径结构与 MinIO 一致 + } + + try (InputStream stream = minioClient.getObject( + GetObjectArgs.builder().bucket(bucketName).object(objectName).build()); + FileOutputStream out = new FileOutputStream(localFile)) { + + byte[] buffer = new byte[1024]; + int bytesRead; + while ((bytesRead = stream.read(buffer)) != -1) { + out.write(buffer, 0, bytesRead); + } + log.info("Downloaded object {} to {}", objectName, localFilePath); + } catch (Exception e) { + log.error("Error while downloading object {}: {}", objectName, e.getMessage()); + } + } + + /** + * 从路径中提取存储桶名称 + * + * @param path MinIO 路径 + * @return 存储桶名称 + */ + public String getBucketNameFromPath(String path) { + int index = path.indexOf("/"); + return path.substring(0, index); // 获取第一级路径作为 bucket 名称 + } + + /** + * 从路径中提取对象名称 + * + * @param path MinIO 路径 + * @return 对象名称 + */ + public String getObjectNameFromPath(String path) { + int index = path.indexOf("/"); + return path.substring(index + 1); // 获取路径的其余部分作为对象名称 + } } diff --git a/src/main/java/com/ai/da/common/utils/SendEmailUtil.java b/src/main/java/com/ai/da/common/utils/SendEmailUtil.java index a89115f8..25e07339 100644 --- a/src/main/java/com/ai/da/common/utils/SendEmailUtil.java +++ b/src/main/java/com/ai/da/common/utils/SendEmailUtil.java @@ -389,20 +389,20 @@ public class SendEmailUtil { // 根据邮件类型设置不同的主题和模板 String subject = ""; Template template = new Template(); - if (type == 1) { - subject = "Upcoming System Upgrade for AiDA 3.0"; - template.setTemplateID(UPGRADE_NOTIFICATION_ID); - }else { - subject = "即将到来的AiDA 3.0系统升级"; - template.setTemplateID(UPGRADE_NOTIFICATION_ID_CHINESE); - } // if (type == 1) { -// subject = "Successful System Upgrade and New Features in AiDA 3.1"; -// template.setTemplateID(UPGRADE_SUCCESS_NOTIFICATION_ID); +// subject = "Upcoming System Upgrade for AiDA 3.0"; +// template.setTemplateID(UPGRADE_NOTIFICATION_ID); // }else { -// subject = "系统升级成功和AiDA 3.1新功能"; -// template.setTemplateID(UPGRADE_SUCCESS_NOTIFICATION_ID_CHINESE); +// subject = "即将到来的AiDA 3.0系统升级"; +// template.setTemplateID(UPGRADE_NOTIFICATION_ID_CHINESE); // } + if (type == 1) { + subject = "Successful System Upgrade and New Features in AiDA 3.1"; + template.setTemplateID(UPGRADE_SUCCESS_NOTIFICATION_ID); + }else { + subject = "系统升级成功和AiDA 3.1新功能"; + template.setTemplateID(UPGRADE_SUCCESS_NOTIFICATION_ID_CHINESE); + } template.setTemplateData(buildAccountData(account)); req.setSubject(subject); @@ -759,6 +759,47 @@ public class SendEmailUtil { req.setSubject(subject); req.setTemplate(template); + // 发送邮件 + SendEmailResponse resp = client.SendEmail(req); + log.info("短信发送结果res###{}", SendEmailResponse.toJsonString(resp)); + } catch (TencentCloudSDKException e) { + log.info("邮件发送失败###{}", e.toString()); + throw new BusinessException("failed.to.send.mail"); + } + } + private final static Long SYSTEM_UPGRADE_CN_ID = 131743L; + private final static Long SYSTEM_UPGRADE_EN_ID = 131744L; + public static void temporaryUpgrade(Account account, String senderAddress, int type) { + try { + // 实例化一个认证对象 + Credential cred = new Credential(SECRET_ID, SECRET_KEy); + HttpProfile httpProfile = new HttpProfile(); + httpProfile.setEndpoint("ses.tencentcloudapi.com"); + ClientProfile clientProfile = new ClientProfile(); + clientProfile.setHttpProfile(httpProfile); + SesClient client = new SesClient(cred, "ap-hongkong", clientProfile); + SendEmailRequest req = new SendEmailRequest(); + if (StringUtils.isEmpty(senderAddress)) { + senderAddress = CODE_CREATE_SEND_ADDRESS; + } + req.setFromEmailAddress(senderAddress); + req.setDestination(new String[]{account.getUserEmail()}); + + // 根据邮件类型设置不同的主题和模板 + String subject = ""; + Template template = new Template(); + if (type == 1) { + subject = "AiDA system upgrade completed"; + template.setTemplateID(SYSTEM_UPGRADE_EN_ID); + }else { + subject = "AiDA系统升级完成"; + template.setTemplateID(SYSTEM_UPGRADE_CN_ID); + } + template.setTemplateData(buildAccountData(account)); + + req.setSubject(subject); + req.setTemplate(template); + // 发送邮件 SendEmailResponse resp = client.SendEmail(req); log.info("短信发送结果res###{}", SendEmailResponse.toJsonString(resp)); diff --git a/src/main/java/com/ai/da/controller/AccountController.java b/src/main/java/com/ai/da/controller/AccountController.java index c73fcdaa..ebf9b8a8 100644 --- a/src/main/java/com/ai/da/controller/AccountController.java +++ b/src/main/java/com/ai/da/controller/AccountController.java @@ -261,5 +261,12 @@ public class AccountController { return Response.success(true); } + @PostMapping("temporaryUpgrade") + @ApiOperation(value = "临时升级") + public Response temporaryUpgrade() { + accountService.temporaryUpgrade(); + return Response.success(true); + } + } diff --git a/src/main/java/com/ai/da/controller/SavedCollectionController.java b/src/main/java/com/ai/da/controller/SavedCollectionController.java index 29cbabac..1967b5c5 100644 --- a/src/main/java/com/ai/da/controller/SavedCollectionController.java +++ b/src/main/java/com/ai/da/controller/SavedCollectionController.java @@ -245,4 +245,10 @@ public class SavedCollectionController { public Response likeHistoryRelSketch() { return Response.success(userLikeGroupService.likeHistoryRelSketch()); } + + @ApiOperation(value = "download") + @PostMapping("/download") + public Response download() { + return Response.success(userLikeGroupService.download()); + } } diff --git a/src/main/java/com/ai/da/service/AccountService.java b/src/main/java/com/ai/da/service/AccountService.java index bd9c99b4..ab6320d0 100644 --- a/src/main/java/com/ai/da/service/AccountService.java +++ b/src/main/java/com/ai/da/service/AccountService.java @@ -186,4 +186,6 @@ public interface AccountService extends IService { String googleCallback(String code, HttpSession session); List getPaidCustomerEmail(); + + void temporaryUpgrade(); } diff --git a/src/main/java/com/ai/da/service/UserLikeGroupService.java b/src/main/java/com/ai/da/service/UserLikeGroupService.java index 71edbc50..918d29f5 100644 --- a/src/main/java/com/ai/da/service/UserLikeGroupService.java +++ b/src/main/java/com/ai/da/service/UserLikeGroupService.java @@ -62,4 +62,6 @@ public interface UserLikeGroupService extends IService { List getRelightResult(List taskIdList); String likeHistoryRelSketch(); + + String download(); } diff --git a/src/main/java/com/ai/da/service/impl/AccountServiceImpl.java b/src/main/java/com/ai/da/service/impl/AccountServiceImpl.java index c6e22d46..1a07fb08 100644 --- a/src/main/java/com/ai/da/service/impl/AccountServiceImpl.java +++ b/src/main/java/com/ai/da/service/impl/AccountServiceImpl.java @@ -1961,4 +1961,30 @@ public class AccountServiceImpl extends ServiceImpl impl // return userRepository.save(newUser); // } + @Override + public void temporaryUpgrade() { + QueryWrapper queryWrapper = new QueryWrapper<>(); + queryWrapper.lambda().ne(Account::getSystemUser, 0); + queryWrapper.lambda().isNotNull(Account::getUserEmail); +// queryWrapper.lambda().eq(Account::getId, 6); + List accountList = accountMapper.selectList(queryWrapper); + int i = 0; + for (Account account : accountList) { + if (i >= 0) { + try { + // 判断用户语言,调用相应邮件发送方法 + if (Language.CHINESE_SIMPLIFIED.name().equals(account.getLanguage())) { + SendEmailUtil.temporaryUpgrade(account, null, 0); + } else { + SendEmailUtil.temporaryUpgrade(account, null, 1); + } + } catch (Exception e) { + // 捕获单个用户的发送失败异常,记录日志但不中断流程 + log.error("邮件发送失败,用户邮箱:{},原因:{}", account.getUserEmail(), e.getMessage(), e); + } + } + i ++; + } + } + } diff --git a/src/main/java/com/ai/da/service/impl/UserLikeGroupServiceImpl.java b/src/main/java/com/ai/da/service/impl/UserLikeGroupServiceImpl.java index 2c51cfa6..492d58d5 100644 --- a/src/main/java/com/ai/da/service/impl/UserLikeGroupServiceImpl.java +++ b/src/main/java/com/ai/da/service/impl/UserLikeGroupServiceImpl.java @@ -15,8 +15,10 @@ import com.ai.da.model.dto.ToProductImageDTO; import com.ai.da.model.vo.*; import com.ai.da.python.PythonService; import com.ai.da.service.*; +import com.alibaba.fastjson.JSON; import com.alibaba.fastjson.JSONArray; import com.alibaba.fastjson.JSONObject; +import com.alibaba.fastjson.serializer.SerializerFeature; import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper; import com.baomidou.mybatisplus.core.conditions.update.UpdateWrapper; import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl; @@ -30,13 +32,14 @@ import org.springframework.util.StringUtils; import org.springframework.web.multipart.MultipartFile; import javax.annotation.Resource; -import java.io.BufferedReader; -import java.io.InputStream; -import java.io.InputStreamReader; +import java.io.*; +import java.nio.file.Paths; import java.time.LocalDateTime; import java.util.*; import java.util.stream.Collectors; +import static cn.hutool.poi.excel.sax.AttributeName.s; + /** * 服务实现类 * @@ -258,25 +261,47 @@ public class UserLikeGroupServiceImpl extends ServiceImpl designItemDetailQueryWrapper = new QueryWrapper<>(); + designItemDetailQueryWrapper.lambda().eq(DesignItemDetail::getDesignItemId, designItemId); + designItemDetailQueryWrapper.lambda().ne(DesignItemDetail::getType, "Body"); + List designItemDetails = designItemDetailMapper.selectList(designItemDetailQueryWrapper); + String collect = designItemDetails.stream().map(DesignItemDetail::getType).collect(Collectors.joining(",")); + Long designId = tDesignPythonOutfit.getDesignId(); Design design = designMapper.selectById(designId); String productType = "overall"; if (design.getSingleOverall().equals("single")) { productType = "single"; + sb.append(collect); + }else { + if (collect.contains("Tops")) { + sb.append("a handsome man"); + }else { + sb.append("a beautiful women"); + } + sb.append("wearing").append(collect); + } + if (StringUtils.isEmpty(prompt)) { + sb.append(",8K realistic,HDR"); + }else { + sb.append(",").append(prompt).append(",8K realistic,HDR"); } // 走模型 - pythonService.toProductImage(tDesignPythonOutfit.getDesignUrl(), taskId, s, toProductImageDTO.getImageStrength(), productType); + pythonService.toProductImage(tDesignPythonOutfit.getDesignUrl(), taskId, sb.toString(), toProductImageDTO.getImageStrength(), productType); ToProductImageResult toProductImageResult = new ToProductImageResult(); toProductImageResult.setElementId(tDesignPythonOutfit.getId()); toProductImageResult.setElementType("DesignOutfit"); @@ -290,10 +315,16 @@ public class UserLikeGroupServiceImpl extends ServiceImpl qw = new QueryWrapper<>(); List userLikeGroups = userLikeGroupMapper.selectList(qw); - Map>>> result = new HashMap<>(); + List>> result = new ArrayList<>(); + for (UserLikeGroup userLikeGroup : userLikeGroups) { Long accountId = userLikeGroup.getAccountId(); Long collectionId = userLikeGroup.getCollectionId(); - QueryWrapper collectionElementQueryWrapper = new QueryWrapper<>(); - collectionElementQueryWrapper.lambda().eq(CollectionElement::getCollectionId, collectionId); - collectionElementQueryWrapper.lambda().eq(CollectionElement::getLevel1Type, "Sketchboard"); - List collectionElements = collectionElementMapper.selectList(collectionElementQueryWrapper); - List urlList = collectionElements.stream().map(CollectionElement::getUrl).collect(Collectors.toList()); + // 提前转换为Set以提高contains的效率 + List collectionElements = getCollectionElementsByCollectionId(collectionId); + Set urlSet = collectionElements.stream() + .map(CollectionElement::getUrl) + .collect(Collectors.toSet()); - QueryWrapper userLikeQueryWrapper = new QueryWrapper<>(); - userLikeQueryWrapper.lambda().eq(UserLike::getUserLikeGroupId, userLikeGroup.getId()); - List userLikes = userLikeMapper.selectList(userLikeQueryWrapper); - - List> list = new ArrayList<>(); + List userLikes = getUserLikesByGroupId(userLikeGroup.getId()); for (UserLike userLike : userLikes) { - QueryWrapper designItemDetailQueryWrapper = new QueryWrapper<>(); - designItemDetailQueryWrapper.lambda().eq(DesignItemDetail::getDesignItemId, userLike.getDesignItemId()); - designItemDetailQueryWrapper.lambda().ne(DesignItemDetail::getType, "Body"); - List designItemDetails = designItemDetailMapper.selectList(designItemDetailQueryWrapper); - Map sketch = new HashMap<>(); + List> userLikeSketchList = new ArrayList<>(); + List designItemDetails = getDesignItemDetails(userLike.getDesignItemId()); + for (DesignItemDetail designItemDetail : designItemDetails) { + Map sketch = new HashMap<>(); String path = designItemDetail.getPath(); + + // 下载路径到本地 +// if (!StringUtils.isEmpty(path)) { +// String bucketName = minioUtil.getBucketNameFromPath(path); +// String objectName = minioUtil.getObjectNameFromPath(path); +// String localBasePath = "C:\\workspace\\fileData\\minio"; +// String localFilePath = Paths.get(localBasePath, path).toString(); +// +// // 检查文件是否已经存在,避免重复下载 +// File localFile = new File(localFilePath); +// if (!localFile.exists()) { +// minioUtil.downloadMinioObjectToLocal(bucketName, objectName, localFilePath); +// } else { +// log.info("File already exists, skipping download: {}", localFilePath); +// } +// } + + Long designId = designItemDetail.getDesignId(); + Design design = designMapper.selectById(designId); + if (null == design) { + continue; + } + if (design.getSingleOverall().equals("single")) { + continue; + } + if (design.getModelType().equals("System")) { + SysFile sysFile = sysFileMapper.selectById(design.getTemplateId()); + if (null == sysFile) { + continue; + } + sketch.put("sex", sysFile.getLevel2Type().equals("Male") ? "Male" : "Female"); + } else { + Library library = libraryMapper.selectById(design.getTemplateId()); + if (null == library) { + continue; + } + sketch.put("sex", library.getLevel2Type().equals("Female") ? "Female" : "Male"); + } + sketch.put("type", designItemDetail.getType()); sketch.put("path", path); - if (urlList.contains(path)) { - sketch.put("beSelected", true); - }else { - sketch.put("beSelected", false); - } + sketch.put("beSelected", urlSet.contains(path)); + sketch.put("accountId", accountId); + if (path.contains("aida-sys-image/images/")) { - String searchPath = new String(path); - String replace = searchPath.replace("aida-sys-image/images/", ""); + String replace = path.replace("aida-sys-image/images/", ""); String style = getStyleByUrl(replace); if (!StringUtils.isEmpty(style)) { sketch.put("style", style); } } + userLikeSketchList.add(sketch); + } + if (CollectionUtil.isNotEmpty(userLikeSketchList)) { + result.add(userLikeSketchList); } - list.add(sketch); - } - if (result.containsKey(accountId)) { - result.get(accountId).add(list); - }else { - List>> lists = new ArrayList<>(); - lists.add(list); - result.put(accountId, lists); } } - System.out.println(JSONObject.toJSONString(result)); + + // 将结果以美观的JSON形式保存到文件中 + saveResultAsPrettyJson(result, "C:\\Users\\10233\\Desktop\\result.json"); + return null; } + @Override + public String download() { + // 下载 aida-collection-element bucket 下所有 Sketchboard 文件夹内容 +// String sketchboardLocalPath = "C:\\workspace\\fileData\\minio"; +// minioUtil.downloadSketchboardDirectories(sketchboardLocalPath); +// +// // 下载整个 aida-users bucket +// String aidaUsersLocalPath = "C:\\workspace\\fileData\\minio"; +// minioUtil.downloadEntireBucket("aida-users", aidaUsersLocalPath); + return null; + } + + // 提取公共方法 + private List getCollectionElementsByCollectionId(Long collectionId) { + QueryWrapper qw = new QueryWrapper<>(); + qw.lambda().eq(CollectionElement::getCollectionId, collectionId) + .eq(CollectionElement::getLevel1Type, "Sketchboard"); + return collectionElementMapper.selectList(qw); + } + + private List getUserLikesByGroupId(Long groupId) { + QueryWrapper qw = new QueryWrapper<>(); + qw.lambda().eq(UserLike::getUserLikeGroupId, groupId); + return userLikeMapper.selectList(qw); + } + + private List getDesignItemDetails(Long designItemId) { + QueryWrapper qw = new QueryWrapper<>(); + qw.lambda().eq(DesignItemDetail::getDesignItemId, designItemId) + .ne(DesignItemDetail::getType, "Body"); + return designItemDetailMapper.selectList(qw); + } + + private void saveResultAsPrettyJson(List>> result, String filePath) { + // 使用一个 Map 将结果存储为字符串键 + Map resultMap = new HashMap<>(); + resultMap.put("data", result); + + try (FileWriter file = new FileWriter(filePath)) { + // 使用 JSONObject 将转换后的 Map 转为 JSON + JSONObject jsonObject = new JSONObject(resultMap); + // 格式化输出为美观的 JSON + file.write(JSON.toJSONString(jsonObject, SerializerFeature.PrettyFormat)); + } catch (IOException e) { + e.printStackTrace(); + } + } + + + + private String getStyleByUrl(String replace) { String[] split = replace.split("/"); String tableName = getTableName(split); if (StringUtils.isEmpty(tableName)) { return null; }else { + replace = split[1] + "/" + split[2]; return attributeRetrievalMapper.getStyleByUrl(replace, tableName); } }