<?php
namespace app\teacher\controller;
use app\base\controller\AdminBase;use app\teacher\model\Teacher as TC;use app\tools\controller\File;
class Index extends AdminBase
{
public function index()
{
$list['webtitle'] = '教师列表';
$list['dataurl'] = '/teacher/index/data';
$list['status'] = '/teacher/index/status';
$this->view->assign('list', $list);
return $this->view->fetch();
}
public function ajaxData()
{
$src = $this->request
->only([
'page' => '1'
,'limit' => '10'
,'field' => 'update_time'
,'order' => 'desc'
,'zhiwu_id' => array()
,'danwei_id' => array()
,'xueli_id' => array()
,'tuixiu' => 0
,'zhicheng_id' => ''
,'searchval' => ''
], 'POST');
$teacher = new TC;
$data = $teacher->search($src)
->visible([
'id'
,'xingming'
,'sex'
,'jsXueli' => ['title']
,'jsZhiwu' => ['title']
,'jsZhicheng' => ['title']
,'jsDanwei' => ['jiancheng']
,'biye'
,'zhuanye'
,'status'
,'phone'
,'update_time'
]);
$data = reSetObject($data, $src);
return json($data);
}
public function delList()
{
$list['webtitle'] = '教师列表';
$list['dataurl'] = '/teacher/index/datadel';
$list['status'] = '/teacher/index/status';
$this->view->assign('list', $list);
return $this->view->fetch();
}
public function ajaxDataDel()
{
$src = $this->request
->only([
'page' => '1'
,'limit' => '10'
,'field' => 'update_time'
,'order' => 'desc'
,'zhiwu_id' => array()
,'danwei_id' => array()
,'xueli_id' => array()
,'searchval' => ''
], 'POST');
$teacher = new TC;
$data = $teacher->searchDel($src)
->visible([
'id'
,'xingming'
,'sex'
,'jsXueli' => ['title']
,'jsZhiwu' => ['title']
,'jsZhicheng' => ['title']
,'jsDanwei' => ['jiancheng']
,'biye'
,'zhuanye'
,'update_time'
]);
$data = reSetObject($data, $src);
return json($data);
}
public function create()
{
$list['set'] = array(
'webtitle' => '添加教师'
,'butname' => '添加'
,'formpost' => 'POST'
,'url' => 'save'
);
$this->view->assign('list', $list);
return $this->view->fetch('create');
}
public function save()
{
$list = request()->only([
'xingming'
,'sex'
,'quanpin'
,'shoupin'
,'shengri'
,'phone'
,'zhiwu_id'
,'zhicheng_id'
,'xueli_id'
,'biye'
,'worktime'
,'zhuanye'
,'danwei_id'
,'tuixiu'
], 'POST');
$validate = new \app\teacher\validate\Teacher;
$result = $validate->scene('create')->check($list);
$msg = $validate->getError();
if(!$result){
return json(['msg' => $msg, 'val' => 0]);
}
$tc = new TC();
$temp = $tc->phoneSrc($list['phone']);
if($temp)
{
return json(['msg' => '手机号已经存在', 'val' => 0]);
}
$list['quanpin'] = trim(strtolower(str_replace(' ', '', $list['quanpin'])));
$list['shoupin'] = trim(strtolower($list['shoupin']));
$data = TC::create($list);
$data ? $data = ['msg' => '添加成功', 'val' => 1]
: $data = ['msg' => '数据处理错误', 'val' => 0];
return json($data);
}
public function read($id)
{
$myInfo = TC::withTrashed()
->where('id', $id)
->with(
[
'jsDanwei' => function($query){
$query->field('id, title');
},
'jsZhiwu' => function($query){
$query->field('id, title');
},
'jsZhicheng' => function($query){
$query->field('id, title');
},
'jsXueli' => function($query){
$query->field('id, title');
},
'jsSubject' => function($query){
$query->field('id, title');
},
]
)
->append(['age', 'gongling'])
->limit(1)
->find();
$myInfo['webtitle'] = $myInfo->xingming . '信息';
$this->view->assign('list', $myInfo);
return $this->view->fetch();
}
public function edit($id)
{
$list['data'] = TC::field('id, xingming, sex, quanpin, shoupin, shengri, phone, zhiwu_id, zhicheng_id, xueli_id, biye, worktime, zhuanye, danwei_id, tuixiu')
->find($id);
$list['set'] = array(
'webtitle' => '编辑教师'
,'butname' => '修改'
,'formpost' => 'PUT'
,'url' => '/teacher/index/update/' . $id
);
$this->view->assign('list', $list);
return $this->view->fetch('create');
}
public function update($id)
{
$list = request()->only([
'xingming'
,'sex'
,'quanpin'
,'shoupin'
,'shengri'
,'phone'
,'zhiwu_id'
,'zhicheng_id'
,'xueli_id'
,'biye'
,'worktime'
,'zhuanye'
,'danwei_id'
,'tuixiu'
], 'PUT');
$list['id'] = $id;
$validate = new \app\teacher\validate\Teacher;
$result = $validate->scene('edit')->check($list);
$msg = $validate->getError();
if(!$result){
return json(['msg' => $msg, 'val' => 0]);;
}
$tc = new TC();
$temp = TC::withTrashed()->where('phone', $list['phone'])->find();
if($temp)
{
if($temp->id != $id*1)
{
return json(['msg' => '手机号已经存在', 'val' => 0]);
}
}
$list['quanpin'] = trim(strtolower(str_replace(' ', '', $list['quanpin'])));
$list['shoupin'] = trim(strtolower($list['shoupin']));
$teacher = new TC();
$teacherlist = $teacher->find($id);
$teacherlist->xingming = $list['xingming'];
$teacherlist->sex = $list['sex'];
$teacherlist->quanpin = $list['quanpin'];
$teacherlist->shoupin = $list['shoupin'];
$teacherlist->shengri = $list['shengri'];
$teacherlist->phone = $list['phone'];
$teacherlist->zhiwu_id = $list['zhiwu_id'];
$teacherlist->zhicheng_id = $list['zhicheng_id'];
$teacherlist->xueli_id = $list['xueli_id'];
$teacherlist->biye = $list['biye'];
$teacherlist->worktime = $list['worktime'];
$teacherlist->zhuanye = $list['zhuanye'];
$teacherlist->danwei_id = $list['danwei_id'];
$teacherlist->tuixiu = $list['tuixiu'];
$data = $teacherlist->save();
$data>=0 ? $data = ['msg' => '更新成功', 'val' => 1]
: $data = ['msg' => '数据处理错误', 'val' => 0];
return json($data);
}
public function delete($id)
{
$id = request()->delete('id');
$id = explode(',', $id);
$data = TC::destroy($id);
$data ? $data = ['msg' => '删除成功', 'val' => 1]
: $data = ['msg' => '数据处理错误', 'val' => 0];
return json($data);
}
public function reDel($id)
{
$user = TC::onlyTrashed()->find($id);
$data = $user->restore();
$data ? $data = ['msg' => '恢复成功', 'val' => 1]
: $data = ['msg' => '数据处理错误', 'val' => 0];
return json($data);
}
public function setStatus()
{
$id = request()->post('id');
$value = request()->post('value');
$data = TC::where('id', $id)->update(['status' => $value]);
$data ? $data = ['msg' => '状态设置成功', 'val' => 1]
: $data = ['msg' => '数据处理错误', 'val' => 0];
return json($data);
}
public function createAll()
{
$list['set'] = array(
'webtitle' => '批量上传教师信息'
,'butname' => '批传'
,'formpost' => 'POST'
,'url' => 'saveall'
);
$this->view->assign('list', $list);
return $this->view->fetch();
}
public function saveAll()
{
$list = request()->only([
'school_id'
,'url'
], 'POST');
$teacherinfo = \app\facade\File::readXls(public_path() . 'uploads\\' . $list['url']);
if("教师基本情况表" != $teacherinfo[0][0] || '姓名*' != $teacherinfo[2][1] || '性别*' != $teacherinfo[2][2])
{
$this->error('请使用模板上传', '/login/err');
return json($data);
}
$teacher = new TC;
$data = $teacher->createAll($teacherinfo, $list['school_id']);
$data ? $data = ['msg' => '数据上传成功', 'val' => 1]
: ['msg' => '数据上传失败', 'val' => 0];
return json($data);
}
public function srcTeacher()
{
$src = $this->request
->only([
'str' => ''
,'danwei_id' => ''
,'field' => 'id'
,'order' => 'desc'
,'teacher_id' => ''
], 'POST');
$teacher = new TC();
$data = $teacher->strSrcTeachers($src);
$data = reSetObject($data, $src);
return json($data);
}
public function downloadXls()
{
$url = public_path() . 'uploads\\teacher\\TeacherInfo.xlsx';
return download($url, '教师名单模板.xlsx');
}
public function srcRy()
{
$src = $this->request
->only([
'page' => '1'
,'limit' => '10'
,'field' => 'update_time'
,'order' => 'desc'
,'teacher_id' => ''
], 'POST');
$rongyu = new \app\rongyu\model\JsRongyuInfo;
$data = $rongyu->srcTeacherRongyu($src['teacher_id'])
->visible([
'id'
,'title'
,'ryTuce' => [
'title'
,'fzSchool'
]
,'jiangxiang_id'
,'hjshijian'
,'update_time'
]);
$data = reSetObject($data, $src);
return json($data);
}
public function srcKt()
{
$src = $this->request
->only([
'page' => '1'
,'limit' => '10'
,'field' => 'update_time'
,'order' => 'desc'
,'teacher_id' => ''
], 'POST');
$keti = new \app\keti\model\KetiInfo;
$data = $keti->srcTeacherKeti($src['teacher_id']);
$data = reSetObject($data, $src);
return json($data);
}
public function resetpassword($id)
{
$md5 = new \WhiteHat101\Crypt\APR1_MD5;
$password = $md5->hash('123456');
$data = TC::where('id', $id)->update(['password' => $password]);
$data ? $data = ['msg' => '密码已经重置为:<br>123456', 'val' => 1]
: $data = ['msg' => '数据处理错误', 'val' => 0];
return json($data);
}
}