Просмотр исходного кода

对外接口-v0.01

(cherry picked from commit 6df12679dd4a5a5f1179a77b00cec6c7d4c68441)
MOKASZ\lw12420 2 лет назад
Родитель
Сommit
afed8e8592

+ 29 - 0
launch-admin/src/main/java/com/webflux/launchadmin/mysql/controller/otherServices/OtherServicesController.java

@@ -0,0 +1,29 @@
+package com.webflux.launchadmin.mysql.controller.otherServices;
+
+import com.webflux.launchadmin.mysql.controller.otherServices.res.ByCodeGetImportDomainRes;
+import com.webflux.launchadmin.mysql.controller.planNew.res.PlanNewAllRs;
+import com.webflux.launchadmin.mysql.service.otherServices.OtherServicesInterface;
+import com.webflux.launchcommon.returnObj.RStatus;
+import jakarta.annotation.Resource;
+import jakarta.validation.constraints.NotNull;
+import org.springframework.web.bind.annotation.*;
+import reactor.core.publisher.Mono;
+
+@RestController
+@RequestMapping("out/otherServices")
+public class OtherServicesController {
+
+    @Resource
+    private OtherServicesInterface otherServicesInterface;
+
+    /**
+     * 【ID1015681】
+     * 入口链接对外接口
+     * @param code
+     * @return {@link Mono}<{@link RStatus}<{@link PlanNewAllRs}>>
+     */
+    @GetMapping("byCodeGetImportDomain")
+    public Mono<RStatus<ByCodeGetImportDomainRes>> byCodeGetImportDomain(@RequestParam("code")  @NotNull String code){
+        return otherServicesInterface.byCodeGetImportDomain(code);
+    }
+}

+ 8 - 0
launch-admin/src/main/java/com/webflux/launchadmin/mysql/controller/otherServices/res/ByCodeGetImportDomainRes.java

@@ -0,0 +1,8 @@
+package com.webflux.launchadmin.mysql.controller.otherServices.res;
+
+import lombok.Data;
+
+@Data
+public class ByCodeGetImportDomainRes {
+    String domain;
+}

+ 152 - 0
launch-admin/src/main/java/com/webflux/launchadmin/mysql/service/otherServices/OtherServicesImpl.java

@@ -0,0 +1,152 @@
+package com.webflux.launchadmin.mysql.service.otherServices;
+
+import cn.hutool.core.util.IdUtil;
+import cn.hutool.core.util.RandomUtil;
+import cn.hutool.core.util.StrUtil;
+import com.webflux.launchadmin.global.BaseException;
+import com.webflux.launchadmin.mysql.controller.otherServices.res.ByCodeGetImportDomainRes;
+import com.webflux.launchadmin.mysql.controller.planNew.res.PlanNewAllRs;
+import com.webflux.launchadmin.mysql.entity.listening.ListeningPlanNew;
+import com.webflux.launchadmin.mysql.entity.planNew.DomainSelect;
+import com.webflux.launchadmin.mysql.entity.planNew.PlanNew;
+import com.webflux.launchadmin.mysql.service.planNew.structure.Item;
+import com.webflux.launchcommon.returnObj.RStatus;
+import jakarta.annotation.Resource;
+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.stereotype.Service;
+import reactor.core.publisher.Mono;
+
+import java.time.ZonedDateTime;
+import java.util.Objects;
+
+@Service
+public class OtherServicesImpl implements   OtherServicesInterface {
+
+    @Resource
+    private R2dbcEntityTemplate template;
+    @Override
+    public Mono<RStatus<ByCodeGetImportDomainRes>> byCodeGetImportDomain(String code) {
+        Query query = Query.query(Criteria.where("code").is(code));
+        Mono<Long> planNew = template.count(query, PlanNew.class)
+                .onErrorResume(throwable ->
+                        Mono.error(new Exception(StrUtil
+                                .format("通过code:{}查询是否裂变计划异常:::{}",
+                                        code,
+                                        throwable.getMessage()))));
+        Mono<Long> listeningPlan = template
+                .count(query, ListeningPlanNew.class)
+                .onErrorResume(throwable ->
+                        Mono.error(new Exception(StrUtil
+                                .format("通过code:{}查询是否听书计划异常:::{}",
+                                        code,
+                                        throwable.getMessage()))));
+        Mono<ByCodeGetImportDomainRes> stringBuilderMono1 = Mono
+                .zip(planNew, listeningPlan)
+                .flatMap(f -> {
+            Long t1 = f.getT1();
+            Long t2 = f.getT2();
+            Mono<ByCodeGetImportDomainRes> stringBuilderMono;
+            if (t1 > 0) {
+                stringBuilderMono = getNewPlan(code, query);
+            } else if (t2 > 0) {
+                stringBuilderMono = getNewListeningNewPlan(code, query);
+            } else {
+                throw new BaseException("code不存在");
+            }
+            return stringBuilderMono;
+        });
+        return RStatus.success(stringBuilderMono1);
+    }
+
+    private Mono<ByCodeGetImportDomainRes> getNewListeningNewPlan(String code, Query query) {
+        return template.selectOne(query, ListeningPlanNew.class)
+                .switchIfEmpty(Mono
+                        .error(new BaseException(StrUtil
+                                .format("通过code:{}查询裂变计划异常:::{}",
+                                        code,
+                                        "返回结构为空"))))
+                .onErrorResume(throwable ->
+                        Mono.error(new BaseException(StrUtil
+                                .format("通过code:{}查询裂变计划异常:::{}",
+                                        code,
+                                        throwable.getMessage()))))
+                .flatMap(ff -> {
+                    Long groupId = ff.groupId();
+                    if (Objects.isNull(groupId)) {
+                        return Mono.error(new BaseException("查询裂变计划 groupId 值为 null"));
+                    }
+                    return getDomainSetV(groupId, code);
+                });
+    }
+
+    private Mono<ByCodeGetImportDomainRes> getNewPlan(String code, Query query) {
+        return template.selectOne(query, PlanNew.class)
+                .switchIfEmpty(Mono
+                        .error(new BaseException(StrUtil
+                                .format("通过code:{}查询裂变计划异常:::{}",
+                                        code,
+                                        "返回结构为空"))))
+                .onErrorResume(throwable ->
+                        Mono.error(new BaseException(StrUtil
+                                .format("通过code:{}查询裂变计划异常:::{}",
+                                        code,
+                                        throwable.getMessage()))))
+                .flatMap(ff -> {
+                    String groupIdStr = ff.importDomain();
+                    if (Objects.isNull(groupIdStr)) {
+                        return Mono.error(new BaseException("查询听书计划入口域名类型值为 null"));
+                    }
+                    long groupId;
+                    try {
+                         groupId = Long.parseLong(groupIdStr);
+                    }catch (Exception e){
+                        return Mono.error(new BaseException("查询听书计划入口域名类型值解析错误"+e.getMessage()));
+                    }
+                    return getDomainSetV(groupId, code);
+                });
+    }
+
+    private Mono<ByCodeGetImportDomainRes> getDomainSetV( Long  groupId, String code) {
+        return template.select(Query.query(Criteria.where("group_type_id").is(groupId)
+                .and("status").is(1)
+                .and("type").is(1)
+                .and("deleted_at").isNull()),
+                DomainSelect.class)
+                .collectList()
+                .switchIfEmpty(Mono.error(new BaseException(StrUtil
+                        .format("通过code:{},groupId:{};查询域名为空",
+                                code,
+                                groupId))))
+                .onErrorResume(throwable ->
+                        Mono.error(new Exception(StrUtil
+                                .format("通过code:{},groupId:{};查询域名Error::{}",
+                                        code,
+                                        groupId,
+                                        throwable.getMessage()))))
+                .flatMap(f2 -> {
+                    StringBuilder stringBuilder = new StringBuilder();
+                    if(!f2.isEmpty()){
+                        DomainSelect domainSelect = f2.get(RandomUtil.randomInt(0, f2.size()));
+                        stringBuilder.append("http://");
+                        stringBuilder.append(get6Str());
+                        stringBuilder.append(".");
+                        stringBuilder.append(domainSelect.domain());
+                        stringBuilder.append("/index.html?code=");
+                        stringBuilder.append(code);
+                        stringBuilder.append("&sk=");
+                        stringBuilder.append(IdUtil.fastSimpleUUID());
+                    }else {
+                        return Mono.error(new BaseException(StrUtil.format("通过code:{},groupId:{};查询域名Error::{}",code,groupId,"错误")));
+                    }
+                    ByCodeGetImportDomainRes domain = new ByCodeGetImportDomainRes();
+                    domain.setDomain(stringBuilder.toString());
+                    return Mono.just(domain);
+                });
+    }
+    private String get6Str() {
+        return RandomUtil.randomString("abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ", 6);
+    }
+
+}

+ 10 - 0
launch-admin/src/main/java/com/webflux/launchadmin/mysql/service/otherServices/OtherServicesInterface.java

@@ -0,0 +1,10 @@
+package com.webflux.launchadmin.mysql.service.otherServices;
+
+import com.webflux.launchadmin.mysql.controller.otherServices.res.ByCodeGetImportDomainRes;
+import com.webflux.launchadmin.mysql.controller.planNew.res.PlanNewAllRs;
+import com.webflux.launchcommon.returnObj.RStatus;
+import reactor.core.publisher.Mono;
+
+public interface OtherServicesInterface {
+    Mono<RStatus<ByCodeGetImportDomainRes>> byCodeGetImportDomain(String code);
+}