ProjectRoomOrder.php 2.3 KB
<?php

namespace app\index\controller;

use app\common\controller\Frontend;
use app\index\model\ProjectRoomOrder as ProjectRoomOrderModel;
use think\Db;
use think\Exception;

class ProjectRoomOrder extends Frontend
{

    protected $noNeedLogin = '*';
    protected $noNeedRight = '*';
    protected $layout = 'default';
    protected $roomOrderModel = null;
    public function _initialize()
    {
        $this->roomOrderModel = new ProjectRoomOrderModel();
        parent::_initialize(); // TODO: Change the autogenerated stub
    }

    /**
     * @return string
     * @throws \think\Exception
     * 房间预约
     */
    public function index()
    {
        $user = $this->auth->getUser();
        $param = $this->request->param();
        $where = ['o.user_id'=>["=",$user['id']]];
        $status = !empty($param['status']) ? $param['status'] : 0;
        switch ($status){
            //查询所有
            case 0:
                $where['o.status'] = ["between",[1,4]];
                break;
            default:
                $where['o.status'] = ["=", $status];
        }
        $list = Db::name('project_room_order')
            ->field('o.*,r.room_name')
            ->alias('o')
            ->join('project_room r','o.room_id = r.id','left')
            ->where($where)
            ->order('o.createtime','desc')
            ->paginate(10,false,['query'=>request()->param()]);
        $this->assign("list",$list);
        $this->assign("status",$status);
        $this->assign("page",$list->render());
        return $this->view->fetch();
    }

    public function update(){
        $param = $this->request->param();
        $info = Db::name("project_room_order")->where('id',$param['id'])->find();
        if(!$info) {
            $this->error("预约记录不存在");
        }
        Db::startTrans();
        try {
            if($param['status'] == 1) {
                $status = Db::name('project_room_order')->where(['id'=>$param['id']])->update(['status' => 4]);
            }
            $result = Db::name("project_room_order")->where('id',$param['id'])->delete();
            Db::commit();
        } catch (Exception $e) {
            Db::rollback();
            $this->error("操作失败,请重试");
        }
        $this->success("操作成功");
    }
}