<?php
declare (strict_types = 1);

namespace app\model\admin;

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

/**
 * @mixin \think\Model
 */
class SiteDetail extends BaseModel
{
    /**
     * 获取列表数据
     * @param $page
     * @param $where
     * @param $pageCount
     * @param $desc
     */
    public function getList($page,$where = [],$pageCount = 20,$filed = '*',$desc = ''){
        $data = $this
            ->field("s.type,s.code,s.time,s.title,s.content,s.v,s.money,s.order_id")
            ->alias("s")
            ->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();
        //echo $this->getLastSql();
        foreach ($data['data'] as $k=>$v) {
            $data['data'][$k]['all_price'] = (new Order)->where('order_id', $v['order_id'])->value("all_price");
        }
        return [$data['total'],$data['data']];
    }

}
