Explorar el Código

商家端代金券代码

wuchen hace 2 meses
padre
commit
a5ac292a7b
Se han modificado 18 ficheros con 393 adiciones y 163 borrados
  1. 1 1
      alien-entity/src/main/java/shop/alien/entity/store/LifeDiscountCoupon.java
  2. 8 1
      alien-entity/src/main/java/shop/alien/entity/store/LifeDiscountCouponFriendRuleDetail.java
  3. 5 1
      alien-entity/src/main/java/shop/alien/entity/store/LifeDiscountCouponStoreFriend.java
  4. 4 1
      alien-entity/src/main/java/shop/alien/entity/store/dto/LifeDiscountCouponStoreFriendDto.java
  5. 3 0
      alien-entity/src/main/java/shop/alien/entity/store/vo/LifeDiscountCouponFriendRuleDetailVo.java
  6. 6 0
      alien-entity/src/main/java/shop/alien/entity/store/vo/LifeDiscountCouponFriendRuleVo.java
  7. 3 0
      alien-entity/src/main/java/shop/alien/entity/store/vo/LifeDiscountCouponVo.java
  8. 18 7
      alien-entity/src/main/java/shop/alien/mapper/LifeDiscountCouponFriendRuleDetailMapper.java
  9. 25 0
      alien-entity/src/main/java/shop/alien/mapper/LifeDiscountCouponMapper.java
  10. 4 3
      alien-entity/src/main/resources/mapper/LifeDiscountCouponFriendRuleDetailMapper.xml
  11. 24 4
      alien-entity/src/main/resources/mapper/LifeDiscountCouponMapper.xml
  12. 2 1
      alien-entity/src/main/resources/mapper/LifeDiscountCouponStoreFriendMapper.xml
  13. 8 8
      alien-store/src/main/java/shop/alien/store/controller/LifeDiscountCouponController.java
  14. 19 15
      alien-store/src/main/java/shop/alien/store/controller/LifeDiscountCouponStoreFriendController.java
  15. 2 1
      alien-store/src/main/java/shop/alien/store/service/LifeDiscountCouponService.java
  16. 4 1
      alien-store/src/main/java/shop/alien/store/service/LifeDiscountCouponStoreFriendService.java
  17. 62 11
      alien-store/src/main/java/shop/alien/store/service/impl/LifeDiscountCouponServiceImpl.java
  18. 195 108
      alien-store/src/main/java/shop/alien/store/service/impl/LifeDiscountCouponStoreFriendServiceImpl.java

+ 1 - 1
alien-entity/src/main/java/shop/alien/entity/store/LifeDiscountCoupon.java

@@ -77,7 +77,7 @@ public class LifeDiscountCoupon extends Model<LifeDiscountCoupon> {
     @TableField(value = "minimum_spending_amount", fill = FieldFill.UPDATE)
     private BigDecimal minimumSpendingAmount;
 
-    @ApiModelProperty(value = "类型   1-优惠券  2-红包 3-平台优惠券")
+    @ApiModelProperty(value = "类型   1-优惠券  2-红包 3-平台优惠券 4代金券")
     @TableField("type")
     private Integer type;
 

+ 8 - 1
alien-entity/src/main/java/shop/alien/entity/store/LifeDiscountCouponFriendRuleDetail.java

@@ -29,13 +29,20 @@ public class LifeDiscountCouponFriendRuleDetail {
     private Integer ruleId;
 
     /**
-     * 优惠券id
+     * 优惠券id(优惠券时使用,对应 life_discount_coupon.id)
      */
     @ApiModelProperty(value = "优惠券id")
     @TableField("coupon_id")
     private Integer couponId;
 
     /**
+     * 代金券id(代金券时使用,对应 life_coupon.id)
+     */
+    @ApiModelProperty(value = "代金券id")
+    @TableField("voucher_id")
+    private String voucherId;
+
+    /**
      * 好友店铺id
      */
     @ApiModelProperty(value = "好友店铺id")

+ 5 - 1
alien-entity/src/main/java/shop/alien/entity/store/LifeDiscountCouponStoreFriend.java

@@ -36,10 +36,14 @@ public class LifeDiscountCouponStoreFriend extends Model<LifeDiscountCouponStore
     @TableField("store_user_id")
     private Integer storeUserId;
 
-    @ApiModelProperty(value = "券id")
+    @ApiModelProperty(value = "券id,优惠券时使用(life_discount_coupon.id)")
     @TableField("coupon_id")
     private Integer couponId;
 
+    @ApiModelProperty(value = "代金券id,代金券时使用(life_coupon.id)")
+    @TableField("voucher_id")
+    private String voucherId;
+
     @ApiModelProperty(value = "好友店铺id")
     @TableField("friend_store_user_id")
     private Integer friendStoreUserId;

+ 4 - 1
alien-entity/src/main/java/shop/alien/entity/store/dto/LifeDiscountCouponStoreFriendDto.java

@@ -33,9 +33,12 @@ public class LifeDiscountCouponStoreFriendDto extends Model<LifeDiscountCouponSt
     @ApiModelProperty(value = "店铺用户id")
     private Integer storeUserId;
 
-    @ApiModelProperty(value = "券id")
+    @ApiModelProperty(value = "券id,优惠券时传(对应 life_discount_coupon.id)")
     private Integer couponId;
 
+    @ApiModelProperty(value = "代金券id,代金券时传(对应 life_coupon.id)")
+    private String voucherId;
+
     @ApiModelProperty(value = "好友店铺id")
     private Integer friendStoreUserId;
 

+ 3 - 0
alien-entity/src/main/java/shop/alien/entity/store/vo/LifeDiscountCouponFriendRuleDetailVo.java

@@ -22,4 +22,7 @@ public class LifeDiscountCouponFriendRuleDetailVo extends LifeDiscountCouponFrie
     private Integer couponNum;
 
     private Integer friendStoreUserId;
+
+    /** 代金券id(type=4 时返回,对应 life_coupon.id) */
+    private String voucherId;
 }

+ 6 - 0
alien-entity/src/main/java/shop/alien/entity/store/vo/LifeDiscountCouponFriendRuleVo.java

@@ -42,6 +42,12 @@ public class LifeDiscountCouponFriendRuleVo extends LifeDiscountCouponFriendRule
     @ApiModelProperty(value = "优惠券id")
     private Integer couponId;
 
+    @ApiModelProperty(value = "代金券id,type=4 时有值")
+    private String voucherId;
+
+    @ApiModelProperty(value = "类型:1=优惠券,4=代金券")
+    private Integer type;
+
     @ApiModelProperty(value = "面值")
     private BigDecimal nominalValue;
 

+ 3 - 0
alien-entity/src/main/java/shop/alien/entity/store/vo/LifeDiscountCouponVo.java

@@ -45,6 +45,9 @@ public class LifeDiscountCouponVo {
     @ApiModelProperty(value = "券id")
     private Integer couponId;
 
+    @ApiModelProperty(value = "代金券id(来自life_coupon时使用)")
+    private String voucherId;
+
     @ApiModelProperty(value = "是否可领")
     private boolean canReceived;
 

+ 18 - 7
alien-entity/src/main/java/shop/alien/mapper/LifeDiscountCouponFriendRuleDetailMapper.java

@@ -25,13 +25,24 @@ public interface LifeDiscountCouponFriendRuleDetailMapper extends BaseMapper<Lif
     @Select("select si.store_name storeName,ldc.name couponName,ldc.id couponId,sum(ldcsf.single_qty) couponNum,ldcsf.friend_store_user_id friendStoreUserId from life_discount_coupon_store_friend ldcsf left join life_discount_coupon ldc on ldcsf.coupon_id = ldc.id left join store_user su on ldcsf.friend_store_user_id = su.id left join store_info si on su.store_id = si.id ${ew.customSqlSegment}")
     List<LifeDiscountCouponFriendRuleDetailVo> getReceivedFriendCouponList(@Param(Constants.WRAPPER) QueryWrapper<LifeDiscountCouponFriendRuleDetailVo> queryWrapper);
 
-    @Select("SELECT\n" +
-            "\ta.*,\n" +
-            "\t(select MAX( end_get_date ) from life_discount_coupon c left join life_discount_coupon_friend_rule_detail b on c.id = b.coupon_id where b.rule_id = a.id) endDate \n" +
-            "FROM\n" +
-            "\tlife_discount_coupon_friend_rule a\n" +
-            "\t where a.store_id = #{storeId} and a.delete_flag = 0 order by endDate")
-    List<LifeDiscountCouponFriendRuleVo> getRuleList(@Param("storeId")String storeId);
+    /** 查询收到的代金券列表(life_coupon,voucher_id 不为空) */
+    @Select("select si.store_name storeName,lc.name couponName,lc.id voucherId,sum(ldcsf.single_qty) couponNum,ldcsf.friend_store_user_id friendStoreUserId from life_discount_coupon_store_friend ldcsf left join life_coupon lc on ldcsf.voucher_id = lc.id left join store_user su on ldcsf.friend_store_user_id = su.id left join store_info si on su.store_id = si.id ${ew.customSqlSegment}")
+    List<LifeDiscountCouponFriendRuleDetailVo> getReceivedFriendVoucherList(@Param(Constants.WRAPPER) QueryWrapper<LifeDiscountCouponFriendRuleDetailVo> queryWrapper);
+
+    /** 一条 SQL 同时查规则下的优惠券(type=1)与代金券(type=4),按 type 区分 */
+    @Select("SELECT a.id, a.store_id, a.ac_name, a.money_low, a.money_high, a.delete_flag, " +
+            "CASE WHEN d.voucher_id IS NOT NULL THEN 4 ELSE 1 END AS type, " +
+            "COALESCE(c.end_get_date, v.end_date) AS endDate, " +
+            "COALESCE(c.name, v.name) AS couponName, " +
+            "c.id AS couponId, " +
+            "d.voucher_id AS voucherId " +
+            "FROM life_discount_coupon_friend_rule a " +
+            "INNER JOIN life_discount_coupon_friend_rule_detail d ON d.rule_id = a.id " +
+            "LEFT JOIN life_discount_coupon c ON d.coupon_id = c.id " +
+            "LEFT JOIN life_coupon v ON d.voucher_id = v.id " +
+            "WHERE a.store_id = #{storeId} AND a.delete_flag = 0 " +
+            "ORDER BY endDate DESC")
+    List<LifeDiscountCouponFriendRuleVo> getRuleList(@Param("storeId") String storeId);
 
     @Select("SELECT a.*, c.name as couponName, c.id as couponId,si.store_name storeName, " +
             "(SELECT\n" +

+ 25 - 0
alien-entity/src/main/java/shop/alien/mapper/LifeDiscountCouponMapper.java

@@ -9,6 +9,9 @@ import org.apache.ibatis.annotations.Select;
 import org.apache.ibatis.annotations.Update;
 import shop.alien.entity.store.LifeDiscountCoupon;
 import shop.alien.entity.store.vo.LifeDiscountCouponPlatformVo;
+import shop.alien.entity.store.vo.LifeDiscountCouponVo;
+
+import java.util.List;
 
 /**
  * <p>
@@ -48,4 +51,26 @@ public interface LifeDiscountCouponMapper extends BaseMapper<LifeDiscountCoupon>
      */
     @Update("UPDATE life_discount_coupon SET single_qty = #{singleQty} where id= #{id}")
     Integer updateCouponById(@Param("id") Integer id, @Param("singleQty") Integer singleQty);
+
+
+    List<LifeDiscountCouponVo> getList(String storeId,int couponStatus,Integer type);
+
+    /**
+     * 关联 life_coupon 查询代金券(仅 type=4):按店铺、券状态、有库存
+     *
+     * @param storeId      店铺ID
+     * @param couponStatus 券状态
+     * @return 符合条件的 life_discount_coupon 列表
+     */
+    List<LifeDiscountCoupon> selectListJoinLifeCoupon(@Param("storeId") String storeId, @Param("couponStatus") Integer couponStatus);
+
+    /**
+     * 单表查询 life_discount_coupon(type 不为 4 时使用)
+     *
+     * @param storeId      店铺ID
+     * @param couponStatus 券状态
+     * @param type         类型,可为 null(不按 type 过滤)
+     * @return 符合条件的 life_discount_coupon 列表
+     */
+    List<LifeDiscountCoupon> selectListSingleTable(@Param("storeId") String storeId, @Param("couponStatus") Integer couponStatus, @Param("type") Integer type);
 }

+ 4 - 3
alien-entity/src/main/resources/mapper/LifeDiscountCouponFriendRuleDetailMapper.xml

@@ -8,17 +8,18 @@
             <id property="id" column="id" />
             <result property="ruleId" column="rule_id" />
             <result property="couponId" column="coupon_id" />
+            <result property="voucherId" column="voucher_id" />
     </resultMap>
 
     <sql id="Base_Column_List">
-        id,rule_id,coupon_id
+        id,rule_id,coupon_id,voucher_id
     </sql>
 
     <insert id="insertList" parameterType="java.util.List">
-        INSERT INTO life_discount_coupon_friend_rule_detail (rule_id, coupon_id, friend_store_user_id,store_id)
+        INSERT INTO life_discount_coupon_friend_rule_detail (rule_id, coupon_id, voucher_id, friend_store_user_id, store_id)
         VALUES
         <foreach collection="list" item="item" separator=",">
-            (#{item.ruleId}, #{item.couponId}, #{item.friendStoreUserId}, #{item.storeId})
+            (#{item.ruleId}, #{item.couponId}, #{item.voucherId}, #{item.friendStoreUserId}, #{item.storeId})
         </foreach>
     </insert>
 

+ 24 - 4
alien-entity/src/main/resources/mapper/LifeDiscountCouponMapper.xml

@@ -27,9 +27,29 @@
         <result column="updated_user_id" property="updatedUserId" />
     </resultMap>
 
-    <!-- 通用查询结果列 -->
-    <sql id="Base_Column_List">
-        id, store_id, name, nominal_value, expiration_date, start_date, end_date, single_qty, supplementary_instruction, status, image_path, get_status, restricted_quantity, minimum_spending_amount, type, approval_comments, delete_flag, created_time, updated_time, created_user_id, updated_user_id
-    </sql>
+
+
+    <!-- 关联 life_coupon 查询代金券(仅 type=4):按 storeId、couponStatus、single_qty!=0,返回 dis 表以匹配 BaseResultMap -->
+    <select id="selectListJoinLifeCoupon" resultMap="BaseResultMap">
+        SELECT dis.*
+        FROM life_discount_coupon dis
+        JOIN life_coupon c ON dis.store_id = c.store_id
+        WHERE dis.store_id = #{storeId}
+          AND dis.type = 4
+          AND dis.coupon_status = #{couponStatus}
+          AND dis.single_qty != 0
+    </select>
+
+    <!-- 单表查询 life_discount_coupon:type 不为 4 时使用 -->
+    <select id="selectListSingleTable" resultMap="BaseResultMap">
+        SELECT *
+        FROM life_discount_coupon
+        WHERE store_id = #{storeId}
+          AND coupon_status = #{couponStatus}
+          AND single_qty != 0
+        <if test="type != null">
+          AND type = #{type}
+        </if>
+    </select>
 
 </mapper>

+ 2 - 1
alien-entity/src/main/resources/mapper/LifeDiscountCouponStoreFriendMapper.xml

@@ -7,6 +7,7 @@
         <id column="id" property="id" />
         <result column="store_user_id" property="storeUserId" />
         <result column="coupon_id" property="couponId" />
+        <result column="voucher_id" property="voucherId" />
         <result column="friend_store_user_id" property="friendStoreUserId" />
         <result column="delete_flag" property="deleteFlag" />
         <result column="created_time" property="createdTime" />
@@ -17,7 +18,7 @@
 
     <!-- 通用查询结果列 -->
     <sql id="Base_Column_List">
-        id, store_user_id, coupon_id, friend_store_user_id, delete_flag, created_time, updated_time, created_user_id, updated_user_id
+        id, store_user_id, coupon_id, voucher_id, friend_store_user_id, delete_flag, created_time, updated_time, created_user_id, updated_user_id
     </sql>
 
 </mapper>

+ 8 - 8
alien-store/src/main/java/shop/alien/store/controller/LifeDiscountCouponController.java

@@ -229,20 +229,20 @@ public class LifeDiscountCouponController {
         }
     }
 
-    @ApiOperation("获取该店铺所有优惠券(不分页)")
+    @ApiOperation("获取该店铺所有优惠券 代金券(不分页)")
     @ApiOperationSupport(order = 11)
     @GetMapping("/getStoreAllCouponListPaginateNot")
     @ApiImplicitParams({
-            @ApiImplicitParam(name = "storeId", value = "商户id", dataType = "String", paramType = "query", required = true)
+            @ApiImplicitParam(name = "storeId", value = "商户id", dataType = "String", paramType = "query", required = true),
+            @ApiImplicitParam(name = "type", value = "类型:1-优惠券(默认),4-代金券", dataType = "Integer", paramType = "query")
     })
     public R<List<LifeDiscountCouponVo>> getStoreAllCouponListPaginateNot(@ApiIgnore @TokenInfo UserLoginInfo userLoginInfo,
-                                                                          @RequestParam(value = "storeId") String storeId,
-                                                                          @RequestParam(value = "status", required = false) String status,
-                                                                          @RequestParam(value = "couponStatus", defaultValue = "1", required = false) int couponStatus
-    ) {
-        log.info("LifeDiscountCouponController.getStoreAllCouponListPaginateNot?storeId={},status={}, couponStatus={}", storeId, status, couponStatus);
+                                                                          @RequestParam(value = "storeId") String storeId,@RequestParam(value = "status", required = false) String status,
+                                                                          @RequestParam(value = "couponStatus", defaultValue = "1", required = false) int couponStatus,
+                                                                          @RequestParam(value = "type", defaultValue = "1", required = false) Integer type) {
+        log.info("LifeDiscountCouponController.getStoreAllCouponListPaginateNot?storeId={}, status={}, couponStatus={}, type={}", storeId, status, couponStatus, type);
         try {
-            List<LifeDiscountCouponVo> storeCouponList = lifeDiscountCouponService.getStoreAllCouponListPaginateNot(status, storeId, userLoginInfo, couponStatus);
+            List<LifeDiscountCouponVo> storeCouponList = lifeDiscountCouponService.getStoreAllCouponListPaginateNot(status, storeId, userLoginInfo, couponStatus, type);
             return R.data(storeCouponList);
         } catch (Exception e) {
             log.error("LifeDiscountCouponController.getStoreAllCouponListPaginateNot ERROR Msg={}", e.getMessage());

+ 19 - 15
alien-store/src/main/java/shop/alien/store/controller/LifeDiscountCouponStoreFriendController.java

@@ -54,12 +54,12 @@ public class LifeDiscountCouponStoreFriendController {
         }
     }
 
-//    @TrackEvent(
-//            eventType = "COUPON_GIVE",
-//            eventCategory = "COUPON",
-//            storeId = "#{#lifeDiscountCouponStoreFriendDto.storeId}",
-//            targetType = "COUPON"
-//    )
+@TrackEvent(
+          eventType = "COUPON_GIVE",
+           eventCategory = "COUPON",
+           storeId = "#{#lifeDiscountCouponStoreFriendDto.storeId}",
+           targetType = "COUPON"
+   )
     @ApiOperation("给好友发放优惠券")
     @ApiOperationSupport(order = 2)
     @PostMapping("/setFriendCoupon")
@@ -153,26 +153,30 @@ public class LifeDiscountCouponStoreFriendController {
     @ApiImplicitParams({@ApiImplicitParam(name = "id", value = "id", dataType = "String", paramType = "query", required = true)
     })
     @GetMapping("/getRuleById")
-    private R<LifeDiscountCouponFriendRuleVo> getRuleById(@RequestParam(value = "id") String id) {
+    public R<LifeDiscountCouponFriendRuleVo> getRuleById(@RequestParam(value = "id") String id) {
         log.info("LifeDiscountCouponStoreFriendController.getRuleById?id={}", id);
         return R.data(lifeDiscountCouponStoreFriendService.getRuleById(id));
     }
 
-    @ApiOperation("查询好友赠券")
-    @ApiImplicitParams({@ApiImplicitParam(name = "storeId", value = "当前登录店铺id", dataType = "String", paramType = "query", required = true)
-    ,@ApiImplicitParam(name = "friendStoreUserId", value = "选中好友店铺用户id", dataType = "String", paramType = "query", required = false)
+    @ApiOperation("查询好友赠券。type=4 返回代金券,否则返回优惠券")
+    @ApiImplicitParams({
+            @ApiImplicitParam(name = "storeId", value = "当前登录店铺id", dataType = "String", paramType = "query", required = true),
+            @ApiImplicitParam(name = "friendStoreUserId", value = "选中好友店铺用户id", dataType = "String", paramType = "query", required = false),
+            @ApiImplicitParam(name = "type", value = "4=代金券,其他=优惠券", dataType = "Integer", paramType = "query", required = false)
     })
     @GetMapping("/getReceivedFriendCouponList")
-    private R<List<LifeDiscountCouponFriendRuleDetailVo>> getReceivedFriendCouponList(@RequestParam(value = "storeId") String storeId, @RequestParam(value = "friendStoreUserId",required = false)String friendStoreUserId) {
-        log.info("LifeDiscountCouponStoreFriendController.getReceivedFriendCouponList?storeId={},friendStoreUserId={}", storeId,friendStoreUserId);
-        return R.data(lifeDiscountCouponStoreFriendService.getReceivedFriendCouponList(storeId,friendStoreUserId));
+    public R<List<LifeDiscountCouponFriendRuleDetailVo>> getReceivedFriendCouponList(@RequestParam(value = "storeId") String storeId,
+                                                                                     @RequestParam(value = "friendStoreUserId", required = false) String friendStoreUserId,
+                                                                                     @RequestParam(value = "type", required = false) Integer type) {
+        log.info("LifeDiscountCouponStoreFriendController.getReceivedFriendCouponList?storeId={},friendStoreUserId={},type={}", storeId, friendStoreUserId, type);
+        return R.data(lifeDiscountCouponStoreFriendService.getReceivedFriendCouponList(storeId, friendStoreUserId, type));
     }
 
     @ApiOperation("查询赠券规则")
     @ApiImplicitParams({@ApiImplicitParam(name = "storeId", value = "当前登录店铺id", dataType = "String", paramType = "query", required = true)
     })
     @GetMapping("/getRuleList")
-    private R<List<LifeDiscountCouponFriendRuleVo>> getRuleList(@RequestParam(value = "storeId") String storeId, String acName, String status) {
+    public R<List<LifeDiscountCouponFriendRuleVo>> getRuleList(@RequestParam(value = "storeId") String storeId, String acName, String status) {
         log.info("LifeDiscountCouponStoreFriendController.getRuleList?storeId={},name={},status={}", storeId, acName, status);
         return R.data(lifeDiscountCouponStoreFriendService.getRuleList(storeId, acName, status));
     }
@@ -182,7 +186,7 @@ public class LifeDiscountCouponStoreFriendController {
             ,@ApiImplicitParam(name = "friendStoreUserId", value = "我赠好友-选中好友店铺用户id", dataType = "String", paramType = "query", required = false)
     })
     @GetMapping("/getReceivedSendFriendCouponList")
-    private R<List<LifeDiscountCouponFriendRuleVo>> getReceivedSendFriendCouponList(@RequestParam(value = "storeUserId",required = false) String storeUserId, @RequestParam(value = "friendStoreUserId",required = false)String friendStoreUserId, @RequestParam(value = "storeName",required = false)String storeName) {
+    public R<List<LifeDiscountCouponFriendRuleVo>> getReceivedSendFriendCouponList(@RequestParam(value = "storeUserId",required = false) String storeUserId, @RequestParam(value = "friendStoreUserId",required = false)String friendStoreUserId, @RequestParam(value = "storeName",required = false)String storeName) {
         log.info("LifeDiscountCouponStoreFriendController.getReceivedSendFriendCouponList?storeId={},friendStoreUserId={},storeName={}", storeUserId,friendStoreUserId,storeName);
         return R.data(lifeDiscountCouponStoreFriendService.getReceivedSendFriendCouponList(storeUserId,friendStoreUserId,storeName));
 

+ 2 - 1
alien-store/src/main/java/shop/alien/store/service/LifeDiscountCouponService.java

@@ -73,8 +73,9 @@ public interface LifeDiscountCouponService extends IService<LifeDiscountCoupon>
 
     /**
      * 获取所有优惠券列表(不分页)
+     * @param type 类型:1-优惠券(默认),4-代金券
      */
-    List<LifeDiscountCouponVo> getStoreAllCouponListPaginateNot(String status, String storeId, UserLoginInfo userLoginInfo, int couponStatus);
+    List<LifeDiscountCouponVo> getStoreAllCouponListPaginateNot(String status, String storeId, UserLoginInfo userLoginInfo, int couponStatus, Integer type);
 
     /**
      * 获取优惠券规则

+ 4 - 1
alien-store/src/main/java/shop/alien/store/service/LifeDiscountCouponStoreFriendService.java

@@ -53,7 +53,10 @@ public interface LifeDiscountCouponStoreFriendService extends IService<LifeDisco
 
     void delFriendCouponRule(String id);
 
-    List<LifeDiscountCouponFriendRuleDetailVo> getReceivedFriendCouponList(String storeId,String friendStoreUserId);
+    /**
+     * 查询收到的赠券列表。type=4 返回代金券(life_coupon),否则返回优惠券(life_discount_coupon)
+     */
+    List<LifeDiscountCouponFriendRuleDetailVo> getReceivedFriendCouponList(String storeId, String friendStoreUserId, Integer type);
 
     List<LifeDiscountCouponFriendRuleVo> getRuleList(String storeId, String acName, String status);
 

+ 62 - 11
alien-store/src/main/java/shop/alien/store/service/impl/LifeDiscountCouponServiceImpl.java

@@ -68,6 +68,8 @@ public class LifeDiscountCouponServiceImpl extends ServiceImpl<LifeDiscountCoupo
 
     private final LifeDiscountCouponUserService lifeDiscountCouponUserService;
 
+    private final LifeCouponMapper lifeCouponMapper;
+
     @Override
     public boolean addDiscountCoupon(LifeDiscountCouponDto lifeDiscountCouponDto) {
         try {
@@ -891,24 +893,39 @@ public class LifeDiscountCouponServiceImpl extends ServiceImpl<LifeDiscountCoupo
     }
 
     @Override
-    public List<LifeDiscountCouponVo> getStoreAllCouponListPaginateNot(String status, String storeId, UserLoginInfo userLoginInfo, int couponStatus) {
-        List<LifeDiscountCouponVo> lifeDiscountCouponVos = new ArrayList<>();
-        //根据店铺id查询该店铺的优惠券,状态是开启领取的券
-        List<LifeDiscountCoupon> lifeDiscountCoupons = lifeDiscountCouponMapper.selectList(new LambdaQueryWrapper<LifeDiscountCoupon>().eq(LifeDiscountCoupon::getStoreId, storeId).eq(LifeDiscountCoupon::getCouponStatus, couponStatus).ne(LifeDiscountCoupon::getSingleQty, 0).orderByDesc(LifeDiscountCoupon::getCreatedTime));
+    public List<LifeDiscountCouponVo> getStoreAllCouponListPaginateNot(String status, String storeId, UserLoginInfo userLoginInfo, int couponStatus, Integer type) {
+        List<LifeDiscountCouponVo> result = new ArrayList<>();
+        if (Integer.valueOf(4).equals(type)) {
+            // type=4:直接查 life_coupon(代金券),返回代金券数据。life_coupon.type=1 为代金券,data_type 0正式/1草稿 与 couponStatus 1/0 对应
+            LambdaQueryWrapper<LifeCoupon> wrapper = new LambdaQueryWrapper<LifeCoupon>()
+                    .eq(LifeCoupon::getStoreId, storeId)
+                    .eq(LifeCoupon::getType, 1)
+                    .ne(LifeCoupon::getSingleQty, 0)
+                    .eq(LifeCoupon::getDataType, 1 - couponStatus);
+            List<LifeCoupon> lifeCoupons = lifeCouponMapper.selectList(wrapper);
+            for (LifeCoupon lc : lifeCoupons) {
+                LifeDiscountCouponVo vo = mapLifeCouponToVo(lc);
+                if (!StringUtils.isEmpty(status) && !status.equals(vo.getStatus() != null ? vo.getStatus().toString() : "")) {
+                    continue;
+                }
+                result.add(vo);
+            }
+            return result;
+        }
+
+        // type 不为 4:只查 life_discount_coupon 单表(优惠券列表)
+        List<LifeDiscountCoupon> lifeDiscountCoupons = lifeDiscountCouponMapper.selectListSingleTable(storeId, couponStatus, type);
         for (LifeDiscountCoupon lifeDiscountCoupon : lifeDiscountCoupons) {
             LifeDiscountCouponVo lifeDiscountCouponVo = new LifeDiscountCouponVo();
             lifeDiscountCouponVo.setCouponId(lifeDiscountCoupon.getId());
-            //处理一下该优惠券的状态 状态(0:进行中,1:已结束,2:未开始,3:已暂停)
-            // 获取当前时间
+            lifeDiscountCouponVo.setType(lifeDiscountCoupon.getType());
             Date now = new Date();
-            // 将 Date 转换为 LocalDate
             Instant instant = now.toInstant();
             ZoneId zoneId = ZoneId.systemDefault();
             LocalDate localNow = instant.atZone(zoneId).toLocalDate();
             if (!StringUtils.isEmpty(lifeDiscountCoupon.getBeginGetDate()) && !StringUtils.isEmpty(lifeDiscountCoupon.getEndGetDate())) {
                 int startResult = localNow.compareTo(lifeDiscountCoupon.getBeginGetDate());
                 int endResult = localNow.compareTo(lifeDiscountCoupon.getEndGetDate());
-                //如果当前时间小于开始时间
                 if (lifeDiscountCoupon.getGetStatus().toString().equals(DiscountCouponEnum.NO_GET.getValue())) {
                     lifeDiscountCouponVo.setStatus(Integer.parseInt(DiscountCouponEnum.SUSPEND_GET.getValue()));
                 } else if (startResult < 0) {
@@ -920,13 +937,47 @@ public class LifeDiscountCouponServiceImpl extends ServiceImpl<LifeDiscountCoupo
                 }
             }
             BeanUtils.copyProperties(lifeDiscountCoupon, lifeDiscountCouponVo);
-            //如果按照状态查询了
+            lifeDiscountCouponVo.setCreatedTime(lifeDiscountCoupon.getCreatedTime() != null ? lifeDiscountCoupon.getCreatedTime() : null);
             if (!StringUtils.isEmpty(status) && !status.equals(lifeDiscountCouponVo.getStatus().toString())) {
                 continue;
             }
-            lifeDiscountCouponVos.add(lifeDiscountCouponVo);
+            result.add(lifeDiscountCouponVo);
         }
-        return lifeDiscountCouponVos;
+        return result;
+    }
+
+    /** 将 life_coupon(代金券)转为 LifeDiscountCouponVo,便于与 life_discount_coupon 统一返回 */
+    private LifeDiscountCouponVo mapLifeCouponToVo(LifeCoupon lc) {
+        LifeDiscountCouponVo vo = new LifeDiscountCouponVo();
+        vo.setVoucherId(lc.getId());
+        vo.setStoreId(lc.getStoreId());
+        vo.setName(lc.getName());
+        vo.setSingleQty(lc.getSingleQty());
+        vo.setCreatedTime(lc.getCreatedTime());
+        vo.setType(4);
+        if (lc.getStartDate() != null) {
+            vo.setStartDate(lc.getStartDate().toInstant().atZone(ZoneId.systemDefault()).toLocalDate());
+        }
+        if (lc.getEndDate() != null) {
+            vo.setEndDate(lc.getEndDate().toInstant().atZone(ZoneId.systemDefault()).toLocalDate());
+        }
+        if (lc.getPrice() != null) {
+            try {
+                vo.setNominalValue(new BigDecimal(lc.getPrice()));
+            } catch (Exception ignored) { }
+        }
+        if (lc.getStatus() != null) {
+            if (lc.getStatus() == 5) {
+                vo.setStatus(0);
+            } else if (lc.getStatus() == 7 || lc.getStatus() == 6) {
+                vo.setStatus(1);
+            } else if (lc.getStatus() == 2) {
+                vo.setStatus(2);
+            } else {
+                vo.setStatus(3);
+            }
+        }
+        return vo;
     }
 
     @Override

+ 195 - 108
alien-store/src/main/java/shop/alien/store/service/impl/LifeDiscountCouponStoreFriendServiceImpl.java

@@ -22,6 +22,7 @@ import shop.alien.util.common.constant.DiscountCouponEnum;
 
 import java.math.BigDecimal;
 import java.time.LocalDate;
+import java.time.ZoneId;
 import java.util.ArrayList;
 import java.util.Arrays;
 import java.util.Date;
@@ -48,6 +49,8 @@ public class LifeDiscountCouponStoreFriendServiceImpl extends ServiceImpl<LifeDi
 
     private final LifeDiscountCouponMapper lifeDiscountCouponMapper;
 
+    private final LifeCouponMapper lifeCouponMapper;
+
     private final LifeDiscountCouponStoreFriendMapper lifeDiscountCouponStoreFriendMapper;
 
     private final LifeDiscountCouponUserMapper lifeDiscountCouponUserMapper;
@@ -137,109 +140,152 @@ public class LifeDiscountCouponStoreFriendServiceImpl extends ServiceImpl<LifeDi
      */
     public boolean setFriendCoupon(UserLoginInfo userLoginInfo, LifeDiscountCouponStoreFriendDto lifeDiscountCouponStoreFriendDto) {
         try {
-            // 从传入的 DTO 对象中获取要处理的优惠券列表
             List<LifeDiscountCouponStoreFriendDto> coupons = lifeDiscountCouponStoreFriendDto.getCouponIds();
-            // 遍历优惠券列表,对每个优惠券进行处理
+            if (CollectionUtils.isEmpty(coupons)) {
+                return true;
+            }
             for (LifeDiscountCouponStoreFriendDto couponDto : coupons) {
-                // 根据优惠券 ID 从数据库中查询对应的优惠券信息
-                LifeDiscountCoupon lifeDiscountCoupon = lifeDiscountCouponMapper.selectById(couponDto.getCouponId());
-                // 检查优惠券的库存数量是否足够发放,如果库存数小于要发放的数量,则拦截操作并返回 false
-                if ((lifeDiscountCoupon.getSingleQty() - couponDto.getSingleQty()) < 0) {
-                    return false;
-                }
-                // 根据优惠券所属店铺 ID 从数据库中查询对应的店铺用户信息
-                StoreUser storeUser = storeUserMapper.selectOne(new LambdaQueryWrapper<StoreUser>().eq(StoreUser::getStoreId, lifeDiscountCoupon.getStoreId()));
-                // 检查该优惠券是否已经发放给指定的好友,如果发放过则获取发放记录
-                LifeDiscountCouponStoreFriend lifeDiscountCouponStoreFriend = lifeDiscountCouponStoreFriendMapper.selectOne(
-                        new LambdaQueryWrapper<LifeDiscountCouponStoreFriend>()
-                                // 匹配优惠券 ID
-                                .eq(LifeDiscountCouponStoreFriend::getCouponId, couponDto.getCouponId())
-                                // 匹配店铺用户 ID
-                                .eq(LifeDiscountCouponStoreFriend::getStoreUserId, lifeDiscountCouponStoreFriendDto.getFriendStoreUserId())
-                                // 匹配好友店铺用户 ID
-                                .eq(LifeDiscountCouponStoreFriend::getFriendStoreUserId, userLoginInfo.getUserId())
-                );
-
-                if (lifeDiscountCouponStoreFriend == null) {
-                    // 如果该优惠券还没有发放给指定的好友,创建一个新的发放记录
-                    lifeDiscountCouponStoreFriend = new LifeDiscountCouponStoreFriend();
-                    // 将传入的 DTO 对象的属性复制到新的发放记录对象中
-                    BeanUtils.copyProperties(lifeDiscountCouponStoreFriendDto, lifeDiscountCouponStoreFriend);
-                    // 设置优惠券 ID
-                    lifeDiscountCouponStoreFriend.setCouponId(couponDto.getCouponId());
-                    // 设置优惠券的过期日期
-                    lifeDiscountCouponStoreFriend.setExpirationDate(lifeDiscountCoupon.getExpirationDate());
-                    // 设置优惠券的开始日期
-                    lifeDiscountCouponStoreFriend.setStartDate(lifeDiscountCoupon.getStartDate());
-                    // 设置优惠券的结束日期
-                    lifeDiscountCouponStoreFriend.setEndDate(lifeDiscountCoupon.getEndDate());
-                    // 设置好友店铺用户 ID
-                    lifeDiscountCouponStoreFriend.setFriendStoreUserId(storeUser.getId());
-                    // 设置要发放的优惠券数量
-                    lifeDiscountCouponStoreFriend.setSingleQty(couponDto.getSingleQty());
-                    // 设置店铺用户 ID
-                    lifeDiscountCouponStoreFriend.setStoreUserId(lifeDiscountCouponStoreFriendDto.getFriendStoreUserId());
-                    // 设置优惠券的发布状态
-                    lifeDiscountCouponStoreFriend.setReleaseType(1);
-                    // 将新的发放记录插入到数据库中
-                    lifeDiscountCouponStoreFriendMapper.insert(lifeDiscountCouponStoreFriend);
-                } else {
-                    // 如果该优惠券已经发放给指定的好友,增加发放记录中的优惠券数量
-                    lifeDiscountCouponStoreFriend.setSingleQty(lifeDiscountCouponStoreFriend.getSingleQty() + couponDto.getSingleQty());
-                    // 更新数据库中的发放记录
-                    lifeDiscountCouponStoreFriendMapper.updateById(lifeDiscountCouponStoreFriend);
-                }
-                // 减少优惠券的库存数量
-                lifeDiscountCoupon.setSingleQty(lifeDiscountCoupon.getSingleQty() - couponDto.getSingleQty());
-                // 更新数据库中的优惠券库存信息
-                lifeDiscountCouponMapper.updateById(lifeDiscountCoupon);
-
-                int friendStoreId =  lifeDiscountCouponStoreFriendDto.getFriendStoreUserId();
-                LambdaQueryWrapper<StoreUser> storeUserLambdaQueryWrapper = new LambdaQueryWrapper<>();
-                storeUserLambdaQueryWrapper.eq(StoreUser::getStoreId, friendStoreId);
-                List<StoreUser> storeUserList = storeUserMapper.selectList(storeUserLambdaQueryWrapper);
-
-                if(CollectionUtils.isNotEmpty(storeUserList)){
-                    StoreUser friendStoreUser = storeUserList.get(0);
-                    String friendPhone = friendStoreUser.getPhone();
-                    if(StringUtils.isNotEmpty(friendPhone)){
-                        // 获取发送优惠券的店铺名称
-                        String storeName = userLoginInfo.getUserName(); // 默认使用用户名
-                        // 根据当前登录用户ID查询店铺用户信息
-                        StoreUser currentStoreUser = storeUserMapper.selectById(userLoginInfo.getUserId());
-                        if (currentStoreUser != null && currentStoreUser.getStoreId() != null) {
-                            // 根据storeId查询店铺信息,获取店铺名称
-                            StoreInfo currentStoreInfo = storeInfoMapper.selectById(currentStoreUser.getStoreId());
-                            if (currentStoreInfo != null && currentStoreInfo.getStoreName() != null) {
-                                storeName = currentStoreInfo.getStoreName();
-                            }
-                        }
-                        // 发送好友优惠券通知
-                        LifeNotice lifeMessage = new LifeNotice();
-                        String text = "您的好友"+storeName+"送了您"+couponDto.getSingleQty()+"张店铺优惠券,快去使用吧!";
-                        JSONObject jsonObject = new JSONObject();
-                        jsonObject.put("message", text);
-                        lifeMessage.setReceiverId("store_"+friendPhone);
-                        lifeMessage.setTitle("赠券通知");
-                        lifeMessage.setContext(jsonObject.toJSONString());
-                        lifeMessage.setNoticeType(1);
-                        lifeMessage.setIsRead(0);
-                        lifeMessage.setDeleteFlag(0);
-                        lifeMessage.setSenderId("system");
-                        lifeNoticeMapper.insert(lifeMessage);
+                if (com.baomidou.mybatisplus.core.toolkit.StringUtils.isNotEmpty(couponDto.getVoucherId())) {
+                    // 代金券:入参为 voucherId,对应 life_coupon 表
+                    if (!handleVoucherFriendCoupon(userLoginInfo, lifeDiscountCouponStoreFriendDto, couponDto)) {
+                        return false;
+                    }
+                } else if (couponDto.getCouponId() != null) {
+                    // 优惠券:入参为 couponId,对应 life_discount_coupon 表
+                    if (!handleDiscountFriendCoupon(userLoginInfo, lifeDiscountCouponStoreFriendDto, couponDto)) {
+                        return false;
                     }
                 }
             }
-            // 如果所有优惠券都成功处理,返回 true
             return true;
         } catch (BeansException e) {
-            // 捕获 Bean 复制过程中可能出现的异常,并打印异常堆栈信息
             e.printStackTrace();
-            // 出现异常时返回 false
             return false;
         }
     }
 
+    /** 处理代金券(life_coupon)发放好友 */
+    private boolean handleVoucherFriendCoupon(UserLoginInfo userLoginInfo, LifeDiscountCouponStoreFriendDto dto, LifeDiscountCouponStoreFriendDto couponDto) {
+        LifeCoupon lifeCoupon = lifeCouponMapper.selectById(couponDto.getVoucherId());
+        if (lifeCoupon == null) {
+            return false;
+        }
+        int qty = couponDto.getSingleQty() != null ? couponDto.getSingleQty() : 0;
+        if (lifeCoupon.getSingleQty() == null || (lifeCoupon.getSingleQty() - qty) < 0) {
+            return false;
+        }
+        StoreUser storeUser = storeUserMapper.selectOne(new LambdaQueryWrapper<StoreUser>().eq(StoreUser::getStoreId, lifeCoupon.getStoreId()));
+        if (storeUser == null) {
+            return false;
+        }
+        LambdaQueryWrapper<LifeDiscountCouponStoreFriend> wrapper = new LambdaQueryWrapper<LifeDiscountCouponStoreFriend>()
+                .eq(LifeDiscountCouponStoreFriend::getVoucherId, couponDto.getVoucherId())
+                .eq(LifeDiscountCouponStoreFriend::getStoreUserId, dto.getFriendStoreUserId())
+                .eq(LifeDiscountCouponStoreFriend::getFriendStoreUserId, userLoginInfo.getUserId());
+        LifeDiscountCouponStoreFriend friend = lifeDiscountCouponStoreFriendMapper.selectOne(wrapper);
+
+        if (friend == null) {
+            friend = new LifeDiscountCouponStoreFriend();
+            friend.setVoucherId(couponDto.getVoucherId());
+            friend.setCouponId(null);
+            friend.setExpirationDate(lifeCoupon.getExpirationDate());
+            if (lifeCoupon.getStartDate() != null) {
+                friend.setStartDate(lifeCoupon.getStartDate().toInstant().atZone(ZoneId.systemDefault()).toLocalDate());
+            }
+            if (lifeCoupon.getEndDate() != null) {
+                friend.setEndDate(lifeCoupon.getEndDate().toInstant().atZone(ZoneId.systemDefault()).toLocalDate());
+            }
+            friend.setFriendStoreUserId(storeUser.getId());
+            friend.setSingleQty(qty);
+            friend.setStoreUserId(dto.getFriendStoreUserId());
+            friend.setReleaseType(1);
+            lifeDiscountCouponStoreFriendMapper.insert(friend);
+        } else {
+            friend.setSingleQty(friend.getSingleQty() + qty);
+            lifeDiscountCouponStoreFriendMapper.updateById(friend);
+        }
+        lifeCoupon.setSingleQty(lifeCoupon.getSingleQty() - qty);
+        lifeCouponMapper.updateById(lifeCoupon);
+        sendFriendCouponNotice(dto.getFriendStoreUserId(), userLoginInfo, qty, true);
+        return true;
+    }
+
+    /** 处理优惠券(life_discount_coupon)发放好友 */
+    private boolean handleDiscountFriendCoupon(UserLoginInfo userLoginInfo, LifeDiscountCouponStoreFriendDto lifeDiscountCouponStoreFriendDto, LifeDiscountCouponStoreFriendDto couponDto) {
+        LifeDiscountCoupon lifeDiscountCoupon = lifeDiscountCouponMapper.selectById(couponDto.getCouponId());
+        if (lifeDiscountCoupon == null || (lifeDiscountCoupon.getSingleQty() - couponDto.getSingleQty()) < 0) {
+            return false;
+        }
+        StoreUser storeUser = storeUserMapper.selectOne(new LambdaQueryWrapper<StoreUser>().eq(StoreUser::getStoreId, lifeDiscountCoupon.getStoreId()));
+        LifeDiscountCouponStoreFriend lifeDiscountCouponStoreFriend = lifeDiscountCouponStoreFriendMapper.selectOne(
+                new LambdaQueryWrapper<LifeDiscountCouponStoreFriend>()
+                        .eq(LifeDiscountCouponStoreFriend::getCouponId, couponDto.getCouponId())
+                        .eq(LifeDiscountCouponStoreFriend::getStoreUserId, lifeDiscountCouponStoreFriendDto.getFriendStoreUserId())
+                        .eq(LifeDiscountCouponStoreFriend::getFriendStoreUserId, userLoginInfo.getUserId())
+        );
+
+        if (lifeDiscountCouponStoreFriend == null) {
+            lifeDiscountCouponStoreFriend = new LifeDiscountCouponStoreFriend();
+            BeanUtils.copyProperties(lifeDiscountCouponStoreFriendDto, lifeDiscountCouponStoreFriend);
+            lifeDiscountCouponStoreFriend.setCouponId(couponDto.getCouponId());
+            lifeDiscountCouponStoreFriend.setVoucherId(null);
+            lifeDiscountCouponStoreFriend.setExpirationDate(lifeDiscountCoupon.getExpirationDate());
+            lifeDiscountCouponStoreFriend.setStartDate(lifeDiscountCoupon.getStartDate());
+            lifeDiscountCouponStoreFriend.setEndDate(lifeDiscountCoupon.getEndDate());
+            lifeDiscountCouponStoreFriend.setFriendStoreUserId(storeUser.getId());
+            lifeDiscountCouponStoreFriend.setSingleQty(couponDto.getSingleQty());
+            lifeDiscountCouponStoreFriend.setStoreUserId(lifeDiscountCouponStoreFriendDto.getFriendStoreUserId());
+            lifeDiscountCouponStoreFriend.setReleaseType(1);
+            lifeDiscountCouponStoreFriendMapper.insert(lifeDiscountCouponStoreFriend);
+        } else {
+            lifeDiscountCouponStoreFriend.setSingleQty(lifeDiscountCouponStoreFriend.getSingleQty() + couponDto.getSingleQty());
+            lifeDiscountCouponStoreFriendMapper.updateById(lifeDiscountCouponStoreFriend);
+        }
+        lifeDiscountCoupon.setSingleQty(lifeDiscountCoupon.getSingleQty() - couponDto.getSingleQty());
+        lifeDiscountCouponMapper.updateById(lifeDiscountCoupon);
+        sendFriendCouponNotice(lifeDiscountCouponStoreFriendDto.getFriendStoreUserId(), userLoginInfo, couponDto.getSingleQty(), false);
+        return true;
+    }
+
+    /** 发送赠券通知:isVoucher true=代金券,false=优惠券 */
+    private void sendFriendCouponNotice(Integer friendStoreUserId, UserLoginInfo userLoginInfo, int qty, boolean isVoucher) {
+        if (friendStoreUserId == null) {
+            return;
+        }
+        LambdaQueryWrapper<StoreUser> storeUserLambdaQueryWrapper = new LambdaQueryWrapper<>();
+        storeUserLambdaQueryWrapper.eq(StoreUser::getStoreId, friendStoreUserId);
+        List<StoreUser> storeUserList = storeUserMapper.selectList(storeUserLambdaQueryWrapper);
+        if (CollectionUtils.isEmpty(storeUserList)) {
+            return;
+        }
+        StoreUser friendStoreUser = storeUserList.get(0);
+        String friendPhone = friendStoreUser.getPhone();
+        if (friendPhone == null || friendPhone.trim().isEmpty()) {
+            return;
+        }
+        String storeName = userLoginInfo.getUserName();
+        StoreUser currentStoreUser = storeUserMapper.selectById(userLoginInfo.getUserId());
+        if (currentStoreUser != null && currentStoreUser.getStoreId() != null) {
+            StoreInfo currentStoreInfo = storeInfoMapper.selectById(currentStoreUser.getStoreId());
+            if (currentStoreInfo != null && currentStoreInfo.getStoreName() != null) {
+                storeName = currentStoreInfo.getStoreName();
+            }
+        }
+        LifeNotice lifeMessage = new LifeNotice();
+        String couponTypeName = isVoucher ? "代金券" : "优惠券";
+        String text = "您的好友" + storeName + "送了您" + qty + "张" + couponTypeName + ",快去使用吧!";
+        JSONObject jsonObject = new JSONObject();
+        jsonObject.put("message", text);
+        lifeMessage.setReceiverId("store_" + friendPhone);
+        lifeMessage.setTitle("赠券通知");
+        lifeMessage.setContext(jsonObject.toJSONString());
+        lifeMessage.setNoticeType(1);
+        lifeMessage.setIsRead(0);
+        lifeMessage.setDeleteFlag(0);
+        lifeMessage.setSenderId("system");
+        lifeNoticeMapper.insert(lifeMessage);
+    }
+
     /**
      * 为当前用户发放店铺为好友设定的可用优惠券。
      *
@@ -432,20 +478,35 @@ public class LifeDiscountCouponStoreFriendServiceImpl extends ServiceImpl<LifeDi
 
     @Override
     public LifeDiscountCouponFriendRuleVo saveFriendCouponRule(LifeDiscountCouponFriendRule lifeDiscountCouponFriendRule) {
+        List<LifeDiscountCouponFriendRuleDetail> details = lifeDiscountCouponFriendRule.getDetails();
+        // 按列表中是否传入代金券id区分:有 voucherId 走代金券逻辑,否则走优惠券逻辑
+        if (ObjectUtils.isNotEmpty(details)) {
+            for (LifeDiscountCouponFriendRuleDetail detail : details) {
+                if (detail.getVoucherId() != null && !detail.getVoucherId().trim().isEmpty()) {
+                    detail.setCouponId(null);
+                } else {
+                    detail.setVoucherId(null);
+                }
+            }
+        }
         if (ObjectUtils.isNotEmpty(lifeDiscountCouponFriendRule.getId())) {
-            List<LifeDiscountCouponFriendRuleDetail> details = lifeDiscountCouponFriendRule.getDetails();
             lifeDiscountCouponFriendRuleDetailMapper.delete(new LambdaQueryWrapper<LifeDiscountCouponFriendRuleDetail>().eq(LifeDiscountCouponFriendRuleDetail::getRuleId, lifeDiscountCouponFriendRule.getId()));
-            for (LifeDiscountCouponFriendRuleDetail detail : details) {
-                detail.setRuleId(lifeDiscountCouponFriendRule.getId());
-                detail.setStoreId(lifeDiscountCouponFriendRule.getStoreId());
+            if (ObjectUtils.isNotEmpty(details)) {
+                for (LifeDiscountCouponFriendRuleDetail detail : details) {
+                    detail.setRuleId(lifeDiscountCouponFriendRule.getId());
+                    detail.setStoreId(lifeDiscountCouponFriendRule.getStoreId());
+                }
+                lifeDiscountCouponFriendRuleDetailMapper.insertList(details);
             }
-            lifeDiscountCouponFriendRuleDetailMapper.insertList(details);
             lifeDiscountCouponFriendRuleMapper.updateById(lifeDiscountCouponFriendRule);
-        }else {
+        } else {
             lifeDiscountCouponFriendRuleMapper.insert(lifeDiscountCouponFriendRule);
-            if (ObjectUtils.isNotEmpty(lifeDiscountCouponFriendRule.getDetails())) {
-                lifeDiscountCouponFriendRule.getDetails().forEach(detail -> detail.setRuleId(lifeDiscountCouponFriendRule.getId()));
-                lifeDiscountCouponFriendRuleDetailMapper.insertList(lifeDiscountCouponFriendRule.getDetails());
+            if (ObjectUtils.isNotEmpty(details)) {
+                details.forEach(detail -> {
+                    detail.setRuleId(lifeDiscountCouponFriendRule.getId());
+                    detail.setStoreId(lifeDiscountCouponFriendRule.getStoreId());
+                });
+                lifeDiscountCouponFriendRuleDetailMapper.insertList(details);
             }
         }
         return null;
@@ -465,17 +526,24 @@ public class LifeDiscountCouponStoreFriendServiceImpl extends ServiceImpl<LifeDi
      * @return 领取的优惠券列表
      */
     @Override
-    public List<LifeDiscountCouponFriendRuleDetailVo> getReceivedFriendCouponList(String storeId,String friendStoreUserId) {
+    public List<LifeDiscountCouponFriendRuleDetailVo> getReceivedFriendCouponList(String storeId, String friendStoreUserId, Integer type) {
         QueryWrapper<LifeDiscountCouponFriendRuleDetailVo> queryWrapper = new QueryWrapper<>();
         queryWrapper.eq("ldcsf.store_user_id", storeId);
         queryWrapper.eq("ldcsf.delete_flag", 0);
         queryWrapper.eq("ldcsf.release_type", 1);
-        //查询送过优惠券的店铺
+        if (Integer.valueOf(4).equals(type)) {
+            queryWrapper.isNotNull("ldcsf.voucher_id");
+            if (StringUtils.isEmpty(friendStoreUserId)) {
+                queryWrapper.groupBy("ldcsf.friend_store_user_id").orderByDesc("couponNum");
+            } else {
+                queryWrapper.eq("ldcsf.friend_store_user_id", friendStoreUserId).groupBy("ldcsf.voucher_id").orderByDesc("couponNum");
+            }
+            return lifeDiscountCouponFriendRuleDetailMapper.getReceivedFriendVoucherList(queryWrapper);
+        }
+        queryWrapper.isNotNull("ldcsf.coupon_id");
         if (StringUtils.isEmpty(friendStoreUserId)) {
             queryWrapper.groupBy("ldcsf.friend_store_user_id").orderByDesc("couponNum");
-        }
-        //查询选中店铺送的优惠券
-        else {
+        } else {
             queryWrapper.eq("ldcsf.friend_store_user_id", friendStoreUserId).groupBy("ldcsf.coupon_id").orderByDesc("couponNum");
         }
         return lifeDiscountCouponFriendRuleDetailMapper.getReceivedFriendCouponList(queryWrapper);
@@ -483,15 +551,34 @@ public class LifeDiscountCouponStoreFriendServiceImpl extends ServiceImpl<LifeDi
 
     @Override
     public List<LifeDiscountCouponFriendRuleVo> getRuleList(String storeId, String acName, String status) {
+        if (StringUtils.isEmpty(storeId)) {
+            return new ArrayList<>();
+        }
         List<LifeDiscountCouponFriendRuleVo> ruleList = lifeDiscountCouponFriendRuleDetailMapper.getRuleList(storeId);
+        if (ruleList == null) {
+            ruleList = new ArrayList<>();
+        }
         if (ObjectUtils.isNotEmpty(ruleList)) {
-            ruleList.forEach(i -> i.setStatus(i.getEndDate().after(new Date()) ? "0" : "1"));
+            Date now = new Date();
+            ruleList.forEach(i -> {
+                if (i.getEndDate() != null) {
+                    i.setStatus(i.getEndDate().after(now) ? "0" : "1");
+                } else {
+                    i.setStatus(null);
+                }
+            });
         }
         if (StringUtils.isNotEmpty(acName)) {
-            ruleList = ruleList.stream().filter(i -> i.getAcName().contains(acName)).collect(Collectors.toList());
+            String name = acName;
+            ruleList = ruleList.stream()
+                    .filter(i -> i.getAcName() != null && i.getAcName().contains(name))
+                    .collect(Collectors.toList());
         }
         if (StringUtils.isNotEmpty(status)) {
-            ruleList = ruleList.stream().filter(i -> i.getStatus().equals(status)).collect(Collectors.toList());
+            String s = status;
+            ruleList = ruleList.stream()
+                    .filter(i -> i.getStatus() != null && i.getStatus().equals(s))
+                    .collect(Collectors.toList());
         }
         return ruleList;
     }