|
1 | 1 | package io.github.llnancy.huaying.service;
|
2 | 2 |
|
| 3 | +import com.google.common.collect.Lists; |
3 | 4 | import io.github.llnancy.huaying.config.Constants;
|
4 |
| -import io.github.llnancy.mojian.base.exception.MjBaseBizException; |
| 5 | +import io.github.llnancy.mojian.base.exception.MoJianBaseBizException; |
5 | 6 | import lombok.RequiredArgsConstructor;
|
6 | 7 | import org.springframework.stereotype.Service;
|
7 | 8 | import org.springframework.util.CollectionUtils;
|
8 | 9 |
|
9 | 10 | import java.time.LocalDate;
|
10 | 11 | import java.time.temporal.ChronoUnit;
|
| 12 | +import java.util.Collections; |
11 | 13 | import java.util.List;
|
12 | 14 | import java.util.concurrent.ThreadLocalRandom;
|
13 | 15 | import java.util.function.Function;
|
|
22 | 24 | @RequiredArgsConstructor
|
23 | 25 | public class RandomService {
|
24 | 26 |
|
25 |
| - public String random(String category) { |
26 |
| - return doRandom(category, size -> ThreadLocalRandom.current().nextInt(size)); |
| 27 | + public List<String> random(String category, Integer count) { |
| 28 | + return doRandom(category, count, size -> ThreadLocalRandom.current().nextInt(size)); |
27 | 29 | }
|
28 | 30 |
|
29 |
| - public String today(String category) { |
30 |
| - return doRandom(category, size -> Math.toIntExact(ChronoUnit.DAYS.between(LocalDate.parse(Constants.INIT_DATE), LocalDate.now()) % size)); |
| 31 | + public List<String> today(String category, Integer count) { |
| 32 | + return doRandom(category, count, size -> Math.toIntExact(ChronoUnit.DAYS.between(LocalDate.parse(Constants.INIT_DATE), LocalDate.now()) % size)); |
31 | 33 | }
|
32 | 34 |
|
33 |
| - public String doRandom(String category, Function<Integer, Integer> function) { |
| 35 | + public List<String> doRandom(String category, Integer count, Function<Integer, Integer> function) { |
34 | 36 | if (Constants.DOMAINS.equals(category) || ResourcesServer.isEmpty()) {
|
35 |
| - throw new MjBaseBizException(Constants.RESOURCES_NOT_EXISTED); |
| 37 | + throw new MoJianBaseBizException(Constants.RESOURCES_NOT_EXISTED); |
36 | 38 | }
|
37 | 39 | List<String> resources = ResourcesServer.get(category);
|
38 | 40 | if (CollectionUtils.isEmpty(resources)) {
|
39 |
| - throw new MjBaseBizException(Constants.RESOURCES_NOT_EXISTED); |
| 41 | + throw new MoJianBaseBizException(Constants.RESOURCES_NOT_EXISTED); |
40 | 42 | }
|
41 |
| - int randomInt = function.apply(resources.size()); |
42 |
| - return resources.get(randomInt); |
| 43 | + int size = resources.size(); |
| 44 | + List<String> result; |
| 45 | + if (count == 1) { |
| 46 | + int randomInt = function.apply(size); |
| 47 | + result = Lists.newArrayList(resources.get(randomInt)); |
| 48 | + } else if (count < size) { |
| 49 | + Collections.shuffle(resources); |
| 50 | + result = resources.subList(0, count); |
| 51 | + } else { |
| 52 | + result = resources; |
| 53 | + } |
| 54 | + return result; |
43 | 55 | }
|
44 | 56 |
|
45 | 57 | public void flush() {
|
|
0 commit comments