Commit 714870bf authored by DongGuoChao's avatar DongGuoChao Committed by linlinjava
Browse files

过滤掉首页已经领取掉的优惠券

parent 0a0e9974
...@@ -3,10 +3,9 @@ package org.linlinjava.litemall.db.service; ...@@ -3,10 +3,9 @@ package org.linlinjava.litemall.db.service;
import com.alibaba.druid.util.StringUtils; import com.alibaba.druid.util.StringUtils;
import com.github.pagehelper.PageHelper; import com.github.pagehelper.PageHelper;
import org.linlinjava.litemall.db.dao.LitemallCouponMapper; import org.linlinjava.litemall.db.dao.LitemallCouponMapper;
import org.linlinjava.litemall.db.domain.LitemallCoupon; import org.linlinjava.litemall.db.dao.LitemallCouponUserMapper;
import org.linlinjava.litemall.db.domain.*;
import org.linlinjava.litemall.db.domain.LitemallCoupon.Column; import org.linlinjava.litemall.db.domain.LitemallCoupon.Column;
import org.linlinjava.litemall.db.domain.LitemallCouponExample;
import org.linlinjava.litemall.db.domain.LitemallCouponUser;
import org.linlinjava.litemall.db.util.CouponConstant; import org.linlinjava.litemall.db.util.CouponConstant;
import org.springframework.stereotype.Service; import org.springframework.stereotype.Service;
...@@ -14,18 +13,21 @@ import javax.annotation.Resource; ...@@ -14,18 +13,21 @@ import javax.annotation.Resource;
import java.time.LocalDateTime; import java.time.LocalDateTime;
import java.util.List; import java.util.List;
import java.util.Random; import java.util.Random;
import java.util.stream.Collectors;
@Service @Service
public class LitemallCouponService { public class LitemallCouponService {
@Resource @Resource
private LitemallCouponMapper couponMapper; private LitemallCouponMapper couponMapper;
@Resource
private LitemallCouponUserMapper couponUserMapper;
private Column[] result = new Column[]{Column.id, Column.name, Column.desc, Column.tag, private Column[] result = new Column[]{Column.id, Column.name, Column.desc, Column.tag,
Column.days, Column.startTime, Column.endTime, Column.days, Column.startTime, Column.endTime,
Column.discount, Column.min}; Column.discount, Column.min};
/** /**
* 查询 * 查询,空参数
* *
* @param offset * @param offset
* @param limit * @param limit
...@@ -34,11 +36,24 @@ public class LitemallCouponService { ...@@ -34,11 +36,24 @@ public class LitemallCouponService {
* @return * @return
*/ */
public List<LitemallCoupon> queryList(int offset, int limit, String sort, String order) { public List<LitemallCoupon> queryList(int offset, int limit, String sort, String order) {
LitemallCouponExample example = new LitemallCouponExample(); return queryList(LitemallCouponExample.newAndCreateCriteria(), offset, limit, sort, order);
example.or().andTypeEqualTo(CouponConstant.TYPE_COMMON).andStatusEqualTo(CouponConstant.STATUS_NORMAL).andDeletedEqualTo(false); }
example.setOrderByClause(sort + " " + order);
/**
* 查询
*
* @param criteria 可扩展的条件
* @param offset
* @param limit
* @param sort
* @param order
* @return
*/
public List<LitemallCoupon> queryList(LitemallCouponExample.Criteria criteria, int offset, int limit, String sort, String order) {
criteria.andTypeEqualTo(CouponConstant.TYPE_COMMON).andStatusEqualTo(CouponConstant.STATUS_NORMAL).andDeletedEqualTo(false);
criteria.example().setOrderByClause(sort + " " + order);
PageHelper.startPage(offset, limit); PageHelper.startPage(offset, limit);
return couponMapper.selectByExampleSelective(example, result); return couponMapper.selectByExampleSelective(criteria.example(), result);
} }
public int queryTotal() { public int queryTotal() {
...@@ -47,6 +62,19 @@ public class LitemallCouponService { ...@@ -47,6 +62,19 @@ public class LitemallCouponService {
return (int) couponMapper.countByExample(example); return (int) couponMapper.countByExample(example);
} }
public List<LitemallCoupon> queryAvailableList(Integer userId, int offset, int limit) {
assert userId != null;
// 过滤掉登录账号已经领取过的coupon
LitemallCouponExample.Criteria c = LitemallCouponExample.newAndCreateCriteria();
List<LitemallCouponUser> used = couponUserMapper.selectByExample(
LitemallCouponUserExample.newAndCreateCriteria().andUserIdEqualTo(userId).example()
);
if(used!=null && !used.isEmpty()){
c.andIdNotIn(used.stream().map(LitemallCouponUser::getCouponId).collect(Collectors.toList()));
}
return queryList(c, offset, limit, "add_time", "desc");
}
public List<LitemallCoupon> queryList(int offset, int limit) { public List<LitemallCoupon> queryList(int offset, int limit) {
return queryList(offset, limit, "add_time", "desc"); return queryList(offset, limit, "add_time", "desc");
} }
......
...@@ -7,6 +7,7 @@ import org.linlinjava.litemall.core.util.ResponseUtil; ...@@ -7,6 +7,7 @@ import org.linlinjava.litemall.core.util.ResponseUtil;
import org.linlinjava.litemall.db.domain.LitemallCategory; import org.linlinjava.litemall.db.domain.LitemallCategory;
import org.linlinjava.litemall.db.domain.LitemallGoods; import org.linlinjava.litemall.db.domain.LitemallGoods;
import org.linlinjava.litemall.db.service.*; import org.linlinjava.litemall.db.service.*;
import org.linlinjava.litemall.wx.annotation.LoginUser;
import org.linlinjava.litemall.wx.service.HomeCacheManager; import org.linlinjava.litemall.wx.service.HomeCacheManager;
import org.springframework.beans.factory.annotation.Autowired; import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.validation.annotation.Validated; import org.springframework.validation.annotation.Validated;
...@@ -70,11 +71,11 @@ public class WxHomeController { ...@@ -70,11 +71,11 @@ public class WxHomeController {
/** /**
* 首页数据 * 首页数据
* * @param userId 当用户已经登录时,非空。为登录状态为null
* @return 首页数据 * @return 首页数据
*/ */
@GetMapping("/index") @GetMapping("/index")
public Object index() { public Object index(@LoginUser Integer userId) {
//优先从缓存中读取 //优先从缓存中读取
if (HomeCacheManager.hasData(HomeCacheManager.INDEX)) { if (HomeCacheManager.hasData(HomeCacheManager.INDEX)) {
return ResponseUtil.ok(HomeCacheManager.getCacheData(HomeCacheManager.INDEX)); return ResponseUtil.ok(HomeCacheManager.getCacheData(HomeCacheManager.INDEX));
...@@ -87,7 +88,13 @@ public class WxHomeController { ...@@ -87,7 +88,13 @@ public class WxHomeController {
Callable<List> channelListCallable = () -> categoryService.queryChannel(); Callable<List> channelListCallable = () -> categoryService.queryChannel();
Callable<List> couponListCallable = () -> couponService.queryList(0, 3); Callable<List> couponListCallable;
if(userId == null){
couponListCallable = () -> couponService.queryList(0, 3);
} else {
couponListCallable = () -> couponService.queryAvailableList(userId,0, 3);
}
Callable<List> newGoodsListCallable = () -> goodsService.queryByNew(0, SystemConfig.getNewLimit()); Callable<List> newGoodsListCallable = () -> goodsService.queryByNew(0, SystemConfig.getNewLimit());
......
...@@ -24,7 +24,7 @@ ...@@ -24,7 +24,7 @@
</view> </view>
</view> </view>
</view> </view>
<view class="b"> <view wx:if="{{coupon.length>0}}" class="b">
<view class="item" wx:for="{{coupon}}" wx:for-index="index" wx:for-item="item" wx:key="id" bindtap="getCoupon" data-index="{{item.id}}"> <view class="item" wx:for="{{coupon}}" wx:for-index="index" wx:for-item="item" wx:key="id" bindtap="getCoupon" data-index="{{item.id}}">
<view class="tag">{{item.tag}}</view> <view class="tag">{{item.tag}}</view>
<view class="content"> <view class="content">
......
Supports Markdown
0% or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment