From cc03a2164bafdd41f2a7be2f01e4060392485088 Mon Sep 17 00:00:00 2001 From: cxdbz Date: Wed, 28 Nov 2018 23:15:21 +0800 Subject: [PATCH] =?UTF-8?q?=E6=B7=BB=E5=8A=A0=E6=88=90=E7=BB=A9=E5=B9=B3?= =?UTF-8?q?=E5=9D=87=E5=88=86=E5=92=8C=E6=80=BB=E5=88=86=E5=88=97?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- application/chengji/controller/Index.php | 6 +- application/chengji/model/Chengji.php | 78 ++++++++++--------- .../migrations/20181114055804_chengji.php | 9 ++- 3 files changed, 52 insertions(+), 41 deletions(-) diff --git a/application/chengji/controller/Index.php b/application/chengji/controller/Index.php index 33494232..c1da1e9e 100644 --- a/application/chengji/controller/Index.php +++ b/application/chengji/controller/Index.php @@ -196,10 +196,10 @@ class Index extends Base // 获取记录集总数 $cnt = Chengji::count(); //查询数据 - $data =Chengji::field('id,kaoshi,school,banji,student,nianji,yuwen,shuxue,waiyu,status') + $data =Chengji::field('id,kaoshi,school,banji,student,nianji,yuwen,shuxue,waiyu,stuSum,stuAvg,status') ->order([$order_field=>$order]) ->limit($limit_start,$limit_length) - ->all(); + ->select(); // dump($data); @@ -214,7 +214,7 @@ class Index extends Base // // } $datacnt = $data->count(); - $data = $data->append(['stuAvg','stuSum']); + // $data = $data->append(['stuAvg','stuSum']); diff --git a/application/chengji/model/Chengji.php b/application/chengji/model/Chengji.php index 11d55bad..e40a9630 100644 --- a/application/chengji/model/Chengji.php +++ b/application/chengji/model/Chengji.php @@ -10,6 +10,41 @@ use app\teach\model\Banji; class Chengji extends Base { + + + // 定义更新成绩后计算平均分和总分 + protected static function init() + { + self::afterUpdate(function ($cj) { + $cj = $cj->where('id',$cj->id)->find(); + // 重新组合数组 + $num[] = $cj->yuwen; + $num[] = $cj->shuxue; + $num[] = $cj->waiyu; + + // 删除空数据 + foreach ($num as $key => $value) { + if($value == null) + { + unset($num[$key]); + } + } + + // 获取数组长度 + $cnt = count($num); + + // 如果存在数据则计算平均分与总分 + if($cnt>0) + { + $cj->stuSum=array_sum($num); + $cj->stuAvg=$cj->stuSum/$cnt; + $cj->stuAvg = round($cj->stuAvg,1); + } + + $cj->save(); + }); + } + // 学生姓名获取器 public function getStudentAttr($value) { @@ -40,41 +75,6 @@ class Chengji extends Base return $bj->numTitle; } - // 学生总分 - public function getStuSumAttr() - { - $sum = $this->getData('yuwen') + $this->getData('shuxue') +$this->getData('waiyu'); - $sum == 0 ? $sum='' : $sum; - return $sum; - } - // 学生总分 - public function getStuAvgAttr() - { - // 将三科成绩以数组形式储存 - $num[] = $this->getAttr('yuwen'); - $num[] = $this->getAttr('shuxue'); - $num[] = $this->getAttr('waiyu'); - - // 删除空数据 - $i = 0; - foreach ($num as $key => $value) { - if($value == null) - { - unset($num[$key]); - } - } - - // 获取数组长度 - if(count($num) == null) - { - $sum = ''; - }else{ - $sum = array_sum($num)/count($num); - $sum = round($sum,1); - } - - return $sum; - } // 格式化成绩 public function getYuwenAttr($val) @@ -89,6 +89,14 @@ class Chengji extends Base { return $this->myval($val); } + public function getStuSumAttr($val) + { + return $this->myval($val); + } + public function getStuAvgAttr($val) + { + return $this->myval($val); + } // 格式化成绩 diff --git a/database/migrations/20181114055804_chengji.php b/database/migrations/20181114055804_chengji.php index 1ac113e9..f1835836 100644 --- a/database/migrations/20181114055804_chengji.php +++ b/database/migrations/20181114055804_chengji.php @@ -39,10 +39,13 @@ class Chengji extends Migrator ->addColumn('nianji','string',['limit'=>4,'null'=>false,'comment'=>'年级']) ->addColumn('banji','integer',['limit'=>11,'null'=>false,'comment'=>'班级']) ->addColumn('student','integer',['limit'=>11,'null'=>false,'comment'=>'学生']) - ->addColumn('yuwen','decimal',['precision'=>4,'scale'=>1,'default'=>Null,'null'=>false,'comment'=>'语文']) - ->addColumn('shuxue','decimal',['precision'=>4,'scale'=>1,'default'=>Null,'null'=>false,'comment'=>'数学']) - ->addColumn('waiyu','decimal',['precision'=>4,'scale'=>1,'default'=>Null,'null'=>false,'comment'=>'外语']) + ->addColumn('yuwen','decimal',['precision'=>4,'scale'=>1,'default'=>Null,'null'=>true,'comment'=>'语文']) + ->addColumn('shuxue','decimal',['precision'=>4,'scale'=>1,'default'=>Null,'null'=>true,'comment'=>'数学']) + ->addColumn('waiyu','decimal',['precision'=>4,'scale'=>1,'default'=>Null,'null'=>true,'comment'=>'外语']) + ->addColumn('stuAvg','decimal',['precision'=>4,'scale'=>1,'default'=>Null,'null'=>true,'comment'=>'平均分']) + ->addColumn('stuSum','decimal',['precision'=>4,'scale'=>1,'default'=>Null,'null'=>true,'comment'=>'总分']) ->addColumn('delete_time','integer',['limit'=>11,'null'=>true,'comment'=>'删除时间']) + ->addColumn('update_time','integer',['limit'=>11,'null'=>true,'comment'=>'更新时间']) ->addColumn('status','boolean',['limit'=>1,'default'=>'1','null'=>false,'comment'=>'0=禁用,1=正常']) ->addIndex(array('kaoshi','student'), array('unique' => true)) ->create();