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.vo._statistics.ReceivableBalanceVO;
import cn.superdesk.uniorder.dto.vo._statistics.payable.PayableBalanceVO;
import cn.superdesk.uniorder.dubbo.facade.sys.notify.DubboSysNotifyService;
import cn.superdesk.uniorder.dubbo.facade.thirdpart.qchjc.statistics.DubboStatisticsBalanceService;
import cn.superdesk.uniorder.service.thirdpart.qchjc.statistics.BalanceStatistcsService;
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.concurrent.CompletableFuture;

/**
 * @author xc
 * @description
 * @date 2022/6/22 09:30
 */
@Service(version = "1.0.0", retries = -1, timeout = 10000)
@Slf4j
public class DubboStatisticsBalanceServiceImpl implements DubboStatisticsBalanceService {

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

    @Autowired
    private BalanceStatistcsService balanceStatistcsService;

    @Override
    public R<ReceivableBalanceVO> getReceivableBalanceVOList(ReceivableBalanceQueryParam param) {

        return balanceStatistcsService.getReceivableBalanceVOList(param);
    }

    @Override
    public R<PayableBalanceVO> getPayableBalanceVOList(PayableBalanceQueryParam param) {
        return balanceStatistcsService.getPayableBalanceVOList(param);
    }

    @Override
    public String exportExcelReceivableBalance(ReceivableBalanceQueryParam param, LoginSysUser loginSysUser) {
        log.info("导出应收余额报表 getReceivableStatisExcel params: \n{}", JSON.toJSONString(param));
        CompletableFuture.runAsync(() -> {
            try {
                SysNotify sysNotify = balanceStatistcsService.exportExcelReceivableBalance(param);
                // 通知
                dubboSysNotifyService.notifyExportExcelScoreOrderReconciliationNotYet(sysNotify, loginSysUser);
            } catch (Exception e) {
                log.error("导出应收余额报表 getReceivableStatisExcel {} -> 执行 异常:{}", AnJsonUtils.toJson(param), e);
            }
        });
        return "导出任务已经接收,正在处理中...";
    }

    @Override
    public String exportExcelPayableBalance(PayableBalanceQueryParam param, LoginSysUser loginSysUser) {
        log.info("导出应付余额报表 getPayableStatisExcel params: \n{}", JSON.toJSONString(param));
        CompletableFuture.runAsync(() -> {
            try {
                SysNotify sysNotify = balanceStatistcsService.exportExcelPayableBalance(param);
                // 通知
                dubboSysNotifyService.notifyExportExcelScoreOrderReconciliationNotYet(sysNotify, loginSysUser);
            } catch (Exception e) {
                log.error("导出应收余额报表 getReceivableStatisExcel {} -> 执行 异常:{}", AnJsonUtils.toJson(param), e);
            }
        });
        return "导出任务已经接收,正在处理中...";
    }
}
