Compare commits
6
Commits
| Author | SHA1 | Date | |
|---|---|---|---|
|
|
9adf8c5630 | ||
|
|
8bb7f63c62 | ||
|
|
b8b3fd867b | ||
|
|
e92ec118fe | ||
|
|
4c4b53cd69 | ||
|
|
d7f226659c |
No files matched your search
@@ -0,0 +1,30 @@
|
||||
# 2026-10-21 | 短剧保存免费集数同步分集
|
||||
|
||||
## 目标
|
||||
|
||||
后台短剧编辑改 `freeEpisodes` / `unlockCoin` 后,同步 `drama_episode.is_free` / `unlock_coin`,与定价页 `PriceService.save()` 规则一致。
|
||||
|
||||
## 方案
|
||||
|
||||
1. 抽出 `EpisodePriceSync.syncByDramaRule(dramaId, freeEpisodes, unlockCoin)`,供 `DramaService.update` 与 `PriceService.save` 共用。
|
||||
2. `DramaService.update` 在 `updateById` 后,仅当免费集数或解锁金币相对更新前有变化时调用同步。
|
||||
3. TDD:先写 `DramaServiceTest` 复现失败,再改实现转绿。
|
||||
|
||||
## 范围
|
||||
|
||||
- 仅 `fruitshorts-module-content`
|
||||
- 不改 Controller / VO / 契约 / 前端
|
||||
|
||||
## 门禁
|
||||
|
||||
```bash
|
||||
mvn -pl fruitshorts-module-content test -Dtest=DramaServiceTest
|
||||
mvn -pl fruitshorts-module-content test
|
||||
```
|
||||
|
||||
## 分支 / 提交
|
||||
|
||||
- `feature/drama-free-episodes-sync` ← `develop`
|
||||
- `test(content): 覆盖短剧免费集数同步分集价格`
|
||||
- `fix(content): 短剧保存免费集数时同步分集免费状态`
|
||||
- `merge --no-ff` 回 develop 并 push
|
||||
@@ -0,0 +1,24 @@
|
||||
# 2026-10-21 | 短剧保存始终同步分集价格(幂等纠偏)
|
||||
|
||||
## 背景
|
||||
|
||||
承接 K104。短剧 `free_episodes=9` 重存后,分集 5~9 仍收费。
|
||||
|
||||
## 根因
|
||||
|
||||
`DramaService.update()` 仅在 `freeEpisodes` / `unlockCoin` 相对更新前有变化时调用 `EpisodePriceSync`。历史数据已漂移时,同值重存不触发同步。
|
||||
|
||||
## 方案
|
||||
|
||||
1. 测试:覆盖「规则字段未变、分集已漂移」仍纠正。
|
||||
2. `DramaService.update()`:每次保存都调用 `EpisodePriceSync.syncByDramaRule(drama)`。
|
||||
3. `EpisodePriceSync`:仅对有差异的行走 `updateById`(已具备,保持)。
|
||||
|
||||
## 范围
|
||||
|
||||
只改 `fruitshorts-module-content` 的 `DramaService` / `EpisodePriceSync` / `DramaServiceTest`。不改 Controller / VO / 契约 / 前端。
|
||||
|
||||
## 提交
|
||||
|
||||
1. `test(content): 覆盖免费集数未变但分集漂移的修复场景`
|
||||
2. `fix(content): 短剧保存时始终同步分集价格`
|
||||
@@ -24571,3 +24571,80 @@ OK_EXIT=0
|
||||
解读:拦截生效,干净 message 正常通过 commitlint。
|
||||
|
||||
---
|
||||
|
||||
## 2026-10-21 | 短剧保存免费集数同步分集免费状态
|
||||
|
||||
【Codex】根因:DramaService.update 只更新 drama,未同步 drama_episode.is_free/unlock_coin。
|
||||
抽出 EpisodePriceSync.syncByDramaRule,DramaService.update 与 PriceService.save 共用;仅 freeEpisodes/unlockCoin 变化时同步。
|
||||
|
||||
### 门禁(原始输出摘录)
|
||||
|
||||
```bash
|
||||
$ mvn -pl fruitshorts-module-content -am test -Dsurefire.failIfNoSpecifiedTests=false
|
||||
[INFO] T E S T S
|
||||
[INFO] -------------------------------------------------------
|
||||
[INFO] Running com.fruitshorts.content.service.DramaServiceTest
|
||||
OpenJDK 64-Bit Server VM warning: Sharing is only supported for boot loader classes because bootstrap classpath has been appended
|
||||
WARNING: A Java agent has been loaded dynamically (/Users/jianghaifeng/.m2/repository/net/bytebuddy/byte-buddy-agent/1.14.13/byte-buddy-agent-1.14.13.jar)
|
||||
WARNING: If a serviceability tool is in use, please run with -XX:+EnableDynamicAgentLoading to hide this warning
|
||||
WARNING: If a serviceability tool is not in use, please run with -Djdk.instrument.traceUsage for more information
|
||||
WARNING: Dynamic loading of agents will be disallowed by default in a future release
|
||||
[INFO] Tests run: 4, Failures: 0, Errors: 0, Skipped: 0, Time elapsed: 1.126 s -- in com.fruitshorts.content.service.DramaServiceTest
|
||||
[INFO]
|
||||
[INFO] Results:
|
||||
[INFO]
|
||||
[INFO] Tests run: 4, Failures: 0, Errors: 0, Skipped: 0
|
||||
[INFO]
|
||||
[INFO] ------------------------------------------------------------------------
|
||||
[INFO] Reactor Summary for fruitshorts 1.0.0-SNAPSHOT:
|
||||
[INFO]
|
||||
[INFO] fruitshorts ........................................ SUCCESS [ 0.001 s]
|
||||
[INFO] fruitshorts-common ................................. SUCCESS [ 3.685 s]
|
||||
[INFO] fruitshorts-framework .............................. SUCCESS [ 31.492 s]
|
||||
[INFO] fruitshorts-module-system .......................... SUCCESS [ 0.051 s]
|
||||
[INFO] fruitshorts-module-content ......................... SUCCESS [ 4.451 s]
|
||||
[INFO] ------------------------------------------------------------------------
|
||||
[INFO] BUILD SUCCESS
|
||||
[INFO] ------------------------------------------------------------------------
|
||||
[INFO] Total time: 39.944 s
|
||||
[INFO] Finished at: 2026-10-21T22:35:43+08:00
|
||||
[INFO] ------------------------------------------------------------------------
|
||||
```
|
||||
|
||||
解读:DramaServiceTest 4 条全过;module-content BUILD SUCCESS。
|
||||
|
||||
### 提交 / 合并 / push
|
||||
|
||||
```bash
|
||||
$ git log -2 --format=%B
|
||||
fix(content): 短剧保存免费集数时同步分集免费状态
|
||||
|
||||
test(content): 覆盖短剧免费集数同步分集价格
|
||||
|
||||
$ git log --oneline -4
|
||||
e92ec11 merge: 短剧保存免费集数同步分集免费状态
|
||||
4c4b53c fix(content): 短剧保存免费集数时同步分集免费状态
|
||||
d7f2266 test(content): 覆盖短剧免费集数同步分集价格
|
||||
979e6eb docs(plans): 登记 K100 提交 AI 痕迹已知偏差
|
||||
|
||||
$ git push origin feature/drama-free-episodes-sync
|
||||
$ git push origin develop
|
||||
# develop 979e6eb..e92ec11
|
||||
```
|
||||
|
||||
解读:作者 Zhou Weidong;无 Co-authored-by / AI 痕迹;develop 与 feature 均已 push。未启动/重启 8080/8081。停住等验收。
|
||||
|
||||
---
|
||||
|
||||
## 2026-10-21 短剧保存始终同步分集价格
|
||||
|
||||
计划:`docs/plans/2026-10-21-drama-price-sync-idempotent.md`。
|
||||
同值重存也调用 `EpisodePriceSync.syncByDramaRule(drama)`,只更新有差异的分集。
|
||||
|
||||
```
|
||||
$ mvn -pl fruitshorts-module-content -am test -Dtest=DramaServiceTest -Dsurefire.failIfNoSpecifiedTests=false
|
||||
[INFO] Tests run: 5, Failures: 0, Errors: 0, Skipped: 0, Time elapsed: 1.184 s -- in com.fruitshorts.content.service.DramaServiceTest
|
||||
[INFO] BUILD SUCCESS
|
||||
```
|
||||
|
||||
解读:新增「规则未变但分集漂移」用例;原「已对齐则不写库」仍通过,但会查询分集。
|
||||
@@ -25,5 +25,10 @@
|
||||
<groupId>com.fruitshorts</groupId>
|
||||
<artifactId>fruitshorts-module-system</artifactId>
|
||||
</dependency>
|
||||
<dependency>
|
||||
<groupId>org.springframework.boot</groupId>
|
||||
<artifactId>spring-boot-starter-test</artifactId>
|
||||
<scope>test</scope>
|
||||
</dependency>
|
||||
</dependencies>
|
||||
</project>
|
||||
+2
@@ -42,6 +42,7 @@ public class DramaService {
|
||||
private final DramaEpisodeMapper dramaEpisodeMapper;
|
||||
private final DramaReviewRecordMapper dramaReviewRecordMapper;
|
||||
private final DramaCategoryService dramaCategoryService;
|
||||
private final EpisodePriceSync episodePriceSync;
|
||||
|
||||
public PageResult<DramaRespVO> page(DramaPageReqVO req) {
|
||||
List<String> dramaTypes = DramaMultiTagSupport.normalizeDramaTypes(req.getDramaTypes());
|
||||
@@ -120,6 +121,7 @@ public class DramaService {
|
||||
drama.setViewCount(nonNegative(req.getHeat(), "热度"));
|
||||
}
|
||||
dramaMapper.updateById(drama);
|
||||
episodePriceSync.syncByDramaRule(drama);
|
||||
}
|
||||
|
||||
@Transactional(rollbackFor = Exception.class)
|
||||
|
||||
+74
@@ -0,0 +1,74 @@
|
||||
// Author: fruit.com.cn
|
||||
|
||||
package com.fruitshorts.content.service;
|
||||
|
||||
import com.baomidou.mybatisplus.core.toolkit.Wrappers;
|
||||
import com.fruitshorts.content.dal.entity.Drama;
|
||||
import com.fruitshorts.content.dal.entity.DramaEpisode;
|
||||
import com.fruitshorts.content.dal.mapper.DramaEpisodeMapper;
|
||||
import lombok.RequiredArgsConstructor;
|
||||
import org.springframework.stereotype.Component;
|
||||
|
||||
import java.util.List;
|
||||
import java.util.Objects;
|
||||
|
||||
/**
|
||||
* 按短剧免费集数规则同步分集 {@code is_free} / {@code unlock_coin}。
|
||||
*
|
||||
* <p>定价页与短剧编辑共用同一套规则,避免两处口径漂移。</p>
|
||||
*/
|
||||
@Component
|
||||
@RequiredArgsConstructor
|
||||
public class EpisodePriceSync {
|
||||
|
||||
private final DramaEpisodeMapper dramaEpisodeMapper;
|
||||
|
||||
/**
|
||||
* 按短剧当前免费集数与解锁金币同步全部分集。
|
||||
*
|
||||
* @return 实际写库的分集数
|
||||
*/
|
||||
public int syncByDramaRule(Drama drama) {
|
||||
int freeEpisodes = drama.getFreeEpisodes() == null ? 0 : Math.max(0, drama.getFreeEpisodes());
|
||||
int unlockCoin = drama.getUnlockCoin() == null ? 0 : Math.max(0, drama.getUnlockCoin());
|
||||
return syncByDramaRule(drama.getId(), freeEpisodes, unlockCoin);
|
||||
}
|
||||
|
||||
/**
|
||||
* 全量同步某剧全部分集价格。
|
||||
*
|
||||
* <p>规则:{@code epNo > 0 && epNo <= freeEpisodes} 为免费且 coin=0,
|
||||
* 否则收费且 coin={@code unlockCoin}。</p>
|
||||
*
|
||||
* @return 实际写库的分集数
|
||||
*/
|
||||
public int syncByDramaRule(Long dramaId, int freeEpisodes, int unlockCoin) {
|
||||
List<DramaEpisode> episodes = dramaEpisodeMapper.selectList(Wrappers.<DramaEpisode>lambdaQuery()
|
||||
.eq(DramaEpisode::getDramaId, dramaId)
|
||||
.orderByAsc(DramaEpisode::getEpNo));
|
||||
int changed = 0;
|
||||
for (DramaEpisode episode : episodes) {
|
||||
int epNo = episode.getEpNo() == null ? 0 : episode.getEpNo();
|
||||
int isFree = resolveIsFree(epNo, freeEpisodes);
|
||||
int coin = resolveUnlockCoin(isFree, unlockCoin);
|
||||
if (!Objects.equals(episode.getIsFree(), isFree)
|
||||
|| !Objects.equals(episode.getUnlockCoin(), coin)) {
|
||||
episode.setIsFree(isFree);
|
||||
episode.setUnlockCoin(coin);
|
||||
dramaEpisodeMapper.updateById(episode);
|
||||
changed++;
|
||||
}
|
||||
}
|
||||
return changed;
|
||||
}
|
||||
|
||||
/** 集号落在免费区间内则为免费。 */
|
||||
public static int resolveIsFree(int epNo, int freeEpisodes) {
|
||||
return epNo > 0 && epNo <= freeEpisodes ? 1 : 0;
|
||||
}
|
||||
|
||||
/** 免费集 coin 置 0,收费集用短剧解锁金币。 */
|
||||
public static int resolveUnlockCoin(int isFree, int unlockCoin) {
|
||||
return isFree == 1 ? 0 : unlockCoin;
|
||||
}
|
||||
}
|
||||
+56
-45
@@ -32,6 +32,7 @@ public class PriceService {
|
||||
private final DramaService dramaService;
|
||||
private final DramaMapper dramaMapper;
|
||||
private final DramaEpisodeMapper dramaEpisodeMapper;
|
||||
private final EpisodePriceSync episodePriceSync;
|
||||
|
||||
/** 读取当前价格配置:后台定价页进入时先看现状再改。 */
|
||||
public PriceRespVO detail(Long dramaId) {
|
||||
@@ -72,55 +73,13 @@ public class PriceService {
|
||||
drama.setUnlockCoin(unlockCoin);
|
||||
dramaMapper.updateById(drama);
|
||||
|
||||
int changed = episodePriceSync.syncByDramaRule(drama.getId(), freeEpisodes, unlockCoin);
|
||||
changed += applyEpisodeOverrides(drama.getId(), unlockCoin, req);
|
||||
|
||||
List<DramaEpisode> episodes = dramaEpisodeMapper.selectList(Wrappers.<DramaEpisode>lambdaQuery()
|
||||
.eq(DramaEpisode::getDramaId, drama.getId())
|
||||
.orderByAsc(DramaEpisode::getEpNo));
|
||||
|
||||
Map<Long, PriceSaveReqVO.EpisodePrice> overrides = new LinkedHashMap<>();
|
||||
if (req.getEpisodes() != null) {
|
||||
for (PriceSaveReqVO.EpisodePrice item : req.getEpisodes()) {
|
||||
if (item != null && item.getEpisodeId() != null) {
|
||||
overrides.put(item.getEpisodeId(), item);
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
int changed = 0;
|
||||
for (DramaEpisode episode : episodes) {
|
||||
int epNo = episode.getEpNo() == null ? 0 : episode.getEpNo();
|
||||
int isFree = epNo > 0 && epNo <= freeEpisodes ? 1 : 0;
|
||||
int coin = isFree == 1 ? 0 : unlockCoin;
|
||||
|
||||
PriceSaveReqVO.EpisodePrice override = overrides.get(episode.getId());
|
||||
Integer previewSeconds = null;
|
||||
if (override != null) {
|
||||
if (override.getIsFree() != null) {
|
||||
isFree = override.getIsFree() == 1 ? 1 : 0;
|
||||
coin = isFree == 1 ? 0 : unlockCoin;
|
||||
}
|
||||
if (isFree == 0 && override.getUnlockCoin() != null) {
|
||||
coin = Math.max(0, override.getUnlockCoin());
|
||||
}
|
||||
if (override.getPreviewSeconds() != null) {
|
||||
previewSeconds = Math.max(0, override.getPreviewSeconds());
|
||||
}
|
||||
}
|
||||
|
||||
boolean priceChanged = !Objects.equals(episode.getIsFree(), isFree)
|
||||
|| !Objects.equals(episode.getUnlockCoin(), coin);
|
||||
boolean previewChanged = previewSeconds != null
|
||||
&& !Objects.equals(episode.getPreviewSeconds(), previewSeconds);
|
||||
if (priceChanged || previewChanged) {
|
||||
episode.setIsFree(isFree);
|
||||
episode.setUnlockCoin(coin);
|
||||
if (previewSeconds != null) {
|
||||
episode.setPreviewSeconds(previewSeconds);
|
||||
}
|
||||
dramaEpisodeMapper.updateById(episode);
|
||||
changed++;
|
||||
}
|
||||
}
|
||||
|
||||
PriceRespVO vo = new PriceRespVO();
|
||||
vo.setDramaId(drama.getId());
|
||||
vo.setFreeEpisodes(freeEpisodes);
|
||||
@@ -139,4 +98,56 @@ public class PriceService {
|
||||
}).toList());
|
||||
return vo;
|
||||
}
|
||||
|
||||
/** 在整剧规则同步后再落单集覆盖(含试看秒数)。 */
|
||||
private int applyEpisodeOverrides(Long dramaId, int unlockCoin, PriceSaveReqVO req) {
|
||||
Map<Long, PriceSaveReqVO.EpisodePrice> overrides = new LinkedHashMap<>();
|
||||
if (req.getEpisodes() != null) {
|
||||
for (PriceSaveReqVO.EpisodePrice item : req.getEpisodes()) {
|
||||
if (item != null && item.getEpisodeId() != null) {
|
||||
overrides.put(item.getEpisodeId(), item);
|
||||
}
|
||||
}
|
||||
}
|
||||
if (overrides.isEmpty()) {
|
||||
return 0;
|
||||
}
|
||||
List<DramaEpisode> episodes = dramaEpisodeMapper.selectList(Wrappers.<DramaEpisode>lambdaQuery()
|
||||
.eq(DramaEpisode::getDramaId, dramaId)
|
||||
.orderByAsc(DramaEpisode::getEpNo));
|
||||
int changed = 0;
|
||||
for (DramaEpisode episode : episodes) {
|
||||
PriceSaveReqVO.EpisodePrice override = overrides.get(episode.getId());
|
||||
if (override == null) {
|
||||
continue;
|
||||
}
|
||||
int isFree = episode.getIsFree() == null ? 0 : episode.getIsFree();
|
||||
int coin = episode.getUnlockCoin() == null ? 0 : episode.getUnlockCoin();
|
||||
Integer previewSeconds = null;
|
||||
if (override.getIsFree() != null) {
|
||||
isFree = override.getIsFree() == 1 ? 1 : 0;
|
||||
coin = EpisodePriceSync.resolveUnlockCoin(isFree, unlockCoin);
|
||||
}
|
||||
if (isFree == 0 && override.getUnlockCoin() != null) {
|
||||
coin = Math.max(0, override.getUnlockCoin());
|
||||
}
|
||||
if (override.getPreviewSeconds() != null) {
|
||||
previewSeconds = Math.max(0, override.getPreviewSeconds());
|
||||
}
|
||||
boolean priceChanged = !Objects.equals(episode.getIsFree(), isFree)
|
||||
|| !Objects.equals(episode.getUnlockCoin(), coin);
|
||||
boolean previewChanged = previewSeconds != null
|
||||
&& !Objects.equals(episode.getPreviewSeconds(), previewSeconds);
|
||||
if (priceChanged || previewChanged) {
|
||||
episode.setIsFree(isFree);
|
||||
episode.setUnlockCoin(coin);
|
||||
if (previewSeconds != null) {
|
||||
episode.setPreviewSeconds(previewSeconds);
|
||||
}
|
||||
dramaEpisodeMapper.updateById(episode);
|
||||
changed++;
|
||||
}
|
||||
}
|
||||
return changed;
|
||||
}
|
||||
}
|
||||
+210
@@ -0,0 +1,210 @@
|
||||
// Author: fruit.com.cn
|
||||
|
||||
package com.fruitshorts.content.service;
|
||||
|
||||
import com.baomidou.mybatisplus.core.MybatisConfiguration;
|
||||
import com.baomidou.mybatisplus.core.metadata.TableInfoHelper;
|
||||
import com.fruitshorts.content.dal.entity.Drama;
|
||||
import com.fruitshorts.content.dal.entity.DramaEpisode;
|
||||
import com.fruitshorts.content.dal.mapper.DramaEpisodeMapper;
|
||||
import com.fruitshorts.content.dal.mapper.DramaMapper;
|
||||
import com.fruitshorts.content.dal.mapper.DramaReviewRecordMapper;
|
||||
import com.fruitshorts.content.vo.DramaSaveReqVO;
|
||||
import org.apache.ibatis.builder.MapperBuilderAssistant;
|
||||
import org.junit.jupiter.api.BeforeAll;
|
||||
import org.junit.jupiter.api.BeforeEach;
|
||||
import org.junit.jupiter.api.Test;
|
||||
import org.mockito.ArgumentCaptor;
|
||||
|
||||
import java.util.ArrayList;
|
||||
import java.util.List;
|
||||
import java.util.Map;
|
||||
import java.util.concurrent.atomic.AtomicInteger;
|
||||
import java.util.function.Function;
|
||||
import java.util.stream.Collectors;
|
||||
|
||||
import static org.assertj.core.api.Assertions.assertThat;
|
||||
import static org.assertj.core.api.Assertions.assertThatCode;
|
||||
import static org.mockito.ArgumentMatchers.any;
|
||||
import static org.mockito.BDDMockito.given;
|
||||
import static org.mockito.Mockito.mock;
|
||||
import static org.mockito.Mockito.never;
|
||||
import static org.mockito.Mockito.times;
|
||||
import static org.mockito.Mockito.verify;
|
||||
|
||||
/**
|
||||
* 短剧保存时免费集数 / 解锁金币须同步分集价格。
|
||||
*/
|
||||
class DramaServiceTest {
|
||||
|
||||
private static final long DRAMA_ID = 1001L;
|
||||
private static final int UNLOCK_COIN = 60;
|
||||
|
||||
private DramaMapper dramaMapper;
|
||||
private DramaEpisodeMapper dramaEpisodeMapper;
|
||||
private DramaService dramaService;
|
||||
private List<DramaEpisode> episodes;
|
||||
|
||||
@BeforeAll
|
||||
static void initTableInfoCache() {
|
||||
MapperBuilderAssistant assistant = new MapperBuilderAssistant(new MybatisConfiguration(), "");
|
||||
TableInfoHelper.initTableInfo(assistant, Drama.class);
|
||||
TableInfoHelper.initTableInfo(assistant, DramaEpisode.class);
|
||||
}
|
||||
|
||||
@BeforeEach
|
||||
void setUp() {
|
||||
dramaMapper = mock(DramaMapper.class);
|
||||
dramaEpisodeMapper = mock(DramaEpisodeMapper.class);
|
||||
DramaReviewRecordMapper reviewRecordMapper = mock(DramaReviewRecordMapper.class);
|
||||
DramaCategoryService categoryService = mock(DramaCategoryService.class);
|
||||
EpisodePriceSync episodePriceSync = new EpisodePriceSync(dramaEpisodeMapper);
|
||||
dramaService = new DramaService(
|
||||
dramaMapper, dramaEpisodeMapper, reviewRecordMapper, categoryService, episodePriceSync);
|
||||
episodes = new ArrayList<>();
|
||||
given(dramaEpisodeMapper.selectList(any())).willAnswer(invocation -> List.copyOf(episodes));
|
||||
given(dramaEpisodeMapper.updateById(any(DramaEpisode.class))).willAnswer(invocation -> {
|
||||
DramaEpisode updated = invocation.getArgument(0);
|
||||
for (int i = 0; i < episodes.size(); i++) {
|
||||
if (episodes.get(i).getId().equals(updated.getId())) {
|
||||
episodes.set(i, updated);
|
||||
break;
|
||||
}
|
||||
}
|
||||
return 1;
|
||||
});
|
||||
}
|
||||
|
||||
@Test
|
||||
void should_mark_episodes_4_and_5_free_when_free_episodes_increase_from_3_to_5() {
|
||||
givenExistingDrama(3, UNLOCK_COIN);
|
||||
seedEpisodesAlignedToFree(3, 6);
|
||||
|
||||
dramaService.update(DRAMA_ID, saveReq(5, UNLOCK_COIN));
|
||||
|
||||
Map<Integer, DramaEpisode> byEpNo = episodeMap();
|
||||
assertThat(byEpNo.get(4).getIsFree()).isEqualTo(1);
|
||||
assertThat(byEpNo.get(4).getUnlockCoin()).isZero();
|
||||
assertThat(byEpNo.get(5).getIsFree()).isEqualTo(1);
|
||||
assertThat(byEpNo.get(5).getUnlockCoin()).isZero();
|
||||
assertThat(byEpNo.get(6).getIsFree()).isZero();
|
||||
assertThat(byEpNo.get(6).getUnlockCoin()).isEqualTo(UNLOCK_COIN);
|
||||
verify(dramaEpisodeMapper, times(2)).updateById(any(DramaEpisode.class));
|
||||
}
|
||||
|
||||
@Test
|
||||
void should_mark_episodes_3_to_5_paid_when_free_episodes_decrease_from_5_to_2() {
|
||||
givenExistingDrama(5, UNLOCK_COIN);
|
||||
seedEpisodesAlignedToFree(5, 5);
|
||||
|
||||
dramaService.update(DRAMA_ID, saveReq(2, UNLOCK_COIN));
|
||||
|
||||
Map<Integer, DramaEpisode> byEpNo = episodeMap();
|
||||
assertThat(byEpNo.get(3).getIsFree()).isZero();
|
||||
assertThat(byEpNo.get(3).getUnlockCoin()).isEqualTo(UNLOCK_COIN);
|
||||
assertThat(byEpNo.get(4).getIsFree()).isZero();
|
||||
assertThat(byEpNo.get(4).getUnlockCoin()).isEqualTo(UNLOCK_COIN);
|
||||
assertThat(byEpNo.get(5).getIsFree()).isZero();
|
||||
assertThat(byEpNo.get(5).getUnlockCoin()).isEqualTo(UNLOCK_COIN);
|
||||
verify(dramaEpisodeMapper, times(3)).updateById(any(DramaEpisode.class));
|
||||
}
|
||||
|
||||
@Test
|
||||
void should_skip_episode_updates_when_free_episodes_unchanged() {
|
||||
givenExistingDrama(3, UNLOCK_COIN);
|
||||
seedEpisodesAlignedToFree(3, 4);
|
||||
AtomicInteger episodeUpdates = new AtomicInteger();
|
||||
given(dramaEpisodeMapper.updateById(any(DramaEpisode.class))).willAnswer(invocation -> {
|
||||
episodeUpdates.incrementAndGet();
|
||||
return 1;
|
||||
});
|
||||
|
||||
dramaService.update(DRAMA_ID, saveReq(3, UNLOCK_COIN));
|
||||
|
||||
assertThat(episodeUpdates.get()).isZero();
|
||||
verify(dramaEpisodeMapper, times(1)).selectList(any());
|
||||
verify(dramaEpisodeMapper, never()).updateById(any(DramaEpisode.class));
|
||||
}
|
||||
|
||||
/**
|
||||
* 历史漂移:短剧 freeEpisodes/unlockCoin 已是目标值,但分集 is_free 仍是旧规则。
|
||||
* 同值重存必须纠偏,不能因「规则字段未变」短路跳过同步。
|
||||
*/
|
||||
@Test
|
||||
void should_correct_drifted_episode_is_free_when_free_episodes_and_unlock_coin_unchanged() {
|
||||
givenExistingDrama(9, UNLOCK_COIN);
|
||||
// 规则已是免费 9 集,分集仍按旧规则只免费到第 4 集(5~9 仍收费)
|
||||
seedEpisodesAlignedToFree(4, 9);
|
||||
|
||||
dramaService.update(DRAMA_ID, saveReq(9, UNLOCK_COIN));
|
||||
|
||||
Map<Integer, DramaEpisode> byEpNo = episodeMap();
|
||||
for (int epNo = 1; epNo <= 9; epNo++) {
|
||||
assertThat(byEpNo.get(epNo).getIsFree()).isEqualTo(1);
|
||||
assertThat(byEpNo.get(epNo).getUnlockCoin()).isZero();
|
||||
}
|
||||
verify(dramaEpisodeMapper, times(5)).updateById(any(DramaEpisode.class));
|
||||
}
|
||||
|
||||
@Test
|
||||
void should_not_fail_when_drama_has_no_episodes() {
|
||||
givenExistingDrama(3, UNLOCK_COIN);
|
||||
episodes.clear();
|
||||
|
||||
assertThatCode(() -> dramaService.update(DRAMA_ID, saveReq(5, UNLOCK_COIN)))
|
||||
.doesNotThrowAnyException();
|
||||
verify(dramaEpisodeMapper, never()).updateById(any(DramaEpisode.class));
|
||||
}
|
||||
|
||||
private void givenExistingDrama(int freeEpisodes, int unlockCoin) {
|
||||
Drama drama = new Drama();
|
||||
drama.setId(DRAMA_ID);
|
||||
drama.setTitle("测试短剧");
|
||||
drama.setFreeEpisodes(freeEpisodes);
|
||||
drama.setUnlockCoin(unlockCoin);
|
||||
drama.setSubtitle("");
|
||||
drama.setGenre("");
|
||||
drama.setSynopsis("");
|
||||
drama.setCoverUrl("");
|
||||
drama.setTrailerUrl("");
|
||||
drama.setLanguage("id-ID");
|
||||
drama.setIsHot(0);
|
||||
drama.setSort(0);
|
||||
given(dramaMapper.selectById(DRAMA_ID)).willReturn(drama);
|
||||
given(dramaMapper.updateById(any(Drama.class))).willReturn(1);
|
||||
}
|
||||
|
||||
private void seedEpisodesAlignedToFree(int freeEpisodes, int total) {
|
||||
episodes.clear();
|
||||
for (int epNo = 1; epNo <= total; epNo++) {
|
||||
DramaEpisode episode = new DramaEpisode();
|
||||
episode.setId((long) epNo);
|
||||
episode.setDramaId(DRAMA_ID);
|
||||
episode.setEpNo(epNo);
|
||||
episode.setTitle("第" + epNo + "集");
|
||||
boolean free = epNo <= freeEpisodes;
|
||||
episode.setIsFree(free ? 1 : 0);
|
||||
episode.setUnlockCoin(free ? 0 : UNLOCK_COIN);
|
||||
episodes.add(episode);
|
||||
}
|
||||
}
|
||||
|
||||
private DramaSaveReqVO saveReq(int freeEpisodes, int unlockCoin) {
|
||||
DramaSaveReqVO req = new DramaSaveReqVO();
|
||||
req.setTitle("测试短剧");
|
||||
req.setFreeEpisodes(freeEpisodes);
|
||||
req.setUnlockCoin(unlockCoin);
|
||||
return req;
|
||||
}
|
||||
|
||||
private Map<Integer, DramaEpisode> episodeMap() {
|
||||
ArgumentCaptor<DramaEpisode> captor = ArgumentCaptor.forClass(DramaEpisode.class);
|
||||
verify(dramaEpisodeMapper, org.mockito.Mockito.atLeastOnce()).updateById(captor.capture());
|
||||
Map<Integer, DramaEpisode> updated = captor.getAllValues().stream()
|
||||
.collect(Collectors.toMap(DramaEpisode::getEpNo, Function.identity(), (a, b) -> b));
|
||||
for (DramaEpisode episode : episodes) {
|
||||
updated.putIfAbsent(episode.getEpNo(), episode);
|
||||
}
|
||||
return updated;
|
||||
}
|
||||
}
|
||||
Reference in new issue
Block a user