PunishmentOrder.php 2.4 KB

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