|
@@ -5,6 +5,7 @@ namespace app\admin\service\coupon;
|
|
|
use app\model\Coupon;
|
|
|
use app\model\CouponDetail;
|
|
|
use app\model\SysSerial;
|
|
|
+use support\Db;
|
|
|
use support\exception\BusinessException;
|
|
|
|
|
|
class CouponDetailService
|
|
@@ -75,15 +76,46 @@ class CouponDetailService
|
|
|
*/
|
|
|
public static function sendPeriodCoupon($params)
|
|
|
{
|
|
|
- $coupon = Coupon::where('coupon_id', $params['coupon_id'])->first();
|
|
|
- if ($coupon->coupon_is_period != 'Y') {
|
|
|
- return;
|
|
|
- }
|
|
|
- $periodJson = json_decode($coupon->coupon_period_json, true);
|
|
|
- if (!empty($periodJson['nbr'])) {
|
|
|
- for ($i = 1; $i < $periodJson['nbr']; $i++) {
|
|
|
- self::generatePeriod($periodJson);
|
|
|
+ Db::beginTransaction();
|
|
|
+ try {
|
|
|
+ $coupon = Coupon::where('coupon_id', $params['coupon_id'])->first();
|
|
|
+ if ($coupon->coupon_is_period != 'Y') {
|
|
|
+ return;
|
|
|
+ }
|
|
|
+ $detailCount = -1;
|
|
|
+ if ($coupon->coupon_number > 0) {
|
|
|
+ $detailCount = CouponDetail::where('join_detail_coupon_id', $params['coupon_id'])->whereIn('coupon_detail_status', ['INIT', 'PENDING'])->count();
|
|
|
+ }
|
|
|
+ $periodJson = json_decode($coupon->coupon_period_json, true);
|
|
|
+ if ($detailCount != -1 && $detailCount - $periodJson['nbr'] < 0) {
|
|
|
+ throw new BusinessException("优惠券余量不足");
|
|
|
}
|
|
|
+ $periodJson['now_nbr'] = 1;
|
|
|
+ if (!empty($periodJson['nbr'])) {
|
|
|
+ for ($i = 0; $i < $periodJson['nbr']; $i++) {
|
|
|
+ $periodParams = self::generatePeriod($periodJson);
|
|
|
+ $periodParams['gettype'] = $params['gettype'] ?? '';
|
|
|
+ $periodParams['coupon_id'] = $params['coupon_id'];
|
|
|
+ $periodParams['member_id'] = $params['member_id'];
|
|
|
+ $periodParams['coupon_detail_period_num'] = $periodJson['now_nbr'];
|
|
|
+ if ($detailCount > 0) {
|
|
|
+ $periodParams['chooseCouponNbr'] = 1;
|
|
|
+ self::sendPeriodCouponHave($periodParams);
|
|
|
+ } else {
|
|
|
+ self::sendPeriodCouponNoLimit($periodParams);
|
|
|
+ }
|
|
|
+
|
|
|
+ $periodJson['now_nbr'] += 1;
|
|
|
+ }
|
|
|
+ }
|
|
|
+ Db::commit();
|
|
|
+ } catch (BusinessException $e) {
|
|
|
+ Db::rollBack();
|
|
|
+ throw new BusinessException($e->getMessage());
|
|
|
+ } catch (\Exception $e) {
|
|
|
+ dump($e->getMessage());
|
|
|
+ Db::rollBack();
|
|
|
+ throw new BusinessException("优惠券发放失败");
|
|
|
}
|
|
|
}
|
|
|
|
|
@@ -97,7 +129,59 @@ class CouponDetailService
|
|
|
*/
|
|
|
public static function sendPeriodCouponHave($params)
|
|
|
{
|
|
|
+ $gettype = 'SEND';
|
|
|
+ if (!empty($params['gettype'])) {
|
|
|
+ $gettype = $params['gettype'];
|
|
|
+ }
|
|
|
+ try {
|
|
|
+ CouponDetail::where('join_detail_coupon_id', $params['coupon_id'])
|
|
|
+ ->whereIn('coupon_detail_status', ['INIT', 'PENDING'])
|
|
|
+ ->limit($params['chooseCouponNbr'])
|
|
|
+ ->update([
|
|
|
+ 'join_coupon_detail_member_id' => $params['member_id'],
|
|
|
+ 'coupon_detail_gain_datetime' => $params['coupon_detail_gain_datetime'],
|
|
|
+ 'coupon_detail_deadline_datetime' => $params['coupon_detail_deadline_datetime'],
|
|
|
+ 'coupon_detail_extend_json' => json_encode(['gettype' => $gettype]),
|
|
|
+ 'coupon_detail_period_num' => $params['coupon_detail_period_num'] ?? 0,
|
|
|
+ 'coupon_detail_status' => 'ACTIVED',
|
|
|
+ 'coupon_detail_addtimes' => time()
|
|
|
+ ]);
|
|
|
+ } catch (\Exception $e) {
|
|
|
+ dump($e->getMessage());
|
|
|
+ throw new BusinessException('写入优惠券失败');
|
|
|
+ }
|
|
|
+ }
|
|
|
|
|
|
+ /**
|
|
|
+ * @Desc 发行量不限
|
|
|
+ * @Author Gorden
|
|
|
+ * @Date 2024/9/27 16:19
|
|
|
+ *
|
|
|
+ * @param $params
|
|
|
+ * @return void
|
|
|
+ * @throws BusinessException
|
|
|
+ */
|
|
|
+ public static function sendPeriodCouponNoLimit($params)
|
|
|
+ {
|
|
|
+ $gettype = 'SEND';
|
|
|
+ if (!empty($params['gettype'])) {
|
|
|
+ $gettype = $params['gettype'];
|
|
|
+ }
|
|
|
+ try {
|
|
|
+ CouponDetail::insert([
|
|
|
+ 'coupon_detail_id' => 'CUDT' . date("ymdHi") . random_string(4, 'up'),
|
|
|
+ 'join_detail_coupon_id' => $params['coupon_id'],
|
|
|
+ 'join_coupon_detail_member_id' => $params['member_id'],
|
|
|
+ 'coupon_detail_status' => 'ACTIVED',
|
|
|
+ 'coupon_detail_gain_datetime' => $params['coupon_detail_gain_datetime'],
|
|
|
+ 'coupon_detail_deadline_datetime' => $params['coupon_detail_deadline_datetime'],
|
|
|
+ 'coupon_detail_period_num' => $params['coupon_detail_period_num'] ?? 0,
|
|
|
+ 'coupon_detail_extend_json' => json_encode(['gettype' => $gettype]),
|
|
|
+ 'coupon_detail_addtimes' => time(),
|
|
|
+ ]);
|
|
|
+ } catch (\Exception $e) {
|
|
|
+ throw new BusinessException('写入优惠券失败');
|
|
|
+ }
|
|
|
}
|
|
|
|
|
|
/**
|
|
@@ -105,36 +189,76 @@ class CouponDetailService
|
|
|
* @Author Gorden
|
|
|
* @Date 2024/9/27 13:54
|
|
|
*
|
|
|
- * @param $periodJson 周期JSON
|
|
|
+ * @param $periodJson
|
|
|
* @return array
|
|
|
*/
|
|
|
public static function generatePeriod($periodJson)
|
|
|
{
|
|
|
+ // 当前第几期
|
|
|
+ $now_nbr = $periodJson['now_nbr'];
|
|
|
$params = [];
|
|
|
if ($periodJson['unit'] == 'day') {
|
|
|
$val = $periodJson['val'] - 1;
|
|
|
if ($val < 1) {
|
|
|
- $params['coupon_detail_gain_datetime'] = date('Y-m-d 00:00:00');
|
|
|
- $params['coupon_detail_deadline_datetime'] = date('Y-m-d 23:59:59');
|
|
|
+ if ($now_nbr == 1) {
|
|
|
+ $params['coupon_detail_gain_datetime'] = date('Y-m-d 00:00:00');
|
|
|
+ $params['coupon_detail_deadline_datetime'] = date('Y-m-d 23:59:59');
|
|
|
+ } else {
|
|
|
+ $now_nbr -= 1;
|
|
|
+ $params['coupon_detail_gain_datetime'] = date('Y-m-d 00:00:00', strtotime("+" . $now_nbr . ' day'));
|
|
|
+ $params['coupon_detail_deadline_datetime'] = date('Y-m-d 23:59:59', strtotime("+" . $now_nbr . ' day'));
|
|
|
+ }
|
|
|
} else {
|
|
|
- $params['coupon_detail_gain_datetime'] = date('Y-m-d 23:59:59', strtotime("+" . $val . " day"));
|
|
|
- $params['coupon_detail_deadline_datetime'] = date('Y-m-d 23:59:59', strtotime("+" . $val . " day"));
|
|
|
+ if ($now_nbr == 1) {
|
|
|
+ $params['coupon_detail_gain_datetime'] = date('Y-m-d 00:00:00');
|
|
|
+ $params['coupon_detail_deadline_datetime'] = date('Y-m-d 23:59:59', strtotime("+" . $val . ' day'));
|
|
|
+ } else {
|
|
|
+ $now_nbr -= 1;
|
|
|
+ $params['coupon_detail_gain_datetime'] = date('Y-m-d 00:00:00', strtotime("+" . ((($val + 1) * $now_nbr)) . " day"));
|
|
|
+ $params['coupon_detail_deadline_datetime'] = date('Y-m-d 23:59:59', strtotime("+" . ((($val + 1) * $now_nbr) + $val) . " day"));
|
|
|
+ }
|
|
|
}
|
|
|
} elseif ($periodJson['unit'] == 'week') {
|
|
|
$val = $periodJson['val'] - 1;
|
|
|
if ($val < 1) {
|
|
|
- $params['coupon_detail_gain_datetime'] = date('Y-m-d 00:00:00', strtotime('this week Monday'));
|
|
|
- $params['coupon_detail_deadline_datetime'] = date('Y-m-d 23:59:59', strtotime('this week Sunday'));
|
|
|
+ if ($now_nbr == 1) {
|
|
|
+ $params['coupon_detail_gain_datetime'] = date('Y-m-d 00:00:00', strtotime('this week Monday'));
|
|
|
+ $params['coupon_detail_deadline_datetime'] = date('Y-m-d 23:59:59', strtotime('this week Sunday'));
|
|
|
+ } else {
|
|
|
+ $now_nbr -= 1;
|
|
|
+ $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'));
|
|
|
+ $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'));
|
|
|
+ }
|
|
|
} else {
|
|
|
- $params['coupon_detail_gain_datetime'] = date('Y-m-d 00:00:00', strtotime('this week Monday'));
|
|
|
- $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'));
|
|
|
+ if ($now_nbr == 1) {
|
|
|
+ $params['coupon_detail_gain_datetime'] = date('Y-m-d 00:00:00', strtotime('this week Monday'));
|
|
|
+ $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'));
|
|
|
+ } else {
|
|
|
+ $now_nbr -= 1;
|
|
|
+ $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'));
|
|
|
+ $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'));
|
|
|
+ }
|
|
|
}
|
|
|
} elseif ($periodJson['unit'] == 'month') {
|
|
|
$val = $periodJson['val'] - 1;
|
|
|
if ($val < 1) {
|
|
|
- $params['coupon_detail_deadline_datetime'] = date('Y-m-t 23:59:59');
|
|
|
+ if ($now_nbr == 1) {
|
|
|
+ $params['coupon_detail_gain_datetime'] = date('Y-m-01 00:00:00');
|
|
|
+ $params['coupon_detail_deadline_datetime'] = date('Y-m-t 23:59:59');
|
|
|
+ } else {
|
|
|
+ $now_nbr -= 1;
|
|
|
+ $params['coupon_detail_gain_datetime'] = date('Y-m-01 00:00:00', strtotime("+" . $now_nbr . ' month'));
|
|
|
+ $params['coupon_detail_deadline_datetime'] = date('Y-m-t 23:59:59', strtotime("+" . $now_nbr . ' month'));
|
|
|
+ }
|
|
|
} else {
|
|
|
- $params['coupon_detail_deadline_datetime'] = date('Y-m-t 23:59:59', strtotime("+" . $val . " month"));
|
|
|
+ if ($now_nbr == 1) {
|
|
|
+ $params['coupon_detail_gain_datetime'] = date('Y-m-01 00:00:00');
|
|
|
+ $params['coupon_detail_deadline_datetime'] = date('Y-m-t 23:59:59', strtotime("+" . $val . ' month'));
|
|
|
+ } else {
|
|
|
+ $now_nbr -= 1;
|
|
|
+ $params['coupon_detail_gain_datetime'] = date('Y-m-01 00:00:00', strtotime( "+" . ((($val + 1) * $now_nbr)) . ' month'));
|
|
|
+ $params['coupon_detail_deadline_datetime'] = date('Y-m-t 23:59:59', strtotime("+" . ((($val + 1) * $now_nbr) + $val) . ' month'));
|
|
|
+ }
|
|
|
}
|
|
|
}
|
|
|
|