couponDispenseLogModel->getInfoById($this->couponDispenseId); if (empty($dispenseInfo)) throw new Exception('分发记录不存在'); $this->log->debug(json_encode($dispenseInfo->toArray())); $templateInfo = $this->couponTemplateModel->getInfoById($dispenseInfo->coupon_template_id); if (empty($templateInfo)) throw new Exception('优惠券模板不存在'); $this->log->debug(json_encode($templateInfo->toArray())); $validityTime = $this->getValidityTime((int)$dispenseInfo->validity_time_type, $dispenseInfo->validity_time_value); //有效期不存在或者到期销毁当前分发逻辑 if (empty($validityTime) || empty($validityTime['start_time']) || empty($validityTime['end_time']) || $validityTime['end_time'] < date('Y-m-d H:i:s')) { $dispenseInfo->status = CouponCode::DISPENSE_NULL; if (!$dispenseInfo->save()) throw new Exception('修改分发记录状态失败'); throw new Exception('该分发逻辑已失效'); } $this->log->debug(json_encode($validityTime)); $userDispenseInfo = $this->couponDispenseUserModel->getInfoByDispenseIdAndUserId($this->couponDispenseId,$this->userId); if (empty($userDispenseInfo)) throw new Exception('该用户不属于该分发逻辑'); $this->log->debug(json_encode($userDispenseInfo)); $userCount = $this->userCouponModel->where('coupon_dispense_id',$this->couponDispenseId)->where('user_id',$this->userId)->count() ?? 0; if ($userCount >= $dispenseInfo->item_count) throw new Exception('该用户该分发逻辑已分发完毕'); // 数据 $oneData = [ 'user_id' => $this->userId, 'coupon_template_id' => $dispenseInfo->coupon_template_id, 'coupon_name' => $dispenseInfo->coupon_name, 'coupon_dispense_id' => $dispenseInfo->id, 'status' => CouponCode::COUPON_STATUS_UNUSED, 'validity_start_time' => $validityTime['start_time'], 'validity_end_time' => $validityTime['end_time'], ]; $insertData = array_map(function () use ($oneData) { return $oneData; }, array_fill(0, $dispenseInfo->item_count - $userCount, null)); Db::transaction(function () use ($insertData,$dispenseInfo,$userDispenseInfo) { // 添加优惠券信息 if (!(new UserCoupon())->insert($insertData)) throw new Exception('写入数据失败'); // 修改分发用户信息 $userDispenseInfo->receive_count = $userDispenseInfo->receive_count + count($insertData); $userDispenseInfo->is_receive = CouponCode::DISPENSE_STATUS_IS_RECEIVED; if (!$userDispenseInfo->save()) throw new Exception('修改分发用户信息失败'); //修改分发 log 信息 (修改receive_count = origin_value + count($insertData) ps : 查询还没发的用户 * item_count / total_count = 完成百分比) $dispenseInfo->receive_count = $dispenseInfo->receive_count + count($insertData); if (!$dispenseInfo->save()) throw new Exception('修改分发log信息失败'); }); $nominalValue = match ($templateInfo->coupon_type) { CouponCode::COUPON_TYPE_INSTANT_REDUCTION => $templateInfo->amount.'元', CouponCode::COUPON_TYPE_DISCOUNT => ($templateInfo->ratio * 10).'%', }; foreach ($insertData as $item) { $msgData = [ 'thing2' => ['value' => $dispenseInfo->coupon_name], 'thing9' => ['value' => $nominalValue], 'thing8' => ['value' => $validityTime['start_time'].'/'.$validityTime['end_time']], 'thing4' => ['value' => 'xxx'], ]; $message = new WxSubMessageProducer([ 'type' => WxMiniCode::SEND_MSG_TYPE_GET_COUPON, 'user_id' => $this->userId, 'data' => $msgData, 'page' => null ]); $this->producer->produce($message); } } }