12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455 |
- <?php
- /**
- * Created by PhpStorm.
- * User: zilongs
- * Date: 20-9-30
- * Time: 下午11:25
- */
- namespace App\Models;
- class UserCoupon extends BaseModel
- {
- public static function getDiscountAmount($id, $user_id, $total_amount, $product_type)
- {
- $userCoupon = self::where('id', $id)->where('user_id', $user_id)->first();
- if (empty($userCoupon)) {
- exit_out(null, 10001, '用户优惠券不存在');
- }
- if ($userCoupon['status'] == 2) {
- exit_out(null, 10002, '该优惠券已被使用,请勿重复使用');
- }
- if ($userCoupon['status'] == 3) {
- exit_out(null, 10003, '该优惠券已过期');
- }
- if ($userCoupon['status'] == 1) {
- if ($userCoupon['expire_time'] < time()) {
- self::where('id', $id)->update(['status' => 3]);
- exit_out(null, 10004, '该优惠券已过期');
- }
- if ($userCoupon['min_consume_amount'] > $total_amount) {
- exit_out(null, 10005, '未达到该优惠券的最低消费金额');
- }
- if ($userCoupon['usable_type'] == 2 && !CouponType::where('user_coupon_id', $id)->where('product_type', $product_type)->count()) {
- exit_out(null, 10006, '该优惠券不支持该类型的订单');
- }
- }
- if ($userCoupon['type'] == 1) {
- $discountAmount = $userCoupon['money'];
- }
- else {
- $discountAmount = floor((1 - $userCoupon['discount']/10)*$total_amount);
- $discountAmount = $userCoupon['max_reduce_amount'] < $discountAmount ? $userCoupon['max_reduce_amount'] : $discountAmount;
- }
- return $discountAmount;
- }
- public function couponType()
- {
- return $this->hasMany(CouponType::class,'coupon_id','coupon_id');
- }
- }
|