|
@@ -45,7 +45,7 @@ class CouponController extends Curd
|
|
|
if (!empty($joinCouponCategoryId)) {
|
|
|
$where['join_coupon_category_id'] = ['in', implode(',', $joinCouponCategoryId)];
|
|
|
}
|
|
|
- if (!empty($where['coupon_addtimes'])){
|
|
|
+ if (!empty($where['coupon_addtimes'])) {
|
|
|
$where['coupon_addtimes'][0] = strtotime($where['coupon_addtimes'][0]);
|
|
|
$where['coupon_addtimes'][1] = strtotime($where['coupon_addtimes'][1]);
|
|
|
}
|
|
@@ -125,7 +125,7 @@ class CouponController extends Curd
|
|
|
}
|
|
|
}
|
|
|
}
|
|
|
- }else if(!isset($couponJson['member']['where']) || empty($couponJson['member']['where'])){
|
|
|
+ } else if (!isset($couponJson['member']['where']) || empty($couponJson['member']['where'])) {
|
|
|
$item['coupon_use_member'] = 'all';
|
|
|
}
|
|
|
}
|
|
@@ -135,6 +135,13 @@ class CouponController extends Curd
|
|
|
$item['premises_range'] = 'all';
|
|
|
}
|
|
|
}
|
|
|
+ if (!empty($item['coupon_period_json'])) {
|
|
|
+ $periodJson = json_decode($item['coupon_period_json'], true);
|
|
|
+ $item['period'] = $periodJson['length'] ?? '';
|
|
|
+ $item['period_unit'] = $periodJson['unit'] ?? '';
|
|
|
+ $item['period_unit_num'] = $periodJson['val'] ?? '';
|
|
|
+ $item['period_nbr'] = $periodJson['nbr'] ?? '';
|
|
|
+ }
|
|
|
if (!empty($item['coupon_validdate_begin']) && !empty($item['coupon_validdate_end'])) {
|
|
|
$item['coupon_use_time'] = 'date';
|
|
|
} elseif ($item['coupon_validdate_day']) {
|
|
@@ -155,7 +162,7 @@ class CouponController extends Curd
|
|
|
}
|
|
|
}
|
|
|
}
|
|
|
- $item['coupon_explain'] = CouponService::couponClassifyInfo($item['coupon_classify'],$item['coupon_category'], $item['coupon_value'], $item['coupon_minimum_limit']);
|
|
|
+ $item['coupon_explain'] = CouponService::couponClassifyInfo($item['coupon_classify'], $item['coupon_category'], $item['coupon_value'], $item['coupon_minimum_limit']);
|
|
|
unset($item['detail']);
|
|
|
}
|
|
|
|
|
@@ -298,9 +305,57 @@ class CouponController extends Curd
|
|
|
|
|
|
$data['coupon_json'] = json_encode($couponJson, JSON_UNESCAPED_UNICODE);
|
|
|
|
|
|
+ // 处理周期优惠券
|
|
|
+ if ($params['coupon_is_period'] != 'N') {
|
|
|
+ $this->disposePeriod($params);
|
|
|
+ $data['coupon_period_json'] = json_encode([
|
|
|
+ 'length' => $params['period'],
|
|
|
+ 'unit' => $params['period_unit'],
|
|
|
+ 'val' => $params['period_unit_num'],
|
|
|
+ 'nbr' => $params['period_nbr']
|
|
|
+ ]);
|
|
|
+ }
|
|
|
+
|
|
|
return $data;
|
|
|
}
|
|
|
|
|
|
+ public function disposePeriod($params)
|
|
|
+ {
|
|
|
+ if (empty($params['period']) || empty($params['period_unit']) || empty($params['period_unit_num']) || empty($params['period_nbr'])) {
|
|
|
+ throw new BusinessException("周期数据异常");
|
|
|
+ }
|
|
|
+
|
|
|
+ if ($params['period'] != 'TO_EXPIRE') {
|
|
|
+ return $params;
|
|
|
+ }
|
|
|
+ if (empty($params['coupon_validdate_end'])) {
|
|
|
+ throw new BusinessException("周期数据异常");
|
|
|
+ }
|
|
|
+ try {
|
|
|
+ $endTimeUnix = strtotime($params['coupon_validdate_end']);
|
|
|
+ if ($params['period_unit'] == 'day') {
|
|
|
+ $diff = ceil(($endTimeUnix - time()) / (3600 * 24));
|
|
|
+ } else if ($params['period_unit'] == 'week') {
|
|
|
+ $diff = ceil(($endTimeUnix - time()) / (3600 * 24));
|
|
|
+ $diff = ceil($diff / 7);
|
|
|
+ } else if ($params['period_unit'] == 'month') {
|
|
|
+ $endTime = new \DateTime($params['coupon_validdate_end']);
|
|
|
+ $nowTime = new \DateTime(date('Y-m-d'));
|
|
|
+ $interval = $nowTime->diff($endTime);
|
|
|
+ $diff = $interval->y * 12 + $interval->m + 1;
|
|
|
+ }
|
|
|
+
|
|
|
+ if ($params['period_nbr'] != ceil($diff / $params['period_unit_num'])) {
|
|
|
+ throw new BusinessException("期数设置错误");
|
|
|
+ }
|
|
|
+
|
|
|
+ return $params;
|
|
|
+ } catch (\Exception $e) {
|
|
|
+
|
|
|
+ throw new BusinessException("周期数据异常");
|
|
|
+ }
|
|
|
+ }
|
|
|
+
|
|
|
public function customSend(Request $request)
|
|
|
{
|
|
|
$chooseCoupons = $request->post('chooseCoupons', []);
|
|
@@ -334,20 +389,49 @@ class CouponController extends Curd
|
|
|
$params['coupon_detail_deadline_datetime'] = $coupon['coupon_validdate_end'];
|
|
|
} elseif ($coupon['coupon_validdate_day'] > 0) {
|
|
|
$endUnix = strtotime($chooseCoupon['gain_datetime']);
|
|
|
- $params['coupon_detail_deadline_datetime'] = date('Y-m-d H:i:s', $endUnix + ($coupon['coupon_validdate_day'] * 24 * 3600) -1);
|
|
|
+ $params['coupon_detail_deadline_datetime'] = date('Y-m-d H:i:s', $endUnix + ($coupon['coupon_validdate_day'] * 24 * 3600) - 1);
|
|
|
}
|
|
|
foreach ($memberList as $item) {
|
|
|
$params['member_id'] = $item;
|
|
|
+ if ($coupon['coupon_is_period'] == 'Y') {
|
|
|
+ if (CouponDetail::where('join_coupon_detail_member_id', $item)->where('join_detail_coupon_id', $coupon['coupon_id'])->exists()) {
|
|
|
+ throw new BusinessException("请勿重复发放周期券");
|
|
|
+ }
|
|
|
+ $params['coupon_detail_period_num'] = 1;
|
|
|
+ $periodJson = json_decode($coupon['coupon_period_json'], true);
|
|
|
+ if ($periodJson['unit'] == 'day') {
|
|
|
+ $val = $periodJson['val'] - 1;
|
|
|
+ if ($val < 1) {
|
|
|
+ $params['coupon_detail_deadline_datetime'] = date('Y-m-d 23:59:59');
|
|
|
+ }else{
|
|
|
+ $params['coupon_detail_deadline_datetime'] = date('Y-m-t 23:59:59',strtotime("+".$val." day"));
|
|
|
+ }
|
|
|
+ } elseif ($periodJson['unit'] == 'week') {
|
|
|
+ $val = $periodJson['val'] - 1;
|
|
|
+ if ($val < 1){
|
|
|
+ $params['coupon_detail_deadline_datetime'] = date('Y-m-d 23:59:59', strtotime('this week Sunday'));
|
|
|
+ }else{
|
|
|
+ $params['coupon_detail_deadline_datetime'] = date('Y-m-t 23:59:59',strtotime("+".$val.' week',date('Y-m-d',strtotime("+".$val." month"))));
|
|
|
+ }
|
|
|
+ } elseif ($periodJson['unit'] == 'month'){
|
|
|
+ $val = $periodJson['val'] - 1;
|
|
|
+ if ($val < 1){
|
|
|
+ $params['coupon_detail_deadline_datetime'] = date('Y-m-t 23:59:59');
|
|
|
+ }else{
|
|
|
+ $params['coupon_detail_deadline_datetime'] = date('Y-m-t 23:59:59',strtotime("+".$val." month"));
|
|
|
+ }
|
|
|
+ }
|
|
|
+ }
|
|
|
if ($chooseCoupon['nbr'] > 0) {
|
|
|
// 有发行数量
|
|
|
- if ($coupon['coupon_number'] != 0){
|
|
|
+ if ($coupon['coupon_number'] != 0) {
|
|
|
// 查询共发行了多少张了
|
|
|
- $count = CouponDetail::where('join_detail_coupon_id',$coupon['coupon_id'])->count();
|
|
|
- if (!isset($couponNbr[$coupon['coupon_id']])){
|
|
|
+ $count = CouponDetail::where('join_detail_coupon_id', $coupon['coupon_id'])->count();
|
|
|
+ if (!isset($couponNbr[$coupon['coupon_id']])) {
|
|
|
$couponNbr[$coupon['coupon_id']] = $count;
|
|
|
}
|
|
|
- if ($couponNbr[$coupon['coupon_id']] + $chooseCoupon['nbr'] > $coupon['coupon_number']){
|
|
|
- throw new BusinessException($coupon['coupon_name']."超出发行数量");
|
|
|
+ if ($couponNbr[$coupon['coupon_id']] + $chooseCoupon['nbr'] > $coupon['coupon_number']) {
|
|
|
+ throw new BusinessException($coupon['coupon_name'] . "超出发行数量");
|
|
|
}
|
|
|
$couponNbr[$coupon['coupon_id']] = $couponNbr[$coupon['coupon_id']] + $chooseCoupon['nbr'];
|
|
|
}
|
|
@@ -593,6 +677,18 @@ class CouponController extends Curd
|
|
|
|
|
|
$data['coupon_json'] = json_encode($couponJson, JSON_UNESCAPED_UNICODE);
|
|
|
|
|
|
+ $data['coupon_period_json'] = '[]';
|
|
|
+ // 处理周期优惠券
|
|
|
+ if ($params['coupon_is_period'] != 'N') {
|
|
|
+ $this->disposePeriod($params);
|
|
|
+ $data['coupon_period_json'] = json_encode([
|
|
|
+ 'length' => $params['period'],
|
|
|
+ 'unit' => $params['period_unit'],
|
|
|
+ 'val' => $params['period_unit_num'],
|
|
|
+ 'nbr' => $params['period_nbr']
|
|
|
+ ]);
|
|
|
+ }
|
|
|
+
|
|
|
unset($data[$primary_key]);
|
|
|
return [$id, $data];
|
|
|
}
|