fix chart
This commit is contained in:
@@ -0,0 +1,309 @@
|
||||
//@ts-ignore
|
||||
import moment from 'moment-jalaali'
|
||||
import { ref, computed, onMounted } from 'vue'
|
||||
import {
|
||||
getDashboard,
|
||||
getDashboardBalance,
|
||||
getDashboardFinance,
|
||||
getDashboardFlow,
|
||||
getPriceChartData,
|
||||
getRecentOrders,
|
||||
toggleGateway,
|
||||
} from '@/api/dashboard'
|
||||
import { getWalletTable } from '@/api/wallet'
|
||||
import type {
|
||||
DashboardFlowPoint,
|
||||
DashboardFinance,
|
||||
DashboardMaster,
|
||||
PriceChartPoint,
|
||||
} from '@/model/dashboard'
|
||||
import type { Order } from '@/model/order'
|
||||
import type { Balance } from '@/model/product'
|
||||
import { usePrice } from '@core/utils/usePrice'
|
||||
import { toJalali } from '@core/utils/useDataTime'
|
||||
import { isStoreApp } from '@/utils/config'
|
||||
|
||||
const MESGHAL = 4.331802
|
||||
|
||||
export function useDashboardPage() {
|
||||
const loading = ref(true)
|
||||
const flowLoading = ref(false)
|
||||
const priceRange = ref<'DAILY' | 'WEEKLY' | 'MONTHLY'>('DAILY')
|
||||
const flowRange = ref<'week' | 'month'>('week')
|
||||
|
||||
const startDate = ref(moment().subtract(30, 'days').startOf('day').format('YYYY-MM-DD HH:mm:ss'))
|
||||
const endDate = ref(moment().endOf('day').format('YYYY-MM-DD HH:mm:ss'))
|
||||
|
||||
const master = ref<DashboardMaster>()
|
||||
const finance = ref<DashboardFinance>()
|
||||
const balance = ref<Balance>()
|
||||
const walletSums = ref<{ gold?: number; cash?: number; goldToCash?: number; cashToCash?: number }>()
|
||||
const recentOrders = ref<Order[]>([])
|
||||
const flowData = ref<DashboardFlowPoint[]>([])
|
||||
const priceSeries = ref<PriceChartPoint[]>([])
|
||||
|
||||
const isStore = computed(() => isStoreApp())
|
||||
|
||||
const pendingOrders = computed(() => master.value?.await_order ?? [])
|
||||
|
||||
const customerStats = computed(() => ({
|
||||
newCount: isStore.value
|
||||
? master.value?.new_customer_major ?? 0
|
||||
: master.value?.new_customer_single ?? 0,
|
||||
total: isStore.value
|
||||
? master.value?.all_customer_major ?? 0
|
||||
: master.value?.all_customer_single ?? 0,
|
||||
active: master.value?.GT_active_customers ?? 0,
|
||||
inactive: master.value?.GT_deActive_customers ?? 0,
|
||||
notVerified: master.value?.GT_no_valid_customers ?? 0,
|
||||
}))
|
||||
|
||||
async function loadCore() {
|
||||
const [dash, fin, bal, wallet, orders, prices] = await Promise.allSettled([
|
||||
getDashboard(),
|
||||
getDashboardFinance(startDate.value, endDate.value),
|
||||
getDashboardBalance(startDate.value, endDate.value),
|
||||
getWalletTable({ limit: 1, page: 1 } as never),
|
||||
getRecentOrders(12),
|
||||
getPriceChartData(),
|
||||
])
|
||||
|
||||
if (dash.status === 'fulfilled') master.value = dash.value
|
||||
if (fin.status === 'fulfilled') finance.value = fin.value
|
||||
if (bal.status === 'fulfilled') balance.value = bal.value
|
||||
if (wallet.status === 'fulfilled') walletSums.value = wallet.value?.sums
|
||||
if (orders.status === 'fulfilled') recentOrders.value = orders.value
|
||||
if (prices.status === 'fulfilled') {
|
||||
priceSeries.value = prices.value?.DAILY ?? []
|
||||
}
|
||||
}
|
||||
|
||||
async function loadFlow() {
|
||||
flowLoading.value = true
|
||||
try {
|
||||
flowData.value = (await getDashboardFlow(flowRange.value)) ?? []
|
||||
} catch (e) {
|
||||
console.error(e)
|
||||
flowData.value = []
|
||||
} finally {
|
||||
flowLoading.value = false
|
||||
}
|
||||
}
|
||||
|
||||
async function refreshAll() {
|
||||
loading.value = true
|
||||
try {
|
||||
await Promise.all([loadCore(), loadFlow()])
|
||||
} finally {
|
||||
loading.value = false
|
||||
}
|
||||
}
|
||||
|
||||
async function applyDateRange() {
|
||||
loading.value = true
|
||||
try {
|
||||
const [fin, bal] = await Promise.all([
|
||||
getDashboardFinance(startDate.value, endDate.value),
|
||||
getDashboardBalance(startDate.value, endDate.value),
|
||||
])
|
||||
finance.value = fin
|
||||
balance.value = bal
|
||||
} finally {
|
||||
loading.value = false
|
||||
}
|
||||
}
|
||||
|
||||
async function switchPriceRange(range?: 'DAILY' | 'WEEKLY' | 'MONTHLY') {
|
||||
const key = range ?? priceRange.value
|
||||
priceRange.value = key
|
||||
const prices = await getPriceChartData()
|
||||
priceSeries.value = prices?.[key] ?? []
|
||||
}
|
||||
|
||||
async function onToggleGateway() {
|
||||
await toggleGateway()
|
||||
master.value = await getDashboard()
|
||||
}
|
||||
|
||||
function formatGram(value?: number) {
|
||||
return `${Number(value ?? 0).toLocaleString('fa-IR', { maximumFractionDigits: 3 })} گرم`
|
||||
}
|
||||
|
||||
function directionLabel(direction?: number) {
|
||||
return direction === 1 ? 'خرید' : direction === 2 ? 'فروش' : '—'
|
||||
}
|
||||
|
||||
function directionClass(direction?: number) {
|
||||
return direction === 1
|
||||
? 'bg-emerald-100 text-emerald-700 dark:bg-emerald-500/20 dark:text-emerald-300'
|
||||
: 'bg-rose-100 text-rose-700 dark:bg-rose-500/20 dark:text-rose-300'
|
||||
}
|
||||
|
||||
function stsClass(sts?: number) {
|
||||
if (sts === 2) {
|
||||
return 'bg-emerald-100 text-emerald-700 dark:bg-emerald-500/20 dark:text-emerald-300'
|
||||
}
|
||||
if (sts === 1) {
|
||||
return 'bg-amber-100 text-amber-700 dark:bg-amber-500/20 dark:text-amber-300'
|
||||
}
|
||||
return 'bg-rose-100 text-rose-700 dark:bg-rose-500/20 dark:text-rose-300'
|
||||
}
|
||||
|
||||
function stsLabel(order: Order) {
|
||||
return (order as Order & { sts_str?: string }).sts_str
|
||||
?? (order.sts === 2 ? 'موفق' : order.sts === 1 ? 'باز' : 'ناموفق')
|
||||
}
|
||||
|
||||
const financeChartOptions = computed(() => {
|
||||
const sood = finance.value?.fullPriceSood ?? 0
|
||||
const karmozd = finance.value?.fullPriceKarmozd ?? finance.value?.fullPriceKaermozd ?? 0
|
||||
const gifts = finance.value?.GiftsPrise ?? 0
|
||||
|
||||
return {
|
||||
chart: { type: 'donut', fontFamily: 'inherit' },
|
||||
labels: ['سود معامله', 'کارمزد طلایی', 'هدایای طلا'],
|
||||
colors: ['#10b981', '#f59e0b', '#8b5cf6'],
|
||||
legend: { position: 'bottom', fontSize: '12px' },
|
||||
dataLabels: { enabled: false },
|
||||
plotOptions: {
|
||||
pie: {
|
||||
donut: {
|
||||
size: '68%',
|
||||
labels: {
|
||||
show: true,
|
||||
total: {
|
||||
show: true,
|
||||
label: 'درآمد خالص',
|
||||
formatter: () => usePrice(finance.value?.sumAll ?? 0, true, ''),
|
||||
},
|
||||
},
|
||||
},
|
||||
},
|
||||
},
|
||||
tooltip: {
|
||||
y: { formatter: (v: number) => usePrice(v, true, '') },
|
||||
},
|
||||
series: [sood, karmozd, gifts],
|
||||
}
|
||||
})
|
||||
|
||||
const flowChartOptions = computed(() => {
|
||||
const points = [...flowData.value].sort((a, b) =>
|
||||
String(a.date ?? '').localeCompare(String(b.date ?? '')),
|
||||
)
|
||||
const categories = points.map((p) => {
|
||||
const d = p.date ?? ''
|
||||
return d ? toJalali(`${d} 12:00:00`, 'jMM/jDD') : d
|
||||
})
|
||||
|
||||
return {
|
||||
chart: { type: 'bar', stacked: true, toolbar: { show: false }, fontFamily: 'inherit' },
|
||||
plotOptions: { bar: { borderRadius: 4, columnWidth: '55%' } },
|
||||
colors: ['#10b981', '#f43f5e'],
|
||||
xaxis: { categories, labels: { rotate: -35, style: { fontSize: '11px' } } },
|
||||
yaxis: {
|
||||
labels: {
|
||||
formatter: (v: number) => `${Number(v).toLocaleString('fa-IR')} گرم`,
|
||||
},
|
||||
},
|
||||
legend: { position: 'top' },
|
||||
dataLabels: { enabled: false },
|
||||
series: [
|
||||
{ name: 'خرید مشتری', data: points.map((p) => Number(p[1] ?? p['1'] ?? 0)) },
|
||||
{ name: 'فروش مشتری', data: points.map((p) => Number(p[2] ?? p['2'] ?? 0)) },
|
||||
],
|
||||
}
|
||||
})
|
||||
|
||||
const priceChartOptions = computed(() => ({
|
||||
chart: { type: 'area', toolbar: { show: false }, fontFamily: 'inherit', zoom: { enabled: false } },
|
||||
stroke: { curve: 'smooth', width: 2 },
|
||||
fill: {
|
||||
type: 'gradient',
|
||||
gradient: { shadeIntensity: 1, opacityFrom: 0.45, opacityTo: 0.05 },
|
||||
},
|
||||
colors: ['#2563eb'],
|
||||
dataLabels: { enabled: false },
|
||||
xaxis: {
|
||||
type: 'datetime',
|
||||
labels: { datetimeUTC: false, style: { fontSize: '11px' } },
|
||||
},
|
||||
yaxis: {
|
||||
labels: {
|
||||
formatter: (v: number) => `${Math.round(v / 1_000_000).toLocaleString('fa-IR')}M`,
|
||||
},
|
||||
},
|
||||
tooltip: {
|
||||
x: { format: 'yyyy/MM/dd HH:mm' },
|
||||
y: { formatter: (v: number) => usePrice(v, true, '') },
|
||||
},
|
||||
series: [
|
||||
{
|
||||
name: 'مظنه',
|
||||
data: priceSeries.value.map((p) => [p.timestamp, p.value]),
|
||||
},
|
||||
],
|
||||
}))
|
||||
|
||||
const productChartOptions = computed(() => {
|
||||
const items = (master.value?.GT_products_report ?? []).slice(0, 8)
|
||||
return {
|
||||
chart: { type: 'bar', toolbar: { show: false }, fontFamily: 'inherit' },
|
||||
plotOptions: { bar: { horizontal: true, borderRadius: 4 } },
|
||||
colors: ['#0ea5e9', '#f97316'],
|
||||
dataLabels: { enabled: false },
|
||||
xaxis: {
|
||||
categories: items.map((p) => p.title ?? `#${p.id}`),
|
||||
labels: { style: { fontSize: '11px' } },
|
||||
},
|
||||
legend: { position: 'top' },
|
||||
series: [
|
||||
{ name: 'خرید (گرم)', data: items.map((p) => p.transaction_sum_gold_buy ?? 0) },
|
||||
{
|
||||
name: 'فروش (گرم)',
|
||||
data: items.map((p) => Math.abs(p.total_negative_gold_sell ?? 0)),
|
||||
},
|
||||
],
|
||||
}
|
||||
})
|
||||
|
||||
onMounted(() => {
|
||||
void refreshAll()
|
||||
})
|
||||
|
||||
return {
|
||||
loading,
|
||||
flowLoading,
|
||||
startDate,
|
||||
endDate,
|
||||
master,
|
||||
finance,
|
||||
balance,
|
||||
walletSums,
|
||||
recentOrders,
|
||||
pendingOrders,
|
||||
customerStats,
|
||||
priceRange,
|
||||
flowRange,
|
||||
flowData,
|
||||
priceSeries,
|
||||
isStore,
|
||||
MESGHAL,
|
||||
refreshAll,
|
||||
applyDateRange,
|
||||
switchPriceRange,
|
||||
loadFlow,
|
||||
onToggleGateway,
|
||||
formatGram,
|
||||
directionLabel,
|
||||
directionClass,
|
||||
stsClass,
|
||||
stsLabel,
|
||||
financeChartOptions,
|
||||
flowChartOptions,
|
||||
priceChartOptions,
|
||||
productChartOptions,
|
||||
usePrice,
|
||||
toJalali,
|
||||
}
|
||||
}
|
||||
Reference in New Issue
Block a user