package com.seamew.lottery.test;

import com.alibaba.fastjson2.JSON;
import com.seamew.lottery.common.Constants;
import com.seamew.lottery.domain.award.model.req.GoodsReq;
import com.seamew.lottery.domain.award.model.res.DistributionRes;
import com.seamew.lottery.domain.award.service.factory.DistributionGoodsFactory;
import com.seamew.lottery.domain.award.service.goods.IDistributionGoods;
import com.seamew.lottery.domain.strategy.model.req.DrawReq;
import com.seamew.lottery.domain.strategy.model.res.DrawResult;
import com.seamew.lottery.domain.strategy.model.vo.DrawAwardVO;
import com.seamew.lottery.domain.strategy.service.draw.IDrawExec;
import com.seamew.lottery.rpc.res.DrawRes;
import lombok.extern.slf4j.Slf4j;
import org.junit.Test;
import org.junit.runner.RunWith;
import org.springframework.boot.test.context.SpringBootTest;
import org.springframework.test.context.junit4.SpringRunner;

import javax.annotation.Resource;

/**
 * @Author: seamew
 * @Title: DrawAlgorithmTest
 * @CreateTime: 2023年02月14日 18:52:00
 * @Description:
 * @Version: 1.0
 */
@RunWith(SpringRunner.class)
@SpringBootTest
@Slf4j
public class DrawAlgorithmTest {
    @Resource
    private IDrawExec drawExec;

    @Resource
    private DistributionGoodsFactory distributionGoodsFactory;

    @Test
    public void test_drawExec() {
        drawExec.doDrawExec(new DrawReq("小佳佳", 10001L, "123"));
    }

    @Test
    public void test_award() {
        // 执行抽奖
        DrawResult drawResult = drawExec.doDrawExec(new DrawReq("admin", 10001L, "123"));

        // 判断抽奖结果
        Integer drawState = drawResult.getDrawState();
        if (Constants.DrawState.FAIL.getCode().equals(drawState)) {
            log.info("未中奖 DrawAwardInfo is null");
            return;
        }

        // 封装发奖参数，orderId：2109313442431 为模拟ID，需要在用户参与领奖活动时生成
        DrawAwardVO drawAwardInfo = drawResult.getDrawAwardInfo();
        GoodsReq goodsReq = new GoodsReq(drawResult.getUId(), 2109313442431L, drawAwardInfo.getAwardId(), drawAwardInfo.getAwardName(), drawAwardInfo.getAwardContent());

        // 根据 awardType 从抽奖工厂中获取对应的发奖服务
        IDistributionGoods distributionGoodsService = distributionGoodsFactory.getDistributionGoodsService(drawAwardInfo.getAwardType());
        DistributionRes distributionRes = distributionGoodsService.doDistribution(goodsReq);

        log.info("测试结果：{}", JSON.toJSONString(distributionRes));
    }


}
