123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181 |
- <?php
- namespace app\admin\controller;
- use app\common\controller\Backend;
- use app\common\model\GoodsInstallLink;
- /**
- * 安装费管理
- *
- * @icon fa fa-circle-o
- */
- class GoodsInstall extends Backend
- {
- protected $noNeedRight=['goods'];
- /**
- * GoodsInstall模型对象
- * @var \app\admin\model\GoodsInstall
- */
- protected $model = null;
- public function _initialize()
- {
- parent::_initialize();
- $this->model = new \app\admin\model\GoodsInstall;
- }
- public function import()
- {
- parent::import();
- }
- /**
- * 默认生成的控制器所继承的父类中有index/add/edit/del/multi五个基础方法、destroy/restore/recyclebin三个回收站方法
- * 因此在当前控制器中可不用编写增删改查的代码,除非需要自己控制这部分逻辑
- * 需要将application/admin/library/traits/Backend.php中对应的方法复制到当前控制器,然后进行修改
- */
-
- /**
- * 查看
- */
- public function index()
- {
- //当前是否为关联查询
- $this->relationSearch = false;
- //设置过滤方法
- $this->request->filter(['strip_tags', 'trim']);
- if ($this->request->isAjax()) {
- //如果发送的来源是Selectpage,则转发到Selectpage
- if ($this->request->request('keyField')) {
- return $this->selectpage();
- }
- list($where, $sort, $order, $offset, $limit) = $this->buildparams();
- $list = $this->model
-
- ->where($where)
- ->order($sort, $order)
- ->paginate($limit);
- foreach ($list as $row) {
- $row->visible(['id','name','create_time','update_time']);
-
- }
- $result = array("total" => $list->total(), "rows" => $list->items());
- return json($result);
- }
- return $this->view->fetch();
- }
- 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){
- $data[]=[
- 'id'=>$cate['id'],
- 'parent'=>'#',
- 'state'=>[
- 'selected'=>false,
- ],
- 'text'=>$cate['name'],
- 'type'=>'menu',
- ];
- foreach ($cate['goods'] as $goods){
- $data[]=[
- 'id'=>"goods-".$goods['id'],
- 'parent'=>$cate['id'],
- 'state'=>[
- 'selected'=>false,
- ],
- 'text'=>$goods['name'],
- 'type'=>'menu',
- ];
- foreach ($goods['sku'] as $sku){
- $data[]=[
- 'id'=>"goods-{$goods['id']}|"."sku-".$sku['id'],
- 'parent'=>"goods-".$goods['id'],
- 'state'=>[
- 'selected'=>in_array($sku['id'],$skus),
- ],
- 'text'=>$sku['name'],
- 'type'=>'menu',
- ];
- }
- }
- }
- $this->success('',null,$data);
- }
- public function add()
- {
- return $this->makeAdd();
- }
- public function edit($ids = null)
- {
- return $this->makeAdd($ids);
- }
- protected function makeAdd($id=null){
- if($this->request->isGet()){
- 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'],
- '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);
- }else{
- $install = new $this->model;
- }
- $install['name']=$data['name'];
- $install['fee']=array_values($data['fee']);
- $install->save();
- $ids=[];
- 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();
- }
- }
- }
|