CouponDetailService.php 13 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289
  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. // 手动发券会传来时间
  91. $periodJson['gain_datetime'] = $params['coupon_detail_gain_datetime'] ?? date('Y-m-d H:i:s');
  92. if (!empty($periodJson['nbr'])) {
  93. for ($i = 0; $i < $periodJson['nbr']; $i++) {
  94. $periodParams = self::generatePeriod($periodJson);
  95. $periodParams['gettype'] = $params['gettype'] ?? '';
  96. $periodParams['coupon_id'] = $params['coupon_id'];
  97. $periodParams['member_id'] = $params['member_id'];
  98. $periodParams['coupon_detail_period_num'] = $periodJson['now_nbr'];
  99. if ($detailCount > 0) {
  100. $periodParams['chooseCouponNbr'] = 1;
  101. self::sendPeriodCouponHave($periodParams);
  102. } else {
  103. self::sendPeriodCouponNoLimit($periodParams);
  104. }
  105. $periodJson['now_nbr'] += 1;
  106. }
  107. }
  108. Db::commit();
  109. } catch (BusinessException $e) {
  110. Db::rollBack();
  111. Log::error("发券失败:" . $e->getMessage());
  112. throw new BusinessException($e->getMessage());
  113. } catch (\Exception $e) {
  114. Db::rollBack();
  115. Log::error("发券失败:" . $e->getMessage());
  116. throw new BusinessException("优惠券发放失败");
  117. }
  118. }
  119. /**
  120. * @Desc 有发行数量
  121. * @Author Gorden
  122. * @Date 2024/9/27 13:45
  123. *
  124. * @param $params
  125. * @return void
  126. */
  127. public static function sendPeriodCouponHave($params)
  128. {
  129. $gettype = 'SEND';
  130. if (!empty($params['gettype'])) {
  131. $gettype = $params['gettype'];
  132. }
  133. try {
  134. CouponDetail::where('join_detail_coupon_id', $params['coupon_id'])
  135. ->whereIn('coupon_detail_status', ['INIT', 'PENDING'])
  136. ->limit($params['chooseCouponNbr'])
  137. ->update([
  138. 'join_coupon_detail_member_id' => $params['member_id'],
  139. 'coupon_detail_gain_datetime' => $params['coupon_detail_gain_datetime'],
  140. 'coupon_detail_deadline_datetime' => $params['coupon_detail_deadline_datetime'],
  141. 'coupon_detail_extend_json' => json_encode(['gettype' => $gettype]),
  142. 'coupon_detail_period_num' => $params['coupon_detail_period_num'] ?? 0,
  143. 'coupon_detail_status' => 'ACTIVED',
  144. 'coupon_detail_addtimes' => time()
  145. ]);
  146. } catch (\Exception $e) {
  147. dump($e->getMessage());
  148. throw new BusinessException('写入优惠券失败');
  149. }
  150. }
  151. /**
  152. * @Desc 发行量不限
  153. * @Author Gorden
  154. * @Date 2024/9/27 16:19
  155. *
  156. * @param $params
  157. * @return void
  158. * @throws BusinessException
  159. */
  160. public static function sendPeriodCouponNoLimit($params)
  161. {
  162. $gettype = 'SEND';
  163. if (!empty($params['gettype'])) {
  164. $gettype = $params['gettype'];
  165. }
  166. try {
  167. CouponDetail::insert([
  168. 'coupon_detail_id' => 'CUDT' . date("ymdHi") . random_string(4, 'up'),
  169. 'join_detail_coupon_id' => $params['coupon_id'],
  170. 'join_coupon_detail_member_id' => $params['member_id'],
  171. 'coupon_detail_status' => 'ACTIVED',
  172. 'coupon_detail_gain_datetime' => $params['coupon_detail_gain_datetime'],
  173. 'coupon_detail_deadline_datetime' => $params['coupon_detail_deadline_datetime'],
  174. 'coupon_detail_period_num' => $params['coupon_detail_period_num'] ?? 0,
  175. 'coupon_detail_extend_json' => json_encode(['gettype' => $gettype]),
  176. 'coupon_detail_addtimes' => time(),
  177. ]);
  178. } catch (\Exception $e) {
  179. throw new BusinessException('写入优惠券失败');
  180. }
  181. }
  182. /**
  183. * @Desc
  184. * @Author Gorden
  185. * @Date 2024/9/27 13:54
  186. *
  187. * @param $periodJson
  188. * @return array
  189. */
  190. public static function generatePeriod($periodJson)
  191. {
  192. // 当前第几期
  193. $now_nbr = $periodJson['now_nbr'];
  194. $params = [];
  195. if ($periodJson['unit'] == 'day') {
  196. $val = $periodJson['val'] - 1;
  197. if ($val < 1) {
  198. if ($now_nbr == 1) {
  199. $params['coupon_detail_gain_datetime'] = date('Y-m-d 00:00:00', strtotime($periodJson['gain_datetime']));
  200. $params['coupon_detail_deadline_datetime'] = date('Y-m-d 23:59:59', strtotime($periodJson['gain_datetime']));
  201. } else {
  202. $now_nbr -= 1;
  203. $params['coupon_detail_gain_datetime'] = date('Y-m-d 00:00:00', strtotime($periodJson['gain_datetime'] . "+" . $now_nbr . ' day'));
  204. $params['coupon_detail_deadline_datetime'] = date('Y-m-d 23:59:59', strtotime($periodJson['gain_datetime'] . "+" . $now_nbr . ' day'));
  205. }
  206. } else {
  207. if ($now_nbr == 1) {
  208. $params['coupon_detail_gain_datetime'] = date('Y-m-d 00:00:00', strtotime($periodJson['gain_datetime']));
  209. $params['coupon_detail_deadline_datetime'] = date('Y-m-d 23:59:59', strtotime($periodJson['gain_datetime'] . "+" . $val . ' day'));
  210. } else {
  211. $now_nbr -= 1;
  212. $params['coupon_detail_gain_datetime'] = date('Y-m-d 00:00:00', strtotime($periodJson['gain_datetime'] . "+" . ((($val + 1) * $now_nbr)) . " day"));
  213. $params['coupon_detail_deadline_datetime'] = date('Y-m-d 23:59:59', strtotime($periodJson['gain_datetime'] . "+" . ((($val + 1) * $now_nbr) + $val) . " day"));
  214. }
  215. }
  216. } elseif ($periodJson['unit'] == 'week') {
  217. $val = $periodJson['val'] - 1;
  218. // 选的日期的周一距离今周周一是几个周
  219. $timestamp = strtotime($periodJson['gain_datetime']);
  220. $gainWeekDay = date("w", $timestamp);
  221. $gainWeekAdd = date("w", $gainWeekDay) == 1 ? 0 : 1 - $gainWeekDay;
  222. $gainMonday = date("Y-m-d 00:00:00", strtotime("$gainWeekAdd days", $timestamp));
  223. $nowMonday = date('Y-m-d 00:00:00', strtotime('this week Monday'));
  224. $interval = (new \DateTime(date($gainMonday)))->diff(new \DateTime($nowMonday));
  225. $weekCut = $interval->days / 7;
  226. if (strtotime($periodJson['gain_datetime']) < time() && $weekCut > 0) {
  227. $weekCut = -$weekCut;
  228. }
  229. if ($val < 1) {
  230. if ($now_nbr == 1) {
  231. $params['coupon_detail_gain_datetime'] = $gainMonday;
  232. $params['coupon_detail_deadline_datetime'] = date('Y-m-d 23:59:59', strtotime($gainMonday . "+6 days"));
  233. } else {
  234. $now_nbr -= 1;
  235. $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 + $weekCut) . ' week'));
  236. $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 + $weekCut) . ' week'));
  237. }
  238. } else {
  239. if ($now_nbr == 1) {
  240. $params['coupon_detail_gain_datetime'] = $gainMonday;
  241. $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 + $weekCut) . ' week'));
  242. } else {
  243. $now_nbr -= 1;
  244. $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) + $weekCut) . ' week'));
  245. $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 + $weekCut) . ' week'));
  246. }
  247. }
  248. } elseif ($periodJson['unit'] == 'month') {
  249. $val = $periodJson['val'] - 1;
  250. // 选的日期的1号距离今月1号是几个月
  251. $interval = (new \DateTime(date('Y-m-02 00:00:00', strtotime($periodJson['gain_datetime']))))->diff(new \DateTime(date('Y-m-02 00:00:00')));
  252. $monthCut = $interval->m + ($interval->y * 12);
  253. if (strtotime($periodJson['gain_datetime']) < time() && $monthCut > 0) {
  254. $monthCut = -$monthCut;
  255. }
  256. if ($val < 1) {
  257. if ($now_nbr == 1) {
  258. $params['coupon_detail_gain_datetime'] = date('Y-m-01 00:00:00', strtotime($periodJson['gain_datetime']));
  259. $params['coupon_detail_deadline_datetime'] = date('Y-m-t 23:59:59', strtotime($periodJson['gain_datetime']));
  260. } else {
  261. $now_nbr -= 1;
  262. $params['coupon_detail_gain_datetime'] = date('Y-m-01 00:00:00', strtotime("+" . ($now_nbr + $monthCut) . ' month'));
  263. $params['coupon_detail_deadline_datetime'] = date('Y-m-t 23:59:59', strtotime(date('Y-m-01 00:00:00') . "+" . ($now_nbr + $monthCut) . ' month'));
  264. }
  265. } else {
  266. if ($now_nbr == 1) {
  267. $params['coupon_detail_gain_datetime'] = date('Y-m-01 00:00:00', strtotime($periodJson['gain_datetime']));
  268. $params['coupon_detail_deadline_datetime'] = date('Y-m-t 23:59:59', strtotime(date('Y-m-01 00:00:00') . "+" . ($val + $monthCut) . ' month'));
  269. } else {
  270. $now_nbr -= 1;
  271. $params['coupon_detail_gain_datetime'] = date('Y-m-01 00:00:00', strtotime("+" . ((($val + 1) * $now_nbr) + $monthCut) . ' month'));
  272. $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 + $monthCut) . ' month'));
  273. }
  274. }
  275. }
  276. return $params;
  277. }
  278. }