<?php

namespace app\admin\model;

use support\Model;

/**
 * 健康产品模型
 * Class Users
 * @package app\admin\model
 */
class HealthyProduct extends Model
{
    const ROLE_STATUS = 1;

    const IS_DEL_YES = 1;
    const IS_DEL_NO  = 0;
    const PRODUCT_IS_SHOW = [
        self::IS_DEL_YES => '已删除',
        self::IS_DEL_NO  => '未删除'
    ];
    /**
     * The table associated with the model.
     *
     * @var string
     */
    protected $table = 'healthy_product';

    public $timestamps = false;

    /**
     * Notes:获取健康产品列表
     * @param string $keywords
     * @param int $page
     * @param int $limit
     * @return array
     * User: ZQ
     * Date: 2022/11/9
     */
    public static function getProductList(int $page, int $limit, string $keywords)
    {
        $list = static::select(['healthy_product.*','merchant_shop_category.category_name','merchant_shop.shop_name'])
            ->where(['product_del'=>static::IS_DEL_NO])
            ->when($keywords != '', function ($query) use ($keywords){
                $query->where('product_name', 'like', '%' . $keywords . '%');
            })
            ->leftJoin('merchant_shop_category','category_id','=','healthy_product.product_category_id')
            ->leftJoin('merchant_shop','shop_id','=','healthy_product.product_shop_id')
            ->orderBy('product_sort','DESC')
            ->orderBy('product_create_time','DESC')
            ->forPage($page, $limit)
            ->get();
        $count = static::where(['product_del'=>static::IS_DEL_NO])
            ->when($keywords != '', function ($query) use ($keywords){
                $query->where('product_name', 'like', '%' . $keywords . '%');
            })
            ->count();
        return [$list, $count];
    }

    /**
     * Notes:获取所有健康产品
     * @return array
     * User: ZQ
     * Date: 2022/10/14
     */
    public static function getProductAll()
    {
        $list = static::select(['product_id','product_name'])
            ->where(['product_del'=>static::IS_DEL_NO])
            ->orderBy('product_add_time','DESC')
            ->get();
        return $list;
    }

    /**
     * Notes:获取健康产品名称
     * @param string $account
     * @return \Illuminate\Database\Eloquent\Builder|\Illuminate\Database\Eloquent\Model|object|null
     * User: ZQ
     * Date: 2022/10/14
     */
    public static function bookMation($admin_cinema)
    {
        $where = [];
        $where['product_del'] = static::IS_DEL_NO;
        $mation = static::where($where)
            ->when($admin_cinema != '', function ($query) use ($admin_cinema){
                $query->whereIn('product_id', $admin_cinema);
            })
            ->get('product_name');
        return $mation;
    }

}
