瀏覽代碼

feat: enhance PpointService with save/delete/query methods and English javadoc

Added savePpoint(), deleteById(), getByProductId() for admin operations. Converted getEffectivePpoint javadoc to English. Added ProductMapper fallback to Product.profitRate. Added Logger.

Ultraworked with Sisyphus

Co-authored-by: Sisyphus <clio-agent@sisyphuslabs.ai>
User 2 月之前
父節點
當前提交
5d6ea7d69e
共有 1 個文件被更改,包括 83 次插入9 次删除
  1. 83 9
      cfc-backend/src/main/java/com/etotem/cfc/service/PpointService.java

+ 83 - 9
cfc-backend/src/main/java/com/etotem/cfc/service/PpointService.java

@@ -1,36 +1,110 @@
 package com.etotem.cfc.service;
 
 import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
+import com.etotem.cfc.entity.Product;
 import com.etotem.cfc.entity.ProductPpoint;
+import com.etotem.cfc.mapper.ProductMapper;
 import com.etotem.cfc.mapper.ProductPpointMapper;
+import org.slf4j.Logger;
+import org.slf4j.LoggerFactory;
 import org.springframework.stereotype.Service;
 
 import javax.annotation.Resource;
 import java.util.Date;
+import java.util.List;
 
 @Service
 public class PpointService {
 
+    private static final Logger log = LoggerFactory.getLogger(PpointService.class);
+
     @Resource
     private ProductPpointMapper productPpointMapper;
 
+    @Resource
+    private ProductMapper productMapper;
+
     /**
-     * 获取商品当前生效的 P-point(利润点数)
-     * 从 product_ppoint 表查询日期范围内有效的配置,未配置则返回 0
+     * Get effective P-point for a product.
+     * First checks product_ppoint table for a date-valid record (startDate <= today <= endDate).
+     * Falls back to Product.profitRate if no valid product_ppoint record exists.
+     * Returns 0 if neither exists.
+     * @param productId product ID
+     * @return effective ppoint value in basis points (e.g., 1000 = 10%)
      */
     public int getEffectivePpoint(Long productId) {
         if (productId == null) {
             return 0;
         }
+
         Date now = new Date();
-        ProductPpoint pp = productPpointMapper.selectOne(
+
+        // Check product_ppoint table for a date-valid record
+        LambdaQueryWrapper<ProductPpoint> wrapper = new LambdaQueryWrapper<ProductPpoint>()
+                .eq(ProductPpoint::getProductId, productId)
+                .le(ProductPpoint::getStartDate, now)
+                .ge(ProductPpoint::getEndDate, now)
+                .orderByDesc(ProductPpoint::getStartDate)
+                .last("LIMIT 1");
+        ProductPpoint record = productPpointMapper.selectOne(wrapper);
+
+        if (record != null && record.getPpoint() != null && record.getPpoint() > 0) {
+            return record.getPpoint();
+        }
+
+        // Fallback to Product.profitRate
+        Product product = productMapper.selectById(productId);
+        if (product != null && product.getProfitRate() != null) {
+            // Convert profitRate (percentage-based, e.g. 10 = 10%) to basis points (1000 = 10%)
+            return product.getProfitRate() * 100;
+        }
+
+        return 0;
+    }
+
+    /**
+     * Save or update P-point config for a product (admin operation).
+     * If a valid record already exists for the product, update it; otherwise insert.
+     */
+    public void savePpoint(Long productId, Integer ppoint, Date startDate, Date endDate, Long adminUserId) {
+        // Check if an existing record exists for this product
+        LambdaQueryWrapper<ProductPpoint> wrapper = new LambdaQueryWrapper<ProductPpoint>()
+                .eq(ProductPpoint::getProductId, productId);
+        ProductPpoint existing = productPpointMapper.selectOne(wrapper);
+
+        if (existing != null) {
+            existing.setPpoint(ppoint);
+            existing.setStartDate(startDate);
+            existing.setEndDate(endDate);
+            existing.setCreatedBy(adminUserId);
+            productPpointMapper.updateById(existing);
+        } else {
+            ProductPpoint record = new ProductPpoint();
+            record.setProductId(productId);
+            record.setPpoint(ppoint);
+            record.setStartDate(startDate);
+            record.setEndDate(endDate);
+            record.setCreatedBy(adminUserId);
+            record.setCreatedAt(new Date());
+            productPpointMapper.insert(record);
+        }
+    }
+
+    /**
+     * Delete P-point config by ID (admin operation).
+     */
+    public void deleteById(Long id) {
+        productPpointMapper.deleteById(id);
+    }
+
+    /**
+     * Get P-point record by product ID (all records, not just effective one).
+     */
+    public List<ProductPpoint> getByProductId(Long productId) {
+        return productPpointMapper.selectList(
                 new LambdaQueryWrapper<ProductPpoint>()
                         .eq(ProductPpoint::getProductId, productId)
-                        .le(ProductPpoint::getStartDate, now)
-                        .ge(ProductPpoint::getEndDate, now)
-                        .orderByDesc(ProductPpoint::getCreatedAt)
-                        .last("LIMIT 1")
+                        .orderByDesc(ProductPpoint::getStartDate)
         );
-        return pp != null ? pp.getPpoint() : 0;
     }
-}
+}