add point totalConsumed
This commit is contained in:
@@ -24,6 +24,9 @@ public class GaoPointAccount extends UUIDEntity {
|
|||||||
/// 累计获得积分
|
/// 累计获得积分
|
||||||
private Long totalEarned;
|
private Long totalEarned;
|
||||||
|
|
||||||
|
/// 累计消耗积分
|
||||||
|
private Long totalConsumed;
|
||||||
|
|
||||||
/// 累计撤销积分
|
/// 累计撤销积分
|
||||||
private Long totalRevoked;
|
private Long totalRevoked;
|
||||||
|
|
||||||
|
|||||||
@@ -33,9 +33,10 @@ public interface GaoPointAccountMapper extends BaseMapper<GaoPointAccount, Strin
|
|||||||
/// @param id 账户 ID
|
/// @param id 账户 ID
|
||||||
/// @param delta 本次余额变化
|
/// @param delta 本次余额变化
|
||||||
/// @param totalEarnedDelta 累计获得积分变化
|
/// @param totalEarnedDelta 累计获得积分变化
|
||||||
|
/// @param totalConsumedDelta 累计消耗积分变化
|
||||||
/// @param totalRevokedDelta 累计撤销积分变化
|
/// @param totalRevokedDelta 累计撤销积分变化
|
||||||
/// @param totalAdjustedDelta 累计人工调整积分变化
|
/// @param totalAdjustedDelta 累计人工调整积分变化
|
||||||
/// @param updatedAt 更新时间
|
/// @param updatedAt 更新时间
|
||||||
/// @return 更新行数
|
/// @return 更新行数
|
||||||
int updateBalance(@Param("id") String id, @Param("delta") long delta, @Param("totalEarnedDelta") long totalEarnedDelta, @Param("totalRevokedDelta") long totalRevokedDelta, @Param("totalAdjustedDelta") long totalAdjustedDelta, @Param("updatedAt") long updatedAt);
|
int updateBalance(@Param("id") String id, @Param("delta") long delta, @Param("totalEarnedDelta") long totalEarnedDelta, @Param("totalConsumedDelta") long totalConsumedDelta, @Param("totalRevokedDelta") long totalRevokedDelta, @Param("totalAdjustedDelta") long totalAdjustedDelta, @Param("updatedAt") long updatedAt);
|
||||||
}
|
}
|
||||||
|
|||||||
@@ -28,8 +28,9 @@ public interface GaoPointAccountService extends BaseService<GaoPointAccount, Str
|
|||||||
/// @param accountId 账户 ID
|
/// @param accountId 账户 ID
|
||||||
/// @param delta 本次余额变化
|
/// @param delta 本次余额变化
|
||||||
/// @param totalEarnedDelta 累计获得积分变化
|
/// @param totalEarnedDelta 累计获得积分变化
|
||||||
|
/// @param totalConsumedDelta 累计消耗积分变化
|
||||||
/// @param totalRevokedDelta 累计撤销积分变化
|
/// @param totalRevokedDelta 累计撤销积分变化
|
||||||
/// @param totalAdjustedDelta 累计人工调整积分变化
|
/// @param totalAdjustedDelta 累计人工调整积分变化
|
||||||
/// @return 更新行数
|
/// @return 更新行数
|
||||||
int updateBalance(String accountId, long delta, long totalEarnedDelta, long totalRevokedDelta, long totalAdjustedDelta);
|
int updateBalance(String accountId, long delta, long totalEarnedDelta, long totalConsumedDelta, long totalRevokedDelta, long totalAdjustedDelta);
|
||||||
}
|
}
|
||||||
|
|||||||
+3
-2
@@ -50,6 +50,7 @@ public class GaoPointAccountServiceImplement extends AbstractEntityService<GaoPo
|
|||||||
account.setCustomerId(customerId);
|
account.setCustomerId(customerId);
|
||||||
account.setBalance(0L);
|
account.setBalance(0L);
|
||||||
account.setTotalEarned(0L);
|
account.setTotalEarned(0L);
|
||||||
|
account.setTotalConsumed(0L);
|
||||||
account.setTotalRevoked(0L);
|
account.setTotalRevoked(0L);
|
||||||
account.setTotalAdjusted(0L);
|
account.setTotalAdjusted(0L);
|
||||||
account.setVersion(0L);
|
account.setVersion(0L);
|
||||||
@@ -64,8 +65,8 @@ public class GaoPointAccountServiceImplement extends AbstractEntityService<GaoPo
|
|||||||
}
|
}
|
||||||
|
|
||||||
@Override
|
@Override
|
||||||
public int updateBalance(String accountId, long delta, long totalEarnedDelta, long totalRevokedDelta, long totalAdjustedDelta) {
|
public int updateBalance(String accountId, long delta, long totalEarnedDelta, long totalConsumedDelta, long totalRevokedDelta, long totalAdjustedDelta) {
|
||||||
TimiException.required(accountId, "not found accountId");
|
TimiException.required(accountId, "not found accountId");
|
||||||
return mapper.updateBalance(accountId, delta, totalEarnedDelta, totalRevokedDelta, totalAdjustedDelta, Time.now());
|
return mapper.updateBalance(accountId, delta, totalEarnedDelta, totalConsumedDelta, totalRevokedDelta, totalAdjustedDelta, Time.now());
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|||||||
+2
-1
@@ -155,6 +155,7 @@ public class GaoPointLedgerServiceImplement extends AbstractEntityService<GaoPoi
|
|||||||
long balanceBefore = account.getBalance();
|
long balanceBefore = account.getBalance();
|
||||||
long balanceAfter = Math.addExact(balanceBefore, pointDelta);
|
long balanceAfter = Math.addExact(balanceBefore, pointDelta);
|
||||||
long totalEarnedDelta = changeType == GaoPointLedger.ChangeType.EARN ? pointDelta : 0L;
|
long totalEarnedDelta = changeType == GaoPointLedger.ChangeType.EARN ? pointDelta : 0L;
|
||||||
|
long totalConsumedDelta = changeType == GaoPointLedger.ChangeType.CONSUME ? -pointDelta : 0L;
|
||||||
long totalRevokedDelta = changeType == GaoPointLedger.ChangeType.REVOKE ? -pointDelta : 0L;
|
long totalRevokedDelta = changeType == GaoPointLedger.ChangeType.REVOKE ? -pointDelta : 0L;
|
||||||
long totalAdjustedDelta = changeType == GaoPointLedger.ChangeType.ADJUST ? pointDelta : 0L;
|
long totalAdjustedDelta = changeType == GaoPointLedger.ChangeType.ADJUST ? pointDelta : 0L;
|
||||||
|
|
||||||
@@ -176,7 +177,7 @@ public class GaoPointLedgerServiceImplement extends AbstractEntityService<GaoPoi
|
|||||||
ledger.setOperatorUserId(operatorUserId);
|
ledger.setOperatorUserId(operatorUserId);
|
||||||
ledger.setOccurredAt(occurredAt);
|
ledger.setOccurredAt(occurredAt);
|
||||||
create(ledger);
|
create(ledger);
|
||||||
int updated = accountService.updateBalance(account.getId(), pointDelta, totalEarnedDelta, totalRevokedDelta, totalAdjustedDelta);
|
int updated = accountService.updateBalance(account.getId(), pointDelta, totalEarnedDelta, totalConsumedDelta, totalRevokedDelta, totalAdjustedDelta);
|
||||||
TimiException.requiredTrue(0 < updated, "积分余额不足");
|
TimiException.requiredTrue(0 < updated, "积分余额不足");
|
||||||
logger.setLevel(Logger.Level.INFO);
|
logger.setLevel(Logger.Level.INFO);
|
||||||
logger.setResult(ledger.getId());
|
logger.setResult(ledger.getId());
|
||||||
|
|||||||
@@ -0,0 +1,13 @@
|
|||||||
|
ALTER TABLE `gao_point_account`
|
||||||
|
ADD COLUMN `total_consumed` BIGINT NOT NULL DEFAULT 0 COMMENT '累计消耗积分' AFTER `total_earned`;
|
||||||
|
|
||||||
|
UPDATE `gao_point_account` account
|
||||||
|
LEFT JOIN (
|
||||||
|
SELECT `account_id`, SUM(-`point_delta`) AS `total_consumed`
|
||||||
|
FROM `gao_point_ledger`
|
||||||
|
WHERE `change_type` = 'CONSUME'
|
||||||
|
AND `point_delta` < 0
|
||||||
|
AND `deleted_at` IS NULL
|
||||||
|
GROUP BY `account_id`
|
||||||
|
) ledger ON ledger.`account_id` = account.`id`
|
||||||
|
SET account.`total_consumed` = COALESCE(ledger.`total_consumed`, 0);
|
||||||
@@ -12,9 +12,9 @@
|
|||||||
|
|
||||||
<insert id="insertIfAbsent">
|
<insert id="insertIfAbsent">
|
||||||
INSERT INTO `gao_point_account` (
|
INSERT INTO `gao_point_account` (
|
||||||
`id`, `store_id`, `customer_id`, `balance`, `total_earned`, `total_revoked`, `total_adjusted`, `version`, `created_at`, `updated_at`, `deleted_at`
|
`id`, `store_id`, `customer_id`, `balance`, `total_earned`, `total_consumed`, `total_revoked`, `total_adjusted`, `version`, `created_at`, `updated_at`, `deleted_at`
|
||||||
) VALUES (
|
) VALUES (
|
||||||
#{account.id}, #{account.storeId}, #{account.customerId}, #{account.balance}, #{account.totalEarned}, #{account.totalRevoked}, #{account.totalAdjusted}, #{account.version}, #{account.createdAt}, #{account.updatedAt}, #{account.deletedAt}
|
#{account.id}, #{account.storeId}, #{account.customerId}, #{account.balance}, #{account.totalEarned}, #{account.totalConsumed}, #{account.totalRevoked}, #{account.totalAdjusted}, #{account.version}, #{account.createdAt}, #{account.updatedAt}, #{account.deletedAt}
|
||||||
)
|
)
|
||||||
ON DUPLICATE KEY UPDATE `id` = `id`
|
ON DUPLICATE KEY UPDATE `id` = `id`
|
||||||
</insert>
|
</insert>
|
||||||
@@ -33,6 +33,7 @@
|
|||||||
SET
|
SET
|
||||||
`balance` = `balance` + #{delta},
|
`balance` = `balance` + #{delta},
|
||||||
`total_earned` = `total_earned` + #{totalEarnedDelta},
|
`total_earned` = `total_earned` + #{totalEarnedDelta},
|
||||||
|
`total_consumed` = `total_consumed` + #{totalConsumedDelta},
|
||||||
`total_revoked` = `total_revoked` + #{totalRevokedDelta},
|
`total_revoked` = `total_revoked` + #{totalRevokedDelta},
|
||||||
`total_adjusted` = `total_adjusted` + #{totalAdjustedDelta},
|
`total_adjusted` = `total_adjusted` + #{totalAdjustedDelta},
|
||||||
`version` = `version` + 1,
|
`version` = `version` + 1,
|
||||||
|
|||||||
Reference in New Issue
Block a user