package com.yxw.demo;

import com.yxw.zuoye.dao.OrderMapper;
import com.yxw.zuoye.entity.Order;
import com.yxw.zuoye.entity.User;
import lombok.extern.slf4j.Slf4j;
import org.apache.ibatis.session.ExecutorType;
import org.apache.ibatis.session.SqlSession;
import org.junit.jupiter.api.Test;
import org.mybatis.spring.SqlSessionTemplate;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.boot.test.context.SpringBootTest;

import java.util.ArrayList;
import java.util.List;
import java.util.UUID;

@SpringBootTest
@Slf4j
class DemoApplicationTests {

    @Autowired
    private SqlSessionTemplate sqlSessionTemplate;

    @Test
    void contextLoads() {

    }

    @Test
    public void mybatisBatchOne() {
        SqlSession session = sqlSessionTemplate.getSqlSessionFactory().openSession();
        try {
            OrderMapper orderMapper =  session.getMapper(OrderMapper.class);
            long start = System.currentTimeMillis();
            for (int i = 0; i < 10000; i++) {
                Order order = new Order();
                User user = new User();
                user.setUserId("userId");
                user.setUserName("userName");
                order.setOrderId("orderId");
                order.setUserInfo(user);
                orderMapper.insertOrder(order);
            }
            long end = System.currentTimeMillis();
           log.info("耗时:" + (end - start));
        } catch (Exception e) {
           log.error(e.toString());
        } finally {
            session.commit();
            session.close();
        }

    }

    @Test
    public void mybatisBatchTwo() {
        SqlSession session = sqlSessionTemplate.getSqlSessionFactory().openSession(ExecutorType.BATCH);
        try {
            OrderMapper orderMapper =  session.getMapper(OrderMapper.class);
            long start = System.currentTimeMillis();
            for (int i = 0; i < 10000; i++) {
                Order order = new Order();
                User user = new User();
                user.setUserId("userId");
                user.setUserName("userName");
                order.setOrderId("orderId");
                order.setUserInfo(user);
                orderMapper.insertOrder(order);
            }
            long end = System.currentTimeMillis();
            log.info("耗时:" + (end - start));
        } catch (Exception e) {
            log.error(e.toString());
        } finally {
            session.commit();
            session.close();
        }

    }

    @Test
    public void saveOrderBatchOne() {
        SqlSession session = sqlSessionTemplate.getSqlSessionFactory().openSession();
        try {
            OrderMapper orderMapper =  session.getMapper(OrderMapper.class);
            long start = System.currentTimeMillis();
            List<Order> orderList = new ArrayList<Order>();
            for (int i = 0; i < 10000; i++) {
                Order order = new Order();
                User user = new User();
                user.setUserId("userId");
                user.setUserName("userName");
                order.setOrderId("orderId");
                order.setUserInfo(user);
                orderList.add(order);
            }
            orderMapper.insertOrderBatch(orderList);
            orderList.clear();
            long end = System.currentTimeMillis();
            log.info("耗时:" + (end - start));
        } catch (Exception e) {
            log.error(e.toString());
        } finally {
            session.commit();
            session.close();
        }
    }

    @Test
    public void saveOrderBatchTwo() {
        //设置ExecutorType.BATCH原理：把SQL语句发个数据库，数据库预编译好，数据库等待需要运行的参数，接收到参数后一次运行，ExecutorType.BATCH只打印一次SQL语句，多次设置参数步骤，
        SqlSession session = sqlSessionTemplate.getSqlSessionFactory().openSession(ExecutorType.BATCH);
        try {
            OrderMapper orderMapper =  session.getMapper(OrderMapper.class);
            long start = System.currentTimeMillis();
            List<Order> orderList = new ArrayList<Order>();
            for (int i = 0; i < 10000; i++) {
                Order order = new Order();
                User user = new User();
                user.setUserId("userId");
                user.setUserName("userName");
                order.setOrderId("orderId");
                order.setUserInfo(user);
                orderList.add(order);
            }
            orderMapper.insertOrderBatch(orderList);
            orderList.clear();
            long end = System.currentTimeMillis();
            log.info("耗时:" + (end - start));
        } catch (Exception e) {
            log.error(e.toString());
        } finally {
            session.commit();
            session.close();
        }
    }
}
