From c8a179a0bdbc7bdc987788e129efce16d467889f Mon Sep 17 00:00:00 2001 From: p-honggang.li <p-honggang.li@pcitc.com> Date: 星期四, 04 九月 2025 20:44:57 +0800 Subject: [PATCH] 修改购物车逻辑 --- src/main/java/com/webmanage/service/impl/PointsFlowServiceImpl.java | 159 +++++++++++++++++++++++++++++++++++++++++++++++------ 1 files changed, 141 insertions(+), 18 deletions(-) diff --git a/src/main/java/com/webmanage/service/impl/PointsFlowServiceImpl.java b/src/main/java/com/webmanage/service/impl/PointsFlowServiceImpl.java index b21c1e4..00d5dfd 100644 --- a/src/main/java/com/webmanage/service/impl/PointsFlowServiceImpl.java +++ b/src/main/java/com/webmanage/service/impl/PointsFlowServiceImpl.java @@ -7,6 +7,7 @@ import com.webmanage.common.BusinessException; import com.webmanage.common.PageResult; import com.webmanage.dto.AddPointsFlowDTO; +import com.webmanage.dto.DeductUserPointsDTO; import com.webmanage.dto.PointsFlowQueryDTO; import com.webmanage.emun.RuleTypeEnum; import com.webmanage.entity.PointsFlow; @@ -14,6 +15,9 @@ import com.webmanage.entity.UserPoints; import com.webmanage.mapper.PointsFlowMapper; import com.webmanage.mapper.UserPointsMapper; +import com.webmanage.mapper.PointsTransactionMapper; +import com.webmanage.entity.PointsTransaction; +import com.webmanage.service.OrderInfoService; import com.webmanage.service.PointsFlowService; import com.webmanage.service.PointsRuleService; import lombok.extern.slf4j.Slf4j; @@ -39,6 +43,10 @@ @Resource private PointsRuleService pointsRuleService; + + @Resource + private PointsTransactionMapper pointsTransactionMapper; + @Override public PageResult<PointsFlow> getPersonalPointsFlowPage(PointsFlowQueryDTO queryDTO) { @@ -213,7 +221,7 @@ if (applyProvider && providerPointsChange != 0) { PointsFlow providerFlow = new PointsFlow(); providerFlow.setUserId(providerId); - providerFlow.setUnitId(providerId); + providerFlow.setUnitId(null); providerFlow.setDataType(ruleType); providerFlow.setDataCategory(addPointsFlowDTO.getCategory()); providerFlow.setPoints(providerPointsChange); @@ -225,8 +233,8 @@ if (!providerSaved) { throw new BusinessException("淇濆瓨鎻愪緵鑰呯Н鍒嗘祦姘村け璐�"); } - - updateProviderUnitPoints(providerId, providerPointsChange); + updateUserPointsByRule(providerId, null, userPointsChange); + // updateProviderUnitPoints(providerId, providerPointsChange); } return true; @@ -326,11 +334,10 @@ */ private PointsRule getLatestEffectiveRule(Integer ruleType, String ruleNameCode, String category, Integer pointsWinner) { QueryWrapper<PointsRule> wrapper = new QueryWrapper<>(); - wrapper.eq("deleted", 0) - .eq("is_enabled", 0) + wrapper.eq("is_enabled", 0) .eq("rule_type", ruleType) .eq("rule_name_code", ruleNameCode) - .eq("data_category", category) + .eq("category", category) .eq(pointsWinner != null, "points_winner", pointsWinner) .orderByDesc("created_at") .last("LIMIT 1"); @@ -382,7 +389,7 @@ * 妫�鏌ョН鍒嗕綑棰濇槸鍚﹁冻澶� */ private void checkBalanceSufficient(String userId, String unitId, Integer requiredPoints) { - // 妫�鏌ヤ釜浜虹Н鍒嗕綑棰� + // 浠呮鏌ヤ釜浜虹Н鍒嗕綑棰濓紙绉婚櫎鍗曚綅浣欓鍒ゅ畾锛� QueryWrapper<UserPoints> userWrapper = new QueryWrapper<>(); userWrapper.eq("deleted", 0) .eq("user_id", userId); @@ -392,15 +399,7 @@ throw new BusinessException("涓汉绉垎浣欓涓嶈冻锛屽綋鍓嶄綑棰�: " + (userPoints != null ? userPoints.getBalance() : 0) + "锛岄渶瑕佹墸闄�: " + requiredPoints); } - // 妫�鏌ュ崟浣嶇Н鍒嗕綑棰� - QueryWrapper<UserPoints> unitWrapper = new QueryWrapper<>(); - unitWrapper.eq("deleted", 0) - .eq("unit_id", unitId); - - UserPoints unitPoints = userPointsMapper.selectOne(unitWrapper); - if (unitPoints == null || unitPoints.getBalance() < requiredPoints) { - throw new BusinessException("鍗曚綅绉垎浣欓涓嶈冻锛屽綋鍓嶄綑棰�: " + (unitPoints != null ? unitPoints.getBalance() : 0) + "锛岄渶瑕佹墸闄�: " + requiredPoints); - } + // 鍘熷崟浣嶄綑棰濇牎楠屽凡绉婚櫎 } /** @@ -493,7 +492,9 @@ userPoints = new UserPoints(); userPoints.setUserId(userId); - userPoints.setUnitId(unitId); + if(StringUtils.hasText(unitId)){ + userPoints.setUnitId(unitId); + } userPoints.setBalance(pointsValue); userPoints.setTotalEarned(pointsValue > 0 ? pointsValue : 0); userPoints.setTotalConsumed(pointsValue < 0 ? Math.abs(pointsValue) : 0); @@ -523,7 +524,7 @@ } // 鏇存柊鍗曚綅绉垎璐︽埛 - QueryWrapper<UserPoints> unitWrapper = new QueryWrapper<>(); + /* QueryWrapper<UserPoints> unitWrapper = new QueryWrapper<>(); unitWrapper.eq("deleted", 0) .eq("unit_id", unitId); @@ -563,6 +564,92 @@ unitPoints.setUpdateTime(LocalDateTime.now()); userPointsMapper.updateById(unitPoints); + } */ + } + + /** + * 鎵e噺鐢ㄦ埛绉垎 + */ + @Override + @Transactional(rollbackFor = Exception.class) + public boolean deductUserPoints(DeductUserPointsDTO deductDTO) { + try { + String userId = deductDTO.getUserId(); + String providerId = deductDTO.getProviderId(); + String unitId = deductDTO.getUnitId(); + Integer points = deductDTO.getPoints(); + String orderId = deductDTO.getOrderId(); + String remark = deductDTO.getRemark(); + String dataCategory = deductDTO.getDataCategory(); + Integer dataType = deductDTO.getDataType(); + + if (!StringUtils.hasText(userId)) { + throw new BusinessException("鐢ㄦ埛ID涓嶈兘涓虹┖"); + } + if (points == null || points <= 0) { + throw new BusinessException("鎵e噺绉垎鏁伴噺蹇呴』澶т簬0"); + } + + // 妫�鏌ョ敤鎴风Н鍒嗕綑棰濇槸鍚﹀厖瓒� + checkBalanceSufficient(userId, unitId, points); + + // 鍒涘缓绉垎娴佹按璁板綍 + PointsFlow pointsFlow = new PointsFlow(); + pointsFlow.setUserId(userId); + pointsFlow.setUnitId(unitId); + pointsFlow.setDataCategory(dataCategory != null ? dataCategory : "绉垎浜ゆ槗"); + pointsFlow.setDataType(dataType != null ? dataType : 1); + pointsFlow.setPoints(-points); // 璐熸暟琛ㄧず鎵e噺 + pointsFlow.setName(remark != null ? remark : "绉垎鎵e噺"); // name瀛楁瀛樺偍remark鍐呭 + pointsFlow.setFlowTime(LocalDateTime.now()); + pointsFlow.setRlueId(null); // 鐩存帴鎵e噺锛屼笉鍏宠仈瑙勫垯 + + boolean saved = save(pointsFlow); + if (!saved) { + throw new BusinessException("淇濆瓨绉垎娴佹按澶辫触"); + } + + // 鏇存柊鐢ㄦ埛绉垎璐︽埛 + updateUserPointsOnly(userId, -points); + + // 鏂板绉垎浜ゆ槗璁板綍锛堜笌娴佹按鍚屼簨鍔★級 + PointsTransaction trans = new PointsTransaction(); + // tb_points_transaction 鐨� chk_data_category 浠呭厑璁� '鐢ㄦ埛鍙備笌'銆�'鍏朵粬' + // 杩欓噷灏嗕笟鍔$被鐩槧灏勪负鏁版嵁搴撳厑璁哥殑鍊� + trans.setDataCategory("鐢ㄦ埛鍙備笌"); + trans.setTransactionName(pointsFlow.getName()); + trans.setTransactionTime(LocalDateTime.now()); + trans.setPointsChange(-points); + trans.setRuleType("娑堣��"); + try { + trans.setUserId(Long.valueOf(userId)); + } catch (Exception ignore) {} + try { + if (StringUtils.hasText(unitId)) trans.setUnitId(Long.valueOf(unitId)); + } catch (Exception ignore) {} + trans.setUserType("涓汉鐢ㄦ埛"); + trans.setRuleId(null); + trans.setDetailId(null); + trans.setCreatedAt(LocalDateTime.now()); + trans.setDeleted(0); + int inserted = pointsTransactionMapper.insert(trans); + log.info("Points transaction inserted rows={}, id={}", inserted, trans.getId()); + if (inserted <= 0 || trans.getId() == null) { + throw new BusinessException("淇濆瓨绉垎浜ゆ槗璁板綍澶辫触"); + } + // 涓烘彁渚涜�呮柊澧炵Н鍒� + AddPointsFlowDTO addPointsFlowDTO = new AddPointsFlowDTO(); + addPointsFlowDTO.setCategory("璧勬簮浜ゆ槗"); + addPointsFlowDTO.setCount(1); + addPointsFlowDTO.setProviderId(providerId); + addPointsFlowDTO.setRuleType(0); + addPointsFlowDTO.setRuleNameCode("digital_product_transaction"); + addPointsFlowDTO.setDescription("浜у搧浜ゆ槗"); + addPointsFlowByRule(addPointsFlowDTO); + return true; + } catch (Exception e) { + log.error("鎵e噺鐢ㄦ埛绉垎澶辫触", e); + throw new BusinessException("鎵e噺鐢ㄦ埛绉垎澶辫触锛�" + e.getMessage()); } } @@ -607,4 +694,40 @@ userPointsMapper.updateById(unitPoints); } } + + /** + * 浠呮洿鏂颁釜浜虹Н鍒嗚处鎴凤紙涓嶆搷浣滃崟浣嶈处鎴凤級 + */ + private void updateUserPointsOnly(String userId, Integer pointsValue) { + // 鏇存柊涓汉绉垎璐︽埛 + QueryWrapper<UserPoints> userWrapper = new QueryWrapper<>(); + userWrapper.eq("deleted", 0) + .eq("user_id", userId); + UserPoints userPoints = userPointsMapper.selectOne(userWrapper); + if (userPoints == null) { + if (pointsValue < 0) { + throw new BusinessException("涓汉绉垎浣欓涓嶈冻锛屾棤娉曟墸闄ょН鍒�"); + } + userPoints = new UserPoints(); + userPoints.setUserId(userId); + userPoints.setUnitId(null); + userPoints.setBalance(pointsValue); + userPoints.setTotalEarned(pointsValue > 0 ? pointsValue : 0); + userPoints.setTotalConsumed(pointsValue < 0 ? Math.abs(pointsValue) : 0); + userPointsMapper.insert(userPoints); + } else { + if (pointsValue < 0 && userPoints.getBalance() + pointsValue < 0) { + throw new BusinessException("涓汉绉垎浣欓涓嶈冻锛屽綋鍓嶄綑棰�: " + userPoints.getBalance() + "锛岄渶瑕佹墸闄�: " + Math.abs(pointsValue)); + } + userPoints.setBalance(userPoints.getBalance() + pointsValue); + if (pointsValue > 0) { + userPoints.setTotalEarned(userPoints.getTotalEarned() != null ? userPoints.getTotalEarned() + pointsValue : pointsValue); + } + if (pointsValue < 0) { + userPoints.setTotalConsumed(userPoints.getTotalConsumed() != null ? userPoints.getTotalConsumed() + Math.abs(pointsValue) : Math.abs(pointsValue)); + } + userPoints.setUpdateTime(LocalDateTime.now()); + userPointsMapper.updateById(userPoints); + } + } } -- Gitblit v1.8.0