DepositCardOrder.php 3.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113
  1. <?php
  2. /**
  3. *
  4. *
  5. * @category xxx
  6. * @package PSR
  7. * @subpackage Documentation\API
  8. * @author xxx <xxx@xxx.com>
  9. * @license GPL https://xxx.com
  10. * @link https://xxx.com
  11. * @ctime: 2020/4/30 15:32
  12. */
  13. namespace App\Models;
  14. use Carbon\Carbon;
  15. use Illuminate\Database\Eloquent\Model;
  16. use Illuminate\Support\Facades\Log;
  17. class DepositCardOrder extends Model
  18. {
  19. protected $table = 'deposit_card_orders';
  20. protected $fillable = ['no', 'area_id', 'user_id', 'deposit_cards_id', 'money', 'effective_days', 'discount', 'pay_money', 'pay_type', 'pay_status', 'pay_time', 'status'];
  21. const NO_TAG = 'M';
  22. const PAY_TYPE_WECHAT = 1;
  23. const PAY_TYPE_ACCOUNT = 2;
  24. const PAY_TYPE_ADMIN_GIVE = 3;
  25. const PAY_TYPE_INVITE_NEW_USER_GIVE = 4;
  26. public static $payTypeMaps = [
  27. self::PAY_TYPE_WECHAT => '微信支付',
  28. self::PAY_TYPE_ACCOUNT => '余额支付',
  29. self::PAY_TYPE_ADMIN_GIVE => '后台赠送',
  30. self::PAY_TYPE_INVITE_NEW_USER_GIVE => '邀请新用户赠送'
  31. ];
  32. const PAY_STATUS_OK = 1;
  33. const PAY_STATUS_NO = 0;
  34. public static $payStatusMaps = [
  35. self::PAY_STATUS_OK => '支付成功',
  36. self::PAY_STATUS_NO => '支付失败',
  37. ];
  38. const STATUS_OK = 1;
  39. const STATUS_NO = 0;
  40. public static $statusMaps = [
  41. self::STATUS_OK => '支付成功',
  42. self::STATUS_NO => '支付失败'
  43. ];
  44. /**
  45. * 生成订单号
  46. * @return bool|string
  47. * User: Mead
  48. */
  49. public static function makeNo()
  50. {
  51. // 订单流水号前缀
  52. $prefix = config('bike.no_tag') . self::NO_TAG . date('YmdHis');
  53. for ($i = 0; $i < 10; $i++) {
  54. // 随机生成 6 位的数字
  55. $no = $prefix . str_pad(random_int(0, 999999), 6, '0', STR_PAD_LEFT);
  56. // 判断是否已经存在
  57. if (!static::query()->where('no', $no)->exists()) {
  58. return $no;
  59. }
  60. }
  61. Log::warning('find order no failed');
  62. return false;
  63. }
  64. public function pay_order_callback()
  65. {
  66. //添加钱包记录
  67. WalletLog::log(WalletLog::OPERATE_TYPE_ADD, $this->attributes['pay_money'], $this->attributes['user_id'], WalletLog::TYPE_ADD_WECHAT_PAY_CARD_DEPOSIT_ORDER, $this->attributes['area_id'], $this->attributes['id'], DepositCardOrder::class);
  68. WalletLog::log(WalletLog::OPERATE_TYPE_SUB, $this->attributes['pay_money'], $this->attributes['user_id'], WalletLog::TYPE_SUB_WECHAT_PAY_CARD_DEPOSIT_ORDER, $this->attributes['area_id'], $this->attributes['id'], DepositCardOrder::class);
  69. // 修改用户押金状态
  70. $user = User::find($this->attributes['user_id']);
  71. $deposit_expire_time = Carbon::parse($user->deposit_expire_time);
  72. if (Carbon::now()->gte($deposit_expire_time)) {
  73. $time = Carbon::now();
  74. } else {
  75. $time = $deposit_expire_time;
  76. }
  77. //判断用户是否已经缴纳押金
  78. if ((int)$user->is_deposit === User::DEPOSIT_OK && (int)$user->deposit_type === User::DEPOSIT_MONEY) {
  79. //已交押金
  80. User::where('id', $this->attributes['user_id'])->update([
  81. 'deposit_expire_time' => $time->addDays($this->attributes['effective_days'])
  82. ]);
  83. } else {
  84. //未交押金
  85. User::where('id', $this->attributes['user_id'])->update([
  86. 'deposit_expire_time' => $time->addDays($this->attributes['effective_days']),
  87. 'deposit_type' => User::DEPOSIT_CARD,
  88. 'is_deposit' => User::DEPOSIT_OK
  89. ]);
  90. }
  91. $this->save();
  92. return true;
  93. }
  94. }