package com.db.service.main;

import com.alibaba.fastjson.JSON;
import com.db.service.dao.ITestDao;
import com.db.service.dao.ITestDao2;
import com.db.service.mapper.TestRowMapper;
import com.db.service.model.TTest;
import com.db.service.model.TTest2;
import com.db.service.model.TTestExample;
import com.elab.core.bean.PageModel;
import com.elab.core.dao.model.DataRangeSearch;
import com.elab.core.dao.model.ListDynamicSearch;
import com.elab.core.dao.model.OrderBySearch;
import org.junit.Assert;
import org.junit.Test;
import org.junit.runner.RunWith;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.test.context.ContextConfiguration;
import org.springframework.test.context.junit4.SpringJUnit4ClassRunner;

import java.util.*;

/**
 * 基于拓展的测试用例
 *
 * @author Liukx
 * @create 2018-04-20 15:57
 * @email liukx@elab-plus.com
 **/
@RunWith(SpringJUnit4ClassRunner.class)  //使用junit4进行测试
@ContextConfiguration
        ({"classpath:applicationContext-datasource.xml",})
public class BasicBaseDaoProxyCase {

    @Autowired
    private ITestDao testDao;

    @Autowired
    private ITestDao2 testDao2;

    ////////////////////////////代理测试/////////////////////////////////////////

    @Test
    public void testInsertCase() throws Exception {
        TTest test = new TTest();
        test.setId(1099);
        test.setStatus("1");
        test.setUsername("某某某xx111");
//        test.setTestId("123123");
        test.setSex("Man");
        int insert = testDao.insert(test);
        System.out.println(insert);
        Assert.assertTrue(insert > 0);

    }

    @Test
    public void testUpdateCase() throws Exception {
//        TTest test = new TTest();
//        test.setId(1);
//        test.setStatus("1");
//        test.setLoveName("aaaaa");
//        test.setUsername("某某某xx33333333");
//        int insert = testDao.updateById(test);
//        System.out.println(insert);

        String json = "{\"brandId\":1,\"dispatchOrderStatus\":1,\"mobileTime\":1614061782345,\"source\":1,\"updated\":1614061782345,\"userId\":34958}";
        LinkedHashMap map = JSON.parseObject(json, LinkedHashMap.class);
        map.put("houseId","100");
        int i = testDao.updateUserInfo(map);
//        Assert.assertTrue(insert > 0);
    }

    @Test
    public void testQueryCase() throws Exception {
        TTest test = new TTest();
//        test.setId(1123);
        test.setStatus("1");
//        test.setTestId("21");
//        test.setUsername("某某某xx2121212");
        List<TTest> tTests = testDao.selectByList(test);
        Assert.assertTrue(tTests.size() > 0);

//        test.setId(1);
//        TTest test1 = testDao.selectByObject(test);
//        System.out.println(test1.toString());
//        System.out.println(tTests.size());
    }

    @Test
    public void testSelectByListDynamic() throws Exception {
        TTest test = new TTest();
//        test.setId(1123);
        test.setStatus("1");
//        test.setTestId("21");
//        test.setUsername("某某某xx2121212");
//        List<TTest> tTests = testDao.selectByList(test);
//        ListDynamicSearch search = new ListDynamicSearch();
////        search.setLimit(10);
////        search.setOrderBy("id");
//        InSearch inSearch = new InSearch();
//        inSearch.setFiled("id");
//        inSearch.setDataList(Arrays.asList(1, 2));
//        search.setInSearches(Arrays.asList(inSearch));
//        DataRangeSearch search1 = new DataRangeSearch();
//        search1.setFiled("time");
//        search1.setStartData(new Date());
//        search.setDataRangeSearches(Arrays.asList(search1));

        ListDynamicSearch id_desc = ListDynamicSearch.newBuilder().orderBy("id", OrderBySearch.ASC).build();

        List<TTest> tTests = testDao.selectByList(test, id_desc);


        Assert.assertTrue(tTests.size() > 0);
    }

    @Test
    public void testExampleQueryCase() throws Exception {
        TTest test = new TTest();
//        test.setId(1);
        test.setStatus("1");
//        test.setUsername("某某某xx2121212");
        TTestExample example = new TTestExample();
        TTestExample.Criteria criteria = example.createCriteria();
//        criteria.andIdEqualTo(1);
        List<Integer> list = new ArrayList<>();
        list.add(1);
        list.add(3);
        list.add(4);
        criteria.andIdIn(list);

//        criteria.andUsernameLike("某某某xx333%");

        List<TTest> tTests = testDao2.find(example);
        System.out.println(tTests);
        Assert.assertTrue(tTests.size() > 0);

    }

    @Test
    public void testProxyDao() {
        TTest test = new TTest();
        test.setStatus("1");
        List<TTest2> testList = testDao.getTestList(test);
        System.out.println();
        Assert.assertTrue(testList.size() > 0);
//        LinkedHashMap<String, Object> map = new LinkedHashMap();
//        map.put("id", "1");


//        List<CustomerDTO> customerDTOs = helloDao.getListForObject(map);
//        System.out.println(testList.size() + "\t" + customerDTOs.size());
    }

    @Test
    public void testPageDao() throws Exception {
        TTest test = new TTest();
        test.setStatus("1");

        PageModel pageModel = new PageModel(0, 3, 10);
        pageModel.setOrderby("order by id desc");
        PageModel<TTest> tTestPageModel = testDao.selectByPage(pageModel, test);
        System.out.println(JSON.toJSONString(tTestPageModel));
        Assert.assertNotNull(tTestPageModel);
    }

    @Test
    public void testSelectByIdDao() throws Exception {
        TTest test = testDao.selectById("1");
        System.out.println(test.toString());
        Assert.assertNotNull(test);
    }

    @Test
    public void testSelectByObjectToCount() throws Exception {
        TTest test = new TTest();
        test.setStatus("1");


        Integer count = testDao.selectByObjectToCount(test);

        ListDynamicSearch search = getListDynamicSearch();
        Integer count1 = testDao.selectByObjectToCount(test, search);
        System.out.println(count + "\t" + count1);
        Assert.assertNotNull(count);
        Assert.assertNotNull(count1);
    }

    private ListDynamicSearch getListDynamicSearch() {
        ListDynamicSearch search = new ListDynamicSearch();
        DataRangeSearch dataRangeSearch = new DataRangeSearch();
        dataRangeSearch.setFiled("time");
        dataRangeSearch.setStartData(new Date());
        search.setDataRangeSearches(Arrays.asList(dataRangeSearch));
        return search;
    }

    @Test
    public void testSelectByName() throws Exception {
        TTest bbb = testDao.selectByName("1", "aaa");
        System.out.println(bbb.toString());
        Assert.assertTrue(bbb != null);
    }

    @Test
    public void testListString() throws Exception {
        TTest test = new TTest();
        test.setStatus("1");
        List<String> resultList = testDao.testListString(test);
        Assert.assertTrue(resultList.size() > 0);
    }

    @Test
    public void testSelectByMapper() throws Exception {
        TTest test = new TTest();
        test.setStatus("1");
        TestRowMapper mapper = new TestRowMapper();
        List<TTest> tTests1 = testDao.selectByList(test);
        List<TTest> tTests = testDao.selectByMapper(test, mapper);
        System.out.println(tTests.toString());
        Assert.assertTrue(tTests1.size() > 0);
        Assert.assertTrue(tTests.size() > 0);
    }

    @Test
    public void testSelectByMapper2() throws Exception {
        Map<String, Object> map = new HashMap<>();
        map.put("status", "1");
        TestRowMapper mapper = new TestRowMapper();
        List<TTest> tTests = testDao.getTestList(map, mapper);
        System.out.println(tTests.toString());
        Assert.assertTrue(tTests.size() > 0);

    }

    @Test
    public void testgetTestList2() throws Exception {
        TTest test = new TTest();
        test.setStatus("1");
        List<String> list = new ArrayList<>();
        list.add("1");
        list.add("2");
        list.add("3");
        ListDynamicSearch listDynamicSearch = ListDynamicSearch.newBuilder().addInSearches("id", list).build();
//        ListDynamicSearch listDynamicSearch = ListDynamicSearch.newBuilder().addLikeSearches("username", "b%")
//                .build();
        ListDynamicSearch.newBuilder().addInSearches("id", list)
                .limit(1)
                .addLikeSearches("", "")
                .dataRangeSearches("time", new Date(), new Date())
                .orderBy("id", OrderBySearch.DESC);
        // 普通方法追加动态参数
//        ListDynamicSearch listDynamicSearch = getListDynamicSearch();
        List<TTest> tTests = testDao.getTestList2(test, listDynamicSearch);
        System.out.println(tTests.toString());
        Assert.assertTrue(tTests.size() > 0);

    }

    ///////////////////////////////////// service //////////////////////////////////

    @Test
    public void testBatchBeanInsert() {
        List<TTest> list = new ArrayList<>();
        for (int i = 0; i < 10; i++) {
            TTest test = new TTest();
            test.setStatus("1");
            test.setUsername("batchTest_" + i);
            test.setSex("Man");
            test.setTestId("bbbb");
            test.setTime(new Date());
            test.setCreated(new Date());
            test.setName("batchTest_name_" + i);
            list.add(test);
        }

        int[] ints = testDao.batchInsert(list);
        System.out.println(Arrays.toString(ints));
        Assert.assertTrue(ints.length > 0);

    }

    @Test
    public void testBatchMapInsert() {
        List<Map> maps = new ArrayList<>();

        for (int i = 0; i < 10; i++) {
            Map<String, String> map = new HashMap<>();
            map.put("status", "1");
            map.put("username", "batchMap_" + i);
            map.put("sex", "Man");
            maps.add(map);
        }
        int[] ints = testDao.batchInsert(maps);
        System.out.println(ints.length);
        Assert.assertTrue(ints.length > 0);
    }

    @Test
    public void testPageListMap() throws Exception {
        PageModel pageModel = new PageModel(0, 0, 10);
        String json = "{\"status\":1}";
        LinkedHashMap linkedHashMap = JSON.parseObject(json, LinkedHashMap.class);
        PageModel<Map> mapPageModel = testDao.getTestPageListMap(pageModel, linkedHashMap);
        System.out.println("====");
    }
//    @Test
//    public void test1() throws Exception {
//        PageModel pageModel = new PageModel(0, 0, 10);
//        String json = "{\"houseId\":10151,\"writeType\":1,\"keyword1\":\"%%\",\"keyword2\n" +
//                "\":\"%%\"}";
//        LinkedHashMap linkedHashMap = JSON.parseObject(json, LinkedHashMap.class);
//        PageModel<Map> mapPageModel = testDao.selectOrganizeMembsersByPage(pageModel, linkedHashMap);
//        System.out.println("====");
//    }

//    public static void main(String[] args) {
//        BasicBaseDaoCase basicBaseDaoCase = new BasicBaseDaoCase();
//        basicBaseDaoCase.executeQueryforListMap();
//        basicBaseDaoCase.executeQueryList();
//        basicBaseDaoCase.executeQueryBigDataList();
//        basicBaseDaoCase.executeInsert();
//        basicBaseDaoCase.executeUpdate();
//    }


}
