Study.php
1.2 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
<?php
namespace app\common\model;
use think\Db;
use think\Model;
class Study extends Model
{
// 表名
protected $name = 'study';
// 自动写入时间戳字段
protected $autoWriteTimestamp = 'int';
// 定义时间戳字段名
protected $createTime = 'createtime';
protected $updateTime = 'updatetime';
// 追加属性
protected $append = [
];
/**
* 变更会员余额
* @param int $score 余额
* @param int $study_id 会员ID
* @param string $memo 备注
*/
public static function score($score, $study_id, $memo)
{
Db::startTrans();
try {
$study = self::lock(true)->find($study_id);
if ($study && $score != 0) {
$after = function_exists('bcadd') ? bcadd($study->earn_score, $score, 2) : $study->earn_score + $score;
//更新会员信息
$study->save(['score' => $after]);
//写入日志
StudyScoreLog::create(['study_id' => $study_id, 'score' => $score, 'memo' => $memo]);
}
Db::commit();
} catch (\Exception $e) {
Db::rollback();
}
}
}