Commit 14ab6ca7 by 吕明尚

增加redis自动任务,更改下单逻辑

parent c52e533d
......@@ -7,6 +7,7 @@ import javax.servlet.http.HttpServletResponse;
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.http.MediaType;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PostMapping;
......@@ -30,6 +31,10 @@ import share.framework.config.ServerConfig;
@RequestMapping("/common")
public class CommonController
{
@Value("${dianping.appKey}")
private String APP_KEY;
@Value("${dianping.appSecret}")
private String APP_SECRET;
private static final Logger log = LoggerFactory.getLogger(CommonController.class);
@Autowired
......@@ -160,4 +165,11 @@ public class CommonController
log.error("下载文件失败", e);
}
}
@GetMapping("/appKey")
public AjaxResult getAppKey() {
AjaxResult ajax = AjaxResult.success();
ajax.put("appKey", APP_KEY);
return ajax;
}
}
package share.web.controller.system;
import io.swagger.annotations.Api;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;
import share.common.core.domain.R;
import share.system.service.QPService;
@RestController
@RequestMapping("/chessCards")
@Api(tags = "首页-验券")
public class QPController {
@Autowired
private QPService qpService;
//通过authCode,获取授权的session
@GetMapping("/oauthToken")
public R<String> oauthToken(String authCode) {
return R.ok(qpService.oauthToken(authCode));
}
}
\ No newline at end of file
package share.common.enums;
public enum ReceiptRdeisEnum {
//1:验劵准备 2:session换取
PREPARE(1, "tuangou.receipt.prepare"), SESSION(2, "oauth.token");
private Integer code;
private String value;
ReceiptRdeisEnum() {
}
ReceiptRdeisEnum(Integer code, String value) {
this.code = code;
this.value = value;
}
public Integer getCode() {
return code;
}
public void setCode(Integer code) {
this.code = code;
}
public String getValue() {
return value;
}
public void setValue(String value) {
this.value = value;
}
}
package share.quartz.task;
import cn.hutool.json.JSONObject;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.redis.core.RedisTemplate;
import org.springframework.stereotype.Component;
import share.system.service.ISConsumerCouponService;
import java.util.Date;
import java.util.Set;
@Component("redisTask")
public class RedisTask {
@Autowired
private ISConsumerCouponService isConsumerCouponService;
RedisTemplate redisTemplate = new RedisTemplate();
public void AuToReceiptCode() {
//获取redis中所有以tuangou.receipt.prepare开头的key
Set keys = redisTemplate.keys("tuangou.receipt.prepare.*");
//遍历key
keys.stream().forEach(o -> {
//获取key对应的value
JSONObject jsonObject = (JSONObject) redisTemplate.opsForValue().get(o);
//获取过期时间
Date expirationTime = jsonObject.getDate("expirationTime");
//判断是否过期
if (expirationTime.getTime() < new Date().getTime()) {
throw new RuntimeException("券码已过期");
}
//获取redis中的券码id
Long consumerCouponId = jsonObject.getLong("consumerCouponId");
//删除redis中的值
redisTemplate.delete(o);
//删除数据库中的值
isConsumerCouponService.deleteSConsumerCouponById(consumerCouponId);
});
}
}
......@@ -129,4 +129,16 @@ public interface ISOrderService extends IService<SOrder>
SOrderVo queryOrderInfoByNo(String orderNo);
List<SOrderVo> availableOrder();
/**
* 支付成功
*
* @param sOrder
*/
void paymentSuccessful(SOrder sOrder);
/**
* 退款成功
*/
void refundSuccessful(SOrder sOrder);
}
......@@ -148,7 +148,7 @@ public class CallbackServiceImpl implements CallbackService {
try {
sOrder.setPayStatus(YesNoEnum.yes.getIndex());
sOrder.setPayTime(DateUtils.getNowDate());
sOrderService.updateById(sOrder);
sOrderService.paymentSuccessful(sOrder);
wechatPayInfoService.updateById(wechatPayInfo);
execute = Boolean.TRUE;
} catch (Exception e) {
......
package share.system.service.impl;
import cn.hutool.json.JSONObject;
import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.dianping.openapi.sdk.api.oauth.CustomerRefreshToken;
import com.dianping.openapi.sdk.api.oauth.DynamicToken;
......@@ -17,8 +18,11 @@ import org.apache.commons.lang3.StringUtils;
import org.springframework.beans.BeanUtils;
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.Service;
import share.common.core.redis.RedisUtil;
import share.common.enums.*;
import share.common.utils.DateUtils;
import share.system.domain.Device;
import share.system.domain.SConsumer;
import share.system.domain.SConsumerCoupon;
......@@ -47,6 +51,8 @@ public class QPServiceImpl implements QPService {
@Autowired
private ISConsumerCouponService isConsumerCouponService;
RedisTemplate redisTemplate = new RedisTemplate();
/**
* 用户验卷接口
*/
......@@ -54,23 +60,46 @@ public class QPServiceImpl implements QPService {
public TuangouReceiptPrepareResponseEntityVo consumeByUser(String code, String openShopUuid) {
//验券准备
TuangouReceiptPrepareResponseEntity prepare = prepare(code, openShopUuid);
//根据优惠卷名称查询优惠劵配置
SCoupon sCoupon = isCouponService.selectSCouponByName(prepare.getDeal_title());
if (sCoupon == null) {
throw new RuntimeException("未找到对应的优惠券");
}
//获取用户信息
SConsumer user = FrontTokenComponent.getWxSConsumerEntry();
TuangouReceiptPrepareResponseEntityVo response = new TuangouReceiptPrepareResponseEntityVo();
//查询领取记录表
LambdaQueryWrapper<SConsumerCoupon> queryWrapper = new LambdaQueryWrapper<>();
queryWrapper.eq(SConsumerCoupon::getDealId, prepare.getDeal_id());
queryWrapper.eq(SConsumerCoupon::getConsumerId, user.getId());
SConsumerCoupon one = isConsumerCouponService.getOne(queryWrapper);
//判断是否领取过
if (ObjectUtils.isEmpty(one)) {
throw new RuntimeException("该券码已被使用");
//领取删除原来的
isConsumerCouponService.deleteSConsumerCouponById(one.getId());
//判断redis键是否存在
if (Boolean.TRUE.equals(redisTemplate.hasKey(ReceiptRdeisEnum.PREPARE.getValue() + "." + prepare.getReceipt_code()))) {
//获取redis中的值
JSONObject jsonObject = (JSONObject) redisTemplate.opsForValue().get(prepare.getReceipt_code());
//获取过期时间
Date expirationTime = jsonObject.getDate("expirationTime");
//判断是否过期
if (expirationTime.getTime() < new Date().getTime()) {
throw new RuntimeException("券码已过期");
}
//获取redis中的券码id
Long consumerCouponId = jsonObject.getLong("consumerCouponId");
//删除redis中的值
redisTemplate.delete(ReceiptRdeisEnum.PREPARE.getValue() + "." + prepare.getReceipt_code());
//删除数据库中的值
isConsumerCouponService.deleteSConsumerCouponById(consumerCouponId);
}
}
if (ObjectUtils.isNotEmpty(sCoupon)) {
SConsumerCoupon sConsumerCoupon = new SConsumerCoupon();
sConsumerCoupon.setDealId(prepare.getDeal_id());
sConsumerCoupon.setCouponCode(code);
sConsumerCoupon.setName(prepare.getDeal_title());
sConsumerCoupon.setCouponType(CouponTypeEnum.CASH.getCode());
sConsumerCoupon.setStoreType(StoreType.getCodeList());
......@@ -115,9 +144,16 @@ 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));
redisTemplate.opsForValue().set(ReceiptRdeisEnum.PREPARE.getValue() + "." + prepare.getReceipt_code(), new JSONObject(map));
SimpleDateFormat format2 = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
String output1 = format2.format(prepare.getReceiptEndDate());
BeanUtils.copyProperties(prepare, response);
response.setExpirationTime(output1);
return response;
......
......@@ -180,7 +180,6 @@ public class SOrderServiceImpl extends ServiceImpl<SOrderMapper,SOrder> implemen
}
SStore sStore = storeService.getById(request.getStoreId());
//验劵
TuangouReceiptPrepareResponseEntityVo tuangouReceiptPrepareResponseEntityVo = qpService.consumeByUser(request.getCode(), sStore.getOpenShopUuid());
SOrder sOrder = generatSOrder(request,user);
//校验订单金额
checkOrderPrice(sOrder,user);
......@@ -200,13 +199,11 @@ public class SOrderServiceImpl extends ServiceImpl<SOrderMapper,SOrder> implemen
}
if (response.getStatus().equals(YesNoEnum.yes.getFlag())) {
SConsumerCoupon consumerCoupon = new SConsumerCoupon();
consumerCoupon.setId(tuangouReceiptPrepareResponseEntityVo.getConsumerCouponId());
consumerCoupon.setId(request.getCouponId());
consumerCoupon.setUseStatus(UserStatusEnum.USED.getCode());
consumerCouponService.updateById(consumerCoupon);
//验劵
qpService.consume(request.getCode(), 1, sStore.getOpenShopUuid());
} else {
consumerCouponService.deleteSConsumerCouponById(tuangouReceiptPrepareResponseEntityVo.getConsumerCouponId());
}
save(sOrder);
return response;
......@@ -444,6 +441,26 @@ public class SOrderServiceImpl extends ServiceImpl<SOrderMapper,SOrder> implemen
return convertDosToVos(orderList);
}
@Override
public void paymentSuccessful(SOrder sOrder) {
sOrderMapper.updateById(sOrder);
Long couponId = sOrder.getCouponId();
if (couponId != null && couponId > 0) {
SConsumerCoupon consumerCoupon = consumerCouponService.getById(couponId);
if (consumerCoupon != null) {
SStore sStore = storeService.getById(sOrder.getStoreId());
qpService.consume(consumerCoupon.getCouponCode(), 1, sStore.getOpenShopUuid());
consumerCoupon.setUseStatus(UserStatusEnum.USED.getCode());
consumerCouponService.updateById(consumerCoupon);
}
}
}
@Override
public void refundSuccessful(SOrder sOrder) {
}
/**
* 订单DO集合转换VO集合,按距离排序
*
......
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