SchoolController.php 3.2 KB
<?php
namespace app\admin\controller;

use app\admin\model\RouteModel;
use cmf\controller\AdminBaseController;
use think\Db;


class SchoolController extends AdminBaseController{

    /*
     * 学校管理列表
     * */
    public function school_list(){

        if($this -> request -> isPost()){
           if(!empty($_POST['start_time']) && !empty($_POST['end_time'])){
                $start_time = strtotime($_POST['start_time']);
                $end_time = strtotime($_POST['end_time']);
                $where['create_time'] = [['>=',$start_time],['<=',$end_time]];
           }
           if(!empty($_POST['keyword'])){
                $keyword = $_POST['keyword'];
                $where['school'] = ['like',"%$keyword%"];
           }
            $data1 = Db::name('school') -> where($where) -> paginate(1000000) ;
            $data = $data1 -> toArray();
        }else{
            $data1 = Db::name('school') -> paginate(12) ;
            $data = $data1 -> toArray();
        }
        foreach ($data['data'] as $key => $val){
            $user_data = Db::name('user') -> where('id',$val['uid']) -> find();
            $data['data'][$key]['uname'] = $user_data['user_nickname'];
        }
        $this -> assign('data',$data['data']);
        $this -> assign('res',$data1);
        return $this -> fetch();

    }

    /*
     * 学校管理编辑
     * */
    public function school_edit(){

        $id = $this -> request -> param();
        $data = Db::name('school') -> where('id',$id['ids']) -> find();
        $data_grade = Db::name('grade_class') -> where('school_id',$id['ids']) -> select();
        $grade_id = [];
        $grade_class = [];
        foreach ($data_grade as $key => $val){
            $grade_id[] = $val['id'];
            $grade_class[] = $val['grade'].','.$val['class'];
        }
        $grade_id_str = implode('-',$grade_id);
        $grade_class_str = implode('|',$grade_class);
        $this -> assign('data',$data);
        $this -> assign('grade_class_str',$grade_class_str);
        $this -> assign('grade_id_str',$grade_id_str);
        $province = Db::name('area')->where(['pid'=>0])->select();
        $this->assign('province',$province);
        return $this -> fetch();

    }


    /*
     * 学校管理编辑提交 修改数据
     * */
    public function school_edit_post(){

        $data['id'] = $_POST['id'];
        $data['region'] = $_POST['region'];
        $data['school'] = $_POST['school'];
        $data['type'] = $_POST['type'];
        $res = Db::name('school') -> update($data);
        $gread = explode('|',$_POST['grade_class_str']);
        foreach($gread as $key => $val){
            $greads[] = explode(',',$gread[$key]);
        }
        $id = explode('-',$_POST['grade_id_str']);
        foreach($id as $key => $val){
            Db::name('grade_class') -> where('id',$id[$key]) -> update(['grade'=>$greads[$key][0],'class'=>$greads[$key][1]]);
        }
            $this -> success('保存成功',url('School/school_edit',array('ids'=>$_POST['id'])));

    }

    /**
     * 删除数据
     *
     */
    public function school_del(){

        $id = $_POST['id'];
        Db::name('school') -> delete($id);
        return true;

    }




















}