Skip to content

Commit

Permalink
[新功能] 支持按单票利润排序 (#49)
Browse files Browse the repository at this point in the history
  • Loading branch information
Ximu-Luya authored Mar 17, 2024
2 parents 8056748 + ca46372 commit 8e0177c
Show file tree
Hide file tree
Showing 5 changed files with 322 additions and 276 deletions.
105 changes: 63 additions & 42 deletions components/City.vue
Original file line number Diff line number Diff line change
Expand Up @@ -30,29 +30,69 @@ const mode = ref<'simple' | 'full' | 'edit'>('simple');
const store = useLatestLogs();
const settingStore = useSettingStore()
// 售出城市列表
const sellCities = computed(() => {
return cities.filter((c) => c.name !== currentCity.name)
})
const settingStore = useSettingStore()
// 按设置排序后的城市列表
const sortCitesWithSetting = (filteredCities: CityInfo[], sourceCityName: string, productName: string) => {
if (settingStore.listSortMode === 'byCity') {
return filteredCities
} else if (settingStore.listSortMode === 'byProfit') {
return sortCitesByPercent(filteredCities, sourceCityName, productName)
} else if (settingStore.listSortMode === 'byPerTicketProfit') {
return sortCitesByPerTicketProfit(filteredCities, sourceCityName, productName)
} else {
return filteredCities
}
}
// 返回对各城市利润排序后的城市列表
// 按单位利润排序城市
const sortCitesByPercent = (filteredCities: CityInfo[], sourceCityName: string, productName: string) => {
const sourceCityPrice = store.getLatestLog(sourceCityName, productName, sourceCityName)?.price || 0
// 计算各城市货物利润
let citiesProfitMap: {[key: string]: number} = {}
filteredCities.map(city => {
const latestLog = store.getLatestLog(sourceCityName, productName, city.name)
// 如果最新交易记录无效,且有最新交易记录和原产地价格,则计算利润
// 如果最新交易记录有效,且有最新交易记录和原产地价格,则计算利润
const profit = !Boolean(isLogValid(latestLog)) && latestLog && sourceCityPrice ? latestLog.price - sourceCityPrice : -9999
return { cityName: city.name, profit }
}).forEach(cityProfit => citiesProfitMap[cityProfit.cityName] = cityProfit.profit)
const sortedCities = filteredCities.sort((a, b) => citiesProfitMap[b.name] - citiesProfitMap[a.name])
return sortedCities
}
// 按单票利润排序城市
const sortCitesByPerTicketProfit = (filteredCities: CityInfo[], sourceCityName: string, productName: string) => {
const sourceCityPrice = store.getLatestLog(sourceCityName, productName, sourceCityName)?.price || 0
const baseVolume = getProductInfo(sourceCityName, productName)?.baseVolume || 1
// 计算各城市货物利润
let citiesProfitMap: {[key: string]: number} = {}
filteredCities.map(city => {
const latestLog = store.getLatestLog(sourceCityName, productName, city.name)
// 如果最新交易记录无效,排名靠最后
if (!latestLog) return { cityName: city.name, profit: -9999 }
// 如果原产地价格无效,排名靠最后
else if (!sourceCityPrice) return { cityName: city.name, profit: -9998 }
// 如果最新交易记录无效,排名在原产地价格之下
else if(!Boolean(isLogValid(latestLog))) return { cityName: city.name, profit: latestLog.price - sourceCityPrice - 999 }
else return { cityName: city.name, profit: latestLog.price - sourceCityPrice }
}).forEach(cityProfit => citiesProfitMap[cityProfit.cityName] = cityProfit.profit)
const sortedCities = filteredCities.sort((a, b) => {
const aProfit = citiesProfitMap[a.name] * baseVolume
const bProfit = citiesProfitMap[b.name] * baseVolume
return bProfit - aProfit
})
return sortedCities
}
</script>

<template>
Expand All @@ -65,7 +105,7 @@ const sortCitesByPercent = (filteredCities: CityInfo[], sourceCityName: string,
<TableHeader>
<TableRow class="boder-t">
<TableHead class="w-[120px]">商品</TableHead>
<!-- 按城市纬度排序 -->
<!-- 按城市维度排序 -->
<template v-if="settingStore.listSortMode === 'byCity'">
<TableHead class="border-r">{{ currentCity.name }}</TableHead>
<TableHead
Expand All @@ -74,9 +114,14 @@ const sortCitesByPercent = (filteredCities: CityInfo[], sourceCityName: string,
>{{ city.name }}</TableHead>
</template>
<!-- 按利润排序 -->
<template v-else-if="settingStore.listSortMode === 'byProfit'">
<template v-else>
<TableHead class="border-r"><div class="w-30">原产地采购价</div></TableHead>
<TableHead :colspan="sellCities.length">城市售卖报价(按利润高低从左到右降序排序)</TableHead>
<TableHead :colspan="sellCities.length">
城市售卖报价(按
{{ settingStore.listSortMode === 'byProfit' ? "单位" : "" }}
{{ settingStore.listSortMode === 'byPerTicketProfit' ? "单票" : "" }}
利润高低从左到右降序排序)
</TableHead>
</template>
</TableRow>
</TableHeader>
Expand All @@ -87,51 +132,27 @@ const sortCitesByPercent = (filteredCities: CityInfo[], sourceCityName: string,
</TableCell>
<TableCell class="border-r">
<Price
:sort-mode="settingStore.listSortMode"
:timestamp="timestamp"
:product="getProductInfo(city.name, product.name)!"
:transaction="undefined"
:log="store.getLatestLog(city.name, product.name, city.name)"
/>
</TableCell>
<!-- 按城市纬度排序 -->
<template v-if="settingStore.listSortMode === 'byCity'">
<TableCell
v-for="target in sellCities"
:key="target.name"
>
<Price
:sort-mode="settingStore.listSortMode"
:timestamp="timestamp"
:product="getProductInfo(city.name, product.name)!"
:transaction="getTransactionInfo(city.name, product.name, target.name)"
:log="store.getLatestLog(city.name, product.name, target.name)"
/>
</TableCell>
</template>
<!-- 按利润排序 -->
<template v-if="settingStore.listSortMode === 'byProfit'">
<TableCell
v-for="target in sortCitesByPercent(sellCities, city.name, product.name)"
:key="target.name"
class="w-40"
>
<Price
:sort-mode="settingStore.listSortMode"
:timestamp="timestamp"
:product="getProductInfo(city.name, product.name)!"
:transaction="getTransactionInfo(city.name, product.name, target.name)"
:log="store.getLatestLog(city.name, product.name, target.name)"
/>
</TableCell>
</template>
<!-- 被排序过的售出城市列表 -->
<TableCell
v-for="targetCity in sortCitesWithSetting(sellCities, city.name, product.name)"
:key="`${product.name}-${targetCity.name}`"
>
<Price
:timestamp="timestamp"
:product="getProductInfo(city.name, product.name)!"
:transaction="getTransactionInfo(city.name, product.name, targetCity.name)"
:log="store.getLatestLog(city.name, product.name, targetCity.name)"
/>
</TableCell>
</TableRow>
</TableBody>
</Table>
</CardContent>
<!-- <CardFooter class="flex justify-between px-6 pb-6">
<Button variant="outline"> Cancel </Button>
<Button>Deploy</Button>
</CardFooter> -->
</Card>
</template>
Loading

0 comments on commit 8e0177c

Please sign in to comment.