AddController.class.php 6.85 KB
<?php
/**
 * 【调研中心-后台】08_新增调研
 * AddController.class.php
 * CreateBy:dj
 * Date:2017-03-03
 */

namespace Apicp\Controller\Questionnaire;

use Common\Common\AttachOperation;
use Common\Service\BaseinfoService;
use Common\Service\QuestionService;
use Common\Service\RightService;

class AddController extends \Apicp\Controller\AbstractController
{

    /** @var BaseinfoService */
    protected $_baseinfo_s;
    /** @var QuestionService */
    protected $_question_s;
    /** @var RightService */
    protected $_right_s;

    public function before_action($action = '')
    {

        if (!parent::before_action($action)) {

            return false;
        }

        // 调研基本信息表
        $this->_baseinfo_s = new BaseinfoService();
        // 调研问题信息表
        $this->_question_s = new QuestionService();
        // 可见范围信息表
        $this->_right_s = new RightService();

        return true;
    }

    /**
     * @return bool
     * @throws \VcySDK\Exception
     */
    public function Index_post()
    {

        // 获取参数
        $data = I('post.');

        // 调研类型:常规类型、任务类、线下培训、其他类型
        $qu_types = [
            BaseinfoService::ROUTINE_TYPE,
            BaseinfoService::TASK_TYPE,
            BaseinfoService::OFF_LINE_TYPE,
            BaseinfoService::OTHER_TYPE
        ];
        // 验证调研类型
        if (!in_array($data['qu_type'], $qu_types)) {
            // 调研类型不符合规范
            E('_ERR_QU_TYPE');
        }

        // 非常规类型:任务类型或者线下培训类型
        if (BaseinfoService::ROUTINE_TYPE != $data['qu_type']) {
            // 跳转新增类型
            $this->new_type($data);

            return true;
        }

        $base_data = $this->_baseinfo_s->checkData($data);
        // 验证并组装基本信息
        if (!$base_data) {

            return false;
        }

        // 验证问题信息
        if (!$this->_question_s->checkData($data['q_list'])) {

            return false;
        }

        // 获取全部可填写人数
        $involved_total = $this->_right_s->user_count($data);
        if (empty($involved_total)) {

            E('_ERR_INVOLVED_TOTAL_GET_FAIL');
        }

        // 全部人数
        $base_data['involved_total'] = $involved_total;
        // 已填写人数
        $base_data['involved_num'] = 0;

        $qu_id = 0;

        try {
            // 开始事务
            $this->_baseinfo_s->start_trans();

            // 插入基本信息
            $qu_id = $this->_baseinfo_s->insert($base_data);

            // 组装问题信息表入库数据
            list($qc_data, $attach_ids) = $this->_question_s->create_data($qu_id, $data['q_list']);
            // 插入问题信息表
            $this->_question_s->insert_all($qc_data);

            // 如果是指定人可见,则组装插入权限数据
            if (is_numeric($data['is_all']) && BaseinfoService::IS_ALL_NO == $data['is_all']) {
                // 验证并组装权限表入库数据
                $qx_data = $this->_right_s->create_data($qu_id, $data['right']);
                // 插入权限表信息
                $this->_right_s->insert_all($qx_data);
            }

            // 调研状态:预发布、发布
            if (BaseinfoService::PRE_PUBLISH_STATUS == $base_data['release_status']
                || BaseinfoService::PUBLISH_STATUS == $base_data['release_status']
            ) {

                $cron_params = [
                    'type' => 0, // 0:添加 1:修改
                    'qu_id' => $qu_id,
                ];
                // 添加定时任务
                $this->_baseinfo_s->cron_add($cron_params);
            }

            if (BaseinfoService::PUBLISH_STATUS == $base_data['release_status'] &&
                BaseinfoService::NOCTICE_YES == $base_data['is_notice']) {
                // 如果为发布状态,且开启发布时发消息则推送消息给用户
                $data = $this->_baseinfo_s->get($qu_id);
                $this->_baseinfo_s->sendmsg_release($data);
            }

            // 提交事务
            $this->_baseinfo_s->commit();
        } catch (\Think\Exception $e) {

            \Think\Log::record($e);
            // 事务回滚
            $this->_set_error($e->getMessage(), $e->getCode());
            $this->_baseinfo_s->rollback();
        } catch (\Exception $e) {

            \Think\Log::record($e);
            $this->_set_error($e->getMessage(), $e->getCode());
            // 事务回滚
            $this->_baseinfo_s->rollback();
        }

        // 附件处理
        if (!empty($attach_ids)) {
            $attach_serv = new AttachOperation();
            $attach_serv->insert_attach(
                APP_DIR,
                'baseinfo',
                $qu_id,
                ['attach_ids' => $attach_ids]
            );
        }

        $this->_result = ['qu_id' => $qu_id];

        return true;
    }

    /**
     * 新增类型处理
     *
     * @param array $data 调研基本信息
     *
     * @return array|bool
     *
     * @throws \VcySDK\Exception
     */
    protected function new_type($data = [])
    {

        // 验证并组装基本信息
        $base_data = $this->_baseinfo_s->check_BaseInfoData($data);
        // 验证问题信息
        $this->_question_s->checkData($data['q_list']);

        try {
            // 开始事务
            $this->_baseinfo_s->start_trans();

            // 插入基本信息
            $qu_id = $this->_baseinfo_s->insert($base_data);
            // 组装问题信息表入库数据
            list($qc_data, $attach_ids) = $this->_question_s->create_data($qu_id, $data['q_list']);
            // 插入问题信息表
            $this->_question_s->insert_all($qc_data);

            // 提交事务
            $this->_baseinfo_s->commit();
        } catch (\Think\Exception $e) {

            \Think\Log::record($e);
            // 事务回滚
            $this->_set_error($e->getMessage(), $e->getCode());
            $this->_baseinfo_s->rollback();
        } catch (\Exception $e) {

            \Think\Log::record($e);
            $this->_set_error($e->getMessage(), $e->getCode());
            // 事务回滚
            $this->_baseinfo_s->rollback();
        }

        // 附件处理
        if (!empty($attach_ids)) {
            $attach_serv = new AttachOperation();
            $attach_serv->update_attach(
                APP_DIR,
                'baseinfo',
                $qu_id,
                ['attach_ids' => $attach_ids]
            );
        }

        $this->_result = ['qu_id' => $qu_id];

        return true;
    }
}