56 lines
1.5 KiB
PHP
56 lines
1.5 KiB
PHP
<?php
|
|
|
|
namespace app\index\controller;
|
|
|
|
use app\common\controller\Frontend;
|
|
use app\admin\model\Student;
|
|
|
|
class Index extends Frontend
|
|
{
|
|
|
|
protected $noNeedLogin = '*';
|
|
protected $noNeedRight = '*';
|
|
protected $layout = '';
|
|
|
|
public function index()
|
|
{
|
|
return $this->view->fetch();
|
|
}
|
|
|
|
/**
|
|
* 根据生日计算年龄、类型
|
|
* @param Student $student
|
|
* @throws \think\db\exception\DataNotFoundException
|
|
* @throws \think\db\exception\ModelNotFoundException
|
|
* @throws \think\exception\DbException
|
|
*/
|
|
public function setInfoByBirthday(Student $student ){
|
|
$return = ['code'=>0,'msg'=>'无改变'];
|
|
|
|
//年龄改变的学生
|
|
$list = Student::field('id,age,TIMESTAMPDIFF(YEAR,`birthday`,CURDATE()) as age_now')
|
|
->where('status','normal')
|
|
->having('age <> age_now')
|
|
->select();
|
|
|
|
if($list){
|
|
$data = [];
|
|
foreach ($list as $v){
|
|
$data[] = [
|
|
'id' => $v['id'],
|
|
'age' => $v['age_now'],
|
|
'type' => $v['age_now']<11 ? 1 : 2,
|
|
];
|
|
}
|
|
$res = $student->isUpdate()->saveAll($data);
|
|
if($res){
|
|
$return = ['code'=>1,'msg'=>count($data).'位学生改变:'.implode(',',array_column($data,'id'))];
|
|
}else{
|
|
$return['msg'] = '改变失败';
|
|
}
|
|
}
|
|
|
|
echo json_encode($return, true);
|
|
}
|
|
}
|