xieruidong vor 2 Jahren
Ursprung
Commit
fa3b853865

+ 50 - 10
application/admin/controller/GoodsInstall.php

@@ -3,6 +3,7 @@
 namespace app\admin\controller;
 
 use app\common\controller\Backend;
+use app\common\model\GoodsInstallLink;
 
 /**
  * 安装费管理
@@ -72,6 +73,11 @@ class GoodsInstall extends Backend
     }
 
     public function goods(){
+        $id=input('id/d');
+        $skus=[];
+        if($id){
+            $skus=GoodsInstallLink::where('goods_install_id',$id)->column('goods_sku_id');
+        }
         $category=\app\common\model\Category::mall(['goods','goods.sku']);
         $data=[];
         foreach ($category as $cate){
@@ -96,10 +102,10 @@ class GoodsInstall extends Backend
                 ];
                 foreach ($goods['sku'] as $sku){
                     $data[]=[
-                        'id'=>"sku-".$sku['id'],
+                        'id'=>"goods-{$goods['id']}|"."sku-".$sku['id'],
                         'parent'=>"goods-".$goods['id'],
                         'state'=>[
-                            'selected'=>false,
+                            'selected'=>in_array($sku['id'],$skus),
                         ],
                         'text'=>$sku['name'],
                         'type'=>'menu',
@@ -115,27 +121,61 @@ class GoodsInstall extends Backend
         return $this->makeAdd();
     }
 
+    public function edit($ids = null)
+    {
+        return $this->makeAdd($ids);
+    }
+
     protected function makeAdd($id=null){
         if($this->request->isGet()){
-            return $this->fetch();
+            if($id){
+                $row=$this->model->find($id);
+                $this->assign('row',$row);
+            }
+            return $this->fetch('add');
         }else{
             $data=input('row/a');
             $this->validate($data,[
-                'name'=>['require'],
-                'goods'=>['require'],
+                'name|名称'=>['require'],
+                'goods|商品'=>['require'],
                 'fee'=>['require','array','min:1']
             ]);
+            $fee=$data['fee'];
+            foreach ($fee as $feeItem){
+                $this->validate($feeItem,[
+                    'num_min|最小数量'=>['require','egt:0'],
+                    'num_max|最大数量'=>['require','egt:0'],
+                    'amount|安装费'=>['require','egt:0'],
+                ]);
+            }
 
+            $data['goods']=array_filter(explode(',',$data['goods']));
             if($id) {
                 $install = $this->model->find($id);
-                $install['name']=$data['name'];
-                $install['fee']=$data['fee'];
-                foreach ($data['goods'] as $good){
-
-                }
             }else{
                 $install = new $this->model;
             }
+            $install['name']=$data['name'];
+            $install['fee']=array_values($data['fee']);
+            $install->save();
+            $ids=[0];
+            foreach ($data['goods'] as $good){
+                if(strpos($good,'|')){
+                    list($goodsStr,$skuStr)=explode('|',$good);
+                    $goods_id=str_replace('goods-','',$goodsStr);
+                    $sku_id=str_replace('sku-','',$skuStr);
+                    $link=$install->link()->sku($sku_id,$goods_id)->find();
+                    if(!$link){
+                        $link=$install->link()->save([
+                            'goods_id'=>$goods_id,
+                            'goods_sku_id'=>$sku_id ,
+                        ]);
+                    }
+                    $ids[]=$link['id'];
+                }
+            }
+            $install->link()->whereNotIn('id',$ids)->delete();
+            $this->success();
         }
     }
 }

+ 9 - 5
application/admin/view/goods_install/add.html

@@ -4,11 +4,13 @@
     }
 </style>
 <form id="add-form" class="form-horizontal" role="form" data-toggle="validator" method="POST" action="">
-
+    {if isset($row)}
+    <input type="hidden" class="rowId" name="row[id]" value="{$row.id}"/>
+    {/if}
     <div class="form-group">
         <label class="control-label col-xs-12 col-sm-2">{:__('Name')}:</label>
         <div class="col-xs-12 col-sm-8">
-            <input id="c-name" data-rule="required" class="form-control" name="row[name]" type="text">
+            <input id="c-name" data-rule="required" class="form-control" name="row[name]" type="text" value="{$row.name|default=''}">
         </div>
     </div>
     <div class="form-group">
@@ -29,19 +31,21 @@
                     <td>安装费</td>
                     <td>操作</td>
                 </tr>
+                {foreach name='$row["fee"]??[[]]' item='a'}
                 <tr class="amountItem">
                     <td>
-                        <input name="row[fee][0][num_min]" class="ism"/>
+                        <input name="row[fee][{$key}][num_min]" class="ism" value="{$a.num_min|default=''}"/>
                         -
-                        <input name="row[fee][0][num_max]" class="ism"/>
+                        <input name="row[fee][{$key}][num_max]" class="ism" value="{$a.num_max|default=''}"/>
                     </td>
                     <td>
-                        <input name="row[fee][0][amount]" class="ism" type="number"/>
+                        <input name="row[fee][{$key}][amount]" class="ism" type="number" value="{$a.amount|default=''}"/>
                     </td>
                     <td>
                         <a class="delOne">删除</a>
                     </td>
                 </tr>
+                {/foreach}
                 <tr>
                     <td colspan="3">
                         <a class="addOne">增加</a>

+ 1 - 0
application/common/model/Goods.php

@@ -128,6 +128,7 @@ class Goods Extends Model
             GoodsBind::where('bind_goods_id',$goods['id'])->delete();
             GoodsCart::where('goods_id',$goods['id'])->delete();
             UserCouponGoods::where('goods_id',$goods['id'])->delete();
+            GoodsInstallLink::where('goods_id',$goods['id'])->delete();
         });
     }
 }

+ 5 - 0
application/common/model/GoodsInstall.php

@@ -10,4 +10,9 @@ class GoodsInstall extends Model
     protected $type=[
         'fee'=>'json',
     ];
+    protected $autoWriteTimestamp=true;
+
+    public function link(){
+        return $this->hasMany(GoodsInstallLink::class);
+    }
 }

+ 17 - 0
application/common/model/GoodsInstallLink.php

@@ -0,0 +1,17 @@
+<?php
+
+namespace app\common\model;
+
+use think\db\Query;
+use think\Model;
+
+
+class GoodsInstallLink extends Model
+{
+    public function scopeSku(Query $query,$sku_id,$goods_id=null){
+        $query->where('goods_sku_id',$sku_id);
+        if($goods_id){
+            $query->where('goods_id',$goods_id);
+        }
+    }
+}

+ 13 - 12
public/assets/js/backend/goods_install.js

@@ -54,17 +54,6 @@ define(['jquery', 'bootstrap', 'backend', 'table', 'form', 'jstree'], function (
         },
         add: function () {
             Controller.api.bindevent();
-            $('.addOne').click(function (){
-                let html=$('.amountItem')[0].outerHTML,time=Date.now()
-                html=html.replace(/value="\s?"/g,'').replace(/\[fee]\[\d+]/g,`[fee][${time}]`)
-                $(this).parent().parent().before(html)
-            })
-            $(document).on('click','.delOne',function (){
-                if($('.amountItem').length===1){
-                    return
-                }
-                $(this).parent().parent().remove()
-            })
         },
         edit: function () {
             Controller.api.bindevent();
@@ -78,9 +67,21 @@ define(['jquery', 'bootstrap', 'backend', 'table', 'form', 'jstree'], function (
                     }
                     return true;
                 });
+                $('.addOne').click(function (){
+                    let html=$('.amountItem')[0].outerHTML,time=Date.now()
+                    html=html.replace(/\[fee]\[\d+]/g,`[fee][${time}]`)
+                    let a=$(this).parent().parent().before(html)
+                    $('.amountItem').last().find('input').val('')
+                })
+                $(document).on('click','.delOne',function (){
+                    if($('.amountItem').length===1){
+                        return
+                    }
+                    $(this).parent().parent().remove()
+                })
                 //渲染权限节点树
                 //变更级别后需要重建节点树
-                var id = 1;
+                var id = $('.rowId').val();
                 $.ajax({
                     url: "goods_install/goods",
                     type: 'post',