Esempio n. 1
0
        public async Task <string> Excute([FromBody] mongodb value)
        {
            var config           = Core.Helpers.CommonHelper.JsonToBsonDocument(value.config);
            var para             = Core.Helpers.CommonHelper.JsonToBsonDocument(value.para);
            var session_customer = !config.Contains("session_customer")?"":(string)config["session_customer"];
            var q                 = (string)para["q"];
            var page_next_bot     = !config.Contains("page_next_bot") ? 0 : (int)config["page_next_bot"];
            var receive_type      = !config.Contains("receive_type") ?"": (string)config["receive_type"];
            var using_full_search = config.Contains("using_full_search") && (string)config["using_full_search"] == "1"?1:0;

            var page_id     = !config.Contains("page_id") ? "" : (string)config["page_id"];
            var auto_agents = !config.Contains("auto_agents") ? "" : (string)config["auto_agents"];
            var business_id = !config.Contains("business_id") ? "" : (string)config["business_id"];

            var key = "data_" + session_customer + "_" +
                      Core.Helpers.CommonHelper.removeSpecialString(q) + "_" + page_next_bot;


            if (!string.IsNullOrWhiteSpace(auto_agents) && auto_agents != "manualagents" && q != "")
            {
                var checkCache = CacheBase.GetItem(key);
                if (checkCache != null)
                {
                    return((string)checkCache);
                }
            }
            return(await _aiService.GetProduct(q, session_customer, receive_type, using_full_search, page_id,
                                               auto_agents, page_next_bot, business_id, key));
        }
Esempio n. 2
0
        public async Task <string> execute([FromBody] mongodb value)
        {
            try
            {
                var configDic = Core.Helpers.CommonHelper.JsonToBsonDocument(value.config);

                MongoClient    client = null;
                IMongoDatabase db     = null;
                if (((string)configDic["mongoconnect"]).ToLower() == "connai")
                {
                    client = new MongoClient(_appSettings.Value.MongoDB.ConnAi);
                    db     = client.GetDatabase(_appSettings.Value.MongoDB.AiDb);
                }
                if (((string)configDic["mongoconnect"]).ToLower() == "connchat")
                {
                    client = new MongoClient(_appSettings.Value.MongoDB.ConnChat);
                    db     = client.GetDatabase(_appSettings.Value.MongoDB.ChatDb);
                }
                IMongoCollection <BsonDocument> collection = null;
                if (configDic.Contains("collectionname") && configDic["collectionname"] != "")
                {
                    collection = db.GetCollection <BsonDocument>((string)configDic["collectionname"]);
                }

                var type = ((string)configDic["type"]).ToLower();
                if (type == "insert")
                {
                    try
                    {
                        var json     = JsonConvert.DeserializeObject(value.para).ToString().Replace("\"", "'");
                        var document = BsonSerializer.Deserialize <BsonDocument>(json);
                        collection.InsertOneAsync(document).Wait();
                        return("[]");
                    }
                    catch (Exception ex) { return(null); }
                }
                if (type == "searchlike")
                {
                    try
                    {
                        string project = "{}";
                        if (value.projection != null)
                        {
                            project = value.projection;
                        }

                        var options = new FindOptions <BsonDocument>();
                        options.Projection = project;
                        options.Limit      = value.limit == 0 ? 10 : value.limit;

                        var json     = JsonConvert.DeserializeObject(value.para).ToString().Replace("\"", "'");
                        var document = BsonSerializer.Deserialize <BsonDocument>(json);
                        var data     = await collection.FindAsync <BsonDocument>(document, options);

                        return(JsonConvert.SerializeObject(data.ToList()));
                    }
                    catch (Exception ex) { return(null); }
                }
                if (type == "search")
                {
                    try
                    {
                        var options = new FindOptions <BsonDocument>();
                        if (value.projection != null)
                        {
                            options.Projection = value.projection;
                        }
                        options.Limit = value.limit == 0 ? 10 : value.limit;
                        if (value.order != null)
                        {
                            var js  = JsonConvert.DeserializeObject(value.order).ToString().Replace("\"", "'");
                            var doc = BsonSerializer.Deserialize <BsonDocument>(js);
                            if (doc.Contains("asc"))
                            {
                                options.Sort = Builders <BsonDocument> .Sort.Ascending(doc["asc"].ToString());
                            }
                            else
                            {
                                options.Sort = Builders <BsonDocument> .Sort.Descending(doc["desc"].ToString());
                            }
                        }
                        var json     = JsonConvert.DeserializeObject(value.para).ToString().Replace("\"", "'");
                        var document = BsonSerializer.Deserialize <BsonDocument>(json);
                        var data     = await collection.FindAsync <BsonDocument>(document, options);

                        var rs = data.ToList();

                        string tt = rs.ToJson();
                        return(tt);
                    }
                    catch (Exception ex) { return(null); }
                }

                if (type == "procedure")
                {
                    try
                    {
                        var cmd = new JsonCommand <BsonDocument>("{ eval: " + value.para + "}");
                        var rs  = await db.RunCommandAsync <BsonDocument>(cmd);

                        var data = rs.ToList();
                        if (data != null && data.Count > 1)
                        {
                            return(data[0].Value.ToString());
                        }
                        return(JsonConvert.SerializeObject(data));
                    }
                    catch (Exception ex) { return(null); }
                }
                if (type == "upsert")
                {
                    try
                    {
                        var filter         = JsonConvert.DeserializeObject(value.filter).ToString().Replace("\"", "'");
                        var filterDocument = BsonSerializer.Deserialize <BsonDocument>(filter);

                        var json     = JsonConvert.DeserializeObject(value.para).ToString().Replace("\"", "'");
                        var document = BsonSerializer.Deserialize <BsonDocument>(json);

                        UpdateOptions op = new UpdateOptions();
                        op.IsUpsert = true;

                        var rs = await collection.UpdateManyAsync(filterDocument, document, op);

                        if (rs != null && rs.MatchedCount > 0 || rs.ModifiedCount > 0 || rs.UpsertedId != null)
                        {
                            return("[]");
                        }
                    }
                    catch (Exception ex) { return(null); }
                }

                if (type == "delete")
                {
                    try
                    {
                        var json     = JsonConvert.DeserializeObject(value.filter).ToString().Replace("\"", "'");
                        var document = BsonSerializer.Deserialize <BsonDocument>(json);
                        collection.DeleteManyAsync(document).Wait();
                        return("[]");
                    }
                    catch (Exception ex) { return(null); }
                }
                return(null);
            }
            catch (Exception ex) { return(null); }
        }