Browse Source

add:领取福利

lyx 4 days ago
parent
commit
e291fa487d

+ 4 - 0
alien-entity/src/main/java/shop/alien/entity/store/LifeDiscountCouponUser.java

@@ -77,6 +77,10 @@ public class LifeDiscountCouponUser extends Model<LifeDiscountCouponUser> {
     @TableField(value = "updated_user_id", fill = FieldFill.INSERT_UPDATE)
     private Integer updatedUserId;
 
+    @ApiModelProperty(value = "U币ID")
+    @TableField("ub_id")
+    private Integer ubId;
+
 
     @Override
     protected Serializable pkVal() {

+ 1 - 1
alien-lawyer/src/main/java/shop/alien/lawyer/controller/AiAutoReview.java

@@ -31,7 +31,7 @@ public class AiAutoReview {
 
     private final RestTemplate restTemplate;
 
-    @Value("${third-party-ai-auto-review.base-url:http://192.168.2.250:9100/ai/auto-review/api/v1/lawyer_complaint_audit_task/submit}")
+    @Value("${third-party-ai-auto-review.base-url:http://192.168.2.250:9000/ai/auto-review/api/v1/lawyer_complaint_audit_task/submit}")
     private String aiAutoReviewUrl;
 
     /**

+ 24 - 0
alien-store/src/main/java/shop/alien/store/controller/StorePlatformBenefitsController.java

@@ -4,6 +4,7 @@ import com.baomidou.mybatisplus.core.metadata.IPage;
 import io.swagger.annotations.*;
 import lombok.RequiredArgsConstructor;
 import lombok.extern.slf4j.Slf4j;
+import org.springframework.transaction.annotation.Transactional;
 import org.springframework.web.bind.annotation.*;
 import shop.alien.entity.result.R;
 import shop.alien.entity.store.StorePlatformBenefits;
@@ -110,5 +111,28 @@ public class StorePlatformBenefitsController {
         }
         return R.fail("审核失败");
     }
+
+    @ApiOperation("领取福利(优惠券或U币)")
+    @ApiOperationSupport(order = 7)
+    @PostMapping("/receive")
+    @ApiImplicitParams({
+            @ApiImplicitParam(name = "id", value = "福利ID", dataType = "int", paramType = "query", required = true),
+            @ApiImplicitParam(name = "userId", value = "用户ID", dataType = "int", paramType = "query", required = true)
+    })
+    @Transactional
+    public R<String> receiveBenefits(@RequestParam Integer id,@RequestParam(required = false) Integer userId) {
+        log.info("StorePlatformBenefitsController.receiveBenefits - id={},userId={}", id,userId);
+        try {
+            String result = benefitsService.receiveBenefits(id,userId);
+            return R.success(result);
+        } catch (RuntimeException e) {
+            log.error("StorePlatformBenefitsController.receiveBenefits - error: {}", e.getMessage());
+            return R.fail(e.getMessage());
+        } catch (Exception e) {
+            log.error("StorePlatformBenefitsController.receiveBenefits - error: ", e);
+            return R.fail("领取失败,请稍后重试");
+        }
+    }
+
 }
 

+ 9 - 0
alien-store/src/main/java/shop/alien/store/service/StorePlatformBenefitsService.java

@@ -64,5 +64,14 @@ public interface StorePlatformBenefitsService extends IService<StorePlatformBene
      * @return 审核结果
      */
     int audit(Integer id, Integer status);
+
+    /**
+     * 领取福利(优惠券或U币)
+     *
+     * @param id     福利ID
+     * @param userId 用户ID
+     * @return 领取结果
+     */
+    String receiveBenefits(Integer id, Integer userId);
 }
 

+ 163 - 4
alien-store/src/main/java/shop/alien/store/service/impl/StorePlatformBenefitsServiceImpl.java

@@ -1,7 +1,6 @@
 package shop.alien.store.service.impl;
 
 import com.alibaba.fastjson.JSONObject;
-import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
 import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
 import com.baomidou.mybatisplus.core.metadata.IPage;
 import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
@@ -9,14 +8,15 @@ import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
 import lombok.RequiredArgsConstructor;
 import lombok.extern.slf4j.Slf4j;
 import org.springframework.stereotype.Service;
+import shop.alien.entity.store.LifeDiscountCoupon;
+import shop.alien.entity.store.LifeDiscountCouponUser;
 import shop.alien.entity.store.StorePlatformBenefits;
 import shop.alien.entity.store.StoreVirtualCurrency;
-import shop.alien.entity.store.vo.LifeUserViolationVo;
 import shop.alien.entity.store.vo.StorePlatformBenefitsVo;
-import shop.alien.mapper.StorePlatformBenefitsMapper;
-import shop.alien.mapper.StoreVirtualCurrencyMapper;
+import shop.alien.mapper.*;
 import shop.alien.store.service.StorePlatformBenefitsService;
 import shop.alien.util.common.JwtUtil;
+import shop.alien.util.common.constant.DiscountCouponEnum;
 
 import java.util.Date;
 
@@ -35,6 +35,10 @@ public class StorePlatformBenefitsServiceImpl extends ServiceImpl<StorePlatformB
 
     private final StoreVirtualCurrencyMapper virtualCurrencyMapper;
 
+    private final LifeCouponMapper lifeCouponMapper;
+    private final LifeDiscountCouponMapper lifeDiscountCouponMapper;
+    private final LifeDiscountCouponUserMapper lifeDiscountCouponUserMapper;
+
 
     @Override
     public IPage<StorePlatformBenefitsVo> getPage(int page, int size, Integer type, String name) {
@@ -136,5 +140,160 @@ public class StorePlatformBenefitsServiceImpl extends ServiceImpl<StorePlatformB
         benefits.setUpdatedTime(new Date());
         return benefitsMapper.updateById(benefits);
     }
+
+    @Override
+    public String receiveBenefits(Integer id, Integer userId) {
+        log.info("StorePlatformBenefitsServiceImpl.receiveBenefits - id={},userId={}", id,userId);
+
+        // 获取当前用户信息
+        JSONObject data = JwtUtil.getCurrentUserInfo();
+        Integer userId1 = null;
+        if (data != null) {
+            userId1 = data.getInteger("userId");
+            log.info("获取用户ID:userId={}", userId1);
+        }
+        if (userId1 == null) {
+            throw new RuntimeException("用户未登录");
+        }
+        
+        // 查询福利信息
+        StorePlatformBenefits benefits = benefitsMapper.selectById(id);
+        if (benefits == null) {
+            throw new RuntimeException("福利不存在");
+        }
+        
+        // 检查福利状态:必须是审核通过(status=2)
+        if (benefits.getStatus() == null || benefits.getStatus() != 2) {
+            throw new RuntimeException("福利未审核通过,无法领取");
+        }
+        
+        // 检查删除标记
+        if (benefits.getDeleteFlag() != null && benefits.getDeleteFlag() == 1) {
+            throw new RuntimeException("福利已删除");
+        }
+        
+        // 检查类型并处理
+        if (benefits.getType() == null) {
+            throw new RuntimeException("福利类型不存在");
+        }
+        
+        if (benefits.getType() == 1) {
+            // 类型1:优惠券
+            return receiveCoupon(benefits, userId, id);
+        } else if (benefits.getType() == 2) {
+            // 类型2:U币
+            return receiveVirtualCurrency(benefits, userId, id);
+        } else {
+            throw new RuntimeException("不支持的福利类型");
+        }
+    }
+    
+    /**
+     * 领取优惠券
+     */
+    private String receiveCoupon(StorePlatformBenefits benefits, Integer userId, Integer benefitsId) {
+        log.info("StorePlatformBenefitsServiceImpl.receiveCoupon - benefitsId={}, userId={}", benefitsId, userId);
+        
+        // 通过 business_id 获取优惠券信息
+        if (benefits.getBusinessId() == null || benefits.getBusinessId().isEmpty()) {
+            throw new RuntimeException("优惠券信息不存在");
+        }
+
+        LifeDiscountCoupon lifeDiscountCoupon = lifeDiscountCouponMapper.selectById(benefitsId);
+        if (lifeDiscountCoupon == null) {
+            throw new RuntimeException("优惠券不存在");
+        }
+        
+        // 检查优惠券删除标记
+        if (lifeDiscountCoupon.getDeleteFlag() != null && lifeDiscountCoupon.getDeleteFlag() == 1) {
+            throw new RuntimeException("优惠券已删除");
+        }
+        
+        // 检查库存
+        if (lifeDiscountCoupon.getSingleQty() == null || lifeDiscountCoupon.getSingleQty() <= 0) {
+            throw new RuntimeException("优惠券库存不足");
+        }
+        
+        // 更新库存:减1
+        lifeDiscountCoupon.setSingleQty(lifeDiscountCoupon.getSingleQty() - 1);
+        lifeDiscountCoupon.setUpdatedTime(new Date());
+        int updateResult = lifeDiscountCouponMapper.updateById(lifeDiscountCoupon);
+
+        // 把优惠券记录插入到用户优惠券表中
+        LifeDiscountCouponUser lifeDiscountCouponUser = new LifeDiscountCouponUser();
+        // 设置该优惠券记录的优惠券 ID
+        lifeDiscountCouponUser.setCouponId(lifeDiscountCoupon.getId());
+        // 设置该优惠券记录的用户 ID 为当前用户 ID
+        lifeDiscountCouponUser.setUserId(userId);
+        // 设置该优惠券的领取时间为当前时间
+        lifeDiscountCouponUser.setReceiveTime(new Date());
+        // 设置该优惠券的过期时间为优惠券本身的结束日期
+        lifeDiscountCouponUser.setExpirationTime(lifeDiscountCoupon.getEndDate());
+        // 设置该优惠券的状态为待使用
+        lifeDiscountCouponUser.setStatus(Integer.parseInt(DiscountCouponEnum.WAITING_USED.getValue()));
+        lifeDiscountCouponUserMapper.insert(lifeDiscountCouponUser);
+
+        if (updateResult > 0) {
+            log.info("用户领取优惠券成功 - userId={}, benefitsId={}, couponId={}", userId, benefitsId, benefits.getBusinessId());
+            return "领取成功";
+        } else {
+            throw new RuntimeException("领取失败,请稍后重试");
+        }
+    }
+    
+    /**
+     * 领取U币
+     */
+    private String receiveVirtualCurrency(StorePlatformBenefits benefits, Integer userId, Integer benefitsId) {
+        log.info("StorePlatformBenefitsServiceImpl.receiveVirtualCurrency - benefitsId={}, userId={}", benefitsId, userId);
+        
+        // 通过 business_id 获取U币信息
+        if (benefits.getBusinessId() == null || benefits.getBusinessId().isEmpty()) {
+            throw new RuntimeException("U币信息不存在");
+        }
+        
+        Integer virtualCurrencyId;
+        try {
+            virtualCurrencyId = Integer.parseInt(benefits.getBusinessId());
+        } catch (NumberFormatException e) {
+            throw new RuntimeException("U币ID格式错误");
+        }
+        
+        StoreVirtualCurrency virtualCurrency = virtualCurrencyMapper.selectById(virtualCurrencyId);
+        if (virtualCurrency == null) {
+            throw new RuntimeException("U币不存在");
+        }
+        
+        // 检查U币删除标记
+        if (virtualCurrency.getDeleteFlag() != null && virtualCurrency.getDeleteFlag() == 1) {
+            throw new RuntimeException("U币已删除");
+        }
+        
+        // 检查U币状态:必须是审核通过(status=2)
+        if (virtualCurrency.getStatus() == null || virtualCurrency.getStatus() != 2) {
+            throw new RuntimeException("U币未审核通过,无法领取");
+        }
+
+        // 插入用户U币记录(假设表名为 store_user_virtual_currency)
+        // 把优惠券记录插入到用户优惠券表中
+        LifeDiscountCouponUser lifeDiscountCouponUser = new LifeDiscountCouponUser();
+        // 设置该优惠券记录的优惠券 ID
+        lifeDiscountCouponUser.setUbId(benefits.getId());
+        // 设置该优惠券记录的用户 ID 为当前用户 ID
+        lifeDiscountCouponUser.setUserId(userId);
+        // 设置该优惠券的领取时间为当前时间
+        lifeDiscountCouponUser.setReceiveTime(new Date());
+        // 设置该优惠券的过期时间为优惠券本身的结束日期
+        // 设置2099年12月31日为过期时间
+        java.time.LocalDate expirationTime = java.time.LocalDate.of(2099, 12, 31);
+        lifeDiscountCouponUser.setExpirationTime(expirationTime);
+        // 设置该优惠券的状态为待使用
+        lifeDiscountCouponUser.setStatus(Integer.parseInt(DiscountCouponEnum.WAITING_USED.getValue()));
+        lifeDiscountCouponUserMapper.insert(lifeDiscountCouponUser);
+        
+        // U币领取成功(目前只验证,不创建记录表,后续可根据业务需求添加用户U币记录表)
+        log.info("用户领取U币成功 - userId={}, benefitsId={}, virtualCurrencyId={}", userId, benefitsId, virtualCurrencyId);
+        return "领取成功";
+    }
 }