|
@@ -0,0 +1,155 @@
|
|
|
|
|
+package com.webflux.launchadmin.mysql.service.groupUsers;
|
|
|
|
|
+
|
|
|
|
|
+import com.webflux.launchadmin.config.BaseContextHandler;
|
|
|
|
|
+import com.webflux.launchadmin.global.BaseException;
|
|
|
|
|
+import com.webflux.launchadmin.mysql.controller.groupUsers.res.DeleteRes;
|
|
|
|
|
+import com.webflux.launchadmin.mysql.controller.groupUsers.res.GroupUsersRes;
|
|
|
|
|
+import com.webflux.launchadmin.mysql.entity.groupUsers.Dto.GroupUsersDto;
|
|
|
|
|
+import com.webflux.launchadmin.mysql.entity.groupUsers.GroupUsers;
|
|
|
|
|
+import com.webflux.launchadmin.mysql.entity.planNew.GroupType;
|
|
|
|
|
+import com.webflux.launchadmin.mysql.entity.planNew.PlanNew;
|
|
|
|
|
+import com.webflux.launchadmin.mysql.repository.groupUsers.GroupUsersRepository;
|
|
|
|
|
+import com.webflux.launchcommon.returnObj.Paged;
|
|
|
|
|
+import com.webflux.launchcommon.returnObj.RStatus;
|
|
|
|
|
+import jakarta.annotation.Resource;
|
|
|
|
|
+import org.springframework.data.domain.*;
|
|
|
|
|
+import org.springframework.data.r2dbc.core.R2dbcEntityTemplate;
|
|
|
|
|
+import org.springframework.data.r2dbc.repository.R2dbcRepository;
|
|
|
|
|
+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.Flux;
|
|
|
|
|
+import reactor.core.publisher.Mono;
|
|
|
|
|
+
|
|
|
|
|
+import java.time.ZonedDateTime;
|
|
|
|
|
+import java.util.List;
|
|
|
|
|
+import java.util.Objects;
|
|
|
|
|
+import java.util.Optional;
|
|
|
|
|
+import java.util.stream.Collectors;
|
|
|
|
|
+
|
|
|
|
|
+@Service
|
|
|
|
|
+public class GroupUsersServiceImpl implements GroupUsersServiceInterface {
|
|
|
|
|
+ @Resource
|
|
|
|
|
+ private GroupUsersRepository groupUsersRepository;
|
|
|
|
|
+ @Resource
|
|
|
|
|
+ private R2dbcEntityTemplate template;
|
|
|
|
|
+
|
|
|
|
|
+ @Override
|
|
|
|
|
+ public Mono<RStatus<Paged<GroupUsersRes>>> page(Integer type, int page, int size) {
|
|
|
|
|
+ Criteria criteria = Criteria.empty();
|
|
|
|
|
+ if (Objects.nonNull(type) &&type>0) {
|
|
|
|
|
+ criteria = criteria.and("group_users_type").is(type);
|
|
|
|
|
+ }
|
|
|
|
|
+ Query query = Query.query(criteria);
|
|
|
|
|
+ int skip = (page - 1) *size;
|
|
|
|
|
+ int limit = size;
|
|
|
|
|
+ Mono<Long> count = template.count(query, GroupUsers.class);
|
|
|
|
|
+ Mono<List<GroupUsers>> created_at = template.select(Query.query(criteria)
|
|
|
|
|
+ .offset(skip)
|
|
|
|
|
+ .limit(limit)
|
|
|
|
|
+ .sort(Sort.by(Sort.Order.desc("created_at"))), GroupUsers.class)
|
|
|
|
|
+ .onErrorResume(throwable -> Mono.error(new BaseException("查询失败::error::" + throwable.getMessage())))
|
|
|
|
|
+ .collectList();
|
|
|
|
|
+ Mono<Paged<GroupUsersRes>> id1 = Mono.zip(count, created_at).flatMap(f -> {
|
|
|
|
|
+ List<GroupUsers> t2 = f.getT2();
|
|
|
|
|
+ List<Integer> collect = t2.stream()
|
|
|
|
|
+ .filter(f3->Objects.nonNull(f3.groupUsersType()))
|
|
|
|
|
+ .map(GroupUsers::groupUsersType)
|
|
|
|
|
+ .collect(Collectors.toList());
|
|
|
|
|
+ if (collect.isEmpty()) {
|
|
|
|
|
+ collect.add(0);
|
|
|
|
|
+ }
|
|
|
|
|
+ Mono<List<GroupType>> id = template.select(Query.query(Criteria.where("id").in(collect)), GroupType.class)
|
|
|
|
|
+ .collectList()
|
|
|
|
|
+ .onErrorResume(throwable -> Mono.error(new BaseException("查询groupType异常" + throwable.getMessage())));
|
|
|
|
|
+ return Mono.zip(id, Mono.just(f.getT1()), Mono.just(f.getT2())).flatMap(fff -> {
|
|
|
|
|
+ List<GroupUsers> t3 = fff.getT3();
|
|
|
|
|
+ Long t21 = fff.getT2();
|
|
|
|
|
+ List<GroupType> t1 = fff.getT1();
|
|
|
|
|
+ List<GroupUsersRes> collect1 = t3.stream().map(fm -> {
|
|
|
|
|
+ GroupUsersRes groupUsersRes = new GroupUsersRes();
|
|
|
|
|
+ groupUsersRes.setGroupUsersId(fm.groupUsersId());
|
|
|
|
|
+ groupUsersRes.setGroupUsersHead(fm.groupUsersHead());
|
|
|
|
|
+ groupUsersRes.setGroupUsersType(fm.groupUsersType());
|
|
|
|
|
+ groupUsersRes.setGroupUsersNickname(fm.groupUsersNickname());
|
|
|
|
|
+ groupUsersRes.setCreatedAt(fm.createdAt());
|
|
|
|
|
+ groupUsersRes.setCreateUser(fm.createUser());
|
|
|
|
|
+ return groupUsersRes;
|
|
|
|
|
+ }).collect(Collectors.toList());
|
|
|
|
|
+ collect1.stream().filter(f1 -> Objects.nonNull(f1.getGroupUsersType())).forEach(fe1 -> {
|
|
|
|
|
+ Optional<String> first = t1.stream()
|
|
|
|
|
+ .filter(f11 -> f11.id() == Long.valueOf(fe1.getGroupUsersType()))
|
|
|
|
|
+ .map(GroupType::name).findFirst();
|
|
|
|
|
+ first.ifPresent(fe1::setGroupUsersName);
|
|
|
|
|
+ });
|
|
|
|
|
+ Paged<GroupUsersRes> groupUsersPaged = new Paged<>(t21, collect1, page, size);
|
|
|
|
|
+ return Mono.just(groupUsersPaged);
|
|
|
|
|
+ });
|
|
|
|
|
+
|
|
|
|
|
+ });
|
|
|
|
|
+ return RStatus.successList(id1);
|
|
|
|
|
+ }
|
|
|
|
|
+
|
|
|
|
|
+ @Override
|
|
|
|
|
+ public Mono<RStatus<DeleteRes>> deletePosterById(Long[] groupUsersId) {
|
|
|
|
|
+ Mono<DeleteRes> deleteResMono = groupUsersRepository
|
|
|
|
|
+ .deleteAllById(List.of(groupUsersId))
|
|
|
|
|
+ .then(Mono.just(new DeleteRes(List.of(groupUsersId))))
|
|
|
|
|
+ .onErrorResume(throwable -> Mono.error(new BaseException("删除::error::" + throwable.getMessage())));
|
|
|
|
|
+ return RStatus.success(deleteResMono);
|
|
|
|
|
+ }
|
|
|
|
|
+
|
|
|
|
|
+ @Override
|
|
|
|
|
+ public Mono<RStatus<GroupUsers>> saveOrUpdate(GroupUsersDto groupUsersDto) {
|
|
|
|
|
+ ZonedDateTime now = ZonedDateTime.now();
|
|
|
|
|
+ if(Objects.isNull(groupUsersDto.getGroupUsersId())){
|
|
|
|
|
+ Mono<GroupUsers> insert = insertGroupUsersMono(groupUsersDto, BaseContextHandler.getUserName(), BaseContextHandler.getUserId(), now);
|
|
|
|
|
+ return RStatus.success(insert);
|
|
|
|
|
+ }else {
|
|
|
|
|
+ Mono<GroupUsers> groupUsersMono = updateGroupUsersMono(groupUsersDto, now);
|
|
|
|
|
+ return RStatus.success(groupUsersMono);
|
|
|
|
|
+ }
|
|
|
|
|
+ }
|
|
|
|
|
+
|
|
|
|
|
+ @Override
|
|
|
|
|
+ public Mono<RStatus<GroupUsers>> getById(Long groupUsersId) {
|
|
|
|
|
+ Mono<GroupUsers> groupUsersMono = groupUsersRepository
|
|
|
|
|
+ .findById(groupUsersId)
|
|
|
|
|
+ .switchIfEmpty( Mono.error(new BaseException("id异常 查询结果为空")))
|
|
|
|
|
+ .onErrorResume(throwable -> Mono.error(new BaseException("查询失败::" + throwable.getMessage())));
|
|
|
|
|
+ return RStatus.success(groupUsersMono);
|
|
|
|
|
+ }
|
|
|
|
|
+
|
|
|
|
|
+ private Mono<GroupUsers> updateGroupUsersMono(GroupUsersDto groupUsersDto, ZonedDateTime now) {
|
|
|
|
|
+ return groupUsersRepository.findById(groupUsersDto.getGroupUsersId())
|
|
|
|
|
+ .onErrorResume(throwable -> Mono.error(new BaseException("修改失败::" + throwable.getMessage())))
|
|
|
|
|
+ .flatMap(f -> {
|
|
|
|
|
+ GroupUsers groupUsers = new GroupUsers(groupUsersDto.getGroupUsersId(),
|
|
|
|
|
+ groupUsersDto.getGroupUsersHead(),
|
|
|
|
|
+ groupUsersDto.getGroupUsersNickname(),
|
|
|
|
|
+ f.createdAt(),
|
|
|
|
|
+ now,
|
|
|
|
|
+ groupUsersDto.getGroupUsersType(),
|
|
|
|
|
+ f.createUser(),
|
|
|
|
|
+ f.createId()
|
|
|
|
|
+ );
|
|
|
|
|
+ return groupUsersRepository.save(groupUsers)
|
|
|
|
|
+ .onErrorResume(throwable -> Mono.error(new BaseException("修改失败::" + throwable.getMessage())));
|
|
|
|
|
+ });
|
|
|
|
|
+ }
|
|
|
|
|
+
|
|
|
|
|
+ private Mono<GroupUsers> insertGroupUsersMono(GroupUsersDto groupUsersDto, String userName, String userId, ZonedDateTime now) {
|
|
|
|
|
+ GroupUsers groupUsers = new GroupUsers(groupUsersDto.getGroupUsersId(),
|
|
|
|
|
+ groupUsersDto.getGroupUsersHead(),
|
|
|
|
|
+ groupUsersDto.getGroupUsersNickname(),
|
|
|
|
|
+ now,
|
|
|
|
|
+ null,
|
|
|
|
|
+ groupUsersDto.getGroupUsersType(),
|
|
|
|
|
+ userName,
|
|
|
|
|
+ userId
|
|
|
|
|
+ );
|
|
|
|
|
+ return template.insert(groupUsers)
|
|
|
|
|
+ .onErrorResume(throwable -> Mono.error(new BaseException("插入失败::"+ throwable.getMessage())));
|
|
|
|
|
+ }
|
|
|
|
|
+}
|
|
|
|
|
+
|