CouponService.php 4.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135
  1. <?php
  2. // +----------------------------------------------------------------------
  3. // | likeshop100%开源免费商用商城系统
  4. // +----------------------------------------------------------------------
  5. // | 欢迎阅读学习系统程序代码,建议反馈是我们前进的动力
  6. // | 开源版本可自由商用,可去除界面版权logo
  7. // | 商业版本务必购买商业授权,以免引起法律纠纷
  8. // | 禁止对系统程序代码以任何目的,任何形式的再发布
  9. // | gitee下载:https://gitee.com/likeshop_gitee
  10. // | github下载:https://github.com/likeshop-github
  11. // | 访问官网:https://www.likeshop.cn
  12. // | 访问社区:https://home.likeshop.cn
  13. // | 访问手册:http://doc.likeshop.cn
  14. // | 微信公众号:likeshop技术社区
  15. // | likeshop团队 版权所有 拥有最终解释权
  16. // +----------------------------------------------------------------------
  17. // | author: likeshopTeam
  18. // +----------------------------------------------------------------------
  19. namespace app\shopapi\service;
  20. use app\common\enum\CouponEnum;
  21. use app\common\model\CouponList;
  22. class CouponService
  23. {
  24. /**
  25. * @notes 验证活动商品是否可用优惠券
  26. * @param $user_id
  27. * @param $coupon_list_id
  28. * @param $orderStatus
  29. * @return array
  30. * @author 张无忌
  31. * @date 2021/8/3 18:37
  32. */
  33. public static function isUsable($user_id, $coupon_list_id, $orderStatus)
  34. {
  35. $coupon = (new CouponList())->alias('CL')
  36. ->field('C.*,CL.id as cl_id,CL.create_time as receive_time')
  37. ->join('coupon C', 'C.id = CL.coupon_id')
  38. ->where([
  39. ['CL.id', '=', intval($coupon_list_id)],
  40. ['CL.user_id', '=', intval($user_id)],
  41. ['CL.status', '=', CouponEnum::USE_STATUS_NOT],
  42. ['CL.invalid_time', '>', time()],
  43. ])->findOrEmpty()->toArray();
  44. if ($coupon) {
  45. $result = self::isUseWhere($coupon, $orderStatus['goods']['id'], $orderStatus['total_amount']);
  46. if ($result) {
  47. return [
  48. 'isUsable' => true,
  49. 'money' => $coupon['money']
  50. ];
  51. }
  52. }
  53. return [
  54. 'isUsable' => false,
  55. 'money' => 0
  56. ];
  57. }
  58. /**
  59. * @notes 验证是否满足基本使用条件
  60. * @param $coupon
  61. * @param $goods_id
  62. * @param $order_total_amount
  63. * @return bool
  64. * @author 张无忌
  65. * @date 2021/8/4 9:47
  66. */
  67. public static function isUseWhere($coupon, $goods_id, $order_total_amount)
  68. {
  69. switch ($coupon['use_time_type']) {
  70. case CouponEnum::USE_TIME_TYPE_FIXED:
  71. if ($coupon['use_time_start'] >= time() and $coupon['use_time_end'] <= time()) {
  72. return false;
  73. }
  74. break;
  75. case CouponEnum::USE_TIME_TYPE_TODAY:
  76. $use_time = intval($coupon['use_time'] * 86400);
  77. if ($coupon['receive_time'] >= time() and $use_time <= time()) {
  78. return false;
  79. }
  80. break;
  81. case CouponEnum::USE_TIME_TYPE_TOMORROW:
  82. $receive_time = date('Y-m-d', $coupon['receive_time']);
  83. $receive_time = strtotime($receive_time) + 86400;
  84. $use_time = intval($coupon['use_time'] * 86400);
  85. if (time() <= $receive_time and $use_time <= time()) {
  86. return false;
  87. }
  88. break;
  89. }
  90. switch ($coupon['use_goods_type']) {
  91. case CouponEnum::USE_GOODS_TYPE_NOT:
  92. break;
  93. case CouponEnum::USE_GOODS_TYPE_ALLOW:
  94. if (!$coupon['use_goods_ids']) {
  95. return false;
  96. }
  97. $use_goods_ids = $coupon['use_goods_ids'];
  98. if (!in_array($goods_id, $use_goods_ids)) {
  99. return false;
  100. }
  101. break;
  102. case CouponEnum::USE_GOODS_TYPE_BAN:
  103. if ($coupon['use_goods_ids']) {
  104. $use_goods_ids = $coupon['use_goods_ids'];
  105. if (in_array($goods_id, $use_goods_ids)) {
  106. return false;
  107. }
  108. }
  109. break;
  110. }
  111. switch ($coupon['condition_type']) {
  112. case CouponEnum::CONDITION_TYPE_NOT:
  113. break;
  114. case CouponEnum::CONDITION_TYPE_FULL:
  115. if ($order_total_amount < $coupon['condition_money']) {
  116. return false;
  117. }
  118. break;
  119. }
  120. return true;
  121. }
  122. }