InstallController.class.php 13.5 KB
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 313 314 315 316 317 318 319 320 321 322 323 324 325 326 327 328 329 330 331 332 333 334 335 336 337 338 339 340 341 342 343 344 345 346 347 348 349 350 351 352 353 354 355 356 357 358 359 360 361 362 363 364 365 366 367 368 369 370 371 372 373 374 375 376 377 378 379 380 381 382 383 384 385 386 387 388 389 390 391 392 393 394 395 396 397 398 399 400 401 402 403 404 405 406 407 408 409 410 411 412 413 414 415 416 417 418 419 420 421 422 423 424 425 426 427 428 429
<?php
/**
 * 套件应用安装初始化数据
 * 任务中心
 * @author yingcai
 * @date 2017-11-23 10:48:30
 */

namespace Rpc\Controller\Common;

use Common\Service\CustomtaskService;
use Common\Service\CustomtaskRightService;
use Common\Service\CustomtaskContentService;
use Think\Db;
use Think\Log;

class InstallController extends AbstractController
{
    private $qyDomain = QY_DOMAIN;
    private $nowTime = MILLI_TIME;

    /** @var CustomtaskService  */
    protected $customtask;
    /** @var CustomtaskContentService  */
    protected $customtask_content;
    /** @var CustomtaskRightService  */
    protected $customtask_rignt;

    public function before_action($action = '')
    {
        if (!parent::before_action($action)) {
            return false;
        }

        $this->customtask = new CustomtaskService();
        $this->customtask_rignt = new CustomtaskRightService();
        $this->customtask_content = new CustomtaskContentService();

        return true;
    }

    public function Index()
    {
        // 安装任务基础数据
        $customtask_id = $this->install_customtask_data();

        if ($customtask_id) {
            // 安装任务内容
            $this->install_customtask_content_data($customtask_id);
        }

        return true;
    }

    /**
     * 安装常规任务基本信息
     *
     * @return int
     */
    private function install_customtask_data()
    {
        $customtask_id = 0;
        // 是否已经有默认数据
        $default_data = $this->customtask->get_by_conds([]);
        if (!$default_data) {

            // 获取任务应参与人数
            $uids = $this->customtask_rignt->getUidsByRight([1=>[2]]);

            $customtask_id = $this->customtask->insert([
                'ea_name' => '员圈小助手',
                'task_name' => '【系统预设】培训系统不会用?3分钟教会你',
                'description' => '通过常规任务的规划,可以把员工每个阶段需要学习掌握的内容串联起来,帮助员工更好的学习成长。预设内容旨在帮助使用者快速了解、熟悉【员圈】培训系统的使用方法。',
                'start_time' => 1506787200000,
                'end_time' => 1917014400000,
                'is_reward' => 1,
                'reward_setting' => '',
                'is_notice' => 2,
                'user_total' => count($uids),
                'task_status' => 3,
                'send_time' => MILLI_TIME,
                'update_time' => MILLI_TIME,
            ]);

            // 任务权限入库
            if ($customtask_id) {

                $this->customtask_rignt->insert([
                    'customtask_id' => $customtask_id,
                    'obj_type' => 1,
                    'obj_id' => 2,
                ]);
            }
        }

        return $customtask_id;
    }

    /**
     * 安装常规任务内容
     *
     * @return bool
     */
    private function install_customtask_content_data($customtask_id)
    {
        $content = [];

        $course = $this->add_task_course();
        $paper = $this->add_task_exam();
        $question = $this->add_task_question();
        $activity = $this->add_task_activiey();

        if ($course) {
            $content[] = [
                'customtask_id' => $customtask_id,
                'app' => 'course',
                'app_data_id' => $course['course_id'],
                'title' => $course['title'],
                'order' => 1,
            ];
        }

        if ($paper) {
            $content[] = [
                'customtask_id' => $customtask_id,
                'app' => 'exam',
                'app_data_id' => $paper['ep_id'],
                'title' => $paper['title'],
                'order' => 2,
            ];
        }

        if ($activity) {
            $content[] = [
                'customtask_id' => $customtask_id,
                'app' => 'activity',
                'app_data_id' => $activity['ac_id'],
                'title' => $activity['title'],
                'order' => 4,
            ];
        }

        if ($question) {
            $content[] = [
                'customtask_id' => $customtask_id,
                'app' => 'questionnaire',
                'app_data_id' => $question['qu_id'],
                'title' => $question['title'],
                'order' => 3,
            ];
        }

        $this->customtask_content->insert_all($content);

        return true;
    }


    /**
     * 添加任务类课程数据
     *
     * @return bool|array
     */
    private function add_task_course()
    {
        $db = &Db::getInstance();

        $course_sql = "select * from `oa_course_article` WHERE `article_title` = '【系统预设】员工如何进行课程学习' and `domain` = '{$this->qyDomain}'  and `status` < 3";
        $courseData = $db->query($course_sql);
        $courseData = $courseData[0];
        if (empty($courseData)) {
            Log::record('---Add Course fail info:没有找到课程默认信息', Log::ERR);
            return true;
        }

        $courseData['course_type'] = 2;
        unset($courseData['article_id']);

        $sql = $this->_formatInsert('oa_course_article', $courseData);
        $this->_exeSql($sql);
        $article_id = Db::getInstance()->getLastLinkID()->lastInsertId();

        // 章节
        $chapterData = [
            'article_id' => $article_id,
            'parent_id' => 0,
            'source_id' => 0,
            'chapter_name' => '章节',
            'et_ids' => '',
            'rand_num' => 0,
            'order' => 1,
            'domain' => $this->qyDomain,
            'status' => 1,
            'created' => $this->nowTime,
        ];
        $courseChapterSql = $this->_formatInsert('oa_course_article_chapter', $chapterData);
        $this->_exeSql($courseChapterSql);

        // 子章节数据
        $chapterSql = "select * from `oa_course_article_chapter` WHERE `article_id` = '{$article_id}' AND `domain` = '{$this->qyDomain}'  and `status` < 3";
        $chapterInfo = $db->query($chapterSql);
        if (empty($chapterInfo)) {
            Log::record('---Add Course fail info:没有找到章节数据', Log::ERR);
            return true;
        }
        $sourceSql = "select * from `oa_course_source` WHERE `source_title` = '员工如何进行课程学习' AND `domain` = '{$this->qyDomain}'  and `status` < 3";
        $sourceInfo = $db->query($sourceSql);
        if (empty($sourceInfo)) {
            Log::record('---Add Course fail info:没有找到素材数据', Log::ERR);
            return true;
        }
        $chapterChildData = [
            'article_id' => $article_id,
            'parent_id' => $chapterInfo[0]['article_chapter_id'],
            'source_id' => $sourceInfo[0]['source_id'],
            'chapter_name' => '章节',
            'et_ids' => '',
            'rand_num' => 0,
            'order' => 1,
            'domain' => $this->qyDomain,
            'status' => 1,
            'created' => $this->nowTime,
        ];
        $chapterChildSql = $this->_formatInsert('oa_course_article_chapter', $chapterChildData);
        $this->_exeSql($chapterChildSql);
        // 课程权限数据
        $rightData = [
            'class_id' => $courseData['class_id'],
            'article_id' => $article_id,
            'award_id' => 0,
            'obj_type' => 1,
            'obj_id' => '',
            'domain' => $this->qyDomain,
            'status' => 1,
            'created' => $this->nowTime,
        ];
        $rightSql = $this->_formatInsert('oa_course_right', $rightData);
        $this->_exeSql($rightSql);

        return ['course_id' => $article_id, 'title' => $courseData['article_title']];
    }

    /**
     * 添加任务类考试数据
     *
     * @return bool|array
     */
    private function add_task_exam()
    {
        $db = &Db::getInstance();
        // 获取默认分类
        $paperSql = "select * from `oa_exam_paper` WHERE `ep_name` = '【系统预设】考试基础功能测试' and `domain` = '{$this->qyDomain}'  and `status` < 3";
        $paper = $db->query($paperSql);
        $paper = $paper[0];
        if (empty($paper)) {
            Log::record('---Add Course fail info:没有找到考试默认信息', Log::ERR);
            return true;
        }

        // 获取默认题库下的题目
        $topic_field = 'et_id,et_type,title,title_pic,score,options,answer,answer_resolve,answer_coverage,match_type,answer_keyword';
        $topicSql = "select {$topic_field} from `oa_exam_topic` WHERE `eb_id` = {$paper['bank_data']} and `domain` = '{$this->qyDomain}'  and `status` < 3";
        $topic_list = $db->query($topicSql);

        if (empty($topic_list)) {
            Log::record('---Add Course fail info:没有找到题库题目信息', Log::ERR);
            return true;
        }

        $paper['exam_type'] = 2;
        $paper['begin_time'] = 0;
        $paper['end_time'] = 0;
        $paper['is_recommend'] = 0;
        unset($paper['ep_id']);

        $paperSql = $this->_formatInsert('oa_exam_paper', $paper);
        $this->_exeSql($paperSql);
        $ep_id = Db::getInstance()->getLastLinkID()->lastInsertId();

        // 试卷总分
        $total_score = 0;

        // 自主选题,分数按照题目分数计算
        foreach ($topic_list as $k => &$v) {
            $total_score += intval($v['score']);
            $v['ep_id'] = $ep_id;
            // 组装试题序号
            $v['order_num'] = $k + 1;
            $v['domain'] = $this->qyDomain;
            $v['created'] = $this->nowTime;
            // 执行数据插入操作
            $snapshotSql = $this->_formatInsert('oa_exam_snapshot', $v);
            $this->_exeSql($snapshotSql);
        }

        return ['ep_id' => $ep_id, 'title' => $paper['ep_name']];
    }

    /**
     * 添加任务类调研数据
     *
     * @return bool|array
     */
    private function add_task_question()
    {
        $db = &Db::getInstance();

        // 获取默认调研
        $quSql = "select * from `oa_questionnaire_baseinfo` WHERE `title` = '【系统预设】企业培训情况调研' and `domain` = '{$this->qyDomain}'  and `status` < 3";
        $qu_data = $db->query($quSql);
        $qu_data = $qu_data[0];
        if (empty($qu_data)) {
            Log::record('---Add Course fail info:没有找到默认调研信息', Log::ERR);
            return true;
        }

        // 获取默认调研问题数据
        $question_sql = "select * from `oa_questionnaire_question` WHERE `qu_id` = {$qu_data['qu_id']} and `domain` = '{$this->qyDomain}'  and `status` < 3";
        $questions = $db->query($question_sql);

        // 写入调研基本信息默认数据
        $qu_data['qu_type'] = 2;
        $qu_data['deadline'] = 0;
        unset($qu_data['qu_id']);

        $quSql = $this->_formatInsert('oa_questionnaire_baseinfo', $qu_data);
        $this->_exeSql($quSql);
        $qu_id = Db::getInstance()->getLastLinkID()->lastInsertId();

        // 写入调研问题默认数据
        foreach ($questions as $v) {
            unset($v['qid']);
            $v['qu_id'] = $qu_id;
            // 执行数据插入操作
            $questionSql = $this->_formatInsert('oa_questionnaire_question', $v);
            $this->_exeSql($questionSql);
        }

        return ['qu_id' => $qu_id, 'title' => $qu_data['title']];
    }

    /**
     * 添加任务类活动数据
     *
     * @return bool|array
     */
    private function add_task_activiey()
    {
        $db = &Db::getInstance();
        // 获取活动默认数据
        $activitySql = "select * from `oa_activity_activity` WHERE `subject` = '【系统预设】真假学霸PK│快来晒出你的考试成绩!' and `domain` = '{$this->qyDomain}'  and `status` < 3";
        $activity = $db->query($activitySql);
        $activity = $activity[0];
        if (empty($activity)) {
            Log::record('---Add Course fail info:没有找到默认活动信息', Log::ERR);
            return true;
        }

        // 添加任务类活动数据
        $activity['activity_type'] = 2;
        $activity['begin_time'] = 0;
        $activity['end_time'] = 0;
        $activity['is_recomend'] = 0;
        unset($activity['ac_id']);

        $task_activity_sql = $this->_formatInsert('oa_activity_activity', $activity);
        $this->_exeSql($task_activity_sql);
        $ac_id = Db::getInstance()->getLastLinkID()->lastInsertId();

        return ['ac_id' => $ac_id, 'title' => $activity['subject']];
    }


    /**
     * 执行sql语句
     * @param $sql
     */
    private function _exeSql($sql)
    {
        if (empty($sql)) {
            return ;
        }

        $db = Db::getInstance();
        try {
            return $db->query($sql);
        } catch (\Exception $e) {

        }
    }

    /**
     * 根据数组数据格式化为添加sql语句
     * @author tangxingguo
     * @param string $tableName 表名
     * @param array $data 数据
     * @return string|bool
     */
    private function _formatInsert($tableName, $data)
    {
        if (empty($data) && !is_array($data)) {
            return true;
        }

        // 拼接sql
        $sql = "INSERT INTO `{$tableName}` ";
        $keys = "(";
        $values = "(";
        foreach ($data as $k => $v) {
            if (array_slice($data, -1, 1) == [$k => $v]) {
                $symbol = "";
            } else {
                $symbol = ",";
            }
            $keys .= "`{$k}`{$symbol} ";
            if (is_numeric($v)) {
                $values .= "{$v}{$symbol} ";
            } else {
                $values .= "'{$v}'{$symbol} ";
            }
        }
        $keys .= ") VALUES ";
        $values .= ");";

        $sql .= $keys . $values;

        return $sql;
    }
}