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.MyEntity;
import com.cesgroup.homwork.mybatis.sample.mapper.MyEntityMapper;
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 MyEntityMapper myEntityMapper;
    @Autowired
    private BatchService batchService;
    @Test
    void contextLoads() {
        MyEntity myEntity = new MyEntity();
        Map<String, Object> context = new HashMap<>();
        myEntity.put("a","aaa");
        myEntity.put("b","bb");
        myEntity.put("c","c");
        myEntity.put("d","dddddddddd");
        myEntity.setId(IdUtil.simpleUUID())
                .setName(RandomUtil.randomString(4))
                .setContext(context);
        myEntityMapper.insert(myEntity);
    }
    
    @Test
    void select() {
        MyEntity byId = myEntityMapper.findById("857b193a846a4246aba221jsf937191");
        Console.log(byId);
        Console.log(JSONUtil.toJsonStr(byId));
    }
    @Test
    void batch() {
        List<MyEntity> myEntityList = new ArrayList<City>();
        for (int i = 0; i < 10000; i++) {
            MyEntity myEntity = new MyEntity();
            myEntity.setName(RandomUtil.randomString("测试测试测试测试测试",4));
            myEntityList.add(myEntity);
        }
        batchService.batchInsertCity(myEntityList);

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