|
@@ -39,31 +39,43 @@ class CouponService
|
|
|
Db::beginTransaction();
|
|
|
try {
|
|
|
$coupons = Coupon::where('coupon_is_period', 'Y')->where('coupon_status', 'ACTIVED')->get()->toArray();
|
|
|
+// dump("周期全",$coupons);
|
|
|
foreach ($coupons as $coupon) {
|
|
|
- if (time() > strtotime($coupon['coupon_validdate_end'])) {
|
|
|
+ if (!empty($coupon['coupon_validdate_end']) && time() > strtotime($coupon['coupon_validdate_end'])) {
|
|
|
continue;
|
|
|
}
|
|
|
- $details = CouponDetail::where('join_detail_coupon_id',$coupon['coupon_id'])
|
|
|
- ->select('join_coupon_detail_member_id')
|
|
|
+ $details = CouponDetail::where('join_detail_coupon_id', $coupon['coupon_id'])
|
|
|
+ ->where('join_coupon_detail_member_id','<>','')
|
|
|
+ ->selectRaw('join_coupon_detail_member_id,COUNT(*) as count')
|
|
|
->groupBy('join_coupon_detail_member_id')
|
|
|
->get();
|
|
|
foreach ($details as $item) {
|
|
|
$detail = CouponDetail::where('join_detail_coupon_id', $coupon['coupon_id'])
|
|
|
- ->where('join_coupon_detail_member_id',$item->join_coupon_detail_member_id)
|
|
|
+ ->where('join_coupon_detail_member_id', $item->join_coupon_detail_member_id)
|
|
|
->orderBy('coupon_detail_addtimes', 'DESC')
|
|
|
->first();
|
|
|
- if (!empty($detail) && strtotime($detail->coupon_detail_deadline_datetime) > time()) {
|
|
|
+ if (strtotime($detail->coupon_detail_deadline_datetime) > time()) {
|
|
|
continue;
|
|
|
+ } elseif (empty($coupon['coupon_validdate_end']) && !empty($coupon['coupon_validdate_day'])) {
|
|
|
+ $firstDetail = CouponDetail::where('join_detail_coupon_id', $coupon['coupon_id'])
|
|
|
+ ->where('join_coupon_detail_member_id', $item->join_coupon_detail_member_id)
|
|
|
+ ->orderBy('coupon_detail_addtimes', 'ASC')
|
|
|
+ ->first();
|
|
|
+ if ((time() - strtotime($firstDetail->coupon_detail_addtimes)) > (3600 * 24 * $coupon['coupon_validdate_day'])) {
|
|
|
+ continue;
|
|
|
+ }
|
|
|
}
|
|
|
+
|
|
|
$params = [
|
|
|
'coupon_detail_gain_datetime' => date('Y-m-d 00:00:00'),
|
|
|
'coupon_id' => $coupon['coupon_id'],
|
|
|
'member_id' => $detail->join_coupon_detail_member_id,
|
|
|
'coupon_detail_period_num' => $detail->coupon_detail_period_num + 1
|
|
|
];
|
|
|
+ // 超出期数
|
|
|
if (!empty($coupon['coupon_period_json'])) {
|
|
|
$periodJson = json_decode($coupon['coupon_period_json'], true);
|
|
|
- if ($periodJson['nbr'] < $params['coupon_detail_period_num']){
|
|
|
+ if ($periodJson['nbr'] < $params['coupon_detail_period_num']) {
|
|
|
continue;
|
|
|
}
|
|
|
if ($periodJson['unit'] == 'day') {
|
|
@@ -89,8 +101,14 @@ class CouponService
|
|
|
}
|
|
|
}
|
|
|
}
|
|
|
- // 写入优惠券
|
|
|
- CouponDetailService::customSendCoupon($params);
|
|
|
+ if (CouponDetail::whereIn('coupon_detail_status',['INIT','PENDING'])->exists()){
|
|
|
+ // 匹配已发行的优惠券
|
|
|
+ $params['chooseCouponNbr'] = 1;
|
|
|
+ CouponDetailService::customSendCouponHave($params);
|
|
|
+ }else{
|
|
|
+ // 写入优惠券
|
|
|
+ CouponDetailService::customSendCoupon($params);
|
|
|
+ }
|
|
|
|
|
|
_syslog("周期券", "发放周期券", false, $params, 1001);
|
|
|
}
|
|
@@ -98,6 +116,7 @@ class CouponService
|
|
|
|
|
|
Db::commit();
|
|
|
} catch (\Exception $e) {
|
|
|
+ dump($e->getTrace());
|
|
|
Db::rollBack();
|
|
|
}
|
|
|
}
|