Floor.php 4.0 KB
<?php

namespace app\school\controller\project_appointment;

use app\common\controller\DoubleBase;

use think\Db;
use think\Exception;
use think\exception\PDOException;
use think\exception\ValidateException;
use app\school\model\project_appointment\Building as BuildingModel;
use app\school\model\project_appointment\Floor as FloorModel;

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

    /**
     * 楼栋index
     * @return array|\think\response\Json
     * @author cdd
     */
//    public function index()
//    {
//        //设置过滤方法
//        $this->request->filter(['strip_tags', 'trim']);
//        if ($this->request->isAjax()) {
//            list($where, $sort, $order, $offset, $limit) = $this->buildparams(null,true);
//            $list = $this->model
//                ->alias('f')
//                ->field('f.*,b.building_name')
//                ->join('project_building b','f.building_id = b.id','left')
//                ->where($where)
//                ->order('f.createtime', $order)
//                ->paginate($limit);
//            $result = array("total" => $list->total(), "rows" => $list->items());
//            return json($result);
//        }
//        return $this->view->fetch();
//    }

    /**
     * 添加
     */
    public function add()
    {
        if ($this->request->isPost()) {
            $params = $this->request->post("row/a");
            if ($params) {
                $params = $this->preExcludeFields($params);
                if($this->model->where(['building_id' => $params['building_id'],'floor_no'=>$params['floor_no']])->find()) {
                    $this->error('该楼层编号已经存在');
                }
                $params['school_id'] = SCHOOL_ID;
                Db::startTrans();
                try {
                    $result = $this->model->allowField(true)->save($params);
                    Db::commit();
                } catch (Exception $e) {
                    Db::rollback();
                    $this->error($e->getMessage());
                }
                $this->success();
            }
            $this->error(__('Parameter %s can not be empty', ''));
        }
        $buildList = (new BuildingModel)->where(['school_id' => SCHOOL_ID])->select();
        $this->assign('build_list',$buildList ? $buildList : []);
        return $this->view->fetch();
    }

    /**
     * 编辑
     */
    public function edit($ids = null)
    {
        if(empty($ids)){
            $ids = $this->request->param('id');
        }
        $row = $this->model->get($ids);
        if (!$row) {
            $this->error(__('No Results were found'));
        }
        if ($this->request->isPost()) {

            $params = $this->request->post("row/a");
            if ($params) {
                $params = $this->preExcludeFields($params);
                $params['school_id'] = SCHOOL_ID;
                if($this->model->where(['floor_no' => $params['floor_no'],'building_id' => $params['building_id']])->where('id','neq',$row['id'])->find()) {
                    $this->error('该楼宇编号已经存在');
                }
                Db::startTrans();
                try {
                    $result = $row->allowField(true)->save($params);
                    Db::commit();
                }  catch (Exception $e) {
                    Db::rollback();
                    $this->error($e->getMessage());
                }
                $this->success();
            }
            $this->error(__('Parameter %s can not be empty', ''));
        }
        $buildList = (new BuildingModel)->where(['school_id' => SCHOOL_ID])->select();
        $this->assign('build_list',$buildList ? $buildList : []);
        $this->view->assign("row", $row); //选中的数据
        $this->assignconfig('id',$ids);
        return $this->view->fetch();
    }


}