CouponService.php 6.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151
  1. <?php
  2. namespace app\admin\service\coupon;
  3. use app\model\Coupon;
  4. use app\model\CouponDetail;
  5. use support\Db;
  6. class CouponService
  7. {
  8. /**
  9. * @Desc 优惠券自动过期
  10. * @Author Gorden
  11. * @Date 2024/8/26 17:58
  12. *
  13. * @return void
  14. */
  15. public static function checkCouponExpired()
  16. {
  17. $couponDetails = Db::select("select * from app_coupon_detail where (coupon_detail_status = 'INIT' OR coupon_detail_status = 'PENDING' OR coupon_detail_status = 'ACTIVED' OR coupon_detail_status = 'WAITING') AND coupon_detail_deadline_datetime != '' AND CAST(UNIX_TIMESTAMP(coupon_detail_deadline_datetime) as SIGNED) < " . time());
  18. foreach ($couponDetails as $detail) {
  19. $endTimeUnix = strtotime($detail->coupon_detail_deadline_datetime);
  20. if ($endTimeUnix < time()) {
  21. CouponDetail::where('coupon_detail_id', $detail->coupon_detail_id)->update(['coupon_detail_status' => 'EXPIRED']);
  22. echo $detail->coupon_detail_id . "已过期\n";
  23. }
  24. }
  25. }
  26. /**
  27. * @Desc
  28. * @Author Gorden
  29. * @Date 2024/9/18 11:44
  30. *
  31. * @return void
  32. */
  33. public static function sendPeriodCoupon()
  34. {
  35. Db::beginTransaction();
  36. try {
  37. $coupons = Coupon::where('coupon_is_period', 'Y')->where('coupon_status', 'ACTIVED')->get()->toArray();
  38. foreach ($coupons as $coupon) {
  39. if (time() > strtotime($coupon['coupon_validdate_end'])) {
  40. continue;
  41. }
  42. $details = CouponDetail::where('join_detail_coupon_id',$coupon['coupon_id'])
  43. ->select('join_coupon_detail_member_id')
  44. ->groupBy('join_coupon_detail_member_id')
  45. ->get();
  46. foreach ($details as $item) {
  47. $detail = CouponDetail::where('join_detail_coupon_id', $coupon['coupon_id'])
  48. ->where('join_coupon_detail_member_id',$item->join_coupon_detail_member_id)
  49. ->orderBy('coupon_detail_addtimes', 'DESC')
  50. ->first();
  51. if (!empty($detail) && strtotime($detail->coupon_detail_deadline_datetime) > time()) {
  52. continue;
  53. }
  54. $params = [
  55. 'coupon_detail_gain_datetime' => date('Y-m-d 00:00:00'),
  56. 'coupon_id' => $coupon['coupon_id'],
  57. 'member_id' => $detail->join_coupon_detail_member_id,
  58. 'coupon_detail_period_num' => $detail->coupon_detail_period_num + 1
  59. ];
  60. if (!empty($coupon['coupon_period_json'])) {
  61. $periodJson = json_decode($coupon['coupon_period_json'], true);
  62. if ($periodJson['nbr'] < $params['coupon_detail_period_num']){
  63. continue;
  64. }
  65. if ($periodJson['unit'] == 'day') {
  66. $val = $periodJson['val'] - 1;
  67. if ($val < 1) {
  68. $params['coupon_detail_deadline_datetime'] = date('Y-m-d 23:59:59');
  69. } else {
  70. $params['coupon_detail_deadline_datetime'] = date('Y-m-t 23:59:59', strtotime("+" . $val . " day"));
  71. }
  72. } elseif ($periodJson['unit'] == 'week') {
  73. $val = $periodJson['val'] - 1;
  74. if ($val < 1) {
  75. $params['coupon_detail_deadline_datetime'] = date('Y-m-d 23:59:59', strtotime('this week Sunday'));
  76. } else {
  77. $params['coupon_detail_deadline_datetime'] = date('Y-m-t 23:59:59', strtotime("+" . $val . ' week', date('Y-m-d', strtotime("+" . $val . " month"))));
  78. }
  79. } elseif ($periodJson['unit'] == 'month') {
  80. $val = $periodJson['val'] - 1;
  81. if ($val < 1) {
  82. $params['coupon_detail_deadline_datetime'] = date('Y-m-t 23:59:59');
  83. } else {
  84. $params['coupon_detail_deadline_datetime'] = date('Y-m-t 23:59:59', strtotime("+" . $val . " month"));
  85. }
  86. }
  87. }
  88. // 写入优惠券
  89. CouponDetailService::customSendCoupon($params);
  90. _syslog("周期券", "发放周期券", false, $params, 1001);
  91. }
  92. }
  93. Db::commit();
  94. } catch (\Exception $e) {
  95. Db::rollBack();
  96. }
  97. }
  98. public static function couponClassifyInfo($classify, $category, $value, $limit)
  99. {
  100. try {
  101. switch ($classify) {
  102. case "满减券":
  103. if (!empty($limit) && $category == 'NORMAL') {
  104. return "满" . $limit . '减' . $value;
  105. } elseif (!empty($limit) && !empty($value) && $category == 'PIECE') {
  106. return "满" . intval($limit) . '件减' . $value;
  107. }
  108. break;
  109. case "立减券":
  110. return "立减" . $value;
  111. break;
  112. case "抵用券":
  113. return "抵用券";
  114. break;
  115. case "折扣券":
  116. if (!empty($limit) && $category == 'NORMAL') {
  117. return "满" . $limit . '打' . ($value / 10) . '折';
  118. } else if (!empty($limit) && !empty($value) && $category == 'PIECE') {
  119. return "满" . intval($limit) . '件打' . ($value / 10) . '折';
  120. } else {
  121. return $value / 10 . '折';
  122. }
  123. break;
  124. case "赠品券":
  125. return "赠品券";
  126. break;
  127. case "福利券":
  128. return '抵扣' . $value;
  129. break;
  130. case "年卡":
  131. return "年卡";
  132. break;
  133. case "季卡":
  134. return "季卡";
  135. break;
  136. case "月卡":
  137. return "月卡";
  138. break;
  139. }
  140. } catch (\Exception $e) {
  141. }
  142. }
  143. }