bug fix
This commit is contained in:
@@ -97,7 +97,7 @@ public class BuyOrderServiceImpl extends ServiceImpl<BuyOrderDao, BuyOrder> impl
|
||||
if (!ObjectUtils.isEmpty(myUserEntity)) {
|
||||
record.setUserName(myUserEntity.getName());
|
||||
record.setProducts(buyOrderDetailService.list(new QueryWrapper<BuyOrderDetail>()
|
||||
.eq("order_id", record.getOrderId())));
|
||||
.eq("order_id", record.getId())));
|
||||
}
|
||||
|
||||
}
|
||||
@@ -121,7 +121,7 @@ public class BuyOrderServiceImpl extends ServiceImpl<BuyOrderDao, BuyOrder> impl
|
||||
page = this.page(page, queryWrapper);
|
||||
List<BuyOrder> records = page.getRecords();
|
||||
for (BuyOrder buyOrder : records) {
|
||||
Integer orderId = buyOrder.getOrderId();
|
||||
Integer orderId = buyOrder.getId();
|
||||
QueryWrapper<BuyOrderDetail> buyOrderDetailQueryWrapper = new QueryWrapper<>();
|
||||
buyOrderDetailQueryWrapper.eq("order_id", orderId);
|
||||
List<BuyOrderDetail> buyOrderDetailList = buyOrderDetailService.list(buyOrderDetailQueryWrapper);
|
||||
@@ -179,14 +179,14 @@ public class BuyOrderServiceImpl extends ServiceImpl<BuyOrderDao, BuyOrder> impl
|
||||
);
|
||||
for (BuyOrder order : buyOrderList) {
|
||||
order.setProducts(buyOrderDetailService.list(new QueryWrapper<BuyOrderDetail>()
|
||||
.eq("order_id", order.getOrderId())));
|
||||
.eq("order_id", order.getId())));
|
||||
orderList.add(order);
|
||||
}
|
||||
// 清洗数据 (与前端传来的id对比后)
|
||||
List<BuyOrder> washOrderList = new ArrayList<>();
|
||||
for (BuyOrder order : orderList) {
|
||||
for (int o : orderIds) {
|
||||
if (o == order.getOrderId()) {
|
||||
if (o == order.getId()) {
|
||||
washOrderList.add(order);
|
||||
}
|
||||
}
|
||||
@@ -268,7 +268,7 @@ public class BuyOrderServiceImpl extends ServiceImpl<BuyOrderDao, BuyOrder> impl
|
||||
);
|
||||
for (BuyOrder order : buyOrderList.getRecords()) {
|
||||
order.setProducts(buyOrderDetailService.list(new QueryWrapper<BuyOrderDetail>()
|
||||
.eq("order_id", order.getOrderId())));
|
||||
.eq("order_id", order.getId())));
|
||||
orderList.add(order);
|
||||
}
|
||||
// 获取有订单的所有用户
|
||||
@@ -334,26 +334,29 @@ public class BuyOrderServiceImpl extends ServiceImpl<BuyOrderDao, BuyOrder> impl
|
||||
|
||||
|
||||
@Override
|
||||
public void delivery(String expressCompanyCode, List<Integer> buyOrderDetailId) {
|
||||
QueryWrapper<BuyOrderDetail> queryWrapper = new QueryWrapper<>();
|
||||
queryWrapper.in("id", buyOrderDetailId);
|
||||
List<BuyOrderDetail> buyOrderDetailList = buyOrderDetailService.list(queryWrapper);
|
||||
public void delivery(String expressCompanyCode, List<Integer> buyOrderProductId) {
|
||||
QueryWrapper<BuyOrderProduct> queryWrapper = new QueryWrapper<>();
|
||||
queryWrapper.in("id", buyOrderProductId);
|
||||
List<BuyOrderProduct> buyOrderProductList = buyOrderProductService.list(queryWrapper);
|
||||
BigDecimal totalWeight = new BigDecimal(0);
|
||||
List<ExpressCommodity> commodityList = new ArrayList<>();
|
||||
for (BuyOrderDetail buyOrderDetail : buyOrderDetailList) {
|
||||
for (BuyOrderProduct buyOrderProduct : buyOrderProductList) {
|
||||
int productId = buyOrderProduct.getProductId();
|
||||
ShopProduct product = shopProductService.getById(productId);
|
||||
ExpressCommodity commodity = new ExpressCommodity();
|
||||
commodity.setGoodsName(buyOrderDetail.getProductName());
|
||||
commodity.setGoodsquantity(buyOrderDetail.getQuantity());
|
||||
commodity.setGoodsWeight(buyOrderDetail.getWeight().doubleValue());
|
||||
commodity.setGoodsName(product.getProductName());
|
||||
commodity.setGoodsquantity(buyOrderProduct.getQuantity());
|
||||
commodity.setGoodsWeight(product.getWeight().doubleValue());
|
||||
totalWeight = totalWeight.add(
|
||||
BigDecimal.valueOf(buyOrderDetail.getWeight().doubleValue()).multiply(new BigDecimal(buyOrderDetail.getQuantity()))
|
||||
BigDecimal.valueOf(product.getWeight().doubleValue()).multiply(new BigDecimal(buyOrderProduct.getQuantity()))
|
||||
);
|
||||
totalWeight = totalWeight.setScale(0, RoundingMode.UP);
|
||||
commodityList.add(commodity);
|
||||
}
|
||||
|
||||
// 获取用户地址
|
||||
Integer orderId = buyOrderDetailList.get(0).getOrderId();
|
||||
Integer orderId = buyOrderProductList.get(0).getOrderId();
|
||||
|
||||
BuyOrder buyOrder = getById(orderId);
|
||||
QueryWrapper<County> countyQueryWrapper = new QueryWrapper<>();
|
||||
countyQueryWrapper.eq("county_name", buyOrder.getDistrict());
|
||||
@@ -374,14 +377,11 @@ public class BuyOrderServiceImpl extends ServiceImpl<BuyOrderDao, BuyOrder> impl
|
||||
expressOrder.setAddress(buyOrder.getAddress());
|
||||
// 生成快递面单
|
||||
ExpressOrderResponseVo response = expressOrderService.placeExpressOrder(expressOrder);
|
||||
String expressBillNo = response.getOrder().getLogisticCode();
|
||||
String expressBillTemplate = response.getPrintTemplate();
|
||||
for (BuyOrderDetail buyOrderDetail : buyOrderDetailList) {
|
||||
buyOrderDetail.setExpressBillNo(expressBillNo);
|
||||
buyOrderDetail.setExpressBillTemplate(expressBillTemplate);
|
||||
buyOrderDetail.setExpressCompanyCode(expressCompanyCode);
|
||||
}
|
||||
buyOrderDetailService.saveBatch(buyOrderDetailList);
|
||||
String expressOrderSn = response.getOrder().getLogisticCode();
|
||||
String printTemplate = response.getPrintTemplate();
|
||||
expressOrder.setExpressOrderSn(expressOrderSn);
|
||||
expressOrder.setPrintTemplate(printTemplate);
|
||||
expressOrderService.save(expressOrder);
|
||||
}
|
||||
|
||||
@Override
|
||||
@@ -425,7 +425,7 @@ public class BuyOrderServiceImpl extends ServiceImpl<BuyOrderDao, BuyOrder> impl
|
||||
consigneeVo.setAddress(buyOrder.getAddress());
|
||||
responseVo.setConsignee(consigneeVo);
|
||||
QueryWrapper<BuyOrderProduct> queryWrapper = new QueryWrapper<>();
|
||||
queryWrapper.eq("order_id", buyOrder.getOrderId());
|
||||
queryWrapper.eq("order_id", buyOrder.getId());
|
||||
List<BuyOrderProduct> buyOrderProductList = buyOrderProductService.list(queryWrapper);
|
||||
List<GoodsResponseVo> goodsResponseVoList = new ArrayList<>();
|
||||
for (BuyOrderProduct buyOrderProduct : buyOrderProductList) {
|
||||
|
||||
@@ -1,255 +1,255 @@
|
||||
package com.peanut.modules.book.service.impl;
|
||||
|
||||
import com.alibaba.fastjson.JSON;
|
||||
import com.alibaba.fastjson.TypeReference;
|
||||
import com.baomidou.mybatisplus.core.toolkit.IdWorker;
|
||||
import com.peanut.modules.book.entity.SeckillProdRelationEntity;
|
||||
import com.peanut.modules.book.entity.ShopProduct;
|
||||
import com.peanut.modules.book.service.SeckillProdRelationService;
|
||||
import com.peanut.modules.book.service.ShopProductService;
|
||||
import com.peanut.modules.book.to.SeckillRedisTo;
|
||||
import com.peanut.modules.book.vo.ProdInfoVo;
|
||||
import com.peanut.modules.book.vo.SeckillSessionWithProdVo;
|
||||
import lombok.extern.slf4j.Slf4j;
|
||||
import org.apache.commons.lang.StringUtils;
|
||||
import org.redisson.api.RSemaphore;
|
||||
import org.redisson.api.RedissonClient;
|
||||
import org.springframework.beans.BeanUtils;
|
||||
import org.springframework.beans.factory.annotation.Autowired;
|
||||
import org.springframework.data.redis.core.BoundHashOperations;
|
||||
import org.springframework.data.redis.core.StringRedisTemplate;
|
||||
import org.springframework.stereotype.Service;
|
||||
|
||||
import java.time.LocalDate;
|
||||
import java.time.LocalDateTime;
|
||||
import java.time.LocalTime;
|
||||
import java.time.format.DateTimeFormatter;
|
||||
import java.util.*;
|
||||
import java.util.concurrent.TimeUnit;
|
||||
import java.util.stream.Collectors;
|
||||
|
||||
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
|
||||
import com.baomidou.mybatisplus.core.metadata.IPage;
|
||||
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
|
||||
import com.peanut.common.utils.PageUtils;
|
||||
import com.peanut.common.utils.Query;
|
||||
|
||||
import com.peanut.modules.book.dao.ShopSeckillDao;
|
||||
import com.peanut.modules.book.entity.ShopSeckillEntity;
|
||||
import com.peanut.modules.book.service.ShopSeckillService;
|
||||
|
||||
@Slf4j
|
||||
@Service("shopSeckillService")
|
||||
public class ShopSeckillServiceImpl extends ServiceImpl<ShopSeckillDao, ShopSeckillEntity> implements ShopSeckillService {
|
||||
|
||||
@Autowired
|
||||
private SeckillProdRelationService seckillProdRelationService;
|
||||
@Autowired
|
||||
StringRedisTemplate stringRedisTemplate;
|
||||
@Autowired
|
||||
private ShopProductService shopProductService;
|
||||
@Autowired
|
||||
RedissonClient redissonClient;
|
||||
|
||||
private final String SESSIONS_CACHE_PREFIX = "seckill:sessions:";
|
||||
private final String PROKILL_CACHE_PREFIX = "prokill:pro";
|
||||
private final String PRO_STOCK_SEMAPHORE = "seckill:stock:";
|
||||
|
||||
@Override
|
||||
public PageUtils queryPage(Map<String, Object> params) {
|
||||
IPage<ShopSeckillEntity> page = this.page(
|
||||
new Query<ShopSeckillEntity>().getPage(params),
|
||||
new QueryWrapper<ShopSeckillEntity>()
|
||||
|
||||
);
|
||||
|
||||
return new PageUtils(page);
|
||||
}
|
||||
|
||||
@Override
|
||||
public List<ShopSeckillEntity> getSeckilProd3Days() {
|
||||
List<ShopSeckillEntity> list = this.baseMapper.selectList(new QueryWrapper<ShopSeckillEntity>().between("start_time", startTime(), endTime()));
|
||||
if (list != null && list.size() > 0) {
|
||||
list.stream().map(session -> {
|
||||
Long seckillId = session.getSeckillId();
|
||||
List<SeckillProdRelationEntity> seckillList = seckillProdRelationService.list(new QueryWrapper<SeckillProdRelationEntity>().eq("promotion_seckill_id", seckillId));
|
||||
session.setRelationProd(seckillList);
|
||||
return session;
|
||||
}).collect(Collectors.toList());
|
||||
}
|
||||
return list;
|
||||
}
|
||||
|
||||
@Override
|
||||
public void uploadSeckilProd3Days() {
|
||||
List<ShopSeckillEntity> seckilProd = this.getSeckilProd3Days();
|
||||
List<SeckillSessionWithProdVo> seckilProdVo = JSON.parseObject(JSON.toJSONString(seckilProd), new TypeReference<List<SeckillSessionWithProdVo>>() {
|
||||
});
|
||||
saveSessionInfo(seckilProdVo);
|
||||
saveSessionProdInfo(seckilProdVo);
|
||||
|
||||
Set<String> keys = stringRedisTemplate.keys(SESSIONS_CACHE_PREFIX + "*");
|
||||
long time = new Date().getTime();
|
||||
for(String key : keys){
|
||||
String replace = key.replace(SESSIONS_CACHE_PREFIX, "");
|
||||
String[] s = replace.split("_");
|
||||
long endTime = Long.parseLong(s[1]);
|
||||
if(endTime <= time){
|
||||
stringRedisTemplate.delete(key);
|
||||
}
|
||||
}
|
||||
|
||||
}
|
||||
|
||||
@Override
|
||||
public List<SeckillRedisTo> getCurrentSeckillProd() {
|
||||
long time = new Date().getTime();
|
||||
|
||||
Set<String> keys = stringRedisTemplate.keys(SESSIONS_CACHE_PREFIX + "*");
|
||||
|
||||
for (String key : keys) {
|
||||
String replace = key.replace(SESSIONS_CACHE_PREFIX, "");
|
||||
String[] s = replace.split("_");
|
||||
long startTime = Long.parseLong(s[0]);
|
||||
long endTime = Long.parseLong(s[1]);
|
||||
|
||||
|
||||
if (time >= startTime && time <= endTime ) {
|
||||
|
||||
List<String> list = stringRedisTemplate.opsForList().range(key, -100, 100);
|
||||
BoundHashOperations<String, String, Object> operations = stringRedisTemplate.boundHashOps(PROKILL_CACHE_PREFIX);
|
||||
List<Object> objects = operations.multiGet(list);
|
||||
if (objects.size() > 0) {
|
||||
List<SeckillRedisTo> collect = objects.stream().map(item -> {
|
||||
SeckillRedisTo seckillRedisTo = JSON.parseObject(item.toString(), SeckillRedisTo.class);
|
||||
// seckillRedisTo.setRandomCode(null);
|
||||
return seckillRedisTo;
|
||||
}).collect(Collectors.toList());
|
||||
return collect;
|
||||
}
|
||||
|
||||
break;
|
||||
}
|
||||
}
|
||||
|
||||
return null;
|
||||
}
|
||||
|
||||
@Override
|
||||
public String kill(String killId, String key, Integer num , Integer userId) {
|
||||
|
||||
|
||||
BoundHashOperations<String, String, String> hashOps = stringRedisTemplate.boundHashOps(PROKILL_CACHE_PREFIX);
|
||||
String o = hashOps.get(killId);
|
||||
if (StringUtils.isEmpty(o)){
|
||||
return null;
|
||||
}else {
|
||||
SeckillRedisTo seckillRedisTo = JSON.parseObject(o, SeckillRedisTo.class);
|
||||
Long startTime = seckillRedisTo.getStartTime();
|
||||
Long endTime = seckillRedisTo.getEndTime();
|
||||
|
||||
long time = new Date().getTime();
|
||||
|
||||
long ttl = endTime - time;
|
||||
if ( time >= startTime && time <= endTime){
|
||||
String randomCode = seckillRedisTo.getRandomCode();
|
||||
String prodId = seckillRedisTo.getPromotionSeckillId() + "_" + seckillRedisTo.getProdId();
|
||||
if( randomCode.equals(key) && killId.equals(prodId)){
|
||||
if (num <= Integer.valueOf(seckillRedisTo.getSeckillLimit())) {
|
||||
String redisKey = userId + "_" + prodId;
|
||||
Boolean aBoolean = stringRedisTemplate.opsForValue().setIfAbsent(redisKey, num.toString(), ttl, TimeUnit.MILLISECONDS);
|
||||
if (aBoolean) {
|
||||
RSemaphore semaphore = redissonClient.getSemaphore(PRO_STOCK_SEMAPHORE + randomCode);
|
||||
try {
|
||||
boolean b = semaphore.tryAcquire(num, 100, TimeUnit.MILLISECONDS);
|
||||
// 秒杀成功
|
||||
String timeId = IdWorker.getTimeId();
|
||||
return timeId;
|
||||
|
||||
} catch (InterruptedException e) {
|
||||
return null;
|
||||
}
|
||||
}else {
|
||||
return null;
|
||||
}
|
||||
}
|
||||
|
||||
}else {
|
||||
return null;
|
||||
}
|
||||
}else {
|
||||
return null;
|
||||
}
|
||||
}
|
||||
|
||||
return null;
|
||||
}
|
||||
|
||||
|
||||
private void saveSessionInfo(List<SeckillSessionWithProdVo> seckilProdVo) {
|
||||
seckilProdVo.stream().forEach(session -> {
|
||||
long startTime = session.getStartTime().getTime();
|
||||
long endTime = session.getEndTime().getTime();
|
||||
String key = SESSIONS_CACHE_PREFIX + startTime + "_" + endTime;
|
||||
|
||||
Boolean aBoolean = stringRedisTemplate.hasKey(key);
|
||||
if (!aBoolean) {
|
||||
List<String> collect = session.getRelationProd().stream().map(item -> item.getPromotionSeckillId() + "_" + item.getProdId().toString()).collect(Collectors.toList());
|
||||
stringRedisTemplate.opsForList().leftPushAll(key, collect);
|
||||
}
|
||||
});
|
||||
}
|
||||
|
||||
private void saveSessionProdInfo(List<SeckillSessionWithProdVo> seckilProdVos) {
|
||||
|
||||
seckilProdVos.stream().forEach(seckilProdVo -> {
|
||||
|
||||
BoundHashOperations<String, Object, Object> ops = stringRedisTemplate.boundHashOps(PROKILL_CACHE_PREFIX);
|
||||
|
||||
seckilProdVo.getRelationProd().stream().forEach(seckilProd -> {
|
||||
String token = UUID.randomUUID().toString().replace("-", "");
|
||||
if (!ops.hasKey(seckilProd.getPromotionSeckillId().toString() + "_" + seckilProd.getProdId().toString())) {
|
||||
SeckillRedisTo seckillRedisTo = new SeckillRedisTo();
|
||||
|
||||
ShopProduct productEntity = shopProductService.getById(seckilProd.getProdId());
|
||||
ProdInfoVo prodInfoVo = JSON.parseObject(JSON.toJSONString(productEntity), new TypeReference<ProdInfoVo>() {
|
||||
});
|
||||
seckillRedisTo.setProdInfo(prodInfoVo);
|
||||
BeanUtils.copyProperties(seckilProd, seckillRedisTo);
|
||||
|
||||
seckillRedisTo.setStartTime(seckilProdVo.getStartTime().getTime());
|
||||
seckillRedisTo.setEndTime(seckilProdVo.getEndTime().getTime());
|
||||
|
||||
seckillRedisTo.setRandomCode(token);
|
||||
|
||||
|
||||
String string = JSON.toJSONString(seckillRedisTo);
|
||||
ops.put(seckilProd.getPromotionSeckillId().toString() + "_" + seckilProd.getProdId().toString(), string);
|
||||
|
||||
RSemaphore semaphore = redissonClient.getSemaphore(PRO_STOCK_SEMAPHORE + token);
|
||||
semaphore.trySetPermits(Integer.valueOf(seckilProd.getSeckillCount()));
|
||||
}
|
||||
|
||||
|
||||
});
|
||||
});
|
||||
|
||||
}
|
||||
|
||||
|
||||
private String startTime() {
|
||||
LocalDate now = LocalDate.now();
|
||||
LocalTime min = LocalTime.MIN;
|
||||
LocalDateTime start = LocalDateTime.of(now, min);
|
||||
String format = start.format(DateTimeFormatter.ofPattern("yyyy-MM-dd HH:mm:ss"));
|
||||
return format;
|
||||
}
|
||||
|
||||
private String endTime() {
|
||||
LocalDate now = LocalDate.now();
|
||||
LocalDate localDate = now.plusDays(2);
|
||||
LocalDateTime of = LocalDateTime.of(localDate, LocalTime.MAX);
|
||||
String format = of.format(DateTimeFormatter.ofPattern("yyyy-MM-dd HH:mm:ss"));
|
||||
return format;
|
||||
}
|
||||
}
|
||||
//package com.peanut.modules.book.service.impl;
|
||||
//
|
||||
//import com.alibaba.fastjson.JSON;
|
||||
//import com.alibaba.fastjson.TypeReference;
|
||||
//import com.baomidou.mybatisplus.core.toolkit.IdWorker;
|
||||
//import com.peanut.modules.book.entity.SeckillProdRelationEntity;
|
||||
//import com.peanut.modules.book.entity.ShopProduct;
|
||||
//import com.peanut.modules.book.service.SeckillProdRelationService;
|
||||
//import com.peanut.modules.book.service.ShopProductService;
|
||||
//import com.peanut.modules.book.to.SeckillRedisTo;
|
||||
//import com.peanut.modules.book.vo.ProdInfoVo;
|
||||
//import com.peanut.modules.book.vo.SeckillSessionWithProdVo;
|
||||
//import lombok.extern.slf4j.Slf4j;
|
||||
//import org.apache.commons.lang.StringUtils;
|
||||
//import org.redisson.api.RSemaphore;
|
||||
//import org.redisson.api.RedissonClient;
|
||||
//import org.springframework.beans.BeanUtils;
|
||||
//import org.springframework.beans.factory.annotation.Autowired;
|
||||
//import org.springframework.data.redis.core.BoundHashOperations;
|
||||
//import org.springframework.data.redis.core.StringRedisTemplate;
|
||||
//import org.springframework.stereotype.Service;
|
||||
//
|
||||
//import java.time.LocalDate;
|
||||
//import java.time.LocalDateTime;
|
||||
//import java.time.LocalTime;
|
||||
//import java.time.format.DateTimeFormatter;
|
||||
//import java.util.*;
|
||||
//import java.util.concurrent.TimeUnit;
|
||||
//import java.util.stream.Collectors;
|
||||
//
|
||||
//import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
|
||||
//import com.baomidou.mybatisplus.core.metadata.IPage;
|
||||
//import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
|
||||
//import com.peanut.common.utils.PageUtils;
|
||||
//import com.peanut.common.utils.Query;
|
||||
//
|
||||
//import com.peanut.modules.book.dao.ShopSeckillDao;
|
||||
//import com.peanut.modules.book.entity.ShopSeckillEntity;
|
||||
//import com.peanut.modules.book.service.ShopSeckillService;
|
||||
//
|
||||
//@Slf4j
|
||||
//@Service("shopSeckillService")
|
||||
//public class ShopSeckillServiceImpl extends ServiceImpl<ShopSeckillDao, ShopSeckillEntity> implements ShopSeckillService {
|
||||
//
|
||||
// @Autowired
|
||||
// private SeckillProdRelationService seckillProdRelationService;
|
||||
// @Autowired
|
||||
// StringRedisTemplate stringRedisTemplate;
|
||||
// @Autowired
|
||||
// private ShopProductService shopProductService;
|
||||
// @Autowired
|
||||
// RedissonClient redissonClient;
|
||||
//
|
||||
// private final String SESSIONS_CACHE_PREFIX = "seckill:sessions:";
|
||||
// private final String PROKILL_CACHE_PREFIX = "prokill:pro";
|
||||
// private final String PRO_STOCK_SEMAPHORE = "seckill:stock:";
|
||||
//
|
||||
// @Override
|
||||
// public PageUtils queryPage(Map<String, Object> params) {
|
||||
// IPage<ShopSeckillEntity> page = this.page(
|
||||
// new Query<ShopSeckillEntity>().getPage(params),
|
||||
// new QueryWrapper<ShopSeckillEntity>()
|
||||
//
|
||||
// );
|
||||
//
|
||||
// return new PageUtils(page);
|
||||
// }
|
||||
//
|
||||
// @Override
|
||||
// public List<ShopSeckillEntity> getSeckilProd3Days() {
|
||||
// List<ShopSeckillEntity> list = this.baseMapper.selectList(new QueryWrapper<ShopSeckillEntity>().between("start_time", startTime(), endTime()));
|
||||
// if (list != null && list.size() > 0) {
|
||||
// list.stream().map(session -> {
|
||||
// Long seckillId = session.getSeckillId();
|
||||
// List<SeckillProdRelationEntity> seckillList = seckillProdRelationService.list(new QueryWrapper<SeckillProdRelationEntity>().eq("promotion_seckill_id", seckillId));
|
||||
// session.setRelationProd(seckillList);
|
||||
// return session;
|
||||
// }).collect(Collectors.toList());
|
||||
// }
|
||||
// return list;
|
||||
// }
|
||||
//
|
||||
// @Override
|
||||
// public void uploadSeckilProd3Days() {
|
||||
// List<ShopSeckillEntity> seckilProd = this.getSeckilProd3Days();
|
||||
// List<SeckillSessionWithProdVo> seckilProdVo = JSON.parseObject(JSON.toJSONString(seckilProd), new TypeReference<List<SeckillSessionWithProdVo>>() {
|
||||
// });
|
||||
// saveSessionInfo(seckilProdVo);
|
||||
// saveSessionProdInfo(seckilProdVo);
|
||||
//
|
||||
// Set<String> keys = stringRedisTemplate.keys(SESSIONS_CACHE_PREFIX + "*");
|
||||
// long time = new Date().getTime();
|
||||
// for(String key : keys){
|
||||
// String replace = key.replace(SESSIONS_CACHE_PREFIX, "");
|
||||
// String[] s = replace.split("_");
|
||||
// long endTime = Long.parseLong(s[1]);
|
||||
// if(endTime <= time){
|
||||
// stringRedisTemplate.delete(key);
|
||||
// }
|
||||
// }
|
||||
//
|
||||
// }
|
||||
//
|
||||
// @Override
|
||||
// public List<SeckillRedisTo> getCurrentSeckillProd() {
|
||||
// long time = new Date().getTime();
|
||||
//
|
||||
// Set<String> keys = stringRedisTemplate.keys(SESSIONS_CACHE_PREFIX + "*");
|
||||
//
|
||||
// for (String key : keys) {
|
||||
// String replace = key.replace(SESSIONS_CACHE_PREFIX, "");
|
||||
// String[] s = replace.split("_");
|
||||
// long startTime = Long.parseLong(s[0]);
|
||||
// long endTime = Long.parseLong(s[1]);
|
||||
//
|
||||
//
|
||||
// if (time >= startTime && time <= endTime ) {
|
||||
//
|
||||
// List<String> list = stringRedisTemplate.opsForList().range(key, -100, 100);
|
||||
// BoundHashOperations<String, String, Object> operations = stringRedisTemplate.boundHashOps(PROKILL_CACHE_PREFIX);
|
||||
// List<Object> objects = operations.multiGet(list);
|
||||
// if (objects.size() > 0) {
|
||||
// List<SeckillRedisTo> collect = objects.stream().map(item -> {
|
||||
// SeckillRedisTo seckillRedisTo = JSON.parseObject(item.toString(), SeckillRedisTo.class);
|
||||
//// seckillRedisTo.setRandomCode(null);
|
||||
// return seckillRedisTo;
|
||||
// }).collect(Collectors.toList());
|
||||
// return collect;
|
||||
// }
|
||||
//
|
||||
// break;
|
||||
// }
|
||||
// }
|
||||
//
|
||||
// return null;
|
||||
// }
|
||||
//
|
||||
// @Override
|
||||
// public String kill(String killId, String key, Integer num , Integer userId) {
|
||||
//
|
||||
//
|
||||
// BoundHashOperations<String, String, String> hashOps = stringRedisTemplate.boundHashOps(PROKILL_CACHE_PREFIX);
|
||||
// String o = hashOps.get(killId);
|
||||
// if (StringUtils.isEmpty(o)){
|
||||
// return null;
|
||||
// }else {
|
||||
// SeckillRedisTo seckillRedisTo = JSON.parseObject(o, SeckillRedisTo.class);
|
||||
// Long startTime = seckillRedisTo.getStartTime();
|
||||
// Long endTime = seckillRedisTo.getEndTime();
|
||||
//
|
||||
// long time = new Date().getTime();
|
||||
//
|
||||
// long ttl = endTime - time;
|
||||
// if ( time >= startTime && time <= endTime){
|
||||
// String randomCode = seckillRedisTo.getRandomCode();
|
||||
// String prodId = seckillRedisTo.getPromotionSeckillId() + "_" + seckillRedisTo.getProdId();
|
||||
// if( randomCode.equals(key) && killId.equals(prodId)){
|
||||
// if (num <= Integer.valueOf(seckillRedisTo.getSeckillLimit())) {
|
||||
// String redisKey = userId + "_" + prodId;
|
||||
// Boolean aBoolean = stringRedisTemplate.opsForValue().setIfAbsent(redisKey, num.toString(), ttl, TimeUnit.MILLISECONDS);
|
||||
// if (aBoolean) {
|
||||
// RSemaphore semaphore = redissonClient.getSemaphore(PRO_STOCK_SEMAPHORE + randomCode);
|
||||
// try {
|
||||
// boolean b = semaphore.tryAcquire(num, 100, TimeUnit.MILLISECONDS);
|
||||
// // 秒杀成功
|
||||
// String timeId = IdWorker.getTimeId();
|
||||
// return timeId;
|
||||
//
|
||||
// } catch (InterruptedException e) {
|
||||
// return null;
|
||||
// }
|
||||
// }else {
|
||||
// return null;
|
||||
// }
|
||||
// }
|
||||
//
|
||||
// }else {
|
||||
// return null;
|
||||
// }
|
||||
// }else {
|
||||
// return null;
|
||||
// }
|
||||
// }
|
||||
//
|
||||
// return null;
|
||||
// }
|
||||
//
|
||||
//
|
||||
// private void saveSessionInfo(List<SeckillSessionWithProdVo> seckilProdVo) {
|
||||
// seckilProdVo.stream().forEach(session -> {
|
||||
// long startTime = session.getStartTime().getTime();
|
||||
// long endTime = session.getEndTime().getTime();
|
||||
// String key = SESSIONS_CACHE_PREFIX + startTime + "_" + endTime;
|
||||
//
|
||||
// Boolean aBoolean = stringRedisTemplate.hasKey(key);
|
||||
// if (!aBoolean) {
|
||||
// List<String> collect = session.getRelationProd().stream().map(item -> item.getPromotionSeckillId() + "_" + item.getProdId().toString()).collect(Collectors.toList());
|
||||
// stringRedisTemplate.opsForList().leftPushAll(key, collect);
|
||||
// }
|
||||
// });
|
||||
// }
|
||||
//
|
||||
// private void saveSessionProdInfo(List<SeckillSessionWithProdVo> seckilProdVos) {
|
||||
//
|
||||
// seckilProdVos.stream().forEach(seckilProdVo -> {
|
||||
//
|
||||
// BoundHashOperations<String, Object, Object> ops = stringRedisTemplate.boundHashOps(PROKILL_CACHE_PREFIX);
|
||||
//
|
||||
// seckilProdVo.getRelationProd().stream().forEach(seckilProd -> {
|
||||
// String token = UUID.randomUUID().toString().replace("-", "");
|
||||
// if (!ops.hasKey(seckilProd.getPromotionSeckillId().toString() + "_" + seckilProd.getProdId().toString())) {
|
||||
// SeckillRedisTo seckillRedisTo = new SeckillRedisTo();
|
||||
//
|
||||
// ShopProduct productEntity = shopProductService.getById(seckilProd.getProdId());
|
||||
// ProdInfoVo prodInfoVo = JSON.parseObject(JSON.toJSONString(productEntity), new TypeReference<ProdInfoVo>() {
|
||||
// });
|
||||
// seckillRedisTo.setProdInfo(prodInfoVo);
|
||||
// BeanUtils.copyProperties(seckilProd, seckillRedisTo);
|
||||
//
|
||||
// seckillRedisTo.setStartTime(seckilProdVo.getStartTime().getTime());
|
||||
// seckillRedisTo.setEndTime(seckilProdVo.getEndTime().getTime());
|
||||
//
|
||||
// seckillRedisTo.setRandomCode(token);
|
||||
//
|
||||
//
|
||||
// String string = JSON.toJSONString(seckillRedisTo);
|
||||
// ops.put(seckilProd.getPromotionSeckillId().toString() + "_" + seckilProd.getProdId().toString(), string);
|
||||
//
|
||||
// RSemaphore semaphore = redissonClient.getSemaphore(PRO_STOCK_SEMAPHORE + token);
|
||||
// semaphore.trySetPermits(Integer.valueOf(seckilProd.getSeckillCount()));
|
||||
// }
|
||||
//
|
||||
//
|
||||
// });
|
||||
// });
|
||||
//
|
||||
// }
|
||||
//
|
||||
//
|
||||
// private String startTime() {
|
||||
// LocalDate now = LocalDate.now();
|
||||
// LocalTime min = LocalTime.MIN;
|
||||
// LocalDateTime start = LocalDateTime.of(now, min);
|
||||
// String format = start.format(DateTimeFormatter.ofPattern("yyyy-MM-dd HH:mm:ss"));
|
||||
// return format;
|
||||
// }
|
||||
//
|
||||
// private String endTime() {
|
||||
// LocalDate now = LocalDate.now();
|
||||
// LocalDate localDate = now.plusDays(2);
|
||||
// LocalDateTime of = LocalDateTime.of(localDate, LocalTime.MAX);
|
||||
// String format = of.format(DateTimeFormatter.ofPattern("yyyy-MM-dd HH:mm:ss"));
|
||||
// return format;
|
||||
// }
|
||||
//}
|
||||
Reference in New Issue
Block a user