<?php

namespace App\Jobs;

use App\Models\BandOriginSyn;
use App\Models\DailyStep;
use Illuminate\Bus\Queueable;
use Illuminate\Contracts\Queue\ShouldBeUnique;
use Illuminate\Contracts\Queue\ShouldQueue;
use Illuminate\Foundation\Bus\Dispatchable;
use Illuminate\Queue\InteractsWithQueue;
use Illuminate\Queue\SerializesModels;
use Log;

class SyncDailyStep implements ShouldQueue
{
    use Dispatchable, InteractsWithQueue, Queueable, SerializesModels;

    protected $user, $start_time, $end_time, $suffix, $band_id;
    /**
     * Create a new job instance.
     *
     * @return void
     */
    public function __construct($user, $start_time, $end_time, $suffix, $band_id)
    {
        $this->user = $user;
        $this->start_time = $start_time;
        $this->end_time = $end_time;
        $this->suffix = $suffix;
        $this->band_id = $band_id;
    }

    /**
     * Execute the job.
     *
     * @return void
     */
    public function handle()
    {
        if ($this->start_time > $this->end_time)
            return;
        $start_date = date("Y-m-d", $this->start_time); // 开始一天
        $end_date = date("Y-m-d", $this->end_time); // 最后一天
        $last_date = date('Y-m-d', strtotime('+1 day', strtotime($end_date))); // 最后一天+1
        if ($start_date == $end_date) { // 同一天
            $this->getDailySyn($start_date, $last_date);
            return;
        } else {// 不同天
            while (true) {
                if ($start_date == $last_date)
                    break;
                $cur_end_date = date('Y-m-d', strtotime('+1 day', strtotime($start_date)));
                $this->getDailySyn($start_date, $cur_end_date);
                $start_date = $cur_end_date;
            }
        }
    }

    public function getDailySyn($start_date, $end_date)
    {
        // Log::info("user_id:{$this->user->id} start_date:{$start_date} end_date:{$end_date}");
        $synList = BandOriginSyn::suffix($this->suffix)->where("user_id", $this->user->id)->where("band_id", $this->band_id)->where("date", ">=", $start_date)->where("date", "<", $end_date)->orderBy("id")->get();
        $last_id = 0;
        $total_step = $total_distance = $total_calories = 0;
        foreach ($synList as $syn) {
            $syn_value = $syn->value;
            if (isset($syn_value["step"])) {
                $total_step += $syn_value["step"];
            } elseif (isset($syn_value["detailMinterStep"])) {
                $total_step += $syn_value["detailMinterStep"];
            }
            if (isset($syn_value["calories"])) {
                $total_calories += number_format($syn_value["calories"], 2, ".", "");
            }
            if (isset($syn_value["distance"])) {
                $total_distance += number_format($syn_value["distance"], 2, ".", "");
            }
            $last_id = $syn->id;
        }
        // Log::info("total_step:{$total_step}");
        if (empty($total_step))
            return;
        $step = DailyStep::where("user_id", $this->user->id)->where("band_id", $this->band_id)->where("date", $start_date)->first();
        if ($step) {
            DailyStep::where("id", $step->id)->update(["step" => $total_step, "distance" => $total_distance, "calories" => $total_calories, "last_id" => $last_id]);
        } else {
            DailyStep::where("user_id", $this->user->id)->where("date", $start_date)->delete();
            $step = DailyStep::create([
                "band_id" => $this->band_id,
                "user_id" => $this->user->id,
                "date" => $start_date,
                "step" => $total_step,
                "distance" => $total_distance,
                "calories" => $total_calories,
                "last_id" => $last_id
            ]);
        }
    }
}
