fix ذشزنلقخعدی

This commit is contained in:
2026-06-23 16:02:41 +03:30
parent 29c58c4202
commit ac28eda09f
29 changed files with 624 additions and 167 deletions
@@ -4,10 +4,10 @@ import type { Product } from '@/models/products'
import { getProductsCatalog, getProductsCatalogRef } from '@/composables/useProductsCatalog'
import { getHomeLayoutId } from '../layouts/registry'
import { getProductBuyPrice, resolvePriceOffsetRatio } from '@/utils/productPrice'
import { isKatodMesGroup, isMaftolMesGroup } from '@/utils/productDealMethod'
import { isKatodMesGroup, isMaftolMesGroup, isShamshMesGroup } from '@/utils/productDealMethod'
import { getTradeBuyPrice, getTradeSellPrice, lastPrice } from '@/utils/price'
export type ChartMetalType = 'katod' | 'maftol'
export type ChartMetalType = 'katod' | 'maftol' | 'shamsh'
type ChartPoint = [number, number]
@@ -25,6 +25,7 @@ function resolveChartProducts(products: Product[]) {
return {
katod: findKatodProduct(products),
maftol: findMaftolProduct(products),
shamsh: findShamshProduct(products),
}
}
@@ -34,17 +35,38 @@ const maftolChartRatio = computed(() => {
return 0
}
// lastPrice را track کن تا بعد از polling نسبت به‌روز شود
void lastPrice.value?.price_last
const { katod, maftol } = resolveChartProducts(catalogProducts.value)
return deriveMaftolChartRatio(maftol, katod)
return deriveMetalChartRatio(maftol, katod)
})
/** نسبت شمش مس به کاتد */
const shamshChartRatio = computed(() => {
if (getHomeLayoutId() !== 'boursecu') {
return 0
}
void lastPrice.value?.price_last
const { katod, shamsh } = resolveChartProducts(catalogProducts.value)
return deriveMetalChartRatio(shamsh, katod)
})
const selectedMetalChartRatio = computed(() => {
if (selectedChartMetal.value === 'maftol') {
return maftolChartRatio.value
}
if (selectedChartMetal.value === 'shamsh') {
return shamshChartRatio.value
}
return 0
})
let chartFetchPromise: Promise<void> | null = null
let chartInitialized = false
let maftolOffsetPromise: Promise<void> | null = null
let maftolOffsetLoaded = false
let metalOffsetPromise: Promise<void> | null = null
let metalOffsetLoaded = false
function findKatodProduct(products: Product[]) {
return (
@@ -60,6 +82,12 @@ function findMaftolProduct(products: Product[]) {
)
}
function findShamshProduct(products: Product[]) {
return products.find((product) =>
isShamshMesGroup(product.group_type, product.title)
)
}
function applyChartRatio(basePrice: number, ratio: number) {
if (!ratio) {
return basePrice
@@ -68,12 +96,12 @@ function applyChartRatio(basePrice: number, ratio: number) {
return Math.round(basePrice + basePrice * ratio)
}
/** بالاترین/پایین‌ترین امروز — برای boursecu با نسبت مفتول نسبت به کاتد */
/** بالاترین/پایین‌ترین امروز — برای boursecu با نسبت مفتول/شمش نسبت به کاتد */
export function resolveMetalDayRangePrices(options: {
price: { up?: { buy?: number; sell?: number }; buy?: number; sell?: number; price_last?: number } | null | undefined
metal: ChartMetalType
isBoursecuLayout: boolean
maftolRatio: number
metalRatio: number
}) {
const baseHigh = options.price?.up?.buy ?? getTradeBuyPrice(options.price as Parameters<typeof getTradeBuyPrice>[0])
const baseLow = options.price?.up?.sell ?? getTradeSellPrice(options.price as Parameters<typeof getTradeSellPrice>[0])
@@ -83,37 +111,36 @@ export function resolveMetalDayRangePrices(options: {
}
return {
high: applyChartRatio(baseHigh, options.maftolRatio),
low: applyChartRatio(baseLow, options.maftolRatio),
high: applyChartRatio(baseHigh, options.metalRatio),
low: applyChartRatio(baseLow, options.metalRatio),
}
}
function deriveMaftolChartRatio(maftolProduct?: Product, katodProduct?: Product) {
const maftolOffset = resolvePriceOffsetRatio(maftolProduct?.price_offset_percent)
if (maftolOffset) {
return maftolOffset
function deriveMetalChartRatio(metalProduct?: Product, katodProduct?: Product) {
const metalOffset = resolvePriceOffsetRatio(metalProduct?.price_offset_percent)
if (metalOffset) {
return metalOffset
}
if (!maftolProduct) {
if (!metalProduct) {
return 0
}
const liveBuy = getTradeBuyPrice(lastPrice.value)
const katodLive = katodProduct ? getProductBuyPrice(katodProduct) : liveBuy
const maftolLive = getProductBuyPrice(maftolProduct)
const metalLive = getProductBuyPrice(metalProduct)
// وقتی فروشگاه بسته است قیمت لحظه‌ای صفر می‌شود — از قیمت ثابت کاتالوگ استفاده کن
const katodStatic = Number(katodProduct?.price ?? 0) + Number(katodProduct?.price_profit ?? 0)
const maftolStatic = Number(maftolProduct.price ?? 0) + Number(maftolProduct.price_profit ?? 0)
const metalStatic = Number(metalProduct.price ?? 0) + Number(metalProduct.price_profit ?? 0)
const referencePrice = katodLive || liveBuy || katodStatic
const maftolPrice = maftolLive || maftolStatic
const derivedMetalPrice = metalLive || metalStatic
if (!referencePrice || !maftolPrice || referencePrice === maftolPrice) {
if (!referencePrice || !derivedMetalPrice || referencePrice === derivedMetalPrice) {
return 0
}
return (maftolPrice - referencePrice) / referencePrice
return (derivedMetalPrice - referencePrice) / referencePrice
}
function normalizeChartTimestamp(timestamp: number) {
@@ -147,18 +174,19 @@ function buildChartPoints(items: Array<{ timestamp: number; value: number }>): C
}
function syncChartSeries() {
const ratio = selectedMetalChartRatio.value
const data =
getHomeLayoutId() === 'boursecu' && selectedChartMetal.value === 'maftol'
getHomeLayoutId() === 'boursecu' && selectedChartMetal.value !== 'katod'
? rawChartPoints.value.map(
([timestamp, value]) =>
[timestamp, applyChartRatio(value, maftolChartRatio.value)] as ChartPoint
[timestamp, applyChartRatio(value, ratio)] as ChartPoint
)
: rawChartPoints.value
priceChartSeries.value[0].data = data
}
watch([selectedChartMetal, maftolChartRatio], () => {
watch([selectedChartMetal, maftolChartRatio, shamshChartRatio], () => {
syncChartSeries()
})
@@ -192,34 +220,34 @@ async function fetchPrices(force = false) {
return chartFetchPromise
}
async function loadMaftolOffset(force = false) {
async function loadMetalChartOffsets(force = false) {
if (getHomeLayoutId() !== 'boursecu') {
syncChartSeries()
return
}
if (!force && maftolOffsetLoaded && catalogProducts.value.length > 0) {
if (!force && metalOffsetLoaded && catalogProducts.value.length > 0) {
return
}
if (!force && maftolOffsetPromise) {
return maftolOffsetPromise
if (!force && metalOffsetPromise) {
return metalOffsetPromise
}
maftolOffsetPromise = (async () => {
metalOffsetPromise = (async () => {
try {
await getProductsCatalog(force)
syncChartSeries()
maftolOffsetLoaded = true
metalOffsetLoaded = true
} catch (error) {
console.error('Error loading maftol chart ratio:', error)
console.error('Error loading boursecu chart metal ratios:', error)
syncChartSeries()
} finally {
maftolOffsetPromise = null
metalOffsetPromise = null
}
})()
return maftolOffsetPromise
return metalOffsetPromise
}
function updateChartData(timeline: string) {
@@ -230,8 +258,8 @@ function updateChartData(timeline: string) {
function setChartMetal(metal: ChartMetalType) {
selectedChartMetal.value = metal
if (metal === 'maftol' && getHomeLayoutId() === 'boursecu' && !catalogProducts.value.length) {
void loadMaftolOffset(true)
if (metal !== 'katod' && getHomeLayoutId() === 'boursecu' && !catalogProducts.value.length) {
void loadMetalChartOffsets(true)
}
}
@@ -242,9 +270,12 @@ export function usePriceChart() {
chartLoading,
selectedChartMetal,
maftolChartRatio,
shamshChartRatio,
selectedMetalChartRatio,
updateChartData,
fetchPrices,
loadMaftolOffset,
loadMaftolOffset: loadMetalChartOffsets,
loadMetalChartOffsets,
setChartMetal,
}
}
@@ -254,7 +285,7 @@ export function fetchHomeChartPrices(force = false) {
}
export function loadHomeMaftolOffset(force = false) {
return loadMaftolOffset(force)
return loadMetalChartOffsets(force)
}
export function resetHomePriceChart() {
@@ -264,6 +295,6 @@ export function resetHomePriceChart() {
selectedChartMetal.value = 'katod'
chartInitialized = false
chartFetchPromise = null
maftolOffsetLoaded = false
maftolOffsetPromise = null
metalOffsetLoaded = false
metalOffsetPromise = null
}