tp5复杂查询自己用的仅做参考

388 阅读1分钟
        

 $model = $model->hasWhere('order',function($query) use($status, $userId){ $query->where(['Order.status' => $status, 'Order.user_id' => $userId]); // 模糊搜索 }) // ->scope('where', $param) ->where('UserOrder.status', $status) ;


    public function base()
    {
        // 设备扩展字段
        $equExtObj = new EquExt();
        $equExt = Db::query('SHOW COLUMNS FROM `lyx_equ_ext`');
        $fileType = config('setting.ext_type');
        $where['type'] = $fileType[0]['id'];
        $equExtDescObj = new EquExtDesc();
        if($this->user['com_id']){
            $where['com_id'] = $this->user['com_id'];
        }
        $equExtDesc = $equExtDescObj->lyxAll($where);
        if (count($equExtDesc) == 0) {
            $equExtWhere = $where;
            $where['com_id'] = 0;
            $oldExtDesc = $equExtDescObj->where($equExtWhere)->select();
            $oldExtDesc = $oldExtDesc->toArray();
            foreach ($oldExtDesc as $key => &$value) {
                $value['user_id'] = $this->user['id'];
                $value['com_id'] = $this->user['com_id'];
                unset($value['id']);
                unset($value['create_time']);
                unset($value['update_time']);
            }
            $equExtDescObj->saveAll($oldExtDesc);
            $equExtDesc = $equExtDescObj->lyxAll($equExtWhere);
        }

        // 生产商
        $objProSup = new ProSup();
        $arrProSup = config('setting.prosup');
        $proWhere['type'] = $arrProSup[0]['id'];
        $arrPro = $objProSup->lyxAll($proWhere);
        // 供应商
        $supWhere['type'] = $arrProSup[1]['id'];
        $arrSup = $objProSup->lyxAll($supWhere);

        // 使用状况
        $objPullDown = new PullDown();
        $arrPullDown = config('equ.type_arr');
        $bjWhere['type'] = $arrPullDown[1]['id'];
        $arrUseStatus = $objPullDown->lyxAll($bjWhere);
        // 使用部门
        $objDep = new Department();
        $arrDep = $objDep->lyxAll();
        $this->assign([
            'arrDep'  => $arrDep,
            'equExt'  => $equExt,
            'equExtDesc'  => $equExtDesc,
            'arrPro'  => $arrPro,
            'arrSup'  => $arrSup,
            'arrUseStatus' => $arrUseStatus,
            
        ]);
    }


        $data = $model->field($field)->append(['int_start_time','api_start_time','api_end_time','execut_id_text','status_text','end_num_int'])
            ->where(function ($query) use ($now) {
                $query->where('end_time', ['gt', $now], ['eq', 0], 'or');
            })
            // ->where(function ($query) use ($now) {
            //     $query->where('order_start_time', '>', $now)
            //         ->where('order_end_time', '<', $now);
            // })
            // ->where('order_start_time', '>', $now)
            ->where('order_end_time', '>', $now)
            // ->group('plan_id')
            // ->order('id asc')
            ->with(['bstandards'])
            ->page($this->page, $this->limit)->select();