12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091 |
- <?php
- declare(strict_types=1);
- namespace App\Model\Arts;
- use App\Model\Model;
- use Hyperf\DbConnection\Db;
- use function Hyperf\Config\config;
- class DriverWalletModel extends Model
- {
- /**
- * The table associated with the model.
- *
- * @var ?string
- */
- protected ?string $table = 'driver_wallet';
- protected ?string $dateFormat = 'U';
- public bool $timestamps = false;
- protected int $is_status_search = 0;// 默认使用 status = 1 筛选
- protected int $is_delete_search = 0;// 默认使用 is_delete = 0 筛选
- /**
- * 默认查询字段
- *
- * @var array|string[]
- */
- public array $select = [
- '*'
- ];
- public static function getOne(int $driver_id, string $field = '')
- {
- //所有钱包余额
- $wallet = self::query()->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('操作成功');
- }
- }
|