where(['driver_id' => $driver_id])->first(); if (!empty($field)) { return $wallet[$field] ?? ''; } return $wallet; } public static function add(int $driver_id, array $params = []): bool { $params['driver_id'] = $driver_id; $insert = self::query()->insertGetId($params); return (bool)$insert; } /** * 钱包操作 * @param int $driver_id * @param float $money * @param string $remark * @param int $type * @return bool */ public function change(int $driver_id, float $money, string $remark = '', int $type = 3, string $about_value = '') { if (!in_array($type, [1, 2, 3, 4, 5, 6])) { return $this->error('余额类型错误'); } $wallet = (new static())->getOne($driver_id); if (in_array($type, [2, 3, 5, 6])) { // 扣除 $data = [ 'money' => bcadd($wallet['money'], (string)(-$money), 2), 'disburse_money' => bcadd($wallet['disburse_money'], (string)$money, 2) ]; // if ($data['money'] < 0) { // return $this->error('余额不足'); // } $log_money = -$money; } else { // 充值 $data = [ 'money' => bcadd($wallet['money'], (string)$money, 2), 'get_money' => bcadd($wallet['get_money'], (string)(-$money), 2) ]; $log_money = $money; } if (!$this->query()->where(['driver_id' => $driver_id, 'money' => $wallet['money']])->update($data)) { return $this->error('操作失败'); } if (!DriverMoneyLogModel::query()->insert(['driver_id' => $driver_id, 'type' => $type, 'money' => $log_money, 'before' => $wallet['money'], 'after' => $data['money'], 'remark' => $remark, 'about_value' => $about_value, 'create_time' => time()])) { return $this->error('记录操作失败'); } return $this->success('操作成功'); } }