package com.db.service.main;

import com.db.service.ITestService;
import com.db.service.dao.ITestDao;
import com.db.service.impl.TestServiceImpl;
import com.db.service.model.TTest;
import com.elab.core.componts.ConcurrentTool;
import com.elab.core.utils.RandomUtils;
import org.databene.contiperf.PerfTest;
import org.databene.contiperf.Required;
import org.databene.contiperf.junit.ContiPerfRule;
import org.junit.Rule;
import org.junit.Test;
import org.junit.runner.RunWith;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.jdbc.core.JdbcTemplate;
import org.springframework.test.context.ContextConfiguration;
import org.springframework.test.context.junit4.SpringJUnit4ClassRunner;
import org.springframework.transaction.annotation.EnableTransactionManagement;

import java.util.Arrays;
import java.util.Date;
import java.util.List;

/**
 * 公共service层公共服务测试用例
 *
 * @author : liukx
 * @create : 2018/5/17 13:47
 * @email : liukx@elab-plus.com
 */
@RunWith(SpringJUnit4ClassRunner.class)  //使用junit4进行测试
@ContextConfiguration({"classpath:applicationContext-*.xml",})
@EnableTransactionManagement
public class BasicServiceCase {
    private Logger logger = LoggerFactory.getLogger(getClass());
    @Autowired
    private ITestService testService;

    @Autowired
    private ITestDao testDao;

    @Autowired
    private JdbcTemplate jdbcTemplate;

    @Rule
    public ContiPerfRule rule = new ContiPerfRule();

    @Test
    public void testDeadLockUpdate() throws Exception {
        ConcurrentTool concurrentTool = new ConcurrentTool(10);
        updateTestById();
        int index = 0;
        concurrentTool.process(() -> {
            updateTestById();
            return null;
        });

        //        concurrentTool.process(() -> {
        //            updateByExample1();
        //            return null;
        //        });
        System.out.println("处理结束");
        System.in.read();
    }

    private void updateTestById() {
        TTest test = new TTest();
        test.setLoveName("bbb" + RandomUtils.randomNum(5));
        test.setCreated(null);
        test.setId(1);
        try {
            testService.updateById(test);
        } catch (Exception e) {
            logger.error("修改异常", e);
        }
    }

    private void updateByExample1() {
        TTest test = new TTest();
        test.setLoveName("bbb" + RandomUtils.randomNum(5));
        test.setName("bbb26175");
        try {
            testService.updateByExample(test);
        } catch (Exception e) {
            logger.error("修改异常", e);
        }
    }

    @Test
//    @PerfTest(invocations = 10000, threads = 10)
//    @Required(throughput = 1000, max = 1000)
    public void insert() throws Exception {
        TTest test = new TTest();
        test.setName("某某某");
        test.setSex("男");
        test.setStatus("1");
        test.setUsername("测试的啊");
        int insert = testService.insert(test);
        //        String sql = "insert into t_test (username, name, sex, status, created, time, test_id, love_name)\n" +
        //                "        values ('某某某', '测试的啊', '男', '1', null, null, null, null)";
        //        jdbcTemplate.update(sql);
    }

    @Test
    public void findById() throws Exception {
        TTest test = testService.selectById("10");
        System.out.println(test);
    }

    @Test
    public void updateByExample() throws Exception {
        TTest test = new TTest();
        test.setName("修改某某某");
        test.setSex("男");
        test.setStatus("1");
        test.setUsername("测试的啊");
        test.setId(10);
        test.setTime(null);
        int result = testService.updateById(test);
//        int[] ints = testDao.batchUpdateByObject(Arrays.asList(test));
//        System.out.println(Arrays.stream(ints).toArray());
    }

    @Test
    public void findByList() throws Exception {
        TTest test = new TTest();
        //        test.setUsername("test");
        test.setStatus("1");
        List<TTest> tTests = testService.selectByList(test);
        System.out.println(tTests.size());
    }

}
