belongsTo(self::class,'parent_id','id'); } /** * 用户资金变动 * * @author 许祖兴 < zuxing.xu@lettered.cn> * @date 2020/7/14 10:34 * * @param int $userId 用户ID * @param float $amount 变动金额 * @param string $remark 备注说明 * @param bool $inc 是否增长 * @throws \think\exception\PDOException */ public function changeBalance($userId,$amount, $remark = "", $inc = false) { $action = $inc ? 'setInc' : 'setDec'; $this->startTrans(); try { // 查余额 $user = $this->lock(true)->where(['id' => $userId])->find(); // 资金变动 $this->where(['id' => $userId])->{$action}('balance',$amount); // 记录写入 $data = [ 'user_id' => $userId, 'action' => $inc ? 1 : 0, 'remark' => $remark ]; // 加减 if ($inc){ $data['inc_amount'] = $amount; $data['aft_amount'] = sprintf("%.2f", round($user['balance'] + $amount, 2)); }else{ $data['dec_amount'] = $amount; $data['aft_amount'] = sprintf("%.2f", round($user['balance'] - $amount, 2)); } UsersBalanceRecord::create($data,true); $this->commit(); }catch (\Exception $e){ $this->rollback(); } } /** * 用户资产变动 * * @author 许祖兴 < zuxing.xu@lettered.cn> * @date 2020/7/14 10:34 * * @param int $userId 用户ID * @param float $amount 变动金额 * @param string $remark 备注说明 * @param bool $inc 是否增长 * @throws \think\exception\PDOException */ public function changeProperty($userId,$amount, $remark = "", $inc = false) { $action = $inc ? 'setInc' : 'setDec'; $this->startTrans(); try { // 查余额 $user = $this->lock(true)->where(['id' => $userId])->find(); // 资金变动 $this->where(['id' => $userId])->{$action}('property',$amount); // 记录写入 $data = [ 'user_id' => $userId, 'action' => $inc ? 1 : 0, 'remark' => $remark ]; // 加减 if ($inc){ $data['inc_amount'] = $amount; $data['aft_amount'] = sprintf("%.3f", round($user['property'] + $amount, 3)); }else{ $data['dec_amount'] = $amount; $data['aft_amount'] = sprintf("%.3f", round($user['property'] - $amount, 3)); } UsersPropertyRecord::create($data,true); $this->commit(); }catch (\Exception $e){ $this->rollback(); } } /** * 用户资金变动 * * @author 许祖兴 < zuxing.xu@lettered.cn> * @date 2020/7/14 10:34 * * @param int $userId 用户ID * @param float $amount 变动金额 * @param string $remark 备注说明 * @param string $type 类型:10-分红,20-提现,30-资产司机收入 * @param bool $inc 是否增长 * @throws \think\exception\PDOException */ public function changePartnership($userId,$amount, $remark = "", $type=30, $inc = false) { $action = $inc ? 'setInc' : 'setDec'; $this->startTrans(); try { // 查余额 $user = $this->lock(true)->where(['id' => $userId])->find(); // 资金变动 $this->where(['id' => $userId])->{$action}('partnership',$amount); // 记录写入 $data = [ 'type' => $type, 'money' => $amount, 'user_id' => $userId, 'tag' => $inc ? 10 : 20, 'created_at' => time(), 'updated_at' => time(), 'memo' => $remark ]; // 加减 if ($inc){ $data['before'] = $user['partnership']; $data['after'] = sprintf("%.2f", round($user['partnership'] + $amount, 2)); }else{ $data['before'] = $user['partnership']; $data['after'] = sprintf("%.2f", round($user['partnership'] - $amount, 2)); } MotorRecord::create($data,true); $this->commit(); return true; }catch (\Exception $e){ $this->rollback(); return false; } } }