package com.izouma.jiashanxia.service;

import com.izouma.jiashanxia.domain.OrderInfo;
import com.izouma.jiashanxia.dto.CreateOrder;
import com.izouma.jiashanxia.dto.OrderInfoVO;
import com.izouma.jiashanxia.dto.PageQuery;
import com.izouma.jiashanxia.enums.PayMethod;
import org.junit.Test;
import org.junit.runner.RunWith;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.boot.test.context.SpringBootTest;
import org.springframework.data.domain.Page;
import org.springframework.data.domain.Pageable;
import org.springframework.test.context.junit4.SpringRunner;

import java.math.BigDecimal;
import java.util.*;

@SpringBootTest
@RunWith(SpringRunner.class)
public class OrderInfoServiceTest {

    @Autowired
    private OrderInfoService orderInfoService;

    @Test
    public void order() {
        System.out.println(orderInfoService.createOrder(916L, 17L, PayMethod.WEIXIN));
    }

    @Test
    public void completed() {
        orderInfoService.completed1(1043L, "11000001", PayMethod.WEIXIN);
    }

    @Test
    public void cancel() {
        System.out.println(orderInfoService.cancelOrder(58L));
    }

    @Test
    public void test() {
        List<OrderInfoVO> contents = orderInfoService.my(Pageable.unpaged(), 916L, null).getContent();
        contents.forEach(content -> System.out.println(content.getCreatedAt()));
    }

    @Test
    public void test1() {
        orderInfoService.distribution(636L, 255L, "1234", BigDecimal.valueOf(600));
    }

    @Test
    public void test2() {
        CreateOrder build = CreateOrder.builder()
                .num(1)
                .packageId(17L)
                .stockId(1009L)
                .payMethod(PayMethod.WEIXIN)
                .build();
        System.out.println(orderInfoService.createOrder1(build, 916L));
    }

    @Test
    public void autoCancel() {
        //开始时间
        long start = System.currentTimeMillis();
        //结束时间
        final long end = start + 60 * 1000;

        final Timer timer = new Timer();
        //延迟0毫秒（即立即执行）开始，每隔1000毫秒执行一次
        timer.schedule(new TimerTask() {
            public void run() {
                System.out.println(end);
            }
        }, 0, 1000);
        //计时结束时候，停止全部timer计时计划任务
        timer.schedule(new TimerTask() {
            public void run() {
                timer.cancel();
                System.out.println("end");
            }

        }, new Date(end));
    }

    @Test
    public void backAll() {
        PageQuery pageQuery = new PageQuery();
        Map<String, Object> query = pageQuery.getQuery();
        query.put("attractionsId", 955L);
        Page<OrderInfo> orderInfos = orderInfoService.backAll(pageQuery);
        orderInfos.getContent().forEach(System.out::println);
    }

    @Test
    public void test3() {
        orderInfoService.batchByCart(Collections.singletonList(1536L));
    }
}