<?php

namespace app\admin\model;

use support\Model;

/**
 * 会员智能设备消息推送日志模型
 * Class Users
 * @package app\admin\model
 */
class IntelligenceEquipment extends Model
{
    /**
     * The table associated with the model.
     *
     * @var string
     */
    protected $table = 'user_intelligence_equipment';

    public $timestamps = false;

    /**
     * Notes:会员智能设备数据查询
     * @return int
     * User: ZQ
     * Date: 2022/9/19
     */
    public static function equipmentList(int $page,int $limit,$equipment_user_id, $equipment_name, $equipment_class,$equipment_tripartite_sn,$equipment_create_time)
    {
        
        $list = static::select(['user_intelligence_equipment.*','user.user_nickname','intelligence_products.product_name','user.user_phone'])
            ->where(['equipment_del'=>0])
            ->leftJoin('user','user_id','=','user_intelligence_equipment.equipment_user_id')
            ->leftJoin('intelligence_products','product_id','=','user_intelligence_equipment.equipment_intelligence_id')
            ->when($equipment_user_id != '', function ($query) use ($equipment_user_id){
                $query->where('equipment_user_id', $equipment_user_id);
            })
            ->when($equipment_class != '', function ($query) use ($equipment_class){
                $query->where('equipment_class', $equipment_class);
            })
            ->when($equipment_name != '', function ($query) use ($equipment_name){
                $query->where('equipment_name',  'like', '%'.$equipment_name.'%');
            })
            ->when($equipment_tripartite_sn != '', function ($query) use ($equipment_tripartite_sn){
                $query->where('equipment_tripartite_sn',$equipment_tripartite_sn);
            })
            ->when($equipment_create_time != '', function ($query) use ($equipment_create_time){
                $start = strtotime($equipment_create_time);
                $end = $start+24*60*60;
                $query->whereBetween('equipment_create_time',[$start,$end]);
            })
            ->forPage($page, $limit)
            ->orderBy('equipment_create_time','DESC')
            ->get();
        $count = static::select()
            ->where(['equipment_del'=>0])
            ->when($equipment_user_id != '', function ($query) use ($equipment_user_id){
                $query->where('equipment_user_id', $equipment_user_id);
            })
            ->when($equipment_class != '', function ($query) use ($equipment_class){
                $query->where('equipment_class', $equipment_class);
            })
            ->when($equipment_name != '', function ($query) use ($equipment_name){
                $query->where('equipment_name', 'like', '%'.$equipment_name.'%');
            })
            ->when($equipment_tripartite_sn != '', function ($query) use ($equipment_tripartite_sn){
                $query->where('equipment_tripartite_sn',$equipment_tripartite_sn);
            })
            ->when($equipment_create_time != '', function ($query) use ($equipment_create_time){
                $start = strtotime($equipment_create_time);
                $end = $start+24*60*60;
                $query->whereBetween('equipment_create_time',[$start,$end]);
            })
            ->count();
        return [$list,$count];
    }

    /**
     * Notes:会员智能设备数据详情
     * @return int
     * User: ZQ
     * Date: 2022/9/23
     */
    public static function equipmentInfor(array $where)
    {
        return static::where($where)
            ->leftJoin('user','user_id','=','user_intelligence_equipment.equipment_user_id')
            ->leftJoin('intelligence_products','product_id','=','user_intelligence_equipment.equipment_intelligence_id')
            ->first(['user_intelligence_equipment.*','user.user_nickname','intelligence_products.product_name','user.user_phone']);
    }

    /**
     * Notes:会员智能设备数据插入
     * @return int
     * User: ZQ
     * Date: 2022/9/19
     */
    public static function equipmentInsert($data)
    {
        $id = static::insertGetId($data);
        return $id;
    }

}
