<?php
namespace app\admin\controller\jeek\shop;
use app\common\controller\Backend;
use think\Db;
use think\Exception;
use think\exception\PDOException;
use think\exception\ValidateException;
/**
* sku商品插件
*/
class Skugoods extends Backend
{
/**
* 模型
*
* @var [type]
*/
protected $model = null;
/**
* 是否是关联查询
*
* @var bool
*/
protected $relationSearch = true;
/**
* 快速搜索时执行查找的字段
*
* @var string
*/
protected $searchFields = 'name';
/**
* 是否开启Validate验证
*
* @var bool
*/
protected $modelValidate = true;
/**
* 是否开启模型场景验证
*
* @var bool
*/
protected $modelSceneValidate = true;
/**
* 初始化方法
*
* @return void
*/
public function _initialize()
{
parent::_initialize();
$this->model = new \app\admin\model\jeek\shop\SkuGoods;
$this->specModel = new \app\admin\model\jeek\shop\SkuSpec;
$this->itemModel = new \app\admin\model\jeek\shop\SkuItem;
$this->productModel = new \app\admin\model\jeek\shop\SkuProduct;
}
/**
* 查看
*/
public function index()
{
//设置过滤方法
$this->request->filter(['strip_tags']);
if ($this->request->isAjax()) {
//如果发送的来源是Selectpage,则转发到Selectpage
if ($this->request->request('keyField')) {
return $this->selectpage();
}
list($where, $sort, $order, $offset, $limit) = $this->buildparams();
$total = $this->model
->where($where)
->with('category')
->order($sort, $order)
->count();
$list = $this->model
->where($where)
->with('category')
->order($sort, $order)
->limit($offset, $limit)
->select();
$list = collection($list)->toArray();
$result = array("total" => $total, "rows" => $list);
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 ($this->dataLimit && $this->dataLimitFieldAutoFill) {
$params[$this->dataLimitField] = $this->auth->id;
}
$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);
$this->createSpecItem($this->model->id,$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);
$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);
// 删除原本的规格参数
$this->specModel->where('goods_id',$row->id)->delete();
$this->itemModel->where('goods_id',$row->id)->delete();
$this->productModel->where('goods_id',$row->id)->delete();
$this->createSpecItem($row->id,$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', ''));
}
$this->getVueInitData($ids);
$this->view->assign("row", $row);
return $this->view->fetch();
}
/**
* 真实删除
*/
public function destroy($ids = "")
{
$pk = $this->model->getPk();
$adminIds = $this->getDataLimitAdminIds();
if (is_array($adminIds)) {
$this->model->where($this->dataLimitField, 'in', $adminIds);
}
if ($ids) {
$this->model->where($pk, 'in', $ids);
}
$count = 0;
Db::startTrans();
try {
$list = $this->model->onlyTrashed()->select();
foreach ($list as $k => $v) {
$count += $v->delete(true);
}
$this->model->where($pk, 'in', $ids);
// 删除原本的规格参数
$this->specModel->where('goods_id','in', $ids)->delete();
$this->itemModel->where('goods_id','in', $ids)->delete();
$this->productModel->where('goods_id','in', $ids)->delete();
Db::commit();
} catch (PDOException $e) {
Db::rollback();
$this->error($e->getMessage());
} catch (Exception $e) {
Db::rollback();
$this->error($e->getMessage());
}
if ($count) {
$this->success();
} else {
$this->error(__('No rows were deleted'));
}
$this->error(__('Parameter %s can not be empty', 'ids'));
}
/**
* 商品规格写入
*
* @param [type] $goods
没有合适的资源?快使用搜索试试~ 我知道了~
资源详情
资源评论
资源推荐
收起资源包目录



























































共 27 条
- 1





















宋拾壹
- 粉丝: 22
- 资源: 5
上传资源 快速赚钱
我的内容管理 展开
我的资源 快来上传第一个资源
我的收益
登录查看自己的收益我的积分 登录查看自己的积分
我的C币 登录后查看C币余额
我的收藏
我的下载
下载帮助


安全验证
文档复制为VIP权益,开通VIP直接复制

评论4