<?php

namespace app\api\controller\Product;

use app\BaseController;
use app\common\model\Product\ProductType as ModelProductType;
use app\common\arw\adjfut\src\Traverse;
use app\Request;
use think\Validate;
use think\exception\ValidateException;
use think\facade\Filesystem;
use app\common\arw\adjfut\src\Excel;
use app\common\arw\adjfut\src\UploadFile;
use app\common\exception\Tool;
use think\facade\Db;
use think\facade\Env;

class ProductType extends BaseController
{
    /**
     * 获取产品系列树形列表接口接口
     *
     * @param Request request
     * @date 2023-04-03
     * @example
     * @author xjh
     * @since 1.0.0
     */
    public function getProductTypeTree(Request $request): array
    {
        $con = Tool::getOptionalQuery(
            ['b.product_delete_time', 'NULL'],
            ['a.product_type_name', 'LIKE']
        );

        $product_type = ModelProductType::field([
            'a.product_type_parent_guid',
            'a.product_type_name',
            'b.product_type_name' => "product_type_parent_name",
            'a.product_type_order',
            'a.product_type_guid',
            'a.product_type_id',
            'a.product_type_icon',
            'a.product_type_cover',
        ])
            ->alias('a')
            ->leftjoin('product_type b', 'a.product_type_parent_guid = b.product_type_guid')
            ->where($con)
            ->order(['product_type_order' => 'asc'])
            ->select()->toArray();

        $Traverse = new Traverse('product_type_guid', 'product_type_parent_guid');
        $product_type_tree = $Traverse->tree($product_type, '0', function ($v) {
            return [
                'product_type_name' => $v['product_type_name'],
                'product_type_parent_name' => $v['product_type_parent_name'],
                'product_type_guid' => $v['product_type_guid'],
                'product_type_id' => $v['product_type_id'],
                'product_type_parent_guid' => $v['product_type_parent_guid'],
                'product_type_order' => $v['product_type_order'],
                'product_type_icon' => $v['product_type_icon'],
                'product_type_cover' => $v['product_type_cover'],
            ];
        });

        return msg("获取产品系列列表成功!", $product_type_tree);
    }
}
