//@张宏伟
module SearchModule {
//获取DO的数量 该接口可以用于主页的DO数量
//也可用于全领监管->总体情况概览 里面的“仓库数量”，“DO数量” ，“DO分布情况”三项。
//可以增加一个参数用于过滤返回结果？比如模糊匹配上的就加到返回列表里。
//str.contains(arg..)
//输入参数，doId，如bdware.txte/Repo
//返回结果：一个数组，里面是doId"contains"这个参数的列表。
// res.addProperty("doCount", this.metaIndex.getDoCount());

    export function getAllRepoCount() {
        var data = executeContract("Registry", "getPageInfo", arg).result.data;
        return {
            "code" : 0, "data" : data.repoCount
        };
    }
    export function getAllUserCount() {
        return {
            "code" : 0, "data" : 472
        };
    }

    export function getAllSearchCount() {
        var data = executeContract("Registry", "getPageInfo", arg).result.data;
        return {
            "code" : 0, "data" : data.searchCount
        };
    }
    export function getAllUpdateCount() {
        var data = executeContract("Registry", "getPageInfo", arg).result.data;
        return {
            "code" : 0, "data" : data.updateCount
        };
    }
    export function getPageInfo(arg) {
        var data = executeContract("Registry", "getPageInfo", arg).result.data;
        return {
            "code" : 0, "data" :data
        };
    }
    export function getAllDoCount() {
        var data = executeContract("Registry", "getPageInfo", arg).result.data;
        return {
            "code" : 0, "data" : data.doCount
        };
    }
    //TODO???
    export function getAllIndexCount() {
        var data = executeContract("Registry", "getPageInfo", arg).result.data;
        return {
            "code" : 0, "data" : data.updateCount
        };
    }
    export function getDoCount(arg) {
        var ret = executeContract("Registry", "getDoCount", arg);
        if (arg.indexOf("shanxi/Repository") != - 1)
            ret.result.data.push({
                "doId" : "shanxi/Repository", "count" : 3
            });
        return ret.result;
    }

    export function testCount(arg) {
        return {
            "data" : "key"
        };
    }

//=====全域监管：数字对象溯源 针对单个DO的，通过SQL查询来实现。
//根据 doId --> 找到“数字对象世系”
//86.500.1/Repo.1/Doab
//根据 doId+时间范围+interval+protocol --> 不同operation的统计数据 （AuditProxy)
//countDORecordByDate()
//{"doId":xxxx,"startTime","endTime","interval","eventPrefix":["doip_create","doip_update"]}
//{"eventxxx":[1,2,3],"event2":[1,2,3]}

//根据 doId +查找条件（时间范围  protocol operation user?) +offset/count -->总数+指定条数的数组（AuditProxy)
// queryDoRecordByDate
//{"doId":xxxx,"startTime","endTime","eventPrefix":["doip_create","doip_update"]}
//{"count":xx, "data":[{doip_request的某一行。},]}
// queryDoRecordByOffset
//{"doId":xxxx,"offset","count","eventPrefix":["doip_create","doip_update"]}
//{"total":xx,"count":xx, "data":[{doip_request的某一行。},]}
//==============



//DO的元数据
//{
// "date": "...",
// "owner": "...",
// "type": "...",
// "description": "...",
// "schema": "...",
//}
//做成可以根据创建时间范围;标识模糊
//{"type":"creatTime","count":10,"offset":"0","from":"1141363811328","to":"1941363811328"}
    export function getDoFilter(arg) {
        var res = executeContract("Registry", "searchByRange", arg);
        return res.result;
    }

//支持模糊查询、按创建时间查询。
//输入参数：
//type：要查询的字段
//keyword:要查询的关键字
//count：（可选项，缺省为20）
//offset：（可选项，缺省为0）
////{"params":[{"type":"owner","keyword":"说明"},{"type":"doid","keyword":"aibd"}],"count":"10","offset":"0"}
//返回：{"data":[{},{},{}],"count":3}
    @ArgSchema({
        "!params" : [{
            "!type" : "string", "!keyword" : "integer"
        }], "count" : "integer", "offset" : "integer"
    })
    export function search(arg) {
        arg = convertArgToJson(arg);
        var req = {"id":"","attributes":{},"body":""};
        for (var i=0;i<arg.params.length;i++){
            req.attributes[arg.params[i].type] = arg.params[i].keyword;
        }
        req.attributes["offset"] = arg.offset;
        req.attributes["count"] = arg.count;
        var res = executeContract("Registry", "search", arg);
        return res.result;
    //ret=[];

    //ret.push({
    //  "doId":"bdware.ss/Repo1/Do.a","name":"数字对象a","meta":{},"owner":"zzz","createTime":1641885492715}
    //        );
    //ret.push({
    //  "doId":"bdware.ss/Repo1/Do.b","name":"数字对象b","meta":{},"owner":"zzz","createTime":1641885322715}
    //        );
    //ret.push({
    //  "doId":"bdware.ss/Repo1/Do.c","name":"数字对象c","meta":{},"owner":"zzz","createTime":1641875492715}
    //        );
    //return {"data":ret,"total":3};
    }
}