CouponDetailService.php 12 KB

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