CouponService.php 7.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170
  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. // dump("周期全",$coupons);
  39. foreach ($coupons as $coupon) {
  40. if (!empty($coupon['coupon_validdate_end']) && time() > strtotime($coupon['coupon_validdate_end'])) {
  41. continue;
  42. }
  43. $details = CouponDetail::where('join_detail_coupon_id', $coupon['coupon_id'])
  44. ->where('join_coupon_detail_member_id','<>','')
  45. ->selectRaw('join_coupon_detail_member_id,COUNT(*) as count')
  46. ->groupBy('join_coupon_detail_member_id')
  47. ->get();
  48. foreach ($details as $item) {
  49. $detail = CouponDetail::where('join_detail_coupon_id', $coupon['coupon_id'])
  50. ->where('join_coupon_detail_member_id', $item->join_coupon_detail_member_id)
  51. ->orderBy('coupon_detail_addtimes', 'DESC')
  52. ->first();
  53. if (strtotime($detail->coupon_detail_deadline_datetime) > time()) {
  54. continue;
  55. } elseif (empty($coupon['coupon_validdate_end']) && !empty($coupon['coupon_validdate_day'])) {
  56. $firstDetail = CouponDetail::where('join_detail_coupon_id', $coupon['coupon_id'])
  57. ->where('join_coupon_detail_member_id', $item->join_coupon_detail_member_id)
  58. ->orderBy('coupon_detail_addtimes', 'ASC')
  59. ->first();
  60. if ((time() - strtotime($firstDetail->coupon_detail_addtimes)) > (3600 * 24 * $coupon['coupon_validdate_day'])) {
  61. continue;
  62. }
  63. }
  64. $params = [
  65. 'coupon_detail_gain_datetime' => date('Y-m-d 00:00:00'),
  66. 'coupon_id' => $coupon['coupon_id'],
  67. 'member_id' => $detail->join_coupon_detail_member_id,
  68. 'coupon_detail_period_num' => $detail->coupon_detail_period_num + 1
  69. ];
  70. // 超出期数
  71. if (!empty($coupon['coupon_period_json'])) {
  72. $periodJson = json_decode($coupon['coupon_period_json'], true);
  73. if ($periodJson['nbr'] < $params['coupon_detail_period_num']) {
  74. continue;
  75. }
  76. if ($periodJson['unit'] == 'day') {
  77. $val = $periodJson['val'] - 1;
  78. if ($val < 1) {
  79. $params['coupon_detail_deadline_datetime'] = date('Y-m-d 23:59:59');
  80. } else {
  81. $params['coupon_detail_deadline_datetime'] = date('Y-m-t 23:59:59', strtotime("+" . $val . " day"));
  82. }
  83. } elseif ($periodJson['unit'] == 'week') {
  84. $val = $periodJson['val'] - 1;
  85. if ($val < 1) {
  86. $params['coupon_detail_deadline_datetime'] = date('Y-m-d 23:59:59', strtotime('this week Sunday'));
  87. } else {
  88. $params['coupon_detail_deadline_datetime'] = date('Y-m-t 23:59:59', strtotime("+" . $val . ' week', date('Y-m-d', strtotime("+" . $val . " month"))));
  89. }
  90. } elseif ($periodJson['unit'] == 'month') {
  91. $val = $periodJson['val'] - 1;
  92. if ($val < 1) {
  93. $params['coupon_detail_deadline_datetime'] = date('Y-m-t 23:59:59');
  94. } else {
  95. $params['coupon_detail_deadline_datetime'] = date('Y-m-t 23:59:59', strtotime("+" . $val . " month"));
  96. }
  97. }
  98. }
  99. if (CouponDetail::whereIn('coupon_detail_status',['INIT','PENDING'])->exists()){
  100. // 匹配已发行的优惠券
  101. $params['chooseCouponNbr'] = 1;
  102. CouponDetailService::customSendCouponHave($params);
  103. }else{
  104. // 写入优惠券
  105. CouponDetailService::customSendCoupon($params);
  106. }
  107. _syslog("周期券", "发放周期券", false, $params, 1001);
  108. }
  109. }
  110. Db::commit();
  111. } catch (\Exception $e) {
  112. dump($e->getTrace());
  113. Db::rollBack();
  114. }
  115. }
  116. public static function couponClassifyInfo($classify, $category, $value, $limit)
  117. {
  118. try {
  119. switch ($classify) {
  120. case "满减券":
  121. if (!empty($limit) && $category == 'NORMAL') {
  122. return "满" . $limit . '减' . $value;
  123. } elseif (!empty($limit) && !empty($value) && $category == 'PIECE') {
  124. return "满" . intval($limit) . '件减' . $value;
  125. }
  126. break;
  127. case "立减券":
  128. return "立减" . $value;
  129. break;
  130. case "抵用券":
  131. return "抵用券";
  132. break;
  133. case "折扣券":
  134. if (!empty($limit) && $category == 'NORMAL') {
  135. return "满" . $limit . '打' . ($value / 10) . '折';
  136. } else if (!empty($limit) && !empty($value) && $category == 'PIECE') {
  137. return "满" . intval($limit) . '件打' . ($value / 10) . '折';
  138. } else {
  139. return $value / 10 . '折';
  140. }
  141. break;
  142. case "赠品券":
  143. return "赠品券";
  144. break;
  145. case "福利券":
  146. return '抵扣' . $value;
  147. break;
  148. case "年卡":
  149. return "年卡";
  150. break;
  151. case "季卡":
  152. return "季卡";
  153. break;
  154. case "月卡":
  155. return "月卡";
  156. break;
  157. }
  158. } catch (\Exception $e) {
  159. }
  160. }
  161. }