Enterform.php 8.6 KB
<?php

namespace app\school\controller\setting;

use app\common\controller\DoubleBase;
use app\school\model\setting\ProjectContainer;
use think\Db;
use think\Exception;
use think\exception\PDOException;
use think\exception\ValidateException;

class Enterform extends DoubleBase
{
    protected $containerId = null;

    public function _initialize()
    {
        parent::_initialize();
        $this->containerId = $_COOKIE['container_id'];
    }

    public function index() {

        if ($this->request->isAjax()) {
            list($where, $sort, $order, $offset, $limit) = $this->buildparams();
            $formField = ['id', 'school_id', 'project_type_id', 'stage_title', 'stage_type', 'icon_image'];

            $total = Db::name('project_form_set')->where($where)->count();
            $data = Db::name('project_form_set')
                ->field($formField)
                ->where($where)
                ->limit($offset, $limit)
                ->select();

            $res = [
                'total' => $total,
                'rows'  => $data
            ];

            return $res;
        }

        $projectTypesWhere = ['container_id' => $this->containerId, 'deletetime' => null];
        $projectTypes = Db::name('project_type')->where($projectTypesWhere)->select();
        // 项目申报开启到哪个阶段了(0:未开启;1:立项;2:中期;3:结题)
        $applyStatus = (new ProjectContainer())->where(['id' => $this->containerId])->value('apply_status_log');
        $container = (new ProjectContainer())->where(['id' => $this->containerId])->find();

        if (!$projectTypes) {
            $this->error('当前容器未设置项目类型,请先设置');
        }

        $this->assign('project_types', $projectTypes);
        $this->assign('can_edit', $applyStatus == 0);
        $this->assignconfig('apply_status', $applyStatus);
        $this->assign('container', $container);
        return $this->fetch();
    }

    public function edit($ids = NULL) {
        if ($this->request->isAjax()) {
            $rows = $this->request->param('row/a');
            $containerInfo = ProjectContainer::get($this->containerId);
            $formInfo = $this->getFormSetData($rows['id']);
            switch ($formInfo['stage_type'])
            {
                case 'meta_check':
                    $containerStartTime     = $containerInfo['midterm_review_starttime'];
                    $containerEndTime       = $containerInfo['midterm_review_endtime'];
                    $messgae = '中期检查';
                    break;
                case 'conclusion':
                    $containerStartTime     = $containerInfo['conclusion_starttime'];
                    $containerEndTime       = $containerInfo['conclusion_endtime'];
                    $messgae = '项目结题';
                    break;
                default:
                    $containerStartTime     = $containerInfo['project_starttime'];
                    $containerEndTime       = $containerInfo['project_endtime'];
                    $messgae = '项目';
            }

            $data = [];
            $data['stage_title'] = $rows['stage_title'];
            unset($rows['stage_title']);
            $data['can_edit_data'] = $rows['can_edit_data'];
            unset($rows['can_edit_data']);
            $data['project_starttime'] = empty($rows['project_starttime']) ? NULL : strtotime($rows['project_starttime']);
            unset($rows['project_starttime']);
            $data['project_endtime'] = empty($rows['project_endtime']) ? NULL : strtotime($rows['project_endtime']);
            unset($rows['project_endtime']);

            if($data['project_endtime'] && !$data['project_starttime']) {
                $this->error('请选择开始时间');
            }
            if($data['project_starttime'] && !$data['project_endtime']) {
                $this->error('请选择结束时间');
            }
            if($data['project_starttime'] && $data['project_endtime']) {
                if($data['project_starttime'] > $data['project_endtime']) {
                    $this->error('开始时间不能大于结束时间');
                }
                if($data['project_starttime'] > $containerEndTime) {
                    $this->error("开始时间不能大于 {$messgae}结束时间");
                }
            }
            $data['setting'] = json_encode($rows);
            Db::startTrans();
            try {
                Db::name('project_form_set')->where(['id' => $ids])->update($data);
                Db::commit();
            } catch (Exception $e) {
                Db::rollback();
                $this->error($e->getMessage());
            }
            $this->success('修改成功', '');
        }

        $row = $this->getFormSetData($ids);

        $this->assign('row', $row);
        return $this->fetch();
    }

    /**
     * 自定义表单
     */
    public function customForm() {
        $stageType = $this->request->param('type');
        $formSetId = $this->request->param('form_set_id');
        // 当前表单对应的项目阶段
        $applyNum = $this->request->param('apply_num');

        try {
            $fieldsData = Db::name('project_form_set')->where(['id' => $formSetId])->find();
        } catch (Exception $e) {
            $this->error($e->getMessage());
        }

        if ($this->request->isAjax()) {
            $data['fields_setting'] = $this->request->param('setting');
            Db::startTrans();
            try {
                Db::name('project_form_set')->where(['id' => $formSetId])->update($data);
                Db::commit();
            } catch (Exception $e) {
                Db::rollback();
                $this->error($e->getMessage());
            }
            $this->success('修改成功');
        }
        // 项目是否开启过申报(0: 没有; 1: 开启过)
        $applyStatus = (new ProjectContainer())->where(['id' => $this->containerId])->value('apply_status_log');

        $fieldsData = json_decode($fieldsData['fields_setting'], true);

        // 当前表单对应的项目阶段大于进行中的项目阶段时,表单可修改
        $this->assign('can_edit', $applyNum > $applyStatus);
        $this->assignconfig('form_type', $stageType);
        $this->assignconfig('fields_data', $fieldsData);
        $this->assignconfig('form_set_id', $formSetId);
        return $this->fetch();
    }

    /**
     * 获取项目报名设置数据
     * @return Array
     */
    private function getFormSetData($ids = NULL) {
        $row = Db::name('project_form_set')->where(['id' => $ids])->find();
        if (!$row) {
            $this->error('没有找到该条记录');
        }
        $row['setting'] = json_decode($row['setting'], true);
        return $row;
    }


    public function add()
    {
        $type_id = request()->param('type_id');
        $this->assign('type_id',$type_id);

        if ($this->request->isPost()) {
            $params = $this->request->post("row/a");
            if ($params) {
                $params = $this->preExcludeFields($params);

                if($this->storeIdFieldAutoFill && STORE_ID ){
                    $params['store_id'] = STORE_ID;
                }

                if($this->shopIdAutoCondition && SCHOOL_ID){
                    $params['school_id'] = SCHOOL_ID;
                }


                $result = false;
                Db::startTrans();
                try {
                    //是否采用模型验证
                    if ($this->modelValidate) {
                        $name = str_replace("\\model\\", "\\validate\\", get_class($this->model));
                        $validate = is_bool($this->modelValidate) ? ($this->modelSceneValidate ? $name . '.add' : $name) : $this->modelValidate;
                        $this->model->validateFailException(true)->validate($validate);
                    }
                    $result = $this->model->allowField(true)->save($params);
                    Db::commit();
                } catch (ValidateException $e) {
                    Db::rollback();
                    $this->error($e->getMessage());
                } catch (PDOException $e) {
                    Db::rollback();
                    $this->error($e->getMessage());
                } catch (Exception $e) {
                    Db::rollback();
                    $this->error($e->getMessage());
                }
                if ($result !== false) {
                    $this->success();
                } else {
                    $this->error(__('No rows were inserted'));
                }
            }
            $this->error(__('Parameter %s can not be empty', ''));
        }

        return $this->fetch();
    }
}