request->get(); $where = ['shopId' => $this->shopId]; $customId = $get['customId'] ?? 0; if (!empty($customId)) { $where['customId'] = $customId; } $status = $get['status'] ?? ''; if ($status != '') { $where['status'] = $status; } $respond = HbClass::getList('*', $where, 'id desc', 'custom'); //统计各状态总数量(不受当前 status 筛选影响) $countWhere = $where; unset($countWhere['status']); $respond['all'] = HbClass::getCount($countWhere); $respond['unUse'] = HbClass::getCount(array_merge($countWhere, ['status' => 0])); $respond['used'] = HbClass::getCount(array_merge($countWhere, ['status' => 1])); $respond['expired'] = HbClass::getCount(array_merge($countWhere, ['status' => -1])); if (!empty($respond['list'])) { foreach ($respond['list'] as $k => &$v) { $v['customName'] = $v['custom']['name'] ?? ''; if ($v['status'] == 0 && $v['endTime'] < time()) { //发现红包已过期,需要作废,多有处作废操作,搜索关键词 cancellation_hb $id = $v['id']; HbClass::updateById($id, ['status' => -1]); if ($status == '' || $status == 0) { // 删除 $respond['list'] 中的 $v unset($respond['list'][$k]); $respond['all']--; $respond['unUse']--; $respond['expired']++; } } } } util::success($respond); } // 单个门店下所有可用红包 public function actionAvailableHb() { $get = Yii::$app->request->get(); $where = []; $where['customId'] = $get['customId']; $where['status'] = 0; $where['beginTime<='] = time(); // 生效时间小于等于当前时间 $list = HbClass::getAllByCondition($where, 'id DESC'); //已经过期的就不再显示,并且作废,多有处作废操作,搜索关键词 cancellation_hb if (!empty($list)) { foreach ($list as $k => $v) { if ($v['status'] == 0 && $v['endTime'] < time()) { unset($list[$k]); $id = $v['id']; HbClass::updateById($id, ['status' => -1]); } } } 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; $adminId = $this->shopAdminId; util::checkRepeatCommit($adminId, 5); if (isset($this->shopAdmin->founder) == false || $this->shopAdmin->founder != 2) { util::fail('管理员才能操作'); } $duration = (int)$form->days; $count = $post['count'] ?? 1; $effectiveDate = $post['effectiveDate']; $beginTime = $effectiveDate == '' ? time() : strtotime($effectiveDate); 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('不是本门店的红包'); } if (isset($this->shopAdmin->founder) == false || $this->shopAdmin->founder != 2) { 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改为当前时间,多有处作废操作,搜索关键词 cancellation_hb 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 actionHbRuleDetail() { $id = Yii::$app->request->get('id'); $rule = RechargeShbClass::getById($id); util::success($rule); } /** * 充值送红包规则创建 */ public function actionSendHbRules() { $data = Yii::$app->request->post(); $id = $data['id']; $amount = $data['amount']; $rules = $data['rules']; $validatedRules = []; foreach ($rules as $item) { if (!is_array($item)) { util::fail('参数格式错误'); return; } $form = new SendHbRuleItemForm(); $form->load($item, ''); if (!$form->validateForm()) { return; } $validatedRules[] = $form->attributes; } $hbRules = RechargeShbClass::setRules($this->shopId, $this->mainId, $id, $amount, $validatedRules); util::success($hbRules); } /** * 充值送红包规则删除 */ public function actionDeleteHbRule() { $id = Yii::$app->request->post('id'); $re = RechargeShbClass::deleteById($id); if ($re == 1) { util::complete(); } else { util::fail('删除失败'); } } }