Commit de22fd43 by hayden

Merge branch 'dev_lch' into dev

parents ef7991f7 5405ee1d
package share.quartz.task;
import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.conditions.update.LambdaUpdateWrapper;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Component;
import org.springframework.util.CollectionUtils;
import share.common.enums.ConsumerCouponStatusEnum;
import share.common.enums.UserStatusEnum;
import share.common.enums.YesNoEnum;
import share.system.domain.CouponLog;
import share.system.domain.SConsumerCoupon;
import share.system.domain.SStore;
import share.system.service.CouponLogService;
import share.system.service.ISConsumerCouponService;
import share.system.service.ISStoreService;
import share.system.service.QPService;
import share.system.service.impl.QPServiceImpl;
import java.util.*;
......@@ -25,17 +17,9 @@ import java.util.stream.Collectors;
@Component("couponRetryTask")
public class CouponRetryTask {
@Autowired
private CouponLogService couponLogService;
@Autowired
private QPService qpService;
@Autowired
private ISStoreService sStoreService;
@Autowired
private ISConsumerCouponService sConsumerCouponService;
private static final Logger logger = LoggerFactory.getLogger(QPServiceImpl.class);
private static final Integer TRY_NUMBER = 3;
public void AuToRetryCoupon() {
LambdaQueryWrapper<SConsumerCoupon> consumerCouponWrapper = new LambdaQueryWrapper<>();
consumerCouponWrapper.eq(SConsumerCoupon::getUseStatus, UserStatusEnum.UNUSED.getCode());
......@@ -50,46 +34,5 @@ public class CouponRetryTask {
if (!CollectionUtils.isEmpty(expiredSConsumerCounpons)) {
sConsumerCouponService.updateBatchById(expiredSConsumerCounpons);
}
LambdaQueryWrapper<CouponLog> queryWrapper = new LambdaQueryWrapper<>();
queryWrapper.eq(CouponLog::getStatus, YesNoEnum.no.getDisplay_3());
queryWrapper.eq(CouponLog::getNumber, TRY_NUMBER);
List<CouponLog> list = couponLogService.list(queryWrapper);
if (CollectionUtils.isEmpty(list)) {
return;
}
//根据code分组
Map<String, List<CouponLog>> collect = list.stream().collect(Collectors.groupingBy(CouponLog::getCode));
//门店id集合
List<Long> storeIds = list.stream().map(CouponLog::getStoreId).distinct().collect(Collectors.toList());
List<SStore> sStoreList = sStoreService.listByIds(storeIds);
//遍历Map
collect.forEach((k, v) -> {
CouponLog couponLog = v.get(0);
SStore sStore = sStoreList.stream().filter(s -> s.getId().equals(couponLog.getStoreId())).findFirst().get();
if (couponLog.getNumber().equals(TRY_NUMBER)) {
for (int i = 0; i < TRY_NUMBER; i++) {
try {
qpService.consume(couponLog.getCode(), 1, sStore.getOpenShopUuid(), ConsumerCouponStatusEnum.TASK.getCode());
} catch (Exception e) {
logger.error("验卷失败:{}", e.getMessage());
} finally {
couponLog.setNumber(couponLog.getNumber() - 1);
couponLogService.updateById(couponLog);
}
}
//根据code批量更新状态
LambdaUpdateWrapper<CouponLog> updateWrapper = new LambdaUpdateWrapper<>();
updateWrapper.eq(CouponLog::getCode, k);
//设置number为0
updateWrapper.set(CouponLog::getNumber, 0);
couponLogService.update(updateWrapper);
//更新优惠卷领取状态
SConsumerCoupon sConsumerCoupon = new SConsumerCoupon();
sConsumerCoupon.setId(couponLog.getCouponId());
sConsumerCoupon.setUseStatus(UserStatusEnum.EXPIRED.getCode());
sConsumerCouponService.updateById(sConsumerCoupon);
}
});
}
}
......@@ -7,11 +7,18 @@ import cn.hutool.json.JSONException;
import cn.hutool.json.JSONObject;
import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.toolkit.Wrappers;
import com.dianping.openapi.sdk.api.oauth.entity.CustomerRefreshTokenResponse;
import com.dianping.openapi.sdk.api.tuangou.TuangouReceiptPrepare;
import com.dianping.openapi.sdk.api.tuangou.entity.TuangouReceiptGetConsumedReponseEntity;
import com.dianping.openapi.sdk.api.tuangou.entity.TuangouReceiptPrepareRequest;
import com.dianping.openapi.sdk.api.tuangou.entity.TuangouReceiptPrepareResponse;
import com.dianping.openapi.sdk.httpclient.DefaultOpenAPIClient;
import com.sun.org.apache.bcel.internal.generic.IF_ACMPEQ;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.data.redis.core.RedisTemplate;
import org.springframework.stereotype.Component;
import org.springframework.util.CollectionUtils;
......@@ -85,45 +92,31 @@ public class RedisTask {
final long THREE_MINUTES = 60 * 3;
public void AuToReceiptCode() {
//获取redis中所有以tuangou.receipt.prepare开头的key
Set<String> keys = redisTemplate.keys(ReceiptRdeisEnum.PREPARE.getValue() + "*");
if (keys.size() == 0) {
return;
}
/**
* 自动删除所有可用但已退款的优惠券
*/
public void AutoDelRefundedCoupon() {
//获取所有待使用的优惠卷
LambdaQueryWrapper<SConsumerCoupon> queryWrapper = new LambdaQueryWrapper<>();
queryWrapper.eq(SConsumerCoupon::getUseStatus, CouponStatusEnum.NORMAL.getValue());
List<SConsumerCoupon> list = isConsumerCouponService.list(queryWrapper);
//遍历key
keys.stream().forEach(o -> {
//获取key对应的value
String value = redisUtil.get(String.valueOf(o));
JSONObject jsonObject = new JSONObject(value);
Date expirationTime = jsonObject.getDate("expirationTime");
//判断是否过期
if (expirationTime.getTime() < new Date().getTime()) {
//获取redis中的券码id
Long consumerCouponId = jsonObject.getLong("consumerCouponId");
if (CollectionUtils.isEmpty(list)) {
//删除redis中的值
redisUtil.delete(o);
//删除数据库中的值
isConsumerCouponService.deleteSConsumerCouponById(consumerCouponId);
//跳过循环
return;
LambdaQueryWrapper<SConsumerCoupon> wrapper = Wrappers.lambdaQuery();
wrapper.eq(SConsumerCoupon::getUseStatus, CouponStatusEnum.NORMAL.getValue());
wrapper.isNotNull(SConsumerCoupon::getCouponCode);
List<SConsumerCoupon> coupons = isConsumerCouponService.list(wrapper);
List<SStore> stores = storeService.list();
List<Long> expiredCoupons = new ArrayList<>();
if (!CollectionUtils.isEmpty(coupons)) {
for (SConsumerCoupon coupon : coupons) {
//验券
TuangouReceiptGetConsumedReponseEntity getconsumed = qpService.getconsumed(coupon.getCouponCode(),
stores.stream().filter(store -> store.getId().equals(coupon.getStoreId())).findFirst().get().getOpenShopUuid());
if (getconsumed == null) {
expiredCoupons.add(coupon.getId());
}
if (ObjectUtil.isEmpty(list.stream().filter(item -> item.getId().equals(consumerCouponId)).findFirst().orElse(null))) {
//删除redis中的值
redisUtil.delete(o);
//删除数据库中的值
isConsumerCouponService.deleteSConsumerCouponById(consumerCouponId);
} else {
//删除redis中的值
redisUtil.delete(o);
logger.info("券码:{}->查询结束:{}", coupon.getCouponCode(), getconsumed);
}
//逻辑删除所有异常优惠券
isConsumerCouponService.removeByIds(expiredCoupons);
}
});
}
public void processAutoReceiptCode() {
......
package share.system.service.impl;
import cn.hutool.core.date.DatePattern;
import cn.hutool.core.date.DateUtil;
import cn.hutool.json.JSONObject;
import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.dianping.openapi.sdk.api.customerauth.session.CustomerKeyShopScopeQuery;
......@@ -90,35 +92,24 @@ public class QPServiceImpl implements QPService {
*/
@Override
public TuangouReceiptPrepareResponseEntityVo consumeByUser(String code, String openShopUuid, String status) {
LambdaQueryWrapper<SStore> sStoreLambdaQueryWrapper = new LambdaQueryWrapper<>();
sStoreLambdaQueryWrapper.eq(SStore::getOpenShopUuid, openShopUuid);
SStore sStore = storeService.getOne(sStoreLambdaQueryWrapper);
if (ObjectUtils.isEmpty(sStore) || StoreStatusEnum.STOP.getIndex().equals(sStore.getStatus())) {
throw new RuntimeException("门店状态异常,请联系管理员");
}
//验券准备
TuangouReceiptPrepareResponseEntity prepare = prepare(code.trim(), openShopUuid, status);
//获取用户信息
SConsumer user = FrontTokenComponent.getWxSConsumerEntry();
//查询领取记录表
LambdaQueryWrapper<SConsumerCoupon> queryWrapper = new LambdaQueryWrapper<>();
queryWrapper.eq(SConsumerCoupon::getDealId, prepare.getDeal_id());
queryWrapper.eq(SConsumerCoupon::getCouponCode, code);
queryWrapper.eq(SConsumerCoupon::getUseStatus, UserStatusEnum.UNUSED.getCode());
SConsumerCoupon couponTwo = isConsumerCouponService.getOne(queryWrapper);
LambdaQueryWrapper<SStore> sStoreLambdaQueryWrapper = new LambdaQueryWrapper<>();
sStoreLambdaQueryWrapper.eq(SStore::getOpenShopUuid, openShopUuid);
SStore sStore = storeService.getOne(sStoreLambdaQueryWrapper);
if (ObjectUtils.isEmpty(sStore)) {
throw new RuntimeException("门店不存在");
}
if (sStore.getStatus().equals(StoreStatusEnum.STOP.getIndex())) {
throw new RuntimeException("门店已停止");
}
if (ObjectUtils.isNotEmpty(couponTwo)) {
//判断是否本人领取过
if (couponTwo.getConsumerId().equals(user.getId())) {
//领取删除原来的
isConsumerCouponService.deleteSConsumerCouponById(couponTwo.getId());
//删除redis
redisUtil.delete(ReceiptRdeisEnum.PREPARE.getValue() + prepare.getReceipt_code());
} else {
return null;
}
SConsumerCoupon unUsedCoupon = isConsumerCouponService.getOne(queryWrapper);
if (ObjectUtils.isNotEmpty(unUsedCoupon)) {
throw new RuntimeException("该券已被领取");
}
//根据优惠卷名称查询优惠劵配置 查询list,取第一个
TuangouReceiptPrepareResponseEntityVo response = new TuangouReceiptPrepareResponseEntityVo();
......@@ -134,20 +125,15 @@ public class QPServiceImpl implements QPService {
sConsumerCoupon.setCreateBy(String.valueOf(user.getId()));
sConsumerCoupon.setCreateTime(new Date());
sConsumerCoupon.setSubPrice(BigDecimal.valueOf(prepare.getDeal_marketprice()));
//根据门店id查询门店信息
List<TuangouDealQueryShopDealResponseEntity> queryshopdeal = queryshopdeal(openShopUuid);
queryshopdeal.stream().forEach(o -> {
//查询美团团购信息
List<TuangouDealQueryShopDealResponseEntity> groupActivities = queryshopdeal(openShopUuid);
groupActivities.forEach(o -> {
//套餐名称相同并且在售卖中
if (prepare.getDeal_title().equals(o.getTitle()) && o.getSale_status().equals(saleStatusEnum.SELLING.getCode())) {
SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd HH:mm");
try {
Date receiptEndDate = sdf.parse(o.getReceipt_end_date());
Date receiptBeginDate = sdf.parse(o.getReceipt_begin_date());
if (prepare.getDeal_title().equals(o.getTitle()) && saleStatusEnum.SELLING.getCode().equals(o.getSale_status())) {
Date receiptEndDate = DateUtil.parse(o.getReceipt_end_date(), DatePattern.NORM_DATETIME_MINUTE_PATTERN);
Date receiptBeginDate = DateUtil.parse(o.getReceipt_begin_date(), DatePattern.NORM_DATETIME_MINUTE_PATTERN);
sConsumerCoupon.setStartDate(receiptBeginDate);
sConsumerCoupon.setEndDate(receiptEndDate);
} catch (Exception e) {
e.printStackTrace();
}
}
});
......@@ -187,19 +173,12 @@ public class QPServiceImpl implements QPService {
}
isConsumerCouponService.insertSConsumerCoupon(sConsumerCoupon);
response.setConsumerCouponId(sConsumerCoupon.getId());
//设置过期时间30分钟
Map<String, String> map = new HashMap<>();
map.put("receipt_code", prepare.getReceipt_code());
map.put("consumerCouponId", String.valueOf(response.getConsumerCouponId()));
//设置30分钟后的时间
Date date = DateUtils.addMinutes(new Date(), 30);
map.put("expirationTime", String.valueOf(date));
JSONObject jsonObject = new JSONObject(map);
redisUtil.set(ReceiptRdeisEnum.PREPARE.getValue() + prepare.getReceipt_code(), jsonObject.toString());
SimpleDateFormat format2 = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
String output1 = format2.format(prepare.getReceiptEndDate());
String output1 = DateUtil.format(prepare.getReceiptEndDate(), DatePattern.NORM_DATETIME_PATTERN);
BeanUtils.copyProperties(prepare, response);
response.setExpirationTime(output1);
//核销美团券
consume(code.trim(), 1, openShopUuid, ConsumerCouponStatusEnum.ORDER.getCode());
return response;
}
......
......@@ -14,6 +14,11 @@ import com.baomidou.dynamic.datasource.annotation.DSTransactional;
import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.toolkit.Wrappers;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.dianping.openapi.sdk.api.tuangou.TuangouReceiptGetConsumed;
import com.dianping.openapi.sdk.api.tuangou.entity.TuangouReceiptGetConsumedReponse;
import com.dianping.openapi.sdk.api.tuangou.entity.TuangouReceiptGetConsumedReponseEntity;
import com.dianping.openapi.sdk.api.tuangou.entity.TuangouReceiptGetConsumedRequest;
import com.dianping.openapi.sdk.httpclient.DefaultOpenAPIClient;
import org.apache.commons.collections4.CollectionUtils;
import org.apache.commons.lang3.StringUtils;
import org.slf4j.Logger;
......@@ -323,7 +328,10 @@ public class SOrderServiceImpl extends ServiceImpl<SOrderMapper, SOrder> impleme
if (Objects.nonNull(byId)) {
if (byId.getUseStatus().equals(UserStatusEnum.UNUSED.getCode())) {
if (StringUtils.isNotEmpty(byId.getCouponCode())) {
qpService.prepare(byId.getCouponCode(), sStore.getOpenShopUuid(), ConsumerCouponStatusEnum.ORDER.getCode());
TuangouReceiptGetConsumedReponseEntity getconsumed = qpService.getconsumed(byId.getCouponCode(), sStore.getOpenShopUuid());
if (getconsumed == null) {
throw new RuntimeException("优惠券异常,请稍后再试");
}
}
} else {
throw new BaseException("优惠券已使用!");
......@@ -349,10 +357,6 @@ public class SOrderServiceImpl extends ServiceImpl<SOrderMapper, SOrder> impleme
save(sOrder);
if (response.getStatus().equals(YesNoEnum.yes.getFlag())) {
if (Objects.nonNull(byId)) {
if (StringUtils.isNotEmpty(byId.getCouponCode())) {
//验劵
qpService.consume(byId.getCouponCode(), 1, sStore.getOpenShopUuid(), ConsumerCouponStatusEnum.ORDER.getCode());
}
SConsumerCoupon consumerCoupon = new SConsumerCoupon();
consumerCoupon.setId(request.getCouponId());
consumerCoupon.setUseDate(new Date());
......@@ -795,10 +799,6 @@ public class SOrderServiceImpl extends ServiceImpl<SOrderMapper, SOrder> impleme
SConsumerCoupon consumerCoupon = consumerCouponService.getById(couponId);
if (ObjectUtil.isNotEmpty(consumerCoupon)) {
if (consumerCoupon.getUseStatus().equals(UserStatusEnum.UNUSED.getCode())) {
if (StringUtils.isNotEmpty(consumerCoupon.getCouponCode())) {
//验劵
qpService.consume(consumerCoupon.getCouponCode(), 1, sStore.getOpenShopUuid(), ConsumerCouponStatusEnum.WECHAT.getCode());
}
consumerCoupon.setUseDate(new Date());
consumerCoupon.setUseStatus(UserStatusEnum.USED.getCode());
consumerCouponService.updateById(consumerCoupon);
......
Markdown is supported
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