package com.brilliance.isc.common.whj.service.impl;

import com.brilliance.isc.bo.Cus;
import com.brilliance.isc.bo.Ine;
import com.brilliance.isc.common.esb.finance.IFinanceFeign;
import com.brilliance.isc.common.util.Utils;
import com.brilliance.isc.common.whj.service.WhjBlcInterService;
import com.brilliance.isc.esb.model.inner.InnerReq;
import com.brilliance.isc.esb.model.inner.InnerReqWhj;
import com.brilliance.isc.esb.model.inner.InnerRes;
import com.brilliance.isc.esb.model.inner.InnerResWhj;
import com.brilliance.isc.esb.model.whj.ReqMsgData;
import com.brilliance.isc.esb.model.whj.content.export.*;
import com.brilliance.isc.vo.finance.BlcStoreVo;
import com.brilliance.mda.runtime.mda.util.MdaUtils;
import com.brilliance.mda.runtime.mda.util.Strings;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

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

@Service
public class WhjBlcInterServiceImpl implements WhjBlcInterService {

    @Autowired(required = false)
    private IFinanceFeign financeFeign;

    /*
    融资申请接口
     */
    @Override
    public InnerRes bankApply(BlcStoreVo blcStoreVo) {
        BankApplyContent bankApplyContent = new BankApplyContent();
        bankApplyContent.setAmount(Utils.fmtAmtToWgLong(blcStoreVo.getRecgrp().getRec().getRcvtotamt()));
        bankApplyContent.setApplyAmount(Utils.fmtAmtToWgLong(blcStoreVo.getRecgrp().getRec().getAmt()));
        bankApplyContent.setApplyEntCode(blcStoreVo.getRecgrp().getRec().getCustcode());
        bankApplyContent.setContractEndDate(Utils.fmtDate(blcStoreVo.getRecgrp().getRec().getConenddat(), "yyyy-MM-dd"));
        bankApplyContent.setCurrency(blcStoreVo.getRecgrp().getRec().getRcvtotcur());

        bankApplyContent.setInvoices(new ArrayList<Invoice>());
        List<Ine> inelst = blcStoreVo.getRecgrp().getInelst();
        if (inelst == null || inelst.size() == 0) {
            Invoice invoice = new Invoice();
            invoice.setImportEntName("");
            invoice.setInvoiceAmount(0L);
            invoice.setInvoiceCode("");

            bankApplyContent.getInvoices().add(invoice);

            Manifest manifest = new Manifest();
            manifest.setManifestAmount(0L);
            manifest.setManifestCode("");

            invoice.setManifests(new ArrayList<Manifest>());
            invoice.getManifests().add(manifest);
        } else {
            for (Ine ine : inelst) {
                Invoice invoice = new Invoice();
                invoice.setImportEntName(ine.getClinam());
                invoice.setInvoiceAmount(Utils.fmtAmtToWgLong(ine.getAmt()));
                invoice.setInvoiceCode(ine.getIneref());

                bankApplyContent.getInvoices().add(invoice);

                invoice.setManifests(new ArrayList<Manifest>());
                List<Cus> cuslst = blcStoreVo.getRecgrp().getCuslst();
                if (cuslst == null || cuslst.size() == 0) {
                    Manifest manifest = new Manifest();
                    manifest.setManifestAmount(0L);
                    manifest.setManifestCode("");
                    invoice.getManifests().add(manifest);
                } else {
                    for (Cus cus : cuslst) {
                        if (MdaUtils.compareTo(cus.getIneref(), ine.getIneref()) == 0) {
                            Manifest manifest = new Manifest();
                            manifest.setManifestAmount(Utils.fmtAmtToWgLong(cus.getAmt()));
                            manifest.setManifestCode(cus.getCusref());
                            invoice.getManifests().add(manifest);
                        }
                    }
                }
            }
        }

        ReqMsgData<BankApplyContent> reqMsgData = new ReqMsgData<>(bankApplyContent);
        reqMsgData.method = "/v1/asset/bank-apply";
        reqMsgData.service = "EXPORT";
        reqMsgData.bankSafeCode = "";

        InnerReqWhj innerReqWhj = new InnerReqWhj();
        innerReqWhj.serviceType = "EXPORT";
        innerReqWhj.reqMsgData = reqMsgData;

        InnerReq<InnerReqWhj> innerReqWhjInnerReq = new InnerReq<>(innerReqWhj);

        InnerRes<InnerResWhj> innerRes = financeFeign.callWhj(innerReqWhjInnerReq);

        return innerRes;
    }


    /*
    关单校验/融资校验 接口
     */
    @Override
    public InnerRes check(BlcStoreVo blcStoreVo) {
        CheckContent checkContent = new CheckContent();
        checkContent.setSafeAssetId(blcStoreVo.getRecgrp().getRec().getWgrref());

        ReqMsgData<CheckContent> reqMsgData = new ReqMsgData<>(checkContent);
        reqMsgData.method = "/v1/asset/check";
        reqMsgData.service = "EXPORT";
        reqMsgData.bankSafeCode = "";

        InnerReqWhj innerReqWhj = new InnerReqWhj();
        innerReqWhj.serviceType = "EXPORT";
        innerReqWhj.reqMsgData = reqMsgData;

        InnerReq<InnerReqWhj> innerReqWhjInnerReq = new InnerReq<>(innerReqWhj);

        InnerRes<InnerResWhj> innerRes = financeFeign.callWhj(innerReqWhjInnerReq);

        return innerRes;
    }

    /*
    融资受理拒绝 接口
     */
    @Override
    public InnerRes accept(BlcStoreVo blcStoreVo) {
        AcceptContent acceptContent = new AcceptContent();
        acceptContent.setSafeAssetId(blcStoreVo.getRecgrp().getRec().getWgrref());
        acceptContent.setReason(Strings.converttoXMLStringPublic(blcStoreVo.getRecgrp().getRec().getRfurea()));

        ReqMsgData<AcceptContent> reqMsgData = new ReqMsgData<>(acceptContent);
        reqMsgData.method = "/v1/asset/reject-accept";
        reqMsgData.service = "EXPORT";
        reqMsgData.bankSafeCode = "";

        InnerReqWhj innerReqWhj = new InnerReqWhj();
        innerReqWhj.serviceType = "EXPORT";
        innerReqWhj.reqMsgData = reqMsgData;

        InnerReq<InnerReqWhj> innerReqWhjInnerReq = new InnerReq<>(innerReqWhj);

        InnerRes<InnerResWhj> innerRes = financeFeign.callWhj(innerReqWhjInnerReq);

        return innerRes;
    }

    /*
    直接放款登记 接口
     */
    @Override
    public InnerRes directPay(BlcStoreVo blcStoreVo) {
        DirectPayContent directPayContent = new DirectPayContent();
        directPayContent.setAcceptAmount(Utils.fmtAmtToWgLong(blcStoreVo.getRecgrp().getRec().getLoaamt()));
        directPayContent.setAcceptCurrency(blcStoreVo.getRecgrp().getRec().getFinloacur());
        directPayContent.setDayLimit(Integer.parseInt(blcStoreVo.getRecgrp().getRec().getLoalimday()));
        directPayContent.setNote(Strings.converttoXMLStringPublic(blcStoreVo.getRecgrp().getRec().getRemark()));
        directPayContent.setPayAmount(Utils.fmtAmtToWgLong(blcStoreVo.getRecgrp().getRec().getFinloaamt()));
        directPayContent.setPayDate(Utils.fmtDate(blcStoreVo.getRecgrp().getRec().getLoadat(), "yyyy-MM-dd"));
        directPayContent.setRecDate(Utils.fmtDate(blcStoreVo.getRecgrp().getRec().getPrcrepdat(), "yyyy-MM-dd"));
        directPayContent.setSafeAssetId(blcStoreVo.getRecgrp().getRec().getWgrref());
        directPayContent.setReason(Strings.converttoXMLStringPublic(blcStoreVo.getRecgrp().getRec().getAbarea()));

        ReqMsgData<DirectPayContent> reqMsgData = new ReqMsgData<>(directPayContent);
        reqMsgData.method = "/v1/asset/direct-pay";
        reqMsgData.service = "EXPORT";
        reqMsgData.bankSafeCode = "";

        InnerReqWhj innerReqWhj = new InnerReqWhj();
        innerReqWhj.serviceType = "EXPORT";
        innerReqWhj.reqMsgData = reqMsgData;

        InnerReq<InnerReqWhj> innerReqWhjInnerReq = new InnerReq<>(innerReqWhj);

        InnerRes<InnerResWhj> innerRes = financeFeign.callWhj(innerReqWhjInnerReq);

        return innerRes;
    }

    /*
    多次还款登记 接口
     */
    @Override
    public InnerRes rec(BlcStoreVo blcStoreVo) {
        RecContent recContent = new RecContent();
        recContent.setSafeAssetId(blcStoreVo.getRecgrp().getRec().getWgrref());
        recContent.setRecs(new ArrayList<RecItem>());
        RecItem rec = new RecItem();
        rec.setRecAmount(Utils.fmtAmtToWgLong(blcStoreVo.getRecgrp().getRec().getRcvamt()));
        rec.setRecDate(Utils.fmtDate(blcStoreVo.getRecgrp().getRec().getFacrcvdat(), "yyyy-MM-dd"));
        recContent.getRecs().add(rec);

        ReqMsgData<RecContent> reqMsgData = new ReqMsgData<>(recContent);
        reqMsgData.method = "/v1/asset/rec";
        reqMsgData.service = "EXPORT";
        reqMsgData.bankSafeCode = "";

        InnerReqWhj innerReqWhj = new InnerReqWhj();
        innerReqWhj.serviceType = "EXPORT";
        innerReqWhj.reqMsgData = reqMsgData;

        InnerReq<InnerReqWhj> innerReqWhjInnerReq = new InnerReq<>(innerReqWhj);

        InnerRes<InnerResWhj> innerRes = financeFeign.callWhj(innerReqWhjInnerReq);

        return innerRes;
    }

    /*
    一次还款登记 接口
     */
    @Override
    public InnerRes recOnce(BlcStoreVo blcStoreVo) {
        RecOnceContent recOnceContent = new RecOnceContent();
        recOnceContent.setSafeAssetId(blcStoreVo.getRecgrp().getRec().getWgrref());
        recOnceContent.setRecDate(Utils.fmtDate(blcStoreVo.getRecgrp().getRec().getFacrcvdat(), "yyyy-MM-dd"));

        ReqMsgData<RecOnceContent> reqMsgData = new ReqMsgData<>(recOnceContent);
        reqMsgData.method = "/v1/asset/recOnce";
        reqMsgData.service = "EXPORT";
        reqMsgData.bankSafeCode = "";

        InnerReqWhj innerReqWhj = new InnerReqWhj();
        innerReqWhj.serviceType = "EXPORT";
        innerReqWhj.reqMsgData = reqMsgData;

        InnerReq<InnerReqWhj> innerReqWhjInnerReq = new InnerReq<>(innerReqWhj);

        InnerRes<InnerResWhj> innerRes = financeFeign.callWhj(innerReqWhjInnerReq);

        return innerRes;
    }

    /*
    上链失败重试 接口
     */
    @Override
    public InnerRes retry(BlcStoreVo blcStoreVo) {
        CheckContent checkContent = new CheckContent();
        checkContent.setSafeAssetId(blcStoreVo.getRecgrp().getRec().getWgrref());

        ReqMsgData<CheckContent> reqMsgData = new ReqMsgData<>(checkContent);
        reqMsgData.method = "/v1/asset/retry";
        reqMsgData.service = "EXPORT";
        reqMsgData.bankSafeCode = "";

        InnerReqWhj innerReqWhj = new InnerReqWhj();
        innerReqWhj.serviceType = "EXPORT";
        innerReqWhj.reqMsgData = reqMsgData;

        InnerReq<InnerReqWhj> innerReqWhjInnerReq = new InnerReq<>(innerReqWhj);

        InnerRes<InnerResWhj> innerRes = financeFeign.callWhj(innerReqWhjInnerReq);

        return innerRes;
    }

    /*
    放款冲正 接口
     */
    @Override
    public InnerRes correct(BlcStoreVo blcStoreVo) {
        AcceptContent acceptContent = new AcceptContent();
        acceptContent.setSafeAssetId(blcStoreVo.getRecgrp().getRec().getWgrref());
        acceptContent.setReason(Strings.converttoXMLStringPublic(blcStoreVo.getRecgrp().getRec().getRfurea()));

        ReqMsgData<AcceptContent> reqMsgData = new ReqMsgData<>(acceptContent);
        reqMsgData.method = "/v1/asset/correct";
        reqMsgData.service = "EXPORT";
        reqMsgData.bankSafeCode = "";

        InnerReqWhj innerReqWhj = new InnerReqWhj();
        innerReqWhj.serviceType = "EXPORT";
        innerReqWhj.reqMsgData = reqMsgData;

        InnerReq<InnerReqWhj> innerReqWhjInnerReq = new InnerReq<>(innerReqWhj);

        InnerRes<InnerResWhj> innerRes = financeFeign.callWhj(innerReqWhjInnerReq);

        return innerRes;
    }

    /*
    申请废弃 接口
     */
    @Override
    public InnerRes abandon(BlcStoreVo blcStoreVo) {
        AcceptContent acceptContent = new AcceptContent();
        acceptContent.setSafeAssetId(blcStoreVo.getRecgrp().getRec().getWgrref());
        acceptContent.setReason(Strings.converttoXMLStringPublic(blcStoreVo.getRecgrp().getRec().getRfurea()));

        ReqMsgData<AcceptContent> reqMsgData = new ReqMsgData<>(acceptContent);
        reqMsgData.method = "/v1/asset/abandon";
        reqMsgData.service = "EXPORT";
        reqMsgData.bankSafeCode = "";

        InnerReqWhj innerReqWhj = new InnerReqWhj();
        innerReqWhj.serviceType = "EXPORT";
        innerReqWhj.reqMsgData = reqMsgData;

        InnerReq<InnerReqWhj> innerReqWhjInnerReq = new InnerReq<>(innerReqWhj);

        InnerRes<InnerResWhj> innerRes = financeFeign.callWhj(innerReqWhjInnerReq);

        return innerRes;
    }

    /*
    详情查询 接口
     */
    @Override
    public InnerRes detail(BlcStoreVo blcStoreVo) {
        CheckContent checkContent = new CheckContent();
        checkContent.setSafeAssetId(blcStoreVo.getRecgrp().getRec().getWgrref());

        ReqMsgData<CheckContent> reqMsgData = new ReqMsgData<>(checkContent);
        reqMsgData.method = "/v1/asset/detail";
        reqMsgData.service = "EXPORT";
        reqMsgData.bankSafeCode = "";

        InnerReqWhj innerReqWhj = new InnerReqWhj();
        innerReqWhj.serviceType = "EXPORT";
        innerReqWhj.reqMsgData = reqMsgData;

        InnerReq<InnerReqWhj> innerReqWhjInnerReq = new InnerReq<>(innerReqWhj);

        InnerRes<InnerResWhj> innerRes = financeFeign.callWhj(innerReqWhjInnerReq);

        return innerRes;
    }
}
