<?php

namespace app\common;

use think\facade\Db;

class Goods
{
    public static function get_goods_prodcut_id($pid){
        //parse pid
        $t_pid = explode('-',$pid);
        $goods_id = $t_pid[0];
        //移除第一個元素,並由小到大排序
        array_shift($t_pid);

        sort($t_pid);

        $goods_attr = implode('|',$t_pid);

        //在product表中取出product_id並返回
        $res = Db::name('products')
        ->where('goods_id',$goods_id)
        ->where('goods_attr',$goods_attr)
        ->value('product_id');
        if(!$res){
            return 0;
        }
        return $res;
    }

    public static function get_goods_properties($id)
    {
        /* 對屬性進行重新排序和分組 */
        $grp = Db::name('goods_type')
            ->alias('gt')
            ->join('goods g', 'gt.cat_id=g.goods_type')
            ->where('g.goods_id', $id)
            ->value('attr_group');

        if (!empty($grp)) {
            $groups = explode("\n", strtr($grp, "\r", ''));
        }

         $res = Db::name('goods_attr')
        ->field('a.attr_id, a.attr_name, a.attr_group, a.is_linked, a.attr_type,g.goods_attr_id, g.attr_value, g.attr_price, g.attr_sort_order,g.thumb_url,g.img_url, g.img_original, hex_color')
        ->alias('g')
        ->join('attribute a', 'a.attr_id=g.attr_id')
        ->where('g.goods_id', $id)
        ->order('a.sort_order, a.attr_id, g.attr_sort_order, g.attr_price, g.goods_attr_id')
        ->select();

        $arr['pro'] = [];     // 屬性
        $arr['spe'] = [];     // 規格
        $arr['lnk'] = [];     // 關聯的屬性

        foreach ($res as $row) {
            $row['attr_value'] = str_replace("\n", '<br />', $row['attr_value']);

            if ($row['attr_type'] == 0) {
                $group = (isset($groups[$row['attr_group']])) ? $groups[$row['attr_group']] : '產品屬性';

                $arr['pro'][$group][$row['attr_id']]['name']  = $row['attr_name'];
                $arr['pro'][$group][$row['attr_id']]['value'] = $row['attr_value'];
            } else {
                $arr['spe'][$row['attr_id']]['attr_id'] = $row['attr_id'];
                $arr['spe'][$row['attr_id']]['attr_type'] = $row['attr_type'];
                $arr['spe'][$row['attr_id']]['name']     = $row['attr_name'];
                $arr['spe'][$row['attr_id']]['values'][] = array(
                    'label'        => $row['attr_value'],
                    'price'        => $row['attr_price'],
                    'thumb_url'        => $row['thumb_url'],
                    'img_url'        => $row['img_url'],
                    'img_original'        => $row['img_original'],
                    'hex_color'        => $row['hex_color'],
                    'format_price' => price_format(abs($row['attr_price']), false),
                    'id'           => $row['goods_attr_id']
                );
            }

            if ($row['is_linked'] == 1) {
                /* 如果該屬性需要關聯，先保存下來 */
                $arr['lnk'][$row['attr_id']]['name']  = $row['attr_name'];
                $arr['lnk'][$row['attr_id']]['value'] = $row['attr_value'];
            }
        }

        return $arr;
    }
}
