123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112 |
- <?php
- namespace App\Repositories\Models\Dwbs;
- use App\Repositories\Enums\Dwbs\ShopOrderStatusEnum;
- use App\Repositories\Models\Base\User;
- use App\Repositories\Models\Model;
- use Illuminate\Support\Facades\Crypt;
- class ShopOrder extends Model
- {
- /**
- * @var string
- */
- protected $table = 'dwbs_shop_orders';
- protected $guarded = [];
- /**
- * The attributes excluded from the model's JSON form.
- *
- * @var array
- */
- protected $hidden = [];
- protected $casts = [];
- const NO_TAG = "O";
- protected static function booted()
- {
- parent::booted(); // TODO: Change the autogenerated stub
- //todo:消息提醒
- self::created(function (ShopOrder $model) {
- //兑换提醒
- });
- self::updated(function (ShopOrder $model) {
- if ($model->isDirty('status')) {
- switch ($model->status) {
- case ShopOrderStatusEnum::close:
- //作废提醒
- break;
- case ShopOrderStatusEnum::wait_shou:
- //发货提醒
- break;
- }
- }
- });
- }
- public static function makeNo()
- {
- // 订单流水号前缀
- $prefix = self::NO_TAG . date('YmdHis');
- for ($i = 0; $i < 10; $i++) {
- // 随机生成 6 位的数字
- $no = $prefix . str_pad(random_int(0, 999999), 6, '0', STR_PAD_LEFT);
- // 判断是否已经存在
- if (!static::query()->where('order_no', $no)->exists()) {
- return $no;
- }
- }
- return false;
- }
- public function goods()
- {
- return $this->hasMany(ShopOrderGood::class, 'order_id')->with(['good'])->select(['nums', 'id', 'good_id', 'jifen', 'total_jifen']);
- }
- public function user()
- {
- return $this->belongsTo(User::class)->select(['id', 'nickname', 'mobile', 'mobile_encryption', 'mobile_code']);
- }
- public function getMSjrMobileAttribute()
- {
- if (isset($this->attributes['sjr_mobile_encryption'])) {
- return Crypt::decryptString($this->attributes['sjr_mobile_encryption']);
- }
- return '';
- }
- /**
- * 根据用户手机号查询用户
- * @param $mobile
- * @return mixed
- */
- public static function byMobileGetIds($mobile)
- {
- // return Cache::remember("model:user:byMobileGetIds:{$mobile}", Carbon::now()->addHours(4), function () use ($mobile) {
- if (strlen($mobile) == 11) {
- $users = self::query()->where('mobile', mobile_hidden($mobile))->select(['id', 'sjr_mobile', 'sjr_mobile_encryption'])->get();
- $len = count($users);
- if (!$len) return [];
- $ids = [];
- foreach ($users as $user) {
- if ($mobile === $user->m_mobile) {
- $ids[] = $user->id;
- }
- }
- return $ids;
- }
- return self::query()->where('mobile', 'like', "%{$mobile}")->pluck('id');
- // });
- }
- }
|