表模型设计
- 营销活动表
- 抽奖规则表
- 奖品表
- 中奖记录表
- 抽奖资格表
- 条件商品表
ISSUSES
具体实现案例
@Data
public class LotteryPrivilegeChain {
private List<LotteryPrivilegeHandler> handlers = Lists.newArrayList();
public boolean process(LotteryPrivilegeContext context) {
for (LotteryPrivilegeHandler handler : handlers) {
boolean flag = handler.handle(context);
if (!flag) {
return false;
}
}
return true;
}
public void addHandlers(List<LotteryPrivilegeHandler> handlers) {
this.handlers = handlers;
}
public void addHandler(LotteryPrivilegeHandler handler) {
this.getHandlers().add(handler);
}
}
@FunctionalInterface
public interface LotteryPrivilegeHandler {
boolean handle(LotteryPrivilegeContext context);
}
@Getter
@Setter
public class LotteryPrivilegeContext {
private Integer userId;
private Integer channel;
}
@Autowired
private List<LotteryPrivilegeHandler> lotteryPrivilegeHandlers;
LotteryPrivilegeChain chain = new LotteryPrivilegeChain();
chain.addHandlers(lotteryPrivilegeHandlers);
LotteryPrivilegeContext lotteryPrivilegeContext = LotteryPrivilegeContext.create()
.withUserId(userId)
.withChannel(request.getChannel());
boolean flag = chain.process(lotteryPrivilegeContext);
@FunctionalInterface
public interface LotteryAcceptService {
LotteryAcceptContext accept(LotteryAcceptContext context);
}
@Slf4j
@Component
public class DefaultLotteryAcceptServiceImpl implements LotteryAcceptService {
@Autowired
private List<LotteryShipHandler> lotteryShipHandlers;
@Override
@Transactional(rollbackFor = Throwable.class)
public LotteryAcceptContext accept(LotteryAcceptContext context) {
check(context);
beforeHandler(context);
process(context);
afterHandler(context);
return context;
}
void process(LotteryAcceptContext context) {
LotteryShipChain chain = new LotteryShipChain();
chain.addHandlers(lotteryShipHandlers);
boolean flag = chain.process(context);
context.setSuccess(flag);
if (!flag) {
context.setFailCode(context.getFailCode());
context.setFailMap(context.getFailMap());
}
}