package cn.superdesk.uniorder.dubbo.provider.thirdpart.qchjc.statistics;

import cn.superdesk.uniorder.common.json.AnJsonUtils;
import cn.superdesk.uniorder.common.rest.response.R;
import cn.superdesk.uniorder.common.security.dto.LoginSysUser;
import cn.superdesk.uniorder.dao.entity.sys.notify.SysNotify;
import cn.superdesk.uniorder.dto.param.accounting.statistics.PayableBalanceQueryParam;
import cn.superdesk.uniorder.dto.param.accounting.statistics.ReceivableBalanceQueryParam;
import cn.superdesk.uniorder.dto.param.accounting.statistics.ReceivableDetailDataQueryParam;
import cn.superdesk.uniorder.dto.param.accounting.statistics.ReceivableStatisticsPageQueryParam;
import cn.superdesk.uniorder.dto.vo._statistics.ReceivableBalanceVO;
import cn.superdesk.uniorder.dto.vo._statistics.ReceivableDetailDataVo;
import cn.superdesk.uniorder.dto.vo._statistics.ReceivableStatisticsPageVo;
import cn.superdesk.uniorder.dto.vo._statistics.payable.PayableBalanceVO;
import cn.superdesk.uniorder.dto.vo._statistics.payable.PayableStatisticsPageVo;
import cn.superdesk.uniorder.dubbo.facade.sys.notify.DubboSysNotifyService;
import cn.superdesk.uniorder.dubbo.facade.thirdpart.qchjc.statistics.DubboReceivableStatisticsService;
import cn.superdesk.uniorder.service.thirdpart.qchjc.statistics.PayableStatisticsService;
import cn.superdesk.uniorder.service.thirdpart.qchjc.statistics.ReceivableStatisticsService;
import com.alibaba.dubbo.config.annotation.Reference;
import com.alibaba.dubbo.config.annotation.Service;
import com.alibaba.fastjson2.JSON;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;

import java.util.List;
import java.util.concurrent.CompletableFuture;

/**
 * @author xc
 * @description
 * @date 2022/4/1 10:25
 */
@Service(version = "1.0.0", retries = -1, timeout = 10000)
@Slf4j
public class DubboReceivableStatisticsServiceImpl implements DubboReceivableStatisticsService {

    @Reference(version = "1.0.0", timeout = 10000, retries = -1)
    private DubboSysNotifyService dubboSysNotifyService;

    @Autowired
    private ReceivableStatisticsService receivableStatisticsService; //应收Service

    @Autowired
    private PayableStatisticsService payableStatisticsService; //应付Service


    //查询应收明细数据
    @Override
    public List<ReceivableDetailDataVo> queryReceivableDetailData(ReceivableDetailDataQueryParam queryParam) {

        return receivableStatisticsService.queryReceivableDetailData(queryParam);
    }

    //保存应收明细数据
    @Override
    public void saveReceivableDetailData(ReceivableDetailDataQueryParam queryParam,LoginSysUser loginSysUser) {
        receivableStatisticsService.saveReceivableDetailData(queryParam,loginSysUser);
    }

    //应收明细数据定时任务到每日数据
    @Override
    public void saveReceivableDetailDataToDayData(ReceivableDetailDataQueryParam queryParam,LoginSysUser loginSysUser) {
        receivableStatisticsService.saveReceivableDetailDataToDayData(queryParam,loginSysUser);
    }

    @Override
    public R<List<ReceivableStatisticsPageVo>> getReceivableStatisticsPageVoList(ReceivableStatisticsPageQueryParam queryParam) {
        return receivableStatisticsService.getReceivableStatisticsPageVoList(queryParam);
    }

    /**
     * 汇总(应收账款) --先处理和测试下
     * 每日数据
     * 明细数据
     * @param queryParam
     * @return
     */
    @Override
    public String getReceivableStatisExcel(ReceivableStatisticsPageQueryParam queryParam, LoginSysUser loginSysUser) {


        log.info("\n异步操作 包裹 外层 getReceivableStatisExcel params: \n{}", JSON.toJSONString(queryParam));

        CompletableFuture.runAsync(() -> {

            try {

                SysNotify sysNotify = receivableStatisticsService.getReceivableStatisExcel(queryParam);

                // 通知
                dubboSysNotifyService.notifyExportExcelScoreOrderReconciliationNotYet(sysNotify, loginSysUser);

            } catch (Exception e2) {
                log.error("\n异步操作 包裹 内层 getReceivableStatisExcel {} -> 执行 异常:{}", AnJsonUtils.toJson(queryParam), e2);
            }
        });

        return "导出任务已经接收,正在处理中...";


    }


    //保存应付明细数据
    @Override
    public void savePayableDetailData(ReceivableDetailDataQueryParam queryParam, LoginSysUser loginSysUser) {
        payableStatisticsService.savePayableDetailData(queryParam, loginSysUser);
    }

    /**
     * 应收明细数据定时任务到每日数据
     * @param queryParam
     */
    @Override
    public void savePayableDetailDataToDayData(ReceivableDetailDataQueryParam queryParam, LoginSysUser loginSysUser) {
        payableStatisticsService.savePrayableDetailDataToDayData(queryParam, loginSysUser);
    }

    @Override
    public R<List<PayableStatisticsPageVo>> getPayableStatisticsPageVoList(ReceivableStatisticsPageQueryParam queryParam) {
        return payableStatisticsService.getPayableStatisticsPageVoList(queryParam);
    }

    @Override
    public String getPayableStatisExcel(ReceivableStatisticsPageQueryParam queryParam, LoginSysUser loginSysUser) {

        log.info("\n异步操作 包裹 外层 getPayableStatisExcel params: \n{}", JSON.toJSONString(queryParam));

        CompletableFuture.runAsync(() -> {

            try {

                SysNotify sysNotify = payableStatisticsService.getPayableStatisExcel(queryParam);

                // 通知
                dubboSysNotifyService.notifyExportExcelScoreOrderReconciliationNotYet(sysNotify, loginSysUser);

            } catch (Exception e2) {
                log.error("\n异步操作 包裹 内层 getPayableStatisExcel {} -> 执行 异常:{}", AnJsonUtils.toJson(queryParam), e2);
            }
        });

        return "导出任务已经接收,正在处理中...";
    }

    @Override
    public void receivableCancelAccounting(ReceivableDetailDataQueryParam queryParam,LoginSysUser loginSysUser) {
        receivableStatisticsService.receivableCancelAccounting(queryParam,loginSysUser);
    }

    @Override
    public void receivableReturnedMoney(ReceivableDetailDataQueryParam queryParam,LoginSysUser loginSysUser) {
        receivableStatisticsService.receivableReturnedMoney(queryParam,loginSysUser);
    }

    @Override
    public void payableReturnedMoney(ReceivableDetailDataQueryParam queryParam, LoginSysUser loginSysUser) {
        payableStatisticsService.payableReturnedMoney(queryParam,loginSysUser);
    }

    @Override
    public void payableCancelAccounting(ReceivableDetailDataQueryParam queryParam, LoginSysUser loginSysUser) {
        payableStatisticsService.payableCancelAccounting(queryParam,loginSysUser);
    }




}
