SchoolController.php 7.8 KB
<?php
// +----------------------------------------------------------------------
// | bronet [ 以客户为中心 以奋斗者为本 ]
// +----------------------------------------------------------------------
// | Copyright (c) 2013-2017 http://www.bronet.cn All rights reserved.
// +----------------------------------------------------------------------
// | Licensed ( http://www.apache.org/licenses/LICENSE-2.0 )
// +----------------------------------------------------------------------
namespace app\portal\controller;

use cmf\controller\WeChatBaseController;
use think\Db;
class SchoolController extends WeChatBaseController{

    /**
     * 显示学校管理页
     */
    public function school(){

        $uid = cmf_get_current_user_id();
        // 获取用户身份
        $my_user = Db::name('my_user')->where('uid',$uid)->find();
        // 如果为业务员,获取所有下线老师
        $ids = [];
        if($my_user['status'] == 2) {
            $ids = Db::name('my_user')->where('pid',$my_user['id'])->column('uid');
        }
        $ids[] = $uid;
        $school = Db::name('school') -> whereIn("uid",$ids) -> order('id desc') -> select() -> toArray();
        foreach ($school as $key => $val){
            $school[$key]['class'] = Db::name('grade_class') -> where('school_id',$val['id']) -> select() -> toArray();
            $school[$key]['create_user'] = '';
            if($val['uid'] != $uid) {
                $school[$key]['create_user'] = Db::name('user')->where('id',$val['uid'])->value('user_nickname');
            }
        }
        $this -> assign('school',$school);
        return $this -> fetch();

    }

    /**
     * 添加学校页
     */
    public function add_school(){

        if($this -> request -> isPost()){
            $class = explode(',',$_POST['str']);
            foreach ($class as $key => $val){
                if($val != '' && $val != 'undefined'){
                    $class_data[] = $val;
                }
            }
            //获取所有学校
            $school_data = Db::name('school') -> select() -> toArray();
            foreach ($school_data as $key => $val){
                $school_data[$key]['grade'] = Db::name('grade_class') -> where("school_id",$val['id']) -> select() -> toArray();
            }
            //循环遍历比对
            foreach ($school_data as $key => $val){
                //如果地区 学校 类型相同就进行下一步比对
                if($_POST['region']==$val['region'] && $_POST['school']==$val['school'] && $_POST['type']==$val['type']){
                    foreach ($val['grade'] as $key1 => $val1){
                        //循环传过来的年级
                        foreach ($class_data as $key2 => $val2){
                            if($_POST['type'] == '学龄前'){
                                if(in_array($val2,explode('-',$val1['class']))) {
                                    return 5;
                                }
                            } else {
                                $val2 = explode('-',$val2);
                                if($val2[0] == $val1['grade']){
                                    //将班级拆分循环装进数组
                                    $class_s = explode('-',$val1['class']);
                                    for($i=$class_s[0];$i<=$class_s[1];$i++){
                                        $class_db[] = $i;
                                    }
                                    //将传过来的班级循环
                                    for($j=$val2[1];$j<=$val2[2];$j++){
                                        $class_my[] = $j;
                                    }
                                    $result = array_intersect($class_db,$class_my);
                                    $result = count($result);
                                    if($result > 0){
                                        return 5;
                                    }
                                }
                            }
                        }
                    }
                }
            }
            $school['uid'] = cmf_get_current_user_id();
            $school['create_time'] = time();
            $school['region'] = $_POST['region'];
            $school['school'] = $_POST['school'];
            $school['type'] = $_POST['type'];
            $data = Db::name('school')->where(['region'=>$_POST['region'],'school'=>$_POST['school']])->find();
            if(!empty($data)){
                return 5;
            }
            $school_id = Db::name('school') -> insertGetId($school);
            // 判断是否是学龄前
            if($_POST['type'] == '学龄前') {
                $grade_class = implode('-',$class_data);
                $grade['school_id'] = $school_id;
                $grade['grade'] = $_POST['type'];
                $grade['class'] = $grade_class;
                Db::name('grade_class') -> insert($grade);
            } else {
                foreach ($class_data as $key => $val) {
                    $grade_class = explode('-', $val);
                    $grade['school_id'] = $school_id;
                    $grade['grade'] = $grade_class[0];
                    $grade['class'] = $grade_class[1] . '-' . $grade_class[2];
                    Db::name('grade_class')->insert($grade);
                }
            }
            if($school_id){
               return true;
            }else{
                return false;
            }

        }else{

            return $this -> fetch();
        }

    }

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

        if($this -> request -> isPost()){
            $school['id'] = $_POST['id'];
            $school['school'] = $_POST['school'];
            $school['type'] = $_POST['type'];
            $school['region'] = $_POST['region'];
            $updae = Db::name('school') -> update($school);
            Db::name('grade_class') -> where('school_id',$_POST['id']) -> delete();
            $class = explode(',',$_POST['str']);
            foreach ($class as $key => $val){
                if($val != '' && $val != 'undefined'){
                    $class_data[] = $val;
                }
            }
            // 判断是否是学龄前
            if($_POST['type'] == '学龄前') {
                $grade_class = implode('-',$class_data);
                $grade['school_id'] = $_POST['id'];
                $grade['grade'] = $_POST['type'];
                $grade['class'] = $grade_class;
                Db::name('grade_class') -> insert($grade);
            } else {
                foreach ($class_data as $key => $val){
                    $grade_class = explode('-',$val);
                    $grade['school_id'] = $_POST['id'];
                    $grade['grade'] = $grade_class[0];
                    $grade['class'] = $grade_class[1].'-'.$grade_class[2];
                    Db::name('grade_class') -> insert($grade);
                }
            }
            return true;
        }else{
            $id = $this -> request -> param();
            $data = Db::name('school') -> where('id',$id['id']) -> find();
            $data_class = Db::name('grade_class') -> where('school_id',$id['id']) -> select();
            $classes_str = '';
            if($data_class) {
                foreach ($data_class as $k=>$v) {
                    $classes_str .= $v['grade'].'-'.$v['class'].',';
                }
            }
            $this->assign('data',$data);
            $this->assign('classes_str',$classes_str);
            return $this -> fetch();
        }

    }

    /**
     * 删除学校
     */
    public function school_del(){

        $data = Db::name('school') -> delete($_POST['id']);
        Db::name('grade_class') -> where('school_id',$_POST['id']) -> delete();
        if($data){
            return true;
        }else{
            return false;
        }

    }



















}