<?php

namespace App\Models;

use Dcat\Admin\Traits\HasDateTimeFormatter;
use Illuminate\Database\Eloquent\Relations\BelongsTo;
use Illuminate\Database\Eloquent\Relations\HasMany;
use Illuminate\Database\Eloquent\SoftDeletes;
use Illuminate\Database\Eloquent\Model;

/**
 * App\Models\Product
 *
 * @property int $id
 * @property string $name 名称
 * @property string $cover_img 封面图
 * @property string $cases 案例
 * @property string $origin_price 原价
 * @property string $sale_price 现价
 * @property int $sort 排序越大越靠前
 * @property int $is_opened 上架状态 0-下架 1-上架
 * @property string $tech_param 集数参数文件
 * @property string $cad_model CAD模型文件
 * @property string $cad_design CAD设计文件
 * @property string $su_model SU模型文件
 * @property string $other 其他文件
 * @property \Illuminate\Support\Carbon|null $updated_at
 * @property \Illuminate\Support\Carbon|null $deleted_at
 * @property \Illuminate\Support\Carbon|null $created_at
 * @method static \Illuminate\Database\Eloquent\Builder|Product newModelQuery()
 * @method static \Illuminate\Database\Eloquent\Builder|Product newQuery()
 * @method static \Illuminate\Database\Query\Builder|Product onlyTrashed()
 * @method static \Illuminate\Database\Eloquent\Builder|Product query()
 * @method static \Illuminate\Database\Eloquent\Builder|Product whereCadDesign($value)
 * @method static \Illuminate\Database\Eloquent\Builder|Product whereCadModel($value)
 * @method static \Illuminate\Database\Eloquent\Builder|Product whereCases($value)
 * @method static \Illuminate\Database\Eloquent\Builder|Product whereCoverImg($value)
 * @method static \Illuminate\Database\Eloquent\Builder|Product whereCreatedAt($value)
 * @method static \Illuminate\Database\Eloquent\Builder|Product whereDeletedAt($value)
 * @method static \Illuminate\Database\Eloquent\Builder|Product whereId($value)
 * @method static \Illuminate\Database\Eloquent\Builder|Product whereIsOpened($value)
 * @method static \Illuminate\Database\Eloquent\Builder|Product whereName($value)
 * @method static \Illuminate\Database\Eloquent\Builder|Product whereOriginPrice($value)
 * @method static \Illuminate\Database\Eloquent\Builder|Product whereOther($value)
 * @method static \Illuminate\Database\Eloquent\Builder|Product whereSalePrice($value)
 * @method static \Illuminate\Database\Eloquent\Builder|Product whereSort($value)
 * @method static \Illuminate\Database\Eloquent\Builder|Product whereSuModel($value)
 * @method static \Illuminate\Database\Eloquent\Builder|Product whereTechParam($value)
 * @method static \Illuminate\Database\Eloquent\Builder|Product whereUpdatedAt($value)
 * @method static \Illuminate\Database\Query\Builder|Product withTrashed()
 * @method static \Illuminate\Database\Query\Builder|Product withoutTrashed()
 * @mixin \Eloquent
 * @property int $cate_id 分类ID
 * @property-read \App\Models\ProductCategory|null $cate
 * @method static \Illuminate\Database\Eloquent\Builder|Product whereCateId($value)
 * @property-read \Illuminate\Database\Eloquent\Collection|\App\Models\ProductSpec[] $specs
 * @property-read int|null $specs_count
 * @property-write mixed $files
 * @property-read \Illuminate\Database\Eloquent\Collection|\App\Models\StatProductDownload[] $download
 * @property-read int|null $download_count
 * @property-read \Illuminate\Database\Eloquent\Collection|\App\Models\StatProduct[] $viewer
 * @property-read int|null $viewer_count
 */
class Product extends Model
{
	use HasDateTimeFormatter;
    use SoftDeletes;

    protected $casts = [
        'cases' => 'json',
        'tech_param' => 'json',
        'cad_model' => 'json',
        'cad_design' => 'json',
        'su_model' => 'json',
        'other' => 'json',
    ];


    public function cate(): BelongsTo
    {
        return $this->belongsTo(ProductCategory::class,'cate_id','id');
    }

    public function specs(): HasMany
    {
        return $this->hasMany(ProductSpec::class,'product_id');
    }

    public function setFilesAttribute(Product $product)
    {
        $files = [];
        foreach ($product->tech_param as $item){
            $files[] = [
                'name' => '产品技术参数下载',
                'url' => $item['url']
            ];
        }
        foreach ($product->cad_model as $item){
            $files[] = [
                'name' => '产品CAD模型下载',
                'url' => $item['url']
            ];
        }
        foreach ($product->cad_design as $item){
            $files[] = [
                'name' => '产品CAD设计下载',
                'url' => $item['url']
            ];
        }
        foreach ($product->su_model as $item){
            $files[] = [
                'name' => '产品SU模型下载',
                'url' => $item['url']
            ];
        }
        $files = array_merge($files,$product->other);
        $this->attributes['files'] = $files;
    }

    public function viewer(): HasMany
    {
        return $this->hasMany(StatProduct::class,'product_id','id');
    }

    public function download(): HasMany
    {
        return $this->hasMany(StatProductDownload::class,'product_id','id');
    }
 }
