dcs 3 月之前
父節點
當前提交
39f48a1791

+ 8 - 0
virgo.api/src/main/java/com/bosshand/virgo/api/workark/controller/ProductCouponController.java

@@ -3,6 +3,7 @@ package com.bosshand.virgo.api.workark.controller;
 
 import com.bosshand.virgo.api.workark.model.ProductCoupon;
 import com.bosshand.virgo.api.workark.service.ProductCouponService;
+import com.bosshand.virgo.api.workark.service.ProductLevelService;
 import com.bosshand.virgo.core.response.Response;
 import io.swagger.annotations.Api;
 import io.swagger.annotations.ApiOperation;
@@ -21,6 +22,9 @@ public class ProductCouponController {
     @Autowired
     ProductCouponService productCouponService;
 
+    @Autowired
+    ProductLevelService productLevelService;
+
     @ApiOperation("新用户优惠券")
     @RequestMapping(value = "/nowReceive/{userId}", method = RequestMethod.GET)
     public Response nowReceive(@PathVariable long userId) {
@@ -58,6 +62,10 @@ public class ProductCouponController {
     @ApiOperation("查询")
     @RequestMapping(value = "/query", method = RequestMethod.POST)
     public Response getList(@RequestBody ProductCoupon productCoupon) {
+        if (productCoupon.getProductLevelId() != 0) {
+            List<Long> allIds = productLevelService.getAllIds(productCoupon.getProductLevelId());
+            productCoupon.setProductLevelIds(allIds);
+        }
         return Response.ok(productCouponService.getList(productCoupon));
     }
 

+ 2 - 0
virgo.api/src/main/java/com/bosshand/virgo/api/workark/dao/ProductCouponDao.java

@@ -26,4 +26,6 @@ public interface ProductCouponDao {
     List<ProductCoupon> getProductCouponModelId(long userId);
 
     ProductCoupon getUserId(long id, long userId);
+
+    void updateTerminateState(long id);
 }

+ 14 - 3
virgo.api/src/main/java/com/bosshand/virgo/api/workark/model/ProductCoupon.java

@@ -4,6 +4,7 @@ import com.fasterxml.jackson.annotation.JsonFormat;
 
 import java.math.BigDecimal;
 import java.util.Date;
+import java.util.List;
 
 /**
  * 商品优惠券
@@ -74,13 +75,15 @@ public class ProductCoupon {
     /**
      * 有效期 (小时)
      */
-    private Integer lifespan;
+    private Long lifespan;
 
     /**
      * 商品目录id (可以使用优惠券的范围)
      */
     private long productLevelId;
 
+    private List<Long> productLevelIds;
+
     /**
      * 优惠金额 (元)
      */
@@ -207,11 +210,11 @@ public class ProductCoupon {
         this.terminateTime = terminateTime;
     }
 
-    public Integer getLifespan() {
+    public Long getLifespan() {
         return lifespan;
     }
 
-    public void setLifespan(Integer lifespan) {
+    public void setLifespan(Long lifespan) {
         this.lifespan = lifespan;
     }
 
@@ -223,6 +226,14 @@ public class ProductCoupon {
         this.productLevelId = productLevelId;
     }
 
+    public List<Long> getProductLevelIds() {
+        return productLevelIds;
+    }
+
+    public void setProductLevelIds(List<Long> productLevelIds) {
+        this.productLevelIds = productLevelIds;
+    }
+
     public BigDecimal getCouponAmount() {
         return couponAmount;
     }

+ 30 - 3
virgo.api/src/main/java/com/bosshand/virgo/api/workark/service/ProductCouponService.java

@@ -8,7 +8,10 @@ import org.springframework.beans.BeanUtils;
 import org.springframework.beans.factory.annotation.Autowired;
 import org.springframework.stereotype.Service;
 
+import java.time.LocalDateTime;
+import java.time.ZoneId;
 import java.util.ArrayList;
+import java.util.Date;
 import java.util.List;
 
 @Service
@@ -32,7 +35,11 @@ public class ProductCouponService {
         productCouponDao.update(productCoupon);
     }
 
-    public List<ProductCoupon> getList(ProductCoupon productCoupon){
+    public List<ProductCoupon> getList(ProductCoupon productCoupon) {
+        List<ProductCoupon> list = productCouponDao.getList(productCoupon);
+        for (ProductCoupon p : list) {
+            updateTerminateState(p);
+        }
         return productCouponDao.getList(productCoupon);
     }
 
@@ -66,11 +73,31 @@ public class ProductCouponService {
         productCoupon.setUserId(userId);
         productCoupon.setProductCouponModelId(productCouponModel.getId());
         productCouponDao.save(productCoupon);
-        productCoupon.setState(1);
-        productCouponDao.update(productCoupon);
+
+        ProductCoupon updateProductCoupon = productCouponDao.get(productCoupon.getId());
+        updateProductCoupon.setState(1);
+        updateProductCoupon.setReceiveTime(new Date());
+        // 领取时间
+        LocalDateTime receiveTime = updateProductCoupon.getReceiveTime().toInstant().atZone(ZoneId.systemDefault()).toLocalDateTime();
+        // 加小时
+        LocalDateTime newDateTime = receiveTime.plusHours(updateProductCoupon.getLifespan());
+        // 失效时间
+        Date date = Date.from(newDateTime.atZone(ZoneId.systemDefault()).toInstant());
+        updateProductCoupon.setTerminateTime(date);
+        productCouponDao.update(updateProductCoupon);
     }
 
     public ProductCoupon getUserId(long productCouponId, long userId) {
         return productCouponDao.getUserId(productCouponId, userId);
     }
+
+    // 判断优惠券是否过期
+    public void updateTerminateState(ProductCoupon productCoupon) {
+        Date currentDate = new Date();
+        boolean isBefore = productCoupon.getTerminateTime().before(currentDate);
+        if (isBefore) {
+            productCouponDao.updateTerminateState(productCoupon.getId());
+        }
+    }
+
 }

+ 21 - 0
virgo.api/src/main/java/com/bosshand/virgo/api/workark/service/ProductLevelService.java

@@ -101,4 +101,25 @@ public class ProductLevelService {
         return list;
     }
 
+    public List<Long> getAllIds(long id) {
+        List<Long> ids = new ArrayList<>();
+        ProductLevel productLevel = this.get(id);
+        getIds(ids, productLevel);
+        ids.add(id);
+        return ids;
+    }
+
+    /**
+     * 获取目录下的所有id
+     */
+    private void getIds(List<Long> ids, ProductLevel productLevel) {
+        List<ProductLevel> children = productLevel.getChildren();
+        if (children.size() > 0) {
+            for (ProductLevel p : children) {
+                ids.add(p.getId());
+                getIds(ids, p);
+            }
+        }
+    }
+
 }

+ 11 - 4
virgo.api/src/main/resources/mapper/ProductCouponMapper.xml

@@ -60,8 +60,11 @@
             <if test="state != null">
                 and state = #{state}
             </if>
-            <if test="productLevelId != 0">
-                and productLevelId = #{productLevelId}
+            <if test="productLevelIds != null">
+                and productLevelId in
+                <foreach collection="productLevelIds" open="(" separator="," close=")" item="id">
+                    #{id}
+                </foreach>
             </if>
         </where>
     </select>
@@ -136,9 +139,9 @@
             <if test="organizationId!=0">organizationId=#{organizationId},</if>
             <if test="userId!=0">userId=#{userId},</if>
             <if test="state!=null">state=#{state},</if>
-            <if test="state==1">receiveTime=now(),</if>
+            <if test="receiveTime!=null">receiveTime=#{receiveTime},</if>
             <if test="state==2">useTime=now(),</if>
-            <if test="state==3">terminateTime=now(),</if>
+            <if test="terminateTime!=null">terminateTime=#{terminateTime},</if>
             <if test="lifespan!=null">lifespan=#{lifespan},</if>
             <if test="productLevelId!=0">productLevelId=#{productLevelId},</if>
             <if test="couponAmount!=null">couponAmount=#{couponAmount},</if>
@@ -151,6 +154,10 @@
         WHERE id=#{id}
     </update>
 
+    <update id="updateTerminateState" parameterType="com.bosshand.virgo.api.workark.model.ProductCoupon">
+        UPDATE workark_product_coupon set state = 3 WHERE id = #{id}
+    </update>
+
     <delete id="delete">
         DELETE FROM workark_product_coupon WHERE id=#{id}
     </delete>