package com.db.service.main;

import com.alibaba.fastjson.JSON;
import com.db.service.model.TTest;
import com.elab.core.dao.BasicBaseDao;
import com.elab.core.dao.model.JdbcParamsModel;
import com.elab.core.sql.ConfigurableFactory;
import com.elab.core.sql.config.SqlCommandType;
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.ArrayList;
import java.util.Date;
import java.util.List;
import java.util.Map;

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

    @Autowired
    private ConfigurableFactory configurableFactory;

    @Autowired
    private BasicBaseDao basicBaseDao;

//    @Autowired
//    private IHelloDao helloDao;

//    @Test
//    public void executeQueryforListMap() {
//        Map<String, String> map = new HashMap<>();
//        map.put("status", "1");
//        List<Map<String, Object>> maps = basicBaseDao.executeQueryForListMap("com.db.service.dao.ITestDao.selectByExample", map);
//        System.out.println(maps.size());
//    }
//
//    @Test
//    public void executeQueryList() {
//        TTest test = new TTest();
//        test.setStatus("1");
//        List<TTest> tTests = basicBaseDao.executeQueryList("com.db.service.dao.ITestDao.selectByExample", test, TTest.class);
//        System.out.println(tTests.size());
//    }
//
//    @Test
//    public void executeQueryBigDataList() {
//        TTest test = new TTest();
//        test.setStatus("1");
//        List<TTest> tTests = basicBaseDao.executeQueryBigDataList("com.db.service.dao.ITestDao.selectByExample", test, TTest.class);
//        System.out.println(tTests.size());
//    }
//
//
//    @Test
//    public void executeInsert() {
//        TTest test = new TTest();
//        test.setStatus("1");
//        test.setUsername("某某某xx");
//        int i = basicBaseDao.insert("test.insert", test);
//        System.out.println(i);
//    }
//
//    @Test
//    public void executeUpdate() {
//        TTest test = new TTest();
//        test.setId(1);
//        test.setStatus("1");
//        test.setUsername("某某某xx");
//        int i = basicBaseDao.executeUpdate("test.updateByPrimaryKey", test);
//        System.out.println(i);
//    }

    @Test
    public void executeQueryList() throws Exception {
//        String sql = configurableFactory.getSqlConfigurableFactory().getSql("test.selectByObjectToCount");
        String sql = "select  count(1) from test.t_test where id =:id and love_name =:loveName  and  created " +
                "=:created  and  sex =:sex  and  name =:name  and  id =:id  and  time =:time  and  username =:username  and  status =:status  and  test_id =:testId";
        TTest test = new TTest();
        test.setStatus("1");
        Long integer = basicBaseDao.executeQueryCount(sql,
                test, null, Long.class);
//        Long integer1 = basicBaseDao.getJdbcTemplate().queryForObject("select  count(1) from test.t_test where 1=1   and  status =?",
//                new Object[]{1}, Long.class);
        System.out.println(integer);
    }

    @Test
    public void testBatchInsert() throws Exception {
        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);
//            test.setLoveName("aaaaa");
            list.add(test);
        }
        String sql = "insert into t_test (username, name, sex, status, created, time, test_id, love_name) values " +
                "(:username, :name, :sex, :status, :created, :time, :test_id, :love_name)";
//        String sql = "insert into t_test (username, name, sex, status, created, time, test_id, love_name) values (?, ?, ?, ?, ?, ?, ?, ?)";
        basicBaseDao.batchOperation(SqlCommandType.INSERT, sql, list);
        System.out.println("=========");
    }

    @Test
    public void testBatchUpdate() throws Exception {
        List<TTest> list = new ArrayList<>();
        for (int i = 1; i < 10; i++) {
            TTest test = new TTest();
            test.setTime(null);
            test.setId(i);
            list.add(test);
        }
        String sql = "update t_test set time = :time where id = :id";
//        String sql = "insert into t_test (username, name, sex, status, created, time, test_id, love_name) values (?, ?, ?, ?, ?, ?, ?, ?)";
        basicBaseDao.batchOperation(SqlCommandType.UPDATE, sql, list);
    }

    @Test
    public void testSql() throws Exception {
        String json = "{\"houseId\":10151,\"writeType\":1,\"keyword1\":\"%%\",\"keyword2\n" +
                "\":\"%%\"}";
        Map map = JSON.parseObject(json, Map.class);
        String sql = "select distinct a.id,a.name,a.mobile,a.sex,a.head,a.online_status,a.video_online_status,\n" +
                "        a.wxno,a.im_adviser,a.video_adviser,a.agent,a.honor,d.id organize_id,\n" +
                "        d.name organize_name,b.user_id worker_id,b.follow_service,b.toker_service,\n" +
                "        b.writer,b.write_time,b.write_type,b.status,b.recommend,a.photo,a.email from worker_account a\n" +
                "        left join organize_user_rlat b on b.user_id = a.id\n" +
                "        left join organize d on d.id = b.organize_id\n" +
                "        left join t_dm_organize_house_rlat f on f.organize_id = d.id and f.house_id = b.house_id\n" +
                "        where d.status = 1 and f.status = 1 and b.house_id = :houseId and b.write_type = :writeType\n" +
                "        and b.organize_id = :organizeId and (a.name like :keyword1 or a.mobile like :keyword2)\n" +
                "        order by ifnull(b.updated,b.created) desc";
        String okSql = "select distinct a.id,a.name,a.mobile,a.sex,a.head,a.online_status,a.video_online_status,\n" +
                "        a.wxno,a.im_adviser,a.video_adviser,a.agent,a.honor,d.id organize_id,\n" +
                "        d.name organize_name,b.user_id worker_id,b.follow_service,b.toker_service,\n" +
                "        b.writer,b.write_time,b.write_type,b.status,b.recommend,a.photo,a.email from worker_account a\n" +
                "        left join organize_user_rlat b on b.user_id = a.id\n" +
                "        left join organize d on d.id = b.organize_id\n" +
                "        left join t_dm_organize_house_rlat f on f.organize_id = d.id and f.house_id = b.house_id\n" +
                "        where d.status = 1 and f.status = 1 and b.house_id = ? and b.write_type = ?  and (a.name like ? )\n" +
                "        order by ifnull(b.updated,b.created) desc";
        JdbcParamsModel model = basicBaseDao.commonParseSql(sql, map);
        Assert.assertTrue(model.getSql().equals(okSql));
    }
}
