request->get('ghsId', 0); $ghs = GhsClass::getById($ghsId, true); GhsClass::valid($ghs, $this->shopId); $ghsShopId = $ghs->shopId ?? 0; $ghsShop = ShopClass::getById($ghsShopId, true); $xrFl = $ghsShop->xrFl ?? 0; if ($xrFl == 0) { util::fail('活动已结束'); } $sendNewGift = $ghs->sendNewGift ?? 0; if ($sendNewGift == 1) { util::fail('已经领过了'); } $new = $ghs->new ?? 0; if ($new == 0) { util::fail('新人才能领取哦'); } $customId = $ghs->customId ?? 0; $custom = CustomClass::getById($customId, true); XrFlClass::giveGift($ghs, $custom); util::complete('领取成功'); } /** * 获取红包列表 */ public function actionHbList() { $where = ['shopId' => $this->shopId]; $customId = Yii::$app->request->get('customId', ''); if (!empty($customId)) { $where['customId'] = $customId; } $status = Yii::$app->request->get('status', ''); if (isset($status) && $status != '') { if ($status == -1) { // 已失效包含:1作废 与 过期(0未使用但endTime<当前时间) --- 所以当操作作废时,status改为-1,同时将endTime改为当前时间 $where['endTime<'] = time(); } else { $where['status'] = $status; } } $list = HbClass::getList('*', $where, 'id desc', 'custom'); if (!empty($list['list'])) { foreach ($list['list'] as &$v) { $v['customName'] = $v['custom']['name']; } } util::success($list); } // 单个门店下所有可用红包 public function actionAvailableHb() { $get = Yii::$app->request->get(); $where = []; $where['customId'] = $get['customId']; $where['status'] = 0; $where['endTime>'] = time(); $list = HbClass::getAllByCondition($where, 'id DESC'); util::success($list); } /** * 获取红包详情 */ public function actionHbDetail() { $hbId = Yii::$app->request->get('hbId'); $hb = HbClass::getById($hbId); util::success($hb); } /** * 创建红包 */ public function actionCreateHb() { $form = new CreateHbForm(); $form->load(Yii::$app->request->post(), ''); if (!$form->validateForm()) { return; } $post = $form->attributes; $duration = (int)$form->days; $count = $post['count'] ?? 1; $beginTime = time(); if ($duration <= 0) { $endTime = 4102416000; } else { $endTime = $beginTime + $duration * 86400; } $data = [ 'mainId' => $this->mainId, 'shopId' => $this->shopId, 'userId' => $post['userId'], 'customId' => $post['customId'], 'amount' => $post['amount'], 'minConsume' => $post['minConsume'], 'beginTime' => $beginTime, 'endTime' => $endTime, 'willTime' => $endTime, 'duration' => $duration, 'getType' => 1, //取得方式 0新人领取 1门店发放 2充值赠送 'remark' => $post['remark'] ?? '', ]; $manageData = [ 'getType' => 1, //取得方式 0新人领取 1门店发放 2充值赠送 'getTargetId' => 0, 'createStaffId' => $this->shopAdminId, 'createStaffName' => $this->shopAdminName, 'remark' => $data['remark'], ]; for ($i = 0; $i < $count; $i++) { $hb = HbClass::add($data); $manageData['hbId'] = $hb['id']; $hbManage = HbManageClass::add($manageData); } $arr = []; if (!empty($hb) && !empty($hbManage)) { $arr = array_merge($hb, $hbManage); } util::success($arr); } /** * 更新红包 */ public function actionUpdateHb() { $rawPost = Yii::$app->request->post(); $form = new UpdateHbForm(); $form->load($rawPost, ''); if (!$form->validateForm()) { return; } $id = (int)$form->id; $hb = HbClass::getById($id, true); //检测是否是本门店的红包 if ($hb->shopId != $this->shopId) { util::fail('不是本门店的红包'); } $connection = Yii::$app->db; $transaction = $connection->beginTransaction(); try { //当是退回红包时,必须检测对应订单 actPrice 为0 if (isset($form->status) && $form->status == 0){ if($hb->status == 1){ //查询订单数据,校验 actPrice,按情况更新 $order = OrderClass::getById($form->orderId, true, 'id, hbId, hbAmount, actPrice'); if($order->hbId == $id){ if($order->actPrice == 0.00){ //更新订单数据 $order->hbId = -$order->hbId; $order->save(); } else { Yii::error('使用红包的订单 actPrice 不等于0,无法执行红包退回。hbId=' . $id); util::fail('使用红包的订单 actPrice 不等于0,无法执行红包退回'); } }else{ Yii::error("订单中的红包id与当前红包不匹配。hbId={$id}, orderId={$form->orderId}"); util::fail('订单中的红包id与当前红包不匹配'); } }else{ Yii::error("当前红包状态不是已使用,不能变更未使用。hbId={$id}"); util::fail("当前红包状态不是已使用,不能变更未使用"); } } // 仅更新允许字段,且只更新请求中实际传入的字段(避免用 null 覆盖原值) $allowFields = array_keys($form->attributes); foreach ($allowFields as $field) { if ($field === 'id') { continue; } if (array_key_exists($field, $rawPost)) { $hb->$field = $form->$field; } } // 当操作作废时,status改为-1,同时将endTime改为当前时间 if (isset($form->status) && $form->status == -1 && $hb->status != -1) { $hb->endTime = time(); //记录作废人 $updateData = [ 'cancelStaffId' => $this->shopAdminId, 'cancelStaffName' => $this->shopAdminName, 'cancelTime' => date('Y-m-d H:i:s'), ]; HbManageClass::updateByCondition(['hbId' => $id], $updateData); } $hb->save(); $transaction->commit(); } catch (\Exception $e) { $transaction->rollBack(); $msg = $e->getMessage(); Yii::error('actionUpdateHb', $msg); util::fail($msg); } util::success($hb->attributes); } /** * 充值送红包规则列表 */ public function actionHbRules() { $list = RechargeShbClass::getList('*', ['shopId' => $this->shopId]); util::success($list); } /** * 充值送红包规则创建 */ public function actionSendHbRules() { $data = Yii::$app->request->post(); $validatedRows = []; foreach ($data as $item) { if (!is_array($item)) { util::fail('参数格式错误'); return; } $form = new SendHbRuleItemForm(); $form->load($item, ''); if (!$form->validateForm()) { return; } $validatedRows[] = $form->attributes; } $hbRules = RechargeShbClass::setRules($this->shopId, $this->mainId, $validatedRows); util::success($hbRules); } /** * 充值送红包规则删除 */ public function actionDeleteHbRule() { $id = Yii::$app->request->post('id'); $re = RechargeShbClass::deleteById($id); if ($re == 1) { util::complete(); } else { util::fail('删除失败'); } } }