From 1ec94c00e5c312818c9e1e3742ca838e60b7e5e7 Mon Sep 17 00:00:00 2001
From: xuhy <3313886187@qq.com>
Date: 星期一, 20 十月 2025 17:15:10 +0800
Subject: [PATCH] Merge remote-tracking branch 'origin/master'

---
 ruoyi-system/src/main/java/com/ruoyi/system/service/impl/TSysGoodsServiceImpl.java |   83 +++++++++++++++++++++++++++++++++++++++++
 1 files changed, 82 insertions(+), 1 deletions(-)

diff --git a/ruoyi-system/src/main/java/com/ruoyi/system/service/impl/TSysGoodsServiceImpl.java b/ruoyi-system/src/main/java/com/ruoyi/system/service/impl/TSysGoodsServiceImpl.java
index 968616e..a435987 100644
--- a/ruoyi-system/src/main/java/com/ruoyi/system/service/impl/TSysGoodsServiceImpl.java
+++ b/ruoyi-system/src/main/java/com/ruoyi/system/service/impl/TSysGoodsServiceImpl.java
@@ -1,10 +1,22 @@
 package com.ruoyi.system.service.impl;
 
+import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
 import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
+import com.ruoyi.system.dto.ExchangeGoodsDto;
+import com.ruoyi.system.mapper.TCrmChangePointsMapper;
+import com.ruoyi.system.mapper.TCrmClinicPointsMapper;
+import com.ruoyi.system.mapper.TSysGoodsExchangeMapper;
 import com.ruoyi.system.mapper.TSysGoodsMapper;
-import com.ruoyi.system.model.TSysGoods;
+import com.ruoyi.system.model.*;
 import com.ruoyi.system.service.TSysGoodsService;
 import org.springframework.stereotype.Service;
+import org.springframework.transaction.annotation.Transactional;
+
+import javax.annotation.Resource;
+import java.time.LocalDateTime;
+import java.util.Date;
+import java.util.List;
+import java.util.stream.Collectors;
 
 /**
  * <p>
@@ -17,4 +29,73 @@
 @Service
 public class TSysGoodsServiceImpl extends ServiceImpl<TSysGoodsMapper, TSysGoods> implements TSysGoodsService {
 
+    @Resource
+    private TSysGoodsExchangeMapper sysGoodsExchangeMapper;
+
+    @Resource
+    private TCrmClinicPointsMapper crmClinicPointsMapper;
+
+    @Resource
+    private TCrmChangePointsMapper crmChangePointsMapper;
+
+    @Override
+    @Transactional(rollbackFor = Exception.class)
+    public void exchangeGoods(TCrmClinic crmClinic, ExchangeGoodsDto dto) {
+        // 盘点库存是否足够  判断积分是否足够  减积分  记录积分变更 添加购买记录
+        TSysGoods tSysGoods = this.getById(dto.getGoodsId());
+        int needPoints = tSysGoods.getRequiredPoints().intValue() * dto.getCount();
+
+        // 判断已经购买的了多少
+        List<TSysGoodsExchange> list = sysGoodsExchangeMapper.selectList(new LambdaQueryWrapper<TSysGoodsExchange>().eq(TSysGoodsExchange::getGoodsId, dto.getGoodsId()));
+        int sum = list.stream().mapToInt(TSysGoodsExchange::getGoodsCount).sum();
+        if(sum+dto.getCount()>tSysGoods.getGoodsTotal()){
+            throw new RuntimeException("商品库存不足");
+        }
+        if(sum+dto.getCount()>tSysGoods.getConvertibleQuantity()){
+            throw new RuntimeException("可兑换数量不足");
+        }
+        List<TCrmClinicPoints> points = crmClinicPointsMapper.selectList(new LambdaQueryWrapper<TCrmClinicPoints>().eq(TCrmClinicPoints::getClinicId, crmClinic.getId()).ge(TCrmClinicPoints::getExpireTime, new Date()).last(" and points > use_points ORDER BY create_time ASC"));
+        // 可用积分
+        int userPoints = points.stream().mapToInt(item -> item.getPoints() - item.getUsePoints()).sum();
+        if(userPoints<needPoints){
+            throw new RuntimeException("积分不足");
+        }
+
+        for (TCrmClinicPoints point : points) {
+            if(needPoints==0){
+                break;
+            }
+            if(point.getPoints()-point.getUsePoints()>=needPoints){
+                point.setUsePoints(point.getUsePoints()+needPoints);
+                crmClinicPointsMapper.updateById(point);
+                needPoints = 0;
+            }else {
+                point.setUsePoints(point.getPoints());
+                crmClinicPointsMapper.updateById(point);
+                needPoints= needPoints - (point.getPoints() - point.getUsePoints());
+            }
+        }
+
+        // 添加兑换记录
+        TSysGoodsExchange exchange = new TSysGoodsExchange();
+        exchange.setClinicName(crmClinic.getClinicName());
+        exchange.setGoodsName(tSysGoods.getGoodsName());
+        exchange.setGoodsId(tSysGoods.getId());
+        exchange.setGoodsCount(dto.getCount());
+        exchange.setUsePoints(tSysGoods.getRequiredPoints().intValue() * dto.getCount());
+        exchange.setExchangeTime(LocalDateTime.now());
+        exchange.setClinicId(crmClinic.getId());
+        sysGoodsExchangeMapper.insert(exchange);
+
+        // 记录
+        TCrmChangePoints tCrmChangePoints = new TCrmChangePoints();
+        tCrmChangePoints.setBranchSalespersonId(crmClinic.getId());
+        tCrmChangePoints.setExchangeId(exchange.getId());
+        tCrmChangePoints.setUserType(3);
+        tCrmChangePoints.setChangeType(2);
+        tCrmChangePoints.setChangeReason(2);
+        tCrmChangePoints.setChangeValue(exchange.getUsePoints());
+        crmChangePointsMapper.insert(tCrmChangePoints);
+
+    }
 }

--
Gitblit v1.7.1