|
|
@@ -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);
|
|
|
+ }
|
|
|
+
|
|
|
+}
|