UserCoupon.php 1.7 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950
  1. <?php
  2. /**
  3. * Created by PhpStorm.
  4. * User: zilongs
  5. * Date: 20-9-30
  6. * Time: 下午11:25
  7. */
  8. namespace App\Models;
  9. class UserCoupon extends BaseModel
  10. {
  11. public static function getDiscountAmount($id, $user_id, $total_amount, $product_type)
  12. {
  13. $userCoupon = self::where('id', $id)->where('user_id', $user_id)->first();
  14. if (empty($userCoupon)) {
  15. exit_out(null, 10001, '用户优惠券不存在');
  16. }
  17. if ($userCoupon['status'] == 2) {
  18. exit_out(null, 10002, '该优惠券已被使用,请勿重复使用');
  19. }
  20. if ($userCoupon['status'] == 3) {
  21. exit_out(null, 10003, '该优惠券已过期');
  22. }
  23. if ($userCoupon['status'] == 1) {
  24. if ($userCoupon['expire_time'] < time()) {
  25. self::where('id', $id)->update(['status' => 3]);
  26. exit_out(null, 10004, '该优惠券已过期');
  27. }
  28. if ($userCoupon['min_consume_amount'] > $total_amount) {
  29. exit_out(null, 10005, '未达到该优惠券的最低消费金额');
  30. }
  31. if ($userCoupon['usable_type'] == 2 && !CouponType::where('user_coupon_id', $id)->where('product_type', $product_type)->count()) {
  32. exit_out(null, 10006, '该优惠券不支持该类型的订单');
  33. }
  34. }
  35. if ($userCoupon['type'] == 1) {
  36. $discountAmount = $userCoupon['money'];
  37. }
  38. else {
  39. $discountAmount = floor((1 - $userCoupon['discount']/10)*$total_amount);
  40. $discountAmount = $userCoupon['max_reduce_amount'] < $discountAmount ? $userCoupon['max_reduce_amount'] : $discountAmount;
  41. }
  42. return $discountAmount;
  43. }
  44. }