package com.elab.test.mongodb;

import com.alibaba.fastjson.JSONObject;
import com.elab.test.mongodb.entity.User;
import com.elab.test.mongodb.ext.JsonObjectDb;
import com.elab.test.mongodb.ext.UserMongoDB;
import com.mongodb.*;
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.*;

/**
 * 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 Exception {
        UserMongoDB mongoDB = new UserMongoDB();
        MongoTemplate mongoTemplate = new MongoTemplate(
            new SimpleMongoDbFactory(new MongoClient("127.0.0.1", Integer.parseInt("27017")), "test"));
        mongoDB.setMongoTemplate(mongoTemplate);

        List<String> strings = Arrays.asList("sss", "fff", "ddd");

        User user = new User("uuu", "123", "7", strings);
        User user1 = new User("jjj", "123", "8", strings);
        User user2 = new User("kkkk", "123", "9", strings);
        Query query = new Query();
        Criteria criteria = Criteria.where("arrayData").in("A", "sss");
        query.addCriteria(criteria);
        List<User> user3 = mongoTemplate.find(query, User.class, "user");
        System.out.println(user3);
        //        List<User> list = new ArrayList<>();
        //        list.add(user);
        //        list.add(user1);
        //        list.add(user2);
        //        mongoDB.batchSave(list);
        //        mongoDB.deleteById(user1.getId());
        //        mongoDB.batchSave(list);
        //        mongoDB.updateById(user1.getId(),user1);
        //        mongoDB.updateById(user.getId(),user2);
        //        mongoDB.save(user);
        //        mongoDB.save(user1);
        //        mongoDB.save(user2);
        System.out.println("成功~");
    }

    @Test
    public void insertJSON() throws UnknownHostException {
        JsonObjectDb mongoDB = new JsonObjectDb();
        MongoTemplate mongoTemplate = new MongoTemplate(
            new SimpleMongoDbFactory(new MongoClient("127.0.0.1", Integer.parseInt("27017")), "test"));

        mongoDB.setMongoTemplate(mongoTemplate);

        JSONObject jsonObject = new JSONObject();
        jsonObject.put("aaa", "fffff");
        jsonObject.put("字段1", "二二二");

        mongoTemplate.insert(jsonObject);

        //        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());
    }

    @Test
    public void findCollectField() throws Exception {

        String username = "elab";
        String password = "elab8888";
        String mongodbName = "mofang";
        List<ServerAddress> serverAddressList = new ArrayList<>();
        serverAddressList.add(new ServerAddress("106.15.201.221", 27017));

        MongoCredential userCredentials =
            MongoCredential.createCredential(username, mongodbName, password.toCharArray());
        List<MongoCredential> userCredentialsList = new ArrayList<>();
        userCredentialsList.add(userCredentials);
        MongoClient mongoClient = new MongoClient(serverAddressList, userCredentialsList);

        SimpleMongoDbFactory mongoDbFactory = new SimpleMongoDbFactory(mongoClient, mongodbName);
        MongoTemplate mongoTemplate = new MongoTemplate(mongoDbFactory);

        Set<String> collectionNames = mongoTemplate.getCollectionNames();
        BasicDBObject doc = new BasicDBObject();
        DBObject dbObject = new BasicDBObject("$natural", -1);

        String fieldType = "string";
        for (String name : collectionNames) {
            System.out.println("----------------------------" + name + "---------------------------");
            try {
                DBObject collectionObject = mongoTemplate.getCollection(name).find(doc).sort(dbObject).limit(1).next();
                Set<String> keySet = collectionObject.keySet();
                for (String key : keySet) {
                    Object o = collectionObject.get(key);
                    String defaultValue = fieldType;
                    if (o instanceof BasicDBList) {
                        defaultValue = "array";
                    }
                    System.out.println(key + "\t" + defaultValue);
                }
            } catch (Exception e) {
                System.out.println(e.getMessage());
            }
        }

    }

}
