1
935090232@qq.com
2020-12-01 611146e69aaa62296cf84f2ccb5aca5ebba17677
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
package com.matrix.system.shopXcx.api.service.impl;
 
import com.matrix.core.pojo.AjaxResult;
import com.matrix.core.pojo.PaginationVO;
import com.matrix.core.tools.LogUtil;
import com.matrix.core.tools.StringUtils;
import com.matrix.biz.bean.BizUser;
import com.matrix.component.redis.RedisUserLoginUtils;
import com.matrix.system.common.constance.AppConstance;
 
import com.matrix.system.shopXcx.bean.ShopCoupon;
import com.matrix.system.shopXcx.bean.ShopCouponRecord;
import com.matrix.system.shopXcx.bean.ShopProductAttrRef;
import com.matrix.system.shopXcx.bean.ShopShoppingCart;
import com.matrix.system.shopXcx.dao.ShopCouponDao;
import com.matrix.system.shopXcx.dao.ShopCouponRecordDao;
import com.matrix.system.shopXcx.dao.ShopProductAttrRefDao;
import com.matrix.system.shopXcx.dao.ShopReceiveAddressDao;
import com.matrix.system.shopXcx.api.service.WxShopCouponService;
import com.matrix.system.shopXcx.api.tools.WxShopCouponUtil;
import com.matrix.system.shopXcx.api.tools.WxShopOrderUtil;
import com.matrix.system.shopXcx.api.vo.CouponReceiveInfoVO;
import com.matrix.system.shopXcx.api.vo.ShopCartVo;
import org.apache.commons.collections.CollectionUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;
 
import java.math.BigDecimal;
import java.util.*;
 
/**
 * @author jyy
 * @description 优惠券服务层
 * @date 2019-06-10 10:58
 */
@Service
public class WxShopCouponServiceImpl implements WxShopCouponService {
    @Autowired
    private WxShopOrderUtil wxShopOrderUtil;
    @Autowired
    private RedisUserLoginUtils redisUserLoginUtils;
    @Autowired
    private ShopCouponDao shopCouponDao;
    @Autowired
    private ShopCouponRecordDao shopCouponRecordDao;
    @Autowired
    private ShopProductAttrRefDao shopProductAttrRefDao;
    @Autowired
    private ShopReceiveAddressDao shopReceiveAddressDao;
    @Autowired
    private WxShopCouponUtil wxShopCouponUtil;
 
 
    /**
     * 根据购物车选中的产品,计算合适的优惠券
     *
     * @return
     */
    public List<CouponReceiveInfoVO> getCartVoCouponList(Long shopId,List<ShopShoppingCart> shopShoppingCarts) {
 
        //可用优惠券列表
        List<CouponReceiveInfoVO> shopCoupons = new ArrayList<>();
 
 
        BizUser bizUser = redisUserLoginUtils.getLoginUser(BizUser.class);
        //1.找到用户所有的优惠券
        List<CouponReceiveInfoVO> userAllCoupon = shopCouponRecordDao.selectMyCouponByStatus(shopId, bizUser.getOpenId(), AppConstance.MY_COUPON_NOT_USE,
                null);
        if (CollectionUtils.isNotEmpty(userAllCoupon)) {
            //根据优惠力度排序,优惠力度大的先参与计算
            Collections.sort(userAllCoupon, new Comparator() {
                @Override
                public int compare(Object o1, Object o2) {
                    CouponReceiveInfoVO e1 = (CouponReceiveInfoVO) o1;
                    CouponReceiveInfoVO e2 = (CouponReceiveInfoVO) o2;
                    return e2.getMinAmount().compareTo(e1.getMinAmount());
                }
            });
 
 
            //定义购物车优惠分组
            List<ShopCartVo> cartList = new ArrayList<>();
 
            // 记录所有优惠券,一个优惠券(活动)表示一组
            Map<Integer, ShopCoupon> shopCouponMap = new HashMap<>();
 
            //2.把用户的优惠券和产品进行匹配匹配成功后分组,
            for (int i = 0; i < userAllCoupon.size(); i++) {
 
                CouponReceiveInfoVO couponReceiveInfoVO = userAllCoupon.get(i);
                ShopCartVo shopCarVo = new ShopCartVo();
                shopCarVo.setCouponReceiveInfoVO(couponReceiveInfoVO);
                shopCarVo.setCartList(new ArrayList<>());
                cartList.add(i, shopCarVo);
 
 
                for (ShopShoppingCart shoppingCart : shopShoppingCarts) {
 
                    // 如果匹配到的产品已经出现在其他优惠组中,则需要判断是否移除之前的组
                    boolean needRemove = false;
 
 
                    //找到产品所有的优惠券
                    List<ShopCoupon> productShopCoupons = getCouponListByProductId(shoppingCart.getCartProductId());
                    for (ShopCoupon productShopCoupon : productShopCoupons) {
                        if (couponReceiveInfoVO.getCouponId().equals(productShopCoupon.getId())) {
                            LogUtil.debug("购物车" + shoppingCart.getName() + "和优惠券" + couponReceiveInfoVO.getcName() + "匹配");
                            shopCarVo.getCartList().add(shoppingCart);
                            break;
                        }
                    }
                }
                // 计算本组优惠券是否已经满足优惠条件
                countPrice(shopCarVo);
            }
 
            //3.计算各组的金额是否满足,把满足的优惠券全部返回
            for (ShopCartVo shopCartVo : cartList) {
                if (shopCartVo.isSatisfactionCoupon() && CollectionUtils.isNotEmpty(shopCartVo.getCartList())) {
                    shopCoupons.add(shopCartVo.getCouponReceiveInfoVO());
                }
            }
        }
        return shopCoupons;
    }
 
 
 
 
    private void countPrice(ShopCartVo shopCartVo) {
        List<ShopShoppingCart> cartList = shopCartVo.getCartList();
        // 计算总价
        BigDecimal sum = BigDecimal.ZERO;
        for (ShopShoppingCart shopShoppingCart : cartList) {
            sum = sum.add(shopShoppingCart.getPrice().multiply(BigDecimal.valueOf(shopShoppingCart.getCartNumber())));
        }
        // 存在优惠券
        if (sum.compareTo(shopCartVo.getCouponReceiveInfoVO().getMinAmount()) >= 0) {
            //标记为合理分组
            shopCartVo.setSatisfactionCoupon(true);
        } else {
            shopCartVo.setSatisfactionCoupon(false);
        }
    }
 
 
    /**
     * 获取可用优惠券
     *
     * @return
     */
    @Override
    public AjaxResult getUsableCoupon(PaginationVO pageVo) {
        BizUser bizUser = redisUserLoginUtils.getLoginUser(BizUser.class);
        boolean newPeople = wxShopCouponUtil.verifyIsNewPeople(bizUser.getOpenId());
        List<ShopCoupon> list = shopCouponDao.selectUsableCoupon(newPeople, bizUser.getOpenId(), pageVo);
        return new AjaxResult(AjaxResult.STATUS_SUCCESS, list);
    }
 
    /**
     * 领取优惠券
     *
     * @param couponId
     * @return
     */
    @Transactional(rollbackFor = Exception.class)
    @Override
    public AjaxResult receiveCoupon(Integer couponId) {
        ShopCoupon shopCoupon = shopCouponDao.selectById(couponId);
        BizUser bizUser = redisUserLoginUtils.getLoginUser(BizUser.class);
 
        // 如果没有找到该优惠券
        if (shopCoupon == null) {
            return new AjaxResult(AjaxResult.STATUS_FAIL, "没有找到该优惠券!");
        }
 
        // 如果优惠券有数量限制并且已经领完
        if (shopCoupon.getMaxQuantity() >= 0 && (shopCoupon.getMaxQuantity() <= shopCoupon.getQuantityReceive())) {
            return new AjaxResult(AjaxResult.STATUS_FAIL, "优惠券已领完!");
        }
 
        boolean newPeople = wxShopCouponUtil.verifyIsNewPeople(bizUser.getOpenId());
 
        // 如果改券是新人专属且当前用户不是新人
        if (AppConstance.COUPON_GET_LIMT_NEW.equals(shopCoupon.getGetLimit()) && !newPeople) {
            return new AjaxResult(AjaxResult.STATUS_FAIL, "该优惠券为新人专享优惠券!");
        }
 
        ShopCouponRecord param = new ShopCouponRecord();
        param.setUserId(bizUser.getOpenId());
        param.setCId(couponId);
        List<ShopCouponRecord> recordList = shopCouponRecordDao.selectByModel(param);
        // 如果已经领取改优惠券
        if (CollectionUtils.isNotEmpty(recordList)) {
            return new AjaxResult(AjaxResult.STATUS_FAIL, "优惠券不能重复领取!");
        }
        ShopCouponRecord insertRecord = new ShopCouponRecord();
        insertRecord.setCreateBy(AppConstance.SYSTEM_USER);
        insertRecord.setUpdateBy(AppConstance.SYSTEM_USER);
        insertRecord.setCId(couponId);
        insertRecord.setUserId(bizUser.getOpenId());
        insertRecord.setIsUsing(2);
        int i = shopCouponRecordDao.insert(insertRecord);
        // 如果插入领取记录表失败
        if (i < 0) {
            return new AjaxResult(AjaxResult.STATUS_FAIL, "优惠券领取失败!");
        }
        shopCoupon.setQuantityReceive(shopCoupon.getQuantityReceive() + 1);
        shopCouponDao.updateByModel(shopCoupon);
        return new AjaxResult(AjaxResult.STATUS_SUCCESS, "优惠券领取成功!");
    }
 
    /**
     * 根据订单状态查询优惠券列表
     *
     *
     * @param shopId
     * @param status 1=已使用;2=未使用;3=过期
     * @return
     */
    @Override
    public AjaxResult getMyCouponInfoByStatus(Long shopId, Integer status, PaginationVO pageVo) {
        BizUser bizUser = redisUserLoginUtils.getLoginUser(BizUser.class);
        // 如果查询使用或未使用状态的优惠券列表
        if (AppConstance.MY_COUPON_USE.equals(status) || AppConstance.MY_COUPON_NOT_USE.equals(status)) {
            List<CouponReceiveInfoVO> list = shopCouponRecordDao.selectMyCouponByStatus(shopId,bizUser.getOpenId(), status,
                    pageVo);
            return new AjaxResult(AjaxResult.STATUS_SUCCESS, list);
        }
        List<CouponReceiveInfoVO> list = shopCouponRecordDao.selectMyPastCoupon(shopId,bizUser.getOpenId(), pageVo);
        return new AjaxResult(AjaxResult.STATUS_SUCCESS, list);
    }
 
    /**
     * 根据产品ID获取可用优惠券列表
     *
     * @param productId
     * @return
     */
    @Override
    public List<ShopCoupon> getCouponListByProductId(Integer productId) {
        ShopProductAttrRef refParam = new ShopProductAttrRef();
        refParam.setPId(productId);
        List<ShopProductAttrRef> refList = shopProductAttrRefDao.selectByModel(refParam);
        List<String> attrIds = new ArrayList<>();
        if (CollectionUtils.isNotEmpty(refList)) {
            for (ShopProductAttrRef shopProductAttrRef : refList) {
                if (shopProductAttrRef.getAttrFullPath() == null) {
                    continue;
                }
                List<String> ids = StringUtils.strToCollToString(shopProductAttrRef.getAttrFullPath(), "/");
                for (int j = ids.size() - 1; j >= 0; j--) {
                    if (StringUtils.isBlank(ids.get(j))) {
                        ids.remove(j);
                    }
                }
                attrIds.addAll(ids);
            }
        }
        BizUser bizUser = redisUserLoginUtils.getLoginUser(BizUser.class);
        List<ShopCoupon> list = shopCouponDao.selectUsableCouponByProductInfo(bizUser.getOpenId(), productId,
                attrIds);
        return list;
    }
 
    /**
     * 根据产品ID获取可用活动券列表
     *
     * @param productId
     * @return
     */
    @Override
    public List<ShopCoupon> getHdListByProductId(Integer productId) {
        ShopProductAttrRef refParam = new ShopProductAttrRef();
        refParam.setPId(productId);
        List<ShopProductAttrRef> refList = shopProductAttrRefDao.selectByModel(refParam);
        List<String> attrIds = new ArrayList<>();
        if (CollectionUtils.isNotEmpty(refList)) {
            for (ShopProductAttrRef shopProductAttrRef : refList) {
                if (shopProductAttrRef.getAttrFullPath() == null) {
                    continue;
                }
                List<String> ids = StringUtils.strToCollToString(shopProductAttrRef.getAttrFullPath(), "/");
                for (int j = ids.size() - 1; j >= 0; j--) {
                    if (StringUtils.isBlank(ids.get(j))) {
                        ids.remove(j);
                    }
                }
                attrIds.addAll(ids);
            }
        }
        List<ShopCoupon> list = shopCouponDao.selectHdListByProductId(productId, attrIds);
        return list;
    }
 
 
 
 
    /**
     * 通过优惠券标签获取优惠券列表
     *
     * @param tag 标签名称
     * @param shopId
     * @return
     */
    @Override
    public AjaxResult getCouponListByTag(String tag, Long shopId) {
        BizUser bizUser = redisUserLoginUtils.getLoginUser(BizUser.class);
        boolean newPeople = wxShopCouponUtil.verifyIsNewPeople(bizUser.getOpenId());
        List<ShopCoupon> couponList = shopCouponDao.selectCouponListByTag(newPeople, bizUser.getOpenId(), tag,shopId);
        AjaxResult res = new AjaxResult(AjaxResult.STATUS_SUCCESS, couponList);
        res.putInMap("isNewPeople", newPeople);
        return res;
    }
 
 
 
}