Browse Source

1.学生端添加激活码激活方式

yuanliang 1 year ago
parent
commit
4f1bba2451

+ 2 - 0
cooleshow-user/user-biz/src/main/java/com/yonge/cooleshow/biz/dal/service/TenantActivationCodeService.java

@@ -74,4 +74,6 @@ public interface TenantActivationCodeService extends IService<TenantActivationCo
      */
     void importActiveCode(List<ExcelDataReaderProperty<TenantActivationCodeWrapper.ImportTemplate>> dataList,
                           Long tenantId, Long userId, Long tenantAlbumPurchaseId);
+
+    void activeById(String id, Long userId);
 }

+ 55 - 0
cooleshow-user/user-biz/src/main/java/com/yonge/cooleshow/biz/dal/service/impl/TenantActivationCodeServiceImpl.java

@@ -219,6 +219,61 @@ public class TenantActivationCodeServiceImpl extends ServiceImpl<TenantActivatio
     }
 
 
+    @Transactional(rollbackFor = Exception.class)
+    @Override
+    public void activeById(String id, Long userId) {
+        Student student = studentDao.selectById(userId);
+        if (student == null) {
+            throw new BizException("学生不存在");
+        }
+        Long tenantId = -1L;
+        if (student.getTenantId() != null) {
+            tenantId = student.getTenantId();
+        }
+        TenantActivationCode code = baseMapper.selectById(id);
+        if(code == null || !code.getTenantId().equals(tenantId)) {
+            throw new BizException("激活码不存在");
+        }
+        SysUser sysUser = sysUserMapper.selectById(student.getUserId());
+        if(!sysUser.getPhone().equals(code.getActivationPhone())) {
+            // 该激活码未指定为当前学生使用
+            throw new BizException("激活码不存在");
+        }
+        if (Boolean.TRUE.equals(code.getActivationStatus())) {
+            throw new BizException("激活码已经被使用");
+        }
+
+        // 通过状态和ID同时判断更新是否存在竞争
+        boolean update = this.lambdaUpdate()
+                .set(TenantActivationCode::getActivationStatus, true)
+                .set(TenantActivationCode::getActivationUserId, student.getUserId())
+                .set(TenantActivationCode::getActivationTime, new Date())
+                .set(TenantActivationCode::getActivationPhone, sysUser.getPhone())
+                .eq(TenantActivationCode::getId, code.getId())
+                .eq(TenantActivationCode::getActivationStatus, false)
+                .update();
+        if (!update) {
+            throw new BizException("激活码已经被使用");
+        }
+        Long tenantAlbumPurchaseId = code.getTenantAlbumPurchaseId();
+        TenantAlbumPurchase purchase = tenantAlbumPurchaseMapper.selectById(tenantAlbumPurchaseId);
+
+        addUserTenantAlbumRecord(student.getUserId(), purchase, null);
+
+        // 更新购买记录中激活码使用统计数量值
+        Integer activeCodeNumber = this.lambdaQuery()
+                .eq(TenantActivationCode::getTenantId, tenantId)
+                .eq(TenantActivationCode::getTenantAlbumPurchaseId, code.getTenantAlbumPurchaseId())
+                .eq(TenantActivationCode::getActivationStatus, true).count();
+
+        TenantAlbumPurchase tenantAlbumPurchase = new TenantAlbumPurchase();
+        tenantAlbumPurchase.setId(code.getTenantAlbumPurchaseId());
+        tenantAlbumPurchase.setActiveQuantity(activeCodeNumber);
+        tenantAlbumPurchaseMapper.updateById(tenantAlbumPurchase);
+
+    }
+
+
     /**
      * 添加用户机构专辑激活记录
      *

+ 11 - 0
cooleshow-user/user-student/src/main/java/com/yonge/cooleshow/student/controller/TenantActivationCodeController.java

@@ -77,4 +77,15 @@ public class TenantActivationCodeController extends BaseController {
         tenantActivationCodeService.active(activationCode, sysUser.getId());
         return succeed();
     }
+
+    @ApiOperation(value = "激活激活码")
+    @PostMapping("/activeById")
+    public HttpResponseResult<Boolean> activeById(@RequestParam("id") String id) {
+        SysUser sysUser = sysUserFeignService.queryUserInfo();
+        if (sysUser == null || null == sysUser.getId()) {
+            throw new BizException("请登录");
+        }
+        tenantActivationCodeService.activeById(id, sysUser.getId());
+        return succeed();
+    }
 }