package com.cesgroup.homwork.mybatis;

import cn.hutool.core.lang.Console;
import cn.hutool.core.util.IdUtil;
import cn.hutool.core.util.RandomUtil;
import cn.hutool.json.JSONUtil;
import com.cesgroup.homwork.mybatis.sample.entity.User;
import com.cesgroup.homwork.mybatis.sample.mapper.UserMapper;
import com.cesgroup.homwork.mybatis.sample.service.BatchService;
import org.junit.jupiter.api.Test;
import org.junit.runner.RunWith;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.boot.test.context.SpringBootTest;
import org.springframework.test.context.junit4.SpringRunner;

import javax.annotation.Resource;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

@RunWith(SpringRunner.class)
@SpringBootTest
class MybatisApplicationTests {

    @Resource
    private UserMapper UserMapper;
    @Autowired
    private BatchService batchService;
    @Test
    void contextLoads() {
        User user = new User();
        Map<String, Object> context = new HashMap<>();
        context.put("a","张三");
        context.put("b","李四");
        context.put("c","哈哈");
        context.put("d","嘻嘻");
        user.setId(IdUtil.simpleUUID())
//                .setId("857b193a846a4246a2087358daa9c9b6")
                .setName(RandomUtil.randomString(4))
                .setContext(context);
        UserMapper.insert(user);
    }
    
    @Test
    void select() {
        User byId = UserMapper.findById("857b193a846a4246a2087358daa9c9b6");
        Console.log(byId);
        Console.log(JSONUtil.toJsonStr(byId));
    }


    @Test
    void batch() {
        List<User> cities = new ArrayList<User>();
        for (int i = 0; i < 10000; i++) {
            User user = new User();
            user.setName(RandomUtil.randomString("哈哈哈哈！",4));
            cities.add(user);
        }
        batchService.batchInsertUser(cities);

        this.noBath(cities);
    }
    void noBath(List<User> cities){
        //批量保存执行前时间
        long start=System.currentTimeMillis();
        for (User user : cities) {
            user.setId(IdUtil.simpleUUID());
            UserMapper.insert(user);
        }
        long end=  System.currentTimeMillis();
        long time2= end-start;
        //批量保存执行后的时间
        Console.log("非批量执行时长"+time2);
    }
}
