package com.zcxk.rmcp.core.dao.mongo;

import com.alibaba.fastjson.JSONObject;
import com.zcxk.core.mongo.base.BaseDao;
import com.zcxk.rmcp.core.mongo.DeviceData;
import com.zcxk.rmcp.core.mongo.DeviceDataItem;
import org.springframework.data.domain.Sort;
import org.springframework.data.mongodb.core.aggregation.*;
import org.springframework.data.mongodb.core.query.Criteria;
import org.springframework.data.mongodb.core.query.Query;
import org.springframework.stereotype.Repository;

import java.util.ArrayList;
import java.util.Comparator;
import java.util.List;
import java.util.stream.Collectors;

/**
 * @author Andy
 * @version V1.0
 * @description: 设备数据
 * @date 2021/8/2
 **/
@Repository
public class DeviceDataDao  extends BaseDao<DeviceData, String> implements BaseMongoDao {

    /**
    * @author Andy
    * @description 查询设备数据
    * @date 11:39 2021/8/2
    * @param deviceId, beginDate, endDate
    * @return java.util.List<com.zcxk.rmcp.core.mongo.DeviceData>
    **/
    public List<DeviceData> queryDeviceData(Long deviceId, Integer beginDate, Integer endDate){
        DeviceData param = new DeviceData();
        param.setDeviceId(deviceId);
        Query query = buildBaseQuery(param);
        if(beginDate != null || endDate != null){
            Criteria criteria = Criteria.where("receiveDate");
            if(beginDate != null){
                criteria.gte(beginDate);
            }
            if(endDate != null){
                criteria.lte(endDate);
            }
            query.addCriteria(criteria);
        }
        query.with(Sort.by(Sort.Order.asc("receiveTime")));
        return findList(query);
    }


    /*
     * @description 查询设备数据项
     * @param deviceId
     * @param beginDate
     * @param endDate
     * @return java.util.List<com.zcxk.rmcp.core.mongo.DeviceDataItem>
     * @author linqingwei
     **/
    public List<DeviceDataItem> queryDeviceDataItem(Long deviceId, Integer beginDate, Integer endDate){
        List<DeviceDataItem> list = new ArrayList<>();
        //封装对象列表查询条件
        List<AggregationOperation> commonOperations = new ArrayList<>();
        //1. 指定查询主文档
        MatchOperation match = Aggregation.match(Criteria.where("deviceId").is(deviceId));
        commonOperations.add(match);
        //2. 指定投影，返回哪些字段
        ProjectionOperation project = Aggregation.project("data");
        commonOperations.add(project);
        //3. 拆分内嵌文档
        UnwindOperation unwind = Aggregation.unwind("data");
        commonOperations.add(unwind);
        //4. 指定查询子文档
        if(beginDate != null || endDate != null){
            Criteria criteria = Criteria.where("data.receiveDate");
            if(beginDate != null){
                criteria.gte(beginDate);
            }
            if(endDate != null){
                criteria.lte(endDate);
            }
            MatchOperation match2 = Aggregation.match(criteria);
            commonOperations.add(match2);
        }


        //创建管道查询对象
        Aggregation aggregation = Aggregation.newAggregation(commonOperations);
        AggregationResults<JSONObject> reminds = mongoTemplate.aggregate(aggregation, "rmcp_device_data", JSONObject.class);
        List<JSONObject> mappedResults = reminds.getMappedResults();
        if (mappedResults != null && mappedResults.size() > 0) {
            for (JSONObject mappedResult : mappedResults) {
                list.add(JSONObject.parseObject(mappedResult.getJSONObject("data").toJSONString(), DeviceDataItem.class));
            }
        }

        return list.stream().sorted(Comparator.comparingLong(DeviceDataItem::getReceiveTime).reversed()).collect(Collectors.toList());

    }
}
