<?php

namespace app\model;

use DateTimeInterface;
use support\Model;

class Goods extends Model
{
    protected $table = 'goods';

    protected $primaryKey = 'goods_id';

    protected $keyType = 'string';
    public $incrementing = false;

    protected $dateFormat = 'U';

    const CREATED_AT = 'goods_addtimes';

    const UPDATED_AT = 'goods_updatetimes';

    // 自动上架
    const LISTING_KEY_PREFIX = "GOODS:LISTING:ON:";

    // 自动下架
    const LISTING_OFF_KEY_PREFIX = "GOODS:LISTING:OFF:";


    public function serializeDate(DateTimeInterface $date)
    {
        return $date->format('Y-m-d H:i:s');
    }

    /**
     * @Desc 关联分类
     * @Author Gorden
     * @Date 2024/3/28 9:36
     *
     * @return \Illuminate\Database\Eloquent\Relations\HasOne
     */
    public function category()
    {
        return $this->hasOne(SysCategory::class, 'category_id', 'join_goods_category_id')
            ->select('category_id', 'category_name');
    }

    public function detail()
    {
        return $this->hasOne(GoodsDetail::class, 'join_detail_goods_id', 'goods_id');
    }

    /**
     * @Desc 关联GoodsRunning
     * @Author Gorden
     * @Date 2024/3/28 10:04
     *
     * @return \Illuminate\Database\Eloquent\Relations\HasOne
     */
    public function running()
    {
        return $this->hasOne(GoodsRunning::class, 'join_running_goods_id', 'goods_id');
    }

    public function supplier()
    {
        return $this->hasOne(Supplier::class, 'supplier_id', 'join_goods_supplier_id');
    }

    public function component()
    {
        return $this->hasMany(GoodsComponent::class, 'join_component_master_goods_id', 'goods_id');
    }

    public function sku()
    {
        return $this->hasMany(GoodsSku::class, 'join_sku_goods_id', 'goods_id')
//            ->where('goods_sku_status','ON')
            ->select('goods_sku_id', 'join_sku_goods_id', 'goods_sku_specs_json', 'goods_sku_sales_price', 'goods_sku_status', 'goods_sku_storage_json');
    }

    public function skuOne()
    {
        return $this->hasOne(GoodsSku::class, 'join_sku_goods_id', 'goods_id')
            ->select('goods_sku_id', 'join_sku_goods_id', 'goods_sku_specs_json', 'goods_sku_sales_price', 'goods_sku_status', 'goods_sku_storage_json');
    }

    public function user()
    {
        return $this->hasOne(SysUser::class, 'user_id', 'creator_user_id');
    }

    public function updateUser()
    {
        return $this->hasOne(SysUser::class, 'user_id', 'updator_user_id');
    }
}