model = new \app\admin\model\app\physical\SkuPrice; } /** * 查看 */ public function index() { $this->request->filter(['strip_tags', 'trim']); $goodsId = $this->request->param('goods_id'); list($where, $sort, $order, $offset, $limit) = $this->buildparams(); $query = $this->model ->where($where) ->where('uniacid', UNIACID) ->order($sort, $order); if ($goodsId) { $query->where(['goods_id' => $goodsId]); } $list = $query->paginate($limit); $result = array("total" => $list->total(), "rows" => $list->items(), 'attr'=>[ 'status'=>$this->model->getStatusList() ]); return $this->success("获取成功",$result); } /** * 添加 */ public function add() { $params = $this->request->param('row/a'); if (empty($params)) { $this->error(__('Parameter %s can not be empty', '')); } $params['uniacid'] = UNIACID; $params['createtime'] = time(); $params['updatetime'] = time(); Db::startTrans(); try { $result = $this->model->allowField(true)->save($params); Db::commit(); } catch (ValidateException|PDOException|Exception $e) { Db::rollback(); $this->error($e->getMessage()); } if ($result !== false) { $this->success("添加成功"); } else { $this->error(__('No rows were inserted')); } } /** * 编辑 */ public function edit($ids = null) { $params = $this->request->param('row/a'); if (empty($params)) { $this->error(__('Parameter %s can not be empty', '')); } $params['updatetime'] = time(); Db::startTrans(); try { $row = $this->model->where('id', $ids)->where('uniacid', UNIACID)->find(); if (!$row) { $this->error(__('No Results were found')); } $result = $row->allowField(true)->save($params); Db::commit(); } catch (ValidateException|PDOException|Exception $e) { Db::rollback(); $this->error($e->getMessage()); } if ($result !== false) { $this->success("更新成功"); } else { $this->error(__('No rows were updated')); } } /** * 详情 */ public function detail($ids = null) { $row = $this->model->where('id', $ids)->where('uniacid', UNIACID)->find(); if (!$row) { $this->error(__('No Results were found')); } $this->success("获取成功", $row); } /** * 删除 */ public function del($ids = "") { if ($ids) { $count = $this->model->where('id', 'in', $ids)->where('uniacid', UNIACID)->delete(); if ($count) { $this->success("删除成功"); } else { $this->error(__('No rows were deleted')); } } $this->error(__('Parameter %s can not be empty', 'ids')); } /** * 批量更新 */ public function multi($ids = "") { $ids = $ids ? $ids : $this->request->param("ids"); if ($ids) { if ($this->request->has('params')) { parse_str($this->request->post("params"), $values); $values = $this->preExcludeFields($values); $values['updatetime'] = time(); $result = $this->model->where('id', 'in', $ids)->where('uniacid', UNIACID)->update($values); if ($result) { $this->success("操作成功"); } else { $this->error(__('No rows were updated')); } } } $this->error(__('Parameter %s can not be empty', 'ids')); } /** * 根据商品ID获取SKU列表 */ public function getByGoodsId() { $goodsId = $this->request->param('goods_id'); if (!$goodsId) { $this->error('商品ID不能为空'); } $list = $this->model->where('goods_id', $goodsId)->where('uniacid', UNIACID)->select(); $this->success("获取成功", $list); } /** * 批量更新库存 */ public function updateStock() { $params = $this->request->param(); $skus = $params['skus'] ?? []; if (empty($skus)) { $this->error(__('Parameter %s can not be empty', 'skus')); } Db::startTrans(); try { foreach ($skus as $sku) { if (isset($sku['id']) && isset($sku['stock'])) { $this->model->where('id', $sku['id'])->update([ 'stock' => $sku['stock'], 'updatetime' => time() ]); } } Db::commit(); $this->success("更新成功"); } catch (PDOException|Exception $e) { Db::rollback(); $this->error($e->getMessage()); } } /** * 批量更新价格 */ public function updatePrice() { $params = $this->request->param(); $skus = $params['skus'] ?? []; if (empty($skus)) { $this->error(__('Parameter %s can not be empty', 'skus')); } Db::startTrans(); try { foreach ($skus as $sku) { if (isset($sku['id'])) { $updateData = ['updatetime' => time()]; if (isset($sku['price'])) { $updateData['price'] = $sku['price']; } if (isset($sku['original_price'])) { $updateData['original_price'] = $sku['original_price']; } if (isset($sku['cost_price'])) { $updateData['cost_price'] = $sku['cost_price']; } $this->model->where('id', $sku['id'])->update($updateData); } } Db::commit(); $this->success("更新成功"); } catch (PDOException|Exception $e) { Db::rollback(); $this->error($e->getMessage()); } } /** * 获取状态列表 */ public function getStatusList() { $this->success("获取成功", $this->model->getStatusList()); } }