package nckd.jimin.jyyy.fi.common.util;

import kd.bos.dataentity.entity.DynamicObjectCollection;
import kd.bos.ext.fi.ai.DapVoucherUtil;
import kd.bos.orm.query.QCP;
import kd.bos.orm.query.QFilter;
import kd.bos.servicehelper.QueryServiceHelper;
import kd.bos.servicehelper.botp.BFTrackerServiceHelper;
import kd.bos.util.CollectionUtils;
import kd.bos.util.StringUtils;
import nckd.jimin.jyyy.fi.common.constant.BillTypeConstants;

import java.util.*;
import java.util.stream.Collectors;

public class ErFindRelationBillUtils {

    /**
     * 查询费用单据关联下游单据
     * @param billId
     * @param billType
     * @return
     */
    public static Map<String ,Set<Object>> getRelationBelowBillMap(String billId,String billType){
        Map<String ,Set<Object>> relationBillMap = new HashMap<>();
        // 查询付款处理
        Set<Object> casPaybillSet = getBotpRelationId(billType, BillTypeConstants.CAS_PAYBILL, Long.parseLong(billId), true);
        // 凭证
        Set<Long> voucherSet = DapVoucherUtil.getBuildData(Collections.singletonList(Long.valueOf(billId)), billType);
        relationBillMap.put(BillTypeConstants.GL_VOUCHER,voucherSet.stream().map(r->(Object)r).collect(Collectors.toSet()));

        if(CollectionUtils.isNotEmpty(casPaybillSet)){
            relationBillMap.put(BillTypeConstants.CAS_PAYBILL,casPaybillSet);
            //Set<Long> voucherSet = DapVoucherUtil.getBuildData(casPaybillSet, BillTypeConstants.CAS_PAYBILL);
            //电子回单
            Set<Object> casPayBillElecReceipt = getCasPayBillElecReceipt(casPaybillSet);
            relationBillMap.put(BillTypeConstants.BEI_ELECRECEIPT,casPayBillElecReceipt);
        }

        return relationBillMap;
    }


    /**
     * 查询Botp单据
     * @param billType 来源单据
     * @param destBillType 目标单据
     * @param billId 来源单据ID
     * @param isPositive 发给想
     * @return
     */
    public static Set<Object> getBotpRelationId(String billType , String destBillType , Long billId , boolean isPositive){
        Set<Long> idSet = null ;
        // 下游单据
        if(isPositive){
            Map<String, HashSet<Long>> targetBillMap = BFTrackerServiceHelper.findTargetBills( billType , new Long[] { billId });
            if(targetBillMap.containsKey(destBillType)){
                idSet = targetBillMap.get(destBillType);
            }
            // 上游单据
        } else{
            Map<String, HashSet<Long>> sourceBillMap = BFTrackerServiceHelper.findSourceBills( billType , new Long[] { billId });
            if(sourceBillMap.containsKey(destBillType)){
                idSet = sourceBillMap.get(destBillType);
            }
        }
        if(!Objects.isNull(idSet)){
            return QueryServiceHelper.query(destBillType , "id" , new QFilter("id" , QCP.in , idSet).toArray())
                    .stream()
                    .map(r -> r.get("id"))
                    .collect(Collectors.toSet());
        }
        return null ;
    }


    /**
     * 获取付款处理电子回单信息
     * @param casPayBillIdList
     * @return
     */
    public static Set<Object> getCasPayBillElecReceipt(Set<Object> casPayBillIdList){
        Set<Object> elecReceiptSet = new HashSet<Object>();
        if(CollectionUtils.isEmpty(casPayBillIdList)){
            return elecReceiptSet;
        }

        // 查询付款处理关联的交易明细ID
        Set<String> transDetailIds = QueryServiceHelper.query("cas_paybill", "detailseqid",
                        new QFilter("id", QCP.in, casPayBillIdList).toArray())
                .stream()
                .filter(r -> StringUtils.isNotEmpty(r.getString("detailseqid")))
                .map(r -> r.getString("detailseqid"))
                .collect(Collectors.toSet());

        if(CollectionUtils.isEmpty(transDetailIds)){
            return elecReceiptSet;
        }

        //查询交易明细关联的电子回单号
        Set<String> receiptNoSet = QueryServiceHelper.query("bei_transdetail", "receiptno",
                        new QFilter("billno", QCP.in, transDetailIds).toArray())
                .stream()
                .filter(r -> StringUtils.isNotEmpty(r.getString("receiptno")))
                .map(r -> r.getString("receiptno"))
                .collect(Collectors.toSet());
        if(CollectionUtils.isEmpty(receiptNoSet)){
            return elecReceiptSet;
        }
        return QueryServiceHelper.query("bei_elecreceipt", "id,receiptno",
                        new QFilter("receiptno", QCP.in, receiptNoSet).toArray())
                .stream()
                .map(r -> r.get("id"))
                .collect(Collectors.toSet());
    }
}
