AppointmentRule.php 3.2 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\AppointmentRule as AppointmentRuleModel;

/**
 * 基地入驻规则管理
 */
class AppointmentRule extends DoubleBase
{
    protected $noNeedRight = ['*'];
    protected $model = null;
    public function _initialize()
    {
        parent::_initialize();
        $this->model = new AppointmentRuleModel();
    }

    /**
     * 添加
     */
    public function add()
    {
        if ($this->request->isPost()) {
            $params = $this->request->post("row/a");
            if ($params) {
                $params = $this->preExcludeFields($params);
                if($this->model->where(['school_id' => SCHOOL_ID])->find()) {
                    $this->error('入驻规则已存在不能重复上传');
                }
                if(!isset($params['image_url'])) {
                    $this->error('请上传入驻流程图');
                }
                if(!isset($params['data_url'])) {
                    $this->error('请上传入驻资料');
                }
                $params['data_url'] = json_encode($params['data_url']);
                $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', ''));
        }
        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(!isset($params['image_url'])) {
                    $this->error('请上传入驻流程图');
                }
                if(!isset($params['data_url'])) {
                    $this->error('请上传入驻资料');
                }
                $params['data_url'] = json_encode($params['data_url']);
                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', ''));
        }
        $row['data_url'] = json_decode($row['data_url'],true);
        $this->view->assign("row", $row); //选中的数据
        $this->assignconfig('id',$ids);
        return $this->view->fetch();
    }


}