/** ================================= 仓单贸易v2 ================================**/
/* 更新全部接口 20210803 */
import APP from '@/services';
import { getSelectedAccountId } from "@/services/bus/account";
import { getUsrId } from "@/services/bus/user";
import { commonSearch_go } from "@/services/go";
import {
    QueryFaProductDetailReq,
    QueryFilterItemReq,
    QueryHoldLBReq,
    QueryOrderQuoteDetailReq,
    QueryOrderQuoteMyqQsp,
    QueryOrderQuoteMyqQsq,
    QueryOrderQuoteReq,
    QueryPerformancePlanReq,
    QueryWrBuybackDetailReq,
    QueryWrDeliveryAvalidHoldLBReq,
    QueryWrFactorTypeInfoReq,
    QueryWrGoodsInfoReq,
    QueryWrMarketTradeConfigReq,
    QueryWrOrderDetailReq,
    QueryWrPerformancePlanStepReq,
    QueryWrPositionReq,
    QueryWrScfContractInterestReq,
    QueryWrSpecialMatchOrderReq,
    QueryWrStandardFactoryItemReq,
    QueryWrTradeDetailReq,
    WrAverageTradePriceQsp,
    WrAverageTradePriceQsq,
    WrBsGoodsInfo,
    WrBuybackDetail,
    WrDeliveryAvalidHoldLB,
    WrDeliveryDetail,
    WrFactorTypeInfo,
    WrFAProductDetail,
    WrFilterItem,
    WrFinanceBuyApply,
    WrFtDeliveryGoods,
    WrGoodsInfo,
    WrHoldLB,
    WrOrderDetail,
    WrOrderQuote,
    WrOrderQuoteDetail,
    WrOutInApply,
    WrPerformancePlan,
    WrPerformancePlanStep,
    WrPosition,
    WrScfContract,
    WrScfContractInterest,
    WrSpecialMatchOrder,
    WrStandardFactoryItemEx,
    WrTradeDetail,
    WrTradeOrderDetailReq,
    WrTradeOrderDetailRsp
} from "@/services/go/wrtrade/interface";

/**
 * 查询融资产品列表 /WrTrade2/QueryFaProductDetail
 */
export function queryFaProductDetail(req: QueryFaProductDetailReq): Promise<WrFAProductDetail[]> {
    return commonSearch_go('/WrTrade2/QueryFaProductDetail', req).catch((err) => {
        throw new Error(`查询融资产品列表: ${err}`);
    });
}

/**
 * 查询筛选框列表  /WrTrade2/QueryFilterItem
 * 页面 - 一口价挂牌 - 上方品类 ｜ 仓库 下拉框数据获取
 * @param req
 */
export function queryFilterItem(req: QueryFilterItemReq): Promise<WrFilterItem[]> {
    return commonSearch_go('/WrTrade2/QueryFilterItem', req).catch((err) => {
        throw new Error(`查询筛选框列表: ${err}`);
    });
}

/**
 * 查询品种品类筛选框列表 /WrTrade2/QueryFtDeliveryGoods
 * @param req
 */
export function queryFtDeliveryGoods(): Promise<WrFtDeliveryGoods[]> {
    return commonSearch_go('/WrTrade2/QueryFtDeliveryGoods', {}).catch((err) => {
        throw new Error(`查询品种品类筛选框列表: ${err}`);
    });
}

/**
 * 查询仓单持有记录 /WrTrade2/QueryHoldLB
 * 页面 现货仓单 -> 现货明细
 * @param req
 */
export function queryHoldLB(): Promise<WrHoldLB[]> {
    const accountid = getSelectedAccountId()
    return commonSearch_go('/WrTrade2/QueryHoldLB', { accountid }).catch((err) => {
        throw new Error(`查询仓单持有记录: ${err}`);
    });
}

/**
 * 页面 现货仓单->现货明细? , 表名叫仓单持有记录， 有挂单特有维度，为防误解汇总也有相关维度， 新写接口，不跟汇总合在一起了。
 * 查询仓单持有记录 /WrTrade2/QueryHoldLB
 * 页面 现货仓单 -> 现货明细
 * @param req
 */
export function queryHoldLBFilter(req: QueryHoldLBReq): Promise<WrHoldLB[]> {
    const accountid = getSelectedAccountId()
    return commonSearch_go('/WrTrade2/QueryHoldLB', req).catch((err) => {
        throw new Error(`查询仓单持有记录: ${err}`);
    });
}

/**
 * 查询大厅行情列表 /WrTrade2/QueryOrderQuote
 * @param req
 */
export function queryOrderQuote(req: QueryOrderQuoteReq): Promise<WrOrderQuote[]> {
    return commonSearch_go('/WrTrade2/QueryOrderQuote', req).catch((err) => {
        throw new Error(`查询大厅行情列表: ${err}`);
    });
}

/**
 * 查询买卖大厅 /WrTrade2/QueryOrderQuoteDetail
 * @param req
 */
export function queryOrderQuoteDetail(req: QueryOrderQuoteDetailReq): Promise<WrOrderQuoteDetail[]> {
    return commonSearch_go('/WrTrade2/QueryOrderQuoteDetail', req).catch((err) => {
        throw new Error(`查询买卖大厅: ${err}`);
    });
}

/**
 * 查询履约信息 /WrTrade2/QueryPerformancePlan
 */
export function QueryPerformancePlan(req: QueryPerformancePlanReq): Promise<WrPerformancePlan[]> {
    req.userid = getUsrId();
    return commonSearch_go('/WrTrade2/QueryPerformancePlan', req).catch((err) => {
        throw new Error(`查询履约信息: ${err}`);
    });
}

/**
 * 查询仓单要素详细信息 /WrTrade2/QueryWrFactorTypeInfo
 * @param req
 * @constructor
 */
export function queryWrFactorTypeInfo(req: QueryWrFactorTypeInfoReq): Promise<WrFactorTypeInfo[]> {
    return commonSearch_go('/WrTrade2/QueryWrFactorTypeInfo', req).catch((err) => {
        throw new Error(`查询仓单要素详细信息: ${err}`);
    });
}

/**
 * 查询仓单商品信息 /WrTrade2/QueryWrGoodsInfo
 * @param req
 */
export function queryWrGoodsInfo(req: QueryWrGoodsInfoReq): Promise<WrGoodsInfo[]> {
    return commonSearch_go('/WrTrade2/QueryWrGoodsInfo', req).catch((err) => {
        throw new Error(`查询仓单商品信息: ${err}`);
    });
}

/**
 * 查询合约交易买卖大厅详情 /WrTrade2/QueryWrBsGoodsInfo
 * @param req
 */
export function queryWrBsGoodsInfo(goodsid: number): Promise<WrBsGoodsInfo[]> {
    return commonSearch_go('/WrTrade2/QueryWrBsGoodsInfo', { goodsid }).catch((err) => {
        throw new Error(`查询合约交易买卖大厅详情: ${err}`);
    });
}

/**
 * 查询市场交易参数配置 /WrTrade2/QueryWrMarketTradeConfig
 * @param req
 */
export function queryWrMarketTradeConfig(req: QueryWrMarketTradeConfigReq): Promise<string> {
    return commonSearch_go('/WrTrade2/QueryWrMarketTradeConfig', req).then(res => {
        APP.set('rules', res)
        return 'ok'
    }).catch((err) => {
        throw new Error(`查询市场交易参数配置: ${err}`);
    });
}

/**
 *  页面 现货仓单 | 预售仓单 -> 挂单
 *  仓单头寸
 *  查询仓单委托单(挂单) /WrTrade2/QueryWrOrderDetail
 * @param req
 */
export function queryWrOrderDetail(req: QueryWrOrderDetailReq): Promise<WrOrderDetail[]> {
    req.userid = getUsrId();
    return commonSearch_go('/WrTrade2/QueryWrOrderDetail', req).catch((err) => {
        throw new Error(`查询仓单委托单(挂单): ${err}`);
    });
}

/**
 * 页面 现货仓单->现货汇总/库存汇总/现货明细 ; 预售仓单->订单汇总
 * 查询现货(预售)仓单持仓 /WrTrade2/QueryWrPosition
 */
export function queryWrPosition(req: QueryWrPositionReq): Promise<WrPosition[]> {
    req.userid = getUsrId();
    return commonSearch_go('/WrTrade2/QueryWrPosition', req).catch((err) => {
        throw new Error(`查询现货(预售)仓单持仓: ${err}`);
    });
}

/**
 * 查询指定成交 /WrTrade2/QueryWrSpecialMatchOrder
 * 页面 现货仓单 | 预售仓单 -> 指定成交
 */
export function queryWrSpecialMatchOrder(req: QueryWrSpecialMatchOrderReq): Promise<WrSpecialMatchOrder[]> {
    req.userid = getUsrId();
    return commonSearch_go('/WrTrade2/QueryWrSpecialMatchOrder', req).catch((err) => {
        throw new Error(`查询指定成交: ${err}`);
    });
}

/**
 * 查询品类要素定义项 /WrTrade2/QueryWrStandardFactoryItem
 * @param req
 */
export function queryWrStandardFactoryItem(req: QueryWrStandardFactoryItemReq): Promise<WrStandardFactoryItemEx[]> {
    return commonSearch_go('/WrTrade2/QueryWrStandardFactoryItem', req).catch((err) => {
        throw new Error(`查询品类要素定义项: ${err}`);
    });
}

/**
 * 查询仓单成交明细 /WrTrade2/QueryWrTradeDetail
 */
export function queryWrTradeDetail(req: QueryWrTradeDetailReq): Promise<WrTradeDetail[]> {
    req.userid = getUsrId();
    return commonSearch_go('/WrTrade2/QueryWrTradeDetail', req).catch((err) => {
        throw new Error(`查询仓单成交明细: ${err}`);
    });
}

/**
 * 查询履约信息详情 /WrTrade2/QueryWrPerformancePlanStep
 */
export function queryWrPerformancePlanStep(req: QueryWrPerformancePlanStepReq): Promise<WrPerformancePlanStep[]> {
    return commonSearch_go('/WrTrade2/QueryWrPerformancePlanStep', req).catch((err) => {
        throw new Error(`查询履约信息详情: ${err}`);
    });
}

/**
 * 查询仓单贸易融资合同  /WrTrade2/QueryWrScfContract  融资管理 - 合同
 */
export function queryWrScfContract(scfcontractid?: number): Promise<WrScfContract[]> {
    const userid = getUsrId()
    const param = scfcontractid ? { scfcontractid, userid } : { userid }
    return commonSearch_go('/WrTrade2/QueryWrScfContract', param).catch((err) => {
        throw new Error(`查询仓单贸易融资合同: ${err}`);
    });
}

/**
 * 查询融资购买申请 /WrTrade2/QueryWrFinanceBuyApply 融资管理 - 申请单
 */
export function queryWrFinanceBuyApply(): Promise<WrFinanceBuyApply[]> {
    const userid = getUsrId()
    return commonSearch_go('/WrTrade2/QueryWrFinanceBuyApply', { userid }).catch((err) => {
        throw new Error(`查询融资购买申请: ${err}`);
    });
}

/**
 * 查询仓单回购明细 /WrTrade2/QueryWrBuybackDetail
 */
export function queryWrBuybackDetail(req: QueryWrBuybackDetailReq): Promise<WrBuybackDetail[]> {
    return commonSearch_go('/WrTrade2/QueryWrBuybackDetail', req).catch((err) => {
        throw new Error(`查询仓单回购明细: ${err}`);
    });
}

/**
 *查询融资合同费用明细 /WrTrade2/QueryWrScfContractInterest
 */
export function queryWrScfContractInterest(req: QueryWrScfContractInterestReq): Promise<WrScfContractInterest[]> {
    return commonSearch_go('/WrTrade2/QueryWrScfContractInterest', req).catch((err) => {
        throw new Error(`查询融资合同费用明细: ${err}`);
    });
}

/**
 * 查询提货申请 /WrTrade2/QueryWrOutInApply
 */
export function queryWrOutInApply(): Promise<WrOutInApply[]> {
    const userid = getUsrId()
    return commonSearch_go('/WrTrade2/QueryWrOutInApply', { userid }).catch((err) => {
        throw new Error(`查询提货申请: ${err}`);
    });
}

/**
 * 查询交收 /WrTrade2/QueryWrDeliveryDetail
 * 资金账号id
 */
export function queryWrDeliveryDetail(): Promise<WrDeliveryDetail[]> {
    const accountid = getSelectedAccountId()
    const userid = getUsrId()
    return commonSearch_go('/WrTrade2/QueryWrDeliveryDetail', { userid, accountid }).catch((err) => {
        throw new Error(`查询交收: ${err}`);
    });
}
/**
 * 查询合约交易买卖大厅 /WrTrade2/QueryWrTradeOrderDetail
 * @param WrTradeOrderDetailReq
 */
export function queryWrTradeOrderDetail(param: WrTradeOrderDetailReq): Promise<WrTradeOrderDetailRsp[]> {
    return commonSearch_go('/WrTrade2/QueryWrTradeOrderDetail', param).catch((err) => {
        throw new Error(`查询合约交易买卖大厅: ${err}`);
    });
}
/**
 * 询成交价(历史走势)WrTrade2/QueryWrAverageTradePrice
 * @param WrTradeOrderDetailReq
 */
export function queryWrAverageTradePrice(param: WrAverageTradePriceQsq): Promise<WrAverageTradePriceQsp[]> {
    return commonSearch_go('/WrTrade2/QueryWrAverageTradePrice', param).catch((err) => {
        throw new Error(`询成交价(历史走势): ${err}`);
    });
}

/**
 * /WrTrade2/QueryWrDeliveryAvalidHoldLB 查询合约交收可点选仓单
 * @param QueryWrDeliveryAvalidHoldLBReq
 */
export function queryWrDeliveryAvalidHoldLB(param: QueryWrDeliveryAvalidHoldLBReq): Promise<WrDeliveryAvalidHoldLB[]> {
    return commonSearch_go('/WrTrade2/QueryWrDeliveryAvalidHoldLB', param).catch((err) => {
        throw new Error(`查询合约交收可点选仓单: ${err}`);
    });
}

/**
 * /WrTrade2/QueryOrderQuoteMyq 查询贸易圈挂牌大厅
 * @param QueryOrderQuoteMyq
 */
export function queryQueryOrderQuoteMyq(param: QueryOrderQuoteMyqQsq): Promise<QueryOrderQuoteMyqQsp[]> {
    if (!param.userid) {
        param.userid = getUsrId()
    }
    return commonSearch_go('/WrTrade2/QueryOrderQuoteMyq', param).catch((err) => {
        throw new Error(`查询贸易圈挂牌大厅: ${err}`);
    });
}

