PunishmentOrder.php 2.4 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788
  1. <?php
  2. namespace App\Models;
  3. use Illuminate\Support\Facades\Log;
  4. class PunishmentOrder extends Model
  5. {
  6. //
  7. const NO_TAG = 'P';
  8. protected $table = 'punishment_orders';
  9. protected $guarded = [];
  10. const PAY_TYPE_NO = 0;
  11. const PAY_TYPE_WECHAT = 1;
  12. const PAY_TYPE_ACCOUNT = 2;
  13. const PAY_TYPE_ALIPAYMINI = 3;
  14. public static $payTypeMaps = [
  15. self::PAY_TYPE_NO => '待支付',
  16. self::PAY_TYPE_WECHAT => '微信支付',
  17. self::PAY_TYPE_ALIPAYMINI => '支付宝支付',
  18. self::PAY_TYPE_ACCOUNT => '余额支付',
  19. ];
  20. const PAY_STATUS_OK = 1;
  21. const PAY_STATUS_NO = 0;
  22. public static $payStatusMaps = [
  23. self::PAY_STATUS_NO => '未支付',
  24. self::PAY_STATUS_OK => '已支付'
  25. ];
  26. public function users()
  27. {
  28. return $this->belongsTo(User::class, 'user_id', 'id');
  29. }
  30. public function bikes()
  31. {
  32. return $this->belongsTo(Bike::class, 'bike_id', 'id');
  33. }
  34. public function area()
  35. {
  36. return $this->belongsTo(Area::class, 'area_id', 'id');
  37. }
  38. /**
  39. * 生成订单号
  40. * @return bool|string
  41. * User: Mead
  42. */
  43. public static function makeNo($tag = null)
  44. {
  45. // 订单流水号前缀
  46. $prefix = $tag . self::NO_TAG . date('YmdHis');
  47. for ($i = 0; $i < 10; $i++) {
  48. // 随机生成 6 位的数字
  49. $no = $prefix . str_pad(random_int(0, 999999), 6, '0', STR_PAD_LEFT);
  50. // 判断是否已经存在
  51. if (!static::query()->where('no', $no)->exists()) {
  52. return $no;
  53. }
  54. }
  55. Log::warning('find order no failed');
  56. return false;
  57. }
  58. public function pay_order_callback()
  59. {
  60. //添加钱包记录
  61. WalletLog::log(WalletLog::OPERATE_TYPE_ADD, $this->attributes['pay_money'], $this->attributes['user_id'], WalletLog::TYPE_ADD_WECHAT_PAY_PUNISHMENT_ORDER, $this->attributes['area_id'], $this->attributes['id'], PunishmentOrder::class);
  62. WalletLog::log(WalletLog::OPERATE_TYPE_SUB, $this->attributes['pay_money'], $this->attributes['user_id'], WalletLog::TYPE_SUB_WECHAT_PAY_PUNISHMENT_ORDER, $this->attributes['area_id'], $this->attributes['id'], PunishmentOrder::class);
  63. //修改订单记录
  64. $this->pay_status = PunishmentOrder::PAY_STATUS_OK;
  65. $this->pay_time = now();
  66. $this->pay_type = PunishmentOrder::PAY_TYPE_WECHAT;
  67. $this->save();
  68. return true;
  69. }
  70. }