|
|
@@ -1,8 +1,194 @@
|
|
|
package com.webflux.launchadmin.mysql.service.planNew.goGenerate;
|
|
|
|
|
|
+import cn.hutool.core.bean.BeanUtil;
|
|
|
+import cn.hutool.core.util.RandomUtil;
|
|
|
+import cn.hutool.http.HttpUtil;
|
|
|
+import cn.hutool.json.JSONObject;
|
|
|
+import cn.hutool.json.JSONUtil;
|
|
|
+import com.webflux.launchadmin.config.BaseContextHandler;
|
|
|
+import com.webflux.launchadmin.global.BaseException;
|
|
|
+import com.webflux.launchadmin.mysql.controller.planNew.req.AudioRequest;
|
|
|
+import com.webflux.launchadmin.mysql.controller.planNew.req.PosterRequest;
|
|
|
+import com.webflux.launchadmin.mysql.controller.planNew.res.PosterRes;
|
|
|
+import com.webflux.launchadmin.mysql.entity.planNew.Ad;
|
|
|
+import com.webflux.launchadmin.mysql.entity.planNew.Image;
|
|
|
+import com.webflux.launchadmin.mysql.entity.planNew.Material;
|
|
|
+import com.webflux.launchadmin.mysql.entity.planNew.Poster;
|
|
|
+import com.webflux.launchadmin.mysql.service.planNew.structure.GoRequest;
|
|
|
+import com.webflux.launchadmin.mysql.service.planNew.structure.Item;
|
|
|
+import com.webflux.launchcommon.returnObj.RStatus;
|
|
|
+import jakarta.annotation.Resource;
|
|
|
+import org.springframework.beans.factory.annotation.Value;
|
|
|
+import org.springframework.data.r2dbc.core.R2dbcEntityTemplate;
|
|
|
+import org.springframework.data.relational.core.query.Criteria;
|
|
|
+import org.springframework.data.relational.core.query.Query;
|
|
|
+import org.springframework.data.relational.core.query.Update;
|
|
|
import org.springframework.stereotype.Service;
|
|
|
+import org.springframework.web.bind.annotation.RequestBody;
|
|
|
+import reactor.core.publisher.Mono;
|
|
|
+
|
|
|
+import java.time.ZonedDateTime;
|
|
|
+import java.util.List;
|
|
|
+import java.util.Objects;
|
|
|
+import java.util.Optional;
|
|
|
+import java.util.concurrent.atomic.AtomicReference;
|
|
|
+import java.util.concurrent.locks.Lock;
|
|
|
|
|
|
@Service
|
|
|
public class GoGenerateService {
|
|
|
|
|
|
+ @Value("${go.data.createImage}")
|
|
|
+ private String value;
|
|
|
+
|
|
|
+ @Resource
|
|
|
+ private R2dbcEntityTemplate template;
|
|
|
+
|
|
|
+ //http://wx-share-test.duiweize.com
|
|
|
+ ///api/image/createImage
|
|
|
+
|
|
|
+ //http://wx-share-8.duiweize.com 正式
|
|
|
+ public String GoGenerate(String temStr){
|
|
|
+ JSONObject jsonObject = JSONUtil.parseObj(temStr);
|
|
|
+ GoRequest goRequest = new GoRequest();
|
|
|
+ BeanUtil.copyProperties(jsonObject,goRequest);
|
|
|
+ String post = HttpUtil.post(value + "/api/image/createImage", JSONUtil.parseObj(goRequest));
|
|
|
+ return post;
|
|
|
+
|
|
|
+ }
|
|
|
+
|
|
|
+ public Mono<RStatus<PosterRes>> savePoster(PosterRequest posterRequset){
|
|
|
+ Poster poster = new Poster(null, posterRequset.getPosterTemplateId()
|
|
|
+ ,posterRequset.getPosterTemplate(), BaseContextHandler.getUserName(),
|
|
|
+ BaseContextHandler.getUserId(), ZonedDateTime.now(),
|
|
|
+ ZonedDateTime.now(),null,null);
|
|
|
+ Mono<Poster> insert = template.insert(poster)
|
|
|
+ .switchIfEmpty(Mono.error(new BaseException(" 操作失败")))
|
|
|
+ .onErrorResume(throwable -> Mono.error(new Exception(throwable.getMessage())));;
|
|
|
+
|
|
|
+ String posterTemplate = posterRequset.getPosterTemplate();
|
|
|
+ JSONObject jsonObject = JSONUtil.parseObj(posterTemplate);
|
|
|
+ GoRequest goRequest = new GoRequest();
|
|
|
+ BeanUtil.copyProperties(jsonObject,goRequest);
|
|
|
+ if (goRequest.getIsRandom()) {
|
|
|
+ // 背景
|
|
|
+ Mono<List<Image>> bgImage = template.select(Query.query(Criteria.where("type").is(2)), Image.class).collectList();
|
|
|
+ //封面
|
|
|
+ Mono<List<Image>> fmImage = template.select(Query.query(Criteria.where("type").is(1)), Image.class).collectList();
|
|
|
+ //文字
|
|
|
+ Mono<List<Material>> text = template.select(Query.query(Criteria.where("type").is(1)), Material.class).collectList();
|
|
|
+ Mono<String> stringMono = Mono.zip(bgImage, fmImage, text).flatMap(f -> {
|
|
|
+ Optional<Image> first = f.getT1().stream().findFirst();
|
|
|
+ first.ifPresent(image -> goRequest.setBg_img_src(image.image()));
|
|
|
+ Optional<Image> first1 = f.getT2().stream().findFirst();
|
|
|
+ first1.ifPresent(image -> goRequest.getItem().stream()
|
|
|
+ .filter(ff -> "image".equals(ff.getT()))
|
|
|
+ .findFirst()
|
|
|
+ .ifPresent(item -> item.setV(image.image())));
|
|
|
+ Optional<Material> first2 = f.getT3().stream().findFirst();
|
|
|
+ first2.ifPresent(material -> goRequest.getItem().stream()
|
|
|
+ .filter(ff -> "text".equals(ff.getT()))
|
|
|
+ .findFirst()
|
|
|
+ .ifPresent(item -> item.setV(material.content())));
|
|
|
+ String s = GoGenerate(JSONUtil.toJsonStr(goRequest));
|
|
|
+ return Mono.just(s);
|
|
|
+ });
|
|
|
+ Mono<PosterRes> zip = getPosterResMono(insert, stringMono);
|
|
|
+ return RStatus.success(zip);
|
|
|
+ }else {
|
|
|
+ String s = GoGenerate(JSONUtil.toJsonStr(goRequest));
|
|
|
+ Mono<PosterRes> zip = getPosterResMono(insert, Mono.just(s));
|
|
|
+ return RStatus.success(zip);
|
|
|
+ }
|
|
|
+ }
|
|
|
+
|
|
|
+ private Mono<PosterRes> getPosterResMono(Mono<Poster> insert, Mono<String> stringMono) {
|
|
|
+ Mono<PosterRes> zip = Mono.zip(insert, stringMono, (entry, ret) -> {
|
|
|
+ PosterRes posterRes = new PosterRes();
|
|
|
+ posterRes.setPosterId(entry.posterId());
|
|
|
+ posterRes.setPosterTemplate(entry.posterTemplate());
|
|
|
+ posterRes.setPosterTemplateId(entry.posterTemplateId());
|
|
|
+ JSONObject jsonObject1 = JSONUtil.parseObj(ret);
|
|
|
+ Criteria empty = Criteria.where("poster_id").is(entry.posterId());
|
|
|
+ Update update = Update.update("updated_at", ZonedDateTime.now());
|
|
|
+ if (jsonObject1.containsKey("code") && jsonObject1.get("code", Integer.class) == 0 && jsonObject1.containsKey("data")) {
|
|
|
+ JSONObject data = jsonObject1.get("data", JSONObject.class);
|
|
|
+ if (data.containsKey("url")) {
|
|
|
+ String url = data.get("url", String.class);
|
|
|
+ update.set("generate_images_url", url);
|
|
|
+ update.set("generate_images_Jjson", ret);
|
|
|
+ posterRes.setGenerateImagesUrl(url);
|
|
|
+ template.update(Query.query(empty), update, Poster.class)
|
|
|
+ .switchIfEmpty(Mono.error(new BaseException(" 操作失败")))
|
|
|
+ .onErrorResume(throwable -> Mono.error(new Exception(throwable.getMessage())));
|
|
|
+ }
|
|
|
+ }
|
|
|
+ return posterRes;
|
|
|
+ });
|
|
|
+ return zip;
|
|
|
+ }
|
|
|
+
|
|
|
+ public Mono<RStatus<PosterRes>> updatePoster(@RequestBody PosterRequest posterRequset){
|
|
|
+
|
|
|
+ String posterTemplate = posterRequset.getPosterTemplate();
|
|
|
+ JSONObject jsonObject = JSONUtil.parseObj(posterTemplate);
|
|
|
+ GoRequest goRequest = new GoRequest();
|
|
|
+ BeanUtil.copyProperties(jsonObject,goRequest);
|
|
|
+ if (goRequest.getIsRandom()) {
|
|
|
+ // 背景
|
|
|
+ Mono<List<Image>> bgImage = template.select(Query.query(Criteria.where("type").is(2)), Image.class).collectList();
|
|
|
+ //封面
|
|
|
+ Mono<List<Image>> fmImage = template.select(Query.query(Criteria.where("type").is(1)), Image.class).collectList();
|
|
|
+ //文字
|
|
|
+ Mono<List<Material>> text = template.select(Query.query(Criteria.where("type").is(1)), Material.class).collectList();
|
|
|
+ Mono<String> stringMono = Mono.zip(bgImage, fmImage, text).flatMap(f -> {
|
|
|
+ Optional<Image> first = f.getT1().stream().findFirst();
|
|
|
+ first.ifPresent(image -> goRequest.setBg_img_src(image.image()));
|
|
|
+ Optional<Image> first1 = f.getT2().stream().findFirst();
|
|
|
+ first1.ifPresent(image -> goRequest.getItem().stream()
|
|
|
+ .filter(ff -> "image".equals(ff.getT()))
|
|
|
+ .findFirst()
|
|
|
+ .ifPresent(item -> item.setV(image.image())));
|
|
|
+ Optional<Material> first2 = f.getT3().stream().findFirst();
|
|
|
+ first2.ifPresent(material -> goRequest.getItem().stream()
|
|
|
+ .filter(ff -> "text".equals(ff.getT()))
|
|
|
+ .findFirst()
|
|
|
+ .ifPresent(item -> item.setV(material.content())));
|
|
|
+ String s = GoGenerate(JSONUtil.toJsonStr(goRequest));
|
|
|
+ return Mono.just(s);
|
|
|
+ });
|
|
|
+ Mono<PosterRes> zip = getPosterResMono2(Mono.just(posterRequset), stringMono);
|
|
|
+ return RStatus.success(zip);
|
|
|
+ }else {
|
|
|
+ String s = GoGenerate(JSONUtil.toJsonStr(goRequest));
|
|
|
+ Mono<PosterRes> zip = getPosterResMono2(Mono.just(posterRequset), Mono.just(s));
|
|
|
+ return RStatus.success(zip);
|
|
|
+ }
|
|
|
+ }
|
|
|
+
|
|
|
+ private Mono<PosterRes> getPosterResMono2(Mono<PosterRequest> insert, Mono<String> stringMono) {
|
|
|
+ Mono<PosterRes> zip = Mono.zip(insert, stringMono, (entry, ret) -> {
|
|
|
+ PosterRes posterRes = new PosterRes();
|
|
|
+ posterRes.setPosterId(entry.getPosterId());
|
|
|
+ posterRes.setPosterTemplate(entry.getPosterTemplate());
|
|
|
+ posterRes.setPosterTemplateId(entry.getPosterTemplateId());
|
|
|
+ JSONObject jsonObject1 = JSONUtil.parseObj(ret);
|
|
|
+ Criteria empty = Criteria.where("poster_id").is(entry.getPosterId());
|
|
|
+ Update update = Update.update("updated_at", ZonedDateTime.now());
|
|
|
+ update.set("poster_template",entry.getPosterTemplate());
|
|
|
+ if (jsonObject1.containsKey("code") && jsonObject1.get("code", Integer.class) == 0 && jsonObject1.containsKey("data")) {
|
|
|
+ JSONObject data = jsonObject1.get("data", JSONObject.class);
|
|
|
+ if (data.containsKey("url")) {
|
|
|
+ String url = data.get("url", String.class);
|
|
|
+ update.set("generate_images_url", url);
|
|
|
+ update.set("generate_images_Jjson", ret);
|
|
|
+ posterRes.setGenerateImagesUrl(url);
|
|
|
+ template.update(Query.query(empty), update, Poster.class)
|
|
|
+ .switchIfEmpty(Mono.error(new BaseException(" 操作失败")))
|
|
|
+ .onErrorResume(throwable -> Mono.error(new Exception(throwable.getMessage())));
|
|
|
+ }
|
|
|
+ }
|
|
|
+ return posterRes;
|
|
|
+ });
|
|
|
+ return zip;
|
|
|
+ }
|
|
|
}
|