从日期开始的持续时间单位总和

时间:2018-06-07 03:30:03

标签: eloquent

我有一个表(项目),其中包含start_date和持续时间(以周为单位)和一个数字(progress_pw),表示每周可以达到多少进度单位

var appService = new AppServiceConnection();

        // Here, we use the app service name defined in the app service provider's Package.appxmanifest file in the <Extension> section.
        appService.AppServiceName = "com.yanscorp.appservicedemo.Values";

        // Use Windows.ApplicationModel.Package.Current.Id.FamilyName within the app service provider to get this value.
        appService.PackageFamilyName = "c97887ad-1f75-4b48-9e3b-21b89c061715_6evysfdvxt248";

        //uwp return Success,but winform return AppServiceUnavailable
        var status = await appService.OpenAsync();
        if (status != AppServiceConnectionStatus.Success)
        {
            textBlock.Text = "Failed to connect";
            return;
        }

我希望按周计算预计在该周内使用的进度单元总数。

例如:

*在6月11日星期一开始的一周中,project.id 1预计将消耗500个单位

*从6月18日星期一开始的一周,project.id 1预计将消耗500个单位&amp; project.id 2预计消耗120个单位,总消费量为620个单位。

*在2018-12-01开始的一周(未来某个时间),没有活动项目,因此消耗了0个单位。

projects
+----+----------+--------+-----------+
| ID |start_date|duration|progress_pw|
+----+----------+--------+-----------+
|  1 |2018-06-15| 2      | 500       |
|  2 |2018-06-19| 4      | 120       |

这一个背后的逻辑指示?

1 个答案:

答案 0 :(得分:0)

试试这个:

$i = 0; $weeks = 12;
while ($i < $weeks) {
    $start = Carbon::now()->addWeeks($i)->startOfWeek();
    $end = (clone $start)->addDays(6);
    $requiredcap = DB::table('projects')
        ->select(DB::raw('sum(progress_pw) as progress'))
        ->whereBetween('start_date', [$start->toDateString(), $end->toDateString()])
        ->orWhere(function ($query) use ($start, $end) {
            $query->where('start_date', '<', $start->toDateString())
                ->where('end_date', '>=', $start->toDateString());
        })->first();
    $capacity['label'][] = $start->format('d M');
    $capacity['required'][] = $requiredcap->progress;
    $i++;
}