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