InstallController.class.php 9.09 KB
<?php
/**
 * 套件应用安装初始化数据
 * 问卷
 *
 * @author zhoutao
 * @date 2017-09-27 10:05:30
 */

namespace Rpc\Controller\Common;

use Common\Common\User;
use Common\Service\BaseinfoService;
use Common\Service\ClassifyService;
use Common\Service\QuestionService;
use Think\Exception;

class InstallController extends AbstractController
{

    // 免登录
    protected $_require_login = false;

    /** @var ClassifyService */
    protected $class_s;
    /** @var BaseinfoService */
    protected $base_info_s;
    /** @var QuestionService */
    protected $question_s;

    public function before_action($action = '')
    {

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

            return false;
        }

        // 实例化调研分类Service
        $this->class_s = new ClassifyService();
        // 实例化调研基础信息Service
        $this->base_info_s = new BaseinfoService();
        // 实例化调研问题Service
        $this->question_s = new QuestionService();

        return true;
    }

    public function Index()
    {

        // 查询是否存在默认分类
        $count = $this->class_s->count_by_conds(['is_system' => 1]);

        // 不存在默认分类
        if (!$count) {

            // 默认分类数据
            $default_class = [
                [
                    'qc_name' => '默认分类',
                    'is_system' => 1
                ],
                [
                    'qc_name' => '培训需求调研',
                    'is_system' => 0
                ],
                [
                    'qc_name' => '公司民意调查',
                    'is_system' => 0
                ],
                [
                    'qc_name' => '意见征集',
                    'is_system' => 0
                ]
            ];
            $this->class_s->insert_all($default_class);

            // 安装默认数据
            $this->default_data_install();
        }

        return true;
    }

    /**
     * 默认数据安装
     *
     * @return bool
     */
    private function default_data_install()
    {

        // 已有调研数
        $base_info_count = $this->base_info_s->count();
        // 无调研数据
        if (!$base_info_count) {
            // 实例化用户类
            $user_s = &User::instance();
            // 全部人员
            $result = $user_s->listByConds([]);
            // 全部人数(应参与总人数)
            $involved_total = $result['total'];

            // 获取培训需求调研的分类信息
            $class_info = $this->class_s->get_by_conds(['qc_name' => '培训需求调研']);

            try {
                $this->base_info_s->start_trans();

                // 组装调研基本信息数据
                $add_base_info = [
                    'qu_type' => 1, // 常规类型
                    'title' => '【系统预设】企业培训情况调研',
                    'introduce' => '本次调研旨在了解企业的基础培训信息,帮助企业搭建优质的培训实施系统,更好的实现员工赋能,提高员工战斗力。',
                    'qc_id' => $class_info['qc_id'],
                    'deadline' => 1917014400000, // [2030/10/01 00:00]
                    'secret' => 1, // 保密开启
                    'share' => 0, // 不可分享
                    'is_all' => 1, // 全公司
                    'anonymous' => 1, // 实名
                    'involved_total' => $involved_total, // 全部人数(应参与总人数)
                    'release_time' => MILLI_TIME, // 发布时间
                    'release_status' => 3, // 发布状态:发布
                    'integral_action_type' => 1, // 积分策略:启用默认
                    'integral_action_type' => 1, // 积分策略:启用默认
                    'updated' => MILLI_TIME // 最后更新时间
                ];
                // 写入调研基本信息默认数据
                $qu_id = $this->base_info_s->insert($add_base_info);

                // 组装调研问题默认数据
                $add_questions = [
                    [
                        'qu_id' => $qu_id,
                        'q_title' => '对于业务知识的掌握情况?',
                        'q_type' => 1, // 单选
                        'q_order' => 1,
                        'q_required' => 1, // 必填
                        'q_min' => 0,
                        'q_max' => 0,
                        'q_other' => 0, // 不包含其他选项
                        'q_other_title' => '',
                        'q_field' => serialize([
                            ['option' => '很好', 'is_check' => 0, 'img_options' => []],
                            ['option' => '一般', 'is_check' => 0, 'img_options' => []],
                            ['option' => '较差', 'is_check' => 0, 'img_options' => []],
                        ]),
                    ],
                    [
                        'qu_id' => $qu_id,
                        'q_title' => '对于自己工作中的执行力是否满意?',
                        'q_type' => 1,
                        'q_order' => 2,
                        'q_required' => 1,
                        'q_min' => 0,
                        'q_max' => 0,
                        'q_other' => 0,
                        'q_other_title' => '',
                        'q_field' => serialize([
                            ['option' => '很好', 'is_check' => 0, 'img_options' => []],
                            ['option' => '一般', 'is_check' => 0, 'img_options' => []],
                            ['option' => '较差', 'is_check' => 0, 'img_options' => []],
                        ]),
                    ],
                    [
                        'qu_id' => $qu_id,
                        'q_title' => '对于自己的沟通能力是否满意?',
                        'q_type' => 1,
                        'q_order' => 3,
                        'q_required' => 1,
                        'q_min' => 0,
                        'q_max' => 0,
                        'q_other' => 0,
                        'q_other_title' => '',
                        'q_field' => serialize([
                            ['option' => '很好', 'is_check' => 0, 'img_options' => []],
                            ['option' => '一般', 'is_check' => 0, 'img_options' => []],
                            ['option' => '较差', 'is_check' => 0, 'img_options' => []],
                        ]),
                    ],
                    [
                        'qu_id' => $qu_id,
                        'q_title' => '对于公司组织的员工培训是否满意?',
                        'q_type' => 1,
                        'q_order' => 4,
                        'q_required' => 1,
                        'q_min' => 0,
                        'q_max' => 0,
                        'q_other' => 1, // 包含其他选项
                        'q_other_title' => '其它',
                        'q_field' => serialize([
                            ['option' => '很好', 'is_check' => 0, 'img_options' => []],
                            ['option' => '一般', 'is_check' => 0, 'img_options' => []],
                            ['option' => '较差', 'is_check' => 0, 'img_options' => []],
                        ]),
                    ],
                    [
                        'qu_id' => $qu_id,
                        'q_title' => '对于哪一种培训方式最认可?',
                        'q_type' => 2, // 多选
                        'q_order' => 5,
                        'q_required' => 1,
                        'q_min' => 2,
                        'q_max' => 5,
                        'q_other' => 1,
                        'q_other_title' => '其它',
                        'q_field' => serialize([
                            ['option' => ' 线下封闭培训', 'is_check' => 0, 'img_options' => []],
                            ['option' => '线上移动化视频/语音学习', 'is_check' => 0, 'img_options' => []],
                            ['option' => '提供资料,自学成才', 'is_check' => 0, 'img_options' => []],
                            ['option' => '工作导师一对一指导', 'is_check' => 0, 'img_options' => []],
                        ]),
                    ],
                    [
                        'qu_id' => $qu_id,
                        'q_title' => '对于培训方式,培训内容有哪些好的建议?',
                        'q_type' => 5, // 多行文本
                        'q_order' => 6,
                        'q_required' => 1,
                        'q_min' => 0,
                        'q_max' => 0,
                        'q_other' => 0,
                        'q_other_title' => '',
                        'q_field' => '',
                    ],
                ];
                // 写入调研问题默认数据
                $this->question_s->insert_all($add_questions);

                $this->base_info_s->commit();
            } catch (Exception $e) {
                // 回滚数据
                $this->base_info_s->rollback();
            }
        }

        return true;
    }
}