package cn.estsh.i3plus.core.apiservice.daoimpl;

import cn.estsh.i3plus.core.apiservice.dao.ISysLogSystemDao;
import cn.estsh.i3plus.platform.common.tool.MathOperation;
import cn.estsh.i3plus.platform.common.tool.StringTool;
import cn.estsh.i3plus.pojo.base.enumutil.ImppEnumUtil;
import cn.estsh.i3plus.pojo.platform.platbean.SysLogSystem;
import com.mongodb.client.AggregateIterable;
import com.mongodb.client.MongoCursor;
import org.bson.Document;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.mongodb.MongoDbFactory;
import org.springframework.stereotype.Service;

import java.util.*;

/**
 * @Description :
 * @Reference :
 * @Author : yunhao
 * @CreateDate : 2018-12-11 13:24
 * @Modify:
 **/
@Service
public class SysLogSystemDaoImpl implements ISysLogSystemDao {

    public static final Logger LOGGER = LoggerFactory.getLogger(SysLogSystemDaoImpl.class);

    @Autowired
    private MongoDbFactory mongoDbFactory;

    @Override
    public Map<String, Object> querySysLogSystemAvgExecuteTime(String startTime, String endTime) {
        // 筛选
        Document sub_match = new Document();
        sub_match.put("createDatetime", new Document("$gte", startTime).append("$lte", endTime));
       // 分组
        Document sub_group = new Document();
        sub_group.put("_id", "$createDatetime");
        sub_group.put("avg", new Document("$avg", "$executeTime"));


        Document match = new Document("$match", sub_match);
        Document group = new Document("$group", sub_group);
        Document sort = new Document("$sort", new Document("_id", -1));

        List<Document> aggregateList = new ArrayList<>();
        aggregateList.add(match);
        aggregateList.add(group);
        aggregateList.add(sort);

        AggregateIterable<Document> findIter = mongoDbFactory.getDb()
                .getCollection(StringTool.toLowerCaseFirstOne(SysLogSystem.class.getSimpleName()))
                .aggregate(aggregateList);
        MongoCursor<Document> cursor = findIter.iterator();

        Map resultData = new LinkedHashMap();
        Document item_doc;
        while (cursor.hasNext()) {
            item_doc = cursor.next();
            resultData.put(item_doc.getString("_id"),MathOperation.round(item_doc.getDouble("avg"),0));
        }
        return resultData;
    }

    @Override
    public Map<String,Object> querySysLogSystemByLevel(String startTime, String endTime){
        // 筛选
        Document sub_match = new Document();
        sub_match.put("createDatetime", new Document("$gte", startTime).append("$lte", endTime));
        // 分组
        Document sub_group = new Document();
        sub_group.put("_id", "$logLevel");
        sub_group.put("count", new Document("$sum", 1));

        Document match = new Document("$match", sub_match);
        Document group = new Document("$group", sub_group);

        List<Document> aggregateList = new ArrayList<>();
        aggregateList.add(match);
        aggregateList.add(group);

        AggregateIterable<Document> findIter = mongoDbFactory.getDb()
                .getCollection(StringTool.toLowerCaseFirstOne(SysLogSystem.class.getSimpleName()))
                .aggregate(aggregateList);
        MongoCursor<Document> cursor = findIter.iterator();

        Map resultData = new LinkedHashMap();
        Document item_doc;
        while (cursor.hasNext()) {
            item_doc = cursor.next();
            resultData.put(ImppEnumUtil.LOG_LEVEL.valueOfName(item_doc.getInteger("_id")),MathOperation.round(item_doc.getInteger("count"),0));
        }

        return resultData;
    }

}
