|
|
@@ -5,10 +5,16 @@ import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
|
|
|
import com.baomidou.mybatisplus.core.conditions.update.LambdaUpdateWrapper;
|
|
|
import com.baomidou.mybatisplus.core.metadata.IPage;
|
|
|
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
|
|
|
+import com.fasterxml.jackson.databind.JsonNode;
|
|
|
+import com.fasterxml.jackson.databind.ObjectMapper;
|
|
|
+import com.fasterxml.jackson.databind.node.ObjectNode;
|
|
|
import lombok.RequiredArgsConstructor;
|
|
|
import lombok.extern.slf4j.Slf4j;
|
|
|
import org.springframework.beans.BeanUtils;
|
|
|
+import org.springframework.beans.factory.annotation.Value;
|
|
|
import org.springframework.stereotype.Service;
|
|
|
+import org.springframework.util.LinkedMultiValueMap;
|
|
|
+import org.springframework.util.MultiValueMap;
|
|
|
import shop.alien.entity.store.LifeDiscountCoupon;
|
|
|
import shop.alien.entity.store.LifeUser;
|
|
|
import shop.alien.entity.store.StoreImg;
|
|
|
@@ -19,6 +25,7 @@ import shop.alien.entity.storePlatform.vo.StoreOperationalActivityVO;
|
|
|
import shop.alien.mapper.LifeDiscountCouponMapper;
|
|
|
import shop.alien.mapper.StoreImgMapper;
|
|
|
import shop.alien.mapper.storePlantform.StoreOperationalActivityMapper;
|
|
|
+import shop.alien.storeplatform.feign.AlienAIFeign;
|
|
|
import shop.alien.storeplatform.service.OperationalActivityService;
|
|
|
|
|
|
import java.text.SimpleDateFormat;
|
|
|
@@ -55,13 +62,21 @@ public class OperationalActivityServiceImpl implements OperationalActivityServic
|
|
|
|
|
|
private final LifeDiscountCouponMapper lifeDiscountCouponMapper;
|
|
|
|
|
|
+ private final AlienAIFeign alienAIFeign;
|
|
|
+
|
|
|
+ @Value("${ai.aiAccount}")
|
|
|
+ private String aiAccount;
|
|
|
+
|
|
|
+ @Value("${ai.aiPassword}")
|
|
|
+ private String aiPassword;
|
|
|
+
|
|
|
@Override
|
|
|
public int createActivity(StoreOperationalActivityDTO dto) {
|
|
|
log.info("OperationalActivityServiceImpl.createActivity: dto={}", dto);
|
|
|
-
|
|
|
+
|
|
|
StoreOperationalActivity activity = new StoreOperationalActivity();
|
|
|
BeanUtils.copyProperties(dto, activity);
|
|
|
-
|
|
|
+
|
|
|
// 设置默认值
|
|
|
if (activity.getParticipationLimit() == null) {
|
|
|
activity.setParticipationLimit(0);
|
|
|
@@ -71,6 +86,57 @@ public class OperationalActivityServiceImpl implements OperationalActivityServic
|
|
|
}
|
|
|
Integer result = activityMapper.insert(activity);
|
|
|
if (result > 0) {
|
|
|
+ // 使用用户描述让AI生成图片。
|
|
|
+ if (dto.getUploadImgType()==2) {
|
|
|
+ try {
|
|
|
+ // 先调用登录接口获取access_token
|
|
|
+ MultiValueMap<String, String> formData = new LinkedMultiValueMap<>();
|
|
|
+ formData.add("username", aiAccount);
|
|
|
+ formData.add("password", aiPassword);
|
|
|
+ JsonNode loginResponse = alienAIFeign.login(formData);
|
|
|
+
|
|
|
+ String accessToken = null;
|
|
|
+ if (loginResponse != null && loginResponse.has("data")) {
|
|
|
+ JsonNode data = loginResponse.get("data");
|
|
|
+ if (data.has("access_token")) {
|
|
|
+ accessToken = data.get("access_token").asText();
|
|
|
+ }
|
|
|
+ }
|
|
|
+
|
|
|
+ if (accessToken == null || accessToken.isEmpty()) {
|
|
|
+ log.error("获取AI服务access_token失败,无法生成促销图片");
|
|
|
+ } else {
|
|
|
+ ObjectMapper objectMapper = new ObjectMapper();
|
|
|
+ ObjectNode requestBody = objectMapper.createObjectNode();
|
|
|
+ requestBody.put("text", dto.getImgDescribe());
|
|
|
+ // 调用接口,传递Bearer token
|
|
|
+ String authorization = "Bearer " + accessToken;
|
|
|
+ JsonNode response = alienAIFeign.generatePromotionImage(authorization, requestBody);
|
|
|
+ // 解析响应
|
|
|
+ if (response.has("data")) {
|
|
|
+ JsonNode data = response.get("data");
|
|
|
+ // 提取横向图(banner_image)的图片URL
|
|
|
+ if (data.has("banner_image")) {
|
|
|
+ JsonNode bannerImage = data.get("banner_image");
|
|
|
+ if (bannerImage.has("image_url") && !bannerImage.get("image_url").isNull()) {
|
|
|
+ String bannerImageUrl = bannerImage.get("image_url").asText();
|
|
|
+ dto.getActivityTitleImg().setImgUrl(bannerImageUrl);
|
|
|
+ }
|
|
|
+ }
|
|
|
+ // 提取竖向图(vertical_image)的图片URL
|
|
|
+ if (data.has("vertical_image")) {
|
|
|
+ JsonNode verticalImage = data.get("vertical_image");
|
|
|
+ if (verticalImage.has("image_url") && !verticalImage.get("image_url").isNull()) {
|
|
|
+ String verticalImageUrl = verticalImage.get("image_url").asText();
|
|
|
+ dto.getActivityDetailImg().setImgUrl(verticalImageUrl);
|
|
|
+ }
|
|
|
+ }
|
|
|
+ }
|
|
|
+ }
|
|
|
+ } catch (Exception e) {
|
|
|
+ log.error("调用AI服务生成促销图片失败", e);
|
|
|
+ }
|
|
|
+ }
|
|
|
dto.getActivityTitleImg().setBusinessId(activity.getId());
|
|
|
dto.getActivityTitleImg().setImgType(26);
|
|
|
imgMapper.insert(dto.getActivityTitleImg());
|