SchoolController.php 5.4 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(){
        $param = $this->request->param();
        $where = [];
        if(!empty($param['start_time']) && !empty($param['end_time'])){
            $start_time = strtotime($param['start_time']);
            $end_time = strtotime($param['end_time']);
            $where['create_time'] = [['>=',$start_time],['<=',$end_time]];
        }
        if(!empty($param['keyword'])){
            $keyword = trim($param['keyword']);
            $where['school'] = ['like',"%$keyword%"];
        }
        $data1 = Db::name('school')->where($where)->order('create_time','DESC') -> paginate(12);
        $data1->appends($param);
        $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('start_time',!empty($param['start_time']) ? $param['start_time'] : '');
        $this->assign('end_time',!empty($param['end_time']) ? $param['end_time'] : '');
        $this->assign('keyword',!empty($param['keyword']) ? trim($param['keyword']) : '');
        $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);
        $area = explode(' ',$data['region']);
        $province = Db::name('area')->where(['level'=>1])->select();
        $data_city = Db::name('area')->where(['level'=>2,'name'=>$area[1]])->find();
        $city = Db::name('area')->where(['pid'=>$data_city['pid'],'level'=>2])->select();
        $data_county = Db::name('area')->where(['level'=>3,'name'=>$area[2]])->find();
        $county = Db::name('area')->where(['level'=>3,'pid'=>$data_county['pid']])->select();
        $this->assign('province',$province);
        $this->assign('city',$city);
        $this->assign('county',$county);
        return $this -> fetch();

    }
    /**
     * 获取城市
     * @return string
     * @throws \think\db\exception\DataNotFoundException
     * @throws \think\db\exception\ModelNotFoundException
     * @throws \think\exception\DbException
     */
    public function select_city(){
        $province_name = $this->request->param('province_name');
        if(!isset($province_name)){
            $arr['code'] = 40005;
            $arr['msg'] = '缺少必要参数';
        }else{
            $province = Db::name('area')->where(['name'=>$province_name,'level'=>1])->find();
            $city = Db::name('area')->where(['pid'=>$province['id'],'level'=>2])->select();
            $arr['code'] = 20000;
            $arr['msg'] = 'SUCCESS';
            $arr['data'] = $city;
        }
        return json_encode($arr);
    }

    /**
     * 获取区/县
     * @return string
     * @throws \think\db\exception\DataNotFoundException
     * @throws \think\db\exception\ModelNotFoundException
     * @throws \think\exception\DbException
     */
    public function select_county(){
        $city_name = $this->request->param('city_name');
        if(!isset($city_name)){
            $arr['code'] = 40005;
            $arr['msg'] = '缺少必要参数';
        }else{
            $city = Db::name('area')->where(['name'=>$city_name,'level'=>2])->find();
            $county = Db::name('area')->where(['pid'=>$city['id'],'level'=>3])->select();
            $arr['code'] = 20000;
            $arr['msg'] = 'SUCCESS';
            $arr['data'] = $county;
        }
        return json_encode($arr);
    }


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

        $data['id'] = $_POST['id'];
        $data['region'] = $_POST['province']." ".$_POST['city']." ".$_POST['county'];
        $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;

    }




















}