CouponDetailService.php 12 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267
  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\Db;
  7. use support\exception\BusinessException;
  8. class CouponDetailService
  9. {
  10. /**
  11. * @Desc 手动发券
  12. * @Author Gorden
  13. * @Date 2024/8/27 9:57
  14. *
  15. * @param $params
  16. * @return void
  17. * @throws BusinessException
  18. */
  19. public static function customSendCoupon($params)
  20. {
  21. $gettype = 'SEND';
  22. if (!empty($params['gettype'])) {
  23. $gettype = $params['gettype'];
  24. }
  25. try {
  26. CouponDetail::insert([
  27. 'coupon_detail_id' => 'CUDT' . date("ymdHi") . random_string(4, 'up'),
  28. 'join_detail_coupon_id' => $params['coupon_id'],
  29. 'join_coupon_detail_member_id' => $params['member_id'],
  30. 'coupon_detail_status' => 'ACTIVED',
  31. 'coupon_detail_gain_datetime' => $params['coupon_detail_gain_datetime'],
  32. 'coupon_detail_deadline_datetime' => $params['coupon_detail_deadline_datetime'],
  33. 'coupon_detail_period_num' => $params['coupon_detail_period_num'] ?? 0,
  34. 'coupon_detail_extend_json' => json_encode(['gettype' => $gettype]),
  35. 'coupon_detail_addtimes' => time(),
  36. ]);
  37. } catch (\Exception $e) {
  38. throw new BusinessException('写入优惠券失败');
  39. }
  40. }
  41. public static function customSendCouponHave($params)
  42. {
  43. $gettype = 'SEND';
  44. if (!empty($params['gettype'])) {
  45. $gettype = $params['gettype'];
  46. }
  47. try {
  48. CouponDetail::where('join_detail_coupon_id', $params['coupon_id'])
  49. ->whereIn('coupon_detail_status', ['INIT', 'PENDING'])
  50. ->limit($params['chooseCouponNbr'])
  51. ->update([
  52. 'join_coupon_detail_member_id' => $params['member_id'],
  53. 'coupon_detail_gain_datetime' => $params['coupon_detail_gain_datetime'],
  54. 'coupon_detail_deadline_datetime' => $params['coupon_detail_deadline_datetime'],
  55. 'coupon_detail_extend_json' => json_encode(['gettype' => $gettype]),
  56. 'coupon_detail_period_num' => $params['coupon_detail_period_num'] ?? 0,
  57. 'coupon_detail_status' => 'ACTIVED',
  58. 'coupon_detail_addtimes' => time()
  59. ]);
  60. } catch (\Exception $e) {
  61. throw new BusinessException('写入优惠券失败');
  62. }
  63. }
  64. /**
  65. * @Desc 发周期优惠券 - 没有发行数量
  66. * @Author Gorden
  67. * @Date 2024/9/27 13:44
  68. *
  69. * @param $params
  70. * @return void
  71. */
  72. public static function sendPeriodCoupon($params)
  73. {
  74. Db::beginTransaction();
  75. try {
  76. $coupon = Coupon::where('coupon_id', $params['coupon_id'])->first();
  77. if ($coupon->coupon_is_period != 'Y') {
  78. return;
  79. }
  80. $detailCount = -1;
  81. if ($coupon->coupon_number > 0) {
  82. $detailCount = CouponDetail::where('join_detail_coupon_id', $params['coupon_id'])->whereIn('coupon_detail_status', ['INIT', 'PENDING'])->count();
  83. }
  84. $periodJson = json_decode($coupon->coupon_period_json, true);
  85. if ($detailCount != -1 && $detailCount - $periodJson['nbr'] < 0) {
  86. throw new BusinessException("优惠券余量不足");
  87. }
  88. $periodJson['now_nbr'] = 1;
  89. if (!empty($periodJson['nbr'])) {
  90. for ($i = 0; $i < $periodJson['nbr']; $i++) {
  91. $periodParams = self::generatePeriod($periodJson);
  92. $periodParams['gettype'] = $params['gettype'] ?? '';
  93. $periodParams['coupon_id'] = $params['coupon_id'];
  94. $periodParams['member_id'] = $params['member_id'];
  95. $periodParams['coupon_detail_period_num'] = $periodJson['now_nbr'];
  96. if ($detailCount > 0) {
  97. $periodParams['chooseCouponNbr'] = 1;
  98. self::sendPeriodCouponHave($periodParams);
  99. } else {
  100. self::sendPeriodCouponNoLimit($periodParams);
  101. }
  102. $periodJson['now_nbr'] += 1;
  103. }
  104. }
  105. Db::commit();
  106. } catch (BusinessException $e) {
  107. Db::rollBack();
  108. throw new BusinessException($e->getMessage());
  109. } catch (\Exception $e) {
  110. dump($e->getMessage());
  111. Db::rollBack();
  112. throw new BusinessException("优惠券发放失败");
  113. }
  114. }
  115. /**
  116. * @Desc 有发行数量
  117. * @Author Gorden
  118. * @Date 2024/9/27 13:45
  119. *
  120. * @param $params
  121. * @return void
  122. */
  123. public static function sendPeriodCouponHave($params)
  124. {
  125. $gettype = 'SEND';
  126. if (!empty($params['gettype'])) {
  127. $gettype = $params['gettype'];
  128. }
  129. try {
  130. CouponDetail::where('join_detail_coupon_id', $params['coupon_id'])
  131. ->whereIn('coupon_detail_status', ['INIT', 'PENDING'])
  132. ->limit($params['chooseCouponNbr'])
  133. ->update([
  134. 'join_coupon_detail_member_id' => $params['member_id'],
  135. 'coupon_detail_gain_datetime' => $params['coupon_detail_gain_datetime'],
  136. 'coupon_detail_deadline_datetime' => $params['coupon_detail_deadline_datetime'],
  137. 'coupon_detail_extend_json' => json_encode(['gettype' => $gettype]),
  138. 'coupon_detail_period_num' => $params['coupon_detail_period_num'] ?? 0,
  139. 'coupon_detail_status' => 'ACTIVED',
  140. 'coupon_detail_addtimes' => time()
  141. ]);
  142. } catch (\Exception $e) {
  143. dump($e->getMessage());
  144. throw new BusinessException('写入优惠券失败');
  145. }
  146. }
  147. /**
  148. * @Desc 发行量不限
  149. * @Author Gorden
  150. * @Date 2024/9/27 16:19
  151. *
  152. * @param $params
  153. * @return void
  154. * @throws BusinessException
  155. */
  156. public static function sendPeriodCouponNoLimit($params)
  157. {
  158. $gettype = 'SEND';
  159. if (!empty($params['gettype'])) {
  160. $gettype = $params['gettype'];
  161. }
  162. try {
  163. CouponDetail::insert([
  164. 'coupon_detail_id' => 'CUDT' . date("ymdHi") . random_string(4, 'up'),
  165. 'join_detail_coupon_id' => $params['coupon_id'],
  166. 'join_coupon_detail_member_id' => $params['member_id'],
  167. 'coupon_detail_status' => 'ACTIVED',
  168. 'coupon_detail_gain_datetime' => $params['coupon_detail_gain_datetime'],
  169. 'coupon_detail_deadline_datetime' => $params['coupon_detail_deadline_datetime'],
  170. 'coupon_detail_period_num' => $params['coupon_detail_period_num'] ?? 0,
  171. 'coupon_detail_extend_json' => json_encode(['gettype' => $gettype]),
  172. 'coupon_detail_addtimes' => time(),
  173. ]);
  174. } catch (\Exception $e) {
  175. throw new BusinessException('写入优惠券失败');
  176. }
  177. }
  178. /**
  179. * @Desc
  180. * @Author Gorden
  181. * @Date 2024/9/27 13:54
  182. *
  183. * @param $periodJson
  184. * @return array
  185. */
  186. public static function generatePeriod($periodJson)
  187. {
  188. // 当前第几期
  189. $now_nbr = $periodJson['now_nbr'];
  190. $params = [];
  191. if ($periodJson['unit'] == 'day') {
  192. $val = $periodJson['val'] - 1;
  193. if ($val < 1) {
  194. if ($now_nbr == 1) {
  195. $params['coupon_detail_gain_datetime'] = date('Y-m-d 00:00:00');
  196. $params['coupon_detail_deadline_datetime'] = date('Y-m-d 23:59:59');
  197. } else {
  198. $now_nbr -= 1;
  199. $params['coupon_detail_gain_datetime'] = date('Y-m-d 00:00:00', strtotime("+" . $now_nbr . ' day'));
  200. $params['coupon_detail_deadline_datetime'] = date('Y-m-d 23:59:59', strtotime("+" . $now_nbr . ' day'));
  201. }
  202. } else {
  203. if ($now_nbr == 1) {
  204. $params['coupon_detail_gain_datetime'] = date('Y-m-d 00:00:00');
  205. $params['coupon_detail_deadline_datetime'] = date('Y-m-d 23:59:59', strtotime("+" . $val . ' day'));
  206. } else {
  207. $now_nbr -= 1;
  208. $params['coupon_detail_gain_datetime'] = date('Y-m-d 00:00:00', strtotime("+" . ((($val + 1) * $now_nbr)) . " day"));
  209. $params['coupon_detail_deadline_datetime'] = date('Y-m-d 23:59:59', strtotime("+" . ((($val + 1) * $now_nbr) + $val) . " day"));
  210. }
  211. }
  212. } elseif ($periodJson['unit'] == 'week') {
  213. $val = $periodJson['val'] - 1;
  214. if ($val < 1) {
  215. if ($now_nbr == 1) {
  216. $params['coupon_detail_gain_datetime'] = date('Y-m-d 00:00:00', strtotime('this week Monday'));
  217. $params['coupon_detail_deadline_datetime'] = date('Y-m-d 23:59:59', strtotime('this week Sunday'));
  218. } else {
  219. $now_nbr -= 1;
  220. $params['coupon_detail_gain_datetime'] = date('Y-m-d 00:00:00', strtotime(date('Y-m-d 00:00:00', strtotime('this week Monday')) . "+" . $now_nbr . ' week'));
  221. $params['coupon_detail_deadline_datetime'] = date('Y-m-d 23:59:59', strtotime(date('Y-m-d 23:59:59', strtotime('this week Sunday')) . "+" . $now_nbr . ' week'));
  222. }
  223. } else {
  224. if ($now_nbr == 1) {
  225. $params['coupon_detail_gain_datetime'] = date('Y-m-d 00:00:00', strtotime('this week Monday'));
  226. $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'));
  227. } else {
  228. $now_nbr -= 1;
  229. $params['coupon_detail_gain_datetime'] = date('Y-m-d 00:00:00', strtotime(date('Y-m-d 00:00:00', strtotime('this week Monday')) . "+" . ((($val + 1) * $now_nbr)) . ' week'));
  230. $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 + 1) * $now_nbr) + $val) . ' week'));
  231. }
  232. }
  233. } elseif ($periodJson['unit'] == 'month') {
  234. $val = $periodJson['val'] - 1;
  235. if ($val < 1) {
  236. if ($now_nbr == 1) {
  237. $params['coupon_detail_gain_datetime'] = date('Y-m-01 00:00:00');
  238. $params['coupon_detail_deadline_datetime'] = date('Y-m-t 23:59:59');
  239. } else {
  240. $now_nbr -= 1;
  241. $params['coupon_detail_gain_datetime'] = date('Y-m-01 00:00:00', strtotime("+" . $now_nbr . ' month'));
  242. $params['coupon_detail_deadline_datetime'] = date('Y-m-t 23:59:59', strtotime(date('Y-m-01 00:00:00')."+" . $now_nbr . ' month'));
  243. }
  244. } else {
  245. if ($now_nbr == 1) {
  246. $params['coupon_detail_gain_datetime'] = date('Y-m-01 00:00:00');
  247. $params['coupon_detail_deadline_datetime'] = date('Y-m-t 23:59:59', strtotime(date('Y-m-01 00:00:00')."+" . $val . ' month'));
  248. } else {
  249. $now_nbr -= 1;
  250. $params['coupon_detail_gain_datetime'] = date('Y-m-01 00:00:00', strtotime( "+" . ((($val + 1) * $now_nbr)) . ' month'));
  251. $params['coupon_detail_deadline_datetime'] = date('Y-m-t 23:59:59', strtotime(date('Y-m-01 00:00:00')."+" . ((($val + 1) * $now_nbr) + $val) . ' month'));
  252. }
  253. }
  254. }
  255. return $params;
  256. }
  257. }