作者 开飞机的舒克

扫码加分问题优化

  1 +<?php
  2 +
  3 +namespace app\common\model;
  4 +
  5 +use think\Model;
  6 +
  7 +class Radar extends Model
  8 +{
  9 +
  10 + // 表名
  11 + protected $name = 'radar';
  12 + // 自动写入时间戳字段
  13 + protected $autoWriteTimestamp = 'int';
  14 + // 定义时间戳字段名
  15 + protected $createTime = 'createtime';
  16 + protected $updateTime = 'updatetime';
  17 + // 追加属性
  18 + protected $append = [
  19 +
  20 + ];
  21 +}
@@ -27,12 +27,14 @@ class StudyScoreLog extends Model @@ -27,12 +27,14 @@ class StudyScoreLog extends Model
27 //加分 27 //加分
28 public function addScore($id, $unique) 28 public function addScore($id, $unique)
29 { 29 {
30 - $study = new Study();  
31 - $team = new Team();  
32 - $item = new Item();  
33 - $stu = $study->where('unique', $unique)->find()->toArray();  
34 - $xm = $item->where('id', $id)->find()->toArray();  
35 - $team = $team->where('title', $stu['team'])->field('title,score')->find(); 30 + $study = new Study(); //学生表
  31 + $teams = new Team(); //战队表
  32 + $item = new Item(); //项目表
  33 + $ronda = new Ronda(); //场次表
  34 + $stu = $study->where('unique', $unique)->find()->toArray(); //学生信息
  35 + $xm = $item->where('id', $id)->find()->toArray(); //项目信息
  36 + $team = $teams->where('title', $stu['team'])->find()->toArray(); //战队信息
  37 + $ronda = $ronda->where('title',$stu['ronda'])->find()->toArray(); //场次信息
36 $radar = explode(',', $xm['radar_ids']); 38 $radar = explode(',', $xm['radar_ids']);
37 $score = explode(',', $xm['score']); 39 $score = explode(',', $xm['score']);
38 $sum_score = 0; 40 $sum_score = 0;
@@ -40,7 +42,6 @@ class StudyScoreLog extends Model @@ -40,7 +42,6 @@ class StudyScoreLog extends Model
40 foreach ($score as $k) { 42 foreach ($score as $k) {
41 $sum_score += $k; 43 $sum_score += $k;
42 } 44 }
43 - //halt($sum_score);  
44 foreach ($radar as $k1 => $v1) { 45 foreach ($radar as $k1 => $v1) {
45 foreach ($score as $k2 => $v2) { 46 foreach ($score as $k2 => $v2) {
46 if ($k1 == $k2) { 47 if ($k1 == $k2) {
@@ -49,31 +50,89 @@ class StudyScoreLog extends Model @@ -49,31 +50,89 @@ class StudyScoreLog extends Model
49 } 50 }
50 } 51 }
51 } 52 }
52 - $sum1 = $team['score'] + $sum_score; //合计战队总分  
53 - $sum2 = $stu['earn_score'] + $sum_score; //合计个人总分 53 + Db::startTrans();
  54 + try {
  55 + //得分加入学生表中
  56 + $study->allowField(true)->save(['earn_score'=>$stu['earn_score'] + $sum_score],['unique'=>$unique]);
  57 + //得分写入学生分数记录
54 $data = [ 58 $data = [
55 'item_id' => $id, 59 'item_id' => $id,
56 - 'campus_ids' => $xm['campus_ids'], 60 + 'campus_ids' => $ronda['campus_id'],
57 'study_id' => $stu['id'], 61 'study_id' => $stu['id'],
58 'team' => $stu['team'], 62 'team' => $stu['team'],
59 'score' => $sum_score, 63 'score' => $sum_score,
60 'memo' => $stu['name'] . '参加' . $xm['title'] . '加分', 64 'memo' => $stu['name'] . '参加' . $xm['title'] . '加分',
61 'createtime' => time() 65 'createtime' => time()
62 ]; 66 ];
63 - Db::startTrans();  
64 - try {  
65 $this->allowField(true)->save($data); 67 $this->allowField(true)->save($data);
66 - $study->save(['earn_score' => $sum2], ['id' => $stu['id']]);  
67 - $team->save(['score' => $sum1, 'updatetime' => time()], ['title' => $stu['team']]); 68 + //学生场次得分
  69 + $study_ronda = \db('study_ronda_score')->where(['sid'=>$stu['id'],'team_id'=>$team['id'],'ronda_id'=>$ronda['id']])->find();
  70 + if (empty($study_ronda)){
  71 + \db('study_ronda_score')
  72 + ->insert([
  73 + 'sid'=>$stu['id'],
  74 + 'team_id'=>$team['id'],
  75 + 'ronda_id'=>$ronda['id'],
  76 + 'score'=>$sum_score
  77 + ]);
  78 + }else{
  79 + \db('study_ronda_score')
  80 + ->where([
  81 + 'sid'=>$stu['id'],
  82 + 'team_id'=>$team['id'],
  83 + 'ronda_id'=>$ronda['id']
  84 + ])->update(['score'=>($study_ronda['score'] + $sum_score)]);
  85 + }
  86 + //学生得分存入item_study_score表中
  87 + $find = \db('item_study_score')
  88 + ->where([
  89 + 'sid' => $stu['id'],
  90 + 'item_id' => $id,
  91 + 'campus_id' => $ronda['campus_id'],
  92 + 'ronda_id' => $ronda['id'],
  93 + ])
  94 + ->find();
  95 + if (empty($find)) {
  96 + \db('item_study_score')->insert([
  97 + 'item_id' => $id,
  98 + 'campus_id' => $ronda['campus_id'],
  99 + 'ronda_id' => $ronda['id'],
  100 + 'sid' => $stu['id'],
  101 + 'score' => $sum_score,
  102 + ]);
  103 + } else {
  104 + \db('item_study_score')
  105 + ->where([
  106 + 'sid' => $stu['id'],
  107 + 'item_id' => $id,
  108 + 'campus_id' => $ronda['campus_id'],
  109 + 'ronda_id' => $ronda['id'],
  110 + ])
  111 + ->update(['score' => ($find['score'] + $sum_score)]);
  112 + }
  113 + //战队得分
  114 + $team_score = \db('team_score')->where(['ronda_id'=>$ronda['id'],'team_id'=>$team['id']])->find();
  115 + if (empty($team_score)){
  116 + \db('team_score')
  117 + ->insert([
  118 + 'ronda_id'=>$ronda['id'],
  119 + 'team_id'=>$team['id'],
  120 + 'score' =>$sum_score
  121 + ]);
  122 + }else{
  123 + \db('team_score')
  124 + ->where(['ronda_id'=>$ronda['id'],'team_id'=>$team['id']])
  125 + ->update(['score'=>($team_score+ $sum_score)]);
  126 + }
  127 + //学生维度得分
68 foreach ($list as $key => $value) { 128 foreach ($list as $key => $value) {
69 $ronda_score = new RadarScore(); 129 $ronda_score = new RadarScore();
70 -  
71 $res = $ronda_score->where(['study_id' => $stu['id'], 'radar_id' => $value['radar']])->find(); 130 $res = $ronda_score->where(['study_id' => $stu['id'], 'radar_id' => $value['radar']])->find();
72 if (empty($res)) { 131 if (empty($res)) {
73 $resc = [ 132 $resc = [
74 'study_id' => $stu['id'], 133 'study_id' => $stu['id'],
75 'radar_id' => $value['radar'], 134 'radar_id' => $value['radar'],
76 - 'campus_ids' => $xm['campus_ids'], 135 + 'campus_ids' => $ronda['campus_id'],
77 'score' => $value['score'], 136 'score' => $value['score'],
78 'createtime' => time() 137 'createtime' => time()
79 ]; 138 ];
@@ -86,6 +145,7 @@ class StudyScoreLog extends Model @@ -86,6 +145,7 @@ class StudyScoreLog extends Model
86 Db::commit(); 145 Db::commit();
87 } catch (Exception $e) { 146 } catch (Exception $e) {
88 Db::rollback(); 147 Db::rollback();
  148 + $e->getMessage();
89 } 149 }
90 150
91 } 151 }