<?php
namespace app\admin\controller;
use think\Controller;
use think\Db;
use Think\Config;
// +----------------------------------------------------------------------
// | ThinkPHP [ WE CAN DO IT JUST THINK ]
// +----------------------------------------------------------------------
// | Copyright (c) 2021 http://mifenlei.com All rights reserved.
// +----------------------------------------------------------------------
// | Author: 梦雨 <50361804@qq.com>
// +----------------------------------------------------------------------
class Model extends Common
{
    //模型管理
    public function index()
    {
		return md5Qm();

    }
    // 创建模型
    public function modeladd()
    {
        if (request()->isPost()) {
            $this->error(config('web.sq')); 
        }
		return md5Qm();
    }
    // 模型分类管理
    public function cate()
    {
		return md5Qm();
    }
    // 模型分类添加编辑
    public function cate_add()
    {
        $c=Db::name('cate_m')->where(array('id' => get('id',0)))->find();
        $this->assign('c',$c);
		return md5Qm();
    }
    // 模型内容
    public function model_index()
    {
        $id = input('id');
        $this->assign('id', $id);
        //输出模型
        $model = input('model');
        $this->assign('model', $model);
        $page = isset($_REQUEST['page']) ? $_REQUEST['page'] : 1;
        $perpage = isset($_REQUEST['perpage']) ? $_REQUEST['perpage'] : 10;
        $keyword = isset($_REQUEST['keyword']) ? trim($_REQUEST['keyword']) : '';
        $tid = isset($_REQUEST['tid']) ? trim($_REQUEST['tid']) : 0;
        $open = isset($_REQUEST['open']) ? trim($_REQUEST['open']) : '';
        $settop = isset($_REQUEST['settop']) ? trim($_REQUEST['settop']) : '';
        $map = [];
        if ($open != '') {
            $map['f.open'] = $open;
        }
        if ($settop != '') {
            $map['f.settop'] = $settop;
        }
        if ($keyword) {
            $map['f.title'] = ['like', "%{$keyword}%"];
        }
        if ($tid == 0) {
            $html = Db::name($model)->alias('f')->join('cate_m'.' c', 'c.id=f.tid')->field('f.*,c.id as cid')->order('f.times desc,f.id desc')->where($map)->paginate($perpage, false, $config = ['query' => array('settop' => $settop, 'open' => $open, 'tid' => $tid, 'keyword' => $keyword, 'perpage' => $perpage)]);
        } else {
            $html = Db::name($model)->alias('f')->join('cate_m'.' c', 'c.id=f.tid')->field('f.*,c.id as cid')->where("f.tid=$tid or c.tid=$tid")->order('f.times desc,f.id desc')->where($map)->paginate($perpage, false, $config = ['query' => array('settop' => $settop, 'open' => $open, 'tid' => $tid, 'keyword' => $keyword, 'perpage' => $perpage)]);
        }
        $this->assign(array('settop' => $settop, 'open' => $open, 'tid' => $tid, 'keyword' => $keyword, 'perpage' => $perpage));
        return $this->fetch('model_index', ['html' => $html]);
    }
    // 添加编辑模型内容
    public function add()
    {
        $c=Db::name(get('model'))->find(get('id',0));
        $this->assign('c',$c);
		return md5Qm();
    }
    // 地区管理
    public function diqu()
    {
		return md5Qm();
    }
    // 地区添加编辑
    public function diqu_add()
    {
        $c=Db::name('cate_diqu')->where(array('id' => get('id',0)))->find();
        $this->assign('c',$c);
		return md5Qm();
    } 
    // 模型配置
    public function model_config()
    {
		$c=Db::name('model')->where(array('id' => get('id',0)))->find();
        $this->assign('c',$c);
        $sql=sql("select *from information_schema.columns where table_name='my_".$c['name']."' ORDER BY ORDINAL_POSITION");
        $this->assign('sql',$sql);
        return md5Qm();
    }
}