GoodsLimitBuy.php 1.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263
  1. <?php
  2. /**
  3. * Created by PhpStorm.
  4. * User: Administrator
  5. * Date: 2018/3/20 0020
  6. * Time: 下午 3:48
  7. */
  8. namespace app\common\models\goods;
  9. use app\common\exceptions\AppException;
  10. use app\common\models\BaseModel;
  11. use app\common\models\Goods;
  12. use Carbon\Carbon;
  13. /**
  14. * Class GoodsLimitBuy
  15. * @package app\common\models\goods
  16. * @property Goods $goods
  17. */
  18. class GoodsLimitBuy extends BaseModel
  19. {
  20. protected $table = 'yz_goods_limitbuy';
  21. public $timestamps = false;
  22. protected $guarded = [''];
  23. protected $appends= [];
  24. public function getDisplayNameAttribute($value)
  25. {
  26. if (empty($value)) {
  27. return '限时购';
  28. }
  29. return $value;
  30. }
  31. static function getDataByGoodsId($goods_id)
  32. {
  33. return self::uniacid()
  34. ->where('goods_id', $goods_id)
  35. ->first();
  36. }
  37. public function goods(){
  38. return $this->belongsTo(Goods::class);
  39. }
  40. public function check(){
  41. if ($this->status) {
  42. $startTime = Carbon::createFromTimestamp($this->start_time);
  43. if (Carbon::now()->lessThan($startTime)) {
  44. throw new AppException('此商品将于' . $startTime->toDateTimeString() . '开启限时购买');
  45. }
  46. $endTime = Carbon::createFromTimestamp($this->end_time);
  47. if (Carbon::now()->greaterThanOrEqualTo($endTime)) {
  48. throw new AppException('商品['.$this->goods->title.']已于' . $endTime->toDateTimeString() . '结束限时购买');
  49. }
  50. }
  51. }
  52. }