package com.zhongshu.card.server.core.dao.pay.impl;

import ch.qos.logback.core.util.StringUtil;
import com.github.microservice.types.OrderState;
import com.zhongshu.card.client.model.pay.QueryRechargeParam;
import com.zhongshu.card.client.model.pay.TotalStatisticsModel;
import com.zhongshu.card.client.type.payAccount.RechargeOrderStatus;
import com.zhongshu.card.client.utils.DateUtils;
import com.zhongshu.card.server.core.dao.pay.extend.BalanceRechargeOrderDaoExtend;
import com.zhongshu.card.server.core.domain.pay.BalanceRechargeOrder;
import com.zhongshu.card.server.core.util.CommonUtil;
import org.apache.commons.lang3.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.domain.Sort;
import org.springframework.data.mongodb.core.MongoTemplate;
import org.springframework.data.mongodb.core.aggregation.Aggregation;
import org.springframework.data.mongodb.core.aggregation.AggregationResults;
import org.springframework.data.mongodb.core.query.Criteria;
import org.springframework.data.mongodb.core.query.Query;

import java.util.ArrayList;
import java.util.List;

public class BalanceRechargeOrderDaoImpl implements BalanceRechargeOrderDaoExtend {

    @Autowired
    MongoTemplate mongoTemplate;

    @Override
    public List<BalanceRechargeOrder> listByMonth(String projectOid, String userId, Long startTime, Long endTime) {
        Criteria criteria = new Criteria();

        if (StringUtils.isNotEmpty(projectOid)){
            criteria.and("projectOid").is(projectOid);
        }

        if (StringUtils.isNotEmpty(userId)){
            criteria.and("userId").is(userId);
        }

        if (!CommonUtil.longIsEmpty(startTime)){
            criteria.and("createTime").gte(startTime);
        }

        if (CommonUtil.longIsEmpty(endTime)){
            criteria.and("createTime").lte(endTime);
        }

        Query query = Query.query(criteria);
        query.with(Sort.by(Sort.Order.desc("createTime")));

        return mongoTemplate.find(query, BalanceRechargeOrder.class);
    }

    @Override
    public Long sumTotal(String projectOid, String userId, RechargeOrderStatus status, Long startTime, Long endTime) {
        Criteria criteria = new  Criteria();

        if (StringUtils.isNotEmpty(projectOid)){
            criteria.and("projectOid").is(projectOid);
        }

        if (StringUtils.isNotEmpty(userId)){
            criteria.and("userId").is(userId);
        }

        if (status != null){
            criteria.and("status").is(status);
        }

        if (!CommonUtil.longIsEmpty(startTime)){
            criteria.and("createTime").gte(startTime);
        }

        if (CommonUtil.longIsEmpty(endTime)){
            criteria.and("createTime").lte(endTime);
        }

        Aggregation aggregation = Aggregation.newAggregation(
                Aggregation.match(criteria),
//                Aggregation.project("_id","amountTotal"),
                Aggregation.group("projectId", "userId").sum("total").as("totalAmount")
//                Aggregation.group("tradeType").last("tradeType").as("_id")
        );


        AggregationResults<TotalStatisticsModel> groupList = mongoTemplate.aggregate(aggregation, BalanceRechargeOrder.class, TotalStatisticsModel.class);
        List<TotalStatisticsModel> list = new ArrayList<>();

        if (groupList.getMappedResults().size()==0){
            return 0L;
        }

        return groupList.getMappedResults().get(0).getTotalAmount().longValue();
    }


}
