DepositRefund.php 4.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151
  1. <?php
  2. namespace App\Models;
  3. use Carbon\Carbon;
  4. use Illuminate\Database\Eloquent\Model;
  5. use Illuminate\Support\Facades\Log;
  6. class DepositRefund extends Model
  7. {
  8. //押金退还记录表
  9. protected $table = "refund_logs";
  10. //
  11. protected $guarded = [];
  12. const NO_TAG = 'T';
  13. const STATUS_OK = 1;
  14. const STATUS_PAUSE = 0;
  15. public static $statusMaps = [
  16. self::STATUS_OK => '正常',
  17. self::STATUS_PAUSE => '暂停'
  18. ];
  19. const PAY_STATUS_OK = 1;
  20. const PAY_STATUS_NO = 0;
  21. const PAY_STATUS_ERROR = 2;
  22. const PAY_STATUS_WAIT = 3;
  23. public static $payStatusMaps = [
  24. self::PAY_STATUS_NO => '已支付',
  25. self::PAY_STATUS_OK => '未支付',
  26. self::PAY_STATUS_ERROR => '退款异常',
  27. self::PAY_STATUS_WAIT => '退款排队中',
  28. ];
  29. const PAY_TYPE_WECHAT = 1;
  30. const PAY_TYPE_ALIPAYMINI = 2;
  31. public static $payTypeMaps = [
  32. self::PAY_TYPE_WECHAT => '微信支付',
  33. self::PAY_TYPE_ALIPAYMINI => '支付宝小程序支付'
  34. ];
  35. const REFUND_OK = 1;
  36. const REFUND_NO = 0;
  37. public static $refundMaps = [
  38. self::REFUND_OK => '已退款',
  39. self::REFUND_NO => '未退款'
  40. ];
  41. const TYPE_USER = 0;
  42. public static $typeMaps = [
  43. self::TYPE_USER => '用户发起一般退款'
  44. ];
  45. const CHECK_STATUS_OK = 1;
  46. const CHECK_STATUS_NO = 0;
  47. public static $checkStatusMaps = [
  48. self::CHECK_STATUS_NO => '审核通过',
  49. self::CHECK_STATUS_OK => '审核未通过'
  50. ];
  51. /**
  52. * 生成订单号
  53. * @return bool|string
  54. * User: Mead
  55. */
  56. public static function makeNo()
  57. {
  58. // 订单流水号前缀
  59. $prefix = AdminMerchant::getOrderTag() . self::NO_TAG . date('YmdHis');
  60. for ($i = 0; $i < 10; $i++) {
  61. // 随机生成 6 位的数字
  62. $no = $prefix . str_pad(random_int(0, 999999), 6, '0', STR_PAD_LEFT);
  63. // 判断是否已经存在
  64. if (!static::query()->where('no', $no)->exists()) {
  65. return $no;
  66. }
  67. }
  68. Log::warning('find order no failed');
  69. return false;
  70. }
  71. public function pay_order_callback()
  72. {
  73. DepositOrder::where('id', $this->attributes['deposit_id'])->update([
  74. 'is_refund' => DepositOrder::REFUND_OK
  75. ]);
  76. // 修改用户押金状态
  77. User::where('id', $this->attributes['user_id'])->update([
  78. 'deposit_money' => 0,
  79. 'is_deposit' => User::DEPOSIT_NO
  80. ]);
  81. return true;
  82. }
  83. /**
  84. * 退款成功回调
  85. * @return bool
  86. */
  87. public function refund_order_callback()
  88. {
  89. DepositOrder::where('id', $this->attributes['deposit_id'])->update([
  90. 'is_refund' => DepositOrder::REFUND_OK
  91. ]);
  92. $user_id = $this->attributes['user_id'];
  93. $user = User::find($user_id);
  94. $re = CouponsUserBag::query()->where('user_id', $user_id)
  95. ->where('coupon_type', CouponsUserBag::COUPON_TYPE_DEPOSIT_FREE)
  96. ->where('valid_end_time', '<=', Carbon::now()->toDateTimeString())
  97. ->where('status', CouponsUserBag::STATUS_OK)->exists();
  98. if ($re) {
  99. //是否有免押金劵
  100. User::where('id', $user_id)->update([
  101. 'deposit_money' => 0,
  102. 'is_deposit' => User::DEPOSIT_OK,
  103. 'deposit_type' => User::DEPOSIT_COUPON
  104. ]);
  105. } else {
  106. $deposit_expire_time = Carbon::parse($user->deposit_expire_time);
  107. if (Carbon::now()->gte($deposit_expire_time)) {
  108. // 免押金卡已经过期
  109. User::where('id', $user_id)->update([
  110. 'deposit_money' => 0,
  111. 'is_deposit' => User::DEPOSIT_NO,
  112. 'deposit_type' => User::DEPOSIT_TYPE_NO
  113. ]);
  114. } else {
  115. // 免押金卡未过期
  116. User::where('id', $user_id)->update([
  117. 'deposit_money' => 0,
  118. 'is_deposit' => User::DEPOSIT_OK,
  119. 'deposit_type' => User::DEPOSIT_CARD
  120. ]);
  121. }
  122. }
  123. // // 修改用户押金状态
  124. // User::where('id', $this->attributes['user_id'])->update([
  125. // 'deposit_money' => 0,
  126. // 'is_deposit' => User::DEPOSIT_NO
  127. // ]);
  128. return true;
  129. }
  130. }