package me.chyxion.tigon.mybatis.test;

import me.chyxion.tigon.mybatis.Search;
import me.chyxion.tigon.mybatis.test.mapper.ActivityMapper;
import me.chyxion.tigon.mybatis.test.mapper.AppMapper;
import me.chyxion.tigon.mybatis.test.model.App;

import org.junit.Test;

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

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;

/**
 * @version 0.0.1
 * @since 0.0.1
 * @author Auto Generated <br>
 * Tech Support <a href="mailto:chyxion@163.com">Shaun Chyxion</a><br>
 * May 17, 2016 9:54:18 AM
 */
@RunWith(SpringJUnit4ClassRunner.class)
@ContextConfiguration("classpath*:spring/spring-*.xml")
public class AppMapperTest // extends AbstractTransactionalJUnit4SpringContextTests
{
    @Autowired
    private AppMapper mapper;
    @Autowired
    private ActivityMapper activityMapper;

    @Test
    public void mapperTest() {
        // String id = String.valueOf(new Date().getTime());
        // init model

        App m = newApp();
        App m2 = newApp();
        // m.setCreatedBy("chyxion");
        // mapper.insertApp(Arrays.asList(m, m2));
        mapper.insert(Arrays.asList(m, m2));
        App m3 = newApp();
        mapper.insert(m3);
        App m4 = newApp();
        App m5 = newApp();
        mapper.insert(new App[] {m4, m5});
        // Assert.assertTrue(mapper.list(null).size() > 0);
        System.err.println(m);
        System.err.println(m2);
        System.err.println(m3);
        System.err.println(m4);
        System.err.println(m5);
        for (App app : mapper.list(null)) {
            System.err.println(app);
        }
    }

    App newApp() {
        App m = new App();
        m.setCreatedBy("donghuang");
        m.setDateCreated(new Date());
        m.setName("s");
        m.setHoldingDate(new Date());
        m.setEnabled(true);
        m.setHoldingPlace("s");
        return m;
    }

    @Test
    public void testFind() {
        App app = mapper.find(1L);
        System.err.println(app);
        app = mapper.find(new Search().eq(App.NAME, "s1").desc(App.NAME).offset(0).limit(1));
        System.err.println(app);
        for (App it : mapper.list(
            new Search(App.NAME, "s1")
                .or(new Search(App.NAME, "s2").eq(App.ID, 2L)))) {
            System.err.println(it);
        }
    }

    @Test
    public void testCount() {
        // System.err.println(mapper.count(null));
        System.err.println(activityMapper.count(new Search(3)));
        System.err.println(mapper.count(new Search(3)));
        System.err.println(mapper.count(new Search().gt(App.ID, 1).limit(3).desc(App.NAME)));
        App app = mapper.find(6L);
        app.setName("Updated 6L");
        app.setDateUpdated(new Date());
        mapper.update(app);
    }

    @Test
    public void testList() {
        System.err.println("Empty Search: ");
        for (App app : mapper.list(new Search().desc(App.NAME))) {
            System.err.println(app);
        }
        System.err.println("Null Search: ");
        for (App app : mapper.list(null)) {
            System.err.println(app);
        }
        System.err.println("ID GTE 3: ");
        for (App app : mapper.list(new Search().gte(App.ID, 3))) {
            System.err.println(app);
        }
        System.err.println("ID LTE 3: ");
        for (App app : mapper.list(new Search().lte(App.ID, 3))) {
            System.err.println(app);
        }
        System.err.println("ID BETWEEN 3 AND 5: ");
        for (App app : mapper.list(new Search().between(App.ID, 3, 5).or(App.ID, 6))) {
            System.err.println(app);
        }
    }

    @Test
    public void testDelete() {
        List<App> list = mapper.list(new Search()
                .gte(App.ID, 1)
                .and(new Search()
                        .lt(App.ID, 20).or(App.ID, 30)));
        System.err.println(list);
        list = mapper.list(new Search()
                // .gte(App.ID, 1)
                .and(new Search()
                        .lt(App.ID, 20).or(App.ID, 30)));
        System.err.println(list);
    }
}
