154 lines
3.9 KiB
PHP
154 lines
3.9 KiB
PHP
<?php
|
|
|
|
namespace app\admin\controller\app\physical;
|
|
|
|
use app\common\controller\Backend;
|
|
use think\Db;
|
|
|
|
/**
|
|
* 规格管理
|
|
*/
|
|
class Sku extends Backend
|
|
{
|
|
|
|
protected $model = null;
|
|
|
|
public function _initialize()
|
|
{
|
|
parent::_initialize();
|
|
$this->model = new \app\admin\model\app\physical\Sku;
|
|
}
|
|
|
|
/**
|
|
* 查看
|
|
*/
|
|
public function index()
|
|
{
|
|
$this->request->filter(['strip_tags', 'trim']);
|
|
|
|
$goodsId = $this->request->param('goods_id');
|
|
|
|
if (!$goodsId) {
|
|
$this->error('商品ID不能为空');
|
|
}
|
|
|
|
$tree = $this->model->getTree($goodsId);
|
|
|
|
$this->success("获取成功", $tree);
|
|
}
|
|
|
|
/**
|
|
* 添加
|
|
*/
|
|
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 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获取规格树
|
|
*/
|
|
public function getTree()
|
|
{
|
|
$goodsId = $this->request->param('goods_id');
|
|
if (!$goodsId) {
|
|
$this->error('商品ID不能为空');
|
|
}
|
|
|
|
$tree = $this->model->getTree($goodsId);
|
|
$this->success("获取成功", $tree);
|
|
}
|
|
}
|