CouponDetailService.php 5.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143
  1. <?php
  2. namespace app\admin\service\coupon;
  3. use app\model\Coupon;
  4. use app\model\CouponDetail;
  5. use app\model\SysSerial;
  6. use support\exception\BusinessException;
  7. class CouponDetailService
  8. {
  9. /**
  10. * @Desc 手动发券
  11. * @Author Gorden
  12. * @Date 2024/8/27 9:57
  13. *
  14. * @param $params
  15. * @return void
  16. * @throws BusinessException
  17. */
  18. public static function customSendCoupon($params)
  19. {
  20. $gettype = 'SEND';
  21. if (!empty($params['gettype'])) {
  22. $gettype = $params['gettype'];
  23. }
  24. try {
  25. CouponDetail::insert([
  26. 'coupon_detail_id' => 'CUDT' . date("ymdHi") . random_string(4, 'up'),
  27. 'join_detail_coupon_id' => $params['coupon_id'],
  28. 'join_coupon_detail_member_id' => $params['member_id'],
  29. 'coupon_detail_status' => 'ACTIVED',
  30. 'coupon_detail_gain_datetime' => $params['coupon_detail_gain_datetime'],
  31. 'coupon_detail_deadline_datetime' => $params['coupon_detail_deadline_datetime'],
  32. 'coupon_detail_period_num' => $params['coupon_detail_period_num'] ?? 0,
  33. 'coupon_detail_extend_json' => json_encode(['gettype' => $gettype]),
  34. 'coupon_detail_addtimes' => time(),
  35. ]);
  36. } catch (\Exception $e) {
  37. throw new BusinessException('写入优惠券失败');
  38. }
  39. }
  40. public static function customSendCouponHave($params)
  41. {
  42. $gettype = 'SEND';
  43. if (!empty($params['gettype'])) {
  44. $gettype = $params['gettype'];
  45. }
  46. try {
  47. CouponDetail::where('join_detail_coupon_id', $params['coupon_id'])
  48. ->whereIn('coupon_detail_status', ['INIT', 'PENDING'])
  49. ->limit($params['chooseCouponNbr'])
  50. ->update([
  51. 'join_coupon_detail_member_id' => $params['member_id'],
  52. 'coupon_detail_gain_datetime' => $params['coupon_detail_gain_datetime'],
  53. 'coupon_detail_deadline_datetime' => $params['coupon_detail_deadline_datetime'],
  54. 'coupon_detail_extend_json' => json_encode(['gettype' => $gettype]),
  55. 'coupon_detail_period_num' => $params['coupon_detail_period_num'] ?? 0,
  56. 'coupon_detail_status' => 'ACTIVED',
  57. 'coupon_detail_addtimes' => time()
  58. ]);
  59. } catch (\Exception $e) {
  60. throw new BusinessException('写入优惠券失败');
  61. }
  62. }
  63. /**
  64. * @Desc 发周期优惠券 - 没有发行数量
  65. * @Author Gorden
  66. * @Date 2024/9/27 13:44
  67. *
  68. * @param $params
  69. * @return void
  70. */
  71. public static function sendPeriodCoupon($params)
  72. {
  73. $coupon = Coupon::where('coupon_id', $params['coupon_id'])->first();
  74. if ($coupon->coupon_is_period != 'Y') {
  75. return;
  76. }
  77. $periodJson = json_decode($coupon->coupon_period_json, true);
  78. if (!empty($periodJson['nbr'])) {
  79. for ($i = 1; $i < $periodJson['nbr']; $i++) {
  80. self::generatePeriod($periodJson);
  81. }
  82. }
  83. }
  84. /**
  85. * @Desc 有发行数量
  86. * @Author Gorden
  87. * @Date 2024/9/27 13:45
  88. *
  89. * @param $params
  90. * @return void
  91. */
  92. public static function sendPeriodCouponHave($params)
  93. {
  94. }
  95. /**
  96. * @Desc
  97. * @Author Gorden
  98. * @Date 2024/9/27 13:54
  99. *
  100. * @param $periodJson 周期JSON
  101. * @return array
  102. */
  103. public static function generatePeriod($periodJson)
  104. {
  105. $params = [];
  106. if ($periodJson['unit'] == 'day') {
  107. $val = $periodJson['val'] - 1;
  108. if ($val < 1) {
  109. $params['coupon_detail_gain_datetime'] = date('Y-m-d 00:00:00');
  110. $params['coupon_detail_deadline_datetime'] = date('Y-m-d 23:59:59');
  111. } else {
  112. $params['coupon_detail_gain_datetime'] = date('Y-m-d 23:59:59', strtotime("+" . $val . " day"));
  113. $params['coupon_detail_deadline_datetime'] = date('Y-m-d 23:59:59', strtotime("+" . $val . " day"));
  114. }
  115. } elseif ($periodJson['unit'] == 'week') {
  116. $val = $periodJson['val'] - 1;
  117. if ($val < 1) {
  118. $params['coupon_detail_gain_datetime'] = date('Y-m-d 00:00:00', strtotime('this week Monday'));
  119. $params['coupon_detail_deadline_datetime'] = date('Y-m-d 23:59:59', strtotime('this week Sunday'));
  120. } else {
  121. $params['coupon_detail_gain_datetime'] = date('Y-m-d 00:00:00', strtotime('this week Monday'));
  122. $params['coupon_detail_deadline_datetime'] = date('Y-m-d 23:59:59', strtotime(date('Y-m-d 23:59:59', strtotime('this week Sunday'))."+" . $val . ' week'));
  123. }
  124. } elseif ($periodJson['unit'] == 'month') {
  125. $val = $periodJson['val'] - 1;
  126. if ($val < 1) {
  127. $params['coupon_detail_deadline_datetime'] = date('Y-m-t 23:59:59');
  128. } else {
  129. $params['coupon_detail_deadline_datetime'] = date('Y-m-t 23:59:59', strtotime("+" . $val . " month"));
  130. }
  131. }
  132. return $params;
  133. }
  134. }