123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251 |
- <?php
- namespace App\Models;
- use App\Http\Controllers\Order;
- use Illuminate\Database\Eloquent\Model;
- use Illuminate\Database\Query\Builder;
- /**
- * App\Models\CouponModel
- *
- * @property int $id
- * @property int $uid 所有者
- * @property string $name 名称
- * @property string $type 券的类型
- * @property string $goods_scope 商品范围
- * @property int $discount 打x折
- * @property int $coupon_amount 减x (单位:分)
- * @property int $min_amount 最低消费(满xx减)(单位:分)
- * @property string $describe 描述
- * @property \Illuminate\Support\Carbon $created_at 创建时间
- * @property \Illuminate\Support\Carbon $updated_at 修改时间
- * @property int $used_at 使用时间
- * @property int $order_id 订单ID
- * @property int $get_at 领取时间
- * @property int $open_at 开放时间
- * @property int $overdue_at 过期时间
- * @property int $looked_at 查看时间
- * @property string $batch_id 优惠券批号
- * @property string|null $code 兑换码
- * @method static \Illuminate\Database\Eloquent\Builder|\App\Models\CouponModel existing($uid)
- * @method static \Illuminate\Database\Eloquent\Builder|\App\Models\CouponModel newModelQuery()
- * @method static \Illuminate\Database\Eloquent\Builder|\App\Models\CouponModel newQuery()
- * @method static \Illuminate\Database\Eloquent\Builder|\App\Models\CouponModel query()
- * @method static \Illuminate\Database\Eloquent\Builder|\App\Models\CouponModel whereBatchId($value)
- * @method static \Illuminate\Database\Eloquent\Builder|\App\Models\CouponModel whereCode($value)
- * @method static \Illuminate\Database\Eloquent\Builder|\App\Models\CouponModel whereCouponAmount($value)
- * @method static \Illuminate\Database\Eloquent\Builder|\App\Models\CouponModel whereCreatedAt($value)
- * @method static \Illuminate\Database\Eloquent\Builder|\App\Models\CouponModel whereDescribe($value)
- * @method static \Illuminate\Database\Eloquent\Builder|\App\Models\CouponModel whereDiscount($value)
- * @method static \Illuminate\Database\Eloquent\Builder|\App\Models\CouponModel whereGetAt($value)
- * @method static \Illuminate\Database\Eloquent\Builder|\App\Models\CouponModel whereGoodsScope($value)
- * @method static \Illuminate\Database\Eloquent\Builder|\App\Models\CouponModel whereId($value)
- * @method static \Illuminate\Database\Eloquent\Builder|\App\Models\CouponModel whereLookedAt($value)
- * @method static \Illuminate\Database\Eloquent\Builder|\App\Models\CouponModel whereMinAmount($value)
- * @method static \Illuminate\Database\Eloquent\Builder|\App\Models\CouponModel whereName($value)
- * @method static \Illuminate\Database\Eloquent\Builder|\App\Models\CouponModel whereOpenAt($value)
- * @method static \Illuminate\Database\Eloquent\Builder|\App\Models\CouponModel whereOrderId($value)
- * @method static \Illuminate\Database\Eloquent\Builder|\App\Models\CouponModel whereOverdueAt($value)
- * @method static \Illuminate\Database\Eloquent\Builder|\App\Models\CouponModel whereType($value)
- * @method static \Illuminate\Database\Eloquent\Builder|\App\Models\CouponModel whereUid($value)
- * @method static \Illuminate\Database\Eloquent\Builder|\App\Models\CouponModel whereUpdatedAt($value)
- * @method static \Illuminate\Database\Eloquent\Builder|\App\Models\CouponModel whereUsedAt($value)
- * @mixin \Eloquent
- */
- class CouponModel extends Model
- {
- //
- protected $table = "kdgx_fpdx_coupons";
- public $dateFormat = 'U';
- public $fillable = [
- 'uid',
- 'name',
- 'describe',
- 'type',
- 'goods',
- 'discount',
- 'amount',
- 'min_amount',
- 'describe',
- 'used_at',
- 'open_at',
- 'overdue_at',
- 'looked_at',
- 'order_id',
- 'batch_id',
- 'code',
- ];
- public const GOODS_TYPE = [
- 'super_vip' => [
- 12,
- 13,
- 14,
- ],
- 'flower' => [
- 1,
- 9,
- 10,
- ],
- 'pair' => [
- 3,
- ]
- ];
- /**
- * 现有的
- * @param $query
- * @param int $uid
- * @return mixed
- */
- public function scopeExisting(/** @var Builder $query */ $query, int $uid)
- {
- return $query->where('uid', $uid)
- ->where('used_at', 0)
- ->where('overdue_at', '>', time());
- }
- /**
- * @param $value
- * @return int
- */
- public function getDiscountAttribute($value)
- {
- return ($value <= 0 || $value > 10) ? 10 : $value;
- }
- /**
- * 卡券使用
- * @param int $goodsId
- * @param int $orderId
- * @return array
- */
- public function use(int $goodsId, int $orderId)
- {
- $this->order_id = $orderId;
- $this->used_at = time();
- $this->save();
- }
- /**
- * 是否归属于某人
- * @param int $uid
- * @return bool
- */
- public function isOwner($uid)
- {
- return $this->uid === $uid;
- }
- /**
- * 卡券是否已使用
- * @return bool
- */
- public function isUsed()
- {
- return $this->used_at !== 0;
- }
- /**
- * 卡券是否已过期
- * @return bool
- */
- public function isOverdue()
- {
- return $this->overdue_at <= time();
- }
- /**
- * 卡券是否开始使用
- * @return bool
- */
- public function isOpened()
- {
- return $this->open_at <= time();
- }
- /**
- * 是否符合商品
- * @param $goodsId
- * @return bool
- */
- public function isGoodsScope($goodsId)
- {
- $goods_list = self::GOODS_TYPE[$this->goods_scope];
- return in_array($goodsId, $goods_list);
- }
- /**
- * 卡券是否满足商品优惠价格
- * @param $goodsId
- * @return bool
- */
- public function isSatisfyPrice($goodsId)
- {
- $goods = Order::GOODS[$goodsId];
- return $goods['total_fee'] >= $this->min_amount;
- }
- /**
- * 计算优惠价格
- * @param $goodsId
- * @return array
- */
- public function getSettlementAmount($goodsId)
- {
- $goods = Order::GOODS[$goodsId];
- $total_amount = $goods['total_fee'];
- switch ($this->type) {
- case "无门槛券":
- case "72小时入场券":
- case "满减券":
- $settlement_coupon_amount = ($this->coupon_amount > $total_amount)
- ? $total_amount
- : $this->coupon_amount;
- break;
- case "折扣券":
- $settlement_coupon_amount = floor($goods['total_fee'] * (1 - $this->discount / 10) / 10) * 10;
- break;
- default:
- $settlement_coupon_amount = 0;
- }
- $settlement_total_amount = (int)($total_amount - $this->coupon_amount);
- return [
- 'total_amount' => $total_amount,
- 'settlement_coupon_amount' => $settlement_coupon_amount,
- 'settlement_total_amount' => $settlement_total_amount,
- ];
- }
- public function look()
- {
- if ($this->looked_at == 0) {
- $this->looked_at = time();
- $this->save();
- }
- }
- public static function exchange($uid, $code)
- {
- }
- }
|