CardRidingOrder.php 3.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899
  1. <?php
  2. namespace App\Models;
  3. use Carbon\Carbon;
  4. use Illuminate\Database\Eloquent\Model;
  5. use Illuminate\Support\Facades\Log;
  6. class CardRidingOrder extends Model
  7. {
  8. public $table = 'card_riding_orders';
  9. protected $fillable = ['no', 'area_id', 'user_id', 'riding_card_id', 'money', 'discount', 'pay_money', 'pay_type', 'pay_status', 'pay_time', 'status'];
  10. const NO_TAG = 'R';
  11. const PAY_TYPE_WECHAT = 1;
  12. const PAY_TYPE_ACCOUNT = 2;
  13. const PAY_TYPE_ADMIN_GIVE = 3;
  14. const PAY_TYPE_INVITE_NEW_USER_GIVE = 4;
  15. public static $payTypeMaps = [
  16. self::PAY_TYPE_WECHAT => '微信支付',
  17. self::PAY_TYPE_ACCOUNT => '余额支付',
  18. self::PAY_TYPE_ADMIN_GIVE => '后台赠送',
  19. self::PAY_TYPE_INVITE_NEW_USER_GIVE => '邀请新用户赠送'
  20. ];
  21. const PAY_STATUS_OK = 1;
  22. const PAY_STATUS_NO = 0;
  23. public static $payStatusMaps = [
  24. self::PAY_STATUS_OK => '支付成功',
  25. self::PAY_STATUS_NO => '支付失败',
  26. ];
  27. const STATUS_OK = 1;
  28. const STATUS_NO = 0;
  29. public static $statusMaps = [
  30. self::STATUS_OK => '支付成功',
  31. self::STATUS_NO => '支付失败'
  32. ];
  33. /**
  34. * 生成订单号
  35. * @return bool|string
  36. * User: Mead
  37. */
  38. public static function makeNo()
  39. {
  40. // 订单流水号前缀
  41. $prefix = config('bike.no_tag') . self::NO_TAG . date('YmdHis');
  42. for ($i = 0; $i < 10; $i++) {
  43. // 随机生成 6 位的数字
  44. $no = $prefix . str_pad(random_int(0, 999999), 6, '0', STR_PAD_LEFT);
  45. // 判断是否已经存在
  46. if (!static::query()->where('no', $no)->exists()) {
  47. return $no;
  48. }
  49. }
  50. Log::warning('find order no failed');
  51. return false;
  52. }
  53. public function pay_order_callback()
  54. {
  55. $order = $this->attributes;
  56. //添加钱包记录
  57. WalletLog::log(WalletLog::OPERATE_TYPE_ADD, $this->attributes['pay_money'], $this->attributes['user_id'], WalletLog::TYPE_ADD_WECHAT_PAY_CARD_RIDING_ORDER, $this->attributes['area_id'], $this->attributes['id'], CardRidingOrder::class);
  58. WalletLog::log(WalletLog::OPERATE_TYPE_SUB, $this->attributes['pay_money'], $this->attributes['user_id'], WalletLog::TYPE_SUB_WECHAT_PAY_CARD_RIDING_ORDER, $this->attributes['area_id'], $this->attributes['id'], CardRidingOrder::class);
  59. //修改订单记录
  60. $this->pay_status = CardRidingOrder::PAY_STATUS_OK;
  61. $this->pay_time = now();
  62. $this->pay_type = CardRidingOrder::PAY_TYPE_WECHAT;
  63. $this->status = CardRidingOrder::STATUS_OK;
  64. $this->save();
  65. //支付成功给用户发卡
  66. $cardRiding = CardRiding::find($this->riding_card_id);
  67. $take_effect_time = Carbon::now()->format('Y-m-d H:i:s');
  68. $expiration_time = Carbon::now()->addDays($cardRiding->effective_days)->format('Y-m-d H:i:s');
  69. $data = [
  70. 'user_id' => $this->attributes['user_id'],
  71. 'riding_card_id' => $this->attributes['riding_card_id'],
  72. 'card_riding_order_id' => $this->attributes['id'],
  73. 'take_effect_time' => $take_effect_time,
  74. 'deduction_money' => $cardRiding->deduction_money,
  75. 'expiration_time' => $expiration_time,
  76. 'is_limit_times' => $cardRiding->is_limit_times,
  77. 'can_ridding_times' => $cardRiding->times,
  78. 'day_can_ridding_times' => $cardRiding->day_can_ridding_times,
  79. 'status' => CardRidingUserBags::STATUS_OK
  80. ];
  81. // Log::info($data);
  82. CardRidingUserBags::firstOrCreate(['card_riding_order_id' => $this->attributes['id']], $data);
  83. return true;
  84. }
  85. }