CouponModel.php 7.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251
  1. <?php
  2. namespace App\Models;
  3. use App\Http\Controllers\Order;
  4. use Illuminate\Database\Eloquent\Model;
  5. use Illuminate\Database\Query\Builder;
  6. /**
  7. * App\Models\CouponModel
  8. *
  9. * @property int $id
  10. * @property int $uid 所有者
  11. * @property string $name 名称
  12. * @property string $type 券的类型
  13. * @property string $goods_scope 商品范围
  14. * @property int $discount 打x折
  15. * @property int $coupon_amount 减x (单位:分)
  16. * @property int $min_amount 最低消费(满xx减)(单位:分)
  17. * @property string $describe 描述
  18. * @property \Illuminate\Support\Carbon $created_at 创建时间
  19. * @property \Illuminate\Support\Carbon $updated_at 修改时间
  20. * @property int $used_at 使用时间
  21. * @property int $order_id 订单ID
  22. * @property int $get_at 领取时间
  23. * @property int $open_at 开放时间
  24. * @property int $overdue_at 过期时间
  25. * @property int $looked_at 查看时间
  26. * @property string $batch_id 优惠券批号
  27. * @property string|null $code 兑换码
  28. * @method static \Illuminate\Database\Eloquent\Builder|\App\Models\CouponModel existing($uid)
  29. * @method static \Illuminate\Database\Eloquent\Builder|\App\Models\CouponModel newModelQuery()
  30. * @method static \Illuminate\Database\Eloquent\Builder|\App\Models\CouponModel newQuery()
  31. * @method static \Illuminate\Database\Eloquent\Builder|\App\Models\CouponModel query()
  32. * @method static \Illuminate\Database\Eloquent\Builder|\App\Models\CouponModel whereBatchId($value)
  33. * @method static \Illuminate\Database\Eloquent\Builder|\App\Models\CouponModel whereCode($value)
  34. * @method static \Illuminate\Database\Eloquent\Builder|\App\Models\CouponModel whereCouponAmount($value)
  35. * @method static \Illuminate\Database\Eloquent\Builder|\App\Models\CouponModel whereCreatedAt($value)
  36. * @method static \Illuminate\Database\Eloquent\Builder|\App\Models\CouponModel whereDescribe($value)
  37. * @method static \Illuminate\Database\Eloquent\Builder|\App\Models\CouponModel whereDiscount($value)
  38. * @method static \Illuminate\Database\Eloquent\Builder|\App\Models\CouponModel whereGetAt($value)
  39. * @method static \Illuminate\Database\Eloquent\Builder|\App\Models\CouponModel whereGoodsScope($value)
  40. * @method static \Illuminate\Database\Eloquent\Builder|\App\Models\CouponModel whereId($value)
  41. * @method static \Illuminate\Database\Eloquent\Builder|\App\Models\CouponModel whereLookedAt($value)
  42. * @method static \Illuminate\Database\Eloquent\Builder|\App\Models\CouponModel whereMinAmount($value)
  43. * @method static \Illuminate\Database\Eloquent\Builder|\App\Models\CouponModel whereName($value)
  44. * @method static \Illuminate\Database\Eloquent\Builder|\App\Models\CouponModel whereOpenAt($value)
  45. * @method static \Illuminate\Database\Eloquent\Builder|\App\Models\CouponModel whereOrderId($value)
  46. * @method static \Illuminate\Database\Eloquent\Builder|\App\Models\CouponModel whereOverdueAt($value)
  47. * @method static \Illuminate\Database\Eloquent\Builder|\App\Models\CouponModel whereType($value)
  48. * @method static \Illuminate\Database\Eloquent\Builder|\App\Models\CouponModel whereUid($value)
  49. * @method static \Illuminate\Database\Eloquent\Builder|\App\Models\CouponModel whereUpdatedAt($value)
  50. * @method static \Illuminate\Database\Eloquent\Builder|\App\Models\CouponModel whereUsedAt($value)
  51. * @mixin \Eloquent
  52. */
  53. class CouponModel extends Model
  54. {
  55. //
  56. protected $table = "kdgx_fpdx_coupons";
  57. public $dateFormat = 'U';
  58. public $fillable = [
  59. 'uid',
  60. 'name',
  61. 'describe',
  62. 'type',
  63. 'goods',
  64. 'discount',
  65. 'amount',
  66. 'min_amount',
  67. 'describe',
  68. 'used_at',
  69. 'open_at',
  70. 'overdue_at',
  71. 'looked_at',
  72. 'order_id',
  73. 'batch_id',
  74. 'code',
  75. ];
  76. public const GOODS_TYPE = [
  77. 'super_vip' => [
  78. 12,
  79. 13,
  80. 14,
  81. ],
  82. 'flower' => [
  83. 1,
  84. 9,
  85. 10,
  86. ],
  87. 'pair' => [
  88. 3,
  89. ]
  90. ];
  91. /**
  92. * 现有的
  93. * @param $query
  94. * @param int $uid
  95. * @return mixed
  96. */
  97. public function scopeExisting(/** @var Builder $query */ $query, int $uid)
  98. {
  99. return $query->where('uid', $uid)
  100. ->where('used_at', 0)
  101. ->where('overdue_at', '>', time());
  102. }
  103. /**
  104. * @param $value
  105. * @return int
  106. */
  107. public function getDiscountAttribute($value)
  108. {
  109. return ($value <= 0 || $value > 10) ? 10 : $value;
  110. }
  111. /**
  112. * 卡券使用
  113. * @param int $goodsId
  114. * @param int $orderId
  115. * @return array
  116. */
  117. public function use(int $goodsId, int $orderId)
  118. {
  119. $this->order_id = $orderId;
  120. $this->used_at = time();
  121. $this->save();
  122. }
  123. /**
  124. * 是否归属于某人
  125. * @param int $uid
  126. * @return bool
  127. */
  128. public function isOwner($uid)
  129. {
  130. return $this->uid === $uid;
  131. }
  132. /**
  133. * 卡券是否已使用
  134. * @return bool
  135. */
  136. public function isUsed()
  137. {
  138. return $this->used_at !== 0;
  139. }
  140. /**
  141. * 卡券是否已过期
  142. * @return bool
  143. */
  144. public function isOverdue()
  145. {
  146. return $this->overdue_at <= time();
  147. }
  148. /**
  149. * 卡券是否开始使用
  150. * @return bool
  151. */
  152. public function isOpened()
  153. {
  154. return $this->open_at <= time();
  155. }
  156. /**
  157. * 是否符合商品
  158. * @param $goodsId
  159. * @return bool
  160. */
  161. public function isGoodsScope($goodsId)
  162. {
  163. $goods_list = self::GOODS_TYPE[$this->goods_scope];
  164. return in_array($goodsId, $goods_list);
  165. }
  166. /**
  167. * 卡券是否满足商品优惠价格
  168. * @param $goodsId
  169. * @return bool
  170. */
  171. public function isSatisfyPrice($goodsId)
  172. {
  173. $goods = Order::GOODS[$goodsId];
  174. return $goods['total_fee'] >= $this->min_amount;
  175. }
  176. /**
  177. * 计算优惠价格
  178. * @param $goodsId
  179. * @return array
  180. */
  181. public function getSettlementAmount($goodsId)
  182. {
  183. $goods = Order::GOODS[$goodsId];
  184. $total_amount = $goods['total_fee'];
  185. switch ($this->type) {
  186. case "无门槛券":
  187. case "72小时入场券":
  188. case "满减券":
  189. $settlement_coupon_amount = ($this->coupon_amount > $total_amount)
  190. ? $total_amount
  191. : $this->coupon_amount;
  192. break;
  193. case "折扣券":
  194. $settlement_coupon_amount = floor($goods['total_fee'] * (1 - $this->discount / 10) / 10) * 10;
  195. break;
  196. default:
  197. $settlement_coupon_amount = 0;
  198. }
  199. $settlement_total_amount = (int)($total_amount - $this->coupon_amount);
  200. return [
  201. 'total_amount' => $total_amount,
  202. 'settlement_coupon_amount' => $settlement_coupon_amount,
  203. 'settlement_total_amount' => $settlement_total_amount,
  204. ];
  205. }
  206. public function look()
  207. {
  208. if ($this->looked_at == 0) {
  209. $this->looked_at = time();
  210. $this->save();
  211. }
  212. }
  213. public static function exchange($uid, $code)
  214. {
  215. }
  216. }