| 123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230 |
- import { toRefs, computed, reactive } from 'vue'
- import { isMatch } from 'lodash'
- import { queryTaAccounts, getTodayAccountConfigInfo } from '@/services/api/account'
- import { defineStore } from '../store'
- import { useEnumStore } from './enum'
- import { useLoginStore } from './login'
- import { useUserStore } from './user'
- import { usePositionStore } from './position'
- import eventBus from '@/services/bus'
- /**
- * 账号存储对象
- * @returns
- */
- export const useAccountStore = defineStore(() => {
- const enumStore = useEnumStore()
- const loginStore = useLoginStore()
- const userStore = useUserStore()
- const positionStore = usePositionStore()
- const state = reactive<{
- loading: boolean;
- accountList: Model.TaAccountsRsp[];
- currentAccountId: number;
- currentAccountConfig: Model.TodayAccountConfigInfoRsp;
- }>({
- loading: false,
- accountList: [],
- currentAccountId: 0,
- currentAccountConfig: {
- todayAccountMargins: [],
- todayAccountTradeRules: [],
- todayAccountTradefees: []
- }
- })
- // 资金账户计算列表
- const accountComputedList = computed(() => {
- const { riskRatioType } = state.currentAccountConfig
- const { addmarginriskratio = 0, notemarginriskratio = 0, cutriskratio = 0 } = riskRatioType ?? {}
- const result: (Model.TaAccountsRsp & {
- freezeMargin: number; // 冻结资金
- avaiableBalance: number; // 可用资金
- netvalue: number; // 净值
- profitLoss: number; // 浮动盈亏
- hazardValue: number; // 风险净值
- hazardRatio: number; // 风险率
- liquidationRate: number; // 斩仓率
- hazardRatioColor: string; // 风险率颜色
- fractionDigits: number; // 币种小数位
- })[] = []
- state.accountList.forEach((item) => {
- // 账号持仓列表
- const positionList = positionStore.positionComputedList.filter((e) => e.accountid === item.accountid)
- // 计算浮动盈亏
- const profitLoss = positionList.reduce((pre, cur) => cur.tradeproperty === 1 ? pre += cur.profitLoss : pre, 0)
- // 计算市值(所有权)
- 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 avaiableBalance = 0
- // *系统参数”113“(当日浮动盈利是否可用) 0:不可用 1:可用
- const param113 = userStore.getSystemParamValue('113')
- if (profitLoss < 0 || param113 === '1') {
- // 账户总浮动盈亏为负:= 期末余额+总浮动盈亏-占用-冻结资金
- avaiableBalance = item.currentbalance + profitLoss - item.usedmargin - freezeMargin
- } else {
- // 账户总浮动盈亏为正 且 113 = 0 :=期末余额-占用-冻结资金
- avaiableBalance = 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
- // 币种小数位
- const currency = enumStore.getEnumTypeInfo('currency', item.currencyid)
- const fractionDigits = currency ? Number(currency.param1) : 2
- result.push({
- ...item,
- freezeMargin,
- avaiableBalance,
- netvalue,
- profitLoss,
- hazardValue,
- hazardRatio,
- hazardRatioColor,
- liquidationRate,
- fractionDigits
- })
- })
- return result
- })
- // 当前资金账户信息
- const currentAccount = computed(() => {
- return {
- ...accountComputedList.value.find((e) => e.accountid === state.currentAccountId)
- }
- })
- const filterAccountsByCurrencyId = (...currencyIds: number[]) => {
- return accountComputedList.value.filter((e) => currencyIds.includes(e.currencyid))
- }
- const getAccountItem = (prop: Partial<Model.TaAccountsRsp>) => {
- return accountComputedList.value.find(item => isMatch(item, prop))
- }
- // 获取资金账户列表
- const getAccountList = async () => {
- try {
- state.loading = true
- await queryTaAccounts({
- data: {
- loginID: loginStore.loginId
- }
- }).then((res) => {
- state.accountList = res.data
- // 查找当前选中的资金账户
- if (!res.data.every((e) => e.accountid === state.currentAccountId)) {
- state.currentAccountId = res.data[0]?.accountid ?? 0
- }
- })
- // 任务 #5511
- await 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<Proto.MarginInfoStruct>('MarginInfoStruct', uint8Array) // proto数据解析
- // }
- // return Promise.reject('商品配置不存在')
- // }
- // 接收资金变动通知
- const moneyChangedNotify = eventBus.$on('MoneyChangedNotify', () => getAccountList())
- return {
- ...toRefs(state),
- accountComputedList,
- currentAccount,
- moneyChangedNotify,
- getAccountList,
- getAccountItem,
- filterAccountsByCurrencyId
- }
- })
|