DriverWalletModel.php 2.9 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091
  1. <?php
  2. declare(strict_types=1);
  3. namespace App\Model\Arts;
  4. use App\Model\Model;
  5. use Hyperf\DbConnection\Db;
  6. use function Hyperf\Config\config;
  7. class DriverWalletModel extends Model
  8. {
  9. /**
  10. * The table associated with the model.
  11. *
  12. * @var ?string
  13. */
  14. protected ?string $table = 'driver_wallet';
  15. protected ?string $dateFormat = 'U';
  16. public bool $timestamps = false;
  17. protected int $is_status_search = 0;// 默认使用 status = 1 筛选
  18. protected int $is_delete_search = 0;// 默认使用 is_delete = 0 筛选
  19. /**
  20. * 默认查询字段
  21. *
  22. * @var array|string[]
  23. */
  24. public array $select = [
  25. '*'
  26. ];
  27. public static function getOne(int $driver_id, string $field = '')
  28. {
  29. //所有钱包余额
  30. $wallet = self::query()->where(['driver_id' => $driver_id])->first();
  31. if (!empty($field)) {
  32. return $wallet[$field] ?? '';
  33. }
  34. return $wallet;
  35. }
  36. public static function add(int $driver_id, array $params = []): bool
  37. {
  38. $params['driver_id'] = $driver_id;
  39. $insert = self::query()->insertGetId($params);
  40. return (bool)$insert;
  41. }
  42. /**
  43. * 钱包操作
  44. * @param int $driver_id
  45. * @param float $money
  46. * @param string $remark
  47. * @param int $type
  48. * @return bool
  49. */
  50. public function change(int $driver_id, float $money, string $remark = '', int $type = 3, string $about_value = '')
  51. {
  52. if (!in_array($type, [1, 2, 3, 4, 5, 6])) {
  53. return $this->error('余额类型错误');
  54. }
  55. $wallet = (new static())->getOne($driver_id);
  56. if (in_array($type, [2, 3, 5, 6])) {
  57. // 扣除
  58. $data = [
  59. 'money' => bcadd($wallet['money'], (string)(-$money), 2),
  60. 'disburse_money' => bcadd($wallet['disburse_money'], (string)$money, 2)
  61. ];
  62. // if ($data['money'] < 0) {
  63. // return $this->error('余额不足');
  64. // }
  65. $log_money = -$money;
  66. } else {
  67. // 充值
  68. $data = [
  69. 'money' => bcadd($wallet['money'], (string)$money, 2),
  70. 'get_money' => bcadd($wallet['get_money'], (string)(-$money), 2)
  71. ];
  72. $log_money = $money;
  73. }
  74. if (!$this->query()->where(['driver_id' => $driver_id, 'money' => $wallet['money']])->update($data)) {
  75. return $this->error('操作失败');
  76. }
  77. 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()])) {
  78. return $this->error('记录操作失败');
  79. }
  80. return $this->success('操作成功');
  81. }
  82. }