|
|
@@ -7,6 +7,7 @@ import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
|
|
|
import com.etotem.cfc.entity.FamilyMember;
|
|
|
import com.etotem.cfc.entity.PointsLog;
|
|
|
import com.etotem.cfc.entity.User;
|
|
|
+import com.etotem.cfc.dto.CfBalanceResult;
|
|
|
import com.etotem.cfc.dto.CfEarnResult;
|
|
|
import com.etotem.cfc.mapper.FamilyMemberMapper;
|
|
|
import com.etotem.cfc.mapper.PointsLogMapper;
|
|
|
@@ -44,6 +45,10 @@ public class PointsService implements PointsServiceInterface {
|
|
|
@Resource
|
|
|
private CfPathwayEarnService cfPathwayEarnService;
|
|
|
|
|
|
+ /** F-5:旧 CF 兼容方法(award/deduct/查询)改投统一账本 cf_balance,不再写 family_members.system_points */
|
|
|
+ @Resource
|
|
|
+ private CfSpendDispatchService cfSpendDispatchService;
|
|
|
+
|
|
|
public Map<String, Object> getBalance(Long memberId) {
|
|
|
FamilyMember child = familyMemberMapper.selectById(memberId);
|
|
|
if (child == null) {
|
|
|
@@ -117,7 +122,13 @@ public class PointsService implements PointsServiceInterface {
|
|
|
return result;
|
|
|
}
|
|
|
|
|
|
- /** @return CF值余额 */
|
|
|
+ /**
|
|
|
+ * 发放 CF 值(F-5:改投统一账本 cf_balance,不再写 family_members.system_points)。
|
|
|
+ *
|
|
|
+ * <p>本方法为旧接口兼容入口,无稳定业务单据 ID,故不参与幂等键(refId 传空)。</p>
|
|
|
+ *
|
|
|
+ * @return 发放后可用 CF 值余额
|
|
|
+ */
|
|
|
@Transactional
|
|
|
public int awardCfPoints(Long memberId, int amount, String reason) {
|
|
|
if (amount <= 0) {
|
|
|
@@ -129,20 +140,8 @@ public class PointsService implements PointsServiceInterface {
|
|
|
throw new RuntimeException("孩子不存在");
|
|
|
}
|
|
|
|
|
|
- int newCfPoints = (child.getSystemPoints() == null ? 0 : child.getSystemPoints()) + amount;
|
|
|
- child.setSystemPoints(newCfPoints);
|
|
|
-
|
|
|
- child.setUpdatedAt(new Date());
|
|
|
- familyMemberMapper.updateById(child);
|
|
|
-
|
|
|
- PointsLog plog = new PointsLog();
|
|
|
- plog.setChildId(memberId);
|
|
|
- plog.setAmount(amount);
|
|
|
- plog.setType("earn");
|
|
|
- plog.setCategory("cf");
|
|
|
- plog.setDescription(reason);
|
|
|
- plog.setCreatedAt(new Date());
|
|
|
- pointsLogMapper.insert(plog);
|
|
|
+ CfBalanceResult result = cfSpendDispatchService.earnForMember(memberId, amount, "points_cf_award", null, reason);
|
|
|
+ int newCfPoints = result != null && result.getAvailable() != null ? result.getAvailable() : amount;
|
|
|
try { sendPointsNotification(memberId, "cf_earn", amount, reason); } catch (Exception e) { log.warn("cf notification failed", e); }
|
|
|
|
|
|
return newCfPoints;
|
|
|
@@ -170,48 +169,37 @@ public class PointsService implements PointsServiceInterface {
|
|
|
return deductCfPoints(memberId, amount, "心愿兑换扣除") >= 0;
|
|
|
}
|
|
|
|
|
|
- /** @return 扣除后余额,余额不足返回-1 */
|
|
|
+ /**
|
|
|
+ * 扣除 CF 值(F-5:改投统一账本 cf_balance,不再写 family_members.system_points)。
|
|
|
+ *
|
|
|
+ * <p>旧接口兼容入口,无稳定业务单据 ID,不做幂等(refId 传空);余额不足返回 -1。</p>
|
|
|
+ *
|
|
|
+ * @return 扣除后可用 CF 值余额,余额不足返回 -1
|
|
|
+ */
|
|
|
@Transactional
|
|
|
public int deductCfPoints(Long memberId, int amount, String reason) {
|
|
|
if (amount <= 0) {
|
|
|
throw new IllegalArgumentException("扣除数量必须为正数");
|
|
|
}
|
|
|
|
|
|
- FamilyMember child = familyMemberMapper.selectById(memberId);
|
|
|
- if (child == null) {
|
|
|
+ if (memberId == null) {
|
|
|
throw new RuntimeException("孩子不存在");
|
|
|
}
|
|
|
-
|
|
|
- int currentCf = child.getSystemPoints() == null ? 0 : child.getSystemPoints();
|
|
|
+ int currentCf = cfSpendDispatchService.getAvailableForMember(memberId);
|
|
|
if (currentCf < amount) {
|
|
|
return -1;
|
|
|
}
|
|
|
|
|
|
- int newCfPoints = currentCf - amount;
|
|
|
- child.setSystemPoints(newCfPoints);
|
|
|
-
|
|
|
- child.setUpdatedAt(new Date());
|
|
|
- familyMemberMapper.updateById(child);
|
|
|
-
|
|
|
- PointsLog plog = new PointsLog();
|
|
|
- plog.setChildId(memberId);
|
|
|
- plog.setAmount(-amount);
|
|
|
- plog.setType("spend");
|
|
|
- plog.setCategory("cf");
|
|
|
- plog.setDescription(reason);
|
|
|
- plog.setCreatedAt(new Date());
|
|
|
- pointsLogMapper.insert(plog);
|
|
|
+ CfBalanceResult result = cfSpendDispatchService.spendForMember(memberId, amount, "points_cf_deduct", null, reason);
|
|
|
+ int newCfPoints = result != null && result.getAvailable() != null ? result.getAvailable() : (currentCf - amount);
|
|
|
try { sendPointsNotification(memberId, "cf_spend", -amount, reason); } catch (Exception e) { log.warn("cf deduction notification failed", e); }
|
|
|
|
|
|
return newCfPoints;
|
|
|
}
|
|
|
|
|
|
+ /** 查询 CF 值余额(F-5:改读统一账本 cf_balance,不再读 family_members.system_points) */
|
|
|
public int getCfPointsBalance(Long memberId) {
|
|
|
- FamilyMember child = familyMemberMapper.selectById(memberId);
|
|
|
- if (child == null) {
|
|
|
- return 0;
|
|
|
- }
|
|
|
- return child.getSystemPoints() == null ? 0 : child.getSystemPoints();
|
|
|
+ return cfSpendDispatchService.getAvailableForMember(memberId);
|
|
|
}
|
|
|
|
|
|
public Page<PointsLog> getPointsLogsByCategory(Long memberId, String category, Integer page, Integer size) {
|