1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
<?php
 
namespace app\common\model\plus\advance;
 
use app\common\model\BaseModel;
 
/**
 * 预售商品模型
 */
class Product extends BaseModel
{
    protected $name = 'advance_product';
    protected $pk = 'advance_product_id';
 
    protected $append = ['product_sales', 'active_time'];
 
    /**
     * 计算显示销量 (初始销量 + 实际销量)
     */
    public function getProductSalesAttr($value, $data)
    {
        return $data['sales_initial'] + $data['total_sales'];
    }
 
    public function getActiveTimeAttr($value, $data)
    {
        return $data['start_time'] && $data['end_time'] ? [date("Y-m-d H:i:s", $data['start_time']), date("Y-m-d H:i:s", $data['end_time'])] : [];
    }
 
    public static function detail($advance_product_id, $with = ['product.sku', 'sku'])
    {
        return (new static())->with($with)->where('advance_product_id', '=', $advance_product_id)->find();
    }
 
    public function product()
    {
        return $this->belongsTo('app\\common\\model\\product\\Product', 'product_id', 'product_id');
    }
 
    public function sku()
    {
        return $this->hasMany('app\\common\\model\\plus\\advance\\AdvanceSku', 'advance_product_id', 'advance_product_id');
    }
 
    /**
     * 关联供应商
     */
    public function supplier()
    {
        return $this->belongsTo('app\\common\\model\\supplier\\Supplier', 'shop_supplier_id', 'shop_supplier_id');
    }
 
    //查询预售商品详情
    public static function getProductAdvanceDetail($product_id)
    {
        $detail = (new static)->with(['product.image.file', 'sku.productSku.image'])
            ->where('product_id', '=', $product_id)
            ->where('end_time', '>', time())
            ->where('status', '=', 10)
            ->where('audit_status', '=', 20)
            ->where('is_delete', '=', 0)
            ->find();
        return $detail;
    }
 
 
}