package com.zhongshu.card.server.core.service.pay;

import com.github.microservice.components.data.base.page.PageableModel;
import com.github.microservice.net.ResultContent;
import com.github.microservice.pay.client.model.ledger.transaction.BaseQueryTransactionLogModel;
import com.github.microservice.pay.client.model.ledger.transaction.DescendantQueryTransactionLogModel;
import com.github.microservice.pay.client.model.ledger.transaction.TransactionLogAggregateRetModel;
import com.github.microservice.pay.client.ret.ResultState;
import com.github.microservice.pay.client.service.ledger.TransactionLogService;
import com.github.microservice.pay.client.type.ledger.LedgerEntry;
import com.github.microservice.pay.client.type.ledger.TransactionType;
import com.zhongshu.card.client.model.pay.MyBillParam;
import com.zhongshu.card.client.utils.DateUtils;
import com.zhongshu.card.server.core.domain.pay.PayAccount;
import com.zhongshu.card.server.core.service.base.SuperService;
import org.apache.commons.lang3.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.domain.Pageable;
import org.springframework.stereotype.Service;

import java.util.Date;
import java.util.HashMap;
import java.util.Map;

@Service
public class BillStatisticsService extends SuperService {

    @Autowired
    PayAccountService payAccountService;

    @Autowired
    TransactionLogService transactionLogService;


    /**
     * 小程序-我的账单
     */
    public ResultContent myBill(Pageable pageable, MyBillParam param) {

        String projectOid = param.getProjectOid();
        String userId = param.getUserId();

        if (StringUtils.isBlank(projectOid)) {
            projectOid = getCurrentProjectOid();
        }

        if (StringUtils.isBlank(userId)) {
            userId = getCurrentUserId();
        }

        DescendantQueryTransactionLogModel descendantQueryModel = new DescendantQueryTransactionLogModel();

        if (param.getYear()!=null && param.getMonth()!=null){
            descendantQueryModel.setStartTime(DateUtils.getMonthStartTime(param.getYear(), param.getMonth()));
            descendantQueryModel.setEndTime(DateUtils.getMonthEndTime(param.getYear(), param.getMonth()));
        }

        PayAccount userParent = payAccountService.getUserParent(projectOid, userId);
        descendantQueryModel.setParentGeneralLedgerId(userParent.getLedgerId());

        Map<String, Object> filter = new HashMap<>();
        if (param.getTransactionType()!=null){
            TransactionType transactionType = TransactionType.valueOf(param.getTransactionType());
            filter.put("transactionType", transactionType);
        }
        descendantQueryModel.setFilter(filter);

        PageableModel pageableModel = new PageableModel();
        pageableModel.setPage(pageable.getPageNumber());
        pageableModel.setSize(pageable.getPageSize());
        descendantQueryModel.setPage(pageableModel);

        com.github.microservice.pay.client.ret.ResultContent<TransactionLogAggregateRetModel> aggregate = transactionLogService.aggregate(descendantQueryModel);
        if (!aggregate.getState().equals(ResultState.Success)){
            return ResultContent.buildFail(aggregate.getMsg());
        }
        return ResultContent.buildContent(aggregate.getContent());
    }

    /**
     * 支出分析
     */
    public ResultContent expenditureAnalysis(MyBillParam param){

        String projectOid = param.getProjectOid();
        String userId = param.getUserId();

        if (StringUtils.isBlank(projectOid)) {
            projectOid = getCurrentProjectOid();
        }

        if (StringUtils.isBlank(userId)) {
            userId = getCurrentUserId();
        }
        DescendantQueryTransactionLogModel descendantQueryModel = new DescendantQueryTransactionLogModel();
        //设置时间
        Long currentYearStartTime = DateUtils.getCurrentYearStartTime();
        Long currentDayEndTime = DateUtils.getCurrentYearEndTime();
        descendantQueryModel.setStartTime(currentYearStartTime);
        descendantQueryModel.setEndTime(currentDayEndTime);
        //设置父账本
        PayAccount userParent = payAccountService.getUserParent(projectOid, userId);
        descendantQueryModel.setParentGeneralLedgerId(userParent.getLedgerId());
        //设置过滤条件
        Map<String, Object> filter = new HashMap<>();
        filter.put("ledgerEntry", LedgerEntry.Debit);
        descendantQueryModel.setFilter(filter);
        //设置分页参数
        PageableModel pageableModel = new PageableModel();
        pageableModel.setPage(0);
        pageableModel.setSize(10);
        descendantQueryModel.setPage(pageableModel);
        //统计今年的总支出
        com.github.microservice.pay.client.ret.ResultContent<TransactionLogAggregateRetModel> aggregate = transactionLogService.aggregate(descendantQueryModel);
        if (!aggregate.getState().equals(ResultState.Success)){
            return ResultContent.buildFail(aggregate.getMsg());
        }

        TransactionLogAggregateRetModel aggregateRetModel = aggregate.getContent();
        //年总笔数
        long yearCount = aggregateRetModel.getPages().getTotalElements();

        return null;
    }
}
