<?php

namespace App\Services;

use App\Models\Food;

class GuideService
{
    private $closeDates = [];

    public function getGuideStatus($daily, $guides, $dates)
    {
        $now_date = date('Y-m-d');
        $status = (in_array($daily, $dates)) ? 1 : 0;
        if (strtotime($daily) > strtotime($now_date) && $status) {//今天以后的日期
            //默认超过当前的日期都是关闭的
            $status = 2;
            //今天餐单
            $current_guide = $guides->where('phase_date', $now_date)->first();
            //明天餐单
            $next_guide = $guides->where('phase_date', '>', $now_date)->first();
            //当前日期餐单
            $daily_guide = $guides->where('phase_date', $daily)->first();
            //            if (empty($current_guide) || $current_guide->phase_title != $next_guide->phase_title) {//进入下一阶段
//                $count = $guides->where('phase_date','>=',  $next_guide->phase_date)->where('phase_date', '<=', $daily_guide->phase_date)->where('phase_title', '!=', $next_guide->phase_title)->count();
//                if ($daily_guide->phase_title == $next_guide->phase_title && !$count) $status = 1; //下一阶段状态开启
//            }else{//未进入下一阶段
//                $count = $guides->where('phase_date','>=',  $now_date)->where('phase_date', '<=', $daily_guide->phase_date)->where('phase_title', '!=', $current_guide->phase_title)->count();
//                if ($daily_guide->phase_title == $current_guide->phase_title && !$count) $status = 1;//同一阶段 并且中间没有其他阶段
//            }

            //最近三天的餐单显示,超过三天则加锁
            if ($daily > $now_date) {
                $this->closeDates[] = $daily;
                // 如果已经找到三个最近日期，则跳出循环
                if (count($this->closeDates) <= 3) {
                    $status = 1;
                }
            }
        }
        return $status;
    }

    public function getQuantity($scheme, $guides)
    {

        //获取订单方案,并处理数组
        $scheme_food = [];
        foreach ($scheme as $value) {
            if (!isset($value->num)) {
                continue;
            }
            $order_scheme = Food::where('id', $value->id)->first();
            if (empty($order_scheme)) {
                continue;
            }
            $order_scheme->quantity = $value->num * $order_scheme->quantity;
            $order_scheme->remain_quantity = $order_scheme->quantity;
            $scheme_food[$value->id] = $order_scheme;
        }
        $flag = false;
        $msg = [];
        //处理数据
        foreach ($guides as $guide) {
            //处理数组,之前的逻辑设计一坨屎,我也不想这么写,没办法
            $guide->menu = json_decode($guide->menu);
            $guide->hint = json_decode($guide->hint);

            $new_array = array();
            foreach ($guide->menu as $menu) {
                foreach ($menu->nutrient as $item) {
                    if (!isset($item->id)) {
                        continue;
                    }
                    $id = $item->id;
                    $number = intval($item->number);

                    if (array_key_exists($id, $new_array)) {
                        $new_array[$id] += $number;
                    } else {
                        $new_array[$id] = $number;
                    }
                }
            }
            $ids = [];
            //再处理一遍
            foreach ($guide->menu as $menu) {
                foreach ($menu->nutrient as $item) {
                    if (!isset($item->id) || !isset($scheme_food[$item->id])) {
                        continue;
                    }
                    $id = $item->id;

                    $quantity = 0;
                    if (isset($scheme_food[$id]->remain_quantity)) {
                        $quantity = $scheme_food[$id]->remain_quantity;
                    }

                    $item->rate = 100;
                    $item->remain_quantity = $scheme_food[$id]->remain_quantity = $quantity;
                    if ($quantity != 0) {
                        $item->rate = round($scheme_food[$id]->remain_quantity / $scheme_food[$id]->quantity, 2) * 100;
                        //如果已经存在数组中,不再做减法计算
                        if (in_array($id, $ids)) {
                            continue;
                        }
                        $quantity = $quantity - $new_array[$id];
                        $item->remain_quantity = $scheme_food[$id]->remain_quantity = $quantity;
                        $ids[] = $id;
                    }

                    if ($quantity <= 0) {
                        $item->remain_quantity = $scheme_food[$id]->remain_quantity = $item->rate = 0;
                    }
                }
            }
        }
        //每一天余量固定
        foreach ($guides as $value) {
            $ids = [];
            foreach ($value->menu as $val) {
                foreach ($val->nutrient as $v) {
                    if (!isset($v->id) || !isset($scheme_food[$v->id])) {
                        continue;
                    }
                    //如果有小于20%的量,提示
                    if ($v->rate <= 20) {
                        $flag = true;
                    }

                    $id = $v->id;

                    if (in_array($id, $ids)) {
                        continue;
                    }
                    $map = [
                        'phase_date' => $value->phase_date,
                        'phase_title' => $value->phase_title,
                        'order_id' => $value->order_id,
                        'user_id' => $value->user_id,
                        'food_id' => $v->id,
                        'number' => $v->number,
                        'title' => $v->title,
                        'rate' => $v->rate,
                        'remain_quantity' => $v->remain_quantity,
                        'quantity' => $scheme_food[$id]->quantity,
                    ];
                    $v->remain_quantity = $scheme_food[$id]->remain_quantity ?? 0;
                    $msg[] = $map;
                    $ids[] = $id;
                }
            }
        }

        return [$guides, $scheme_food, $flag, $msg];
    }
}
