zhanglinxin 1 年之前
父节点
当前提交
f23ce577ef

+ 66 - 32
application/approve/controller/Goods.php

@@ -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;
     }
 
     /**

+ 1 - 1
application/approve/view/approve_info_log/form_3.html

@@ -125,7 +125,7 @@
                                     <p class="help-block"></p>
                                 </div>
                             </div>
-                            
+
                             {notempty name='vo2.module_info.document_text'}
                             <div class="layui-form-item">
                                 <label class="layui-form-label color-green font-w7">附件</label>

+ 1 - 1
application/common/service/ApproveInfoService.php

@@ -84,7 +84,7 @@ class ApproveInfoService
                         ->where('module', CommonConstant::MODULE_1)
                         ->where('status', CommonConstant::STATUS_3)
                         ->where('is_deleted', CommonConstant::IS_DELETED_0)
-                        ->find($id);
+                        ->find($params['apply_id']);
                     if (!$apply) {
                         except('申购申请单不存在或已删除');
                     }