model = new \app\admin\model\lottery\LotteryPrize; // 获取枚举值列表 $this->view->assign("typeList", $this->model->getTypeList()); $this->view->assign("deliverTypeList", $this->model->getDeliverTypeList()); $this->view->assign("statusList", $this->model->getStatusList()); } /** * 查看 */ public function index() { //当前是否为关联查询 $this->relationSearch = true; //设置过滤方法 $this->request->filter(['strip_tags', 'trim']); if ($this->request->isAjax()) { //如果发送的来源是Selectpage,则转发到Selectpage if ($this->request->request('keyField')) { return $this->selectpage(); } list($where, $sort, $order, $offset, $limit) = $this->buildparams(); $list = $this->model ->with(['activity']) ->where($where) ->order($sort, $order) ->paginate($limit); foreach ($list as $row) { $row->visible(['id','activity_id','name','type','image','probability','total_stock','remain_stock','win_count','sort_order','deliver_type','status']); $row->visible(['activity']); $row->getRelation('activity')->visible(['name']); } $result = array("total" => $list->total(), "rows" => $list->items()); return json($result); } return $this->view->fetch(); } /** * 添加 */ public function add() { if ($this->request->isPost()) { $params = $this->request->post("row/a"); if ($params) { $params = $this->preExcludeFields($params); // 处理兑换码 if (isset($params['exchange_codes']) && is_array($params['exchange_codes'])) { $params['exchange_codes'] = json_encode($params['exchange_codes']); } // 初始化剩余库存 if (isset($params['total_stock'])) { $params['remain_stock'] = $params['total_stock']; } $result = false; Db::startTrans(); try { //是否采用模型验证 if ($this->modelValidate) { $name = str_replace("\\model\\", "\\validate\\", get_class($this->model)); $validate = is_bool($this->modelValidate) ? ($this->modelSceneValidate ? $name . '.add' : $name) : $this->modelValidate; $this->model->validateFailException(true)->validate($validate); } $result = $this->model->allowField(true)->save($params); Db::commit(); } catch (ValidateException $e) { Db::rollback(); $this->error($e->getMessage()); } catch (PDOException $e) { Db::rollback(); $this->error($e->getMessage()); } catch (Exception $e) { Db::rollback(); $this->error($e->getMessage()); } if ($result !== false) { $this->success(); } else { $this->error(__('No rows were inserted')); } } $this->error(__('Parameter %s can not be empty', '')); } return $this->view->fetch(); } /** * 编辑 */ public function edit($ids = null) { $row = $this->model->get($ids); if (!$row) { $this->error(__('No Results were found')); } $adminIds = $this->getDataLimitAdminIds(); if (is_array($adminIds)) { if (!in_array($row[$this->dataLimitField], $adminIds)) { $this->error(__('You have no permission')); } } if ($this->request->isPost()) { $params = $this->request->post("row/a"); if ($params) { $params = $this->preExcludeFields($params); // 处理兑换码 if (isset($params['exchange_codes']) && is_array($params['exchange_codes'])) { $params['exchange_codes'] = json_encode($params['exchange_codes']); } // 如果修改了总库存,同步修改剩余库存 if (isset($params['total_stock']) && $params['total_stock'] != $row->total_stock) { $diff = $params['total_stock'] - $row->total_stock; $params['remain_stock'] = $row->remain_stock + $diff; } $result = false; Db::startTrans(); try { //是否采用模型验证 if ($this->modelValidate) { $name = str_replace("\\model\\", "\\validate\\", get_class($this->model)); $validate = is_bool($this->modelValidate) ? ($this->modelSceneValidate ? $name . '.edit' : $name) : $this->modelValidate; $row->validateFailException(true)->validate($validate); } $result = $row->allowField(true)->save($params); Db::commit(); } catch (ValidateException $e) { Db::rollback(); $this->error($e->getMessage()); } catch (PDOException $e) { Db::rollback(); $this->error($e->getMessage()); } catch (Exception $e) { Db::rollback(); $this->error($e->getMessage()); } if ($result !== false) { $this->success(); } else { $this->error(__('No rows were updated')); } } $this->error(__('Parameter %s can not be empty', '')); } // 处理兑换码显示 if ($row->exchange_codes) { $row->exchange_codes = json_decode($row->exchange_codes, true); } $this->view->assign("row", $row); return $this->view->fetch(); } /** * 批量导入兑换码 */ public function importCodes($ids = null) { $row = $this->model->get($ids); if (!$row) { $this->error(__('No Results were found')); } if ($this->request->isPost()) { $codes = $this->request->post('codes'); if (empty($codes)) { $this->error('兑换码不能为空'); } // 处理兑换码列表 $codeArray = array_filter(array_map('trim', explode("\n", $codes))); $existingCodes = $row->exchange_codes ? json_decode($row->exchange_codes, true) : []; $newCodes = array_merge($existingCodes, $codeArray); // 更新奖品 $row->exchange_codes = json_encode($newCodes); $row->total_stock = count($newCodes); $row->remain_stock = count($newCodes) - $row->win_count; if ($row->save()) { $this->success('导入成功,共导入' . count($codeArray) . '个兑换码'); } else { $this->error('导入失败'); } } $this->view->assign("row", $row); return $this->view->fetch(); } /** * 补充库存 */ public function addStock($ids = null) { $row = $this->model->get($ids); if (!$row) { $this->error(__('No Results were found')); } if ($this->request->isPost()) { $addCount = $this->request->post('add_count', 0); if ($addCount <= 0) { $this->error('补充数量必须大于0'); } $row->total_stock += $addCount; $row->remain_stock += $addCount; if ($row->save()) { $this->success('库存补充成功'); } else { $this->error('库存补充失败'); } } $this->view->assign("row", $row); return $this->view->fetch(); } }