|
|
@@ -0,0 +1,639 @@
|
|
|
+package com.webflux.launchadmin.mysql.service.article;
|
|
|
+
|
|
|
+
|
|
|
+import cn.hutool.core.util.RandomUtil;
|
|
|
+import com.webflux.launchadmin.config.BaseContextHandler;
|
|
|
+import com.webflux.launchadmin.global.BaseException;
|
|
|
+import com.webflux.launchadmin.mysql.controller.article.request.*;
|
|
|
+import com.webflux.launchadmin.mysql.controller.article.res.*;
|
|
|
+import com.webflux.launchadmin.mysql.entity.article.*;
|
|
|
+import com.webflux.launchadmin.mysql.service.sourceMaterial.SourceMaterialInterface;
|
|
|
+import com.webflux.launchadmin.mysql.service.sourceMaterial.data.sql.Where;
|
|
|
+import com.webflux.launchcommon.returnObj.Paged;
|
|
|
+import com.webflux.launchcommon.returnObj.RStatus;
|
|
|
+import com.webflux.launchcommon.utils.R;
|
|
|
+import jakarta.annotation.Resource;
|
|
|
+import org.springframework.beans.factory.annotation.Autowired;
|
|
|
+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 reactor.core.publisher.Mono;
|
|
|
+
|
|
|
+import java.time.ZonedDateTime;
|
|
|
+import java.util.ArrayList;
|
|
|
+import java.util.Comparator;
|
|
|
+import java.util.List;
|
|
|
+import java.util.Objects;
|
|
|
+import java.util.function.Consumer;
|
|
|
+import java.util.stream.Collectors;
|
|
|
+
|
|
|
+@Service
|
|
|
+public class ArticleSercieImp implements ArticleServiceInterface {
|
|
|
+ @Autowired
|
|
|
+ private SourceMaterialInterface sourceMaterialInterface;
|
|
|
+ @Resource
|
|
|
+ private R2dbcEntityTemplate template;
|
|
|
+
|
|
|
+ @Override
|
|
|
+ public Mono<RStatus<Paged<Classification>>> getClassificationList() {
|
|
|
+ return sourceMaterialInterface.page(List.of(), Classification.class, 1000, 1);
|
|
|
+ }
|
|
|
+
|
|
|
+ @Override
|
|
|
+ public Mono<RStatus<Boolean>> addClassification(ClassificationReq save) {
|
|
|
+ String userId = BaseContextHandler.getUserId();
|
|
|
+ String userName = BaseContextHandler.getUserName();
|
|
|
+ Classification classification = new Classification(null, save.getName(), save.getStIx(), save.getShowType(), userName, userId, ZonedDateTime.now(), null);
|
|
|
+ Mono<Boolean> mono = template.insert(classification)
|
|
|
+ .onErrorResume(throwable -> Mono.error(new BaseException("保存失败:" + throwable.getMessage())))
|
|
|
+ .flatMap(classification1 -> Mono.just(true));
|
|
|
+ return RStatus.success(mono);
|
|
|
+ }
|
|
|
+
|
|
|
+ @Override
|
|
|
+ public Mono<RStatus<Boolean>> updateClassification(ClassificationReq save) {
|
|
|
+ Mono<Boolean> mono1 = template.selectOne(Query.query(Criteria.where("id").is(save.getId())), Classification.class)
|
|
|
+ .switchIfEmpty(Mono.error(new BaseException("id查询为空")))
|
|
|
+ .onErrorResume(throwable -> Mono.error(new BaseException("查询异常:" + throwable.getMessage())))
|
|
|
+ .flatMap(classification -> {
|
|
|
+ Classification classificationnew =
|
|
|
+ new Classification(save.getId(), save.getName(), save.getStIx(), save.getShowType(),
|
|
|
+ classification.name(), classification.createId(), classification.createdAt(), ZonedDateTime.now());
|
|
|
+ return template.update(classificationnew)
|
|
|
+ .onErrorResume(throwable -> Mono.error(new BaseException("保存失败:" + throwable.getMessage())))
|
|
|
+ .flatMap(classification1 -> Mono.just(true));
|
|
|
+ });
|
|
|
+ return RStatus.success(mono1);
|
|
|
+ }
|
|
|
+
|
|
|
+ @Override
|
|
|
+ public Mono<RStatus<Boolean>> deleteClassification(Long id) {
|
|
|
+ Mono<Boolean> id1 = template.delete(Query.query(Criteria.where("id").is(id)), Classification.class)
|
|
|
+ .onErrorResume(throwable -> Mono.error(new BaseException("移除失败:" + throwable.getMessage())))
|
|
|
+ .flatMap(aLong -> Mono.just(true));
|
|
|
+ return RStatus.success(id1);
|
|
|
+ }
|
|
|
+ @Override
|
|
|
+ public Mono<RStatus<Classification>> getClassification(Long id) {
|
|
|
+ Mono<Classification> id1 = template.selectOne(Query.query(Criteria.where("id").is(id)), Classification.class)
|
|
|
+ .onErrorResume(throwable -> Mono.error(new BaseException("查询失败:" + throwable.getMessage())));
|
|
|
+ return RStatus.success(id1);
|
|
|
+ }
|
|
|
+
|
|
|
+ @Override
|
|
|
+ public Mono<RStatus<LabelManagement>> getLable(Long id) {
|
|
|
+ Mono<LabelManagement> id1 = template.selectOne(Query.query(Criteria.where("id").is(id)), LabelManagement.class)
|
|
|
+ .onErrorResume(throwable -> Mono.error(new BaseException("查询失败:" + throwable.getMessage())));
|
|
|
+ return RStatus.success(id1);
|
|
|
+ }
|
|
|
+
|
|
|
+ @Override
|
|
|
+ public Mono<RStatus<Boolean>> deleteArticle(Long id) {
|
|
|
+ Update deleted_at = Update.update("deleted_at", ZonedDateTime.now());
|
|
|
+ Mono<Boolean> id1 = template.update(Query.query(Criteria.where("id").is(id)),deleted_at, Article.class)
|
|
|
+ .onErrorResume(throwable -> Mono.error(new BaseException("移除失败:" + throwable.getMessage())))
|
|
|
+ .flatMap(aLong -> Mono.just(true));
|
|
|
+ return RStatus.success(id1);
|
|
|
+ }
|
|
|
+
|
|
|
+ @Override
|
|
|
+ public Mono<RStatus<LabelManagement>> getArticle(Long id) {
|
|
|
+ Mono<LabelManagement> id1 = template.selectOne(Query.query(Criteria.where("id").is(id)), LabelManagement.class)
|
|
|
+ .onErrorResume(throwable -> Mono.error(new BaseException("查询失败:" + throwable.getMessage())));
|
|
|
+ return RStatus.success(id1);
|
|
|
+ }
|
|
|
+
|
|
|
+ @Override
|
|
|
+ public Mono<RStatus<Paged<LabelManagementRes>>> getLableList(String name,Integer page,Integer size) {
|
|
|
+ ArrayList<Where> wheres = new ArrayList<>();
|
|
|
+ if (!"".equals(name.trim())) {
|
|
|
+ Where where = new Where();
|
|
|
+ where.setValue(name);
|
|
|
+ where.setKey("name");
|
|
|
+ where.setWhereType(2);
|
|
|
+ }
|
|
|
+ return sourceMaterialInterface.pageAndFunctionRtE(wheres, LabelManagement.class,size,page,labelManagements -> {
|
|
|
+ Mono<List<Classification>> listMono = Mono.just(List.of());
|
|
|
+ if (!labelManagements.isEmpty()) {
|
|
|
+ List<Long> collect = labelManagements.stream().map(LabelManagement::classificationId).collect(Collectors.toList());
|
|
|
+ if (!collect.isEmpty()) {
|
|
|
+ listMono = template.select(Query.query(Criteria.where("id").in(collect)).columns("id","name"), Classification.class).collectList();
|
|
|
+ }
|
|
|
+ }
|
|
|
+ return Mono.zip(Mono.just(labelManagements), listMono).flatMap(objects -> {
|
|
|
+ List<LabelManagement> t1 = objects.getT1();
|
|
|
+ List<Classification> t2 = objects.getT2();
|
|
|
+ List<LabelManagementRes> labelManagementRes1 = new ArrayList<>();
|
|
|
+ t1.stream().<Consumer<Classification>>map(labelManagement -> classification -> {
|
|
|
+ Long aLong = labelManagement.classificationId();
|
|
|
+ Long id = classification.id();
|
|
|
+ if (id.equals(aLong)) {
|
|
|
+ LabelManagementRes labelManagementRes = new LabelManagementRes();
|
|
|
+ labelManagementRes.setId(labelManagement.id());
|
|
|
+ labelManagementRes.setClassificationName(classification.name());
|
|
|
+ labelManagementRes.setIsTop(labelManagement.isTop());
|
|
|
+ labelManagementRes.setName(labelManagement.name());
|
|
|
+ labelManagementRes.setUrl(labelManagement.url());
|
|
|
+ labelManagementRes.setStIx(labelManagement.stIx());
|
|
|
+ labelManagementRes1.add(labelManagementRes);
|
|
|
+ }
|
|
|
+ }).forEach(t2::forEach);
|
|
|
+ List<LabelManagementRes> collect = labelManagementRes1.stream().sorted(Comparator.comparing(LabelManagementRes::getStIx)).collect(Collectors.toList());
|
|
|
+ return Mono.just(collect);
|
|
|
+ });
|
|
|
+ });
|
|
|
+ }
|
|
|
+
|
|
|
+ @Override
|
|
|
+ public Mono<RStatus<Boolean>> addLable(LabelReq save) {
|
|
|
+ String userId = BaseContextHandler.getUserId();
|
|
|
+ String userName = BaseContextHandler.getUserName();
|
|
|
+ LabelManagement labelManagement = new LabelManagement(null, save.getName(),
|
|
|
+ save.getStIx(), save.getUrl(), save.getClassificationId(),save.getClassificationName(),
|
|
|
+ save.getIsTop(), userName, userId, ZonedDateTime.now(), null);
|
|
|
+ Mono<Boolean> mono = template.insert(labelManagement)
|
|
|
+ .onErrorResume(throwable -> Mono.error(new BaseException("保存失败:" + throwable.getMessage()))).flatMap(labelManagement1 -> Mono.just(true));
|
|
|
+ return RStatus.success(mono);
|
|
|
+ }
|
|
|
+
|
|
|
+ @Override
|
|
|
+ public Mono<RStatus<Boolean>> updateLable(LabelReq save) {
|
|
|
+ Mono<Boolean> mono1 = template.selectOne(Query.query(Criteria.where("id").is(save.getId())), LabelManagement.class)
|
|
|
+ .switchIfEmpty(Mono.error(new BaseException("id查询为空")))
|
|
|
+ .onErrorResume(throwable -> Mono.error(new BaseException("查询异常:" + throwable.getMessage())))
|
|
|
+ .flatMap(labelManagement -> {
|
|
|
+ LabelManagement labelManagement1 = new LabelManagement(save.getId(), save.getName(),
|
|
|
+ save.getStIx(), save.getUrl(), save.getClassificationId(),save.getClassificationName(),
|
|
|
+ save.getIsTop(), labelManagement.createUser(), labelManagement.createId(), labelManagement.createdAt(), ZonedDateTime.now());
|
|
|
+ return template.update(labelManagement1)
|
|
|
+ .onErrorResume(throwable -> Mono.error(new BaseException("保存失败:" + throwable.getMessage())))
|
|
|
+ .flatMap(classification1 -> Mono.just(true));
|
|
|
+ });
|
|
|
+ return RStatus.success(mono1);
|
|
|
+ }
|
|
|
+
|
|
|
+ @Override
|
|
|
+ public Mono<RStatus<Boolean>> deleteLable(Long id) {
|
|
|
+ Mono<Boolean> id1 = template.delete(Query.query(Criteria.where("id").is(id)), LabelManagement.class)
|
|
|
+ .onErrorResume(throwable -> Mono.error(new BaseException("移除失败:" + throwable.getMessage())))
|
|
|
+ .flatMap(aLong -> Mono.just(true));
|
|
|
+ return RStatus.success(id1);
|
|
|
+ }
|
|
|
+
|
|
|
+ @Override
|
|
|
+ public Mono<RStatus<Paged<ArticleRes>>> getArticleList(ArticleReq articleReq) {
|
|
|
+ StringBuilder startSql = new StringBuilder();
|
|
|
+ startSql.append("SELECT id,front_cover,title,date_time_str,author,labels,send_friends,send_friends_circle,ad FROM article WHERE deleted_at IS NULL ");
|
|
|
+ StringBuilder startSqlCount= new StringBuilder(" SELECT id FROM article WHERE deleted_at IS NULL ");
|
|
|
+ StringBuilder stringBuilder =new StringBuilder();
|
|
|
+ Criteria empty =Criteria.empty();
|
|
|
+ if(Objects.nonNull(articleReq.getClassificationId())){
|
|
|
+ Long classificationId = articleReq.getClassificationId();
|
|
|
+ empty = empty.and("classification_id").is(classificationId);
|
|
|
+ }
|
|
|
+ if(Objects.nonNull(articleReq.getLabelId())){
|
|
|
+ Long labelId = articleReq.getLabelId();
|
|
|
+ empty = empty.and("id").is(labelId);
|
|
|
+ }
|
|
|
+ Mono<List<LabelManagement>> listMono;
|
|
|
+ if (!empty.isEmpty()) {
|
|
|
+ listMono = template.select(Query.query(empty), LabelManagement.class).collectList()
|
|
|
+ .onErrorResume(throwable -> Mono.error(new BaseException("查询标签异常::" + throwable.getMessage())));
|
|
|
+ }else {
|
|
|
+ listMono= Mono.just(List.of());
|
|
|
+ }
|
|
|
+ if(Objects.nonNull(articleReq.getTitle()) && !"".equals(articleReq.getTitle().trim())){
|
|
|
+ stringBuilder.append(" AND title like ");
|
|
|
+ stringBuilder.append("%").append(articleReq.getTitle()).append("%");
|
|
|
+ }
|
|
|
+ if(Objects.nonNull(articleReq.getAuthor()) && !"".equals(articleReq.getAuthor().trim())){
|
|
|
+ stringBuilder.append(" AND author like ");
|
|
|
+ stringBuilder.append("%").append(articleReq.getAuthor()).append("%");
|
|
|
+ }
|
|
|
+ if(Objects.nonNull(articleReq.getDateTimeStr()) && !"".equals(articleReq.getDateTimeStr().trim())){
|
|
|
+ String dateTimeStr = articleReq.getDateTimeStr();
|
|
|
+ String[] s = dateTimeStr.split("_");
|
|
|
+ if (s.length > 1) {
|
|
|
+ stringBuilder.append(" AND date_time_str > ");
|
|
|
+ stringBuilder.append(s[0]);
|
|
|
+ stringBuilder.append(" AND date_time_str < ");
|
|
|
+ stringBuilder.append(s[1]);
|
|
|
+ }
|
|
|
+ }
|
|
|
+ Mono<Paged<ArticleRes>> pagedMono = listMono.flatMap(labelManagements -> {
|
|
|
+ if (!labelManagements.isEmpty()) {
|
|
|
+ labelManagements.stream().map(LabelManagement::name).forEach(labelManagement -> {
|
|
|
+ stringBuilder.append(" OR labels like ");
|
|
|
+ stringBuilder.append("%").append(labelManagement).append("%");
|
|
|
+ });
|
|
|
+ }
|
|
|
+ StringBuilder stringBuilderPage = new StringBuilder();
|
|
|
+ stringBuilderPage.append(" LIMIT ");
|
|
|
+ stringBuilderPage.append(articleReq.getSize());
|
|
|
+ stringBuilderPage.append(" OFFSET ");
|
|
|
+ stringBuilderPage.append((articleReq.getPage() - 1) * articleReq.getSize());
|
|
|
+ Mono<List<Long>> listMono2 = template.getDatabaseClient().sql(startSqlCount.toString() + stringBuilder).map((row, rowMetadata) -> 1L).all().collectList();
|
|
|
+ return listMono2.flatMap(list -> {
|
|
|
+ Mono<List<ArticleRes>> listMono1 = template.getDatabaseClient().sql(startSql.toString()+stringBuilder+stringBuilderPage).map((row, rowMetadata) -> {
|
|
|
+ ArticleRes articleRes = new ArticleRes();
|
|
|
+ articleRes.setId(row.get("id", Long.class));
|
|
|
+ articleRes.setTitle(row.get("title", String.class));
|
|
|
+ articleRes.setDateTimeStr(row.get("date_time_str", String.class));
|
|
|
+ articleRes.setAuthor(row.get("author", String.class));
|
|
|
+ articleRes.setLabels(row.get("labels", String.class));
|
|
|
+ articleRes.setSendFriends(row.get("send_friends", Integer.class));
|
|
|
+ articleRes.setSendFriendsCircle(row.get("send_friends_circle", Integer.class));
|
|
|
+ articleRes.setAd(row.get("ad", Integer.class));
|
|
|
+ articleRes.setFrontCover(row.get("front_cover", String.class));
|
|
|
+ labelManagements.stream()
|
|
|
+ .filter(labelManagement -> Objects.nonNull(articleRes.getLabels())
|
|
|
+ && articleRes.getLabels().contains(labelManagement.name())).map(LabelManagement::classificationName).findFirst()
|
|
|
+ .ifPresent(articleRes::setClassificationName);
|
|
|
+ return articleRes;
|
|
|
+ }).all().collectList();
|
|
|
+ return Mono.zip(Mono.just(list.size()), listMono1).flatMap(objects -> {
|
|
|
+ Paged<ArticleRes> articleResPaged =
|
|
|
+ new Paged<>(Long.valueOf(objects.getT1()),
|
|
|
+ objects.getT2(), articleReq.getPage(), articleReq.getSize());
|
|
|
+ return Mono.just(articleResPaged);
|
|
|
+ });
|
|
|
+ });
|
|
|
+ });
|
|
|
+ return RStatus.successList(pagedMono);
|
|
|
+ }
|
|
|
+ @Override
|
|
|
+ public Mono<RStatus<Boolean>> articleUpdate(ArticleUpdate save) {
|
|
|
+ Mono<Boolean> mono1 = template.selectOne(Query.query(Criteria.where("id").is(save.getId())), Article.class)
|
|
|
+ .switchIfEmpty(Mono.error(new BaseException("id查询为空")))
|
|
|
+ .onErrorResume(throwable -> Mono.error(new BaseException("查询异常:" + throwable.getMessage())))
|
|
|
+ .flatMap(articleOld -> {
|
|
|
+ Article article = new Article(save.getId(),
|
|
|
+ articleOld.obtainId(),
|
|
|
+ save.getTitle(),
|
|
|
+ save.getDateTimeStr(),
|
|
|
+ save.getAuthor(),
|
|
|
+ save.getLabels(),
|
|
|
+ articleOld.audioLink(),
|
|
|
+ articleOld.audioName(),
|
|
|
+ articleOld.videoLink(),
|
|
|
+ articleOld.htmlContent(),
|
|
|
+ articleOld.createUser(),
|
|
|
+ articleOld.createId(),
|
|
|
+ articleOld.createdAt(),
|
|
|
+ ZonedDateTime.now(),
|
|
|
+ articleOld.deletedAt(),
|
|
|
+ articleOld.sendFriends(),
|
|
|
+ articleOld.sendFriendsCircle(),
|
|
|
+ articleOld.ad(),
|
|
|
+ save.getFrontCover(),
|
|
|
+ save.getType());
|
|
|
+ return template.update(article)
|
|
|
+ .onErrorResume(throwable -> Mono.error(new BaseException("保存失败:" + throwable.getMessage())))
|
|
|
+ .flatMap(classification1 -> Mono.just(true));
|
|
|
+ });
|
|
|
+ return RStatus.success(mono1);
|
|
|
+ }
|
|
|
+
|
|
|
+ @Override
|
|
|
+ public Mono<RStatus<Boolean>> articleUpdateSend(ArticleState save) {
|
|
|
+ Update updated_at = Update.update("updated_at", ZonedDateTime.now());
|
|
|
+ if (Objects.nonNull(save.getAd())) {
|
|
|
+ updated_at=updated_at.set("ad",save.getAd());
|
|
|
+ }
|
|
|
+ if (Objects.nonNull(save.getSendFriends())) {
|
|
|
+ updated_at=updated_at.set("send_friends",save.getSendFriends());
|
|
|
+ }
|
|
|
+ if (Objects.nonNull(save.getSendFriendsCircle())) {
|
|
|
+ updated_at=updated_at.set("send_friends_circle",save.getSendFriendsCircle());
|
|
|
+ }
|
|
|
+ Mono<Boolean> id = template.update(Query.query(Criteria.where("id").is(save.getId())), updated_at, Article.class)
|
|
|
+ .onErrorResume(throwable -> Mono.error(new BaseException("修改失败:" + throwable.getMessage()))).flatMap(aLong -> Mono.just(true));
|
|
|
+ return RStatus.success(id);
|
|
|
+ }
|
|
|
+
|
|
|
+ @Override
|
|
|
+ public Mono<RStatus<Boolean>> articleAdd(ArticleSave save) {
|
|
|
+ String userId = BaseContextHandler.getUserId();
|
|
|
+ String userName = BaseContextHandler.getUserName();
|
|
|
+ Article article = new Article(null,
|
|
|
+ null,
|
|
|
+ save.getTitle(),
|
|
|
+ save.getDateTimeStr(),
|
|
|
+ save.getAuthor(),
|
|
|
+ save.getLabels(),
|
|
|
+ save.getAudioLink(),
|
|
|
+ save.getAudioName(),
|
|
|
+ save.getVideoLink(),
|
|
|
+ save.getHtmlContent(),
|
|
|
+ userName,
|
|
|
+ userId,
|
|
|
+ ZonedDateTime.now(),
|
|
|
+ null,
|
|
|
+ null,
|
|
|
+ save.getSendFriends(),
|
|
|
+ save.getSendFriendsCircle(),
|
|
|
+ save.getAd(),
|
|
|
+ save.getFrontCover(),
|
|
|
+ save.getType());
|
|
|
+ Mono<Boolean> mono = template.insert(article)
|
|
|
+ .onErrorResume(throwable -> Mono.error(new BaseException("保存失败" + throwable.getMessage()))).flatMap(f -> Mono.just(true));
|
|
|
+ return RStatus.success(mono);
|
|
|
+ }
|
|
|
+
|
|
|
+ @Override
|
|
|
+ public Mono<RStatus<Paged<LabelManagementRes>>> getOutLableList(Long classificationId, Integer page, Integer size) {
|
|
|
+ ArrayList<Where> wheres = new ArrayList<>();
|
|
|
+ if (Objects.nonNull(classificationId) && 0!=classificationId) {
|
|
|
+ Where where = new Where();
|
|
|
+ where.setValue(classificationId);
|
|
|
+ where.setKey("classification_id");
|
|
|
+ where.setWhereType(1);
|
|
|
+ }
|
|
|
+ return sourceMaterialInterface.pageAndFunctionRtE(wheres, LabelManagement.class,size,page,labelManagements -> {
|
|
|
+ Mono<List<Classification>> listMono = Mono.just(List.of());
|
|
|
+ if (!labelManagements.isEmpty()) {
|
|
|
+ List<Long> collect = labelManagements.stream().map(LabelManagement::classificationId).collect(Collectors.toList());
|
|
|
+ if (!collect.isEmpty()) {
|
|
|
+ listMono = template.select(Query.query(Criteria.where("id").in(collect)).columns("id","name"), Classification.class).collectList();
|
|
|
+ }
|
|
|
+ }
|
|
|
+ return Mono.zip(Mono.just(labelManagements), listMono).flatMap(objects -> {
|
|
|
+ List<LabelManagement> t1 = objects.getT1();
|
|
|
+ List<Classification> t2 = objects.getT2();
|
|
|
+ List<LabelManagementRes> labelManagementRes1 = new ArrayList<>();
|
|
|
+ t1.stream().<Consumer<Classification>>map(labelManagement -> classification -> {
|
|
|
+ Long aLong = labelManagement.classificationId();
|
|
|
+ Long id = classification.id();
|
|
|
+ if (id.equals(aLong)) {
|
|
|
+ LabelManagementRes labelManagementRes = new LabelManagementRes();
|
|
|
+ labelManagementRes.setId(labelManagement.id());
|
|
|
+ labelManagementRes.setClassificationName(classification.name());
|
|
|
+ labelManagementRes.setIsTop(labelManagement.isTop());
|
|
|
+ labelManagementRes.setName(labelManagement.name());
|
|
|
+ labelManagementRes.setUrl(labelManagement.url());
|
|
|
+ labelManagementRes.setStIx(labelManagement.stIx());
|
|
|
+ labelManagementRes1.add(labelManagementRes);
|
|
|
+ }
|
|
|
+ }).forEach(t2::forEach);
|
|
|
+ List<LabelManagementRes> collect = labelManagementRes1.stream().sorted(Comparator.comparing(LabelManagementRes::getStIx)).collect(Collectors.toList());
|
|
|
+ return Mono.just(collect);
|
|
|
+ });
|
|
|
+ });
|
|
|
+ }
|
|
|
+
|
|
|
+ @Override
|
|
|
+ public Mono<RStatus<Paged<ArticleOutRes>>> byLableGetArticle(Long labelId, Integer page, Integer size) {
|
|
|
+ StringBuilder startSql = new StringBuilder();
|
|
|
+ startSql.append("SELECT id,front_cover,title FROM article WHERE deleted_at IS NULL ");
|
|
|
+ StringBuilder startSqlCount= new StringBuilder(" SELECT id FROM article WHERE deleted_at IS NULL ");
|
|
|
+ StringBuilder stringBuilder =new StringBuilder();
|
|
|
+ Criteria empty =Criteria.empty();
|
|
|
+
|
|
|
+ if(Objects.nonNull(labelId)){
|
|
|
+ empty = empty.and("id").is(labelId);
|
|
|
+ }
|
|
|
+ Mono<List<LabelManagement>> listMono;
|
|
|
+ if (!empty.isEmpty()) {
|
|
|
+ listMono = template.select(Query.query(empty), LabelManagement.class).collectList()
|
|
|
+ .onErrorResume(throwable -> Mono.error(new BaseException("查询标签异常::" + throwable.getMessage())));
|
|
|
+ }else {
|
|
|
+ listMono= Mono.just(List.of());
|
|
|
+ }
|
|
|
+ Mono<Paged<ArticleOutRes>> pagedMono = listMono.flatMap(labelManagements -> {
|
|
|
+ if (!labelManagements.isEmpty()) {
|
|
|
+ labelManagements.stream().map(LabelManagement::name).forEach(labelManagement -> {
|
|
|
+ stringBuilder.append(" OR labels like ");
|
|
|
+ stringBuilder.append("%").append(labelManagement).append("%");
|
|
|
+ });
|
|
|
+ }else {
|
|
|
+ Paged<ArticleOutRes> articleResPaged = new Paged<>(0,List.of(), page, size);
|
|
|
+ return Mono.just(articleResPaged);
|
|
|
+ }
|
|
|
+ StringBuilder stringBuilderPage = new StringBuilder();
|
|
|
+ stringBuilderPage.append(" LIMIT ");
|
|
|
+ stringBuilderPage.append(size);
|
|
|
+ stringBuilderPage.append(" OFFSET ");
|
|
|
+ stringBuilderPage.append((page - 1) * size);
|
|
|
+ Mono<List<Long>> listMono2 = template.getDatabaseClient().sql(startSqlCount.toString() + stringBuilder).map((row, rowMetadata) -> 1L).all().collectList();
|
|
|
+ return listMono2.flatMap(list -> {
|
|
|
+ Mono<List<ArticleOutRes>> listMono1 = template.getDatabaseClient().sql(startSql.toString()+stringBuilder+stringBuilderPage).map((row, rowMetadata) -> {
|
|
|
+ ArticleOutRes articleRes = new ArticleOutRes();
|
|
|
+ articleRes.setId(row.get("id", Long.class));
|
|
|
+ articleRes.setTitle(row.get("title", String.class));
|
|
|
+// articleRes.setDateTimeStr(row.get("date_time_str", String.class));
|
|
|
+// articleRes.setAuthor(row.get("author", String.class));
|
|
|
+// articleRes.setLabels(row.get("labels", String.class));
|
|
|
+// articleRes.setSendFriends(row.get("send_friends", Integer.class));
|
|
|
+// articleRes.setSendFriendsCircle(row.get("send_friends_circle", Integer.class));
|
|
|
+// articleRes.setAd(row.get("ad", Integer.class));
|
|
|
+ articleRes.setFrontCover(row.get("front_cover", String.class));
|
|
|
+// articleRes.setAudioLink(row.get("audio_link", String.class));
|
|
|
+// articleRes.setAudioName(row.get("audio_name", String.class));
|
|
|
+// articleRes.setVideoLink(row.get("video_link", String.class));
|
|
|
+// articleRes.setHtmlContent(row.get("html_content", String.class));
|
|
|
+// articleRes.setType(row.get("type", String.class));
|
|
|
+// labelManagements.stream()
|
|
|
+// .filter(labelManagement -> Objects.nonNull(articleRes.getLabels())
|
|
|
+// && articleRes.getLabels().contains(labelManagement.name())).map(LabelManagement::classificationName).findFirst()
|
|
|
+// .ifPresent(articleRes::setClassificationName);
|
|
|
+ return articleRes;
|
|
|
+ }).all().collectList();
|
|
|
+ return Mono.zip(Mono.just(list.size()), listMono1).flatMap(objects -> {
|
|
|
+ List<ArticleOutRes> collect = objects.getT2().stream().sorted(Comparator.comparing(ArticleOutRes::getDateTimeStr)).collect(Collectors.toList());
|
|
|
+ Paged<ArticleOutRes> articleResPaged =
|
|
|
+ new Paged<>(Long.valueOf(objects.getT1()),collect
|
|
|
+ , page, size);
|
|
|
+ return Mono.just(articleResPaged);
|
|
|
+ });
|
|
|
+ });
|
|
|
+ });
|
|
|
+ return RStatus.successList(pagedMono);
|
|
|
+ }
|
|
|
+
|
|
|
+ @Override
|
|
|
+ public Mono<RStatus<List<LabelManagementRes>>> byNamegetLable(String name) {
|
|
|
+ Mono<List<LabelManagementRes>> listMono = template.select(Query.query(Criteria.where("name").is(name)), LabelManagement.class)
|
|
|
+ .switchIfEmpty(Mono.error(new BaseException("查询内容不存在")))
|
|
|
+ .flatMap(labelManagement -> {
|
|
|
+ LabelManagementRes labelManagementRes = new LabelManagementRes();
|
|
|
+ labelManagementRes.setName(labelManagement.name());
|
|
|
+ labelManagementRes.setUrl(labelManagement.url());
|
|
|
+ labelManagementRes.setId(labelManagement.id());
|
|
|
+ labelManagementRes.setIsTop(labelManagement.isTop());
|
|
|
+ labelManagementRes.setStIx(labelManagement.stIx());
|
|
|
+ return Mono.just(labelManagementRes);
|
|
|
+ }).collectList().flatMap(labelManagementRes -> {
|
|
|
+ List<LabelManagementRes> collect = labelManagementRes.stream().sorted(Comparator.comparing(LabelManagementRes::getStIx)).collect(Collectors.toList());
|
|
|
+ return Mono.just(collect);
|
|
|
+ });
|
|
|
+ return RStatus.success(listMono);
|
|
|
+ }
|
|
|
+
|
|
|
+ @Override
|
|
|
+ public Mono<RStatus<List<ArticleUnlockStateRes>>> byUserIdGetUnlockState(String userId, Long articleId) {
|
|
|
+ Mono<List<ArticleUnlockStateRes>> listMono = template.selectOne(Query.query(Criteria.where("user_id").is(userId)), UserInfo.class)
|
|
|
+ .switchIfEmpty(template.insert(new UserInfo(null, userId, ZonedDateTime.now())))
|
|
|
+ .onErrorResume(throwable -> Mono.error(new BaseException("查询用户信息失败")))
|
|
|
+ .flatMap(userInfo -> {
|
|
|
+ Criteria user_info_id = Criteria.where("user_info_id").is(userInfo.id());
|
|
|
+ if (articleId != 0) {
|
|
|
+ user_info_id = user_info_id.and("article_id").is(articleId);
|
|
|
+ }
|
|
|
+ return template.select(Query
|
|
|
+ .query(user_info_id), ArticleUnlockState.class)
|
|
|
+ .collectList()
|
|
|
+ .switchIfEmpty(Mono.just(List.of()))
|
|
|
+ .onErrorResume(throwable -> Mono.error(new BaseException("查询或保存用户解锁文章状态失败")))
|
|
|
+ .flatMap(articleUnlockStates -> {
|
|
|
+ List<ArticleUnlockStateRes> collect = articleUnlockStates.stream().map(articleUnlockState -> {
|
|
|
+ ArticleUnlockStateRes articleUnlockStateRes = new ArticleUnlockStateRes();
|
|
|
+ articleUnlockStateRes.setArticleId(articleUnlockState.articleId());
|
|
|
+ return articleUnlockStateRes;
|
|
|
+ }).collect(Collectors.toList());
|
|
|
+ return Mono.just(collect);
|
|
|
+ });
|
|
|
+ }
|
|
|
+ );
|
|
|
+ return RStatus.success(listMono);
|
|
|
+ }
|
|
|
+
|
|
|
+ @Override
|
|
|
+ public Mono<RStatus<AppidDomainRes>> getdomain() {
|
|
|
+ Mono<AppidDomainRes> appidDomainResMono = template.select(AppidDomain.class).all().collectList().flatMap(appidDomains -> {
|
|
|
+ int i = RandomUtil.randomInt(0, appidDomains.size());
|
|
|
+ AppidDomain appidDomain = appidDomains.get(i);
|
|
|
+ AppidDomainRes appidDomainRes = new AppidDomainRes();
|
|
|
+ appidDomainRes.setAppid(appidDomain.appid());
|
|
|
+ appidDomainRes.setDomain(appidDomain.domain());
|
|
|
+ return Mono.just(appidDomainRes);
|
|
|
+ });
|
|
|
+ return RStatus.success(appidDomainResMono);
|
|
|
+ }
|
|
|
+
|
|
|
+ @Override
|
|
|
+ public Mono<RStatus<ArticleOutRes>> byArticleIdGet(Long articleId) {
|
|
|
+ Mono<ArticleOutRes> articleOutResMono = template.selectOne(Query.query(Criteria.where("id").is(articleId)), Article.class)
|
|
|
+ .switchIfEmpty(Mono.error(new BaseException("查询内容为空")))
|
|
|
+ .flatMap(article -> {
|
|
|
+ ArticleOutRes articleRes = new ArticleOutRes();
|
|
|
+ articleRes.setId(article.id());
|
|
|
+ articleRes.setTitle(article.title());
|
|
|
+ articleRes.setDateTimeStr(article.dateTimeStr());
|
|
|
+ articleRes.setAuthor(article.author());
|
|
|
+ articleRes.setLabels(article.labels());
|
|
|
+ articleRes.setSendFriends(article.sendFriends());
|
|
|
+ articleRes.setSendFriendsCircle(article.sendFriendsCircle());
|
|
|
+ articleRes.setAd(article.ad());
|
|
|
+ articleRes.setFrontCover(article.frontCover());
|
|
|
+ articleRes.setAudioLink(article.audioLink());
|
|
|
+ articleRes.setAudioName(article.audioName());
|
|
|
+ articleRes.setVideoLink(article.videoLink());
|
|
|
+ articleRes.setHtmlContent(article.htmlContent());
|
|
|
+ articleRes.setType(article.type());
|
|
|
+ return Mono.just(articleRes);
|
|
|
+ });
|
|
|
+ return RStatus.success(articleOutResMono);
|
|
|
+ }
|
|
|
+
|
|
|
+ @Override
|
|
|
+ public Mono<RStatus<ArticleOutRes>> randomNextPage() {
|
|
|
+ Mono<ArticleOutRes> articleMono = template.select(Query.query(Criteria.empty()).columns("id"), Article.class).collectList()
|
|
|
+ .flatMap(articles -> {
|
|
|
+ int i = RandomUtil.randomInt(0, articles.size());
|
|
|
+ Article article = articles.get(i);
|
|
|
+ Mono<Article> id = template.selectOne(Query.query(Criteria.where("id").is(article.id())), Article.class);
|
|
|
+ return id;
|
|
|
+ }).flatMap(article -> {
|
|
|
+ ArticleOutRes articleRes = new ArticleOutRes();
|
|
|
+ articleRes.setId(article.id());
|
|
|
+ articleRes.setTitle(article.title());
|
|
|
+ articleRes.setDateTimeStr(article.dateTimeStr());
|
|
|
+ articleRes.setAuthor(article.author());
|
|
|
+ articleRes.setLabels(article.labels());
|
|
|
+ articleRes.setSendFriends(article.sendFriends());
|
|
|
+ articleRes.setSendFriendsCircle(article.sendFriendsCircle());
|
|
|
+ articleRes.setAd(article.ad());
|
|
|
+ articleRes.setFrontCover(article.frontCover());
|
|
|
+ articleRes.setAudioLink(article.audioLink());
|
|
|
+ articleRes.setAudioName(article.audioName());
|
|
|
+ articleRes.setVideoLink(article.videoLink());
|
|
|
+ articleRes.setHtmlContent(article.htmlContent());
|
|
|
+ articleRes.setType(article.type());
|
|
|
+ return Mono.just(articleRes);
|
|
|
+ });
|
|
|
+ return RStatus.success(articleMono);
|
|
|
+ }
|
|
|
+
|
|
|
+ @Override
|
|
|
+ public Mono<RStatus<Paged<ArticleOutRes>>> byLableNameGetArticle(String name, Integer page, Integer size) {
|
|
|
+ StringBuilder startSql = new StringBuilder();
|
|
|
+ startSql.append("SELECT id,front_cover,title FROM article WHERE deleted_at IS NULL ");
|
|
|
+ StringBuilder startSqlCount= new StringBuilder(" SELECT id FROM article WHERE deleted_at IS NULL ");
|
|
|
+ StringBuilder stringBuilder =new StringBuilder();
|
|
|
+ Criteria empty =Criteria.empty();
|
|
|
+ if(Objects.nonNull(name)){
|
|
|
+ empty = empty.and("name").is(name);
|
|
|
+ }
|
|
|
+ Mono<List<LabelManagement>> listMono;
|
|
|
+ if (!empty.isEmpty()) {
|
|
|
+ listMono = template.select(Query.query(empty), LabelManagement.class).collectList()
|
|
|
+ .onErrorResume(throwable -> Mono.error(new BaseException("查询标签异常::" + throwable.getMessage())));
|
|
|
+ }else {
|
|
|
+ listMono= Mono.just(List.of());
|
|
|
+ }
|
|
|
+ Mono<Paged<ArticleOutRes>> pagedMono = listMono.flatMap(labelManagements -> {
|
|
|
+ if (!labelManagements.isEmpty()) {
|
|
|
+ labelManagements.stream().map(LabelManagement::name).forEach(labelManagement -> {
|
|
|
+ stringBuilder.append(" OR labels like ");
|
|
|
+ stringBuilder.append("%").append(labelManagement).append("%");
|
|
|
+ });
|
|
|
+ }else {
|
|
|
+ Paged<ArticleOutRes> articleResPaged =
|
|
|
+ new Paged<>(0,List.of()
|
|
|
+ , page, size);
|
|
|
+ return Mono.just(articleResPaged);
|
|
|
+ }
|
|
|
+ StringBuilder stringBuilderPage = new StringBuilder();
|
|
|
+ stringBuilderPage.append(" LIMIT ");
|
|
|
+ stringBuilderPage.append(size);
|
|
|
+ stringBuilderPage.append(" OFFSET ");
|
|
|
+ stringBuilderPage.append((page - 1) * size);
|
|
|
+ Mono<List<Long>> listMono2 = template.getDatabaseClient().sql(startSqlCount.toString() + stringBuilder).map((row, rowMetadata) -> 1L).all().collectList();
|
|
|
+ return listMono2.flatMap(list -> {
|
|
|
+ Mono<List<ArticleOutRes>> listMono1 = template.getDatabaseClient().sql(startSql.toString()+stringBuilder+stringBuilderPage).map((row, rowMetadata) -> {
|
|
|
+ ArticleOutRes articleRes = new ArticleOutRes();
|
|
|
+ articleRes.setId(row.get("id", Long.class));
|
|
|
+ articleRes.setTitle(row.get("title", String.class));
|
|
|
+// articleRes.setDateTimeStr(row.get("date_time_str", String.class));
|
|
|
+// articleRes.setAuthor(row.get("author", String.class));
|
|
|
+// articleRes.setLabels(row.get("labels", String.class));
|
|
|
+// articleRes.setSendFriends(row.get("send_friends", Integer.class));
|
|
|
+// articleRes.setSendFriendsCircle(row.get("send_friends_circle", Integer.class));
|
|
|
+// articleRes.setAd(row.get("ad", Integer.class));
|
|
|
+ articleRes.setFrontCover(row.get("front_cover", String.class));
|
|
|
+// articleRes.setAudioLink(row.get("audio_link", String.class));
|
|
|
+// articleRes.setAudioName(row.get("audio_name", String.class));
|
|
|
+// articleRes.setVideoLink(row.get("video_link", String.class));
|
|
|
+// articleRes.setHtmlContent(row.get("html_content", String.class));
|
|
|
+// articleRes.setType(row.get("type", String.class));
|
|
|
+// labelManagements.stream()
|
|
|
+// .filter(labelManagement -> Objects.nonNull(articleRes.getLabels())
|
|
|
+// && articleRes.getLabels().contains(labelManagement.name())).map(LabelManagement::classificationName).findFirst()
|
|
|
+// .ifPresent(articleRes::setClassificationName);
|
|
|
+ return articleRes;
|
|
|
+ }).all().collectList();
|
|
|
+ return Mono.zip(Mono.just(list.size()), listMono1).flatMap(objects -> {
|
|
|
+ List<ArticleOutRes> collect = objects.getT2().stream().sorted(Comparator.comparing(ArticleOutRes::getDateTimeStr)).collect(Collectors.toList());
|
|
|
+ Paged<ArticleOutRes> articleResPaged =
|
|
|
+ new Paged<>(Long.valueOf(objects.getT1()),collect
|
|
|
+ , page, size);
|
|
|
+ return Mono.just(articleResPaged);
|
|
|
+ });
|
|
|
+ });
|
|
|
+ });
|
|
|
+ return RStatus.successList(pagedMono);
|
|
|
+ }
|
|
|
+
|
|
|
+}
|