首页
学习
活动
专区
圈层
工具
发布
社区首页 >问答首页 >预订系统定价条件重构

预订系统定价条件重构
EN

Stack Overflow用户
提问于 2022-02-20 17:58:37
回答 1查看 48关注 0票数 0

我做了一个有预订和保留的项目,一个客户有一个以前用Laravel/PHP完成的平台,并要求我们做一些修改。这是我第一次用算法计算日期范围内的价格,所以我试图猜测什么是最有效的方法。

现在,客户有4种不同的价格:

  • 淡季(11月1日至3月31日)
  • 中季(4月1日至6月30日和10月1日至10月31日)
  • 高级季节(7月1日至7月15日和8月16日至30日(7月16日至8月15日)

)

在这几个季节里,他们有不同种类的汽车要租,但之前的代码太麻烦了,无法计算出合适的价格。以下是一段简短的节选:

代码语言:javascript
复制
$startDate = str_replace("/", "-", explode(' ', $request->startDate)[0]);
$endDate = str_replace("/", "-", explode(' ', $request->endDate)[0]);

$begin = new DateTime($startDate);
$end = new DateTime($endDate);
if ($startDate == $endDate) {
    $end->setTime(0, 0, 1);
}

$startYear = intval($begin->format('Y'));
$endYear = intval($end->format('Y'));

$interval = new DateInterval('P1D');
$daterange = new DatePeriod($begin, $interval, $end);

$groupPrices = DB::table("Group")->select("*")->get();
$totalGroupPrices = [];
$totalGroupPricesWithInsurance = [];

foreach ($groupPrices as $group) {
    foreach ($daterange as $date) {
        if ($date->format('d-m-Y') >= "1/11/" . strval($startYear) and $date->format('d-m-Y') <= "31/3/" . strval(($endYear + 1))) {
            (isset($totalGroupPrices[$group->id]) ? $totalGroupPrices[$group->id] += $group->lowSeasonPrice : $totalGroupPrices[$group->id] = $group->lowSeasonPrice);
            (isset($totalGroupPricesWithInsurance[$group->id]) ? $totalGroupPricesWithInsurance[$group->id] += $group->lowSeasonPriceWithInsurance : $totalGroupPricesWithInsurance[$group->id] = $group->lowSeasonPriceWithInsurance);
        } elseif ($date->format('d-m-Y') >= "1/1/" . strval($startYear) and $date->format('d-m-Y') <= "31/3/" . strval(($endYear))) {
            (isset($totalGroupPrices[$group->id]) ? $totalGroupPrices[$group->id] += $group->lowSeasonPrice : $totalGroupPrices[$group->id] = $group->lowSeasonPrice);
            (isset($totalGroupPricesWithInsurance[$group->id]) ? $totalGroupPricesWithInsurance[$group->id] += $group->lowSeasonPriceWithInsurance : $totalGroupPricesWithInsurance[$group->id] = $group->lowSeasonPriceWithInsurance);
        } elseif (($date->format('d-m') >= "1/4" and $date->format('d-m') <= "30/6") or ($date->format('d-m') >= "1/10" and $date->format('d-m') <= "31/10")) {
            (isset($totalGroupPrices[$group->id]) ? $totalGroupPrices[$group->id] += $group->mediumSeasonPrice : $totalGroupPrices[$group->id] = $group->mediumSeasonPrice);
            (isset($totalGroupPricesWithInsurance[$group->id]) ? $totalGroupPricesWithInsurance[$group->id] += $group->mediumSeasonPriceWithInsurance : $totalGroupPricesWithInsurance[$group->id] = $group->mediumSeasonPriceWithInsurance);
        } elseif (($date->format('d-m') >= "1/7" and $date->format('d-m') <= "15/7") or ($date->format('d-m') >= "16/8" and $date->format('d-m') <= "30/9")) {
            (isset($totalGroupPrices[$group->id]) ? $totalGroupPrices[$group->id] += $group->highSeasonPrice : $totalGroupPrices[$group->id] = $group->highSeasonPrice);
            (isset($totalGroupPricesWithInsurance[$group->id]) ? $totalGroupPricesWithInsurance[$group->id] += $group->highSeasonPriceWithInsurance : $totalGroupPricesWithInsurance[$group->id] = $group->highSeasonPriceWithInsurance);
        } elseif ($date->format('d-m') >= "16/7" and $date->format('d-m') <= "15/8") {
            (isset($totalGroupPrices[$group->id]) ? $totalGroupPrices[$group->id] += $group->peakSeasonPrice : $totalGroupPrices[$group->id] = $group->peakSeasonPrice);
            (isset($totalGroupPricesWithInsurance[$group->id]) ? $totalGroupPricesWithInsurance[$group->id] += $group->peakSeasonPriceWithInsurance : $totalGroupPricesWithInsurance[$group->id] = $group->peakSeasonPriceWithInsurance);
        }
    }
}

我确信这必须有一个非常简单的方法,或者至少用更少的代码来减少buggy。我只想知道你会如何设计这个算法。

提前谢谢。

EN

回答 1

Stack Overflow用户

发布于 2022-02-20 20:18:00

就像这样:

代码语言:javascript
复制
private function season($date): string
{
    $seasons = [
        // items are month, day, duration.
        'medium' => [[4, 1, 90], [10, 1, 30]],
        'heigh' => [[7, 1, 14], [8, 16, 45]],
        'peak' => [[7, 16, 30]],
    ];

    $year = Carbon::now()->year;

    $date = Carbon::createFromDate($year, $date->month, $date->day);

    foreach ($seasons as $name => $periods) {
        foreach ($periods as $period) {
            $start = Carbon::createFromDate($year, $period[0], $period[1]);

            $end = $start->copy()->addDays($period[2]);

            if ($date->gt($start) && $date->lte($end)) return $name; 
        }
    }

    return 'low';
}

private function calculate($season, $group, $gp, $gpwi)
{
    $sp = "{$season}SeasonPrice";
    
    $spwi = "{$season}SeasonPriceWithInsurance";

    return [
        (isset($gp[$group->id]) ? $gp[$group->id] : 0) + $group->$sp,
        (isset($gpwi[$group->id]) ? $gpwi[$group->id] : 0) + $group->$spwi
    ];
}

private function makePrices()
{
    foreach ($groupPrices as $group) {
        foreach ($daterange as $date) {
            [$totalGroupPrices, $totalGroupPricesWithInsurance] = $this->calculate(
                $this->season($date),
                $group,
                $totalGroupPrices,
                $totalGroupPricesWithInsurance
            );
        }
    }
}
票数 0
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/71197190

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档