import { toRefs, computed, reactive } from 'vue' import { queryTaAccounts, getTodayAccountConfigInfo } from '@/services/api/account' import { defineStore } from '../store' import { useLoginStore } from './login' import { useUserStore } from './user' import { usePositionStore, useSBYJOrderStore } from './position' import eventBus from '@/services/bus' /** * 账号存储对象 * @returns */ export const useAccountStore = defineStore(() => { const loginStore = useLoginStore() const userStore = useUserStore() const positionStore = usePositionStore() const sbyjOrderStore = useSBYJOrderStore() const state = reactive<{ loading: boolean; accountList: Model.TaAccountsRsp[]; currentAccountId: number; currentAccountConfig?: Model.TodayAccountConfigInfoRsp; }>({ loading: false, accountList: [], currentAccountId: 0 }) // 资金账户计算列表 const accountComputedList = computed(() => { const { riskRatioType } = state.currentAccountConfig ?? {} const { addmarginriskratio = 0, notemarginriskratio = 0, cutriskratio = 0 } = riskRatioType ?? {} const result: (Model.TaAccountsRsp & { freezeMargin: number; // 冻结资金 avaiableMoney: number; // 可用资金 netvalue: number; // 净值 profitLoss: number; // 浮动盈亏 hazardValue: number; // 风险净值 hazardRatio: number; // 风险率 liquidationRate: number; // 斩仓率 hazardRatioColor: string; // 风险率颜色 })[] = [] state.accountList.forEach((item) => { // 账号持仓列表 const positionList = positionStore.positionComputedList.filter((e) => e.accountid === item.accountid) // 计算浮动盈亏 const profitLoss1 = positionList.reduce((pre, cur) => cur.tradeproperty === 1 ? pre += cur.closepl : pre, 0) // 水贝亿爵订单列表 const orderList = sbyjOrderStore.orderComputedList.filter((e) => e.tHDetailEx.accountID === item.accountid) // 计算浮动盈亏 const profitLoss2 = orderList.reduce((pre, cur) => pre += cur.tHDetailEx.floatPL, 0) // 计算总浮动盈亏 const profitLoss = profitLoss1 + profitLoss2 // 计算市值(所有权) const marketValue = positionList.reduce((pre, cur) => cur.tradeproperty === 2 ? pre += cur.marketValue : pre, 0) // 计算权益/净值 // 根据系统参数“307 账户净值是否减冻结资金 - 0:不减 1:减“ // 0.净值=期末余额+市值+浮动盈亏(收益权) // 1.净值=期末余额+市值+浮动盈亏(收益权)-其他冻结-出金冻结 const param307 = userStore.getSystemParamValue('307') const free = param307 === '1' ? item.otherfreezemargin + item.outamountfreeze : 0 // 冻结资金 const netvalue = item.currentbalance + profitLoss + marketValue - free // 计算冻结资金 = 冻结+其它冻结+手续费冻结+出金冻结 const freezeMargin = item.freezemargin + item.otherfreezemargin + item.freezecharge + item.outamountfreeze // 计算可用资金 let avaiableMoney = 0 // *系统参数”113“(当日浮动盈利是否可用) 0:不可用 1:可用 const param113 = userStore.getSystemParamValue('113') if (profitLoss < 0 || param113 === '1') { // 账户总浮动盈亏为负:= 期末余额+总浮动盈亏-占用-冻结资金 avaiableMoney = item.currentbalance + profitLoss - item.usedmargin - freezeMargin } else { // 账户总浮动盈亏为正 且 113 = 0 :=期末余额-占用-冻结资金 avaiableMoney = item.currentbalance - item.usedmargin - freezeMargin } // 计算风险净值 = 期末余额 + 浮动盈亏(收益权) - 其他冻结 - 出金冻结 let hazardValue = item.currentbalance + profitLoss - item.otherfreezemargin - item.outamountfreeze // 根据系统参数“087 风险净值是否加上市值 - 0:不加 1:加“ const param087 = userStore.getSystemParamValue('087') if (param087 === '1') { // 风险净值 = 期末余额 + 市值(所有权) + 浮动盈亏(收益权) - 其他冻结 - 出金冻结 hazardValue += marketValue } // 计算风险率 let hazardRatio = 0 if (hazardValue < 0 || (hazardValue === 0 && item.usedmargin > 0)) { hazardRatio = 100 } if (hazardValue > 0 && item.usedmargin > 0) { // 根据系统参数“132 风险率计算公式” const param132 = userStore.getSystemParamValue('132') if (param132 === '1') { // 风险率 = 占用 / 风险净值 hazardRatio = item.usedmargin / hazardValue } else { // 风险率 = (占用 - 授信金额) / (风险净值 - 授信金额) hazardRatio = (item.usedmargin - item.mortgagecredit) / (hazardValue - item.mortgagecredit) } } // 风险率颜色 let hazardRatioColor = '' if (hazardRatio < notemarginriskratio) { hazardRatioColor = 'g-risk-green' } if (hazardRatio >= notemarginriskratio && hazardRatio < addmarginriskratio) { hazardRatioColor = 'g-risk-orange' } if (hazardRatio >= addmarginriskratio && hazardRatio < cutriskratio) { hazardRatioColor = 'g-risk-yellow' } if (hazardRatio >= cutriskratio && cutriskratio > 0) { hazardRatioColor = 'g-risk-red' } // 计算斩仓率 任务 #5808 const liquidationRate = cutriskratio ? hazardRatio / cutriskratio : 0 result.push({ ...item, freezeMargin, avaiableMoney, netvalue, profitLoss, hazardValue, hazardRatio, hazardRatioColor, liquidationRate }) }) return result }) // 当前资金账户信息 const currentAccount = computed(() => { return { ...accountComputedList.value.find((e) => e.accountid === state.currentAccountId) } }) // 获取资金账户列表 const getAccountList = async () => { try { state.loading = true const res = await queryTaAccounts({ data: { loginID: loginStore.loginId } }) const data = res.data state.accountList = data // 查找当前选中的资金账户 if (!data.every((e) => e.accountid === state.currentAccountId)) { state.currentAccountId = data[0]?.accountid ?? 0 } // 任务 #5511 if (!state.currentAccountConfig) { getTodayAccountConfigInfo().then((res) => { state.currentAccountConfig = res.data }) } } finally { state.loading = false } } // 获取账户商品配置 // const getAccountGoodsConfig = (goodsId: number) => { // const config = state.currentAccountConfig?.todayAccountMargins.find((item, index, self) => { // // 优先返回账户的商品配置 // if (item.accountid === state.currentAccountId && item.goodsid === goodsId) { // return true // } // // 返回通用的商品配置 // if (!self.some((e) => e.accountid === state.currentAccountId) && item.goodsid === goodsId) { // return true // } // return false // }) // if (config) { // const wordArray = CryptoJS.enc.Base64.parse(config.infocontent) // 解析base64 // const uint8Array = wordArrayToUint8Array(wordArray) // return decodeProto('MarginInfoStruct', uint8Array) // proto数据解析 // } // return Promise.reject('商品配置不存在') // } // 接收资金变动通知 const moneyChangedNotify = eventBus.$on('MoneyChangedNotify', () => getAccountList()) return { ...toRefs(state), accountComputedList, currentAccount, moneyChangedNotify, getAccountList } })