package edu.whut.trigger.http;
import com.alibaba.fastjson.JSON;
import edu.whut.api.IMarketTradeService;
import edu.whut.api.dto.LockMarketPayOrderRequestDTO;
import edu.whut.api.dto.LockMarketPayOrderResponseDTO;
import edu.whut.api.response.Response;
import lombok.RequiredArgsConstructor;
import lombok.extern.slf4j.Slf4j;
import org.apache.commons.lang3.RandomStringUtils;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.RestController;

@Slf4j
@RestController
@RequiredArgsConstructor
@RequestMapping("/api/v1/gbm/test/")
public class MarketTestController {

    private final IMarketTradeService marketTradeService;

    /**
     * 一键生成测试拼团订单
     * @param prefix 用户名前缀（比如 "smile2"），默认 "testUser"
     * @param count 模拟人数，默认 3
     */
    @GetMapping("market/lockOrders")
    public String generateTestOrders(
            @RequestParam(defaultValue = "testUser") String prefix,
            @RequestParam(defaultValue = "3") int count
    ) {
        String teamId = null;
        StringBuilder sb = new StringBuilder();

        for (int i = 1; i <= count; i++) {
            LockMarketPayOrderRequestDTO req = new LockMarketPayOrderRequestDTO();
            req.setUserId(prefix + i);   // 这里拼接参数前缀
            req.setTeamId(teamId);
            req.setActivityId(100123L);
            req.setGoodsId("9890001");
            req.setSource("s01");
            req.setChannel("c01");
            req.setNotifyMQ();
            req.setOutTradeNo(RandomStringUtils.randomNumeric(12));

            Response<LockMarketPayOrderResponseDTO> res = marketTradeService.lockMarketPayOrder(req);

            if (res.getData() != null) {
                teamId = res.getData().getTeamId(); // 组团ID沿用
            }

            log.info("第{}笔，测试结果 req:{} res:{}",
                    i, JSON.toJSONString(req), JSON.toJSONString(res));

            sb.append("第").append(i).append("笔订单：\n")
                    .append("请求：").append(JSON.toJSONString(req)).append("\n")
                    .append("响应：").append(JSON.toJSONString(res)).append("\n\n");
        }

        return sb.toString();
    }
}
