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

import com.liquidnet.commons.lang.util.JsonUtils;
import com.liquidnet.service.goblin.dto.vo.GoblinMarketingZhengzaiRelationVo;
import com.liquidnet.service.goblin.dto.vo.GoblinSelfMarketingVo;
import com.liquidnet.service.goblin.entity.GoblinMarketingZhengzaiRelation;
import com.liquidnet.service.goblin.entity.GoblinSelfMarketing;
import com.liquidnet.service.kylin.dto.vo.partner.KylinTicketTimesPartnerVo;
import com.mongodb.BasicDBObject;
import com.mongodb.client.model.FindOneAndUpdateOptions;
import com.mongodb.client.model.ReturnDocument;
import com.mongodb.client.result.DeleteResult;
import com.mongodb.client.result.UpdateResult;
import org.bson.Document;
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;
import org.springframework.stereotype.Component;

import java.util.List;

@Component
public class GoblinMongoUtils {

    @Autowired
    MongoTemplate mongoTemplate;
    @Autowired
    private MongoConverter mongoConverter;

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

    public GoblinSelfMarketingVo updateSelfMarket(String selfMarketId, GoblinSelfMarketingVo data) {
        BasicDBObject object = GoblinObjectUtils.cloneBasicDBObject().append("$set", mongoConverter.convertToMongoType(data));
        Document doc = mongoTemplate.getCollection(GoblinSelfMarketingVo.class.getSimpleName()).findOneAndUpdate(
                Query.query(Criteria.where("selfMarketId").is(selfMarketId)).getQueryObject(),
                object,
                new FindOneAndUpdateOptions().returnDocument(ReturnDocument.AFTER));
        return JsonUtils.fromJson(doc.toJson(), GoblinSelfMarketingVo.class);
    }

    public DeleteResult delSelfMarket(String selfMarketId) {
        return mongoTemplate.remove(Query.query(Criteria.where("selfMarketId").is(selfMarketId)), GoblinSelfMarketingVo.class.getSimpleName());
    }

    public GoblinMarketingZhengzaiRelationVo insertZhengzaiRelation(GoblinMarketingZhengzaiRelationVo data) {
        return mongoTemplate.insert(data, GoblinMarketingZhengzaiRelationVo.class.getSimpleName());
    }

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

    public DeleteResult delZhengzaiRelation(String marketId,String storeId) {
        return mongoTemplate.remove(Query.query(Criteria.where("storeId").is(storeId).and("selfMarketId").is(marketId)), GoblinMarketingZhengzaiRelationVo.class.getSimpleName());
    }

}
