DepositOrder.php 4.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145
  1. <?php
  2. namespace App\Models;
  3. use App\Traits\ModelHelpers;
  4. use Carbon\Carbon;
  5. use Illuminate\Database\Eloquent\Model;
  6. use Illuminate\Support\Facades\Log;
  7. class DepositOrder extends Model
  8. {
  9. use ModelHelpers;
  10. const NO_TAG = 'Y';
  11. //押金订单表
  12. protected $table = "deposit_orders";
  13. protected $guarded = [];
  14. const STATUS_OK = 1;
  15. const STATUS_PAUSE = 0;
  16. public static $statusMaps = [
  17. self::STATUS_OK => '正常',
  18. self::STATUS_PAUSE => '暂停'
  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. const PAY_TYPE_NO = 0;
  27. const PAY_TYPE_WECHAT = 1;
  28. const PAY_TYPE_ACCOUNT = 2;
  29. const PAY_TYPE_ALIPAYMINI = 3;
  30. const PAY_TYPE_ALIPAYMINI_CREDIT = 4;
  31. public static $payTypeMaps = [
  32. self::PAY_TYPE_NO => '待支付',
  33. self::PAY_TYPE_WECHAT => '微信支付',
  34. self::PAY_TYPE_ACCOUNT => '余额支付',
  35. self::PAY_TYPE_ALIPAYMINI => '支付宝支付',
  36. self::PAY_TYPE_ALIPAYMINI_CREDIT => '支付宝免押金支付',
  37. ];
  38. const REFUND_OK = 1;
  39. const REFUND_NO = 0;
  40. public static $refundMaps = [
  41. self::REFUND_OK => '已退款',
  42. self::REFUND_NO => '未退款'
  43. ];
  44. // 交易类型 用is_refund 判断
  45. public static $transactionTypeMaps = [
  46. self::REFUND_OK => '退还押金',
  47. self::REFUND_NO => '缴纳押金'
  48. ];
  49. public function depositRefund()
  50. {
  51. return $this->hasOne(DepositRefund::class, 'deposit_id', 'id');
  52. }
  53. public function users()
  54. {
  55. return $this->belongsTo('App\Models\User', 'user_id', 'id');
  56. }
  57. public function area()
  58. {
  59. return $this->belongsTo(Area::class, 'area_id', 'id');
  60. }
  61. /**
  62. * 生成订单号
  63. * @return bool|string
  64. * User: Mead
  65. */
  66. public static function makeNo()
  67. {
  68. // 订单流水号前缀
  69. $prefix = AdminMerchant::getOrderTag() . self::NO_TAG . date('YmdHis');
  70. for ($i = 0; $i < 10; $i++) {
  71. // 随机生成 6 位的数字
  72. $no = $prefix . str_pad(random_int(0, 999999), 6, '0', STR_PAD_LEFT);
  73. // 判断是否已经存在
  74. if (!static::query()->where('no', $no)->exists()) {
  75. return $no;
  76. }
  77. }
  78. Log::warning('find order no failed');
  79. return false;
  80. }
  81. /**
  82. * 退款回调
  83. * Author: Mead
  84. */
  85. public function refund_order_callback()
  86. {
  87. //商户平台直接退押金回调
  88. $user_id = $this->attributes['user_id'];
  89. $user = User::find($user_id);
  90. $re = CouponsUserBag::query()->where('user_id', $user_id)->where('coupon_type', CouponsUserBag::COUPON_TYPE_DEPOSIT_FREE)->where('valid_end_time', '<=', Carbon::now()->toDateTimeString())->where('status', CouponsUserBag::STATUS_OK)->exists();
  91. if ($re) {
  92. //是否有免押金劵
  93. User::where('id', $user_id)->update([
  94. 'deposit_money' => 0,
  95. 'is_deposit' => User::DEPOSIT_OK,
  96. 'deposit_type' => User::DEPOSIT_COUPON
  97. ]);
  98. } else {
  99. $deposit_expire_time = Carbon::parse($user->deposit_expire_time);
  100. if (Carbon::now()->gte($deposit_expire_time)) {
  101. // 免押金卡已经过期
  102. User::where('id', $user_id)->update([
  103. 'deposit_money' => 0,
  104. 'is_deposit' => User::DEPOSIT_NO,
  105. 'deposit_type' => User::DEPOSIT_TYPE_NO
  106. ]);
  107. } else {
  108. // 免押金卡未过期
  109. User::where('id', $user_id)->update([
  110. 'deposit_money' => 0,
  111. 'is_deposit' => User::DEPOSIT_OK,
  112. 'deposit_type' => User::DEPOSIT_CARD
  113. ]);
  114. }
  115. }
  116. //检查退款请求
  117. RefundLog::where('deposit_id', $this->attributes['id'])->update([
  118. 'pay_status' => RefundLog::PAY_STATUS_OK,
  119. 'pay_time' => date('Y-m-d H:i:s')
  120. ]);
  121. }
  122. }