import { BuyOrSell } from "@/common/constants/enumCommon";
import { getQuoteDayInfoByCodeFindPrice } from "@/services/bus/goods";
import { QueryTradeHolderDetailRsp } from "@/services/go/order/interface";

// 计算盈亏
// 浮动盈亏	持仓单：
// 收益权=(最新价-持仓价)*持仓数量*合约单位*方向(买[1]:卖[-1])(*汇率)
// 所有权=(最新价*持仓数量*合约单位(*汇率) - 持仓金额)
export function useProfit(item: QueryTradeHolderDetailRsp, goodcode: string) {
    // 最新价
    const lastPrice = getQuoteDayInfoByCodeFindPrice(goodcode);
    if (lastPrice && lastPrice !== '--') {
        const { holderprice, holderqty, agreeunit, decimalplace, buyorsell } = item
        const temp = buyorsell === BuyOrSell.buy ? 1 : -1
        return ((+lastPrice - holderprice) * holderqty * agreeunit * temp).toFixed(decimalplace)
    } else {
        return lastPrice
    }
}
