id ?? 0; $where = [ 'mainId' => $mainId, 'time' => $time, ]; $model = self::getByCondition($where, true); if (empty($model)) { $model = self::add(['mainId' => $mainId, 'time' => $time, 'riseNum' => 0, 'totalNum' => $main->totalOrder], true); } $id = $model->id; //查id行级锁 $stat = StatWorkCount::findBySql('select * from ' . StatWorkCount::tableName() . ' where id = :id for update', ['id' => $id])->one(); $stat->riseNum += 1; $stat->totalNum += 1; $stat->save(); return $stat->riseNum; } }