提交 68ac25f4 编写于 作者: H HappyChan

完成award领域开发

上级 c2801294
......@@ -100,4 +100,90 @@ public class Constants {
}
}
public enum AwardState{
/**
* 等待发奖
*/
WAIT(0, "等待发奖"),
/**
* 发奖成功
*/
SUCCESS(1, "发奖成功"),
/**
* 发奖失败
*/
FAILURE(2, "发奖失败");
private Integer code;
private String info;
AwardState(Integer code, String info) {
this.code = code;
this.info = info;
}
public Integer getCode() {
return code;
}
public void setCode(Integer code) {
this.code = code;
}
public String getInfo() {
return info;
}
public void setInfo(String info) {
this.info = info;
}
}
public enum AwardType{
/**
* 文字描述
*/
DESC(1, "文字描述"),
/**
* 兑换码
*/
RedeemCodeGoods(2, "兑换码"),
/**
* 优惠券
*/
CouponGoods(3, "优惠券"),
/**
* 实物奖品
*/
PhysicalGoods(4, "实物奖品");
private Integer code;
private String info;
AwardType(Integer code, String info) {
this.code = code;
this.info = info;
}
public Integer getCode() {
return code;
}
public void setCode(Integer code) {
this.code = code;
}
public String getInfo() {
return info;
}
public void setInfo(String info) {
this.info = info;
}
}
}
package cn.happy.lottery.domain.award.model.req;
import cn.happy.lottery.domain.award.model.vo.ShippingAddress;
/**
* @author Happy
* @description:
* @date 2022/2/17
*/
public class GoodsReq {
/** 用户ID */
private String uId;
/** 抽奖单号 ID */
private String orderId;
/** 奖品ID */
private String awardId;
/**
* 奖品名称
*/
private String awardName;
/**
* 奖品内容「描述、奖品码、sku」
*/
private String awardContent;
/** 四级送货地址(只有实物类商品需要地址) */
private ShippingAddress shippingAddress;
public GoodsReq(String uId, String orderId, String awardId, String awardName, String awardContent) {
this.uId = uId;
this.orderId = orderId;
this.awardId = awardId;
this.awardName = awardName;
this.awardContent = awardContent;
}
public GoodsReq(String uId, String orderId, String awardId, String awardName, String awardContent, ShippingAddress shippingAddress) {
this.uId = uId;
this.orderId = orderId;
this.awardId = awardId;
this.awardName = awardName;
this.awardContent = awardContent;
this.shippingAddress = shippingAddress;
}
public String getuId() {
return uId;
}
public void setuId(String uId) {
this.uId = uId;
}
public String getOrderId() {
return orderId;
}
public void setOrderId(String orderId) {
this.orderId = orderId;
}
public String getAwardId() {
return awardId;
}
public void setAwardId(String awardId) {
this.awardId = awardId;
}
public String getAwardName() {
return awardName;
}
public void setAwardName(String awardName) {
this.awardName = awardName;
}
public String getAwardContent() {
return awardContent;
}
public void setAwardContent(String awardContent) {
this.awardContent = awardContent;
}
public ShippingAddress getShippingAddress() {
return shippingAddress;
}
public void setShippingAddress(ShippingAddress shippingAddress) {
this.shippingAddress = shippingAddress;
}
}
package cn.happy.lottery.domain.award.model.res;
/**
* @author Happy
* @description:
* @date 2022/2/17
*/
public class DistributionRes {
/**
* 用户ID
*/
private String uId;
/**
* 编码
*/
private Integer code;
/**
* 描述
*/
private String info;
/**
* 结算单ID,如:发券后有券码、发货后有单号等,用于存根查询
*/
private String statementId;
public DistributionRes() {
}
public DistributionRes(String uId, Integer code, String info) {
this.uId = uId;
this.code = code;
this.info = info;
}
public DistributionRes(String uId, Integer code, String info, String statementId) {
this.uId = uId;
this.code = code;
this.info = info;
this.statementId = statementId;
}
public String getuId() {
return uId;
}
public void setuId(String uId) {
this.uId = uId;
}
public Integer getCode() {
return code;
}
public void setCode(Integer code) {
this.code = code;
}
public String getInfo() {
return info;
}
public void setInfo(String info) {
this.info = info;
}
public String getStatementId() {
return statementId;
}
public void setStatementId(String statementId) {
this.statementId = statementId;
}
}
package cn.happy.lottery.domain.award.model.vo;
/**
* @author Happy
* @description:
* @date 2022/2/17
*/
public class ShippingAddress {
}
package cn.happy.lottery.domain.award.repository;
/**
* @author Happy
* @description:
* @date 2022/2/17
*/
public interface IAwardRepository {
}
package cn.happy.lottery.domain.award.repository.impl;
import cn.happy.lottery.domain.award.repository.IAwardRepository;
import org.springframework.stereotype.Component;
/**
* @author Happy
* @description:
* @date 2022/2/17
*/
@Component
public class AwardRepository implements IAwardRepository {
// TODO 对分库分表中的用户中奖记录操作
}
package cn.happy.lottery.domain.award.service.factory;
import cn.happy.lottery.domain.award.service.goods.IDistributionGoods;
import org.springframework.stereotype.Component;
import org.springframework.stereotype.Service;
/**
* @author Happy
* @description:
* @date 2022/2/17
*/
@Service
public class DistributionGoodsFactory extends GoodsConfig {
public IDistributionGoods getDistributionGoodsService(Integer awardType) {
return goodsMap.get(awardType);
}
}
package cn.happy.lottery.domain.award.service.factory;
import cn.happy.lottery.common.Constants;
import cn.happy.lottery.domain.award.service.goods.IDistributionGoods;
import cn.happy.lottery.domain.award.service.goods.impl.CouponGoods;
import cn.happy.lottery.domain.award.service.goods.impl.DescGoods;
import cn.happy.lottery.domain.award.service.goods.impl.PhysicalGoods;
import cn.happy.lottery.domain.award.service.goods.impl.RedeemCodeGoods;
import javax.annotation.PostConstruct;
import javax.annotation.Resource;
import java.util.HashMap;
import java.util.Map;
import java.util.concurrent.ConcurrentHashMap;
/**
* @author Happy
* @description:
* @date 2022/2/17
*/
public class GoodsConfig {
/**
* 奖品发放策略组
*/
protected static Map<Integer, IDistributionGoods> goodsMap = new ConcurrentHashMap<>();
@Resource
private DescGoods descGoods;
@Resource
private CouponGoods couponGoods;
@Resource
private RedeemCodeGoods redeemCodeGoods;
@Resource
private PhysicalGoods physicalGoods;
@PostConstruct
public void init() {
goodsMap.put(Constants.AwardType.DESC.getCode(), descGoods);
goodsMap.put(Constants.AwardType.RedeemCodeGoods.getCode(), redeemCodeGoods);
goodsMap.put(Constants.AwardType.CouponGoods.getCode(), couponGoods);
goodsMap.put(Constants.AwardType.PhysicalGoods.getCode(), physicalGoods);
}
}
package cn.happy.lottery.domain.award.service.goods;
import cn.happy.lottery.domain.award.repository.IAwardRepository;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import javax.annotation.Resource;
/**
* @author Happy
* @description:
* @date 2022/2/17
*/
public class DistributionBase {
protected Logger logger = LoggerFactory.getLogger(DistributionBase.class);
@Resource
IAwardRepository awardRepository;
protected void updateUserAwardState(String uId, String orderId, String awardId, Integer awardState, String awardStateInfo) {
// TODO 后期添加更新分库分表中,用户个人的抽奖记录表中奖品发奖状态
logger.info("TODO 后期添加更新分库分表中,用户个人的抽奖记录表中奖品发奖状态 uId:{}", uId);
}
}
package cn.happy.lottery.domain.award.service.goods;
import cn.happy.lottery.domain.award.model.req.GoodsReq;
import cn.happy.lottery.domain.award.model.res.DistributionRes;
/**
* @author Happy
* @description: 奖品配送接口
* @date 2022/2/17
*/
public interface IDistributionGoods {
/**
* 奖品配送接口,奖品类型(1:文字描述、2:兑换码、3:优惠券、4:实物奖品)
*
* @param req 物品信息
* @return 配送结果
*/
DistributionRes doDistribution(GoodsReq req);
}
package cn.happy.lottery.domain.award.service.goods.impl;
import cn.happy.lottery.domain.award.model.req.GoodsReq;
import cn.happy.lottery.domain.award.model.res.DistributionRes;
import cn.happy.lottery.domain.award.service.goods.DistributionBase;
import cn.happy.lottery.domain.award.service.goods.IDistributionGoods;
import cn.happy.lottery.common.Constants;
import org.springframework.stereotype.Component;
/**
* @author Happy
* @description: 优惠券
* @date 2022/2/17
*/
@Component
public class CouponGoods extends DistributionBase implements IDistributionGoods {
@Override
public DistributionRes doDistribution(GoodsReq req) {
// 模拟调用优惠券发放接口
logger.info("模拟优惠券调用发放接口 uId,{} awardContent:{}", req.getuId(), req.getAwardContent());
// 更新用户领奖结果
super.updateUserAwardState(req.getuId(), req.getOrderId(), req.getAwardId(), Constants.AwardState.SUCCESS.getCode(), Constants.AwardState.SUCCESS.getInfo());
return new DistributionRes(req.getuId(), Constants.AwardState.SUCCESS.getCode(), Constants.AwardState.SUCCESS.getInfo());
}
}
package cn.happy.lottery.domain.award.service.goods.impl;
import cn.happy.lottery.common.Constants;
import cn.happy.lottery.domain.award.model.req.GoodsReq;
import cn.happy.lottery.domain.award.model.res.DistributionRes;
import cn.happy.lottery.domain.award.service.goods.DistributionBase;
import cn.happy.lottery.domain.award.service.goods.IDistributionGoods;
import org.springframework.stereotype.Component;
/**
* @description: 描述类商品,以文字形式展示给用户
* @author: 小傅哥,微信:fustack
* @date: 2021/9/4
* @github: https://github.com/fuzhengwei
* @Copyright: 公众号:bugstack虫洞栈 | 博客:https://bugstack.cn - 沉淀、分享、成长,让自己和他人都能有所收获!
*/
@Component
public class DescGoods extends DistributionBase implements IDistributionGoods {
@Override
public DistributionRes doDistribution(GoodsReq req) {
super.updateUserAwardState(req.getuId(), req.getOrderId(), req.getAwardId(), Constants.AwardState.SUCCESS.getCode(), Constants.AwardState.SUCCESS.getInfo());
return new DistributionRes(req.getuId(), Constants.AwardState.SUCCESS.getCode(), Constants.AwardState.SUCCESS.getInfo());
}
}
package cn.happy.lottery.domain.award.service.goods.impl;
import cn.happy.lottery.common.Constants;
import cn.happy.lottery.domain.award.model.req.GoodsReq;
import cn.happy.lottery.domain.award.model.res.DistributionRes;
import cn.happy.lottery.domain.award.service.goods.DistributionBase;
import cn.happy.lottery.domain.award.service.goods.IDistributionGoods;
import org.springframework.stereotype.Component;
/**
* @description: 实物发货类商品
* @author: 小傅哥,微信:fustack
* @date: 2021/9/4
* @github: https://github.com/fuzhengwei
* @Copyright: 公众号:bugstack虫洞栈 | 博客:https://bugstack.cn - 沉淀、分享、成长,让自己和他人都能有所收获!
*/
@Component
public class PhysicalGoods extends DistributionBase implements IDistributionGoods {
@Override
public DistributionRes doDistribution(GoodsReq req) {
// 模拟调用实物发奖
logger.info("模拟调用实物发奖 uId:{} awardContent:{}", req.getuId(), req.getAwardContent());
// 更新用户领奖结果
super.updateUserAwardState(req.getuId(), req.getOrderId(), req.getAwardId(), Constants.AwardState.SUCCESS.getCode(), Constants.AwardState.SUCCESS.getInfo());
return new DistributionRes(req.getuId(), Constants.AwardState.SUCCESS.getCode(), Constants.AwardState.SUCCESS.getInfo());
}
}
package cn.happy.lottery.domain.award.service.goods.impl;
import cn.happy.lottery.common.Constants;
import cn.happy.lottery.domain.award.model.req.GoodsReq;
import cn.happy.lottery.domain.award.model.res.DistributionRes;
import cn.happy.lottery.domain.award.service.goods.DistributionBase;
import cn.happy.lottery.domain.award.service.goods.IDistributionGoods;
import org.springframework.stereotype.Component;
/**
* @description: 兑换码类商品
* @author: 小傅哥,微信:fustack
* @date: 2021/9/4
* @github: https://github.com/fuzhengwei
* @Copyright: 公众号:bugstack虫洞栈 | 博客:https://bugstack.cn - 沉淀、分享、成长,让自己和他人都能有所收获!
*/
@Component
public class RedeemCodeGoods extends DistributionBase implements IDistributionGoods {
@Override
public DistributionRes doDistribution(GoodsReq req) {
// 模拟调用兑换码
logger.info("模拟调用兑换码 uId:{} awardContent:{}", req.getuId(), req.getAwardContent());
// 更新用户领奖结果
super.updateUserAwardState(req.getuId(), req.getOrderId(), req.getAwardId(), Constants.AwardState.SUCCESS.getCode(), Constants.AwardState.SUCCESS.getInfo());
return new DistributionRes(req.getuId(), Constants.AwardState.SUCCESS.getCode(), Constants.AwardState.SUCCESS.getInfo());
}
}
package cn.happy.lottery.domain.strategy.model.vo;
/**
* @author Happy
* @description: 中奖奖品信息
* @date 2022/2/17
* @author:小傅哥,微信:fustack
* @date: 2021/8/28
* @Copyright: 公众号:bugstack虫洞栈 | 博客:https://bugstack.cn - 沉淀、分享、成长,让自己和他人都能有所收获!
*/
public class DrawAwardInfo {
/**
* 奖品ID
*/
private String rewardId;
private String awardId;
/**
* 奖品类型(1:文字描述、2:兑换码、3:优惠券、4:实物奖品)
*/
private Integer awardType;
/**
* 奖品名称
*/
private String awardName;
public DrawAwardInfo(String rewardId, String awardName) {
this.rewardId = rewardId;
/**
* 奖品内容「描述、奖品码、sku」
*/
private String awardContent;
public DrawAwardInfo() {
}
public DrawAwardInfo(String awardId, Integer awardType, String awardName, String awardContent) {
this.awardId = awardId;
this.awardType = awardType;
this.awardName = awardName;
this.awardContent = awardContent;
}
public String getRewardId() {
return rewardId;
public String getAwardId() {
return awardId;
}
public void setRewardId(String rewardId) {
this.rewardId = rewardId;
public void setAwardId(String awardId) {
this.awardId = awardId;
}
public Integer getAwardType() {
return awardType;
}
public void setAwardType(Integer awardType) {
this.awardType = awardType;
}
public String getAwardName() {
......@@ -37,4 +61,12 @@ public class DrawAwardInfo {
public void setAwardName(String awardName) {
this.awardName = awardName;
}
public String getAwardContent() {
return awardContent;
}
public void setAwardContent(String awardContent) {
this.awardContent = awardContent;
}
}
......@@ -84,7 +84,7 @@ public abstract class AbstractDrawBase extends DrawStrategySupport implements ID
// 中奖
Award award = super.queryAwardInfoByAwardId(awardId);
DrawAwardInfo drawAwardInfo = new DrawAwardInfo(award.getAwardId(), award.getAwardName());
DrawAwardInfo drawAwardInfo = new DrawAwardInfo(award.getAwardId(), award.getAwardType(), award.getAwardName(), award.getAwardContent());
logger.info("执行策略抽奖完成【已中奖】,用户:{} 策略ID:{} 奖品ID:{} 奖品名称:{}", uid, strategyId, award.getAwardId(), award.getAwardName());
return new DrawResult(uid, strategyId, Constants.DrawState.SUCCESS.getCode(), drawAwardInfo);
}
......
......@@ -24,6 +24,12 @@ public class StrategyDetail {
*/
private String awardId;
/**
* 奖品名称
*/
private String awardName;
/**
* 奖品数量
*/
......@@ -73,6 +79,14 @@ public class StrategyDetail {
this.awardId = awardId;
}
public String getAwardName() {
return awardName;
}
public void setAwardName(String awardName) {
this.awardName = awardName;
}
public String getAwardCount() {
return awardCount;
}
......
package cn.happy.lottery.test;
import cn.happy.lottery.common.Constants;
import cn.happy.lottery.domain.award.model.req.GoodsReq;
import cn.happy.lottery.domain.award.model.res.DistributionRes;
import cn.happy.lottery.domain.award.service.goods.IDistributionGoods;
import cn.happy.lottery.domain.strategy.model.req.DrawReq;
import cn.happy.lottery.domain.strategy.model.res.DrawResult;
import cn.happy.lottery.domain.strategy.model.vo.DrawAwardInfo;
import cn.happy.lottery.domain.strategy.service.draw.IDrawExec;
import cn.happy.lottery.infrastructure.dao.IActivityDao;
import cn.happy.lottery.infrastructure.dao.IAwardDao;
......@@ -21,6 +27,7 @@ import org.springframework.test.context.junit4.SpringRunner;
import javax.annotation.Resource;
import java.util.Date;
import java.util.List;
import cn.happy.lottery.domain.award.service.factory.DistributionGoodsFactory;
/**
* @author Happy
......@@ -34,19 +41,22 @@ public class SpringRunnerTest {
private Logger logger = LoggerFactory.getLogger(SpringRunnerTest.class);
@Resource
IActivityDao activityDao;
private IActivityDao activityDao;
@Resource
IAwardDao awardDao;
private IAwardDao awardDao;
@Resource
IStrategyDao strategyDao;
private IStrategyDao strategyDao;
@Resource
IStrategyDetailDao strategyDetailDao;
private IStrategyDetailDao strategyDetailDao;
@Resource
IDrawExec drawExec;
private IDrawExec drawExec;
@Resource
private DistributionGoodsFactory distributionGoodsFactory;
@Test
public void test_drawExec() {
......@@ -56,6 +66,28 @@ public class SpringRunnerTest {
drawExec.doDrawExec(new DrawReq("八杯水", 10001L));
}
@Test
public void test_award() {
// 执行抽奖
DrawResult drawResult = drawExec.doDrawExec(new DrawReq("happy", 10001L));
// 判断抽奖结果
if (Constants.DrawState.FAIL.getCode().equals(drawResult.getDrawState())) {
logger.info("未中奖 DrawAwardInfo is null");
return;
}
// 封装发奖参数,orderId:2109313442431 为模拟ID,需要在用户参与领奖活动时生成
DrawAwardInfo drawAwardInfo = drawResult.getDrawAwardInfo();
GoodsReq goodsReq = new GoodsReq(drawResult.getuId(), "2109313442431", drawAwardInfo.getAwardId(), drawAwardInfo.getAwardName(), drawAwardInfo.getAwardContent());
// 根据 awardType 从抽奖工厂中获取对应的发奖服务
IDistributionGoods distributionGoodsService = distributionGoodsFactory.getDistributionGoodsService(drawAwardInfo.getAwardType());
DistributionRes distributionRes = distributionGoodsService.doDistribution(goodsReq);
logger.info("测试结果:{}", JSON.toJSONString(distributionRes));
}
@Test
public void test_insert() {
Activity activity = new Activity();
......
Markdown is supported
0% .
You are about to add 0 people to the discussion. Proceed with caution.
先完成此消息的编辑!
想要评论请 注册