Study.php 1.2 KB
<?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();
        }
    }
}