记得上下班打卡 | git大法好,push需谨慎

Commit 561c2c8a authored by 张国柄's avatar 张国柄

~API:商品活动数据获取及店铺活动存储;

parent 145c9fd6
......@@ -17,4 +17,14 @@ public class GoblinStoreMarketDto implements Serializable, Cloneable {
* 活动类型:1-优惠券
*/
private Integer type;
private static final GoblinStoreMarketDto obj = new GoblinStoreMarketDto();
public static GoblinStoreMarketDto getNew() {
try {
return (GoblinStoreMarketDto) obj.clone();
} catch (CloneNotSupportedException e) {
return new GoblinStoreMarketDto();
}
}
}
......@@ -966,24 +966,24 @@ public class GoblinRedisUtils {
return dtos;
}
public boolean addStoreMarketDto(String spuId, GoblinStoreMarketDto dto) {
List<GoblinStoreMarketDto> dtos = this.getStoreMarketDtos(spuId);
public boolean addStoreMarketDto(String storeId, GoblinStoreMarketDto dto) {
List<GoblinStoreMarketDto> dtos = this.getStoreMarketDtos(storeId);
dtos.add(dto);
return redisUtil.set(GoblinRedisConst.STORE_MARKETS.concat(spuId), JsonUtils.toJson(dtos));
return redisUtil.set(GoblinRedisConst.STORE_MARKETS.concat(storeId), JsonUtils.toJson(dtos));
}
public void delStoreMarket(String spuId) {
redisUtil.del(GoblinRedisConst.STORE_MARKETS.concat(spuId));
public void delStoreMarketDto(String storeId) {
redisUtil.del(GoblinRedisConst.STORE_MARKETS.concat(storeId));
}
public void delStoreMarket(String spuId, GoblinStoreMarketDto dto) {
String rk = GoblinRedisConst.STORE_MARKETS.concat(spuId);
List<GoblinStoreMarketDto> dtos = this.getStoreMarketDtos(spuId);
public void delStoreMarketDto(String storeId, GoblinStoreMarketDto dto) {
String rk = GoblinRedisConst.STORE_MARKETS.concat(storeId);
List<GoblinStoreMarketDto> dtos = this.getStoreMarketDtos(storeId);
if (!CollectionUtils.isEmpty(dtos)) {
int beforeSize = dtos.size();
dtos.removeIf(r -> r.getId().equals(dto.getId()) && r.getType().equals(dto.getType()));
if (beforeSize > dtos.size()) {
redisUtil.set(GoblinRedisConst.STORE_MARKETS.concat(spuId), JsonUtils.toJson(dtos));
redisUtil.set(GoblinRedisConst.STORE_MARKETS.concat(storeId), JsonUtils.toJson(dtos));
}
}
}
......
package com.liquidnet.service.platform.controller.goblin.task;
import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.conditions.update.LambdaUpdateWrapper;
import com.baomidou.mybatisplus.core.toolkit.Wrappers;
import com.liquidnet.common.cache.redis.util.AbstractRedisUtil;
import com.liquidnet.common.cache.redis.util.RedisDataSourceUtil;
import com.liquidnet.commons.lang.util.JsonUtils;
import com.liquidnet.service.base.ResponseDto;
import com.liquidnet.service.goblin.constant.GoblinRedisConst;
import com.liquidnet.service.goblin.entity.GoblinGoods;
import com.liquidnet.service.goblin.entity.GoblinStoreCoupon;
import com.liquidnet.service.platform.service.impl.goblin.PlatformGoblinGoodsService;
import com.liquidnet.service.platform.service.impl.goblin.PlatformGoblinStoreCouponService;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.util.CollectionUtils;
import org.springframework.web.bind.annotation.PutMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;
import java.time.LocalDateTime;
import java.util.List;
import java.util.stream.Collectors;
/**
* 商城商品任务处理
*
* @author zhanggb
* Created by IntelliJ IDEA at 2022/1/21
*/
@Slf4j
@RestController
@RequestMapping("gstore/task")
public class PlatformGoblinStoreTaskController {
@Autowired
private PlatformGoblinStoreCouponService platformGoblinStoreCouponService;
@PutMapping("market")
public ResponseDto<String> marketActivityProcessing() {
int startNum = 0, stopNum = 0;
LocalDateTime now = LocalDateTime.now();
{// 优惠券活动
startNum += platformGoblinStoreCouponService.activityStartProcessing(now);
stopNum += platformGoblinStoreCouponService.activityStopProcessing(now);
}
return ResponseDto.success(String.format("商铺活动处理成功[开启=%s,结束=%s]", startNum, stopNum));
}
}
package com.liquidnet.service.platform.service.impl.goblin;
import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.conditions.update.LambdaUpdateWrapper;
import com.baomidou.mybatisplus.core.toolkit.Wrappers;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.liquidnet.common.cache.redis.util.AbstractRedisUtil;
import com.liquidnet.common.cache.redis.util.RedisDataSourceUtil;
import com.liquidnet.commons.lang.util.JsonUtils;
import com.liquidnet.service.base.ResponseDto;
import com.liquidnet.service.goblin.constant.GoblinRedisConst;
import com.liquidnet.service.goblin.dto.GoblinStoreMarketDto;
import com.liquidnet.service.goblin.dto.vo.GoblinStoreCouponBasicVo;
import com.liquidnet.service.goblin.entity.GoblinStoreCoupon;
import com.liquidnet.service.goblin.mapper.GoblinGoodsSkuMapper;
import com.liquidnet.service.goblin.mapper.GoblinStoreCouponMapper;
import com.liquidnet.service.platform.utils.GoblinRedisUtils;
import com.mongodb.client.result.UpdateResult;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.mongodb.core.MongoTemplate;
import org.springframework.data.mongodb.core.query.Criteria;
import org.springframework.data.mongodb.core.query.Query;
import org.springframework.data.mongodb.core.query.Update;
import org.springframework.stereotype.Service;
import org.springframework.util.CollectionUtils;
import java.time.LocalDateTime;
import java.util.List;
import java.util.stream.Collectors;
@Slf4j
@Service
public class PlatformGoblinStoreCouponService extends ServiceImpl<GoblinStoreCouponMapper, GoblinStoreCoupon> {
@Autowired
private GoblinStoreCouponMapper goblinStoreCouponMapper;
@Autowired
private GoblinRedisUtils goblinRedisUtils;
@Autowired
private MongoTemplate mongoTemplate;
public int activityStartProcessing(LocalDateTime now) {
LambdaQueryWrapper<GoblinStoreCoupon> queryWrapper = Wrappers.lambdaQuery(GoblinStoreCoupon.class);
queryWrapper.eq(GoblinStoreCoupon::getDelFlg, "0");
queryWrapper.eq(GoblinStoreCoupon::getState, "0");
queryWrapper.le(GoblinStoreCoupon::getStartTime, now);
List<GoblinStoreCoupon> storeCouponList = this.list(queryWrapper);
if (!CollectionUtils.isEmpty(storeCouponList)) {
List<String> storeCouponIdList = storeCouponList.stream().map(GoblinStoreCoupon::getStoreCouponId).collect(Collectors.toList());
LambdaUpdateWrapper<GoblinStoreCoupon> updateWrapper = Wrappers.lambdaUpdate(GoblinStoreCoupon.class);
updateWrapper.eq(GoblinStoreCoupon::getDelFlg, "0");
updateWrapper.eq(GoblinStoreCoupon::getState, "0");
updateWrapper.in(GoblinStoreCoupon::getStoreCouponId, storeCouponIdList);
updateWrapper.set(GoblinStoreCoupon::getState, "1");
updateWrapper.set(GoblinStoreCoupon::getUpdatedAt, now);
updateWrapper.set(GoblinStoreCoupon::getUpdatedBy, "task");
if (this.update(updateWrapper)) {
try {
UpdateResult updateResult = mongoTemplate.getCollection(GoblinStoreCouponBasicVo.class.getSimpleName()).updateMany(
Query.query(Criteria.where("delFlg").is("0").and("state").is("0").and("storeCouponId").in(storeCouponIdList)).getQueryObject(),
Update.update("state", "1").set("updatedAt", now).set("updatedBy", "task").getUpdateObject()
);
if (updateResult.getModifiedCount() <= 0) {
log.warn("###商铺活动:优惠券处理失败:[now={},storeCouponIdList={}]", now, JsonUtils.toJson(storeCouponIdList));
} else {
storeCouponList.forEach(storeCoupon -> {
GoblinStoreMarketDto storeMarketDto = GoblinStoreMarketDto.getNew();
storeMarketDto.setId(storeCoupon.getStoreCouponId());
storeMarketDto.setType(1);
goblinRedisUtils.addStoreMarketDto(storeCoupon.getStoreId(), storeMarketDto);
goblinRedisUtils.del(GoblinRedisConst.STORE_COUPON.concat(storeCoupon.getStoreCouponId()));
});
return storeCouponList.size();
}
} catch (Exception e) {
log.error("###商铺活动:优惠券处理异常[now={},storeCouponList={}]", now, JsonUtils.toJson(storeCouponIdList));
}
}
}
return 0;
}
public int activityStopProcessing(LocalDateTime now) {
LambdaQueryWrapper<GoblinStoreCoupon> queryWrapper = Wrappers.lambdaQuery(GoblinStoreCoupon.class);
queryWrapper.eq(GoblinStoreCoupon::getDelFlg, "0");
queryWrapper.eq(GoblinStoreCoupon::getState, "1");
queryWrapper.le(GoblinStoreCoupon::getEndTime, now);
List<GoblinStoreCoupon> storeCouponList = this.list(queryWrapper);
if (!CollectionUtils.isEmpty(storeCouponList)) {
List<String> storeCouponIdList = storeCouponList.stream().map(GoblinStoreCoupon::getStoreCouponId).collect(Collectors.toList());
LambdaUpdateWrapper<GoblinStoreCoupon> updateWrapper = Wrappers.lambdaUpdate(GoblinStoreCoupon.class);
updateWrapper.eq(GoblinStoreCoupon::getDelFlg, "0");
updateWrapper.eq(GoblinStoreCoupon::getState, "1");
updateWrapper.in(GoblinStoreCoupon::getStoreCouponId, storeCouponIdList);
updateWrapper.set(GoblinStoreCoupon::getState, "2");
updateWrapper.set(GoblinStoreCoupon::getUpdatedAt, now);
updateWrapper.set(GoblinStoreCoupon::getUpdatedBy, "task");
if (this.update(updateWrapper)) {
try {
UpdateResult updateResult = mongoTemplate.getCollection(GoblinStoreCouponBasicVo.class.getSimpleName()).updateMany(
Query.query(Criteria.where("delFlg").is("0").and("state").is("1").and("storeCouponId").in(storeCouponIdList)).getQueryObject(),
Update.update("state", "2").set("updatedAt", now).set("updatedBy", "task").getUpdateObject()
);
if (updateResult.getModifiedCount() <= 0) {
log.warn("###商铺活动:优惠券处理失败:[now={},storeCouponIdList={}]", now, JsonUtils.toJson(storeCouponIdList));
} else {
storeCouponList.forEach(storeCoupon -> {
GoblinStoreMarketDto storeMarketDto = GoblinStoreMarketDto.getNew();
storeMarketDto.setId(storeCoupon.getStoreCouponId());
storeMarketDto.setType(1);
goblinRedisUtils.delStoreMarketDto(storeCoupon.getStoreId(), storeMarketDto);
goblinRedisUtils.del(GoblinRedisConst.STORE_COUPON.concat(storeCoupon.getStoreCouponId()));
});
return storeCouponList.size();
}
} catch (Exception e) {
log.error("###商铺活动:优惠券处理异常[now={},storeCouponList={}]", now, JsonUtils.toJson(storeCouponIdList));
}
}
}
return 0;
}
}
package com.liquidnet.service.platform.utils;
import com.fasterxml.jackson.core.type.TypeReference;
import com.liquidnet.common.cache.redis.util.AbstractRedisUtil;
import com.liquidnet.common.cache.redis.util.RedisDataSourceUtil;
import com.liquidnet.commons.lang.util.CollectionUtil;
import com.liquidnet.commons.lang.util.JsonUtils;
import com.liquidnet.service.goblin.constant.GoblinRedisConst;
import com.liquidnet.service.goblin.dto.GoblinStoreMarketDto;
import com.liquidnet.service.goblin.dto.vo.GoblinOrderSkuVo;
import com.liquidnet.service.goblin.dto.vo.GoblinStoreOrderVo;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Component;
import org.springframework.util.CollectionUtils;
import org.springframework.util.StringUtils;
import java.util.List;
......@@ -21,6 +26,10 @@ public class GoblinRedisUtils {
return redisDataSourceUtil.getRedisGoblinUtil();
}
public void del(String... keys) {
getRedis().del(keys);
}
// 获取 订单相关vo
public GoblinStoreOrderVo getGoblinOrder(String orderId) {
String redisKey = GoblinRedisConst.REDIS_GOBLIN_ORDER.concat(orderId);
......@@ -111,4 +120,42 @@ public class GoblinRedisUtils {
String redisKey = GoblinRedisConst.REDIS_GOBLIN_ORDER_SKU.concat(orderSkuId);
getRedis().set(redisKey, vo);
}
/* ---------------------------------------- 商铺活动 ---------------------------------------- */
public List<GoblinStoreMarketDto> getStoreMarketDtos(String storeId) {
String rk = GoblinRedisConst.STORE_MARKETS.concat(storeId);
String valStr = (String) getRedis().get(rk);
List<GoblinStoreMarketDto> dtos = null;
if (StringUtils.isEmpty(valStr)) {
// TODO: 2022/2/17 zhanggb mongodb 查取该商品所有参与的活动并整理集合
// dtos = ObjectUtil.getGoblinGoodsMarketDtoArrayList();
} else {
dtos = JsonUtils.fromJson(valStr, new TypeReference<List<GoblinStoreMarketDto>>() {
});
}
return dtos;
}
public boolean addStoreMarketDto(String storeId, GoblinStoreMarketDto dto) {
List<GoblinStoreMarketDto> dtos = this.getStoreMarketDtos(storeId);
dtos.add(dto);
return getRedis().set(GoblinRedisConst.STORE_MARKETS.concat(storeId), JsonUtils.toJson(dtos));
}
public void delStoreMarketDto(String storeId, GoblinStoreMarketDto dto) {
String rk = GoblinRedisConst.STORE_MARKETS.concat(storeId);
List<GoblinStoreMarketDto> dtos = this.getStoreMarketDtos(storeId);
if (!CollectionUtils.isEmpty(dtos)) {
int beforeSize = dtos.size();
dtos.removeIf(r -> r.getId().equals(dto.getId()) && r.getType().equals(dto.getType()));
if (beforeSize > dtos.size()) {
getRedis().set(GoblinRedisConst.STORE_MARKETS.concat(storeId), JsonUtils.toJson(dtos));
}
}
}
/* ---------------------------------------- ---------------------------------------- */
/* ---------------------------------------- ---------------------------------------- */
/* ---------------------------------------- ---------------------------------------- */
}
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