<?php

namespace app\api\controller\v1;

use app\BaseController;
use app\model\api\Warehouse;
use app\model\api\SystemCity;
use app\model\system\Sys;
use app\Request;
use library\services\UtilService;
use library\services\CacheService;

class Pub extends BaseController
{

    /**
     * 站点信息
     */
    public function siteResouce() {
        $info = Sys::where("id", 1)->find()->toArray();
        return app('json')->success($info);
    }

    /**
     * 获取图片base64
     * @param Request $request
     * @return mixed
     * @throws Exception
     */
    public function get_image_base64(Request $request)
    {
        list($imageUrl, $codeUrl) = UtilService::getMore([
            ['image', ''],
            ['code', ''],
        ], $request, true);
        try {
            $codeTmp = $code = $codeUrl ? image_to_base64($codeUrl) : false;
            if (!$codeTmp) {
                $putCodeUrl = put_image($codeUrl);
                $code = $putCodeUrl ? image_to_base64($_SERVER['HTTP_HOST'] . '/' . $putCodeUrl) : false;
                $code ?? unlink($_SERVER["DOCUMENT_ROOT"] . '/' . $putCodeUrl);
            }

            $imageTmp = $image = $imageUrl ? image_to_base64($imageUrl) : false;
            if (!$imageTmp) {
                $putImageUrl = put_image($imageUrl);
                $image = $putImageUrl ? image_to_base64($_SERVER['HTTP_HOST'] . '/' . $putImageUrl) : false;
                $image ?? unlink($_SERVER["DOCUMENT_ROOT"] . '/' . $putImageUrl);
            }
            return app('json')->successful(compact('code', 'image'));
        } catch (Exception $e) {
            return app('json')->fail($e->getMessage());
        }
    }


    /**
     * 获取仓库列表
     * @param Request $request
     */
    public function warehouse(Request $request) {
       [$isExp] = UtilService::getMore([
            ['isExp',''],
        ],$request,true);
        $warehouse = new Warehouse;
        $data = $warehouse->field("name,id,platform_ids")
            ->where("status",1)
            ->order("seq","desc")
            ->select()
            ->toArray();
        $platform = (new \app\model\system\Platform());
        foreach ($data as $k => $v) {
            $idsAr = explode(',',$v['platform_ids']);
            $platformAr = [];
            $data[$k]['platform']  = array_map(
                function ($item) use($platform){
                    return  $platform->getPlatformId($item,'*');
                },$idsAr);
        }

        return app('json')->success($data);
    }

    /**
     * 查找城市数据
     * @param Request $request
     * @return mixed
     * @throws \throwable
     */
    public function city_list(Request $request)
    {
        $list = CacheService::get('CITY_LIST', function () {
            $list = SystemCity::with('children')->field(['city_id', 'name', 'id', 'parent_id'])->where('parent_id', 0)->order('id asc')->select()->toArray();
            $data = [];
            foreach ($list as &$item) {
                $value = ['v' => $item['city_id'], 'n' => $item['name']];
                if ($item['children']) {
                    foreach ($item['children'] as $key => &$child) {
                        $value['c'][$key] = ['v' => $child['city_id'], 'n' => $child['name']];
                        unset($child['id'], $child['area_code'], $child['merger_name'], $child['is_show'], $child['level'], $child['lng'], $child['lat'], $child['lat']);
                        if (SystemCity::where('parent_id', $child['city_id'])->count()) {
                            $child['children'] = SystemCity::where('parent_id', $child['city_id'])->field(['city_id', 'name', 'id', 'parent_id'])->select()->toArray();
                            foreach ($child['children'] as $kk => $vv) {
                                $value['c'][$key]['c'][$kk] = ['v' => $vv['city_id'], 'n' => $vv['name']];
                            }
                        }
                    }
                }
                $data[] = $value;
            }
            return $data;
        }, 0);
        return app('json')->successful($list);
    }





}
