EditController.class.php 7.99 KB
<?php
/**
 * 【调研中心-后台】10_编辑调研
 * EditController.class.php
 * CreateBy:dj
 * Date:2017-03-06
 */

namespace Apicp\Controller\Questionnaire;

use Common\Common\AttachOperation;
use Common\Common\StudyMap;
use Common\Common\TaskCenter;
use Common\Common\Train;
use Common\Service\BaseinfoService;
use Common\Service\QuestionService;
use Common\Service\RightService;

class EditController 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_id = rintval($data['qu_id']);
        if (empty($qu_id)) {

            E('_EMPTY_BASEINFO_ID');
        }

        // 【对接任务】0907新增任务中心使用判断
        $taskCenter = &TaskCenter::instance();
        // 查询调研是否被任务模块使用
        $used_ids = $taskCenter->checkAppDataUsed($qu_id);
        if (!empty($used_ids)) {

            E('_ERR_QUESTIONNAIRE_USED_TASK_EDIT');
        }

        // 查询调研是否被线下培训模块使用
        $train = &Train::instance();
        $train_used_ids = $train->checkUsing($qu_id);
        if (!empty($train_used_ids)) {

            E('_ERR_QUESTIONNAIRE_USED_TRAIN_EDIT');
        }

        // 查询是否被学习地图模块使用
        if (StudyMap::checkAppDataUsed([$qu_id])) {
            E('_ERR_QUESTIONNAIRE_USED_MAP_EDIT');
        }

        // 调研类型:常规类型、任务类、线下培训、学习地图
        $qu_types = [
            BaseinfoService::ROUTINE_TYPE,
            BaseinfoService::TASK_TYPE,
            BaseinfoService::OFF_LINE_TYPE,
            BaseinfoService::OTHER_TYPE
        ];
        if (!in_array($data['qu_type'], $qu_types)) {

            E('_EMPTY_QU_TYPE');
        }

        // 非常规类型:任务类型或者线下培训类型
        if (BaseinfoService::ROUTINE_TYPE != $data['qu_type']) {
            // 跳转编辑新类型
            $this->new_type($qu_id, $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_RIGHT_SAVE_FALI');
        }

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

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

            // 修改基本信息
            $this->_baseinfo_s->update($qu_id, $base_data);
            // 删除之前存入的问题信息
            $this->_question_s->delete_by_conds(['qu_id' => $qu_id]);

            // 组装问题信息表入库数据
            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']) && $data['is_all'] == BaseinfoService::IS_ALL_NO) {
                // 删除之前的权限记录
                $this->_right_s->delete_by_conds(['qu_id' => $qu_id]);

                // 验证并组装权限表入库数据
                $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' => 1, // 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->update_attach(
                APP_DIR,
                'baseinfo',
                $qu_id,
                ['attach_ids' => $attach_ids]
            );
        }

        return true;
    }

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

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

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

            // 修改基本信息
            $this->_baseinfo_s->update($qu_id, $base_data);
            // 删除之前存入的问题信息
            $this->_question_s->delete_by_conds(['qu_id' => $qu_id]);

            // 组装问题信息表入库数据
            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]
            );
        }

        return true;
    }
}