StudyScoreLog.php
3.0 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
<?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();
$team = new Team();
$item = new Item();
$stu = $study->where('unique', $unique)->find()->toArray();
$xm = $item->where('id', $id)->find()->toArray();
$team = $team->where('title', $stu['team'])->field('title,score')->find();
$radar = explode(',', $xm['radar_ids']);
$score = explode(',', $xm['score']);
$sum_score = 0;
$list = [];
foreach ($score as $k) {
$sum_score += $k;
}
//halt($sum_score);
foreach ($radar as $k1 => $v1) {
foreach ($score as $k2 => $v2) {
if ($k1 == $k2) {
$list[$k1]['radar'] = $v1;
$list[$k1]['score'] = $v2;
}
}
}
$sum1 = $team['score'] + $sum_score; //合计战队总分
$sum2 = $stu['earn_score'] + $sum_score; //合计个人总分
$data = [
'item_id' => $id,
'campus_ids' => $xm['campus_ids'],
'study_id' => $stu['id'],
'team' => $stu['team'],
'score' => $sum_score,
'memo' => $stu['name'] . '参加' . $xm['title'] . '加分',
'createtime' => time()
];
Db::startTrans();
try {
$this->allowField(true)->save($data);
$study->save(['earn_score' => $sum2], ['id' => $stu['id']]);
$team->save(['score' => $sum1, 'updatetime' => time()], ['title' => $stu['team']]);
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_ids' => $xm['campus_ids'],
'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();
}
}
}