GoodsController.php 8.9 KB
<?php
namespace app\admin\controller;

use app\admin\model\RouteModel;
use cmf\controller\AdminBaseController;
use think\Db;

class GoodsController extends AdminBaseController{

    /**
     * 平台商品列表
     */
    public function goods_list(){

        if($this -> request -> isPost()){
            $where = [
                'type' => ['=', 1],
            ];
            if(!empty($_POST['start_time']) && !empty($_POST['end_time'])){
                $start_time = strtotime($_POST['start_time']);
                $end_time = strtotime($_POST['end_time']);
                $where['create_time'] = [['>=',$start_time],['<=',$end_time]];
            }
            $keyword = $_POST['keyword'];
            if(!empty($_POST['keyword'])){
                $where['book_name'] = ['like',"%$keyword%"];
            }
            if(!empty($_POST['classify_id'])){
                if($_POST['classify_id'] == 0){
                    $where['classify_id'] = 0;
                }elseif ($_POST['classify_id'] == 100){

                }else{
                    $where['classify_id'] = $_POST['classify_id'];
                }
            }
            $classif_data = Db::name('classification') -> select();
            $this -> assign('classif_data',$classif_data);
            $data = Db::name('goods') -> where($where) -> paginate(12);
            $data1 = $data -> toArray();
            foreach ($data1['data'] as $key => $val){
                $class_data = Db::name('classification') -> where('id',$val['classify_id']) -> find();
                $data1['data'][$key]['classify_name'] = $class_data['name'];
            }
            $this -> assign('data',$data1['data']);
            $this -> assign('res',$data);
            return $this -> fetch();
        }else{
            $classif_data = Db::name('classification') -> select();
            $this -> assign('classif_data',$classif_data);
            $data = Db::name('goods') -> where('type','1') -> paginate(12);
            $data1 = $data -> toArray();
            foreach ($data1['data'] as $key => $val){
                $class_data = Db::name('classification') -> where('id',$val['classify_id']) -> find();
                $data1['data'][$key]['classify_name'] = $class_data['name'];
            }
            $this -> assign('data',$data1['data']);
            $this -> assign('res',$data);
            return $this -> fetch();
        }

    }

    /**
     * 平台商品添加
     */
    public function goods_add(){

        if($this -> request -> isPost()){
            if(!empty($_POST['det_img'])){
                $_POST['det_img'] = json_encode($_POST['det_img']);
                $_POST['det_name'] = json_encode($_POST['det_name']);
            }
            $_POST['type'] = 1;
            $_POST['create_time'] = time();
            if(!empty($_POST['label_id'])){
                $label_id = $_POST['label_id'];
            }
            unset($_POST['label_id']);
            $data = Db::name('goods') -> insertGetId($_POST);
            if(!empty($label_id)){
                foreach($label_id as $key => $val){
                    Db::name('goods_label') -> insert(['goods_id'=>$data,'label_id'=>$val]);
                }
            }
            if($data){
                $this -> success('添加成功',url('Goods/goods_add'));
            }else{
                $this -> error('添加失败');
            }
        }else{
            $classif_data = Db::name('classification') -> select();
            $this -> assign('classif_data',$classif_data);
            $label_data = Db::name('label') -> select();
            $this -> assign('label_data',$label_data);
            return $this -> fetch();
        }

    }

    /**
     * 平台商品编辑
     */
    public function goods_edit(){

        if($this -> request -> isPost()){
            if(!empty($_POST['det_img'])){
                $_POST['det_img'] = json_encode($_POST['det_img']);
                $_POST['det_name'] = json_encode($_POST['det_name']);
            }
            if(!empty($_POST['label_id'])){
                $label_id = $_POST['label_id'];
            }
            unset($_POST['label_id']);
            $goods_label_del = Db::name('goods_label') -> where('goods_id',$_POST['id']) -> delete();
            if(!empty($label_id)){
                foreach ($label_id as $key => $val){
                    $goods_label_data = Db::name('goods_label') -> insert(['goods_id'=>$_POST['id'],'label_id'=>$val]);
                }
            }
            $data = Db::name('goods') -> update($_POST);
            $this -> success('保存成功',url('Goods/goods_edit',array('id'=>$_POST['id'])));
        }else{
            $id = $this -> request -> param();
            $data = Db::name('Goods') -> where('id',$id['id']) -> find();
            if(!empty($data['det_img'])){
                $data['det_img'] = json_decode($data['det_img'],true);
                $data['det_name'] = json_decode($data['det_name'],true);
                foreach ($data['det_img'] as $key => $val){
                    $data['more'][$key]['url'] = $data['det_img'][$key];
                    $data['more'][$key]['name'] = $data['det_name'][$key];
                }
            }
            $data_gradeall = Db::name('gradeall ') -> select();
            $this -> assign('data_gradeall',$data_gradeall);
            $this -> assign('data',$data);
        }

        $classif_data = Db::name('classification') -> select();
        $this -> assign('classif_data',$classif_data);
        $label_data = Db::name('label') -> select();
        $this -> assign('label_data',$label_data);
        $goods_label_data = Db::name('goods_label') -> where('goods_id',$id['id']) -> column('label_id');
        $this -> assign('goods_label_data',$goods_label_data);
        return $this -> fetch();

    }

    /**
     * 平台商品下架
     */
    public function sold_out(){

        $id = $_POST['id'];
        $data['id'] = $id;
        $data['is_out'] = 0;
        $res = Db::name('Goods') -> update($data);
        if($res){
            return true;
        }else{
            return false;
        }

    }

    /**
     * 平台商品上架
     */
    public function putaway(){

        $id = $_POST['id'];
        $data['id'] = $id;
        $data['is_out'] = 1;
        $res = Db::name('Goods') -> update($data);
        if($res){
            return true;
        }else{
            return false;
        }

    }

    /**
     * 平台商品删除
     */
    public function goods_del(){

        $id = $_POST['id'];
        $data = Db::name('goods') -> delete($id);
        Db::name('goods_label') -> where('goods_id',$id) -> delete();
        Db::name('collect') -> where("goods_id = ".$id) -> delete();
        if($data){
            return true;
        }else{
            return false;
        }

    }

    /**
     * 推荐新品
     */
    public function goods_new(){

        if($this -> request -> isPost()){
            $ids = $_POST['ids'];
            $arr_id = explode(',',$ids);
            $data = Db::name('goods') -> where('recommend_new',1) -> select();
            if(!empty($data)){
                foreach ($data as $key => $val){
                    Db::name("goods") -> where('id',$val['id']) -> update(["recommend_new"=>0]);
                }
            }
            foreach ($arr_id as $key => $val){
                Db::name("goods") -> where('id',$arr_id[$key]) -> update(["recommend_new"=>1]);
            }
            return true;
        }else{
            $data = Db::name('goods') -> where('is_out =1 and type =1') -> select() -> toArray();
            foreach ($data as $key => $val){
                $class_data = Db::name('classification') -> where('id',$val['classify_id']) -> find();
                $data[$key]['classify_name'] = $class_data['name'];
            }
            $this -> assign('data',$data);
            return $this -> fetch();
        }

    }

    /**
     * 推荐热门
     */
    public function goods_hot(){

        if($this -> request -> isPost()){
            $ids = $_POST['ids'];
            $arr_id = explode(',',$ids);
            $data = Db::name('Goods') -> where('recommend_hot',1) -> select();
            if(!empty($data)){
                foreach ($data as $key => $val){
                    Db::name("Goods") -> where('id',$val['id']) -> update(["recommend_hot"=>0]);
                }
            }
            foreach ($arr_id as $key => $val){
                Db::name("Goods") -> where('id',$arr_id[$key]) -> update(["recommend_hot"=>1]);
            }
            return true;
        }else{
            $data = Db::name('Goods') -> where('is_out =1 and type =1') -> select() -> toArray();
            foreach ($data as $key => $val){
                $class_data = Db::name('classification') -> where('id',$val['classify_id']) -> find();
                $data[$key]['classify_name'] = $class_data['name'];
            }
            $this -> assign('data',$data);
            return $this -> fetch();
        }

    }
























}