IT数码 购物 网址 头条 软件 日历 阅读 图书馆
TxT小说阅读器
↓语音阅读,小说下载,古典文学↓
图片批量下载器
↓批量下载图片,美女图库↓
图片自动播放器
↓图片自动播放器↓
一键清除垃圾
↓轻轻一点,清除系统垃圾↓
开发: C++知识库 Java知识库 JavaScript Python PHP知识库 人工智能 区块链 大数据 移动开发 嵌入式 开发工具 数据结构与算法 开发测试 游戏开发 网络协议 系统运维
教程: HTML教程 CSS教程 JavaScript教程 Go语言教程 JQuery教程 VUE教程 VUE3教程 Bootstrap教程 SQL数据库教程 C语言教程 C++教程 Java教程 Python教程 Python3教程 C#教程
数码: 电脑 笔记本 显卡 显示器 固态硬盘 硬盘 耳机 手机 iphone vivo oppo 小米 华为 单反 装机 图拉丁
 
   -> 数据结构与算法 -> uberEate接口获取或者提交的数据结构处理(部分) -> 正文阅读

[数据结构与算法]uberEate接口获取或者提交的数据结构处理(部分)

uberEate数据重组, 接上篇:优步外卖(Ubereat)接口请求封装-全球uberapi

/**
     * 数据转化
     * @param int $store_id 本地的数据库店铺id
     * @param array $data
     * @return false|string
     */
    public function dataChange($data, $store_id = 0){
        $new_data = [
            'in' => [], //进中台的数据
            'out' => [] //进uber的数据
        ];
        switch ($this->uri){
            case 'getStoreStatus':
                $status = 0;
                if($data['data']['status'] == 'ONLINE'){
                    $status = 1;
                }elseif ($data['data']['status'] == 'OFFLINE' || $data['data']['status'] == 'PAUSED'){
                    $status = 0;
                }
                $new_data['in'][$store_id] = [
                    'id' => $store_id,
                    'is_in_business' => $status
                ];
                break;
            case 'setStoreStatus':
                /*$params = ['status' => 1];*/

                $status = 'PAUSED';
                if($data['status'] == 1){
                    $status = 'ONLINE';
                }
                $new_data['out'][$store_id] = [
                    'status' => $status
                ];
                $return_status = 0;
                if($data['data']['status'] == 'ONLINE'){
                    $return_status = 1;
                }
                $new_data['in'][$store_id] = [
                    'id' => $store_id,
                    'is_in_business' => $return_status
                ];
                break;
            case 'getHolidayHours':
                foreach ($data['data']['holiday_hours'] as $key=>$val){
                    foreach ($val['open_time_periods'] as $key1=>$hour){
                        $val['open_time_periods'][$key1] = [
                            's' => $hour['start_time'],
                            'e' => $hour['end_time'],
                        ];
                    }
                    $new_data['in'][$store_id] = [
                        'week' => $this->getTimeWeek($key),
                        'business_hours' => $val['open_time_periods']
                    ];
                }
                break;
            case 'setHolidayHours':
                /*$str = 'a:2:{i:0;a:2:{s:1:"s";s:5:"00:00";s:1:"e";s:5:"23:58";}i:1;a:2:{s:1:"s";s:5:"11:20";s:1:"e";s:5:"22:20";}}';
                $prams = [
                    'date' => '2021-07-10',
                    'business_hours' => unserialize($str)
                ];*/
                foreach ($data['business_hours'] as $hour){
                    $new_data['out']['holiday_hours'][$data['date']]['open_time_periods'][] = [
                        'start_time' => $hour['s'],
                        'end_time' => $hour['e'],
                    ];
                }
                break;
            case 'setItem'://time:更新商品暂停下架规则,price:更新商品的价格,menu_type:更新自取还是派送
                /*$params = [
                    'price' => 1000,
                    'time' => time(),//暂停后恢复销售的时间
                    'reason' => '测试暂停销售', //暂停的原因
                    'menu_type' => 1 //菜单类型
                ];*/
                if(isset($data['price'])){
                    $new_data['out']['price_info']['price'] = (int)$data['price'] * 100;
                }
                if(isset($data['time'])){//更新下架规则
                    $new_data['out']['suspension_info'] = [
                        'suspension' => [
                            'suspend_until' => $data['time'],//未来时间
                            'reason' => isset($data['reason']) ? $data['reason'] : '暂停售卖'//暂停的原因
                        ]
                    ];
                }
                if(isset($data['menu_type'])){
                    if($data['menu_type'] == 1){
                        $new_data['out']['menu_type'] = 'MENU_TYPE_FULFILLMENT_DELIVERY';
                    }elseif ($data['menu_type'] == 2){
                        $new_data['out']['menu_type'] = 'MENU_TYPE_FULFILLMENT_PICK_UP';
                    }else{
                        $new_data['out']['menu_type'] = '';
                    }
                }
                break;
            case 'putMenus':
                $new_data['out'] = $this->dealData($store_id);
                break;
            case 'getMenus':
                $new_data['in'] = $this->dataReBuild($data);
                break;
            default:break;
        }

        return $new_data;
    }


    public function getTimeWeek($date) {
        $time = strtotime($date);
        $week_array_name = ['日','一','二','三','四','五','六'];
        $week = date('w', $time);
        if($week == 0){
            $week = 7;
        }
        return ['name' => '星期'.$week_array_name[$week], 'num' => $week];
    }


    /**
     * uber上传菜单数据重组
     * @param $store_id int 平台店铺id
     * @return array|bool
     */
    public function dealData($store_id = 0){
        //数量, 价格, 主档商品编号,排序,商品名称,商品分类,配送类型
        $init_data = [
            'menus' => [
                [
                    'id' => 'All-day',
                    'title' => [
                        'translations' => [
                            'en_us' => 'All day',
                        ]
                    ],
                    'service_availability' => [
                        [
                            'time_periods' => [
                                [
                                    'start_time' => '00:00',
                                    'end_time' => '23:59',
                                ]
                            ],
                            'day_of_week' => 'monday'
                        ],
                        [
                            'time_periods' => [
                                [
                                    'start_time' => '00:00',
                                    'end_time' => '23:59',
                                ]
                            ],
                            'day_of_week' => 'tuesday'
                        ],
                        [
                            'time_periods' => [
                                [
                                    'start_time' => '00:00',
                                    'end_time' => '23:59',
                                ]
                            ],
                            'day_of_week' => 'wednesday'
                        ],
                        [
                            'time_periods' => [
                                [
                                    'start_time' => '00:00',
                                    'end_time' => '23:59',
                                ]
                            ],
                            'day_of_week' => 'thursday'
                        ],
                        [
                            'time_periods' => [
                                [
                                    'start_time' => '00:00',
                                    'end_time' => '23:59',
                                ]
                            ],
                            'day_of_week' => 'friday'
                        ],
                        [
                            'time_periods' => [
                                [
                                    'start_time' => '00:00',
                                    'end_time' => '23:59',
                                ]
                            ],
                            'day_of_week' => 'saturday'
                        ],
                        [
                            'time_periods' => [
                                [
                                    'start_time' => '00:00',
                                    'end_time' => '23:59',
                                ]
                            ],
                            'day_of_week' => 'sunday'
                        ],
                    ]
                ],
            ],
            'categories' => [],
            'items' => [],
            'modifier_groups' => [],
            'display_options' => [
                'disable_item_instructions' => true
            ],
        ];
        if(!$store_id){
            //参数错误
            return false;
        }
        $store = pdo_get('ims_tiny_wmall_store', array('id' => $store_id));
        if(!$store){
            //店铺不存在
            return false;
        }

        //获取店铺下的所有分类
        $categorys = pdo_getall('tiny_wmall_goods_category', array('sid' => $store_id, 'isdeleted' => 0));
        if(count($categorys) < 1){
            //没有分类
            return false;
        }
        $category_id_name = 'Category-';
        $prefix_group = 'GROUP-';
        foreach ($categorys as $key=>$category){
            $init_data['menus'][0]['category_ids'][] = $category_id_name.$category['id'];
        }

        $goods = pdo_getall('ims_tiny_wmall_goods', array('sid' => $store_id));
        $new_goods = [];
        $entities = [];
        $group_name = 'group-item-';//额外商品id
        foreach ($goods as $key=>$good){
            //1019 a:2:{i:0;a:2:{s:4:"name";s:6:"辣度";s:5:"label";a:3:{i:0;s:6:"微辣";i:1;s:6:"中辣";i:2;s:6:"特辣";}}i:1;a:2:{s:4:"name";s:3:"量";s:5:"label";a:2:{i:0;s:6:"大份";i:1;s:6:"小份";}}}
            //1047 a:1:{i:0;a:2:{s:4:"name";s:6:"包装";s:5:"label";a:3:{i:0;s:3:"大";i:1;s:3:"中";i:2;s:3:"小";}}}
            $new_goods[$good['id']] = $good;
            foreach ($categorys as $item){
                if($item['id'] == $good['cid']){
                    $entities[$category_id_name.$item['id']][] = [
                        'id' => 'Item-'.$good['id'],
                        'type' => 'ITEM',
                    ];
                }
            }

            $overrides = [];//商品附加物
            $group_ids = [];
            $num = 0;
            //商品附属物添加,并且添加商品
            $attrs = iunserializer($good['attrs']);
            foreach ($attrs as $key1=>$attr){
                $group_name .= $key.'-';
                $overrides[$good['id']][] = [
                    'context_type' => 'ITEM',
                    'context_value' => $prefix_group.($key1 + 1 + $key)
                ];
                $group_ids[$good['id']][] = $prefix_group.($key1 + 1 + $key);
                foreach ($attr['label'] as $item){
                    ++$num;
                    $init_data['items'][] = [
                        'id' => $group_name.$num,
                        'title' => [
                            'translations' => [
                                "zh_tw" => $item
                            ]
                        ],
                        'price_info' => [
                            'price' => 0,
                        ]
                    ];

                    //添加商品
                    $modifier_options[$key1 + 1 + $key][] = [
                        'id' => $group_name.$num,
                        'type' => 'ITEM'
                    ];
                }
                if(!empty($modifier_options)){
                    $group = [
                        'id' => $prefix_group.($key1 + 1 + $key),
                        'title' => [
                            'translations' => [
                                "zh_tw" => $attrs[$key1]['name']
                            ]
                        ],
                        'modifier_options' => $modifier_options[$key1 + 1 + $key]
                    ];
                    array_push($init_data['modifier_groups'], $group);
                }
                //MENU_TYPE_FULFILLMENT_PICK_UP 自提,MENU_TYPE_FULFILLMENT_DELIVERY 外送,不选 均可
                if($good['type'] == 1){
                    $init_data['menu_type'] = 'MENU_TYPE_FULFILLMENT_DELIVERY';
                }elseif ($good['type'] == 2){
                    $init_data['menu_type'] = 'MENU_TYPE_FULFILLMENT_PICK_UP';
                }
            }

            $init_data['items'][] = [
                'id' => 'Item-'.$good['id'],
                'external_data' => $good['data'],
                'title' => [
                    'translations' => [
                        "zh_tw" => $good['title']
                    ]
                ],
                'description' => [
                    'translations' => [
                        "zh_tw" => $good['content']
                    ]
                ],
                'image_url' => tomedia($good['thumb']),
                'price_info' => [
                    'price' => (int)$good['price'] * 100,
                ],
                'quantity_info' => [
                    'quantity' => [
                        'max_permitted' => (int)$good['unitnum'],
                        'default_quantity' => 1//默认数量
                    ],
                    'overrides' => $good['id'] ? $overrides[$good['id']] : [],
                ],
                'modifier_group_ids' => [
                    'ids' => $good['id'] ? $group_ids[$good['id']] : []
                ],
                /*'visibility_info' => [
                    'hours' => [
                        'hours_of_week' => [
                            'time_periods' => [
                                [
                                    "start_time"=>"00:00",
                                    "end_time"=>"23:59"
                                ]
                            ],
                            'day_of_week' => 'monday'
                        ]
                    ]
                ],*/
            ];
        }

        foreach ($init_data['menus'][0]['category_ids'] as $key=>$category_id){
            if(!empty($entities[$category_id])){
                $init_data['categories'][] = [
                    'id' => $category_id,
                    'title' => [
                        'translations' => [
                            "zh_tw" => $categorys[$key]['title']
                        ]
                    ],
                    'entities' => $entities[$category_id]
                ];
            }else{
                $init_data['categories'][] = [
                    'id' => $category_id,
                    'title' => [
                        'translations' => [
                            "zh_tw" => $categorys[$key]['title']
                        ]
                    ]
                ];
            }

        }

        return $init_data;
    }

    /**
     * 处理获取到的订单详情数据
     * @param $data
     */
    public function setOrderData($data){

    }


    /**
     * 对获取到的数据重新组装
     * @param $res json
     * @return array[]
     */
    public function dataReBuild($res){
        $categories = $res['data']['categories'] ?? [];
        $items = $res['data']['items'] ?? [];
        $modifier_groups = $res['data']['modifier_groups'] ?? [];

        $product_group = [];
        $attrs = [];
        $new_categories = [];
        $new_items = [];
        foreach ($categories as $category){
            $categories_id_arr = explode('-', $category['id']);
            $new_categories[$categories_id_arr[1]] = [
                'id' => $categories_id_arr[1],
                'title' => $category['title']['translations']['zh_tw']
            ];
            foreach ($category['entities'] as $entity){
                foreach ($items as $key=> $item){
                    if($item['id'] == $entity['id']){
                        if($res['data']['menu_type'] == 'MENU_TYPE_FULFILLMENT_DELIVERY'){
                            $type = 1;
                        }elseif ($res['data']['menu_type'] == 'MENU_TYPE_FULFILLMENT_PICK_UP'){
                            $type = 2;
                        }else{
                            $type = 3;
                        }

                        $entity_id_arr = explode('-', $entity['id']);
                        $new_items[$entity_id_arr[1]] = [
                            'id' => $entity_id_arr[1],
                            'data' => $item['external_data'],
                            'title' => $item['title']['translations']['zh_tw'],
                            'thumb' => tomedia($item['image_url']),
                            'content' => $item['description']['translations']['zh_tw'],
                            'unitnum' => $item['quantity_info']['quantity']['max_permitted'],
                            'price' => $item['price_info']['price'] / 100,
                            'type' => $type
                        ];

                        if(!empty($item['modifier_group_ids']['ids'])){
                            $product_group[$entity_id_arr[1]] = $item['modifier_group_ids']['ids'];
                        }

                    }
                }
            }
        }


        foreach ($product_group as $key1=>$groups){
            foreach ($modifier_groups as $key2=>$modifier_group){
                foreach ($groups as $group){
                    if($modifier_group['id'] == $group){
                        $attrs[$key1][$key2] = [
                            'name' => $modifier_group['title']['translations']['zh_tw'],
                            'label' => []
                        ];

                        foreach ($modifier_group['modifier_options'] as $modifier_option){
                            foreach ($items as $item){
                                if($modifier_option['id'] == $item['id']){
                                    array_push($attrs[$key1][$key2]['label'], $item['title']['translations']['zh_tw']);
                                }
                            }
                        }
                    }
                }
            }
        }
        foreach ($new_items as &$item){
            $item['attrs'] = $attrs[$item['id']] ? serialize($attrs[$item['id']]) : serialize([]);
        }

        return ['items' => $new_items, 'categories' => $new_categories];
    }

uber接口文档:https://developer.uber.com/docs/eats/api/v1/post-eats-store-storeid-status

菜鸟代码请绕到此地:https://download.csdn.net/download/weixin_31501115/20005098?spm=1001.2014.3001.5501

  数据结构与算法 最新文章
【力扣106】 从中序与后续遍历序列构造二叉
leetcode 322 零钱兑换
哈希的应用:海量数据处理
动态规划|最短Hamilton路径
华为机试_HJ41 称砝码【中等】【menset】【
【C与数据结构】——寒假提高每日练习Day1
基础算法——堆排序
2023王道数据结构线性表--单链表课后习题部
LeetCode 之 反转链表的一部分
【题解】lintcode必刷50题<有效的括号序列
上一篇文章      下一篇文章      查看所有文章
加:2021-07-15 23:50:43  更:2021-07-15 23:51:45 
 
开发: C++知识库 Java知识库 JavaScript Python PHP知识库 人工智能 区块链 大数据 移动开发 嵌入式 开发工具 数据结构与算法 开发测试 游戏开发 网络协议 系统运维
教程: HTML教程 CSS教程 JavaScript教程 Go语言教程 JQuery教程 VUE教程 VUE3教程 Bootstrap教程 SQL数据库教程 C语言教程 C++教程 Java教程 Python教程 Python3教程 C#教程
数码: 电脑 笔记本 显卡 显示器 固态硬盘 硬盘 耳机 手机 iphone vivo oppo 小米 华为 单反 装机 图拉丁

360图书馆 购物 三丰科技 阅读网 日历 万年历 2024年12日历 -2024/12/27 10:33:09-

图片自动播放器
↓图片自动播放器↓
TxT小说阅读器
↓语音阅读,小说下载,古典文学↓
一键清除垃圾
↓轻轻一点,清除系统垃圾↓
图片批量下载器
↓批量下载图片,美女图库↓
  网站联系: qq:121756557 email:121756557@qq.com  IT数码
数据统计