|
@@ -22,6 +22,7 @@ use app\common\service\GoodsCategoryService;
|
|
|
use library\Controller;
|
|
|
use library\tools\Data;
|
|
|
use think\Db;
|
|
|
+use think\Exception;
|
|
|
|
|
|
/**
|
|
|
* 商品
|
|
@@ -91,15 +92,41 @@ class Goods extends Controller
|
|
|
public function add()
|
|
|
{
|
|
|
$this->title = '添加';
|
|
|
-// $this->_form($this->table, 'form');
|
|
|
+ $this->_submit('form');
|
|
|
+ }
|
|
|
+
|
|
|
+ /**
|
|
|
+ * 编辑
|
|
|
+ * @auth true
|
|
|
+ * @throws \think\Exception
|
|
|
+ * @throws \think\db\exception\DataNotFoundException
|
|
|
+ * @throws \think\db\exception\ModelNotFoundException
|
|
|
+ * @throws \think\exception\DbException
|
|
|
+ * @throws \think\exception\PDOException
|
|
|
+ */
|
|
|
+ public function edit()
|
|
|
+ {
|
|
|
+ $this->title = '编辑';
|
|
|
+ $this->_submit('form');
|
|
|
+ }
|
|
|
+
|
|
|
+ protected function _submit($template){
|
|
|
+ $id = input('id') ?:0;
|
|
|
if ($this->request->isGet()) {
|
|
|
+ list($data) = [[]];
|
|
|
+ if($id > 0){
|
|
|
+ $data = model::with(['goodsStock'])->find($id);
|
|
|
+ if($data){
|
|
|
+ $data['first_classify'] = $data['goods_category_first'];
|
|
|
+ $data['second_classify'] = $data['goods_category_id'];
|
|
|
+ }
|
|
|
+ }
|
|
|
$this->category_list = GoodsCategoryService::get_list([],1);
|
|
|
- return $this->fetch('form',['vo' => []]);
|
|
|
+ return $this->fetch($template,['vo' => $data]);
|
|
|
}
|
|
|
if ($this->request->isPost()) {
|
|
|
list($data) = [$this->request->post()];
|
|
|
if($data){
|
|
|
- // 添加编辑提交数据时
|
|
|
$data['goods_category_first'] = $data['first_classify'];
|
|
|
$data['goods_category_id'] = $data['second_classify'];
|
|
|
$goods_info = model::field('id')
|
|
@@ -112,43 +139,50 @@ class Goods extends Controller
|
|
|
$this->error('该商品已存在不能重复添加');
|
|
|
}
|
|
|
|
|
|
- $result = model::create($data);
|
|
|
- $goods_stock = $data['goods_stock'];
|
|
|
- $goods_stock_data = [];
|
|
|
- if(isset($data['id']) && $data['id']){
|
|
|
-
|
|
|
- } else{
|
|
|
- // 添加商品 添加规格
|
|
|
- foreach ($goods_stock as $key=>$val){
|
|
|
- $goods_stock_data[] = [
|
|
|
- 'goods_id'=>$result,
|
|
|
- 'name'=>$val['name'],
|
|
|
- 'stock'=>$val['stock'],
|
|
|
- ];
|
|
|
- }
|
|
|
- if($goods_stock_data){
|
|
|
- GoodsStock::insertAll($goods_stock_data);
|
|
|
+ Db::startTrans();
|
|
|
+ try{
|
|
|
+ if($id > 0){
|
|
|
+ model::where('id',$id)->update($data);
|
|
|
+ $goods_id = $id;
|
|
|
+ } else{
|
|
|
+ $result = model::create($data);
|
|
|
+ $goods_id = $result->id;
|
|
|
}
|
|
|
+ self::create_stock($goods_id,$data);
|
|
|
+
|
|
|
+ Db::commit();
|
|
|
+ } catch (Exception $e){
|
|
|
+ Db::rollback();
|
|
|
+ $this->error($e->getMessage());
|
|
|
}
|
|
|
$this->success('商品编辑成功!', 'javascript:history.back()');
|
|
|
+
|
|
|
+
|
|
|
+
|
|
|
}
|
|
|
}
|
|
|
-
|
|
|
}
|
|
|
|
|
|
- /**
|
|
|
- * 编辑
|
|
|
- * @auth true
|
|
|
- * @throws \think\Exception
|
|
|
- * @throws \think\db\exception\DataNotFoundException
|
|
|
- * @throws \think\db\exception\ModelNotFoundException
|
|
|
- * @throws \think\exception\DbException
|
|
|
- * @throws \think\exception\PDOException
|
|
|
- */
|
|
|
- public function edit()
|
|
|
+ protected function create_stock($id, $data = [])
|
|
|
{
|
|
|
- $this->title = '编辑';
|
|
|
- $this->_form($this->table, 'form');
|
|
|
+ $goods_stock = $data['goods_stock'];
|
|
|
+ $goods_stock_data = [];
|
|
|
+ if(isset($data['id']) && $data['id'] > 0){
|
|
|
+
|
|
|
+ } else{
|
|
|
+ // 添加商品 添加规格
|
|
|
+ foreach ($goods_stock as $key=>$val){
|
|
|
+ $goods_stock_data[] = [
|
|
|
+ 'goods_id'=>$id,
|
|
|
+ 'name'=>$val['name'],
|
|
|
+ 'stock'=>$val['stock'],
|
|
|
+ ];
|
|
|
+ }
|
|
|
+ if($goods_stock_data){
|
|
|
+ GoodsStock::insertAll($goods_stock_data);
|
|
|
+ }
|
|
|
+ }
|
|
|
+ return true;
|
|
|
}
|
|
|
|
|
|
/**
|