package com.liquidnet.client.admin.zhengzai.goblin.utils;

import com.liquidnet.service.goblin.entity.GoblinSelfMarketing;
import com.mongodb.BasicDBObject;
import com.mongodb.client.result.UpdateResult;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.mongodb.core.MongoTemplate;
import org.springframework.data.mongodb.core.convert.MongoConverter;
import org.springframework.data.mongodb.core.query.Criteria;
import org.springframework.data.mongodb.core.query.Query;

public class GoblinMongoUtils {

    @Autowired
    MongoTemplate mongoTemplate;
    @Autowired
    private MongoConverter mongoConverter;

    public GoblinSelfMarketing insertSelfMarket(GoblinSelfMarketing data) {
        return mongoTemplate.insert(data, GoblinSelfMarketing.class.getSimpleName());
    }

    public UpdateResult updateSelfMarket(String selfMarketId, GoblinSelfMarketing data) {
        BasicDBObject object = GoblinObjectUtils.cloneBasicDBObject().append("$set", mongoConverter.convertToMongoType(data));
        return mongoTemplate.getCollection(GoblinSelfMarketing.class.getSimpleName()).updateOne(
                Query.query(Criteria.where("selfMarketId").is(selfMarketId)).getQueryObject(),
                object);
    }

}
