package com.qs.mp.service;

import com.qs.mp.MpApplication;
import com.qs.mp.admin.domain.TicketAwardsPrize;
import com.qs.mp.admin.service.ITicketBoxSerialService;
import com.qs.mp.common.utils.DateUtils;
import com.qs.mp.common.utils.LogUtil;
import com.qs.mp.common.utils.http.MpHttpRequestUtils;
import com.qs.mp.framework.service.IWxUrlLinkService;
import com.qs.mp.quartz.task.DayStatTask;
import com.qs.mp.quartz.task.TicketBoxTask;
import com.qs.mp.quartz.task.TicketPkgDataTask;
import java.io.IOException;
import java.util.ArrayList;
import java.util.Date;
import java.util.List;
import org.junit.jupiter.api.Test;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.boot.test.autoconfigure.web.servlet.AutoConfigureMockMvc;
import org.springframework.boot.test.context.SpringBootTest;
import org.springframework.test.context.ActiveProfiles;
import org.springframework.test.context.TestPropertySource;

/**
 * @author zhongcp
 * @Date 2021/8/31
 */
@AutoConfigureMockMvc
@SpringBootTest(webEnvironment = SpringBootTest.WebEnvironment.RANDOM_PORT, classes = MpApplication.class)
@TestPropertySource(locations = "classpath:application-dev.yml")
@ActiveProfiles("dev")
public class ServiceImplTest {

  protected final Logger logger = LoggerFactory.getLogger(this.getClass().getSimpleName());

  @Autowired
  private IWxUrlLinkService wxUrlLinkService;

  @Autowired
  private ITicketBoxSerialService ticketBoxSerialService;

  @Autowired
  private TicketBoxTask ticketBoxTask;

  @Autowired
  private DayStatTask dayStatTask;

  @Autowired
  private TicketPkgDataTask ticketPkgDataTask;

  @Test
  public void testGenerateQrCode() {
    System.out.println(wxUrlLinkService.generateCode("pages/index/index", "id=1", "wxc3a684f1196f6e15"));

  }

  @Test
  public void testGenerateUnlimitCode() {
//    System.out.println(wxUrlLinkService.generateUnlimitCode("pages/ticketBox/detail", "boxId=1&suid=2&type=user", "wx8533800e393dbd6b"));
    System.out.println(wxUrlLinkService.generateUnlimitCode("", "2325&2", "wx8533800e393dbd6b"));
  }

  @Test
  public void testGenerateShortUrl() {
    System.out.println(wxUrlLinkService.generate("", "", "wxc3a684f1196f6e15"));

  }

  @Test
  public void testGenerateUrlSchema() {
//    System.out.println(wxUrlLinkService.generateUrlSchema("/pages/login/invitation", "", "wxc3a684f1196f6e15"));
    System.out.println(wxUrlLinkService.generateUrlSchema("", "2325&2", "wx8533800e393dbd6b"));
  }

  @Test
  public void testGenerateSerial() {
    System.out.println(ticketBoxSerialService.generateSerial(null));

  }


  @Test
  public void testCheckTicketBox() {
    ticketBoxTask.checkPrize("954112676047486977");

  }

  @Test
  public void testDayStat() {
    dayStatTask.stat(null);
  }

  @Test
  public void testPkgDataRefresh() {
    ticketPkgDataTask.refresh();
  }


  @Test
  public void testGetAccessToken() {
    String appId = "wx8533800e393dbd6b";
    String params = "{\"appId\":\"" + appId + "\"}";
    String url = "https://test-mp.quanshu123.com/api/v1/mp/wx/getAccessToken?";
    String res = MpHttpRequestUtils.request(url, params);
    LogUtil.info(logger, "获取accessToken结果：" + res);
  }

  public static void main(String[] args) {
    List<TicketAwardsPrize> newTicketAwardsPrizes = new ArrayList<>();
    TicketAwardsPrize prize1 = new TicketAwardsPrize();
    prize1.setRemainQty(1);
    newTicketAwardsPrizes.add(prize1);
    TicketAwardsPrize prize2 = new TicketAwardsPrize();
    prize2.setRemainQty(1);
    newTicketAwardsPrizes.add(prize2);
    TicketAwardsPrize prize3 = new TicketAwardsPrize();
    prize3.setRemainQty(1);
    newTicketAwardsPrizes.add(prize3);
    TicketAwardsPrize prize4 = new TicketAwardsPrize();
    prize4.setRemainQty(1);
    newTicketAwardsPrizes.add(prize4);
    TicketAwardsPrize prize5 = new TicketAwardsPrize();
    prize5.setRemainQty(2000);
    newTicketAwardsPrizes.add(prize5);
    for (int i=0; i<500; i++) {
      System.out.println(getRandom(newTicketAwardsPrizes));
      i++;
    }

    /*String appId = "wx8533800e393dbd6b";
    String params = "{\"appId\":\"" + appId + "\"}";
    String url = "https://test-mp.quanshu123.com/api/v1/mp/wx/getAccessToken?";
    String res = MpHttpRequestUtils.request(url, params);
    System.out.println("获取accessToken结果：" + res);*/
    /*int n = 500;
    Random rand = new Random();
    boolean[] bool = new boolean[n];
    int randInt = 0;
    for(int i = 0; i < 50 ; i++) {
      do {
        randInt = rand.nextInt(n);
      }while(bool[randInt]);
      bool[randInt] = true;
      System.out.println(randInt);
    }
  */
    /*String s = "z";

    int i = Character.codePointAt(s, 0);

    System.out.println(i + "");

    char[] a = Character.toChars(72);

    System.out.println(new String(a));
    System.out.println(DateUtils.dateTime());
    System.out.println(new Date().getTime());*/
  }

  private static int getRandom(List<TicketAwardsPrize> newTicketAwardsPrizes) {
    // 开奖
    int random = -1;
    // 计算总权重
    double sumWeight = 0;
    for (TicketAwardsPrize ticketAwardsPrize : newTicketAwardsPrizes) {
      sumWeight += ticketAwardsPrize.getRemainQty();
    }

    //产生随机数
    double randomNumber;
    randomNumber = Math.random();
    System.out.println("random:" + randomNumber);
    //根据随机数在所有奖品分布的区域并确定所抽奖品
    double d1 = 0;
    double d2 = 0;
    for (int i = 0; i < newTicketAwardsPrizes.size(); i++) {
      d2 += Double.parseDouble(String.valueOf(newTicketAwardsPrizes.get(i).getRemainQty())) / sumWeight;
      if (i == 0) {
        d1 = 0;
      } else {
        d1 += Double.parseDouble(String.valueOf(newTicketAwardsPrizes.get(i - 1).getRemainQty())) / sumWeight;
      }
      System.out.println("d1:" + d1 + ",d2:" +d2);
      if (randomNumber >= d1 && randomNumber < d2) {
        random = i;
        break;
      }
    }
    return random;
  }

  }
