package com.elab.test.mongodb;

import com.elab.test.mongodb.entity.User;
import com.elab.test.mongodb.ext.UserMongoDB;
import com.mongodb.MongoClient;
import org.junit.Test;
import org.springframework.data.mongodb.core.MongoTemplate;
import org.springframework.data.mongodb.core.SimpleMongoDbFactory;
import org.springframework.data.mongodb.core.query.Criteria;
import org.springframework.data.mongodb.core.query.Query;

import java.net.UnknownHostException;
import java.util.ArrayList;
import java.util.List;
import java.util.Map;

/**
 * MongoDB测试类
 *
 * @author Liukx
 * @create 2018-05-03 15:40
 * @email liukx@elab-plus.com
 **/
//@RunWith(SpringJUnit4ClassRunner.class)  //使用junit4进行测试
//@ContextConfiguration
//        ({})
public class MongodbTest {

    @Test
    public void insert() throws UnknownHostException {
        UserMongoDB mongoDB = new UserMongoDB();
        MongoTemplate mongoTemplate = new MongoTemplate(new SimpleMongoDbFactory(new MongoClient("127.0.0.1", Integer.parseInt("27017")),
                "test"));
        mongoDB.setMongoTemplate(mongoTemplate);
        User user = new User("uuu", "123", "7");
        User user1 = new User("jjj", "123", "8");
        User user2 = new User("kkkk", "123", "9");
        List<User> list = new ArrayList<>();
        list.add(user);
        list.add(user1);
        list.add(user2);
        mongoDB.batchSave(list);
//        mongoDB.save(user);
//        mongoDB.save(user1);
//        mongoDB.save(user2);
        System.out.println("成功~");
    }

    @Test
    public void update() throws Exception {
        MongoTemplate mongoTemplate = new MongoTemplate(new SimpleMongoDbFactory(new MongoClient("127.0.0.1", Integer.parseInt("27017")),
                "test"));
        UserMongoDB mongoDB = new UserMongoDB();
        User user = new User("lkx", "4444", "1");
        mongoDB.updateById("1", user);
        System.out.println("成功~");
    }

    @Test
    public void delete() throws Exception {
        MongoTemplate mongoTemplate = new MongoTemplate(new SimpleMongoDbFactory(new MongoClient("127.0.0.1", Integer.parseInt("27017")),
                "test"));
        UserMongoDB mongoDB = new UserMongoDB();
        mongoDB.deleteById("1");
        System.out.println("成功~");
    }

    @Test
    public void query() throws Exception {
        MongoTemplate mongoTemplate = new MongoTemplate(new SimpleMongoDbFactory(new MongoClient("139.196.5.59", Integer.parseInt("27017")),
                "logdb"));
//        UserMongoDB mongoDB = new UserMongoDB();
//        User user = new User("xxx", null, null);
//        List<User> byList = mongoDB.findByList(user);
//        System.out.println(JSON.toJSONString(byList));
//        mongoDB.
        Criteria area = Criteria.where("area").is("bbbb");
//        Criteria price = Criteria.where("price").lt("100").gt("200").in("aaaaa");

        Query query = new Query();
        query.addCriteria(area);
//        query.addCriteria(price);
        List<Map> copy_of_rental_house = mongoTemplate.find(query, Map.class, "Copy_of_rental_house");
        System.out.println(copy_of_rental_house.size());
    }

}
