<?php
declare (strict_types = 1);

namespace app\model\system;

use Closure;
use library\basic\BaseModel;
use think\Model;

/**
 * @mixin \think\Model
 */
class WlnPro extends BaseModel
{
    public function getList($page,$where = [],$pageCount = 20,$filed = '*',$desc = ''){
        $data =  $this
            ->field("w.*,
            (SELECT name  from table_warehouse where id = w.warehouse_id) as warehouse_name,
            (SELECT title from table_express where id = w.express_id) as exp_name,
            (SELECT name  from table_platform where id = w.platform_id) as platform_name,
            (SELECT title from table_product where id = w.pro_id) as product_name
            ")
            ->alias("w")
            ->when(!empty($where),function ($query) use($where){
                foreach ($where as $k=>$v) {
                    if($v instanceof Closure) {
                        $v($query);
                    } else {
                        if(is_array($v)) {
                            //whereLike
                            if($v[1] == 'whereLike') {
                                if(!empty($v[0]))
                                    $query->whereLike($k,$v[0]);
                                continue;
                            }
                            if($v[1] == 'whereBetween') {
                                if(!empty($v[0]))  $query->whereBetween($k,$v[0]);
                                continue;
                            }


                            $bool = false;
                            eval('$bool = '.$v[1].'($v[0]);');
                            if($bool) {
                                $query->where($k,$v[0]);
                            }
                        } else {
                            $query->where($k,$v);
                        }
                    }
                }
            })
            ->order($desc)
            ->paginate(['list_rows'=>$pageCount,'page'=>$page])
            ->toArray();
        return [$data['total'],$data['data']];
    }
}
