ZjServiceController.php 6.4 KB
<?php
/**
 * Created by PhpStorm.
 * User: wz
 * Date: 2018/6/29
 * Time: 15:25
 */

namespace app\admin\controller;


use cmf\controller\AdminBaseController;
use Think\Db;

class ZjServiceController extends AdminBaseController
{
    /**
     * 后台服务列表
     */
    public function index()
    {
        $all=Db::name('zj_service')->alias('s')->join('zj_service_type t','s.ser_type=t.id')
            ->field('s.id,s.ser_type,s.name,s.price_ori,s.price_new,s.describes,s.apply,s.create_time,s.update_time,s.is_dis,s.is_rec,t.name as tname');
        if ($this->request->isPost()){
            $arr=input('post.');
            //搜索条件
            if($arr['ser_type']=='0'||$arr['ser_type']=='1'||$arr['ser_type']=='2'){
                $all=$all->where('ser_type',$arr['ser_type']);
            }
            if($arr['apply']=='0'||$arr['apply']=='1'||$arr['apply']=='2'||$arr['apply']=='3'){
                $all=$all->where('apply',$arr['apply']);
            }
            if (is_numeric($arr['price_max'])&&is_numeric($arr['price_min'])&&$arr['price_min']<=$arr['price_max']){
                    $all=$all->where('price_new','>=',$arr['price_min']);
                    $all=$all->where('price_new','<=',$arr['price_max']);
            }
        }

        $all=$all->order('id','asc')->paginate(10);
        $ser=Db::name('zj_service_type')->select();
        $this->assign('ser',$ser);
        $this->assign('allser',$all);
        return $this->fetch();
    }
    /**
     *添加服务
     */
    public function add()
    {
        $ser=Db::name('zj_service_type')->select();
        $this->assign('ser',$ser);
        return $this->fetch();
    }

    /**
     *添加服务提交
     */
    public function addPost()
    {
        if ($this->request->isPost()){
            $arr=input('post.');
            $name=Db::name('zj_service')->field('name')->where('name',$arr['name'])->find();
            if ($name){
                $this->error('服务名称已存在');
            }else{
                if (is_numeric($arr['price_ori'])&&is_numeric($arr['price_new'])){
                    if(empty($arr['content'])){
                        $this->error('详情不能为空');
                    }else{
                        $arr['create_time']=time();
                        $arr['update_time']=time();
                        if (Db::name('zj_service')->insert($arr)){
                            $this->success('添加成功',url('zj_service/index'));
                        }else{
                            $this->error('添加失败');
                        }
                    }
                }else{
                    $this->error('价格请输入数字内容');
                }
            }

        }
    }

    /**
     * 编辑服务
     */
    public function edit()
    {
        if ($this->request->param()){
            $id=input('param.id');
            $one=Db::name('zj_service')->where('id',$id)->find();
            $ser=Db::name('zj_service_type')->select();
            $this->assign('ser',$ser);
            $this->assign('one',$one);
            return $this->fetch();
        }
    }

    /**
     * 编辑服务提交
     */
    public function editPost()
    {
        if ($this->request->isPost()){
            $arr=input('post.');
                if (is_numeric($arr['price_ori'])&&is_numeric($arr['price_new'])){
                    if(empty($arr['content'])){
                        $this->error('详情不能为空');
                    }else{
                        $arr['update_time']=time();
                        if (Db::name('zj_service')->where('id',$arr['id'])->update($arr)){
                            $this->success('修改成功',url('zj_service/index'));
                        }else{
                            $this->error('修改失败');
                        }
                    }
                }else{
                    $this->error('价格请输入数字内容');
                }
        }
    }

    /**
     * 服务发布与取消发布
     */
    public function publish()
    {
        $param           = $this->request->param();
        if (isset($param['ids']) && isset($param["yes"])) {
            $ids = $this->request->param('ids/a');

            $yes=Db::name('zj_service')->where(['id' => ['in', $ids]])->update(['is_dis' => 1]);

                $this->success("发布成功!", '');
        }

        if (isset($param['ids']) && isset($param["no"])) {
            $ids = $this->request->param('ids/a');

            $yes=Db::name('zj_service')->where(['id' => ['in', $ids]])->update(['is_dis' => 0]);

                $this->success("取消发布成功!", '');
        }

    }
    /**
     * 服务推荐与取消
     */
    public function recommend()
    {
        $param           = $this->request->param();

        if (isset($param['ids']) && isset($param["yes"])) {
            $ids = $this->request->param('ids/a');

            $yes=Db::name('zj_service')->where(['id' => ['in', $ids]])->update(['is_rec' => 1]);

                $this->success("推荐成功!", '');
        }
        if (isset($param['ids']) && isset($param["no"])) {
            $ids = $this->request->param('ids/a');

            $yes=Db::name('zj_service')->where(['id' => ['in', $ids]])->update(['is_rec' => 0]);

                $this->success("取消推荐成功!", '');
        }
    }

    /**
     * 服务删除
     */
    public function delete()
    {
        $param           = $this->request->param();

        if (isset($param['id'])) {
            $id           = $this->request->param('id');
            $del=Db::name('zj_service')->where('id',$id)->delete();

            $this->success("删除成功!", '');

        }

        if (isset($param['ids'])) {
            $ids     = $this->request->param('ids/a');
            $del=Db::name('zj_service')->where(['id' => ['in', $ids]])->delete();
            $this->success("删除成功!", '');
            }
        }

        /**
         *添加服务分类
         */
        public function addType()
        {
            if ($this->request->param()){
                $name=input('param.name');
                $type=Db::name('zj_service_type')->insert(['name'=>$name]);
                if ($type){
                    $this->success('增加分类成功','');
                }else{
                    $this->error('增加分类失败');
                }
            }
            return $this->fetch();
        }

}