ShopOrder.php 3.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112
  1. <?php
  2. namespace App\Repositories\Models\Dwbs;
  3. use App\Repositories\Enums\Dwbs\ShopOrderStatusEnum;
  4. use App\Repositories\Models\Base\User;
  5. use App\Repositories\Models\Model;
  6. use Illuminate\Support\Facades\Crypt;
  7. class ShopOrder extends Model
  8. {
  9. /**
  10. * @var string
  11. */
  12. protected $table = 'dwbs_shop_orders';
  13. protected $guarded = [];
  14. /**
  15. * The attributes excluded from the model's JSON form.
  16. *
  17. * @var array
  18. */
  19. protected $hidden = [];
  20. protected $casts = [];
  21. const NO_TAG = "O";
  22. protected static function booted()
  23. {
  24. parent::booted(); // TODO: Change the autogenerated stub
  25. //todo:消息提醒
  26. self::created(function (ShopOrder $model) {
  27. //兑换提醒
  28. });
  29. self::updated(function (ShopOrder $model) {
  30. if ($model->isDirty('status')) {
  31. switch ($model->status) {
  32. case ShopOrderStatusEnum::close:
  33. //作废提醒
  34. break;
  35. case ShopOrderStatusEnum::wait_shou:
  36. //发货提醒
  37. break;
  38. }
  39. }
  40. });
  41. }
  42. public static function makeNo()
  43. {
  44. // 订单流水号前缀
  45. $prefix = 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('order_no', $no)->exists()) {
  51. return $no;
  52. }
  53. }
  54. return false;
  55. }
  56. public function goods()
  57. {
  58. return $this->hasMany(ShopOrderGood::class, 'order_id')->with(['good'])->select(['nums', 'id', 'good_id', 'jifen', 'total_jifen']);
  59. }
  60. public function user()
  61. {
  62. return $this->belongsTo(User::class)->select(['id', 'nickname', 'mobile', 'mobile_encryption', 'mobile_code']);
  63. }
  64. public function getMSjrMobileAttribute()
  65. {
  66. if (isset($this->attributes['sjr_mobile_encryption'])) {
  67. return Crypt::decryptString($this->attributes['sjr_mobile_encryption']);
  68. }
  69. return '';
  70. }
  71. /**
  72. * 根据用户手机号查询用户
  73. * @param $mobile
  74. * @return mixed
  75. */
  76. public static function byMobileGetIds($mobile)
  77. {
  78. // return Cache::remember("model:user:byMobileGetIds:{$mobile}", Carbon::now()->addHours(4), function () use ($mobile) {
  79. if (strlen($mobile) == 11) {
  80. $users = self::query()->where('mobile', mobile_hidden($mobile))->select(['id', 'sjr_mobile', 'sjr_mobile_encryption'])->get();
  81. $len = count($users);
  82. if (!$len) return [];
  83. $ids = [];
  84. foreach ($users as $user) {
  85. if ($mobile === $user->m_mobile) {
  86. $ids[] = $user->id;
  87. }
  88. }
  89. return $ids;
  90. }
  91. return self::query()->where('mobile', 'like', "%{$mobile}")->pluck('id');
  92. // });
  93. }
  94. }