BuildingEnter.php 10.3 KB
<?php

namespace app\school\controller\project_appointment;

use app\common\controller\DoubleBase;

use app\school\model\project_appointment\BuildingEnterLog;
use app\school\controller\basic\User;
use think\Db;
use think\Exception;
use think\exception\PDOException;
use think\exception\ValidateException;
use app\school\model\project_appointment\BuildingEnter as BuildingEnterModel;
use app\school\model\project_appointment\RoomOrder;
use app\school\model\project_appointment\Room;
use think\Url;

/**
 * 楼栋管理
 */
class BuildingEnter extends DoubleBase
{
    protected $noNeedRight = ['*'];
    protected $model = null;
    public function _initialize()
    {
        parent::_initialize();
        $this->model = new BuildingEnterModel();
    }

    public function index()
    {
        //设置过滤方法
        $this->request->filter(['strip_tags', 'trim']);
        if ($this->request->isAjax()) {
            list($where, $sort, $order, $offset, $limit) = $this->buildparams();
            $list = $this->model
                ->field('en.*,u.username,u.mobile,s.name as school_name,c.college_name')
                ->alias('en')
                ->join('user u','en.user_id = u.id','left')
                ->join('manystore_shop s','en.school_id = s.id','left')
                ->join('college c','en.college_id = c.id','left')
                ->where(['en.school_id' => SCHOOL_ID])
                ->order('en.createtime', 'desc')
                ->paginate($limit);
//            dump(collection($list)->toArray());die;
            $result = array("total" => $list->total(), "rows" => $list->items());
            return json($result);
        }
        return $this->view->fetch();
    }

    public function room_allocation($id='')
    {
        if(empty($id)){
            $id = $this->request->param('id');
        }
        if ($this->request->isPost()) {
            $params = $this->request->post("row/a");
            if ($params) {
                $roomModel = new Room();
                $roomInfo = $roomModel->where(['id'=>$params['room_id']])->find();
                if(!$roomInfo) {
                    $this->error('该房间不存在或已下线');
                }
                if($roomInfo['is_use'] == Room::IS_USE_Y) {
                    $this->error('该房间已经被分配,请选择其他房间');
                }
                $params['school_id'] = SCHOOL_ID;
                Db::startTrans();
                try {
                    //修改房间状态被使用
                    $roomInfo->is_use = Room::IS_USE_Y;
                    $roomInfo->save();
                    //修改审核状态为审核通过
                    $enter = $this->model->where(['id' => $params['enter_id']])->find();
                    $enter['status'] = Room::IS_USE_Y;
                    $enter->save();
                    //添加房间预约记录
                    $roomOrder = new RoomOrder();
                    if($params['term_type'] == 1) {
                        $endTime = strtotime(date('Y-m-d',strtotime('+6 month')));
                    }else if($params['term_type'] == 2) {
                        $endTime = strtotime(date('Y-m-d',strtotime('+12 month')));
                    }else if($params['term_type'] == 3){
                        $endTime = strtotime(date('Y-m-d',strtotime('+24 month')));
                    }else {
                        $endTime = strtotime($params['end_time']);
                    }
                    $save = [
                        'user_id'       => $enter['user_id'],
                        'room_id'       => $params['room_id'],
                        'apply_date'    => $params['apply_date'],
                        'start_time'    => strtotime(date('Y-m-d',strtotime('+7 day'))),
                        'end_time'      => $endTime,
                        'use_count'     => $enter['station_num'],
                        'order_source'  => Room::IS_USE_Y,
                        'enter_id'      => $params['enter_id'],
                    ];
                    $log = [
                        'enter_id'  => $params['enter_id'],
                        'log_type'  => BuildingEnterLog::AGREE,
                        'createtime'=> time(),
                    ];
                    $enterLogModel  = new BuildingEnterLog();
                    $enterLogModel->allowField(true)->save($log);

                    $result = (new RoomOrder())->allowField(true)->save($save);
                    Db::commit();
                } catch (Exception $e) {
                    Db::rollback();
                    $this->error($e->getMessage());
                }
                $this->success('审核成功','project_appointment/building_enter/index');
            }
            $this->error(__('Parameter %s can not be empty', ''));
        }
        $timeLimit = BuildingEnterModel::TIME_LIMIT;
//        dump($timeLimit);die;
        $this->assign('id',$id);
        $this->assign('timeLimit',$timeLimit);
        return $this->view->fetch();
    }

    /**
     * 入驻申请详情
     * @param $id
     */
    public function enter_detail($id)
    {
        $enterId = $this->request->param('id');
        $enterInfo = $this->model
            ->field('en.*,u.*,c.college_name,s.specialty_name,z.clazz_name')
            ->alias('en')
            ->join('user u','en.user_id = u.id','left')
            ->join('college c','u.college = c.id','left')
            ->join('specialty s','u.specialty = s.id','left')
            ->join('clazz z','u.clazz = z.id','left')
            ->where(['en.id' => $enterId])
            ->find();
        $this->assignconfig('enter_id', $enterId);
        $this->assign('row', $enterInfo);
        return $this->fetch();
    }

    /**
     * //入驻成员列表
     */
    public function get_members()
    {
        if ($this->request->isAjax()) {
            $id = $this->request->param('id');
            $userIds = $this->model->where(['id' => $id])->value('enter_student_ids');
            $rows = (new \app\school\model\basic\User())
                ->alias('a')
                ->where(['id' => ['IN', $userIds]])
                ->select();
            $res = [
                'total' => count($rows),
                'rows'  => $rows
            ];
            return $res;
        }
    }

    //入驻指导教师
    public function get_teachers()
    {
        if ($this->request->isAjax()) {
            $id = $this->request->param('id');
            $teachers = $this->model->where(['id' => $id])->value('enter_teacher_ids');
            $rows = (new \app\school\model\basic\Teacher())
                ->alias('a')
                ->where(['id' => ['IN', $teachers]])
                ->select();
            $res = [
                'total' => count($rows),
                'rows'  => $rows
            ];
            return $res;
        }
    }


    //审核驳回
    public function reject()
    {
        $id  = $this->request->param('id');
        $info = $this->model->where(['id' => $id])->find();
        $enterLogModel  = new BuildingEnterLog();
        Db::startTrans();
        try{
            $info->status = 3;
            $info->save();
            $log = [
                'enter_id'  => $id,
                'log_type'  => BuildingEnterLog::REJECT,
                'createtime'=> time(),
            ];
            $enterLogModel->allowField(true)->save($log);

            Db::commit();
        } catch (\Exception $e) {
            // 回滚事务
            Db::rollback();
            $this->error('驳回失败,稍后请重试');
        }
        $this->success('成功');
    }

    public function company_index()
    {
        $this->request->filter(['strip_tags', 'trim']);
        if ($this->request->isAjax()) {
            list($where, $sort, $order, $offset, $limit) = $this->buildparams();
            $list = $this->model
                ->alias('en')
                ->field('en.*,p.end_time,p.createtime,p.start_time,p.room_id,r.room_name,c.college_name,s.name as school_name,u.username,u.mobile,b.building_name,f.floor_name')
//                ->field('en.*,u.username,u.mobile,s.name as school_name,c.college_name,p.end_time,p.start_time')
                ->join('project_room_order p','en.id = p.enter_id','left')
                ->join('project_room r','p.room_id = r.id','left')
                ->join('college c','en.college_id = c.id','left')
                ->join('manystore_shop s','en.school_id = s.id','left')
                ->join('project_building b','r.building_id = b.id','left')
                ->join('project_floor f','r.floor_id = f.id','left')
                ->join('user u','en.user_id = u.id','left')
                ->where(['en.status' => 2,'en.school_id' => SCHOOL_ID])
                ->order('en.createtime', 'desc')
                ->paginate($limit);
            if($list) {
                foreach ($list as &$value) {
                    $value['expire_type'] = '未到期';
                    if(isset($value['end_time'])) {
                        if(time() > $value['end_time']) {
                            $value['expire_type'] = '已到期';
                        }
                    }else{
                        $value['expire_type'] = '已到期';
                    }
                }
            }
            $result = array("total" => $list->total(), "rows" => $list->items());
            return json($result);
        }
        return $this->view->fetch();
    }


    //强制到期
    public function expire()
    {
        $id  = $this->request->param('id');
        $roomOrder = (new RoomOrder())->where(['enter_id'=>$id])->find();
        if(!$roomOrder) {
            $this->error('该记录不存在');
        }
        $roomInfo = (new Room())->where(['id' => $roomOrder['room_id']])->find();
        Db::startTrans();
        try {
            $roomOrder->start_time = 0;
            $roomOrder->end_time = 0;
            $roomOrder->save();
            $roomOrder->delete();
            $roomInfo->is_use = 1;
            $log = [
                'enter_id'  => $id,
                'log_type'  => BuildingEnterLog::EXPIRE,
                'createtime'=> time(),
            ];
            $enterLogModel  = new BuildingEnterLog();
            $enterLogModel->allowField(true)->save($log);
            $roomInfo->save();
            Db::commit();
        } catch (Exception $e) {
            Db::rollback();
            $this->error($e->getMessage());
        }
        $this->success('成功');
    }
}